From 21169fe008d79b1981bd8f17aeb42e78752882d2 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 18 Jan 2024 19:48:36 +0700 Subject: Lazy creation of SigAlgName --- crypto/src/x509/X509Certificate.cs | 17 ++++------ crypto/src/x509/X509Crl.cs | 21 +++++++----- crypto/src/x509/X509SignatureUtil.cs | 64 ++++++------------------------------ 3 files changed, 29 insertions(+), 73 deletions(-) diff --git a/crypto/src/x509/X509Certificate.cs b/crypto/src/x509/X509Certificate.cs index 465899042..316eaad99 100644 --- a/crypto/src/x509/X509Certificate.cs +++ b/crypto/src/x509/X509Certificate.cs @@ -58,11 +58,12 @@ namespace Org.BouncyCastle.X509 private readonly X509CertificateStructure c; //private Dictionary<> pkcs12Attributes = new Dictionary<>(); //private List<> pkcs12Ordering = new List<>(); - private readonly string sigAlgName; private readonly byte[] sigAlgParams; private readonly BasicConstraints basicConstraints; private readonly bool[] keyUsage; + private string m_sigAlgName = null; + private AsymmetricKeyParameter publicKeyValue; private CachedEncoding cachedEncoding; @@ -84,10 +85,8 @@ namespace Org.BouncyCastle.X509 try { - this.sigAlgName = X509SignatureUtilities.GetSignatureName(c.SignatureAlgorithm); - Asn1Encodable parameters = c.SignatureAlgorithm.Parameters; - this.sigAlgParams = (null == parameters) ? null : parameters.GetEncoded(Asn1Encodable.Der); + this.sigAlgParams = parameters?.GetEncoded(Asn1Encodable.Der); } catch (Exception e) { @@ -288,13 +287,11 @@ namespace Org.BouncyCastle.X509 } /// - /// A meaningful version of the Signature Algorithm. (EG SHA1WITHRSA) + /// A meaningful version of the Signature Algorithm. (e.g. SHA1WITHRSA) /// - /// A sting representing the signature algorithm. - public virtual string SigAlgName - { - get { return sigAlgName; } - } + /// A string representing the signature algorithm. + public virtual string SigAlgName => Objects.EnsureSingletonInitialized(ref m_sigAlgName, SignatureAlgorithm, + X509SignatureUtilities.GetSignatureName); /// /// Get the Signature Algorithms Object ID. diff --git a/crypto/src/x509/X509Crl.cs b/crypto/src/x509/X509Crl.cs index 5a1ce95e2..9c3e0fd03 100644 --- a/crypto/src/x509/X509Crl.cs +++ b/crypto/src/x509/X509Crl.cs @@ -59,10 +59,11 @@ namespace Org.BouncyCastle.X509 } private readonly CertificateList c; - private readonly string sigAlgName; private readonly byte[] sigAlgParams; private readonly bool isIndirect; + private string m_sigAlgName = null; + private CachedEncoding cachedEncoding; private volatile bool hashValueSet; @@ -79,10 +80,8 @@ namespace Org.BouncyCastle.X509 try { - this.sigAlgName = X509SignatureUtilities.GetSignatureName(c.SignatureAlgorithm); - Asn1Encodable parameters = c.SignatureAlgorithm.Parameters; - this.sigAlgParams = (null == parameters) ? null : parameters.GetEncoded(Asn1Encodable.Der); + this.sigAlgParams = parameters?.GetEncoded(Asn1Encodable.Der); this.isIndirect = IsIndirectCrl; } @@ -270,12 +269,14 @@ namespace Org.BouncyCastle.X509 return c.GetSignatureOctets(); } - public virtual string SigAlgName - { - get { return sigAlgName; } - } + /// + /// A meaningful version of the Signature Algorithm. (e.g. SHA1WITHRSA) + /// + /// A string representing the signature algorithm. + public virtual string SigAlgName => Objects.EnsureSingletonInitialized(ref m_sigAlgName, SignatureAlgorithm, + X509SignatureUtilities.GetSignatureName); - public virtual string SigAlgOid + public virtual string SigAlgOid { get { return c.SignatureAlgorithm.Algorithm.Id; } } @@ -285,6 +286,8 @@ namespace Org.BouncyCastle.X509 return Arrays.Clone(sigAlgParams); } + public virtual AlgorithmIdentifier SignatureAlgorithm => c.SignatureAlgorithm; + /// /// Return the DER encoding of this CRL. /// diff --git a/crypto/src/x509/X509SignatureUtil.cs b/crypto/src/x509/X509SignatureUtil.cs index 6a6c0cf2d..307d5a527 100644 --- a/crypto/src/x509/X509SignatureUtil.cs +++ b/crypto/src/x509/X509SignatureUtil.cs @@ -1,5 +1,3 @@ -using System; - using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.Nist; @@ -8,61 +6,26 @@ using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.TeleTrust; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; namespace Org.BouncyCastle.X509 { - internal class X509SignatureUtilities + internal class X509SignatureUtilities { - private static readonly Asn1Null derNull = DerNull.Instance; - - internal static void SetSignatureParameters( - ISigner signature, - Asn1Encodable parameters) - { - if (parameters != null && !derNull.Equals(parameters)) - { - // TODO Put back in -// AlgorithmParameters sigParams = AlgorithmParameters.GetInstance(signature.getAlgorithm()); -// -// try -// { -// sigParams.Init(parameters.ToAsn1Object().GetDerEncoded()); -// } -// catch (IOException e) -// { -// throw new SignatureException("IOException decoding parameters: " + e.Message); -// } -// -// if (Platform.EndsWith(signature.getAlgorithm(), "MGF1")) -// { -// try -// { -// signature.setParameter(sigParams.getParameterSpec(PSSParameterSpec.class)); -// } -// catch (GeneralSecurityException e) -// { -// throw new SignatureException("Exception extracting parameters: " + e.Message); -// } -// } - } - } - - internal static string GetSignatureName( - AlgorithmIdentifier sigAlgId) + internal static string GetSignatureName(AlgorithmIdentifier sigAlgID) { - Asn1Encodable parameters = sigAlgId.Parameters; + DerObjectIdentifier sigAlgOid = sigAlgID.Algorithm; + Asn1Encodable parameters = sigAlgID.Parameters; - if (parameters != null && !derNull.Equals(parameters)) + if (parameters != null && !DerNull.Instance.Equals(parameters)) { - if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss)) + if (PkcsObjectIdentifiers.IdRsassaPss.Equals(sigAlgOid)) { RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(parameters); return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1"; } - if (sigAlgId.Algorithm.Equals(X9ObjectIdentifiers.ECDsaWithSha2)) + if (X9ObjectIdentifiers.ECDsaWithSha2.Equals(sigAlgOid)) { Asn1Sequence ecDsaParams = Asn1Sequence.GetInstance(parameters); @@ -70,21 +33,14 @@ namespace Org.BouncyCastle.X509 } } - string sigName = SignerUtilities.GetEncodingName(sigAlgId.Algorithm); - if (null != sigName) - { - return sigName; - } - - return sigAlgId.Algorithm.Id; + return SignerUtilities.GetEncodingName(sigAlgOid) ?? sigAlgOid.GetID(); } /** * Return the digest algorithm using one of the standard JCA string * representations rather than the algorithm identifier (if possible). */ - private static string GetDigestAlgName( - DerObjectIdentifier digestAlgOID) + private static string GetDigestAlgName(DerObjectIdentifier digestAlgOID) { if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOID)) { @@ -128,7 +84,7 @@ namespace Org.BouncyCastle.X509 } else { - return digestAlgOID.Id; + return digestAlgOID.GetID(); } } } -- cgit 1.4.1