bc-csharp icon indicating copy to clipboard operation
bc-csharp copied to clipboard

[BUG] Incorrect PEM format for ECGOST3410 PrivateKey

Open heggi opened this issue 1 year ago • 0 comments

Describe the Bug

The saved private key cannot be read by library and not correctly parsed by asn1 parsers

To Reproduce

var generator = GeneratorUtilities.GetKeyPairGenerator("ECGOST3410");
generator.Init(new ECKeyGenerationParameters(CryptoProObjectIdentifiers.GostR3410x2001CryptoProB, new SecureRandom()));
var gostKeyPair = generator.GenerateKeyPair();

using var writer = new StringWriter();
using var pemWriter = new PemWriter(writer);
pemWriter.WriteObject(keyPair.Private);
pemWriter.Writer.Flush();
var pem = writer.ToString(); 
// ^^^ Here PEM with `EC PRIVATE KEY` header, but can't correctly decoded by asn1 parsers (such as https://lapo.it/asn1js)

using var pemReader = new PemReader(new StringReader(pem));
var pemObj = pemReader.ReadObject() as AsymmetricKeyParameter ; 
// ^^^ Here got error
// Org.BouncyCastle.OpenSsl.PemException : problem creating EC private key: System.NullReferenceException: Object reference not set to an instance of an object.

Expected Behavior

Create PEM with PRIVATE KEY header (as it do openssl) Read saved PEM back to AsymmetricKeyParameter

Product Deployment

Please complete the following information:

  • Version [2.5.0]

Desktop

Please complete the following information:

  • OS: Windows 10

Additional Context

heggi avatar Dec 17 '24 13:12 heggi