summary refs log tree commit diff
path: root/crypto/src/asn1/pkcs/SignedData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/pkcs/SignedData.cs')
-rw-r--r--crypto/src/asn1/pkcs/SignedData.cs140
1 files changed, 51 insertions, 89 deletions
diff --git a/crypto/src/asn1/pkcs/SignedData.cs b/crypto/src/asn1/pkcs/SignedData.cs
index e309d9245..de9c758fb 100644
--- a/crypto/src/asn1/pkcs/SignedData.cs
+++ b/crypto/src/asn1/pkcs/SignedData.cs
@@ -8,111 +8,72 @@ namespace Org.BouncyCastle.Asn1.Pkcs
     public class SignedData
         : Asn1Encodable
     {
-        private readonly DerInteger		version;
-        private readonly Asn1Set		digestAlgorithms;
-        private readonly ContentInfo	contentInfo;
-        private readonly Asn1Set		certificates;
-        private readonly Asn1Set		crls;
-        private readonly Asn1Set		signerInfos;
-
         public static SignedData GetInstance(object obj)
         {
             if (obj == null)
                 return null;
-            SignedData existing = obj as SignedData;
-            if (existing != null)
-                return existing;
+            if (obj is SignedData signedData)
+                return signedData;
             return new SignedData(Asn1Sequence.GetInstance(obj));
         }
 
-        public SignedData(
-            DerInteger        _version,
-            Asn1Set           _digestAlgorithms,
-            ContentInfo       _contentInfo,
-            Asn1Set           _certificates,
-            Asn1Set           _crls,
-            Asn1Set           _signerInfos)
+        public static SignedData GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            version          = _version;
-            digestAlgorithms = _digestAlgorithms;
-            contentInfo      = _contentInfo;
-            certificates     = _certificates;
-            crls             = _crls;
-            signerInfos      = _signerInfos;
+            return new SignedData(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
         }
 
-        private SignedData(
-            Asn1Sequence seq)
-        {
-            var e = seq.GetEnumerator();
-
-            e.MoveNext();
-            version = (DerInteger) e.Current;
-
-            e.MoveNext();
-            digestAlgorithms = (Asn1Set) e.Current;
+        private readonly DerInteger m_version;
+        private readonly Asn1Set m_digestAlgorithms;
+        private readonly ContentInfo m_contentInfo;
+        private readonly Asn1Set m_certificates;
+        private readonly Asn1Set m_crls;
+        private readonly Asn1Set m_signerInfos;
 
-            e.MoveNext();
-            contentInfo = ContentInfo.GetInstance(e.Current);
-
-            while (e.MoveNext())
-            {
-                Asn1Object o = e.Current.ToAsn1Object();
-
-                //
-                // an interesting feature of SignedData is that there appear to be varying implementations...
-                // for the moment we ignore anything which doesn't fit.
-                //
-                if (o is Asn1TaggedObject tagged)
-                {
-                    switch (tagged.TagNo)
-                    {
-                    case 0:
-                        certificates = Asn1Set.GetInstance(tagged, false);
-                        break;
-                    case 1:
-                        crls = Asn1Set.GetInstance(tagged, false);
-                        break;
-                    default:
-                        throw new ArgumentException("unknown tag value " + tagged.TagNo);
-                    }
-                }
-                else
-                {
-                    signerInfos = (Asn1Set) o;
-                }
-            }
-        }
-
-        public DerInteger Version
+        private SignedData(Asn1Sequence seq)
         {
-            get { return version; }
+            int count = seq.Count, pos = 0;
+            if (count < 4 || count > 6)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+            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_signerInfos = Asn1Set.GetInstance(seq[pos++]);
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-        public Asn1Set DigestAlgorithms
+        // TODO[api] Improve parameter names
+        public SignedData(
+            DerInteger _version,
+            Asn1Set _digestAlgorithms,
+            ContentInfo _contentInfo,
+            Asn1Set _certificates,
+            Asn1Set _crls,
+            Asn1Set _signerInfos)
         {
-            get { return digestAlgorithms; }
+            m_version = _version ?? throw new ArgumentNullException(nameof(_version));
+            m_digestAlgorithms = _digestAlgorithms ?? throw new ArgumentNullException(nameof(_digestAlgorithms));
+            m_contentInfo = _contentInfo ?? throw new ArgumentNullException(nameof(_contentInfo));
+            m_certificates = _certificates;
+            m_crls = _crls;
+            m_signerInfos = _signerInfos ?? throw new ArgumentNullException(nameof(_signerInfos));
         }
 
-        public ContentInfo ContentInfo
-        {
-            get { return contentInfo; }
-        }
+        public DerInteger Version => m_version;
 
-        public Asn1Set Certificates
-        {
-            get { return certificates; }
-        }
+        public Asn1Set DigestAlgorithms => m_digestAlgorithms;
 
-        public Asn1Set Crls
-        {
-            get { return crls; }
-        }
+        public ContentInfo ContentInfo => m_contentInfo;
 
-        public Asn1Set SignerInfos
-        {
-            get { return signerInfos; }
-        }
+        public Asn1Set Certificates => m_certificates;
+
+        public Asn1Set Crls => m_crls;
+
+        public Asn1Set SignerInfos => m_signerInfos;
 
         /**
          * Produce an object suitable for an Asn1OutputStream.
@@ -131,10 +92,11 @@ namespace Org.BouncyCastle.Asn1.Pkcs
          */
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(version, digestAlgorithms, contentInfo);
-            v.AddOptionalTagged(false, 0, certificates);
-            v.AddOptionalTagged(false, 1, crls);
-            v.Add(signerInfos);
+            Asn1EncodableVector v = new Asn1EncodableVector(6);
+            v.Add(m_version, m_digestAlgorithms, m_contentInfo);
+            v.AddOptionalTagged(false, 0, m_certificates);
+            v.AddOptionalTagged(false, 1, m_crls);
+            v.Add(m_signerInfos);
             return new BerSequence(v);
         }
     }