Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.PlatformNotSupportedException' occurred in System.Security.Cryptography.Algorithms.dll #27276

Closed
jkotas opened this issue Aug 30, 2018 · 8 comments

Comments

@jkotas
Copy link
Member

jkotas commented Aug 30, 2018

From @furoraest on August 30, 2018 17:44

Hope this is the right repo for this problem:

When trying to export from ECDiffieHellman to ECDiffieHellmanPublicKey.ToByteArray() the exception rises:
Exception has occurred: CLR/System.PlatformNotSupportedException
An exception of type 'System.PlatformNotSupportedException' occurred in System.Security.Cryptography.Algorithms.dll but was not handled in user code: 'Operation is not supported on this platform.' at System.Security.Cryptography.ECDiffieHellmanImplementation.ECDiffieHellmanOpenSslPublicKey.ToByteArray()

.NET Core SDK (reflecting any global.json):
Version: 2.1.401
Commit: 91b1c13032

Runtime Environment:
OS Name: ubuntu
OS Version: 18.04
OS Platform: Linux
RID: ubuntu.18.04-x64
Base Path: /usr/share/dotnet/sdk/2.1.401/

Host (useful for support):
Version: 2.1.3
Commit: 124038c13e

.NET Core SDKs installed:
2.1.401 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

When looking at the data stucture the value of ECDH key is presented as type:
Microsoft.Win32.SafeHandles.SafeEcKeyHandle

Perhaps this is the problem?
Method description lists net core 2.0 and 2.1
https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.ecdiffiehellmanpublickey.tobytearray?view=netframework-4.7.2

Best regards

Copied from original issue: dotnet/coreclr#19767

@IcanBENCHurCAT
Copy link

Trying to look into this (so I can learn a bit more), but haven't found anything definitive yet.

I see the ECDiffieHellmanSecurityTransforms will throw this exception when calling ExportExplicitParameters. I see this function being called in plenty of tests that I assume have run on a Linux machine before.

I'm assuming that there is another class that finishes this implementation that I am not seeing. I see there is a factory object that should be doing this, but I am missing some symbols to look further. Working on getting this project to build, and then I will dig deeper if nobody else has.

@bartonjs
Copy link
Member

ECDiffieHellmanPublicKey.ToByteArray() doesn't have a (standards-)defined export format. The version used by ECDiffieHellmanPublicKeyCng is Windows-specific, and only works for NIST P-256 (secp256r1), NIST P-384 (secp384r1), and NIST P-521 (secp521r1).

To create a copy of a key you should use the ExportParameters() method to obtain the rich ECParameters object; which can then be sent through ECDiffieHellman.Create(ECParameters) and the PublicKey property of that object be read to get back a second instance of ECDiffieHellmanPublicKey.

@danmoseley
Copy link
Member

is there something we can do to make the exception more helpful?

@bartonjs
Copy link
Member

@danmosemsft I'm not sure that a resource string of "This method is only available on Windows" would really help. If we ever made the Roslyn analyzer to warn about methods like this, and this didn't already get added, we should add it there. But I don't know what precedent we have other than just throw a default PNSE.

@danmoseley
Copy link
Member

@bartonjs I guess I was thinking that the message could potentially say something like "To create a copy of a key you should use the ExportParameters() method to obtain the rich ECParameters object; which can then be sent through ECDiffieHellman.Create(ECParameters) and the PublicKey property of that object be read to get back a second instance of ECDiffieHellmanPublicKey."

(I don't know)

@furoraest
Copy link

Main problem was to get serialized output from (public) key....
I am very new on .net core architecture but seems there are more similar cases where platform is not supported... Rewriting some base classes would bad idea/ too much work?

@filipnavara
Copy link
Member

filipnavara commented Sep 6, 2018

@furoraest The problem is that the subset of algorithms and parameters supported on Windows and Unix (using OpenSSL) is different. The format of the output BLOB returned by ToByteArray is internal representation used by Windows, which doesn't necessarily support all algorithms supported on Unix.

It would be possible to implement certain subset, but it's likely not worth it since the code for loading the byte array and re-creating it uses *Cng class, which is Windows specific and would thus need a user code modification anyway.

You may be interested in the new APIs in #22020, particularly ExportSubjectPublicKeyInfo.

@furoraest
Copy link

Thank You for hints, it helped me develop platform-independent code and get it done without CNG.
Since I discovered the ECParameters capability (fourtunately it consists the D (private key) and Q(public key) part which are readable and therefore derivable to other formats), there are various ways to around - unfortunately currently there are much handwork for converting (like importing public keys as byte arrays and convert them to ECParameters, no direct method to derive public key straightforward from imported private key etcetc.)
Anyway, I wait to see when the new API will be out, much can be resolved by that.
Still, lot of things need to be done with the crypto part yet, I would like to see full Inferno and new API support included, the old BouncyCastle is clumsy and I hear in some enterprises forbidden, also some compatibility with "industry standard" (like java KeyAgreement) would be also much appreciated.
Thank You for explanations, I think its safe to close it now.

@bartonjs bartonjs closed this as completed Sep 7, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants