bc-csharp
bc-csharp copied to clipboard
Broken space handling in Common Name since version 2.3.0
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}");
}
}