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

Broken space handling in Common Name since version 2.3.0 #542

Open
mguzlews opened this issue Jun 3, 2024 · 1 comment
Open

Broken space handling in Common Name since version 2.3.0 #542

mguzlews opened this issue Jun 3, 2024 · 1 comment

Comments

@mguzlews
Copy link

mguzlews commented Jun 3, 2024

Hello,

starting from the version 2.3.0 (last working version 2.2.1) spaces are removed from the beginning of the Common Name (X509Name). I don't know if this change is intentional but a bunch of our logic requires spaces at the beginning of the common name for padding purposes.

Current behavior: "CN= Test" becomes "CN=Test" in the certificate
Expected behavior: "CN= Test" stays "CN= Test"

Could you restore previous space handling behavior?
Thanks in advance

Demo:

 public static void Main()
 {
     const string commonName = "CN= Test"; // Common Name with space at the beginning

     var keyPairGenerator = new RsaKeyPairGenerator();
     keyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
     AsymmetricCipherKeyPair keyPair = keyPairGenerator.GenerateKeyPair();

     var gen = new X509V3CertificateGenerator();
     var CN = new X509Name(commonName);
     gen.SetSubjectDN(CN);
     gen.SetIssuerDN(CN);
     gen.SetSerialNumber(Org.BouncyCastle.Math.BigInteger.One);
     gen.SetNotBefore(DateTime.UtcNow.Date);
     gen.SetNotAfter(DateTime.UtcNow.Date.AddYears(1));
     gen.SetPublicKey(keyPair.Public);

     var signatureFactory = new Asn1SignatureFactory("SHA256WithRSA", keyPair.Private);
     var certificate = gen.Generate(signatureFactory);

     // Check if Common Name is correct
     if (certificate.SubjectDN.ToString() != commonName)
     {
         throw new Exception($"Incorrect common name, expected {commonName}, got {certificate.SubjectDN}");
     }
 }
@mguzlews
Copy link
Author

mguzlews commented Jul 2, 2024

Found workaround

string commonName = " Test";
var CN = new X509Name(new List<DerObjectIdentifier>() { X509Name.CN }, new List<string>() { commonName });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant