Enhance PKCS#8 support: add .NET X509Certificate2 interop and fix RSA…#670
Enhance PKCS#8 support: add .NET X509Certificate2 interop and fix RSA…#670KonradSop wants to merge 1 commit intobcgit:masterfrom
Conversation
| var bcCert = FromX509Certificate(certificate); | ||
| return SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(bcCert.GetPublicKey()); |
There was a problem hiding this comment.
From .NET 6.0 could use:
return SubjectPublicKeyInfo.GetInstance(certificate.PublicKey.ExportSubjectPublicKeyInfo());
| /// <exception cref="ArgumentNullException">If <paramref name="certificate"/> is null.</exception> | ||
| public static byte[] GetSubjectPublicKeyInfoDer(SystemX509.X509Certificate2 certificate) | ||
| { | ||
| return GetSubjectPublicKeyInfo(certificate).GetEncoded(Asn1Encodable.Der); |
There was a problem hiding this comment.
From .NET 6.0 could use:
return certificate.PublicKey.ExportSubjectPublicKeyInfo();
| new X509Certificate(x509Cert.RawData); | ||
|
|
||
| /// <summary> | ||
| /// Extract the <see cref="SubjectPublicKeyInfo"/> (X.509 / PKCS#8) from a .NET <see cref="SystemX509.X509Certificate2"/>. |
There was a problem hiding this comment.
"PKCS#8" doesn't really belong here. SubjectPublicKeyInfo is an ASN.1 type from the X.509 standard and is used for public keys. PKCS#8 is a separate standard for private keys.
| { | ||
| /// <summary> | ||
| /// A factory to produce Public Key Info Objects. | ||
| /// A factory to produce SubjectPublicKeyInfo (X.509 / PKCS#8) objects from Bouncy Castle public key parameters. |
| /// <returns>A subject public key info object.</returns> | ||
| /// <exception cref="Exception">Throw exception if object provided is not one of the above.</exception> | ||
| /// <example> | ||
| /// Example of converting a .NET X509Certificate2 to a PKCS#8/DER byte array: |
| namespace Org.BouncyCastle.Pkcs.Tests | ||
| { | ||
| [TestFixture] | ||
| public class Pkcs8Test |
There was a problem hiding this comment.
This test name (because of the Pkcs8) and location makes little sense.
I suggest adding TestDotNetUtilitiesGetSubjectPublicKeyInfoDer to Org.BouncyCastle.Security.Tests.TestDotNetUtilities instead (include the whole test case under the #if). The other two could perhaps just be added to Org.BouncyCastle.Security.TestsTestEncodings or, if you prefer, a new test class in Org.BouncyCastle.X509.Tests.
Description
This PR addresses several issues regarding PKCS#8 and X.509 interoperability brought up in the community discussions, specifically regarding RSA key encoding and usage convenience.
Related Discussion
Changes
GetSubjectPublicKeyInfoandGetSubjectPublicKeyInfoDermethods to simplify extracting PKCS#8/X.509 public key information directly from .NETX509Certificate2objects in a single line.SubjectPublicKeyInfoFactorycorrectly includes mandatoryDER NULL (05 00)parameters for RSA AlgorithmIdentifiers (OID 1.2.840.113549.1.1.1), resolving the "missing parameter" issue some users encountered when interoping with other crypto providers.DotNetUtilities,SubjectPublicKeyInfoFactory, andPrivateKeyInfoFactory.<example>snippets for common tasks.Pkcs8Test.csto verify correct ASN.1 encoding (specifically RSA NULL parameters) and test the new utility methods across all supported .NET frameworks.Checklist before requesting a review
See also Contributing Guidelines.