summary refs log tree commit diff
path: root/crypto/src/asn1/esf/RevocationValues.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/esf/RevocationValues.cs')
-rw-r--r--crypto/src/asn1/esf/RevocationValues.cs122
1 files changed, 47 insertions, 75 deletions
diff --git a/crypto/src/asn1/esf/RevocationValues.cs b/crypto/src/asn1/esf/RevocationValues.cs
index 497bf15f2..eef246ee8 100644
--- a/crypto/src/asn1/esf/RevocationValues.cs
+++ b/crypto/src/asn1/esf/RevocationValues.cs
@@ -6,81 +6,67 @@ using Org.BouncyCastle.Asn1.X509;
 
 namespace Org.BouncyCastle.Asn1.Esf
 {
-	/// <remarks>
-	/// RFC 5126: 6.3.4.  revocation-values Attribute Definition
-	/// <code>
-	/// RevocationValues ::=  SEQUENCE {
-	///		crlVals			[0] SEQUENCE OF CertificateList     OPTIONAL,
-	///		ocspVals		[1] SEQUENCE OF BasicOCSPResponse   OPTIONAL,
-	///		otherRevVals	[2] OtherRevVals OPTIONAL
-	/// }
-	/// </code>
-	/// </remarks>
-	public class RevocationValues
+    /// <remarks>
+    /// RFC 5126: 6.3.4.  revocation-values Attribute Definition
+    /// <code>
+    /// RevocationValues ::=  SEQUENCE {
+    ///		crlVals			[0] SEQUENCE OF CertificateList     OPTIONAL,
+    ///		ocspVals		[1] SEQUENCE OF BasicOCSPResponse   OPTIONAL,
+    ///		otherRevVals	[2] OtherRevVals OPTIONAL
+    /// }
+    /// </code>
+    /// </remarks>
+    public class RevocationValues
 		: Asn1Encodable
 	{
-		private readonly Asn1Sequence m_crlVals;
-		private readonly Asn1Sequence m_ocspVals;
-		private readonly OtherRevVals m_otherRevVals;
-
 		public static RevocationValues GetInstance(object obj)
 		{
             if (obj == null)
                 return null;
-
             if (obj is RevocationValues revocationValues)
 				return revocationValues;
-
 			return new RevocationValues(Asn1Sequence.GetInstance(obj));
 		}
 
-		private RevocationValues(Asn1Sequence seq)
+        public static RevocationValues GetInstance(Asn1TaggedObject obj, bool explicitly)
+        {
+            return new RevocationValues(Asn1Sequence.GetInstance(obj, explicitly));
+        }
+
+        private readonly Asn1Sequence m_crlVals;
+        private readonly Asn1Sequence m_ocspVals;
+        private readonly OtherRevVals m_otherRevVals;
+
+        private RevocationValues(Asn1Sequence seq)
 		{
-			if (seq == null)
-				throw new ArgumentNullException(nameof(seq));
-			if (seq.Count > 3)
-				throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq));
+			int count = seq.Count;
+			if (count < 0 || count > 3)
+				throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			foreach (var element in seq)
-			{
-				var o = Asn1TaggedObject.GetInstance(element, Asn1Tags.ContextSpecific);
-				switch (o.TagNo)
-				{
-				case 0:
-					Asn1Sequence crlValsSeq = (Asn1Sequence)o.GetExplicitBaseObject();
-
-					// Validate
-					crlValsSeq.MapElements(CertificateList.GetInstance);
-
-					m_crlVals = crlValsSeq;
-					break;
-				case 1:
-					Asn1Sequence ocspValsSeq = (Asn1Sequence)o.GetExplicitBaseObject();
-
-					// Validate
-					ocspValsSeq.MapElements(BasicOcspResponse.GetInstance);
-
-					m_ocspVals = ocspValsSeq;
-					break;
-				case 2:
-					m_otherRevVals = OtherRevVals.GetInstance(o.GetExplicitBaseObject());
-					break;
-				default:
-					throw new ArgumentException("Illegal tag in RevocationValues", nameof(seq));
-				}
-			}
-		}
+			int pos = 0;
+
+			m_crlVals = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, Asn1Sequence.GetInstance);
+            m_crlVals?.MapElements(CertificateList.GetInstance); // Validate
+
+            m_ocspVals = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, Asn1Sequence.GetInstance);
+            m_ocspVals?.MapElements(BasicOcspResponse.GetInstance); // Validate
+
+            m_otherRevVals = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, OtherRevVals.GetInstance);
 
-		public RevocationValues(CertificateList[] crlVals, BasicOcspResponse[] ocspVals, OtherRevVals otherRevVals)
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
+        }
+
+        public RevocationValues(CertificateList[] crlVals, BasicOcspResponse[] ocspVals, OtherRevVals otherRevVals)
 		{
 			if (crlVals != null)
 			{
-				m_crlVals = new DerSequence(crlVals);
+				m_crlVals = DerSequence.FromElements(crlVals);
 			}
 
 			if (ocspVals != null)
 			{
-				m_ocspVals = new DerSequence(ocspVals);
+				m_ocspVals = DerSequence.FromElements(ocspVals);
 			}
 
 			m_otherRevVals = otherRevVals;
@@ -91,44 +77,30 @@ namespace Org.BouncyCastle.Asn1.Esf
 		{
 			if (crlVals != null)
 			{
-				m_crlVals = new DerSequence(Asn1EncodableVector.FromEnumerable(crlVals));
+				m_crlVals = DerSequence.FromVector(Asn1EncodableVector.FromEnumerable(crlVals));
 			}
 
 			if (ocspVals != null)
 			{
-				m_ocspVals = new DerSequence(Asn1EncodableVector.FromEnumerable(ocspVals));
+				m_ocspVals = DerSequence.FromVector(Asn1EncodableVector.FromEnumerable(ocspVals));
 			}
 
 			m_otherRevVals = otherRevVals;
 		}
 
-		public CertificateList[] GetCrlVals()
-		{
-			return m_crlVals.MapElements(element => CertificateList.GetInstance(element.ToAsn1Object()));
-		}
+		public CertificateList[] GetCrlVals() => m_crlVals?.MapElements(CertificateList.GetInstance);
 
-		public BasicOcspResponse[] GetOcspVals()
-		{
-            return m_ocspVals.MapElements(element => BasicOcspResponse.GetInstance(element.ToAsn1Object()));
-		}
+		public BasicOcspResponse[] GetOcspVals() => m_ocspVals?.MapElements(BasicOcspResponse.GetInstance);
 
-		public OtherRevVals OtherRevVals
-		{
-			get { return m_otherRevVals; }
-		}
+		public OtherRevVals OtherRevVals => m_otherRevVals;
 
 		public override Asn1Object ToAsn1Object()
 		{
 			Asn1EncodableVector v = new Asn1EncodableVector(3);
             v.AddOptionalTagged(true, 0, m_crlVals);
             v.AddOptionalTagged(true, 1, m_ocspVals);
-
-            if (m_otherRevVals != null)
-			{
-				v.Add(new DerTaggedObject(true, 2, m_otherRevVals.ToAsn1Object()));
-			}
-
-            return new DerSequence(v);
+            v.AddOptionalTagged(true, 2, m_otherRevVals);
+            return DerSequence.FromVector(v);
 		}
 	}
 }