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

Remove parameters in AlgorithmIdentifier for 25519 series algorithm

Open universorum opened this issue 2 years ago • 1 comments

For X25519/X448/Ed25519/Ed448, there is not algorithm parameters.

For all of the OIDs, the parameters MUST be absent. --EFC8410 Section 3

For now, BouncyCastle will output NULL parameter for cert. This PR set X25519/X448/Ed25519/Ed448 to noParams to avoid parameter field.

using Org.BouncyCastle.Asn1.EdEC;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;

var pair25519Generator = new Ed25519KeyPairGenerator();
pair25519Generator.Init(new Ed25519KeyGenerationParameters(new SecureRandom()));

var certGenerator = new X509V3CertificateGenerator();
certGenerator.SetIssuerDN(new X509Name("CN=localhost"));
certGenerator.SetSubjectDN(new X509Name("CN=localhost"));
certGenerator.SetNotBefore(DateTime.UtcNow);
certGenerator.SetNotAfter(DateTime.UtcNow.AddYears(1));
certGenerator.SetSerialNumber(BigInteger.ProbablePrime(120, new Random()));

var pair = pair25519Generator.GenerateKeyPair();
certGenerator.SetPublicKey(pair.Public);

var signer = new Asn1SignatureFactory(EdECObjectIdentifiers.id_Ed25519.Id, pair.Private);
var cert   = certGenerator.Generate(signer);
var encode = cert.GetEncoded();
Console.WriteLine(Convert.ToBase64String(encode));

Before

MIHnMIGYoAMCAQICEAClAs992GvabIjEIaAbJvMwBwYDK2VwBQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIzMDkyMDA4NDM0N1oXDTI0MDkyMDA4NDM0N1owFDESMBAGA1UEAwwJbG9jYWxob3N0MCowBQYDK2VwAyEAawxw2qSkNUfm2ggoIguzQf7yu0A2IKOpOlc8v7v7luwwBwYDK2VwBQADQQAOM9fsLW78bql8WTZhDP+WYqKrtPAOhDBsa4Ap46TQYQk/Vq6nHkr6ELZl4+xBT0OIlcxUotfDjIQeZFlk1rwH
signature AlgorithmIdentifier SEQUENCE (2 elem)
      algorithm OBJECT IDENTIFIER 1.3.101.112 curveEd25519 (EdDSA 25519 signature algorithm)
      parameters ANY NULL

After

MIHjMIGWoAMCAQICEAC7Ztip5kbvbu1ftXMA/80wBQYDK2VwMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0yMzA5MjAwODQ0NTFaFw0yNDA5MjAwODQ0NTFaMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDAqMAUGAytlcAMhAPA/v+yUYcHNLmKk7JaLyMAnyyVpn+f4FhJjHPxwtn9uMAUGAytlcANBAAytbHcymU4DUVd2gEVczfFdrsnKQFjqD0STlWAejgJ+bElQ3usZNhNwyaq1KNs+0eSLC8Jzp7jh5+AixVBpAgY=
signature AlgorithmIdentifier SEQUENCE (1 elem)
      algorithm OBJECT IDENTIFIER 1.3.101.112 curveEd25519 (EdDSA 25519 signature algorithm)

universorum avatar Sep 20 '23 08:09 universorum

I've added the entries for id_Ed25519 and id_Ed448 (also to Pkcs10CertificationRequest for certificate requests). X25519, X448 aren't relevant here; their AlgorithmIdentifier is correctly encoded without parameters where it does matter in PrivateKeyInfoFactory and SubjectPublicKeyInfoFactory.

peterdettman avatar Jan 28 '24 13:01 peterdettman