-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Support loading cryptographic keys from files other than certificate PFX #22020
Comments
@yaakov-h Are you looking for
I don't know if there's an issue for the last one specifically, but it's on my radar as something important. I feel like that would solve your problems here without making things murky by having another family of classes which partially work x-plat. |
In a perfect world I'd have the ability to load an arbitrary key from a standard PEM/DER. In practice for my particular use case I need an In this case, the ability to load a known type from a known container (PKCS8 to ECDsa) would suffice. |
Not necessarily... The X509Certificate/2 constructors accept both PEM and DER encoded data, nothing says that a load-from-parameters method for PKCS#8 and SubjectPublicKeyInfo would be DER-only. |
True, I just assumed that |
@bartonjs estimates 1 week remaining |
This won't work on Windows anyways due to dotnet/corefx#24454. Will reconsider adding this when/if dotnet/corefx#20414.
Hi I would also really like some way to "easily" import and export RSA & ECDsa, public & private keys directly to and from PEM & DER files. And I end up writing allot of (hopefully error free) code that does the importing and exporting. Bonus points for allowing cryptographic operations directly on the public&private keys as I sometimes need to pass data trough a private key to "sign" the data, or pass it trough the public key to "encrypt" the data, and I cannot change that because the external api's are from other companies that just set it up that way. |
I see the PEM\DER reader\writer is tracked on dotnet/corefx#21833 |
I'm a little confused by this, since "directly on the public&private keys" is where the operations are already defined...
Yep, that's the purpose of this feature 😄. I think I'm close to happy with the shape of dependencies and data formats in local development, so this should end up with an API proposal relatively soon. |
"directly on the public&private keys" |
@KLuuKer I'm still not following. The RSA and ECDsa classes has There's the problem of how one uses the same RSA key later that they did now, with the current answer of RSAParameters / RSA.ImportParameters; and this feature to expand that to PKCS1/PKCS8/EncryptedPkcs8 import. |
@bartonjs I need todo a RSA for example when building licensing systems, or making sure you can only read config files instead of writing them (I know a hash would be better but hey I'm not the one making them) |
Oh! Yeah, that's not something we're very likely to add, partly because not all of the underlying cryptographic libraries we use are capable of doing it; sorry. |
No problem I figured as much (you need to support allot), the AsnReader\Writer is already helping allot tough :) |
Looks good, a few things:
|
This was reopened as a 2.2 porting candidate, but that release is feature locked now, so it'll just be 3.0. Since it's already submitted there, no further work required. |
I understand that this represents a thin wrapper over the Win32 CNG APIs, but it does make a whole suite of cryptographic operations simpler.
In my case, I'm trying to read an ECDSA public/private key pair out of a PKCS8 container. In .NET Framework and .NET Core on Windows, this is possible with the following:
Under macOS (and I assume Linux too), this throws a
PlatformNotSupportedException
.As per #21259, .NET Core can handle loading ECDSA keys from an X509 structure.
Would it be possible to implement the Cng suite of APIs with a backing from a non-Windows-CNG source such as OpenSSL on Linux or the Apple crypto libraries on macOS?
Failing that, are there any plans for a unified cryptography API in .NET Standard future which would enable a single API call to work across all platforms for fiddly cryptography bits such as this?
Thanks.
Feature Proposal
Add API which allows binary-encoded asymmetric cryptographic keys from standard data formats to be loaded, and add API which allows asymmetric cryptographic keys to be exported to those same standard data formats (subject to key exportability permissions).
Based on competitive landscape and other research, the following data formats are to be included:
Notably, the
ECPublicKey
type does not appear in this listing due to not finding standards which transported it without theSubjectPublicKeyInfo
wrapper.Caveats: On Linux, in particular, PEM-encoding (from the Privacy Enhanced Mail specification) is more standard. PEM, as a data format, can contain extra metadata in addition to the BER-encoded payload. Rather than add PEM-reading complications into these APIs the recommendation is to build a separate PEM reading class, which is capable of reporting on the additional metadata.
The data export methods are repeated in the API proposal, once to write to a destination
Span<byte>
(TryExport*
) and again to return abyte[]
(Export*
).Rather than do content-sniffing, these APIs are explicit as to their data format.
PKCS#8 EncryptedPrivateKeyInfo methods have two variants:
char
-based variant will generally convert the textual input to UTF-8 bytes, but PKCS12-PBE operates on the textual input as UTF16-BE (as per the specification).byte
-based variant will generally accept the input bytes as the input to the Key Derivation Function (KDF) dictated by the encryption options. Since this is not valid for PKCS12-PBE, this variant will throw when the input data was protected with PKCS12-PBE.override
strategyPKCS#8 data formats can contain additional metadata (attributes). Derived types using implementations which respect those attributes (such as Windows CNG) should defer to their underlying platform's PKCS#8 importer and exporter. Otherwise, the overrides (or initial definitions) on the algorithm-specific base classes should be sufficient (which call the existing
ImportParameters
andExportParameters
routines).Power scenario
To allow for the creation of PKCS#8 data containing custom attributes (as well as reading those attributes), a class for reading and writing PKCS#8 will also be added. In this type the default will be to make defensive copies of inputs, but options are exposed to reuse input memory when possible.
API Proposal
System.Security.Cryptography.Primitives:
System.Security.Cryptography.Algorithms (
override
methods omitted)System.Security.Cryptography.Pkcs
The text was updated successfully, but these errors were encountered: