summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/asn1/cms/Evidence.cs88
-rw-r--r--crypto/src/asn1/cms/OriginatorIdentifierOrKey.cs67
-rw-r--r--crypto/src/asn1/cms/OriginatorPublicKey.cs77
-rw-r--r--crypto/src/asn1/x509/SubjectPublicKeyInfo.cs80
-rw-r--r--crypto/src/cms/KeyAgreeRecipientInfoGenerator.cs14
-rw-r--r--crypto/src/cms/KeyAgreeRecipientInformation.cs10
-rw-r--r--crypto/src/openssl/PEMReader.cs2
7 files changed, 133 insertions, 205 deletions
diff --git a/crypto/src/asn1/cms/Evidence.cs b/crypto/src/asn1/cms/Evidence.cs
index 54d498cb9..19dab50c2 100644
--- a/crypto/src/asn1/cms/Evidence.cs
+++ b/crypto/src/asn1/cms/Evidence.cs
@@ -1,5 +1,6 @@
 using System;
 
+using Org.BouncyCastle.Asn1.Tsp;
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Cms
@@ -7,67 +8,68 @@ namespace Org.BouncyCastle.Asn1.Cms
 	public class Evidence
 		: Asn1Encodable, IAsn1Choice
 	{
-		private TimeStampTokenEvidence tstEvidence;
-        private Asn1Sequence otherEvidence;
+        public static Evidence GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is Evidence evidence)
+                return evidence;
+            if (obj is Asn1TaggedObject taggedObject)
+                return new Evidence(Asn1Utilities.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific));
+
+            throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
+        }
 
-		public Evidence(TimeStampTokenEvidence tstEvidence)
+        public static Evidence GetInstance(Asn1TaggedObject obj, bool isExplicit)
+        {
+            return Asn1Utilities.GetInstanceFromChoice(obj, isExplicit, GetInstance);
+        }
+
+        private readonly TimeStampTokenEvidence m_tstEvidence;
+        private readonly EvidenceRecord m_ersEvidence;
+        private readonly Asn1Sequence m_otherEvidence;
+
+        public Evidence(TimeStampTokenEvidence tstEvidence)
 		{
-			this.tstEvidence = tstEvidence;
+			m_tstEvidence = tstEvidence;
 		}
 
-		private Evidence(Asn1TaggedObject tagged)
+        public Evidence(EvidenceRecord ersEvidence)
+        {
+            m_ersEvidence = ersEvidence;
+        }
+
+        private Evidence(Asn1TaggedObject tagged)
 		{
             if (tagged.TagNo == 0)
             {
-                this.tstEvidence = TimeStampTokenEvidence.GetInstance(tagged, false);
+                m_tstEvidence = TimeStampTokenEvidence.GetInstance(tagged, false);
+            }
+            else if (tagged.TagNo == 1)
+            {
+                m_ersEvidence = EvidenceRecord.GetInstance(tagged, false);
             }
-            //else if (tagged.TagNo == 1)
-            //{
-            //    this.ersEvidence = EvidenceRecord.GetInstance(tagged, false);
-            //}
             else if (tagged.TagNo == 2)
             {
-                this.otherEvidence = Asn1Sequence.GetInstance(tagged, false);
+                m_otherEvidence = Asn1Sequence.GetInstance(tagged, false);
             }
             else
             {
-                throw new ArgumentException("unknown tag in Evidence", "tagged");
+                throw new ArgumentException("unknown tag in Evidence", nameof(tagged));
             }
         }
 
-		public static Evidence GetInstance(object obj)
-		{
-			if (obj is Evidence evidence)
-				return evidence;
-
-			if (obj is Asn1TaggedObject taggedObject)
-				return new Evidence(Asn1Utilities.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific));
+        public virtual TimeStampTokenEvidence TstEvidence => m_tstEvidence;
 
-			throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
-		}
+        public virtual EvidenceRecord ErsEvidence => m_ersEvidence;
 
-        public static Evidence GetInstance(Asn1TaggedObject obj, bool isExplicit)
+        public override Asn1Object ToAsn1Object()
         {
-            return Asn1Utilities.GetInstanceFromChoice(obj, isExplicit, GetInstance);
+            if (m_tstEvidence != null)
+                return new DerTaggedObject(false, 0, m_tstEvidence);
+            if (m_ersEvidence != null)
+                return new DerTaggedObject(false, 1, m_ersEvidence);
+            return new DerTaggedObject(false, 2, m_otherEvidence);
         }
-
-		public virtual TimeStampTokenEvidence TstEvidence
-		{
-			get { return tstEvidence; }
-		}
-
-        //public EvidenceRecord ErsEvidence
-        //{
-        //    get { return ersEvidence; }
-        //}
-
-		public override Asn1Object ToAsn1Object()
-		{
-			if (tstEvidence != null)
-				return new DerTaggedObject(false, 0, tstEvidence);
-            //if (ersEvidence != null)
-            //    return new DerTaggedObject(false, 1, ersEvidence);
-            return new DerTaggedObject(false, 2, otherEvidence);
-		}
-	}
+    }
 }
diff --git a/crypto/src/asn1/cms/OriginatorIdentifierOrKey.cs b/crypto/src/asn1/cms/OriginatorIdentifierOrKey.cs
index 12b1f518d..69b860f5f 100644
--- a/crypto/src/asn1/cms/OriginatorIdentifierOrKey.cs
+++ b/crypto/src/asn1/cms/OriginatorIdentifierOrKey.cs
@@ -8,8 +8,7 @@ namespace Org.BouncyCastle.Asn1.Cms
     public class OriginatorIdentifierOrKey
         : Asn1Encodable, IAsn1Choice
     {
-        public static OriginatorIdentifierOrKey GetInstance(
-            object o)
+        public static OriginatorIdentifierOrKey GetInstance(object o)
         {
             if (o == null)
                 return null;
@@ -20,14 +19,17 @@ namespace Org.BouncyCastle.Asn1.Cms
             if (o is IssuerAndSerialNumber issuerAndSerialNumber)
                 return new OriginatorIdentifierOrKey(issuerAndSerialNumber);
 
-            if (o is SubjectKeyIdentifier subjectKeyIdentifier)
-                return new OriginatorIdentifierOrKey(subjectKeyIdentifier);
-
-            if (o is OriginatorPublicKey originatorPublicKey)
-                return new OriginatorIdentifierOrKey(originatorPublicKey);
+            if (o is Asn1Sequence sequence)
+                return new OriginatorIdentifierOrKey(IssuerAndSerialNumber.GetInstance(sequence));
 
             if (o is Asn1TaggedObject taggedObject)
-                return new OriginatorIdentifierOrKey(Asn1Utilities.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific));
+            {
+                if (taggedObject.HasContextTag(0))
+                    return new OriginatorIdentifierOrKey(SubjectKeyIdentifier.GetInstance(taggedObject, false));
+
+                if (taggedObject.HasContextTag(1))
+                    return new OriginatorIdentifierOrKey(OriginatorPublicKey.GetInstance(taggedObject, false));
+            }
 
             throw new ArgumentException("Invalid OriginatorIdentifierOrKey: " + Platform.GetTypeName(o));
         }
@@ -37,55 +39,33 @@ namespace Org.BouncyCastle.Asn1.Cms
             return Asn1Utilities.GetInstanceFromChoice(o, explicitly, GetInstance);
         }
 
-        private readonly Asn1Encodable id;
+        private readonly Asn1Encodable m_id;
 
         public OriginatorIdentifierOrKey(IssuerAndSerialNumber id)
         {
-            this.id = id;
+            m_id = id;
         }
 
         public OriginatorIdentifierOrKey(SubjectKeyIdentifier id)
         {
-            this.id = new DerTaggedObject(false, 0, id);
+            m_id = new DerTaggedObject(false, 0, id);
         }
 
         public OriginatorIdentifierOrKey(OriginatorPublicKey id)
         {
-            this.id = new DerTaggedObject(false, 1, id);
+            m_id = new DerTaggedObject(false, 1, id);
         }
 
-		private OriginatorIdentifierOrKey(Asn1TaggedObject id)
-		{
-			// TODO Add validation
-			this.id = id;
-		}
-
-		public Asn1Encodable ID
-		{
-			get { return id; }
-		}
-
-		public IssuerAndSerialNumber IssuerAndSerialNumber
-		{
-			get
-			{
-				if (id is IssuerAndSerialNumber)
-				{
-					return (IssuerAndSerialNumber)id;
-				}
+        public Asn1Encodable ID => m_id;
 
-				return null;
-			}
-		}
+        public IssuerAndSerialNumber IssuerAndSerialNumber => m_id as IssuerAndSerialNumber;
 
 		public SubjectKeyIdentifier SubjectKeyIdentifier
 		{
 			get
 			{
-				if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 0)
-				{
-					return SubjectKeyIdentifier.GetInstance((Asn1TaggedObject)id, false);
-				}
+                if (m_id is Asn1TaggedObject taggedObject && taggedObject.HasContextTag(0))
+                    return SubjectKeyIdentifier.GetInstance(taggedObject, false);
 
 				return null;
 			}
@@ -95,10 +75,8 @@ namespace Org.BouncyCastle.Asn1.Cms
 		{
 			get
 			{
-				if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 1)
-				{
-					return OriginatorPublicKey.GetInstance((Asn1TaggedObject)id, false);
-				}
+                if (m_id is Asn1TaggedObject taggedObject && taggedObject.HasContextTag(1))
+					return OriginatorPublicKey.GetInstance(taggedObject, false);
 
 				return null;
 			}
@@ -116,9 +94,6 @@ namespace Org.BouncyCastle.Asn1.Cms
          * SubjectKeyIdentifier ::= OCTET STRING
          * </pre>
          */
-        public override Asn1Object ToAsn1Object()
-        {
-            return id.ToAsn1Object();
-        }
+        public override Asn1Object ToAsn1Object() => m_id.ToAsn1Object();
     }
 }
diff --git a/crypto/src/asn1/cms/OriginatorPublicKey.cs b/crypto/src/asn1/cms/OriginatorPublicKey.cs
index 83db619ea..8c7c6b50f 100644
--- a/crypto/src/asn1/cms/OriginatorPublicKey.cs
+++ b/crypto/src/asn1/cms/OriginatorPublicKey.cs
@@ -1,73 +1,47 @@
-using System;
-
 using Org.BouncyCastle.Asn1.X509;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Cms
 {
     public class OriginatorPublicKey
         : Asn1Encodable
     {
-        private readonly AlgorithmIdentifier mAlgorithm;
-        private readonly DerBitString        mPublicKey;
-
-        public OriginatorPublicKey(
-            AlgorithmIdentifier algorithm,
-            byte[]              publicKey)
+        public static OriginatorPublicKey GetInstance(object obj)
         {
-            this.mAlgorithm = algorithm;
-            this.mPublicKey = new DerBitString(publicKey);
+            if (obj == null)
+                return null;
+            if (obj is OriginatorPublicKey originatorPublicKey)
+                return originatorPublicKey;
+            return new OriginatorPublicKey(Asn1Sequence.GetInstance(obj));
         }
 
-		private OriginatorPublicKey(Asn1Sequence seq)
+        public static OriginatorPublicKey GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
-            this.mAlgorithm = AlgorithmIdentifier.GetInstance(seq[0]);
-            this.mPublicKey = DerBitString.GetInstance(seq[1]);
+            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
         }
 
-		/**
-         * return an OriginatorPublicKey object from a tagged object.
-         *
-         * @param obj the tagged object holding the object we want.
-         * @param explicitly true if the object is meant to be explicitly
-         *              tagged false otherwise.
-         * @exception ArgumentException if the object held by the
-         *          tagged object cannot be converted.
-         */
-        public static OriginatorPublicKey GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+        private readonly AlgorithmIdentifier m_algorithm;
+        private readonly DerBitString m_publicKey;
+
+        public OriginatorPublicKey(AlgorithmIdentifier algorithm, byte[] publicKey)
+            : this(algorithm, new DerBitString(publicKey))
         {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
         }
 
-		/**
-         * return an OriginatorPublicKey object from the given object.
-         *
-         * @param obj the object we want converted.
-         * @exception ArgumentException if the object cannot be converted.
-         */
-        public static OriginatorPublicKey GetInstance(
-            object obj)
+        public OriginatorPublicKey(AlgorithmIdentifier algorithm, DerBitString publicKey)
         {
-            if (obj == null || obj is OriginatorPublicKey)
-                return (OriginatorPublicKey)obj;
-
-			if (obj is Asn1Sequence)
-                return new OriginatorPublicKey(Asn1Sequence.GetInstance(obj));
+            m_algorithm = algorithm;
+            m_publicKey = publicKey;
+        }
 
-            throw new ArgumentException("Invalid OriginatorPublicKey: " + Platform.GetTypeName(obj));
+        private OriginatorPublicKey(Asn1Sequence seq)
+        {
+            m_algorithm = AlgorithmIdentifier.GetInstance(seq[0]);
+            m_publicKey = DerBitString.GetInstance(seq[1]);
         }
 
-		public AlgorithmIdentifier Algorithm
-		{
-			get { return mAlgorithm; }
-		}
+        public AlgorithmIdentifier Algorithm => m_algorithm;
 
-		public DerBitString PublicKey
-		{
-			get { return mPublicKey; }
-		}
+        public DerBitString PublicKey => m_publicKey;
 
 		/**
          * Produce an object suitable for an Asn1OutputStream.
@@ -78,9 +52,6 @@ namespace Org.BouncyCastle.Asn1.Cms
          * }
          * </pre>
          */
-        public override Asn1Object ToAsn1Object()
-        {
-			return new DerSequence(mAlgorithm, mPublicKey);
-        }
+        public override Asn1Object ToAsn1Object() => new DerSequence(m_algorithm, m_publicKey);
     }
 }
diff --git a/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs b/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
index 234990fc7..5faab82d0 100644
--- a/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
+++ b/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
@@ -11,66 +11,59 @@ namespace Org.BouncyCastle.Asn1.X509
     public class SubjectPublicKeyInfo
         : Asn1Encodable
     {
-        private readonly AlgorithmIdentifier	algID;
-        private readonly DerBitString			keyData;
-
-		public static SubjectPublicKeyInfo GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+        public static SubjectPublicKeyInfo GetInstance(object obj)
         {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
+            if (obj == null)
+                return null;
+            if (obj is SubjectPublicKeyInfo subjectPublicKeyInfo)
+                return subjectPublicKeyInfo;
+            return new SubjectPublicKeyInfo(Asn1Sequence.GetInstance(obj));
         }
 
-		public static SubjectPublicKeyInfo GetInstance(
-            object obj)
+        public static SubjectPublicKeyInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
-            if (obj is SubjectPublicKeyInfo)
-                return (SubjectPublicKeyInfo) obj;
+            return new SubjectPublicKeyInfo(Asn1Sequence.GetInstance(obj, explicitly));
+        }
 
-			if (obj != null)
-				return new SubjectPublicKeyInfo(Asn1Sequence.GetInstance(obj));
+        private readonly AlgorithmIdentifier m_algID;
+        private readonly DerBitString m_keyData;
 
-			return null;
+        public SubjectPublicKeyInfo(AlgorithmIdentifier algID, DerBitString publicKey)
+        {
+            m_algID = algID;
+            m_keyData = publicKey;
         }
 
-		public SubjectPublicKeyInfo(
-            AlgorithmIdentifier	algID,
-            Asn1Encodable		publicKey)
+        public SubjectPublicKeyInfo(AlgorithmIdentifier algID, Asn1Encodable publicKey)
         {
-            this.keyData = new DerBitString(publicKey);
-            this.algID = algID;
+            m_algID = algID;
+            m_keyData = new DerBitString(publicKey);
         }
 
-		public SubjectPublicKeyInfo(
-            AlgorithmIdentifier	algID,
-            byte[]				publicKey)
+        public SubjectPublicKeyInfo(AlgorithmIdentifier algID, byte[] publicKey)
         {
-            this.keyData = new DerBitString(publicKey);
-            this.algID = algID;
+            m_algID = algID;
+            m_keyData = new DerBitString(publicKey);
         }
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         public SubjectPublicKeyInfo(AlgorithmIdentifier algID, ReadOnlySpan<byte> publicKey)
         {
-            this.keyData = new DerBitString(publicKey);
-            this.algID = algID;
+            m_algID = algID;
+            m_keyData = new DerBitString(publicKey);
         }
 #endif
 
-        private SubjectPublicKeyInfo(
-            Asn1Sequence seq)
+        private SubjectPublicKeyInfo(Asn1Sequence seq)
         {
-			if (seq.Count != 2)
+            if (seq.Count != 2)
 				throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
 
-            this.algID = AlgorithmIdentifier.GetInstance(seq[0]);
-			this.keyData = DerBitString.GetInstance(seq[1]);
+            m_algID = AlgorithmIdentifier.GetInstance(seq[0]);
+			m_keyData = DerBitString.GetInstance(seq[1]);
 		}
 
-		public AlgorithmIdentifier AlgorithmID
-        {
-			get { return algID; }
-        }
+        public AlgorithmIdentifier AlgorithmID => m_algID;
 
         /**
          * for when the public key is an encoded object - if the bitstring
@@ -79,18 +72,12 @@ namespace Org.BouncyCastle.Asn1.X509
          * @exception IOException - if the bit string doesn't represent a Der
          * encoded object.
          */
-        public Asn1Object ParsePublicKey()
-        {
-            return Asn1Object.FromByteArray(keyData.GetOctets());
-        }
+        public Asn1Object ParsePublicKey() => Asn1Object.FromByteArray(m_keyData.GetOctets());
 
-		/**
+        /**
          * for when the public key is raw bits...
          */
-        public DerBitString PublicKeyData
-        {
-			get { return keyData; }
-        }
+        public DerBitString PublicKeyData => m_keyData;
 
 		/**
          * Produce an object suitable for an Asn1OutputStream.
@@ -100,9 +87,6 @@ namespace Org.BouncyCastle.Asn1.X509
          *                          publicKey BIT STRING }
          * </pre>
          */
-        public override Asn1Object ToAsn1Object()
-        {
-			return new DerSequence(algID, keyData);
-        }
+        public override Asn1Object ToAsn1Object() => new DerSequence(m_algID, m_keyData);
     }
 }
diff --git a/crypto/src/cms/KeyAgreeRecipientInfoGenerator.cs b/crypto/src/cms/KeyAgreeRecipientInfoGenerator.cs
index 479aa50cb..97fbd115b 100644
--- a/crypto/src/cms/KeyAgreeRecipientInfoGenerator.cs
+++ b/crypto/src/cms/KeyAgreeRecipientInfoGenerator.cs
@@ -146,12 +146,10 @@ namespace Org.BouncyCastle.Cms
 				new DerSequence(recipientEncryptedKeys)));
 		}
 
-		private static OriginatorPublicKey CreateOriginatorPublicKey(AsymmetricKeyParameter publicKey)
-		{
-			SubjectPublicKeyInfo spki = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
-			return new OriginatorPublicKey(
-                new AlgorithmIdentifier(spki.AlgorithmID.Algorithm, DerNull.Instance),
-				spki.PublicKeyData.GetBytes());
-		}
-	}
+		private static OriginatorPublicKey CreateOriginatorPublicKey(AsymmetricKeyParameter publicKey) =>
+			CreateOriginatorPublicKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey));
+
+        private static OriginatorPublicKey CreateOriginatorPublicKey(SubjectPublicKeyInfo originatorKeyInfo) =>
+			new OriginatorPublicKey(originatorKeyInfo.AlgorithmID, originatorKeyInfo.PublicKeyData);
+    }
 }
diff --git a/crypto/src/cms/KeyAgreeRecipientInformation.cs b/crypto/src/cms/KeyAgreeRecipientInformation.cs
index cc1823cc6..32679803d 100644
--- a/crypto/src/cms/KeyAgreeRecipientInformation.cs
+++ b/crypto/src/cms/KeyAgreeRecipientInformation.cs
@@ -104,14 +104,12 @@ namespace Org.BouncyCastle.Cms
             return GetPublicKeyFromOriginatorID(origID);
         }
 
-        private AsymmetricKeyParameter GetPublicKeyFromOriginatorPublicKey(
-            AsymmetricKeyParameter	receiverPrivateKey,
-            OriginatorPublicKey		originatorPublicKey)
+        private AsymmetricKeyParameter GetPublicKeyFromOriginatorPublicKey(AsymmetricKeyParameter receiverPrivateKey,
+            OriginatorPublicKey originatorPublicKey)
         {
             PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(receiverPrivateKey);
-            SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(
-                privInfo.PrivateKeyAlgorithm,
-                originatorPublicKey.PublicKey.GetBytes());
+            SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(privInfo.PrivateKeyAlgorithm,
+                originatorPublicKey.PublicKey);
             return PublicKeyFactory.CreateKey(pubInfo);
         }
 
diff --git a/crypto/src/openssl/PEMReader.cs b/crypto/src/openssl/PEMReader.cs
index cb6950c47..b963ed56a 100644
--- a/crypto/src/openssl/PEMReader.cs
+++ b/crypto/src/openssl/PEMReader.cs
@@ -321,7 +321,7 @@ namespace Org.BouncyCastle.OpenSsl
                         DerBitString pubKey = pKey.GetPublicKey();
                         if (pubKey != null)
                         {
-                            SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(algId, pubKey.GetBytes());
+                            SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(algId, pubKey);
 
                             // TODO Are the keys returned here ECDSA, as Java version forces?
                             pubSpec = PublicKeyFactory.CreateKey(pubInfo);