summary refs log tree commit diff
path: root/crypto/src/asn1/crmf/PKIPublicationInfo.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-09-20 18:25:17 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-09-20 18:25:17 +0700
commit427206058f1c7e3e53daa31ab510f32c5a64845a (patch)
treea908048615c058f1cf08b0b063fbe7fb3d55e558 /crypto/src/asn1/crmf/PKIPublicationInfo.cs
parentCleanup (diff)
downloadBouncyCastle.NET-ed25519-427206058f1c7e3e53daa31ab510f32c5a64845a.tar.xz
Port of Asn1.Cmp tests from bc-java
Diffstat (limited to 'crypto/src/asn1/crmf/PKIPublicationInfo.cs')
-rw-r--r--crypto/src/asn1/crmf/PKIPublicationInfo.cs92
1 files changed, 69 insertions, 23 deletions
diff --git a/crypto/src/asn1/crmf/PKIPublicationInfo.cs b/crypto/src/asn1/crmf/PKIPublicationInfo.cs
index a7d2bc603..c855a7d28 100644
--- a/crypto/src/asn1/crmf/PKIPublicationInfo.cs
+++ b/crypto/src/asn1/crmf/PKIPublicationInfo.cs
@@ -1,48 +1,91 @@
-using System;
-
-using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Math;
 
 namespace Org.BouncyCastle.Asn1.Crmf
 {
+    /**
+     * <pre>
+     * PKIPublicationInfo ::= SEQUENCE {
+     *                  action     INTEGER {
+     *                                 dontPublish (0),
+     *                                 pleasePublish (1) },
+     *                  pubInfos  SEQUENCE SIZE (1..MAX) OF SinglePubInfo OPTIONAL }
+     * -- pubInfos MUST NOT be present if action is "dontPublish"
+     * -- (if action is "pleasePublish" and pubInfos is omitted,
+     * -- "dontCare" is assumed)
+     * </pre>
+     */
     public class PkiPublicationInfo
         : Asn1Encodable
     {
-        private readonly DerInteger action;
-        private readonly Asn1Sequence pubInfos;
+        public static readonly DerInteger DontPublish = new DerInteger(0);
+        public static readonly DerInteger PleasePublish = new DerInteger(1);
+
+        public static PkiPublicationInfo GetInstance(object obj)
+        {
+            if (obj is PkiPublicationInfo pkiPublicationInfo)
+                return pkiPublicationInfo;
+
+            if (obj != null)
+                return new PkiPublicationInfo(Asn1Sequence.GetInstance(obj));
+
+            return null;
+        }
+
+        private readonly DerInteger m_action;
+        private readonly Asn1Sequence m_pubInfos;
 
         private PkiPublicationInfo(Asn1Sequence seq)
         {
-            action = DerInteger.GetInstance(seq[0]);
-            pubInfos = Asn1Sequence.GetInstance(seq[1]);
+            m_action = DerInteger.GetInstance(seq[0]);
+            if (seq.Count > 1)
+            {
+                m_pubInfos = Asn1Sequence.GetInstance(seq[1]);
+            }
         }
 
-        public static PkiPublicationInfo GetInstance(object obj)
+        public PkiPublicationInfo(BigInteger action)
+            : this(new DerInteger(action))
         {
-            if (obj is PkiPublicationInfo)
-                return (PkiPublicationInfo)obj;
+        }
 
-            if (obj is Asn1Sequence)
-                return new PkiPublicationInfo((Asn1Sequence)obj);
+        public PkiPublicationInfo(DerInteger action)
+        {
+            m_action = action;
+        }
 
-            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
+        /**
+         * Constructor with a single pubInfo, assumes pleasePublish as the action.
+         *
+         * @param pubInfo the pubInfo to be published (can be null if don't care is required).
+         */
+        public PkiPublicationInfo(SinglePubInfo pubInfo)
+            : this(pubInfo != null ? new SinglePubInfo[1]{ pubInfo } : null)
+        {
         }
 
-        public virtual DerInteger Action
+        /**
+         * Constructor with multiple pubInfo, assumes pleasePublish as the action.
+         *
+         * @param pubInfos the pubInfos to be published (can be null if don't care is required).
+         */
+        public PkiPublicationInfo(SinglePubInfo[] pubInfos)
         {
-            get { return action; }
+            m_action = PleasePublish;
+
+            if (pubInfos != null)
+            {
+                m_pubInfos = new DerSequence(pubInfos);
+            }
         }
 
+        public virtual DerInteger Action => m_action;
+
         public virtual SinglePubInfo[] GetPubInfos()
         {
-            if (pubInfos == null)
+            if (m_pubInfos == null)
                 return null;
 
-            SinglePubInfo[] results = new SinglePubInfo[pubInfos.Count];
-            for (int i = 0; i != results.Length; ++i)
-            {
-                results[i] = SinglePubInfo.GetInstance(pubInfos[i]);
-            }
-            return results;
+            return m_pubInfos.MapElements(SinglePubInfo.GetInstance);
         }
 
         /**
@@ -60,7 +103,10 @@ namespace Org.BouncyCastle.Asn1.Crmf
          */
         public override Asn1Object ToAsn1Object()
         {
-            return new DerSequence(action, pubInfos);
+            if (m_pubInfos == null)
+                return new DerSequence(m_action);
+
+            return new DerSequence(m_action, m_pubInfos);
         }
     }
 }