summary refs log tree commit diff
path: root/crypto/src/asn1/cmp/CmpCertificate.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-09-20 14:07:11 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-09-20 14:07:11 +0700
commitc17e29d769cf8a87402ff4d819d334389a7faca4 (patch)
treee60811ecfc45b6c229ce2e87d8b4a6db6d9f2da9 /crypto/src/asn1/cmp/CmpCertificate.cs
parentadded randomized signing (diff)
downloadBouncyCastle.NET-ed25519-c17e29d769cf8a87402ff4d819d334389a7faca4.tar.xz
Update Asn1.Cmp from bc-java
Diffstat (limited to 'crypto/src/asn1/cmp/CmpCertificate.cs')
-rw-r--r--crypto/src/asn1/cmp/CmpCertificate.cs93
1 files changed, 62 insertions, 31 deletions
diff --git a/crypto/src/asn1/cmp/CmpCertificate.cs b/crypto/src/asn1/cmp/CmpCertificate.cs
index 33356b486..af433ec4d 100644
--- a/crypto/src/asn1/cmp/CmpCertificate.cs
+++ b/crypto/src/asn1/cmp/CmpCertificate.cs
@@ -1,4 +1,5 @@
 using System;
+using System.IO;
 
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Utilities;
@@ -8,54 +9,84 @@ namespace Org.BouncyCastle.Asn1.Cmp
     public class CmpCertificate
         : Asn1Encodable, IAsn1Choice
     {
-        private readonly X509CertificateStructure x509v3PKCert;
-        private readonly AttributeCertificate x509v2AttrCert;
-
-        /**
-         * Note: the addition of attribute certificates is a BC extension.
-         */
-        public CmpCertificate(AttributeCertificate x509v2AttrCert)
+        public static CmpCertificate GetInstance(object obj)
         {
-            this.x509v2AttrCert = x509v2AttrCert;
-        }
+            // TODO[cmp] Review this whole metho
 
-        public CmpCertificate(X509CertificateStructure x509v3PKCert)
-        {
-            if (x509v3PKCert.Version != 3)
-                throw new ArgumentException("only version 3 certificates allowed", "x509v3PKCert");
+            if (obj == null)
+                return null;
 
-            this.x509v3PKCert = x509v3PKCert;
-        }
+            if (obj is CmpCertificate cmpCertificate)
+                return cmpCertificate;
 
-        public static CmpCertificate GetInstance(object obj)
-        {
-            if (obj is CmpCertificate)
-                return (CmpCertificate)obj;
+            if (obj is byte[] bs)
+            {
+                try
+                {
+                    obj = Asn1Object.FromByteArray(bs);
+                }
+                catch (IOException)
+                {
+                    throw new ArgumentException("Invalid encoding in CmpCertificate");
+                }
+            }
 
             if (obj is Asn1Sequence)
                 return new CmpCertificate(X509CertificateStructure.GetInstance(obj));
 
-            if (obj is Asn1TaggedObject)
-                return new CmpCertificate(AttributeCertificate.GetInstance(((Asn1TaggedObject)obj).GetObject()));
+            if (obj is Asn1TaggedObject taggedObject)
+                return new CmpCertificate(taggedObject.TagNo, taggedObject.GetObject());
 
-            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
+            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), nameof(obj));
         }
 
-        public virtual bool IsX509v3PKCert
+        public static CmpCertificate GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            get { return x509v3PKCert != null; }
+            // TODO[cmp]
+            if (taggedObject == null)
+                return null;
+
+            if (!declaredExplicit)
+                throw new ArgumentException("tag must be explicit");
+
+            // TODO[cmp]
+            return GetInstance(taggedObject.GetObject());
         }
 
-        public virtual X509CertificateStructure X509v3PKCert
+        private readonly X509CertificateStructure m_x509v3PKCert;
+
+        private readonly int m_otherTagValue;
+        private readonly Asn1Encodable m_otherCert;
+
+        /**
+         * Note: the addition of other certificates is a BC extension. If you use this constructor they
+         * will be added with an explicit tag value of type.
+         *
+         * @param type      the type of the certificate (used as a tag value).
+         * @param otherCert the object representing the certificate
+         */
+        public CmpCertificate(int type, Asn1Encodable otherCert)
         {
-            get { return x509v3PKCert; }
+            m_otherTagValue = type;
+            m_otherCert = otherCert;
         }
 
-        public virtual AttributeCertificate X509v2AttrCert
+        public CmpCertificate(X509CertificateStructure x509v3PKCert)
         {
-            get { return x509v2AttrCert; }
+            if (x509v3PKCert.Version != 3)
+                throw new ArgumentException("only version 3 certificates allowed", nameof(x509v3PKCert));
+
+            m_x509v3PKCert = x509v3PKCert;
         }
 
+        public virtual bool IsX509v3PKCert => m_x509v3PKCert != null;
+
+        public virtual X509CertificateStructure X509v3PKCert => m_x509v3PKCert;
+
+        public virtual int OtherCertTag => m_otherTagValue;
+
+        public virtual Asn1Encodable OtherCert => m_otherCert;
+
         /**
          * <pre>
          * CMPCertificate ::= CHOICE {
@@ -69,13 +100,13 @@ namespace Org.BouncyCastle.Asn1.Cmp
          */
         public override Asn1Object ToAsn1Object()
         {
-            if (x509v2AttrCert != null)
+            if (m_otherCert != null)
             {
                 // explicit following CMP conventions
-                return new DerTaggedObject(true, 1, x509v2AttrCert);
+                return new DerTaggedObject(true, m_otherTagValue, m_otherCert);
             }
 
-            return x509v3PKCert.ToAsn1Object();
+            return m_x509v3PKCert.ToAsn1Object();
         }
     }
 }