summary refs log tree commit diff
path: root/crypto/src
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
parentCleanup (diff)
downloadBouncyCastle.NET-ed25519-427206058f1c7e3e53daa31ab510f32c5a64845a.tar.xz
Port of Asn1.Cmp tests from bc-java
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/asn1/cmp/CertifiedKeyPair.cs4
-rw-r--r--crypto/src/asn1/crmf/EncryptedValue.cs94
-rw-r--r--crypto/src/asn1/crmf/PKIPublicationInfo.cs92
3 files changed, 107 insertions, 83 deletions
diff --git a/crypto/src/asn1/cmp/CertifiedKeyPair.cs b/crypto/src/asn1/cmp/CertifiedKeyPair.cs
index a40a2730a..ec60ce965 100644
--- a/crypto/src/asn1/cmp/CertifiedKeyPair.cs
+++ b/crypto/src/asn1/cmp/CertifiedKeyPair.cs
@@ -42,8 +42,8 @@ namespace Org.BouncyCastle.Asn1.Cmp
 				}
 				else
 				{
-					m_privateKey = EncryptedKey.GetInstance(Asn1TaggedObject.GetInstance(seq[1]));
-					m_publicationInfo = PkiPublicationInfo.GetInstance(Asn1TaggedObject.GetInstance(seq[2]));
+                    m_privateKey = EncryptedKey.GetInstance(Asn1TaggedObject.GetInstance(seq[1]).GetObject());
+                    m_publicationInfo = PkiPublicationInfo.GetInstance(Asn1TaggedObject.GetInstance(seq[2]).GetObject());
 				}
 			}
 		}
diff --git a/crypto/src/asn1/crmf/EncryptedValue.cs b/crypto/src/asn1/crmf/EncryptedValue.cs
index ad3a4c3f7..950298504 100644
--- a/crypto/src/asn1/crmf/EncryptedValue.cs
+++ b/crypto/src/asn1/crmf/EncryptedValue.cs
@@ -9,8 +9,8 @@ namespace Org.BouncyCastle.Asn1.Crmf
     {
         public static EncryptedValue GetInstance(object obj)
         {
-            if (obj is EncryptedValue)
-                return (EncryptedValue)obj;
+            if (obj is EncryptedValue encryptedValue)
+                return encryptedValue;
 
             if (obj != null)
                 return new EncryptedValue(Asn1Sequence.GetInstance(obj));
@@ -18,89 +18,67 @@ namespace Org.BouncyCastle.Asn1.Crmf
             return null;
         }
 
-        private readonly AlgorithmIdentifier intendedAlg;
-        private readonly AlgorithmIdentifier symmAlg;
-        private readonly DerBitString encSymmKey;
-        private readonly AlgorithmIdentifier keyAlg;
-        private readonly Asn1OctetString valueHint;
-        private readonly DerBitString encValue;
+        private readonly AlgorithmIdentifier m_intendedAlg;
+        private readonly AlgorithmIdentifier m_symmAlg;
+        private readonly DerBitString m_encSymmKey;
+        private readonly AlgorithmIdentifier m_keyAlg;
+        private readonly Asn1OctetString m_valueHint;
+        private readonly DerBitString m_encValue;
 
         private EncryptedValue(Asn1Sequence seq)
         {
             int index = 0;
-            while (seq[index++] is Asn1TaggedObject tObj)
+            while (seq[index] is Asn1TaggedObject tObj)
             {
                 switch (tObj.TagNo)
                 {
                 case 0:
-                    intendedAlg = AlgorithmIdentifier.GetInstance(tObj, false);
+                    m_intendedAlg = AlgorithmIdentifier.GetInstance(tObj, false);
                     break;
                 case 1:
-                    symmAlg = AlgorithmIdentifier.GetInstance(tObj, false);
+                    m_symmAlg = AlgorithmIdentifier.GetInstance(tObj, false);
                     break;
                 case 2:
-                    encSymmKey = DerBitString.GetInstance(tObj, false);
+                    m_encSymmKey = DerBitString.GetInstance(tObj, false);
                     break;
                 case 3:
-                    keyAlg = AlgorithmIdentifier.GetInstance(tObj, false);
+                    m_keyAlg = AlgorithmIdentifier.GetInstance(tObj, false);
                     break;
                 case 4:
-                    valueHint = Asn1OctetString.GetInstance(tObj, false);
+                    m_valueHint = Asn1OctetString.GetInstance(tObj, false);
                     break;
                 }
+                ++index;
             }
 
-            encValue = DerBitString.GetInstance(seq[index]);
+            m_encValue = DerBitString.GetInstance(seq[index]);
         }
 
-        public EncryptedValue(
-            AlgorithmIdentifier intendedAlg,
-            AlgorithmIdentifier symmAlg,
-            DerBitString encSymmKey,
-            AlgorithmIdentifier keyAlg,
-            Asn1OctetString valueHint,
-            DerBitString encValue)
+        public EncryptedValue(AlgorithmIdentifier intendedAlg, AlgorithmIdentifier symmAlg, DerBitString encSymmKey,
+            AlgorithmIdentifier keyAlg, Asn1OctetString valueHint, DerBitString encValue)
         {
             if (encValue == null)
                 throw new ArgumentNullException(nameof(encValue));
 
-            this.intendedAlg = intendedAlg;
-            this.symmAlg = symmAlg;
-            this.encSymmKey = encSymmKey;
-            this.keyAlg = keyAlg;
-            this.valueHint = valueHint;
-            this.encValue = encValue;
+            m_intendedAlg = intendedAlg;
+            m_symmAlg = symmAlg;
+            m_encSymmKey = encSymmKey;
+            m_keyAlg = keyAlg;
+            m_valueHint = valueHint;
+            m_encValue = encValue;
         }
 
-        public virtual AlgorithmIdentifier IntendedAlg
-        {
-            get { return intendedAlg; }
-        }
+        public virtual AlgorithmIdentifier IntendedAlg => m_intendedAlg;
 
-        public virtual AlgorithmIdentifier SymmAlg
-        {
-            get { return symmAlg; }
-        }
+        public virtual AlgorithmIdentifier SymmAlg => m_symmAlg;
 
-        public virtual DerBitString EncSymmKey
-        {
-            get { return encSymmKey; }
-        }
+        public virtual DerBitString EncSymmKey => m_encSymmKey;
 
-        public virtual AlgorithmIdentifier KeyAlg
-        {
-            get { return keyAlg; }
-        }
+        public virtual AlgorithmIdentifier KeyAlg => m_keyAlg;
 
-        public virtual Asn1OctetString ValueHint
-        {
-            get { return valueHint; }
-        }
+        public virtual Asn1OctetString ValueHint => m_valueHint;
 
-        public virtual DerBitString EncValue
-        {
-            get { return encValue; }
-        }
+        public virtual DerBitString EncValue => m_encValue;
 
         /**
          * <pre>
@@ -127,12 +105,12 @@ namespace Org.BouncyCastle.Asn1.Crmf
         public override Asn1Object ToAsn1Object()
         {
             Asn1EncodableVector v = new Asn1EncodableVector();
-            v.AddOptionalTagged(false, 0, intendedAlg);
-            v.AddOptionalTagged(false, 1, symmAlg);
-            v.AddOptionalTagged(false, 2, encSymmKey);
-            v.AddOptionalTagged(false, 3, keyAlg);
-            v.AddOptionalTagged(false, 4, valueHint);
-            v.Add(encValue);
+            v.AddOptionalTagged(false, 0, m_intendedAlg);
+            v.AddOptionalTagged(false, 1, m_symmAlg);
+            v.AddOptionalTagged(false, 2, m_encSymmKey);
+            v.AddOptionalTagged(false, 3, m_keyAlg);
+            v.AddOptionalTagged(false, 4, m_valueHint);
+            v.Add(m_encValue);
             return new DerSequence(v);
         }
     }
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);
         }
     }
 }