From c4edc700b5c68cf4e05a22e83513a39d3de9636a Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 4 Apr 2023 13:10:44 +0700 Subject: Add constructor from template certificate --- crypto/src/x509/X509V3CertificateGenerator.cs | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crypto/src/x509/X509V3CertificateGenerator.cs b/crypto/src/x509/X509V3CertificateGenerator.cs index ee35b9479..d5c9ffe92 100644 --- a/crypto/src/x509/X509V3CertificateGenerator.cs +++ b/crypto/src/x509/X509V3CertificateGenerator.cs @@ -26,6 +26,39 @@ namespace Org.BouncyCastle.X509 tbsGen = new V3TbsCertificateGenerator(); } + /// Create a generator for a version 3 certificate, initialised with another certificate. + /// Template certificate to base the new one on. + public X509V3CertificateGenerator(X509Certificate template) + : this(template.CertificateStructure) + { + } + + public X509V3CertificateGenerator(X509CertificateStructure template) + { + tbsGen = new V3TbsCertificateGenerator(); + tbsGen.SetSerialNumber(template.SerialNumber); + tbsGen.SetIssuer(template.Issuer); + tbsGen.SetStartDate(template.StartDate); + tbsGen.SetEndDate(template.EndDate); + tbsGen.SetSubject(template.Subject); + tbsGen.SetSubjectPublicKeyInfo(template.SubjectPublicKeyInfo); + + var extensions = template.TbsCertificate.Extensions; + + foreach (var oid in extensions.ExtensionOids) + { + if (X509Extensions.SubjectAltPublicKeyInfo.Equals(oid) || + X509Extensions.AltSignatureAlgorithm.Equals(oid) || + X509Extensions.AltSignatureValue.Equals(oid)) + { + continue; + } + + X509Extension ext = extensions.GetExtension(oid); + extGenerator.AddExtension(oid, ext.critical, ext.Value.GetOctets()); + } + } + /// /// Reset the Generator. /// -- cgit 1.4.1