core icon indicating copy to clipboard operation
core copied to clipboard

Unhandled Exception: System.PlatformNotSupportedException: Windows Cryptography Next Generation (CNG) is not supported on this platform. at System.Security.Cryptography.ECDsaCng..ctor()

Open shmisra opened this issue 8 years ago • 7 comments

Issue Title

CNG is NOT supported in LINUX. Is that true? Exception Details: Unhandled Exception: System.PlatformNotSupportedException: Windows Cryptography Next Generation (CNG) is not supported on this platform. at System.Security.Cryptography.ECDsaCng..ctor()

General

Here is what I did:

Created a .NET Core project with the code as shown below:

using System; using System.IO; using System.Security.Cryptography; using System.Text;

namespace Core1 { class Alice { public static void Main(string[] args) { Bob bob = new Bob(); using (ECDsaCng dsa = new ECDsaCng()) { dsa.HashAlgorithm = CngAlgorithm.Sha256; bob.key = dsa.Key.Export(CngKeyBlobFormat.EccPublicBlob);

            byte[] data = new byte[] { 21, 5, 8, 12, 207 };

            byte[] signature = dsa.SignData(data);

            bob.Receive(data, signature);
        }
    }
}

public class Bob
{
    public byte[] key;

    public void Receive(byte[] data, byte[] signature)
    {
        using (ECDsaCng ecsdKey = new ECDsaCng(CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob)))
        {
            // set hash algorithm manually here
            ecsdKey.HashAlgorithm = CngAlgorithm.Sha256;
            if (ecsdKey.VerifyData(data, signature))
                Console.WriteLine("Data is good");
            else
                Console.WriteLine("Data is bad");
        }
    }
}

}

The above code failed to identify ECDsaCng. I had to reference the Nuget package System.Security.Cryptography.Cng from here https://www.nuget.org/packages/System.Security.Cryptography.Cng/

System.Security.Cryptography.Cng - Latest Stable Version: 4.5.0

Provides cryptographic algorithm implementations and key management with Windows Cryptographic Next Generation API (CNG).

Commonly Used Types: System.Security.Cryptography.RSACng System.Security.Cryptography.ECDsaCng System.Security.Cryptography.CngKey

30ab651fcb4354552bd4891619a0bdd81e0ebdbf When using NuGet 3.x this package requires at least version 3.4.

Link: https://www.nuget.org/packages/System.Security.Cryptography.Cng/

GET https://api.nuget.org/v3/registration3-gz-semver2/system.security.cryptography.cng/index.json OK https://api.nuget.org/v3/registration3-gz-semver2/system.security.cryptography.cng/index.json 1165ms Restoring packages for E:\LINUXTEST\Core1\Core1\Core1.csproj... GET https://api.nuget.org/v3-flatcontainer/system.security.cryptography.cng/index.json OK https://api.nuget.org/v3-flatcontainer/system.security.cryptography.cng/index.json 1126ms GET https://api.nuget.org/v3-flatcontainer/system.security.cryptography.cng/4.5.0/system.security.cryptography.cng.4.5.0.nupkg OK https://api.nuget.org/v3-flatcontainer/system.security.cryptography.cng/4.5.0/system.security.cryptography.cng.4.5.0.nupkg 983ms Installing System.Security.Cryptography.Cng 4.5.0. Installing NuGet package System.Security.Cryptography.Cng 4.5.0. Committing restore... Writing lock file to disk. Path: E:\LINUXTEST\Core1\Core1\obj\project.assets.json Restore completed in 3.55 sec for E:\LINUXTEST\Core1\Core1\Core1.csproj. Successfully installed 'System.Security.Cryptography.Cng 4.5.0' to Core1 Executing nuget actions took 721.03 ms Time Elapsed: 00:00:05.5506996 ========== Finished ==========

Post this the code builds & runs fine. Tested on Windows 10 OS. The code runs fine. I now, used pscp.exe, to port the code to Ubuntu 18.04 LINUX OS. Here is the command & its output:

E:\LINUXTEST\Core1>E:\LINUXTEST\pscp.exe -r * [email protected]:/home/busure/TESTCNG

[email protected]'s password: .suo | 36 kB | 36.0 kB/s | ETA: 00:00:00 | 100% pscp: .vs/Core1/v15/Server/sqlite3/db.lock: Cannot open file

pscp: .vs/Core1/v15/Server/sqlite3/storage.ide: Cannot open file

pscp: .vs/Core1/v15/Server/sqlite3/storage.ide-shm: Cannot open file

pscp: .vs/Core1/v15/Server/sqlite3/storage.ide-wal: Cannot open file

Core1.deps.json | 1 kB | 1.2 kB/s | ETA: 00:00:00 | 100% Core1.dll | 5 kB | 5.5 kB/s | ETA: 00:00:00 | 100% Core1.pdb | 0 kB | 0.8 kB/s | ETA: 00:00:00 | 100% Core1.runtimeconfig.dev.j | 0 kB | 0.2 kB/s | ETA: 00:00:00 | 100% Core1.runtimeconfig.json | 0 kB | 0.2 kB/s | ETA: 00:00:00 | 100% Core1.csproj | 0 kB | 0.3 kB/s | ETA: 00:00:00 | 100% Core1.csproj.nuget.cache | 0 kB | 0.1 kB/s | ETA: 00:00:00 | 100% Core1.csproj.nuget.g.prop | 1 kB | 1.5 kB/s | ETA: 00:00:00 | 100% Core1.csproj.nuget.g.targ | 0 kB | 1.0 kB/s | ETA: 00:00:00 | 100% Core1.AssemblyInfo.cs | 0 kB | 1.0 kB/s | ETA: 00:00:00 | 100% Core1.AssemblyInfoInputs. | 0 kB | 0.0 kB/s | ETA: 00:00:00 | 100% Core1.csproj.CoreCompileI | 0 kB | 0.0 kB/s | ETA: 00:00:00 | 100% Core1.csproj.FileListAbso | 0 kB | 0.8 kB/s | ETA: 00:00:00 | 100% Core1.csprojAssemblyRefer | 474 kB | 474.7 kB/s | ETA: 00:00:00 | 100% Core1.dll | 5 kB | 5.5 kB/s | ETA: 00:00:00 | 100% Core1.pdb | 0 kB | 0.8 kB/s | ETA: 00:00:00 | 100% project.assets.json | 42 kB | 42.3 kB/s | ETA: 00:00:00 | 100% Program.cs | 1 kB | 1.2 kB/s | ETA: 00:00:00 | 100% Core1.sln | 1 kB | 1.1 kB/s | ETA: 00:00:00 | 100%

E:\LINUXTEST\Core1>

I ran the code on Ubuntu 18.04 LINUX OS, to get the following output:

busure@Ubuntu1804:~/TESTCNG/Core1$ dotnet run

Unhandled Exception: System.PlatformNotSupportedException: Windows Cryptography Next Generation (CNG) is not supported on this platform. at System.Security.Cryptography.ECDsaCng..ctor() at Core1.Alice.Main(String[] args) in /home/busure/TESTCNG/Core1/Program.cs:line 13

Questions:

  1. Is CNG supported on .NET Core for LINUX OS? I am using UBUNTU 18.04.
  2. Is there a way to make the above code run on UBUNTU LINUX?

shmisra avatar Sep 06 '18 03:09 shmisra

It is only supported on Windows. It's a thin wrapper over a windows only crypto library.

Petermarcu avatar Sep 06 '18 03:09 Petermarcu

The ECDSA algorithm is supported cross-platform, via ECDsa.Create(); but ECDsaCng is Windows-only.

The CNG-specific key export and import isn't available for other platforms, but current builds out of master support exporting and importing keys in the SubjectPublicKeyInfo format. Current released builds support importing and exporting keys via the ECParameters structure (and the ImportParameters and ExportParameters methods), since .NET Core 1.0 and .NET Framework 4.6.2.

bartonjs avatar Sep 06 '18 05:09 bartonjs

I found hint that some of crypto functionalities is planned for 3.0 is this true or we can expect them to reach to 2.x also?

furoraest avatar Sep 06 '18 05:09 furoraest

Thank you all for the info.

shmisra avatar Sep 06 '18 09:09 shmisra

There is always more to do in crypto. We will continue to bring new functionality in newer releases. I don't expect new features to go into 2.X unless critical.

Petermarcu avatar Sep 14 '18 15:09 Petermarcu

what's the alternative , all my work is used using ECDsaCng !!

revoltez avatar May 03 '20 12:05 revoltez

@revoltez Use ECDsa.Create() and the ECDsa base class. Unless you're doing named key interop work, which is only available on Windows, the base class can do everything.

bartonjs avatar May 03 '20 17:05 bartonjs