diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-06-24 20:04:26 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-06-24 20:04:26 +0700 |
commit | 438dc8d53aac9b7483a2014cd4398b8275c496ab (patch) | |
tree | d4e53f01313d43c20cf32ae5c930e0d54a364114 /crypto/src/asn1/pkcs | |
parent | Add TODO (diff) | |
download | BouncyCastle.NET-ed25519-438dc8d53aac9b7483a2014cd4398b8275c496ab.tar.xz |
ASN.1: Use GetTagged with cursor methods
- Add GetTagged method for all CHOICE types - Make most Asn1Utilities methods public
Diffstat (limited to 'crypto/src/asn1/pkcs')
-rw-r--r-- | crypto/src/asn1/pkcs/CertificationRequestInfo.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/pkcs/PrivateKeyInfo.cs | 24 | ||||
-rw-r--r-- | crypto/src/asn1/pkcs/RSAESOAEPparams.cs | 6 | ||||
-rw-r--r-- | crypto/src/asn1/pkcs/RSASSAPSSparams.cs | 8 | ||||
-rw-r--r-- | crypto/src/asn1/pkcs/SignedData.cs | 4 | ||||
-rw-r--r-- | crypto/src/asn1/pkcs/SignerInfo.cs | 4 |
6 files changed, 32 insertions, 16 deletions
diff --git a/crypto/src/asn1/pkcs/CertificationRequestInfo.cs b/crypto/src/asn1/pkcs/CertificationRequestInfo.cs index 53bdffdaa..38d2d39dd 100644 --- a/crypto/src/asn1/pkcs/CertificationRequestInfo.cs +++ b/crypto/src/asn1/pkcs/CertificationRequestInfo.cs @@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Asn1.Pkcs m_subjectPKInfo = SubjectPublicKeyInfo.GetInstance(seq[pos++]); // NOTE: some CertificationRequestInfo objects seem to treat this field as optional. - m_attributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetInstance); + m_attributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetTagged); if (pos != count) throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); diff --git a/crypto/src/asn1/pkcs/PrivateKeyInfo.cs b/crypto/src/asn1/pkcs/PrivateKeyInfo.cs index 7397b7061..1185cf2f8 100644 --- a/crypto/src/asn1/pkcs/PrivateKeyInfo.cs +++ b/crypto/src/asn1/pkcs/PrivateKeyInfo.cs @@ -53,11 +53,27 @@ namespace Org.BouncyCastle.Asn1.Pkcs return new PrivateKeyInfo(Asn1Sequence.GetInstance(obj)); } - public static PrivateKeyInfo GetInstance(Asn1TaggedObject obj, bool explicitly) + public static PrivateKeyInfo GetInstance(Asn1TaggedObject obj, bool explicitly) => + new PrivateKeyInfo(Asn1Sequence.GetInstance(obj, explicitly)); + + public static PrivateKeyInfo GetOptional(Asn1Encodable element) { - return new PrivateKeyInfo(Asn1Sequence.GetInstance(obj, explicitly)); + if (element == null) + throw new ArgumentNullException(nameof(element)); + + if (element is PrivateKeyInfo privateKeyInfo) + return privateKeyInfo; + + Asn1Sequence asn1Sequence = Asn1Sequence.GetOptional(element); + if (asn1Sequence != null) + return new PrivateKeyInfo(asn1Sequence); + + return null; } + public static PrivateKeyInfo GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new PrivateKeyInfo(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); + private readonly DerInteger m_version; private readonly AlgorithmIdentifier m_privateKeyAlgorithm; private readonly Asn1OctetString m_privateKey; @@ -73,8 +89,8 @@ namespace Org.BouncyCastle.Asn1.Pkcs m_version = DerInteger.GetInstance(seq[pos++]); m_privateKeyAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]); m_privateKey = Asn1OctetString.GetInstance(seq[pos++]); - m_attributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetInstance); - m_publicKey = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, DerBitString.GetInstance); + m_attributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetTagged); + m_publicKey = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, DerBitString.GetTagged); if (pos != count) throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); diff --git a/crypto/src/asn1/pkcs/RSAESOAEPparams.cs b/crypto/src/asn1/pkcs/RSAESOAEPparams.cs index 3490509e7..1ce8c7daf 100644 --- a/crypto/src/asn1/pkcs/RSAESOAEPparams.cs +++ b/crypto/src/asn1/pkcs/RSAESOAEPparams.cs @@ -43,13 +43,13 @@ namespace Org.BouncyCastle.Asn1.Pkcs if (count < 0 || count > 3) throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - m_hashAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, AlgorithmIdentifier.GetInstance) + m_hashAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, AlgorithmIdentifier.GetTagged) ?? DefaultHashAlgorithm; - m_maskGenAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, AlgorithmIdentifier.GetInstance) + m_maskGenAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, AlgorithmIdentifier.GetTagged) ?? DefaultMaskGenAlgorithm; - m_pSourceAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, AlgorithmIdentifier.GetInstance) + m_pSourceAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, AlgorithmIdentifier.GetTagged) ?? DefaultPSourceAlgorithm; if (pos != count) diff --git a/crypto/src/asn1/pkcs/RSASSAPSSparams.cs b/crypto/src/asn1/pkcs/RSASSAPSSparams.cs index f7a9d6956..7f38cd73f 100644 --- a/crypto/src/asn1/pkcs/RSASSAPSSparams.cs +++ b/crypto/src/asn1/pkcs/RSASSAPSSparams.cs @@ -45,16 +45,16 @@ namespace Org.BouncyCastle.Asn1.Pkcs if (count < 0 || count > 4) throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - m_hashAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, AlgorithmIdentifier.GetInstance) + m_hashAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, AlgorithmIdentifier.GetTagged) ?? DefaultHashAlgorithm; - m_maskGenAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, AlgorithmIdentifier.GetInstance) + m_maskGenAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, AlgorithmIdentifier.GetTagged) ?? DefaultMaskGenAlgorithm; - m_saltLength = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, DerInteger.GetInstance) + m_saltLength = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, DerInteger.GetTagged) ?? DefaultSaltLength; - m_trailerField = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 3, true, DerInteger.GetInstance) + m_trailerField = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 3, true, DerInteger.GetTagged) ?? DefaultTrailerField; if (pos != count) diff --git a/crypto/src/asn1/pkcs/SignedData.cs b/crypto/src/asn1/pkcs/SignedData.cs index de9c758fb..77f9d00dc 100644 --- a/crypto/src/asn1/pkcs/SignedData.cs +++ b/crypto/src/asn1/pkcs/SignedData.cs @@ -38,8 +38,8 @@ namespace Org.BouncyCastle.Asn1.Pkcs m_version = DerInteger.GetInstance(seq[pos++]); m_digestAlgorithms = Asn1Set.GetInstance(seq[pos++]); m_contentInfo = ContentInfo.GetInstance(seq[pos++]); - m_certificates = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetInstance); - m_crls = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, Asn1Set.GetInstance); + m_certificates = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetTagged); + m_crls = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, Asn1Set.GetTagged); m_signerInfos = Asn1Set.GetInstance(seq[pos++]); if (pos != count) diff --git a/crypto/src/asn1/pkcs/SignerInfo.cs b/crypto/src/asn1/pkcs/SignerInfo.cs index 01d9de98e..8f2e94983 100644 --- a/crypto/src/asn1/pkcs/SignerInfo.cs +++ b/crypto/src/asn1/pkcs/SignerInfo.cs @@ -46,10 +46,10 @@ namespace Org.BouncyCastle.Asn1.Pkcs m_version = DerInteger.GetInstance(seq[pos++]); m_issuerAndSerialNumber = IssuerAndSerialNumber.GetInstance(seq[pos++]); m_digAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]); - m_authenticatedAttributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetInstance); + m_authenticatedAttributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetTagged); m_digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]); m_encryptedDigest = Asn1OctetString.GetInstance(seq[pos++]); - m_unauthenticatedAttributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, Asn1Set.GetInstance); + m_unauthenticatedAttributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, Asn1Set.GetTagged); if (pos != count) throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); |