summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/asn1/Asn1Utilities.cs5
-rw-r--r--crypto/src/asn1/DERExternal.cs2
-rw-r--r--crypto/src/asn1/cmp/CertStatus.cs2
-rw-r--r--crypto/src/asn1/cmp/CrlSource.cs2
-rw-r--r--crypto/src/asn1/cmp/OobCertHash.cs2
-rw-r--r--crypto/src/asn1/cms/EncryptedData.cs27
-rw-r--r--crypto/src/asn1/crmf/PKIPublicationInfo.cs8
-rw-r--r--crypto/src/asn1/ocsp/CertStatus.cs2
-rw-r--r--crypto/src/asn1/pkcs/PBES2Parameters.cs2
-rw-r--r--crypto/src/asn1/tsp/ArchiveTimeStamp.cs9
-rw-r--r--crypto/src/asn1/x509/IssuerSerial.cs37
-rw-r--r--crypto/src/asn1/x509/PolicyInformation.cs25
-rw-r--r--crypto/src/asn1/x509/X509Extensions.cs9
-rw-r--r--crypto/src/asn1/x509/X509Name.cs4
14 files changed, 58 insertions, 78 deletions
diff --git a/crypto/src/asn1/Asn1Utilities.cs b/crypto/src/asn1/Asn1Utilities.cs
index ea61ab01a..d3b6c2f00 100644
--- a/crypto/src/asn1/Asn1Utilities.cs
+++ b/crypto/src/asn1/Asn1Utilities.cs
@@ -668,8 +668,9 @@ namespace Org.BouncyCastle.Asn1
                 sequence[sequencePosition] is Asn1TaggedObject taggedObject &&
                 taggedObject.HasTag(tagClass, tagNo))
             {
+                var result = constructor(taggedObject, state);
                 sequencePosition++;
-                return constructor(taggedObject, state);
+                return result;
             }
 
             return null;
@@ -691,8 +692,8 @@ namespace Org.BouncyCastle.Asn1
                 sequence[sequencePosition] is Asn1TaggedObject taggedObject &&
                 taggedObject.HasTag(tagClass, tagNo))
             {
-                sequencePosition++;
                 result = constructor(taggedObject, state);
+                sequencePosition++;
                 return true;
             }
 
diff --git a/crypto/src/asn1/DERExternal.cs b/crypto/src/asn1/DERExternal.cs
index 975cd1665..be1223f98 100644
--- a/crypto/src/asn1/DERExternal.cs
+++ b/crypto/src/asn1/DERExternal.cs
@@ -262,7 +262,7 @@ namespace Org.BouncyCastle.Asn1
             case 2:
                 return DerBitString.GetInstance(encoding, false);
             default:
-                throw new ArgumentException("invalid tag: " + Asn1Utilities.GetTagText(encoding), nameof(encoding));
+                throw new ArgumentException("unknown tag: " + Asn1Utilities.GetTagText(encoding), nameof(encoding));
             }
         }
 
diff --git a/crypto/src/asn1/cmp/CertStatus.cs b/crypto/src/asn1/cmp/CertStatus.cs
index 29db607d7..488f78376 100644
--- a/crypto/src/asn1/cmp/CertStatus.cs
+++ b/crypto/src/asn1/cmp/CertStatus.cs
@@ -44,7 +44,7 @@ namespace Org.BouncyCastle.Asn1.Cmp
 					if (p is Asn1TaggedObject dto)
 					{
 						if (!dto.HasContextTag(0))
-							throw new ArgumentException("unknown tag " + Asn1Utilities.GetTagText(dto));
+							throw new ArgumentException("unknown tag: " + Asn1Utilities.GetTagText(dto));
 
 						m_hashAlg = AlgorithmIdentifier.GetInstance(dto, true);
 					}
diff --git a/crypto/src/asn1/cmp/CrlSource.cs b/crypto/src/asn1/cmp/CrlSource.cs
index 61fc96b32..4b6b396d9 100644
--- a/crypto/src/asn1/cmp/CrlSource.cs
+++ b/crypto/src/asn1/cmp/CrlSource.cs
@@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Asn1.Cmp
             }
             else
             {
-                throw new ArgumentException("unknown tag " + Asn1Utilities.GetTagText(taggedObject),
+                throw new ArgumentException("unknown tag: " + Asn1Utilities.GetTagText(taggedObject),
                     nameof(taggedObject));
             }
         }
diff --git a/crypto/src/asn1/cmp/OobCertHash.cs b/crypto/src/asn1/cmp/OobCertHash.cs
index 1e6224612..209113167 100644
--- a/crypto/src/asn1/cmp/OobCertHash.cs
+++ b/crypto/src/asn1/cmp/OobCertHash.cs
@@ -58,7 +58,7 @@ namespace Org.BouncyCastle.Asn1.Cmp
 				}
 				else
 				{
-                    throw new ArgumentException("unknown tag " + Asn1Utilities.GetTagText(tObj));
+                    throw new ArgumentException("unknown tag: " + Asn1Utilities.GetTagText(tObj));
                 }
             }
 		}
diff --git a/crypto/src/asn1/cms/EncryptedData.cs b/crypto/src/asn1/cms/EncryptedData.cs
index a79f8119b..4136994e6 100644
--- a/crypto/src/asn1/cms/EncryptedData.cs
+++ b/crypto/src/asn1/cms/EncryptedData.cs
@@ -41,24 +41,23 @@ namespace Org.BouncyCastle.Asn1.Cms
 			this.unprotectedAttrs = unprotectedAttrs;
 		}
 
-		private EncryptedData(
-			Asn1Sequence seq)
+		private EncryptedData(Asn1Sequence seq)
 		{
-			if (seq == null)
-				throw new ArgumentNullException("seq");
-			if (seq.Count < 2 || seq.Count > 3)
-				throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
+			int count = seq.Count;
+			if (count < 2 || count > 3)
+				throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			this.version = DerInteger.GetInstance(seq[0]);
-			this.encryptedContentInfo = EncryptedContentInfo.GetInstance(seq[1]);
+			int pos = 0;
 
-			if (seq.Count > 2)
-			{
-                this.unprotectedAttrs = Asn1Set.GetInstance((Asn1TaggedObject)seq[2], false);
-            }
-		}
+			this.version = DerInteger.GetInstance(seq[pos++]);
+			this.encryptedContentInfo = EncryptedContentInfo.GetInstance(seq[pos++]);
+			this.unprotectedAttrs = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetInstance);
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
+        }
 
-		public virtual DerInteger Version
+        public virtual DerInteger Version
 		{
 			get { return version; }
 		}
diff --git a/crypto/src/asn1/crmf/PKIPublicationInfo.cs b/crypto/src/asn1/crmf/PKIPublicationInfo.cs
index c855a7d28..aac6d9911 100644
--- a/crypto/src/asn1/crmf/PKIPublicationInfo.cs
+++ b/crypto/src/asn1/crmf/PKIPublicationInfo.cs
@@ -80,13 +80,7 @@ namespace Org.BouncyCastle.Asn1.Crmf
 
         public virtual DerInteger Action => m_action;
 
-        public virtual SinglePubInfo[] GetPubInfos()
-        {
-            if (m_pubInfos == null)
-                return null;
-
-            return m_pubInfos.MapElements(SinglePubInfo.GetInstance);
-        }
+        public virtual SinglePubInfo[] GetPubInfos() => m_pubInfos?.MapElements(SinglePubInfo.GetInstance);
 
         /**
          * <pre>
diff --git a/crypto/src/asn1/ocsp/CertStatus.cs b/crypto/src/asn1/ocsp/CertStatus.cs
index e1d06ed19..7ce44ea3f 100644
--- a/crypto/src/asn1/ocsp/CertStatus.cs
+++ b/crypto/src/asn1/ocsp/CertStatus.cs
@@ -34,7 +34,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp
                 }
             }
 
-            throw new ArgumentException("Unknown tag encountered: " + Asn1Utilities.GetTagText(choice));
+            throw new ArgumentException("unknown tag: " + Asn1Utilities.GetTagText(choice), nameof(choice));
         }
 
         private readonly int m_tagNo;
diff --git a/crypto/src/asn1/pkcs/PBES2Parameters.cs b/crypto/src/asn1/pkcs/PBES2Parameters.cs
index 66e4755c8..7d8a91ffa 100644
--- a/crypto/src/asn1/pkcs/PBES2Parameters.cs
+++ b/crypto/src/asn1/pkcs/PBES2Parameters.cs
@@ -42,7 +42,7 @@ namespace Org.BouncyCastle.Asn1.Pkcs
                 func = new KeyDerivationFunc(funcSeq);
             }
 
-            scheme = EncryptionScheme.GetInstance(seq[1].ToAsn1Object());
+            scheme = EncryptionScheme.GetInstance(seq[1]);
         }
 
         public KeyDerivationFunc KeyDerivationFunc
diff --git a/crypto/src/asn1/tsp/ArchiveTimeStamp.cs b/crypto/src/asn1/tsp/ArchiveTimeStamp.cs
index dbc426bc8..e1be303b7 100644
--- a/crypto/src/asn1/tsp/ArchiveTimeStamp.cs
+++ b/crypto/src/asn1/tsp/ArchiveTimeStamp.cs
@@ -153,13 +153,8 @@ namespace Org.BouncyCastle.Asn1.Tsp
             return PartialHashtree.GetInstance(m_reducedHashTree[0]);
         }
 
-        public virtual PartialHashtree[] GetReducedHashTree()
-        {
-            if (m_reducedHashTree == null)
-                return null;
-
-            return m_reducedHashTree.MapElements(PartialHashtree.GetInstance);
-        }
+        public virtual PartialHashtree[] GetReducedHashTree() =>
+            m_reducedHashTree?.MapElements(PartialHashtree.GetInstance);
 
         public virtual ContentInfo TimeStamp => m_timeStamp;
 
diff --git a/crypto/src/asn1/x509/IssuerSerial.cs b/crypto/src/asn1/x509/IssuerSerial.cs
index 2c26339bd..09b6dae18 100644
--- a/crypto/src/asn1/x509/IssuerSerial.cs
+++ b/crypto/src/asn1/x509/IssuerSerial.cs
@@ -1,40 +1,29 @@
 using System;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
     public class IssuerSerial
         : Asn1Encodable
     {
-        internal readonly GeneralNames	issuer;
-        internal readonly DerInteger	serial;
-        internal readonly DerBitString	issuerUid;
-
-		public static IssuerSerial GetInstance(
-            object obj)
+		public static IssuerSerial GetInstance(object obj)
         {
-            if (obj == null || obj is IssuerSerial)
-            {
-                return (IssuerSerial) obj;
-            }
-
-			if (obj is Asn1Sequence)
-            {
-                return new IssuerSerial((Asn1Sequence) obj);
-            }
-
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
+            if (obj == null)
+                return null;
+            if (obj is IssuerSerial issuerSerial)
+                return issuerSerial;
+            return new IssuerSerial(Asn1Sequence.GetInstance(obj));
 		}
 
-        public static IssuerSerial GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+        public static IssuerSerial GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
+            return new IssuerSerial(Asn1Sequence.GetInstance(obj, explicitly));
         }
 
-		private IssuerSerial(
+        private readonly GeneralNames issuer;
+        private readonly DerInteger serial;
+        private readonly DerBitString issuerUid;
+
+        private IssuerSerial(
             Asn1Sequence seq)
         {
 			if (seq.Count != 2 && seq.Count != 3)
diff --git a/crypto/src/asn1/x509/PolicyInformation.cs b/crypto/src/asn1/x509/PolicyInformation.cs
index 90db29a36..fca5fd03b 100644
--- a/crypto/src/asn1/x509/PolicyInformation.cs
+++ b/crypto/src/asn1/x509/PolicyInformation.cs
@@ -5,6 +5,20 @@ namespace Org.BouncyCastle.Asn1.X509
     public class PolicyInformation
         : Asn1Encodable
     {
+        public static PolicyInformation GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is PolicyInformation policyInformation)
+                return policyInformation;
+            return new PolicyInformation(Asn1Sequence.GetInstance(obj));
+        }
+
+        public static PolicyInformation GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        {
+            return new PolicyInformation(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+        }
+
         private readonly DerObjectIdentifier	policyIdentifier;
         private readonly Asn1Sequence			policyQualifiers;
 
@@ -38,17 +52,6 @@ namespace Org.BouncyCastle.Asn1.X509
             this.policyQualifiers = policyQualifiers;
         }
 
-		public static PolicyInformation GetInstance(
-            object obj)
-        {
-            if (obj == null || obj is PolicyInformation)
-            {
-                return (PolicyInformation) obj;
-            }
-
-			return new PolicyInformation(Asn1Sequence.GetInstance(obj));
-        }
-
 		public DerObjectIdentifier PolicyIdentifier
 		{
 			get { return policyIdentifier; }
diff --git a/crypto/src/asn1/x509/X509Extensions.cs b/crypto/src/asn1/x509/X509Extensions.cs
index 5d83ecdc6..2893f8b79 100644
--- a/crypto/src/asn1/x509/X509Extensions.cs
+++ b/crypto/src/asn1/x509/X509Extensions.cs
@@ -233,17 +233,16 @@ namespace Org.BouncyCastle.Asn1.X509
 
 			foreach (Asn1Encodable ae in seq)
 			{
-				Asn1Sequence s = Asn1Sequence.GetInstance(ae.ToAsn1Object());
+				Asn1Sequence s = Asn1Sequence.GetInstance(ae);
 
 				if (s.Count < 2 || s.Count > 3)
 					throw new ArgumentException("Bad sequence size: " + s.Count);
 
-				DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(s[0].ToAsn1Object());
+				DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(s[0]);
 
-				bool isCritical = s.Count == 3
-					&& DerBoolean.GetInstance(s[1].ToAsn1Object()).IsTrue;
+				bool isCritical = s.Count == 3 && DerBoolean.GetInstance(s[1]).IsTrue;
 
-				Asn1OctetString octets = Asn1OctetString.GetInstance(s[s.Count - 1].ToAsn1Object());
+				Asn1OctetString octets = Asn1OctetString.GetInstance(s[s.Count - 1]);
 
                 if (m_extensions.ContainsKey(oid))
                     throw new ArgumentException("repeated extension found: " + oid);
diff --git a/crypto/src/asn1/x509/X509Name.cs b/crypto/src/asn1/x509/X509Name.cs
index 6135ad2fa..8f25e49ef 100644
--- a/crypto/src/asn1/x509/X509Name.cs
+++ b/crypto/src/asn1/x509/X509Name.cs
@@ -360,7 +360,7 @@ namespace Org.BouncyCastle.Asn1.X509
             foreach (Asn1Encodable asn1Obj in seq)
             {
                 // RelativeDistinguishedName ::= SET SIZE(1..MAX) OF AttributeTypeAndValue
-                Asn1Set rdn = Asn1Set.GetInstance(asn1Obj.ToAsn1Object());
+                Asn1Set rdn = Asn1Set.GetInstance(asn1Obj);
 
                 // TODO Apply this check? (Currently "breaks" CertificateTest.CheckDudCertificate)
                 //if (rdn.Count < 1)
@@ -368,7 +368,7 @@ namespace Org.BouncyCastle.Asn1.X509
 
                 for (int i = 0; i < rdn.Count; ++i)
                 {
-                    Asn1Sequence attributeTypeAndValue = Asn1Sequence.GetInstance(rdn[i].ToAsn1Object());
+                    Asn1Sequence attributeTypeAndValue = Asn1Sequence.GetInstance(rdn[i]);
                     if (attributeTypeAndValue.Count != 2)
                         throw new ArgumentException("badly sized AttributeTypeAndValue");