diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-07-26 13:28:56 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-07-26 13:28:56 +0700 |
commit | a69de14e882795cc0a0c4d1e7910e08044318626 (patch) | |
tree | 3a5ab7703d2e7b8ece047496986234322f6b7e37 /crypto/src/cmp | |
parent | Refactoring around algorithm finders (diff) | |
download | BouncyCastle.NET-ed25519-a69de14e882795cc0a0c4d1e7910e08044318626.tar.xz |
Create new API for algorithm finders
Diffstat (limited to 'crypto/src/cmp')
-rw-r--r-- | crypto/src/cmp/CertificateConfirmationContent.cs | 26 | ||||
-rw-r--r-- | crypto/src/cmp/CertificateConfirmationContentBuilder.cs | 29 | ||||
-rw-r--r-- | crypto/src/cmp/CertificateStatus.cs | 39 |
3 files changed, 60 insertions, 34 deletions
diff --git a/crypto/src/cmp/CertificateConfirmationContent.cs b/crypto/src/cmp/CertificateConfirmationContent.cs index 13db89c37..262a28531 100644 --- a/crypto/src/cmp/CertificateConfirmationContent.cs +++ b/crypto/src/cmp/CertificateConfirmationContent.cs @@ -1,44 +1,52 @@ using System; using Org.BouncyCastle.Asn1.Cmp; -using Org.BouncyCastle.Cms; +using Org.BouncyCastle.Operators.Utilities; namespace Org.BouncyCastle.Cmp { public class CertificateConfirmationContent { public static CertificateConfirmationContent FromPkiBody(PkiBody pkiBody) => - FromPkiBody(pkiBody, DefaultDigestAlgorithmIdentifierFinder.Instance); + FromPkiBody(pkiBody, DefaultDigestAlgorithmFinder.Instance); public static CertificateConfirmationContent FromPkiBody(PkiBody pkiBody, - DefaultDigestAlgorithmIdentifierFinder digestAlgFinder) + IDigestAlgorithmFinder digestAlgorithmFinder) { if (!IsCertificateConfirmationContent(pkiBody.Type)) throw new ArgumentException("content of PkiBody wrong type: " + pkiBody.Type); - return new CertificateConfirmationContent(CertConfirmContent.GetInstance(pkiBody.Content), digestAlgFinder); + var content = CertConfirmContent.GetInstance(pkiBody.Content); + + return new CertificateConfirmationContent(content, digestAlgorithmFinder); } public static bool IsCertificateConfirmationContent(int bodyType) => PkiBody.TYPE_CERT_CONFIRM == bodyType; private readonly CertConfirmContent m_content; - private readonly DefaultDigestAlgorithmIdentifierFinder m_digestAlgIDFinder; + private readonly IDigestAlgorithmFinder m_digestAlgorithmFinder; public CertificateConfirmationContent(CertConfirmContent content) - : this(content, DefaultDigestAlgorithmIdentifierFinder.Instance) + : this(content, DefaultDigestAlgorithmFinder.Instance) { } + [Obsolete("Use constructor taking 'IDigestAlgorithmFinder' instead")] public CertificateConfirmationContent(CertConfirmContent content, - DefaultDigestAlgorithmIdentifierFinder digestAlgFinder) + Org.BouncyCastle.Cms.DefaultDigestAlgorithmIdentifierFinder digestAlgFinder) + : this(content, (IDigestAlgorithmFinder)digestAlgFinder) + { + } + + public CertificateConfirmationContent(CertConfirmContent content, IDigestAlgorithmFinder digestAlgorithmFinder) { m_content = content; - m_digestAlgIDFinder = digestAlgFinder; + m_digestAlgorithmFinder = digestAlgorithmFinder; } public CertConfirmContent ToAsn1Structure() => m_content; public CertificateStatus[] GetStatusMessages() => Array.ConvertAll(m_content.ToCertStatusArray(), - element => new CertificateStatus(m_digestAlgIDFinder, element)); + element => new CertificateStatus(m_digestAlgorithmFinder, element)); } } diff --git a/crypto/src/cmp/CertificateConfirmationContentBuilder.cs b/crypto/src/cmp/CertificateConfirmationContentBuilder.cs index 4178264b4..32fef908b 100644 --- a/crypto/src/cmp/CertificateConfirmationContentBuilder.cs +++ b/crypto/src/cmp/CertificateConfirmationContentBuilder.cs @@ -1,10 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cmp; -using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Cms; using Org.BouncyCastle.Math; +using Org.BouncyCastle.Operators.Utilities; using Org.BouncyCastle.Security; using Org.BouncyCastle.X509; @@ -12,18 +12,24 @@ namespace Org.BouncyCastle.Cmp { public sealed class CertificateConfirmationContentBuilder { - private readonly DefaultDigestAlgorithmIdentifierFinder m_digestAlgFinder; + private readonly IDigestAlgorithmFinder m_digestAlgorithmFinder; private readonly List<X509Certificate> m_acceptedCerts = new List<X509Certificate>(); private readonly List<BigInteger> m_acceptedReqIDs = new List<BigInteger>(); public CertificateConfirmationContentBuilder() - : this(DefaultDigestAlgorithmIdentifierFinder.Instance) + : this(DefaultDigestAlgorithmFinder.Instance) { } - public CertificateConfirmationContentBuilder(DefaultDigestAlgorithmIdentifierFinder digestAlgFinder) + [Obsolete("Use constructor taking 'IDigestAlgorithmFinder' instead")] + public CertificateConfirmationContentBuilder(Org.BouncyCastle.Cms.DefaultDigestAlgorithmIdentifierFinder digestAlgFinder) + : this((IDigestAlgorithmFinder)digestAlgFinder) { - m_digestAlgFinder = digestAlgFinder; + } + + public CertificateConfirmationContentBuilder(IDigestAlgorithmFinder digestAlgorithmFinder) + { + m_digestAlgorithmFinder = digestAlgorithmFinder; } public CertificateConfirmationContentBuilder AddAcceptedCertificate(X509Certificate certHolder, @@ -42,10 +48,10 @@ namespace Org.BouncyCastle.Cmp X509Certificate cert = m_acceptedCerts[i]; BigInteger reqID = m_acceptedReqIDs[i]; - var sigAlgID = DefaultSignatureAlgorithmIdentifierFinder.Instance.Find(cert.SigAlgName) + var sigAlgID = DefaultSignatureAlgorithmFinder.Instance.Find(cert.SigAlgName) ?? throw new CmpException("cannot find algorithm identifier for signature name"); - AlgorithmIdentifier digAlgID = m_digestAlgFinder.Find(sigAlgID) + var digAlgID = m_digestAlgorithmFinder.Find(sigAlgID) ?? throw new CmpException("cannot find algorithm for digest from signature"); byte[] digest = DigestUtilities.CalculateDigest(digAlgID.Algorithm, cert.GetEncoded()); @@ -53,8 +59,9 @@ namespace Org.BouncyCastle.Cmp v.Add(new CertStatus(digest, reqID)); } - return new CertificateConfirmationContent(CertConfirmContent.GetInstance(new DerSequence(v)), - m_digestAlgFinder); + var content = CertConfirmContent.GetInstance(new DerSequence(v)); + + return new CertificateConfirmationContent(content, m_digestAlgorithmFinder); } } } diff --git a/crypto/src/cmp/CertificateStatus.cs b/crypto/src/cmp/CertificateStatus.cs index 8ccf89562..55292329a 100644 --- a/crypto/src/cmp/CertificateStatus.cs +++ b/crypto/src/cmp/CertificateStatus.cs @@ -1,7 +1,9 @@ -using Org.BouncyCastle.Asn1.Cmp; +using System; + +using Org.BouncyCastle.Asn1.Cmp; using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Cms; using Org.BouncyCastle.Math; +using Org.BouncyCastle.Operators.Utilities; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.X509; @@ -10,30 +12,39 @@ namespace Org.BouncyCastle.Cmp { public class CertificateStatus { - private readonly DefaultDigestAlgorithmIdentifierFinder digestAlgFinder; - private readonly CertStatus certStatus; + private readonly IDigestAlgorithmFinder m_digestAlgorithmFinder; + private readonly CertStatus m_certStatus; - public CertificateStatus(DefaultDigestAlgorithmIdentifierFinder digestAlgFinder, CertStatus certStatus) + [Obsolete("Use constructor taking 'IDigestAlgorithmFinder' instead")] + public CertificateStatus(Org.BouncyCastle.Cms.DefaultDigestAlgorithmIdentifierFinder digestAlgFinder, + CertStatus certStatus) + : this((IDigestAlgorithmFinder)digestAlgFinder, certStatus) { - this.digestAlgFinder = digestAlgFinder; - this.certStatus = certStatus; } - public virtual PkiStatusInfo StatusInfo => certStatus.StatusInfo; + public CertificateStatus(IDigestAlgorithmFinder digestAlgorithmFinder, CertStatus certStatus) + { + m_digestAlgorithmFinder = digestAlgorithmFinder; + m_certStatus = certStatus; + } - public virtual BigInteger CertRequestID => certStatus.CertReqID.Value; + public virtual PkiStatusInfo StatusInfo => m_certStatus.StatusInfo; + + public virtual BigInteger CertRequestID => m_certStatus.CertReqID.Value; public virtual bool IsVerified(X509Certificate cert) { - var sigAlgID = DefaultSignatureAlgorithmIdentifierFinder.Instance.Find(cert.SigAlgName) + var signatureName = cert.SigAlgName; + + var signatureAlgorithm = DefaultSignatureAlgorithmFinder.Instance.Find(signatureName) ?? throw new CmpException("cannot find algorithm identifier for signature name"); - var digAlgID = digestAlgFinder.Find(sigAlgID) - ?? throw new CmpException("cannot find algorithm for digest from signature " + cert.SigAlgName); + var digestAlgorithm = m_digestAlgorithmFinder.Find(signatureAlgorithm) + ?? throw new CmpException("cannot find algorithm for digest from signature " + signatureName); - byte[] digest = DigestUtilities.CalculateDigest(digAlgID.Algorithm, cert.GetEncoded()); + byte[] digest = DigestUtilities.CalculateDigest(digestAlgorithm.Algorithm, cert.GetEncoded()); - return Arrays.FixedTimeEquals(certStatus.CertHash.GetOctets(), digest); + return Arrays.FixedTimeEquals(m_certStatus.CertHash.GetOctets(), digest); } } } |