summary refs log tree commit diff
path: root/crypto/src/asn1/x509
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-06-25 21:18:38 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-06-25 21:18:38 +0700
commit499bafdd45b0f8c14f74023b38b29fc3ebb5611d (patch)
tree87153088006b67c2ef36d743c361d1bf22da3675 /crypto/src/asn1/x509
parentRSA: Use input range check from fips (diff)
downloadBouncyCastle.NET-ed25519-499bafdd45b0f8c14f74023b38b29fc3ebb5611d.tar.xz
Refactoring in Asn1.IsisMtt
Diffstat (limited to 'crypto/src/asn1/x509')
-rw-r--r--crypto/src/asn1/x509/GeneralName.cs63
-rw-r--r--crypto/src/asn1/x509/SubjectPublicKeyInfo.cs5
-rw-r--r--crypto/src/asn1/x509/Target.cs6
-rw-r--r--crypto/src/asn1/x509/X509CertificateStructure.cs128
4 files changed, 96 insertions, 106 deletions
diff --git a/crypto/src/asn1/x509/GeneralName.cs b/crypto/src/asn1/x509/GeneralName.cs
index 08f85bb8e..ee96ec9ca 100644
--- a/crypto/src/asn1/x509/GeneralName.cs
+++ b/crypto/src/asn1/x509/GeneralName.cs
@@ -46,50 +46,71 @@ namespace Org.BouncyCastle.Asn1.X509
 
 		public static GeneralName GetInstance(object obj)
         {
-			if (obj == null)
-				return null;
-			if (obj is GeneralName generalName)
-				return generalName;
-			return GetInstanceSelection(Asn1TaggedObject.GetInstance(obj));
-		}
+            if (obj == null)
+                return null;
+
+            if (obj is Asn1Encodable element)
+            {
+                var result = GetOptional(element);
+                if (result != null)
+                    return result;
+            }
+
+            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), nameof(obj));
+        }
 
 		public static GeneralName GetInstance(Asn1TaggedObject tagObj, bool explicitly) =>
             Asn1Utilities.GetInstanceChoice(tagObj, explicitly, GetInstance);
 
+        public static GeneralName GetOptional(Asn1Encodable element)
+        {
+            if (element == null)
+                throw new ArgumentNullException(nameof(element));
+
+            if (element is GeneralName generalName)
+                return generalName;
+
+            if (element is Asn1TaggedObject taggedObject)
+            {
+                Asn1Encodable baseObject = GetOptionalBaseObject(taggedObject);
+                if (baseObject != null)
+                    return new GeneralName(taggedObject.TagNo, baseObject);
+            }
+
+            return null;
+        }
+
         public static GeneralName GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
             Asn1Utilities.GetTaggedChoice(taggedObject, declaredExplicit, GetInstance);
 
-        private static GeneralName GetInstanceSelection(Asn1TaggedObject taggedObject)
-		{
+        private static Asn1Encodable GetOptionalBaseObject(Asn1TaggedObject taggedObject)
+        {
             if (taggedObject.HasContextTag())
-			{
-				int tag = taggedObject.TagNo;
-
-				switch (tag)
-				{
+            {
+                switch (taggedObject.TagNo)
+                {
 				case EdiPartyName:
 				case OtherName:
 				case X400Address:
-					return new GeneralName(tag, Asn1Sequence.GetInstance(taggedObject, false));
+					return Asn1Sequence.GetInstance(taggedObject, false);
 
 				case DnsName:
 				case Rfc822Name:
 				case UniformResourceIdentifier:
-					return new GeneralName(tag, DerIA5String.GetInstance(taggedObject, false));
+					return DerIA5String.GetInstance(taggedObject, false);
 
 				case DirectoryName:
 					// CHOICE so explicit
-					return new GeneralName(tag, X509Name.GetInstance(taggedObject, true));
+					return X509Name.GetInstance(taggedObject, true);
 
 				case IPAddress:
-					return new GeneralName(tag, Asn1OctetString.GetInstance(taggedObject, false));
+					return Asn1OctetString.GetInstance(taggedObject, false);
 
 				case RegisteredID:
-					return new GeneralName(tag, DerObjectIdentifier.GetInstance(taggedObject, false));
-				}
+					return DerObjectIdentifier.GetInstance(taggedObject, false);
+                }
             }
-
-            throw new ArgumentException("unknown tag: " + Asn1Utilities.GetTagText(taggedObject));
+            return null;
         }
 
         private readonly int m_tag;
diff --git a/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs b/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
index c6f4838db..07a726172 100644
--- a/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
+++ b/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
@@ -72,8 +72,9 @@ namespace Org.BouncyCastle.Asn1.X509
 
         private SubjectPublicKeyInfo(Asn1Sequence seq)
         {
-            if (seq.Count != 2)
-				throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
+            int count = seq.Count;
+            if (count != 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
             m_algorithm = AlgorithmIdentifier.GetInstance(seq[0]);
 			m_publicKey = DerBitString.GetInstance(seq[1]);
diff --git a/crypto/src/asn1/x509/Target.cs b/crypto/src/asn1/x509/Target.cs
index eff740143..da3ccccae 100644
--- a/crypto/src/asn1/x509/Target.cs
+++ b/crypto/src/asn1/x509/Target.cs
@@ -62,12 +62,12 @@ namespace Org.BouncyCastle.Asn1.X509
         private Target(
 			Asn1TaggedObject tagObj)
 		{
-			switch ((Choice) tagObj.TagNo)
+			switch (tagObj.TagNo)
 			{
-				case Choice.Name:	// GeneralName is already a choice so explicit
+				case (int)Choice.Name:	// GeneralName is already a choice so explicit
 					targetName = GeneralName.GetInstance(tagObj, true);
 					break;
-				case Choice.Group:
+				case (int)Choice.Group:
 					targetGroup = GeneralName.GetInstance(tagObj, true);
 					break;
 				default:
diff --git a/crypto/src/asn1/x509/X509CertificateStructure.cs b/crypto/src/asn1/x509/X509CertificateStructure.cs
index 5394b2be3..aed326d44 100644
--- a/crypto/src/asn1/x509/X509CertificateStructure.cs
+++ b/crypto/src/asn1/x509/X509CertificateStructure.cs
@@ -1,7 +1,5 @@
 using System;
 
-using Org.BouncyCastle.Asn1.Pkcs;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
     /**
@@ -17,15 +15,6 @@ namespace Org.BouncyCastle.Asn1.X509
     public class X509CertificateStructure
         : Asn1Encodable
     {
-        private readonly TbsCertificateStructure	tbsCert;
-        private readonly AlgorithmIdentifier		sigAlgID;
-        private readonly DerBitString				sig;
-
-        public static X509CertificateStructure GetInstance(Asn1TaggedObject obj, bool explicitly)
-        {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
-        }
-
         public static X509CertificateStructure GetInstance(object obj)
         {
             if (obj == null)
@@ -35,95 +24,74 @@ namespace Org.BouncyCastle.Asn1.X509
             return new X509CertificateStructure(Asn1Sequence.GetInstance(obj));
         }
 
-        public X509CertificateStructure(
-            TbsCertificateStructure	tbsCert,
-            AlgorithmIdentifier		sigAlgID,
-            DerBitString			sig)
+        public static X509CertificateStructure GetInstance(Asn1TaggedObject obj, bool explicitly) =>
+            new X509CertificateStructure(Asn1Sequence.GetInstance(obj, explicitly));
+
+        public static X509CertificateStructure GetOptional(Asn1Encodable element)
+        {
+            if (element == null)
+                throw new ArgumentNullException(nameof(element));
+
+            if (element is X509CertificateStructure x509CertificateStructure)
+                return x509CertificateStructure;
+
+            Asn1Sequence asn1Sequence = Asn1Sequence.GetOptional(element);
+            if (asn1Sequence != null)
+                return new X509CertificateStructure(asn1Sequence);
+
+            return null;
+        }
+
+        public static X509CertificateStructure GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new X509CertificateStructure(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
+
+        private readonly TbsCertificateStructure m_tbsCert;
+        private readonly AlgorithmIdentifier m_sigAlgID;
+        private readonly DerBitString m_sig;
+
+        public X509CertificateStructure(TbsCertificateStructure tbsCert, AlgorithmIdentifier sigAlgID, DerBitString sig)
         {
-            if (tbsCert == null)
-                throw new ArgumentNullException("tbsCert");
-            if (sigAlgID == null)
-                throw new ArgumentNullException("sigAlgID");
-            if (sig == null)
-                throw new ArgumentNullException("sig");
-
-            this.tbsCert = tbsCert;
-            this.sigAlgID = sigAlgID;
-            this.sig = sig;
+            m_tbsCert = tbsCert ?? throw new ArgumentNullException(nameof(tbsCert));
+            m_sigAlgID = sigAlgID ?? throw new ArgumentNullException(nameof(sigAlgID));
+            m_sig = sig ?? throw new ArgumentNullException(nameof(sig));
         }
 
-        private X509CertificateStructure(
-            Asn1Sequence seq)
+        private X509CertificateStructure(Asn1Sequence seq)
         {
-            if (seq.Count != 3)
-                throw new ArgumentException("sequence wrong size for a certificate", "seq");
+            int count = seq.Count;
+            if (count != 3)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
             //
             // correct x509 certficate
             //
-            tbsCert = TbsCertificateStructure.GetInstance(seq[0]);
-            sigAlgID = AlgorithmIdentifier.GetInstance(seq[1]);
-            sig = DerBitString.GetInstance(seq[2]);
+            m_tbsCert = TbsCertificateStructure.GetInstance(seq[0]);
+            m_sigAlgID = AlgorithmIdentifier.GetInstance(seq[1]);
+            m_sig = DerBitString.GetInstance(seq[2]);
         }
 
-        public TbsCertificateStructure TbsCertificate
-        {
-            get { return tbsCert; }
-        }
+        public TbsCertificateStructure TbsCertificate => m_tbsCert;
 
-        public int Version
-        {
-            get { return tbsCert.Version; }
-        }
+        public int Version => m_tbsCert.Version;
 
-        public DerInteger SerialNumber
-        {
-            get { return tbsCert.SerialNumber; }
-        }
+        public DerInteger SerialNumber => m_tbsCert.SerialNumber;
 
-        public X509Name Issuer
-        {
-            get { return tbsCert.Issuer; }
-        }
+        public X509Name Issuer => m_tbsCert.Issuer;
 
-        public Time StartDate
-        {
-            get { return tbsCert.StartDate; }
-        }
+        public Time StartDate => m_tbsCert.StartDate;
 
-        public Time EndDate
-        {
-            get { return tbsCert.EndDate; }
-        }
+        public Time EndDate => m_tbsCert.EndDate;
 
-        public X509Name Subject
-        {
-            get { return tbsCert.Subject; }
-        }
+        public X509Name Subject => m_tbsCert.Subject;
 
-        public SubjectPublicKeyInfo SubjectPublicKeyInfo
-        {
-            get { return tbsCert.SubjectPublicKeyInfo; }
-        }
+        public SubjectPublicKeyInfo SubjectPublicKeyInfo => m_tbsCert.SubjectPublicKeyInfo;
 
-        public AlgorithmIdentifier SignatureAlgorithm
-        {
-            get { return sigAlgID; }
-        }
+        public AlgorithmIdentifier SignatureAlgorithm => m_sigAlgID;
 
-        public DerBitString Signature
-        {
-            get { return sig; }
-        }
+        public DerBitString Signature => m_sig;
 
-        public byte[] GetSignatureOctets()
-        {
-            return sig.GetOctets();
-        }
+        public byte[] GetSignatureOctets() => m_sig.GetOctets();
 
-        public override Asn1Object ToAsn1Object()
-        {
-            return new DerSequence(tbsCert, sigAlgID, sig);
-        }
+        public override Asn1Object ToAsn1Object() => new DerSequence(m_tbsCert, m_sigAlgID, m_sig);
     }
 }