diff options
Diffstat (limited to 'crypto/src/asn1/cmp/CertifiedKeyPair.cs')
-rw-r--r-- | crypto/src/asn1/cmp/CertifiedKeyPair.cs | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/crypto/src/asn1/cmp/CertifiedKeyPair.cs b/crypto/src/asn1/cmp/CertifiedKeyPair.cs index 9c7fd8175..61da1d37a 100644 --- a/crypto/src/asn1/cmp/CertifiedKeyPair.cs +++ b/crypto/src/asn1/cmp/CertifiedKeyPair.cs @@ -21,36 +21,37 @@ namespace Org.BouncyCastle.Asn1.Cmp return new CertifiedKeyPair(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); } + public static CertifiedKeyPair GetOptional(Asn1Encodable element) + { + if (element == null) + throw new ArgumentNullException(nameof(element)); + + if (element is CertifiedKeyPair certifiedKeyPair) + return certifiedKeyPair; + + Asn1Sequence asn1Sequence = Asn1Sequence.GetOptional(element); + if (asn1Sequence != null) + return new CertifiedKeyPair(asn1Sequence); + + return null; + } + private readonly CertOrEncCert m_certOrEncCert; private readonly EncryptedKey m_privateKey; private readonly PkiPublicationInfo m_publicationInfo; private CertifiedKeyPair(Asn1Sequence seq) { - m_certOrEncCert = CertOrEncCert.GetInstance(seq[0]); - - if (seq.Count >= 2) - { - if (seq.Count == 2) - { - Asn1TaggedObject tagged = Asn1TaggedObject.GetInstance(seq[1], Asn1Tags.ContextSpecific); - if (tagged.TagNo == 0) - { - m_privateKey = EncryptedKey.GetInstance(tagged.GetExplicitBaseObject()); - } - else - { - m_publicationInfo = PkiPublicationInfo.GetInstance(tagged.GetExplicitBaseObject()); - } - } - else - { - m_privateKey = EncryptedKey.GetInstance( - Asn1TaggedObject.GetInstance(seq[1], Asn1Tags.ContextSpecific).GetExplicitBaseObject()); - m_publicationInfo = PkiPublicationInfo.GetInstance( - Asn1TaggedObject.GetInstance(seq[2], Asn1Tags.ContextSpecific).GetExplicitBaseObject()); - } - } + int count = seq.Count, pos = 0; + if (count < 1 || count > 3) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); + + m_certOrEncCert = CertOrEncCert.GetInstance(seq[pos++]); + m_privateKey = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, EncryptedKey.GetInstance); + m_publicationInfo = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, PkiPublicationInfo.GetInstance); + + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); } public CertifiedKeyPair(CertOrEncCert certOrEncCert) @@ -58,6 +59,7 @@ namespace Org.BouncyCastle.Asn1.Cmp { } + [Obsolete("Use constructor with EncryptedKey instead")] public CertifiedKeyPair(CertOrEncCert certOrEncCert, EncryptedValue privateKey, PkiPublicationInfo publicationInfo) : this(certOrEncCert, privateKey == null ? null : new EncryptedKey(privateKey), publicationInfo) @@ -67,10 +69,7 @@ namespace Org.BouncyCastle.Asn1.Cmp public CertifiedKeyPair(CertOrEncCert certOrEncCert, EncryptedKey privateKey, PkiPublicationInfo publicationInfo) { - if (certOrEncCert == null) - throw new ArgumentNullException(nameof(certOrEncCert)); - - m_certOrEncCert = certOrEncCert; + m_certOrEncCert = certOrEncCert ?? throw new ArgumentNullException(nameof(certOrEncCert)); m_privateKey = privateKey; m_publicationInfo = publicationInfo; } @@ -82,13 +81,14 @@ namespace Org.BouncyCastle.Asn1.Cmp public virtual PkiPublicationInfo PublicationInfo => m_publicationInfo; /** + * RFC 9480 * <pre> * CertifiedKeyPair ::= SEQUENCE { - * certOrEncCert CertOrEncCert, - * privateKey [0] EncryptedValue OPTIONAL, - * -- see [CRMF] for comment on encoding - * publicationInfo [1] PKIPublicationInfo OPTIONAL - * } + * certOrEncCert CertOrEncCert, + * privateKey [0] EncryptedKey OPTIONAL, + * -- See [RFC4211] for comments on encoding. + * publicationInfo [1] PKIPublicationInfo OPTIONAL + * } * </pre> * @return a basic ASN.1 object representation. */ |