diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-01-17 20:32:13 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-01-17 20:32:13 +0700 |
commit | 2e1dc7df1b47554ed229b65b9574973b43541a10 (patch) | |
tree | fd8b51ede5fe64ef823d8eb60c5db17e40f31544 /crypto | |
parent | FIx fetching of DateOfCertGen extension (diff) | |
download | BouncyCastle.NET-ed25519-2e1dc7df1b47554ed229b65b9574973b43541a10.tar.xz |
Refactoring in Asn1
Diffstat (limited to 'crypto')
30 files changed, 559 insertions, 742 deletions
diff --git a/crypto/src/asn1/ASN1StreamParser.cs b/crypto/src/asn1/ASN1StreamParser.cs index 490b20ccf..85b890a8c 100644 --- a/crypto/src/asn1/ASN1StreamParser.cs +++ b/crypto/src/asn1/ASN1StreamParser.cs @@ -205,7 +205,7 @@ namespace Org.BouncyCastle.Asn1 internal Asn1TaggedObjectParser ParseTaggedObject() { int tagHdr = _in.ReadByte(); - if (tagHdr< 0) + if (tagHdr < 0) return null; int tagClass = tagHdr & Asn1Tags.Private; diff --git a/crypto/src/asn1/Asn1OutputStream.cs b/crypto/src/asn1/Asn1OutputStream.cs index 163e3848c..1363aa676 100644 --- a/crypto/src/asn1/Asn1OutputStream.cs +++ b/crypto/src/asn1/Asn1OutputStream.cs @@ -1,6 +1,6 @@ using System; -using System.IO; using System.Diagnostics; +using System.IO; #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER using System.Buffers.Binary; using System.Numerics; @@ -24,13 +24,9 @@ namespace Org.BouncyCastle.Asn1 public static Asn1OutputStream Create(Stream output, string encoding) { if (Asn1Encodable.Der.Equals(encoding)) - { return new DerOutputStream(output); - } - else - { - return new Asn1OutputStream(output); - } + + return new Asn1OutputStream(output); } internal Asn1OutputStream(Stream os) diff --git a/crypto/src/asn1/BERSequenceGenerator.cs b/crypto/src/asn1/BERSequenceGenerator.cs index 5ea2c9b82..64ac23c57 100644 --- a/crypto/src/asn1/BERSequenceGenerator.cs +++ b/crypto/src/asn1/BERSequenceGenerator.cs @@ -5,17 +5,13 @@ namespace Org.BouncyCastle.Asn1 public class BerSequenceGenerator : BerGenerator { - public BerSequenceGenerator( - Stream outStream) + public BerSequenceGenerator(Stream outStream) : base(outStream) { WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.Sequence); } - public BerSequenceGenerator( - Stream outStream, - int tagNo, - bool isExplicit) + public BerSequenceGenerator(Stream outStream, int tagNo, bool isExplicit) : base(outStream, tagNo, isExplicit) { WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.Sequence); diff --git a/crypto/src/asn1/BERSetGenerator.cs b/crypto/src/asn1/BERSetGenerator.cs index 72b1f903a..29fc37b93 100644 --- a/crypto/src/asn1/BERSetGenerator.cs +++ b/crypto/src/asn1/BERSetGenerator.cs @@ -5,17 +5,13 @@ namespace Org.BouncyCastle.Asn1 public class BerSetGenerator : BerGenerator { - public BerSetGenerator( - Stream outStream) + public BerSetGenerator(Stream outStream) : base(outStream) { WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.Set); } - public BerSetGenerator( - Stream outStream, - int tagNo, - bool isExplicit) + public BerSetGenerator(Stream outStream, int tagNo, bool isExplicit) : base(outStream, tagNo, isExplicit) { WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.Set); diff --git a/crypto/src/asn1/DLTaggedObjectParser.cs b/crypto/src/asn1/DLTaggedObjectParser.cs index 3a89a69cd..75e8995be 100644 --- a/crypto/src/asn1/DLTaggedObjectParser.cs +++ b/crypto/src/asn1/DLTaggedObjectParser.cs @@ -71,4 +71,3 @@ namespace Org.BouncyCastle.Asn1 } } } - diff --git a/crypto/src/asn1/DerObjectIdentifier.cs b/crypto/src/asn1/DerObjectIdentifier.cs index cb5771958..06a7b25f3 100644 --- a/crypto/src/asn1/DerObjectIdentifier.cs +++ b/crypto/src/asn1/DerObjectIdentifier.cs @@ -80,7 +80,7 @@ namespace Org.BouncyCastle.Asn1 private const long LongLimit = (long.MaxValue >> 7) - 0x7F; - private static readonly DerObjectIdentifier[] cache = new DerObjectIdentifier[1024]; + private static readonly DerObjectIdentifier[] Cache = new DerObjectIdentifier[1024]; private readonly string identifier; private byte[] contents; @@ -208,15 +208,15 @@ namespace Org.BouncyCastle.Asn1 int hashCode = Arrays.GetHashCode(contents); int first = hashCode & 1023; - lock (cache) + lock (Cache) { - DerObjectIdentifier entry = cache[first]; + DerObjectIdentifier entry = Cache[first]; if (entry != null && Arrays.AreEqual(contents, entry.GetContents())) { return entry; } - return cache[first] = new DerObjectIdentifier(contents, clone); + return Cache[first] = new DerObjectIdentifier(contents, clone); } } diff --git a/crypto/src/asn1/cmp/PKIHeader.cs b/crypto/src/asn1/cmp/PKIHeader.cs index 7ed914e6a..7db9cde1d 100644 --- a/crypto/src/asn1/cmp/PKIHeader.cs +++ b/crypto/src/asn1/cmp/PKIHeader.cs @@ -76,13 +76,13 @@ namespace Org.BouncyCastle.Asn1.Cmp public static PkiHeader GetInstance(object obj) { - if (obj is PkiHeader) - return (PkiHeader)obj; + if (obj is PkiHeader pkiHeader) + return pkiHeader; - if (obj is Asn1Sequence) - return new PkiHeader((Asn1Sequence)obj); + if (obj is Asn1Sequence asn1Sequence) + return new PkiHeader(asn1Sequence); - throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj"); + throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), nameof(obj)); } public PkiHeader( @@ -160,16 +160,7 @@ namespace Org.BouncyCastle.Asn1.Cmp public virtual InfoTypeAndValue[] GetGeneralInfo() { - if (generalInfo == null) - { - return null; - } - InfoTypeAndValue[] results = new InfoTypeAndValue[generalInfo.Count]; - for (int i = 0; i < results.Length; i++) - { - results[i] = InfoTypeAndValue.GetInstance(generalInfo[i]); - } - return results; + return generalInfo?.MapElements(InfoTypeAndValue.GetInstance); } /** diff --git a/crypto/src/asn1/cmp/PKIHeaderBuilder.cs b/crypto/src/asn1/cmp/PKIHeaderBuilder.cs index cbefc73b8..ab8db958a 100644 --- a/crypto/src/asn1/cmp/PKIHeaderBuilder.cs +++ b/crypto/src/asn1/cmp/PKIHeaderBuilder.cs @@ -189,15 +189,15 @@ namespace Org.BouncyCastle.Asn1.Cmp public virtual PkiHeader Build() { Asn1EncodableVector v = new Asn1EncodableVector(pvno, sender, recipient); - AddOptional(v, 0, messageTime); - AddOptional(v, 1, protectionAlg); - AddOptional(v, 2, senderKID); - AddOptional(v, 3, recipKID); - AddOptional(v, 4, transactionID); - AddOptional(v, 5, senderNonce); - AddOptional(v, 6, recipNonce); - AddOptional(v, 7, freeText); - AddOptional(v, 8, generalInfo); + v.AddOptionalTagged(true, 0, messageTime); + v.AddOptionalTagged(true, 1, protectionAlg); + v.AddOptionalTagged(true, 2, senderKID); + v.AddOptionalTagged(true, 3, recipKID); + v.AddOptionalTagged(true, 4, transactionID); + v.AddOptionalTagged(true, 5, senderNonce); + v.AddOptionalTagged(true, 6, recipNonce); + v.AddOptionalTagged(true, 7, freeText); + v.AddOptionalTagged(true, 8, generalInfo); messageTime = null; protectionAlg = null; @@ -211,13 +211,5 @@ namespace Org.BouncyCastle.Asn1.Cmp return PkiHeader.GetInstance(new DerSequence(v)); } - - private void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj) - { - if (obj != null) - { - v.Add(new DerTaggedObject(true, tagNo, obj)); - } - } } } diff --git a/crypto/src/asn1/cmp/PKIMessage.cs b/crypto/src/asn1/cmp/PKIMessage.cs index c87bf2126..9ac54b3da 100644 --- a/crypto/src/asn1/cmp/PKIMessage.cs +++ b/crypto/src/asn1/cmp/PKIMessage.cs @@ -96,15 +96,7 @@ namespace Org.BouncyCastle.Asn1.Cmp public virtual CmpCertificate[] GetExtraCerts() { - if (extraCerts == null) - return null; - - CmpCertificate[] results = new CmpCertificate[extraCerts.Count]; - for (int i = 0; i < results.Length; ++i) - { - results[i] = CmpCertificate.GetInstance(extraCerts[i]); - } - return results; + return extraCerts?.MapElements(CmpCertificate.GetInstance); } /** diff --git a/crypto/src/asn1/cmp/PKIMessages.cs b/crypto/src/asn1/cmp/PKIMessages.cs index 0008f476a..8e2e8a1ed 100644 --- a/crypto/src/asn1/cmp/PKIMessages.cs +++ b/crypto/src/asn1/cmp/PKIMessages.cs @@ -7,37 +7,32 @@ namespace Org.BouncyCastle.Asn1.Cmp public class PkiMessages : Asn1Encodable { - private Asn1Sequence content; + private Asn1Sequence m_content; internal PkiMessages(Asn1Sequence seq) { - content = seq; + m_content = seq; } public static PkiMessages GetInstance(object obj) { - if (obj is PkiMessages) - return (PkiMessages)obj; + if (obj is PkiMessages pkiMessages) + return pkiMessages; - if (obj is Asn1Sequence) - return new PkiMessages((Asn1Sequence)obj); + if (obj is Asn1Sequence asn1Sequence) + return new PkiMessages(asn1Sequence); - throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj"); + throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), nameof(obj)); } public PkiMessages(params PkiMessage[] msgs) { - content = new DerSequence(msgs); + m_content = new DerSequence(msgs); } public virtual PkiMessage[] ToPkiMessageArray() { - PkiMessage[] result = new PkiMessage[content.Count]; - for (int i = 0; i != result.Length; ++i) - { - result[i] = PkiMessage.GetInstance(content[i]); - } - return result; + return m_content.MapElements(PkiMessage.GetInstance); } /** @@ -48,7 +43,7 @@ namespace Org.BouncyCastle.Asn1.Cmp */ public override Asn1Object ToAsn1Object() { - return content; + return m_content; } } } diff --git a/crypto/src/asn1/cms/ContentInfoParser.cs b/crypto/src/asn1/cms/ContentInfoParser.cs index 750d8ba7a..e423be848 100644 --- a/crypto/src/asn1/cms/ContentInfoParser.cs +++ b/crypto/src/asn1/cms/ContentInfoParser.cs @@ -22,10 +22,7 @@ namespace Org.BouncyCastle.Asn1.Cms m_content = (Asn1TaggedObjectParser)seq.ReadObject(); } - public DerObjectIdentifier ContentType - { - get { return m_contentType; } - } + public DerObjectIdentifier ContentType => m_contentType; public IAsn1Convertible GetContent(int tag) { diff --git a/crypto/src/asn1/cms/EncryptedContentInfoParser.cs b/crypto/src/asn1/cms/EncryptedContentInfoParser.cs index 09434d7ef..42f72164a 100644 --- a/crypto/src/asn1/cms/EncryptedContentInfoParser.cs +++ b/crypto/src/asn1/cms/EncryptedContentInfoParser.cs @@ -15,32 +15,24 @@ namespace Org.BouncyCastle.Asn1.Cms */ public class EncryptedContentInfoParser { - private DerObjectIdentifier _contentType; - private AlgorithmIdentifier _contentEncryptionAlgorithm; - private Asn1TaggedObjectParser _encryptedContent; + private readonly DerObjectIdentifier m_contentType; + private readonly AlgorithmIdentifier m_contentEncryptionAlgorithm; + private readonly Asn1TaggedObjectParser m_encryptedContent; - public EncryptedContentInfoParser( - Asn1SequenceParser seq) + public EncryptedContentInfoParser(Asn1SequenceParser seq) { - _contentType = (DerObjectIdentifier)seq.ReadObject(); - _contentEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq.ReadObject().ToAsn1Object()); - _encryptedContent = (Asn1TaggedObjectParser)seq.ReadObject(); + m_contentType = (DerObjectIdentifier)seq.ReadObject(); + m_contentEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq.ReadObject().ToAsn1Object()); + m_encryptedContent = (Asn1TaggedObjectParser)seq.ReadObject(); } - public DerObjectIdentifier ContentType - { - get { return _contentType; } - } + public DerObjectIdentifier ContentType => m_contentType; - public AlgorithmIdentifier ContentEncryptionAlgorithm - { - get { return _contentEncryptionAlgorithm; } - } + public AlgorithmIdentifier ContentEncryptionAlgorithm => m_contentEncryptionAlgorithm; - public IAsn1Convertible GetEncryptedContent( - int tag) + public IAsn1Convertible GetEncryptedContent(int tag) { - return Asn1Utilities.ParseContextBaseUniversal(_encryptedContent, 0, false, tag); + return Asn1Utilities.ParseContextBaseUniversal(m_encryptedContent, 0, false, tag); } } } diff --git a/crypto/src/asn1/cms/EnvelopedDataParser.cs b/crypto/src/asn1/cms/EnvelopedDataParser.cs index a86608bb4..06e10c104 100644 --- a/crypto/src/asn1/cms/EnvelopedDataParser.cs +++ b/crypto/src/asn1/cms/EnvelopedDataParser.cs @@ -2,7 +2,7 @@ using System; namespace Org.BouncyCastle.Asn1.Cms { - /** + /** * Produce an object suitable for an Asn1OutputStream. * <pre> * EnvelopedData ::= SEQUENCE { @@ -14,96 +14,96 @@ namespace Org.BouncyCastle.Asn1.Cms * } * </pre> */ - public class EnvelopedDataParser - { - private Asn1SequenceParser _seq; - private DerInteger _version; - private IAsn1Convertible _nextObject; - private bool _originatorInfoCalled; - - public EnvelopedDataParser( - Asn1SequenceParser seq) - { - this._seq = seq; - this._version = (DerInteger)seq.ReadObject(); - } - - public DerInteger Version - { - get { return _version; } - } - - public OriginatorInfo GetOriginatorInfo() - { - _originatorInfoCalled = true; - - if (_nextObject == null) - { - _nextObject = _seq.ReadObject(); - } - - if (_nextObject is Asn1TaggedObjectParser o) - { - if (o.HasContextTag(0)) - { - Asn1SequenceParser originatorInfo = (Asn1SequenceParser)o.ParseBaseUniversal(false, Asn1Tags.Sequence); - _nextObject = null; - return OriginatorInfo.GetInstance(originatorInfo.ToAsn1Object()); - } - } - - return null; - } - - public Asn1SetParser GetRecipientInfos() - { - if (!_originatorInfoCalled) - { - GetOriginatorInfo(); - } - - if (_nextObject == null) - { - _nextObject = _seq.ReadObject(); - } - - Asn1SetParser recipientInfos = (Asn1SetParser)_nextObject; - _nextObject = null; - return recipientInfos; - } - - public EncryptedContentInfoParser GetEncryptedContentInfo() - { - if (_nextObject == null) - { - _nextObject = _seq.ReadObject(); - } - - if (_nextObject != null) - { - Asn1SequenceParser o = (Asn1SequenceParser) _nextObject; - _nextObject = null; - return new EncryptedContentInfoParser(o); - } - - return null; - } - - public Asn1SetParser GetUnprotectedAttrs() - { - if (_nextObject == null) - { - _nextObject = _seq.ReadObject(); - } - - if (_nextObject != null) - { - Asn1TaggedObjectParser o = (Asn1TaggedObjectParser)_nextObject; - _nextObject = null; - return (Asn1SetParser)Asn1Utilities.ParseContextBaseUniversal(o, 1, false, Asn1Tags.SetOf); - } - - return null; - } - } + public class EnvelopedDataParser + { + private Asn1SequenceParser _seq; + private DerInteger _version; + private IAsn1Convertible _nextObject; + private bool _originatorInfoCalled; + + public EnvelopedDataParser( + Asn1SequenceParser seq) + { + this._seq = seq; + this._version = (DerInteger)seq.ReadObject(); + } + + public DerInteger Version + { + get { return _version; } + } + + public OriginatorInfo GetOriginatorInfo() + { + _originatorInfoCalled = true; + + if (_nextObject == null) + { + _nextObject = _seq.ReadObject(); + } + + if (_nextObject is Asn1TaggedObjectParser o) + { + if (o.HasContextTag(0)) + { + Asn1SequenceParser originatorInfo = (Asn1SequenceParser)o.ParseBaseUniversal(false, Asn1Tags.Sequence); + _nextObject = null; + return OriginatorInfo.GetInstance(originatorInfo.ToAsn1Object()); + } + } + + return null; + } + + public Asn1SetParser GetRecipientInfos() + { + if (!_originatorInfoCalled) + { + GetOriginatorInfo(); + } + + if (_nextObject == null) + { + _nextObject = _seq.ReadObject(); + } + + Asn1SetParser recipientInfos = (Asn1SetParser)_nextObject; + _nextObject = null; + return recipientInfos; + } + + public EncryptedContentInfoParser GetEncryptedContentInfo() + { + if (_nextObject == null) + { + _nextObject = _seq.ReadObject(); + } + + if (_nextObject != null) + { + Asn1SequenceParser o = (Asn1SequenceParser)_nextObject; + _nextObject = null; + return new EncryptedContentInfoParser(o); + } + + return null; + } + + public Asn1SetParser GetUnprotectedAttrs() + { + if (_nextObject == null) + { + _nextObject = _seq.ReadObject(); + } + + if (_nextObject != null) + { + Asn1TaggedObjectParser o = (Asn1TaggedObjectParser)_nextObject; + _nextObject = null; + return (Asn1SetParser)Asn1Utilities.ParseContextBaseUniversal(o, 1, false, Asn1Tags.SetOf); + } + + return null; + } + } } diff --git a/crypto/src/asn1/cms/KEKIdentifier.cs b/crypto/src/asn1/cms/KEKIdentifier.cs index 36ab94f52..70ae8c438 100644 --- a/crypto/src/asn1/cms/KEKIdentifier.cs +++ b/crypto/src/asn1/cms/KEKIdentifier.cs @@ -21,19 +21,18 @@ namespace Org.BouncyCastle.Asn1.Cms this.other = other; } - public KekIdentifier( - Asn1Sequence seq) + public KekIdentifier(Asn1Sequence seq) { - keyIdentifier = (Asn1OctetString) seq[0]; + keyIdentifier = (Asn1OctetString)seq[0]; switch (seq.Count) { case 1: break; case 2: - if (seq[1] is Asn1GeneralizedTime) + if (seq[1] is Asn1GeneralizedTime asn1GeneralizedTime) { - date = (Asn1GeneralizedTime) seq[1]; + date = asn1GeneralizedTime; } else { @@ -41,7 +40,7 @@ namespace Org.BouncyCastle.Asn1.Cms } break; case 3: - date = (Asn1GeneralizedTime) seq[1]; + date = (Asn1GeneralizedTime)seq[1]; other = OtherKeyAttribute.GetInstance(seq[2]); break; default: diff --git a/crypto/src/asn1/cms/RecipientKeyIdentifier.cs b/crypto/src/asn1/cms/RecipientKeyIdentifier.cs index dea9ce09d..512025808 100644 --- a/crypto/src/asn1/cms/RecipientKeyIdentifier.cs +++ b/crypto/src/asn1/cms/RecipientKeyIdentifier.cs @@ -37,20 +37,18 @@ namespace Org.BouncyCastle.Asn1.Cms this.other = other; } - public RecipientKeyIdentifier( - Asn1Sequence seq) + public RecipientKeyIdentifier(Asn1Sequence seq) { - subjectKeyIdentifier = Asn1OctetString.GetInstance( - seq[0]); + subjectKeyIdentifier = Asn1OctetString.GetInstance(seq[0]); switch(seq.Count) { case 1: break; case 2: - if (seq[1] is Asn1GeneralizedTime) + if (seq[1] is Asn1GeneralizedTime asn1GeneralizedTime) { - date = (Asn1GeneralizedTime)seq[1]; + date = asn1GeneralizedTime; } else { @@ -58,7 +56,7 @@ namespace Org.BouncyCastle.Asn1.Cms } break; case 3: - date = (Asn1GeneralizedTime)seq[1]; + date = (Asn1GeneralizedTime)seq[1]; other = OtherKeyAttribute.GetInstance(seq[2]); break; default: diff --git a/crypto/src/asn1/cms/SignedDataParser.cs b/crypto/src/asn1/cms/SignedDataParser.cs index 64114f292..3afbe09f4 100644 --- a/crypto/src/asn1/cms/SignedDataParser.cs +++ b/crypto/src/asn1/cms/SignedDataParser.cs @@ -5,7 +5,7 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Cms { - /** + /** * <pre> * SignedData ::= SEQUENCE { * version CMSVersion, @@ -17,102 +17,102 @@ namespace Org.BouncyCastle.Asn1.Cms * } * </pre> */ - public class SignedDataParser - { - private Asn1SequenceParser _seq; - private DerInteger _version; - private object _nextObject; - private bool _certsCalled; - private bool _crlsCalled; - - public static SignedDataParser GetInstance( - object o) - { - if (o is Asn1Sequence) - return new SignedDataParser(((Asn1Sequence)o).Parser); - - if (o is Asn1SequenceParser) - return new SignedDataParser((Asn1SequenceParser)o); + public class SignedDataParser + { + private Asn1SequenceParser _seq; + private DerInteger _version; + private object _nextObject; + private bool _certsCalled; + private bool _crlsCalled; + + public static SignedDataParser GetInstance( + object o) + { + if (o is Asn1Sequence) + return new SignedDataParser(((Asn1Sequence)o).Parser); + + if (o is Asn1SequenceParser) + return new SignedDataParser((Asn1SequenceParser)o); throw new IOException("unknown object encountered: " + Platform.GetTypeName(o)); - } - - public SignedDataParser( - Asn1SequenceParser seq) - { - this._seq = seq; - this._version = (DerInteger)seq.ReadObject(); - } - - public DerInteger Version - { - get { return _version; } - } - - public Asn1SetParser GetDigestAlgorithms() - { - return (Asn1SetParser)_seq.ReadObject(); - } - - public ContentInfoParser GetEncapContentInfo() - { - return new ContentInfoParser((Asn1SequenceParser)_seq.ReadObject()); - } - - public Asn1SetParser GetCertificates() - { - _certsCalled = true; - _nextObject = _seq.ReadObject(); - - if (_nextObject is Asn1TaggedObjectParser o) - { - if (o.HasContextTag(0)) - { - Asn1SetParser certs = (Asn1SetParser)o.ParseBaseUniversal(false, Asn1Tags.SetOf); - _nextObject = null; - return certs; - } - } - - return null; - } - - public Asn1SetParser GetCrls() - { - if (!_certsCalled) - throw new IOException("GetCerts() has not been called."); - - _crlsCalled = true; - - if (_nextObject == null) - { - _nextObject = _seq.ReadObject(); - } - - if (_nextObject is Asn1TaggedObjectParser o) - { - if (o.HasContextTag(1)) - { - Asn1SetParser crls = (Asn1SetParser)o.ParseBaseUniversal(false, Asn1Tags.SetOf); - _nextObject = null; - return crls; - } - } + } + + public SignedDataParser( + Asn1SequenceParser seq) + { + this._seq = seq; + this._version = (DerInteger)seq.ReadObject(); + } + + public DerInteger Version + { + get { return _version; } + } + + public Asn1SetParser GetDigestAlgorithms() + { + return (Asn1SetParser)_seq.ReadObject(); + } + + public ContentInfoParser GetEncapContentInfo() + { + return new ContentInfoParser((Asn1SequenceParser)_seq.ReadObject()); + } + + public Asn1SetParser GetCertificates() + { + _certsCalled = true; + _nextObject = _seq.ReadObject(); + + if (_nextObject is Asn1TaggedObjectParser o) + { + if (o.HasContextTag(0)) + { + Asn1SetParser certs = (Asn1SetParser)o.ParseBaseUniversal(false, Asn1Tags.SetOf); + _nextObject = null; + return certs; + } + } return null; } - public Asn1SetParser GetSignerInfos() - { - if (!_certsCalled || !_crlsCalled) - throw new IOException("GetCerts() and/or GetCrls() has not been called."); + public Asn1SetParser GetCrls() + { + if (!_certsCalled) + throw new IOException("GetCerts() has not been called."); + + _crlsCalled = true; + + if (_nextObject == null) + { + _nextObject = _seq.ReadObject(); + } - if (_nextObject == null) - { - _nextObject = _seq.ReadObject(); - } + if (_nextObject is Asn1TaggedObjectParser o) + { + if (o.HasContextTag(1)) + { + Asn1SetParser crls = (Asn1SetParser)o.ParseBaseUniversal(false, Asn1Tags.SetOf); + _nextObject = null; + return crls; + } + } - return (Asn1SetParser)_nextObject; - } - } + return null; + } + + public Asn1SetParser GetSignerInfos() + { + if (!_certsCalled || !_crlsCalled) + throw new IOException("GetCerts() and/or GetCrls() has not been called."); + + if (_nextObject == null) + { + _nextObject = _seq.ReadObject(); + } + + return (Asn1SetParser)_nextObject; + } + } } diff --git a/crypto/src/asn1/cms/Time.cs b/crypto/src/asn1/cms/Time.cs index 7b6b49c30..19bc1a633 100644 --- a/crypto/src/asn1/cms/Time.cs +++ b/crypto/src/asn1/cms/Time.cs @@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Asn1.Cms public Time(Asn1GeneralizedTime generalizedTime) { - this.m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime)); + m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime)); } public Time(Asn1UtcTime utcTime) @@ -42,7 +42,7 @@ namespace Org.BouncyCastle.Asn1.Cms // Validate utcTime is in the appropriate year range utcTime.ToDateTime(2049); - this.m_timeObject = utcTime; + m_timeObject = utcTime; } /** diff --git a/crypto/src/asn1/crmf/CertTemplateBuilder.cs b/crypto/src/asn1/crmf/CertTemplateBuilder.cs index 51c73c4e1..d26e6399c 100644 --- a/crypto/src/asn1/crmf/CertTemplateBuilder.cs +++ b/crypto/src/asn1/crmf/CertTemplateBuilder.cs @@ -99,27 +99,17 @@ namespace Org.BouncyCastle.Asn1.Crmf public virtual CertTemplate Build() { Asn1EncodableVector v = new Asn1EncodableVector(); - - AddOptional(v, 0, false, version); - AddOptional(v, 1, false, serialNumber); - AddOptional(v, 2, false, signingAlg); - AddOptional(v, 3, true, issuer); // CHOICE - AddOptional(v, 4, false, validity); - AddOptional(v, 5, true, subject); // CHOICE - AddOptional(v, 6, false, publicKey); - AddOptional(v, 7, false, issuerUID); - AddOptional(v, 8, false, subjectUID); - AddOptional(v, 9, false, extensions); - + v.AddOptionalTagged(false, 0, version); + v.AddOptionalTagged(false, 1, serialNumber); + v.AddOptionalTagged(false, 2, signingAlg); + v.AddOptionalTagged(true, 3, issuer); // CHOICE + v.AddOptionalTagged(false, 4, validity); + v.AddOptionalTagged(true, 5, subject); // CHOICE + v.AddOptionalTagged(false, 6, publicKey); + v.AddOptionalTagged(false, 7, issuerUID); + v.AddOptionalTagged(false, 8, subjectUID); + v.AddOptionalTagged(false, 9, extensions); return CertTemplate.GetInstance(new DerSequence(v)); } - - private void AddOptional(Asn1EncodableVector v, int tagNo, bool isExplicit, Asn1Encodable obj) - { - if (obj != null) - { - v.Add(new DerTaggedObject(isExplicit, tagNo, obj)); - } - } } } diff --git a/crypto/src/asn1/esf/CertificateValues.cs b/crypto/src/asn1/esf/CertificateValues.cs index ee0d2830c..8329e45f8 100644 --- a/crypto/src/asn1/esf/CertificateValues.cs +++ b/crypto/src/asn1/esf/CertificateValues.cs @@ -15,69 +15,58 @@ namespace Org.BouncyCastle.Asn1.Esf public class CertificateValues : Asn1Encodable { - private readonly Asn1Sequence certificates; + private readonly Asn1Sequence m_certificates; - public static CertificateValues GetInstance( - object obj) + public static CertificateValues GetInstance(object obj) { - if (obj == null || obj is CertificateValues) - return (CertificateValues) obj; + if (obj == null) + return null; - if (obj is Asn1Sequence) - return new CertificateValues((Asn1Sequence) obj); + if (obj is CertificateValues certificateValues) + return certificateValues; - throw new ArgumentException( - "Unknown object in 'CertificateValues' factory: " - + Platform.GetTypeName(obj), - "obj"); + if (obj is Asn1Sequence asn1Sequence) + return new CertificateValues(asn1Sequence); + + throw new ArgumentException("Unknown object in 'CertificateValues' factory: " + Platform.GetTypeName(obj), + nameof(obj)); } - private CertificateValues( - Asn1Sequence seq) + private CertificateValues(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); - foreach (Asn1Encodable ae in seq) - { - X509CertificateStructure.GetInstance(ae.ToAsn1Object()); - } + // Validate + seq.MapElements(element => X509CertificateStructure.GetInstance(element.ToAsn1Object())); - this.certificates = seq; + m_certificates = seq; } - public CertificateValues( - params X509CertificateStructure[] certificates) + public CertificateValues(params X509CertificateStructure[] certificates) { if (certificates == null) - throw new ArgumentNullException("certificates"); + throw new ArgumentNullException(nameof(certificates)); - this.certificates = new DerSequence(certificates); + m_certificates = new DerSequence(certificates); } - public CertificateValues( - IEnumerable<X509CertificateStructure> certificates) + public CertificateValues(IEnumerable<X509CertificateStructure> certificates) { if (certificates == null) - throw new ArgumentNullException("certificates"); + throw new ArgumentNullException(nameof(certificates)); - this.certificates = new DerSequence( - Asn1EncodableVector.FromEnumerable(certificates)); + m_certificates = new DerSequence(Asn1EncodableVector.FromEnumerable(certificates)); } public X509CertificateStructure[] GetCertificates() { - X509CertificateStructure[] result = new X509CertificateStructure[certificates.Count]; - for (int i = 0; i < certificates.Count; ++i) - { - result[i] = X509CertificateStructure.GetInstance(certificates[i]); - } - return result; + return m_certificates.MapElements(element => X509CertificateStructure.GetInstance(element.ToAsn1Object())); } public override Asn1Object ToAsn1Object() { - return certificates; + return m_certificates; } } } diff --git a/crypto/src/asn1/esf/CompleteCertificateRefs.cs b/crypto/src/asn1/esf/CompleteCertificateRefs.cs index 2ed66e3dc..24727dc25 100644 --- a/crypto/src/asn1/esf/CompleteCertificateRefs.cs +++ b/crypto/src/asn1/esf/CompleteCertificateRefs.cs @@ -14,69 +14,58 @@ namespace Org.BouncyCastle.Asn1.Esf public class CompleteCertificateRefs : Asn1Encodable { - private readonly Asn1Sequence otherCertIDs; + private readonly Asn1Sequence m_otherCertIDs; - public static CompleteCertificateRefs GetInstance( - object obj) + public static CompleteCertificateRefs GetInstance(object obj) { - if (obj == null || obj is CompleteCertificateRefs) - return (CompleteCertificateRefs) obj; + if (obj == null) + return null; - if (obj is Asn1Sequence) - return new CompleteCertificateRefs((Asn1Sequence) obj); + if (obj is CompleteCertificateRefs completeCertificateRefs) + return completeCertificateRefs; - throw new ArgumentException( - "Unknown object in 'CompleteCertificateRefs' factory: " - + Platform.GetTypeName(obj), - "obj"); + if (obj is Asn1Sequence asn1Sequence) + return new CompleteCertificateRefs(asn1Sequence); + + throw new ArgumentException("Unknown object in 'CompleteCertificateRefs' factory: " + Platform.GetTypeName(obj), + nameof(obj)); } - private CompleteCertificateRefs( - Asn1Sequence seq) + private CompleteCertificateRefs(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); - foreach (Asn1Encodable ae in seq) - { - OtherCertID.GetInstance(ae.ToAsn1Object()); - } + // Validate + seq.MapElements(element => OtherCertID.GetInstance(element.ToAsn1Object())); - this.otherCertIDs = seq; + m_otherCertIDs = seq; } - public CompleteCertificateRefs( - params OtherCertID[] otherCertIDs) + public CompleteCertificateRefs(params OtherCertID[] otherCertIDs) { if (otherCertIDs == null) - throw new ArgumentNullException("otherCertIDs"); + throw new ArgumentNullException(nameof(otherCertIDs)); - this.otherCertIDs = new DerSequence(otherCertIDs); + m_otherCertIDs = new DerSequence(otherCertIDs); } - public CompleteCertificateRefs( - IEnumerable<OtherCertID> otherCertIDs) + public CompleteCertificateRefs(IEnumerable<OtherCertID> otherCertIDs) { if (otherCertIDs == null) - throw new ArgumentNullException("otherCertIDs"); + throw new ArgumentNullException(nameof(otherCertIDs)); - this.otherCertIDs = new DerSequence( - Asn1EncodableVector.FromEnumerable(otherCertIDs)); + m_otherCertIDs = new DerSequence(Asn1EncodableVector.FromEnumerable(otherCertIDs)); } public OtherCertID[] GetOtherCertIDs() { - OtherCertID[] result = new OtherCertID[otherCertIDs.Count]; - for (int i = 0; i < otherCertIDs.Count; ++i) - { - result[i] = OtherCertID.GetInstance(otherCertIDs[i].ToAsn1Object()); - } - return result; + return m_otherCertIDs.MapElements(element => OtherCertID.GetInstance(element.ToAsn1Object())); } public override Asn1Object ToAsn1Object() { - return otherCertIDs; + return m_otherCertIDs; } } } diff --git a/crypto/src/asn1/esf/CompleteRevocationRefs.cs b/crypto/src/asn1/esf/CompleteRevocationRefs.cs index 9942cec8f..2c120f0f9 100644 --- a/crypto/src/asn1/esf/CompleteRevocationRefs.cs +++ b/crypto/src/asn1/esf/CompleteRevocationRefs.cs @@ -14,69 +14,58 @@ namespace Org.BouncyCastle.Asn1.Esf public class CompleteRevocationRefs : Asn1Encodable { - private readonly Asn1Sequence crlOcspRefs; + private readonly Asn1Sequence m_crlOcspRefs; - public static CompleteRevocationRefs GetInstance( - object obj) + public static CompleteRevocationRefs GetInstance(object obj) { - if (obj == null || obj is CompleteRevocationRefs) - return (CompleteRevocationRefs) obj; + if (obj == null) + return null; - if (obj is Asn1Sequence) - return new CompleteRevocationRefs((Asn1Sequence) obj); + if (obj is CompleteRevocationRefs completeRevocationRefs) + return completeRevocationRefs; - throw new ArgumentException( - "Unknown object in 'CompleteRevocationRefs' factory: " - + Platform.GetTypeName(obj), - "obj"); + if (obj is Asn1Sequence asn1Sequence) + return new CompleteRevocationRefs(asn1Sequence); + + throw new ArgumentException("Unknown object in 'CompleteRevocationRefs' factory: " + Platform.GetTypeName(obj), + nameof(obj)); } - private CompleteRevocationRefs( - Asn1Sequence seq) + private CompleteRevocationRefs(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); - foreach (Asn1Encodable ae in seq) - { - CrlOcspRef.GetInstance(ae.ToAsn1Object()); - } + // Validate + seq.MapElements(element => CrlOcspRef.GetInstance(element.ToAsn1Object())); - this.crlOcspRefs = seq; + m_crlOcspRefs = seq; } - public CompleteRevocationRefs( - params CrlOcspRef[] crlOcspRefs) + public CompleteRevocationRefs(params CrlOcspRef[] crlOcspRefs) { if (crlOcspRefs == null) - throw new ArgumentNullException("crlOcspRefs"); + throw new ArgumentNullException(nameof(crlOcspRefs)); - this.crlOcspRefs = new DerSequence(crlOcspRefs); + m_crlOcspRefs = new DerSequence(crlOcspRefs); } - public CompleteRevocationRefs( - IEnumerable<CrlOcspRef> crlOcspRefs) + public CompleteRevocationRefs(IEnumerable<CrlOcspRef> crlOcspRefs) { if (crlOcspRefs == null) - throw new ArgumentNullException("crlOcspRefs"); + throw new ArgumentNullException(nameof(crlOcspRefs)); - this.crlOcspRefs = new DerSequence( - Asn1EncodableVector.FromEnumerable(crlOcspRefs)); + m_crlOcspRefs = new DerSequence(Asn1EncodableVector.FromEnumerable(crlOcspRefs)); } public CrlOcspRef[] GetCrlOcspRefs() { - CrlOcspRef[] result = new CrlOcspRef[crlOcspRefs.Count]; - for (int i = 0; i < crlOcspRefs.Count; ++i) - { - result[i] = CrlOcspRef.GetInstance(crlOcspRefs[i].ToAsn1Object()); - } - return result; + return m_crlOcspRefs.MapElements(element => CrlOcspRef.GetInstance(element.ToAsn1Object())); } public override Asn1Object ToAsn1Object() { - return crlOcspRefs; + return m_crlOcspRefs; } } } diff --git a/crypto/src/asn1/esf/CrlIdentifier.cs b/crypto/src/asn1/esf/CrlIdentifier.cs index 54e930acc..29003260a 100644 --- a/crypto/src/asn1/esf/CrlIdentifier.cs +++ b/crypto/src/asn1/esf/CrlIdentifier.cs @@ -20,9 +20,9 @@ namespace Org.BouncyCastle.Asn1.Esf public class CrlIdentifier : Asn1Encodable { - private readonly X509Name crlIssuer; - private readonly Asn1UtcTime crlIssuedTime; - private readonly DerInteger crlNumber; + private readonly X509Name m_crlIssuer; + private readonly Asn1UtcTime m_crlIssuedTime; + private readonly DerInteger m_crlNumber; public static CrlIdentifier GetInstance(object obj) { @@ -46,15 +46,15 @@ namespace Org.BouncyCastle.Asn1.Esf if (seq.Count < 2 || seq.Count > 3) throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq)); - this.crlIssuer = X509Name.GetInstance(seq[0]); - this.crlIssuedTime = Asn1UtcTime.GetInstance(seq[1]); + this.m_crlIssuer = X509Name.GetInstance(seq[0]); + this.m_crlIssuedTime = Asn1UtcTime.GetInstance(seq[1]); // Validate crlIssuedTime is in the appropriate year range - crlIssuedTime.ToDateTime(2049); + m_crlIssuedTime.ToDateTime(2049); if (seq.Count > 2) { - this.crlNumber = DerInteger.GetInstance(seq[2]); + this.m_crlNumber = DerInteger.GetInstance(seq[2]); } } @@ -75,37 +75,37 @@ namespace Org.BouncyCastle.Asn1.Esf public CrlIdentifier(X509Name crlIssuer, Asn1UtcTime crlIssuedTime, BigInteger crlNumber) { - this.crlIssuer = crlIssuer ?? throw new ArgumentNullException(nameof(crlIssuer)); - this.crlIssuedTime = crlIssuedTime ?? throw new ArgumentNullException(nameof(crlIssuedTime)); + m_crlIssuer = crlIssuer ?? throw new ArgumentNullException(nameof(crlIssuer)); + m_crlIssuedTime = crlIssuedTime ?? throw new ArgumentNullException(nameof(crlIssuedTime)); if (null != crlNumber) { - this.crlNumber = new DerInteger(crlNumber); + m_crlNumber = new DerInteger(crlNumber); } // Validate crlIssuedTime is in the appropriate year range - this.crlIssuedTime.ToDateTime(2049); + m_crlIssuedTime.ToDateTime(2049); } public X509Name CrlIssuer { - get { return crlIssuer; } + get { return m_crlIssuer; } } public DateTime CrlIssuedTime { - get { return crlIssuedTime.ToDateTime(2049); } + get { return m_crlIssuedTime.ToDateTime(2049); } } public BigInteger CrlNumber { - get { return crlNumber?.Value; } + get { return m_crlNumber?.Value; } } public override Asn1Object ToAsn1Object() { - var v = new Asn1EncodableVector(crlIssuer.ToAsn1Object(), crlIssuedTime); - v.AddOptional(crlNumber); + var v = new Asn1EncodableVector(m_crlIssuer.ToAsn1Object(), m_crlIssuedTime); + v.AddOptional(m_crlNumber); return new DerSequence(v); } } diff --git a/crypto/src/asn1/esf/CrlListID.cs b/crypto/src/asn1/esf/CrlListID.cs index d3c4365c5..1ee58aebc 100644 --- a/crypto/src/asn1/esf/CrlListID.cs +++ b/crypto/src/asn1/esf/CrlListID.cs @@ -17,71 +17,60 @@ namespace Org.BouncyCastle.Asn1.Esf public class CrlListID : Asn1Encodable { - private readonly Asn1Sequence crls; + private readonly Asn1Sequence m_crls; - public static CrlListID GetInstance( - object obj) + public static CrlListID GetInstance(object obj) { - if (obj == null || obj is CrlListID) - return (CrlListID) obj; + if (obj == null) + return null; - if (obj is Asn1Sequence) - return new CrlListID((Asn1Sequence) obj); + if (obj is CrlListID crlListID) + return crlListID; - throw new ArgumentException( - "Unknown object in 'CrlListID' factory: " - + Platform.GetTypeName(obj), - "obj"); + if (obj is Asn1Sequence asn1Sequence) + return new CrlListID(asn1Sequence); + + throw new ArgumentException("Unknown object in 'CrlListID' factory: " + Platform.GetTypeName(obj), + nameof(obj)); } - private CrlListID( - Asn1Sequence seq) + private CrlListID(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); if (seq.Count != 1) - throw new ArgumentException("Bad sequence size: " + seq.Count, "seq"); + throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq)); - this.crls = (Asn1Sequence) seq[0].ToAsn1Object(); + m_crls = (Asn1Sequence)seq[0].ToAsn1Object(); - foreach (Asn1Encodable ae in this.crls) - { - CrlValidatedID.GetInstance(ae.ToAsn1Object()); - } + // Validate + m_crls.MapElements(element => CrlValidatedID.GetInstance(element.ToAsn1Object())); } - public CrlListID( - params CrlValidatedID[] crls) + public CrlListID(params CrlValidatedID[] crls) { if (crls == null) - throw new ArgumentNullException("crls"); + throw new ArgumentNullException(nameof(crls)); - this.crls = new DerSequence(crls); + this.m_crls = new DerSequence(crls); } - public CrlListID( - IEnumerable<CrlValidatedID> crls) + public CrlListID(IEnumerable<CrlValidatedID> crls) { if (crls == null) - throw new ArgumentNullException("crls"); + throw new ArgumentNullException(nameof(crls)); - this.crls = new DerSequence( - Asn1EncodableVector.FromEnumerable(crls)); + this.m_crls = new DerSequence(Asn1EncodableVector.FromEnumerable(crls)); } public CrlValidatedID[] GetCrls() { - CrlValidatedID[] result = new CrlValidatedID[crls.Count]; - for (int i = 0; i < crls.Count; ++i) - { - result[i] = CrlValidatedID.GetInstance(crls[i].ToAsn1Object()); - } - return result; + return m_crls.MapElements(element => CrlValidatedID.GetInstance(element.ToAsn1Object())); } public override Asn1Object ToAsn1Object() { - return new DerSequence(crls); + return new DerSequence(m_crls); } } } diff --git a/crypto/src/asn1/esf/OcspListID.cs b/crypto/src/asn1/esf/OcspListID.cs index 3918dfd42..974962571 100644 --- a/crypto/src/asn1/esf/OcspListID.cs +++ b/crypto/src/asn1/esf/OcspListID.cs @@ -16,71 +16,60 @@ namespace Org.BouncyCastle.Asn1.Esf public class OcspListID : Asn1Encodable { - private readonly Asn1Sequence ocspResponses; + private readonly Asn1Sequence m_ocspResponses; - public static OcspListID GetInstance( - object obj) + public static OcspListID GetInstance(object obj) { - if (obj == null || obj is OcspListID) - return (OcspListID) obj; + if (obj == null) + return null; - if (obj is Asn1Sequence) - return new OcspListID((Asn1Sequence) obj); + if (obj is OcspListID ocspListID) + return ocspListID; - throw new ArgumentException( - "Unknown object in 'OcspListID' factory: " - + Platform.GetTypeName(obj), - "obj"); + if (obj is Asn1Sequence asn1Sequence) + return new OcspListID(asn1Sequence); + + throw new ArgumentException("Unknown object in 'OcspListID' factory: " + Platform.GetTypeName(obj), + nameof(obj)); } - private OcspListID( - Asn1Sequence seq) + private OcspListID(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); if (seq.Count != 1) - throw new ArgumentException("Bad sequence size: " + seq.Count, "seq"); + throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq)); - this.ocspResponses = (Asn1Sequence) seq[0].ToAsn1Object(); + m_ocspResponses = (Asn1Sequence)seq[0].ToAsn1Object(); - foreach (Asn1Encodable ae in this.ocspResponses) - { - OcspResponsesID.GetInstance(ae.ToAsn1Object()); - } + // Validate + m_ocspResponses.MapElements(element => OcspResponsesID.GetInstance(element.ToAsn1Object())); } - public OcspListID( - params OcspResponsesID[] ocspResponses) + public OcspListID(params OcspResponsesID[] ocspResponses) { if (ocspResponses == null) - throw new ArgumentNullException("ocspResponses"); + throw new ArgumentNullException(nameof(ocspResponses)); - this.ocspResponses = new DerSequence(ocspResponses); + m_ocspResponses = new DerSequence(ocspResponses); } - public OcspListID( - IEnumerable<OcspResponsesID> ocspResponses) + public OcspListID(IEnumerable<OcspResponsesID> ocspResponses) { if (ocspResponses == null) - throw new ArgumentNullException("ocspResponses"); + throw new ArgumentNullException(nameof(ocspResponses)); - this.ocspResponses = new DerSequence( - Asn1EncodableVector.FromEnumerable(ocspResponses)); + m_ocspResponses = new DerSequence(Asn1EncodableVector.FromEnumerable(ocspResponses)); } public OcspResponsesID[] GetOcspResponses() { - OcspResponsesID[] result = new OcspResponsesID[ocspResponses.Count]; - for (int i = 0; i < ocspResponses.Count; ++i) - { - result[i] = OcspResponsesID.GetInstance(ocspResponses[i].ToAsn1Object()); - } - return result; + return m_ocspResponses.MapElements(element => OcspResponsesID.GetInstance(element.ToAsn1Object())); } public override Asn1Object ToAsn1Object() { - return new DerSequence(ocspResponses); + return new DerSequence(m_ocspResponses); } } } diff --git a/crypto/src/asn1/esf/OtherSigningCertificate.cs b/crypto/src/asn1/esf/OtherSigningCertificate.cs index a4f4a0727..aa1dcbf99 100644 --- a/crypto/src/asn1/esf/OtherSigningCertificate.cs +++ b/crypto/src/asn1/esf/OtherSigningCertificate.cs @@ -17,111 +17,89 @@ namespace Org.BouncyCastle.Asn1.Esf public class OtherSigningCertificate : Asn1Encodable { - private readonly Asn1Sequence certs; - private readonly Asn1Sequence policies; + private readonly Asn1Sequence m_certs; + private readonly Asn1Sequence m_policies; - public static OtherSigningCertificate GetInstance( - object obj) + public static OtherSigningCertificate GetInstance(object obj) { - if (obj == null || obj is OtherSigningCertificate) - return (OtherSigningCertificate) obj; + if (obj == null) + return null; + + if (obj is OtherSigningCertificate otherSigningCertificate) + return otherSigningCertificate; - if (obj is Asn1Sequence) - return new OtherSigningCertificate((Asn1Sequence) obj); + if (obj is Asn1Sequence asn1Sequence) + return new OtherSigningCertificate(asn1Sequence); - throw new ArgumentException( - "Unknown object in 'OtherSigningCertificate' factory: " - + Platform.GetTypeName(obj), - "obj"); + throw new ArgumentException("Unknown object in 'OtherSigningCertificate' factory: " + Platform.GetTypeName(obj), + nameof(obj)); } - private OtherSigningCertificate( - Asn1Sequence seq) + private OtherSigningCertificate(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); if (seq.Count < 1 || seq.Count > 2) - throw new ArgumentException("Bad sequence size: " + seq.Count, "seq"); + throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq)); - this.certs = Asn1Sequence.GetInstance(seq[0].ToAsn1Object()); + m_certs = Asn1Sequence.GetInstance(seq[0].ToAsn1Object()); if (seq.Count > 1) { - this.policies = Asn1Sequence.GetInstance(seq[1].ToAsn1Object()); + m_policies = Asn1Sequence.GetInstance(seq[1].ToAsn1Object()); } } - public OtherSigningCertificate( - params OtherCertID[] certs) + public OtherSigningCertificate(params OtherCertID[] certs) : this(certs, null) { } - public OtherSigningCertificate( - OtherCertID[] certs, - params PolicyInformation[] policies) + public OtherSigningCertificate(OtherCertID[] certs, params PolicyInformation[] policies) { if (certs == null) - throw new ArgumentNullException("certs"); + throw new ArgumentNullException(nameof(certs)); - this.certs = new DerSequence(certs); + m_certs = new DerSequence(certs); if (policies != null) { - this.policies = new DerSequence(policies); + m_policies = new DerSequence(policies); } } - public OtherSigningCertificate( - IEnumerable<OtherCertID> certs) + public OtherSigningCertificate(IEnumerable<OtherCertID> certs) : this(certs, null) { } - public OtherSigningCertificate( - IEnumerable<OtherCertID> certs, - IEnumerable<PolicyInformation> policies) + public OtherSigningCertificate(IEnumerable<OtherCertID> certs, IEnumerable<PolicyInformation> policies) { if (certs == null) - throw new ArgumentNullException("certs"); + throw new ArgumentNullException(nameof(certs)); - this.certs = new DerSequence( - Asn1EncodableVector.FromEnumerable(certs)); + m_certs = new DerSequence(Asn1EncodableVector.FromEnumerable(certs)); if (policies != null) { - this.policies = new DerSequence( - Asn1EncodableVector.FromEnumerable(policies)); + m_policies = new DerSequence(Asn1EncodableVector.FromEnumerable(policies)); } } public OtherCertID[] GetCerts() { - OtherCertID[] cs = new OtherCertID[certs.Count]; - for (int i = 0; i < certs.Count; ++i) - { - cs[i] = OtherCertID.GetInstance(certs[i].ToAsn1Object()); - } - return cs; + return m_certs.MapElements(element => OtherCertID.GetInstance(element.ToAsn1Object())); } public PolicyInformation[] GetPolicies() { - if (policies == null) - return null; - - PolicyInformation[] ps = new PolicyInformation[policies.Count]; - for (int i = 0; i < policies.Count; ++i) - { - ps[i] = PolicyInformation.GetInstance(policies[i].ToAsn1Object()); - } - return ps; + return m_policies?.MapElements(element => PolicyInformation.GetInstance(element.ToAsn1Object())); } public override Asn1Object ToAsn1Object() { - Asn1EncodableVector v = new Asn1EncodableVector(certs); - v.AddOptional(policies); + Asn1EncodableVector v = new Asn1EncodableVector(m_certs); + v.AddOptional(m_policies); return new DerSequence(v); } } diff --git a/crypto/src/asn1/esf/RevocationValues.cs b/crypto/src/asn1/esf/RevocationValues.cs index 682728dde..61bfd0be1 100644 --- a/crypto/src/asn1/esf/RevocationValues.cs +++ b/crypto/src/asn1/esf/RevocationValues.cs @@ -19,129 +19,113 @@ namespace Org.BouncyCastle.Asn1.Esf public class RevocationValues : Asn1Encodable { - private readonly Asn1Sequence crlVals; - private readonly Asn1Sequence ocspVals; - private readonly OtherRevVals otherRevVals; + private readonly Asn1Sequence m_crlVals; + private readonly Asn1Sequence m_ocspVals; + private readonly OtherRevVals m_otherRevVals; - public static RevocationValues GetInstance( - object obj) + public static RevocationValues GetInstance(object obj) { - if (obj == null || obj is RevocationValues) - return (RevocationValues) obj; + if (obj == null) + return null; + + if (obj is RevocationValues revocationValues) + return revocationValues; return new RevocationValues(Asn1Sequence.GetInstance(obj)); } - private RevocationValues( - Asn1Sequence seq) + private RevocationValues(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); if (seq.Count > 3) - throw new ArgumentException("Bad sequence size: " + seq.Count, "seq"); + throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq)); foreach (Asn1TaggedObject taggedObj in seq) { Asn1Object asn1Obj = taggedObj.GetObject(); switch (taggedObj.TagNo) { - case 0: - Asn1Sequence crlValsSeq = (Asn1Sequence) asn1Obj; - foreach (Asn1Encodable ae in crlValsSeq) - { - CertificateList.GetInstance(ae.ToAsn1Object()); - } - this.crlVals = crlValsSeq; - break; - case 1: - Asn1Sequence ocspValsSeq = (Asn1Sequence) asn1Obj; - foreach (Asn1Encodable ae in ocspValsSeq) - { - BasicOcspResponse.GetInstance(ae.ToAsn1Object()); - } - this.ocspVals = ocspValsSeq; - break; - case 2: - this.otherRevVals = OtherRevVals.GetInstance(asn1Obj); - break; - default: - throw new ArgumentException("Illegal tag in RevocationValues", "seq"); + case 0: + Asn1Sequence crlValsSeq = (Asn1Sequence)asn1Obj; + + // Validate + crlValsSeq.MapElements(element => CertificateList.GetInstance(element.ToAsn1Object())); + + m_crlVals = crlValsSeq; + break; + case 1: + Asn1Sequence ocspValsSeq = (Asn1Sequence)asn1Obj; + + // Validate + ocspValsSeq.MapElements(element => BasicOcspResponse.GetInstance(element.ToAsn1Object())); + + m_ocspVals = ocspValsSeq; + break; + case 2: + m_otherRevVals = OtherRevVals.GetInstance(asn1Obj); + break; + default: + throw new ArgumentException("Illegal tag in RevocationValues", nameof(seq)); } } } - public RevocationValues( - CertificateList[] crlVals, - BasicOcspResponse[] ocspVals, - OtherRevVals otherRevVals) + public RevocationValues(CertificateList[] crlVals, BasicOcspResponse[] ocspVals, OtherRevVals otherRevVals) { if (crlVals != null) { - this.crlVals = new DerSequence(crlVals); + m_crlVals = new DerSequence(crlVals); } if (ocspVals != null) { - this.ocspVals = new DerSequence(ocspVals); + m_ocspVals = new DerSequence(ocspVals); } - this.otherRevVals = otherRevVals; + m_otherRevVals = otherRevVals; } - public RevocationValues( - IEnumerable<CertificateList> crlVals, - IEnumerable<BasicOcspResponse> ocspVals, + public RevocationValues(IEnumerable<CertificateList> crlVals, IEnumerable<BasicOcspResponse> ocspVals, OtherRevVals otherRevVals) { if (crlVals != null) { - this.crlVals = new DerSequence( - Asn1EncodableVector.FromEnumerable(crlVals)); + m_crlVals = new DerSequence(Asn1EncodableVector.FromEnumerable(crlVals)); } if (ocspVals != null) { - this.ocspVals = new DerSequence( - Asn1EncodableVector.FromEnumerable(ocspVals)); + m_ocspVals = new DerSequence(Asn1EncodableVector.FromEnumerable(ocspVals)); } - this.otherRevVals = otherRevVals; + m_otherRevVals = otherRevVals; } public CertificateList[] GetCrlVals() { - CertificateList[] result = new CertificateList[crlVals.Count]; - for (int i = 0; i < crlVals.Count; ++i) - { - result[i] = CertificateList.GetInstance(crlVals[i].ToAsn1Object()); - } - return result; + return m_crlVals.MapElements(element => CertificateList.GetInstance(element.ToAsn1Object())); } public BasicOcspResponse[] GetOcspVals() { - BasicOcspResponse[] result = new BasicOcspResponse[ocspVals.Count]; - for (int i = 0; i < ocspVals.Count; ++i) - { - result[i] = BasicOcspResponse.GetInstance(ocspVals[i].ToAsn1Object()); - } - return result; + return m_ocspVals.MapElements(element => BasicOcspResponse.GetInstance(element.ToAsn1Object())); } public OtherRevVals OtherRevVals { - get { return otherRevVals; } + get { return m_otherRevVals; } } public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); - v.AddOptionalTagged(true, 0, crlVals); - v.AddOptionalTagged(true, 1, ocspVals); + v.AddOptionalTagged(true, 0, m_crlVals); + v.AddOptionalTagged(true, 1, m_ocspVals); - if (otherRevVals != null) + if (m_otherRevVals != null) { - v.Add(new DerTaggedObject(true, 2, otherRevVals.ToAsn1Object())); + v.Add(new DerTaggedObject(true, 2, m_otherRevVals.ToAsn1Object())); } return new DerSequence(v); diff --git a/crypto/src/asn1/esf/SignaturePolicyId.cs b/crypto/src/asn1/esf/SignaturePolicyId.cs index 21bb40560..84e9f8c62 100644 --- a/crypto/src/asn1/esf/SignaturePolicyId.cs +++ b/crypto/src/asn1/esf/SignaturePolicyId.cs @@ -21,119 +21,103 @@ namespace Org.BouncyCastle.Asn1.Esf public class SignaturePolicyId : Asn1Encodable { - private readonly DerObjectIdentifier sigPolicyIdentifier; - private readonly OtherHashAlgAndValue sigPolicyHash; - private readonly Asn1Sequence sigPolicyQualifiers; + private readonly DerObjectIdentifier m_sigPolicyIdentifier; + private readonly OtherHashAlgAndValue m_sigPolicyHash; + private readonly Asn1Sequence m_sigPolicyQualifiers; - public static SignaturePolicyId GetInstance( - object obj) + public static SignaturePolicyId GetInstance(object obj) { - if (obj == null || obj is SignaturePolicyId) - return (SignaturePolicyId) obj; + if (obj == null) + return null; + + if (obj is SignaturePolicyId signaturePolicyId) + return signaturePolicyId; - if (obj is Asn1Sequence) - return new SignaturePolicyId((Asn1Sequence) obj); + if (obj is Asn1Sequence asn1Sequence) + return new SignaturePolicyId(asn1Sequence); - throw new ArgumentException( - "Unknown object in 'SignaturePolicyId' factory: " - + Platform.GetTypeName(obj), - "obj"); + throw new ArgumentException("Unknown object in 'SignaturePolicyId' factory: " + Platform.GetTypeName(obj), + nameof(obj)); } - private SignaturePolicyId( - Asn1Sequence seq) + private SignaturePolicyId(Asn1Sequence seq) { if (seq == null) - throw new ArgumentNullException("seq"); + throw new ArgumentNullException(nameof(seq)); if (seq.Count < 2 || seq.Count > 3) - throw new ArgumentException("Bad sequence size: " + seq.Count, "seq"); + throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq)); - this.sigPolicyIdentifier = (DerObjectIdentifier) seq[0].ToAsn1Object(); - this.sigPolicyHash = OtherHashAlgAndValue.GetInstance(seq[1].ToAsn1Object()); + m_sigPolicyIdentifier = (DerObjectIdentifier)seq[0].ToAsn1Object(); + m_sigPolicyHash = OtherHashAlgAndValue.GetInstance(seq[1].ToAsn1Object()); if (seq.Count > 2) { - this.sigPolicyQualifiers = (Asn1Sequence) seq[2].ToAsn1Object(); + m_sigPolicyQualifiers = (Asn1Sequence)seq[2].ToAsn1Object(); } } - public SignaturePolicyId( - DerObjectIdentifier sigPolicyIdentifier, - OtherHashAlgAndValue sigPolicyHash) + public SignaturePolicyId(DerObjectIdentifier sigPolicyIdentifier, OtherHashAlgAndValue sigPolicyHash) : this(sigPolicyIdentifier, sigPolicyHash, null) { } - public SignaturePolicyId( - DerObjectIdentifier sigPolicyIdentifier, - OtherHashAlgAndValue sigPolicyHash, + public SignaturePolicyId(DerObjectIdentifier sigPolicyIdentifier, OtherHashAlgAndValue sigPolicyHash, params SigPolicyQualifierInfo[] sigPolicyQualifiers) { if (sigPolicyIdentifier == null) - throw new ArgumentNullException("sigPolicyIdentifier"); + throw new ArgumentNullException(nameof(sigPolicyIdentifier)); if (sigPolicyHash == null) - throw new ArgumentNullException("sigPolicyHash"); + throw new ArgumentNullException(nameof(sigPolicyHash)); - this.sigPolicyIdentifier = sigPolicyIdentifier; - this.sigPolicyHash = sigPolicyHash; + m_sigPolicyIdentifier = sigPolicyIdentifier; + m_sigPolicyHash = sigPolicyHash; if (sigPolicyQualifiers != null) { - this.sigPolicyQualifiers = new DerSequence(sigPolicyQualifiers); + m_sigPolicyQualifiers = new DerSequence(sigPolicyQualifiers); } } - public SignaturePolicyId( - DerObjectIdentifier sigPolicyIdentifier, - OtherHashAlgAndValue sigPolicyHash, + public SignaturePolicyId(DerObjectIdentifier sigPolicyIdentifier, OtherHashAlgAndValue sigPolicyHash, IEnumerable<SigPolicyQualifierInfo> sigPolicyQualifiers) { - if (sigPolicyIdentifier == null) - throw new ArgumentNullException("sigPolicyIdentifier"); - if (sigPolicyHash == null) - throw new ArgumentNullException("sigPolicyHash"); + if (sigPolicyIdentifier == null) + throw new ArgumentNullException(nameof(sigPolicyIdentifier)); + if (sigPolicyHash == null) + throw new ArgumentNullException(nameof(sigPolicyHash)); - this.sigPolicyIdentifier = sigPolicyIdentifier; - this.sigPolicyHash = sigPolicyHash; + m_sigPolicyIdentifier = sigPolicyIdentifier; + m_sigPolicyHash = sigPolicyHash; if (sigPolicyQualifiers != null) { - this.sigPolicyQualifiers = new DerSequence( - Asn1EncodableVector.FromEnumerable(sigPolicyQualifiers)); + m_sigPolicyQualifiers = new DerSequence(Asn1EncodableVector.FromEnumerable(sigPolicyQualifiers)); } } public DerObjectIdentifier SigPolicyIdentifier { - get { return sigPolicyIdentifier; } + get { return m_sigPolicyIdentifier; } } public OtherHashAlgAndValue SigPolicyHash { - get { return sigPolicyHash; } + get { return m_sigPolicyHash; } } public SigPolicyQualifierInfo[] GetSigPolicyQualifiers() { - if (sigPolicyQualifiers == null) - return null; - - SigPolicyQualifierInfo[] infos = new SigPolicyQualifierInfo[sigPolicyQualifiers.Count]; - for (int i = 0; i < sigPolicyQualifiers.Count; ++i) - { - infos[i] = SigPolicyQualifierInfo.GetInstance(sigPolicyQualifiers[i]); - } - return infos; + return m_sigPolicyQualifiers?.MapElements(SigPolicyQualifierInfo.GetInstance); } public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector( - sigPolicyIdentifier, sigPolicyHash.ToAsn1Object()); + m_sigPolicyIdentifier, m_sigPolicyHash.ToAsn1Object()); - if (sigPolicyQualifiers != null) + if (m_sigPolicyQualifiers != null) { - v.Add(sigPolicyQualifiers.ToAsn1Object()); + v.Add(m_sigPolicyQualifiers.ToAsn1Object()); } return new DerSequence(v); diff --git a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs index c9c96cbda..471776630 100644 --- a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs +++ b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs @@ -33,20 +33,17 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 DateOfBirth = 2 }; - private readonly Asn1TaggedObject declaration; + private readonly Asn1TaggedObject m_declaration; - public DeclarationOfMajority( - int notYoungerThan) + public DeclarationOfMajority(int notYoungerThan) { - declaration = new DerTaggedObject(false, 0, new DerInteger(notYoungerThan)); + m_declaration = new DerTaggedObject(false, 0, new DerInteger(notYoungerThan)); } - public DeclarationOfMajority( - bool fullAge, - string country) + public DeclarationOfMajority(bool fullAge, string country) { if (country.Length > 2) - throw new ArgumentException("country can only be 2 characters"); + throw new ArgumentException("country can only be 2 characters", nameof(country)); DerPrintableString countryString = new DerPrintableString(country, true); @@ -60,38 +57,34 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 seq = new DerSequence(DerBoolean.False, countryString); } - this.declaration = new DerTaggedObject(false, 1, seq); + m_declaration = new DerTaggedObject(false, 1, seq); } - public DeclarationOfMajority( - Asn1GeneralizedTime dateOfBirth) + public DeclarationOfMajority(Asn1GeneralizedTime dateOfBirth) { - this.declaration = new DerTaggedObject(false, 2, dateOfBirth); + m_declaration = new DerTaggedObject(false, 2, dateOfBirth); } - public static DeclarationOfMajority GetInstance( - object obj) + public static DeclarationOfMajority GetInstance(object obj) { - if (obj == null || obj is DeclarationOfMajority) - { - return (DeclarationOfMajority) obj; - } + if (obj == null) + return null; - if (obj is Asn1TaggedObject) - { - return new DeclarationOfMajority((Asn1TaggedObject) obj); - } + if (obj is DeclarationOfMajority declarationOfMajority) + return declarationOfMajority; + + if (obj is Asn1TaggedObject asn1TaggedObject) + return new DeclarationOfMajority(asn1TaggedObject); - throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); + throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), nameof(obj)); } - private DeclarationOfMajority( - Asn1TaggedObject o) + private DeclarationOfMajority(Asn1TaggedObject o) { if (o.TagNo > 2) throw new ArgumentException("Bad tag number: " + o.TagNo); - this.declaration = o; + m_declaration = o; } /** @@ -116,12 +109,12 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 */ public override Asn1Object ToAsn1Object() { - return declaration; + return m_declaration; } public Choice Type { - get { return (Choice) declaration.TagNo; } + get { return (Choice)m_declaration.TagNo; } } /** @@ -131,12 +124,12 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 { get { - switch ((Choice) declaration.TagNo) + switch (Type) { - case Choice.NotYoungerThan: - return DerInteger.GetInstance(declaration, false).IntValueExact; - default: - return -1; + case Choice.NotYoungerThan: + return DerInteger.GetInstance(m_declaration, false).IntValueExact; + default: + return -1; } } } @@ -145,12 +138,12 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 { get { - switch ((Choice) declaration.TagNo) + switch (Type) { - case Choice.FullAgeAtCountry: - return Asn1Sequence.GetInstance(declaration, false); - default: - return null; + case Choice.FullAgeAtCountry: + return Asn1Sequence.GetInstance(m_declaration, false); + default: + return null; } } } @@ -159,12 +152,12 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 { get { - switch ((Choice) declaration.TagNo) + switch (Type) { - case Choice.DateOfBirth: - return Asn1GeneralizedTime.GetInstance(declaration, false); - default: - return null; + case Choice.DateOfBirth: + return Asn1GeneralizedTime.GetInstance(m_declaration, false); + default: + return null; } } } diff --git a/crypto/src/asn1/tsp/TSTInfo.cs b/crypto/src/asn1/tsp/TSTInfo.cs index dde11494c..c37208d3c 100644 --- a/crypto/src/asn1/tsp/TSTInfo.cs +++ b/crypto/src/asn1/tsp/TSTInfo.cs @@ -64,14 +64,14 @@ namespace Org.BouncyCastle.Asn1.Tsp switch (tagged.TagNo) { - case 0: - tsa = GeneralName.GetInstance(tagged, true); - break; - case 1: - extensions = X509Extensions.GetInstance(tagged, false); - break; - default: - throw new ArgumentException("Unknown tag value " + tagged.TagNo); + case 0: + tsa = GeneralName.GetInstance(tagged, true); + break; + case 1: + extensions = X509Extensions.GetInstance(tagged, false); + break; + default: + throw new ArgumentException("Unknown tag value " + tagged.TagNo); } } diff --git a/crypto/src/asn1/x509/Time.cs b/crypto/src/asn1/x509/Time.cs index e03055f6d..f7746257c 100644 --- a/crypto/src/asn1/x509/Time.cs +++ b/crypto/src/asn1/x509/Time.cs @@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Asn1.X509 public Time(Asn1GeneralizedTime generalizedTime) { - this.m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime)); + m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime)); } public Time(Asn1UtcTime utcTime) @@ -42,7 +42,7 @@ namespace Org.BouncyCastle.Asn1.X509 // Validate utcTime is in the appropriate year range utcTime.ToDateTime(2049); - this.m_timeObject = utcTime; + m_timeObject = utcTime; } /** |