From eb89e9957f34982f75f36ef237166ccbc2042768 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 14 Jul 2023 14:32:40 +0700 Subject: Refactor using MapElements --- crypto/src/asn1/crmf/CertReqMessages.cs | 7 +--- crypto/src/asn1/crmf/CertReqMsg.cs | 10 +---- crypto/src/asn1/crmf/Controls.cs | 7 +--- crypto/src/asn1/ess/SigningCertificate.cs | 39 +++++--------------- crypto/src/asn1/ess/SigningCertificateV2.cs | 17 +-------- crypto/src/asn1/isismtt/x509/AdmissionSyntax.cs | 9 +---- crypto/src/asn1/isismtt/x509/ProfessionInfo.cs | 43 ++++++---------------- crypto/src/asn1/x509/AuthorityInformationAccess.cs | 10 +---- crypto/src/asn1/x509/CRLDistPoint.cs | 13 ++----- crypto/src/asn1/x509/CertificatePolicies.cs | 7 +--- crypto/src/asn1/x509/TargetInformation.cs | 21 ++++------- crypto/src/asn1/x509/Targets.cs | 21 ++++------- .../asn1/x509/qualified/SemanticsInformation.cs | 11 ++---- crypto/src/ocsp/BasicOCSPResp.cs | 21 +++-------- crypto/src/ocsp/OCSPReq.cs | 18 +++------ crypto/src/ocsp/RespData.cs | 18 +++------ crypto/src/x509/X509V2AttributeCertificate.cs | 18 +++------ crypto/test/src/asn1/test/CertificateTest.cs | 12 +----- 18 files changed, 73 insertions(+), 229 deletions(-) diff --git a/crypto/src/asn1/crmf/CertReqMessages.cs b/crypto/src/asn1/crmf/CertReqMessages.cs index 422950b9e..d49b90fe3 100644 --- a/crypto/src/asn1/crmf/CertReqMessages.cs +++ b/crypto/src/asn1/crmf/CertReqMessages.cs @@ -32,12 +32,7 @@ namespace Org.BouncyCastle.Asn1.Crmf public virtual CertReqMsg[] ToCertReqMsgArray() { - CertReqMsg[] result = new CertReqMsg[content.Count]; - for (int i = 0; i != result.Length; ++i) - { - result[i] = CertReqMsg.GetInstance(content[i]); - } - return result; + return content.MapElements(CertReqMsg.GetInstance); } /** diff --git a/crypto/src/asn1/crmf/CertReqMsg.cs b/crypto/src/asn1/crmf/CertReqMsg.cs index 03ce32d99..ba9cfd389 100644 --- a/crypto/src/asn1/crmf/CertReqMsg.cs +++ b/crypto/src/asn1/crmf/CertReqMsg.cs @@ -81,15 +81,7 @@ namespace Org.BouncyCastle.Asn1.Crmf public virtual AttributeTypeAndValue[] GetRegInfo() { - if (regInfo == null) - return null; - - AttributeTypeAndValue[] results = new AttributeTypeAndValue[regInfo.Count]; - for (int i = 0; i != results.Length; ++i) - { - results[i] = AttributeTypeAndValue.GetInstance(regInfo[i]); - } - return results; + return regInfo?.MapElements(AttributeTypeAndValue.GetInstance); } /** diff --git a/crypto/src/asn1/crmf/Controls.cs b/crypto/src/asn1/crmf/Controls.cs index 70b48a959..ac568d741 100644 --- a/crypto/src/asn1/crmf/Controls.cs +++ b/crypto/src/asn1/crmf/Controls.cs @@ -33,12 +33,7 @@ namespace Org.BouncyCastle.Asn1.Crmf public virtual AttributeTypeAndValue[] ToAttributeTypeAndValueArray() { - AttributeTypeAndValue[] result = new AttributeTypeAndValue[content.Count]; - for (int i = 0; i != result.Length; ++i) - { - result[i] = AttributeTypeAndValue.GetInstance(content[i]); - } - return result; + return content.MapElements(AttributeTypeAndValue.GetInstance); } /** diff --git a/crypto/src/asn1/ess/SigningCertificate.cs b/crypto/src/asn1/ess/SigningCertificate.cs index 6b8deee8b..ae263428e 100644 --- a/crypto/src/asn1/ess/SigningCertificate.cs +++ b/crypto/src/asn1/ess/SigningCertificate.cs @@ -53,36 +53,17 @@ namespace Org.BouncyCastle.Asn1.Ess certs = new DerSequence(essCertID); } - public EssCertID[] GetCerts() - { - EssCertID[] cs = new EssCertID[certs.Count]; - - for (int i = 0; i != certs.Count; i++) - { - cs[i] = EssCertID.GetInstance(certs[i]); - } - - return cs; - } - - public PolicyInformation[] GetPolicies() - { - if (policies == null) - { - return null; - } - - PolicyInformation[] ps = new PolicyInformation[policies.Count]; - - for (int i = 0; i != policies.Count; i++) - { - ps[i] = PolicyInformation.GetInstance(policies[i]); - } + public EssCertID[] GetCerts() + { + return certs.MapElements(EssCertID.GetInstance); + } - return ps; - } + public PolicyInformation[] GetPolicies() + { + return policies?.MapElements(PolicyInformation.GetInstance); + } - /** + /** * The definition of SigningCertificate is *
 		 * SigningCertificate ::=  SEQUENCE {
@@ -94,7 +75,7 @@ namespace Org.BouncyCastle.Asn1.Ess
 		 *  member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs9(9)
 		 *  smime(16) id-aa(2) 12 }
 		 */
-		public override Asn1Object ToAsn1Object()
+        public override Asn1Object ToAsn1Object()
 		{
 			Asn1EncodableVector v = new Asn1EncodableVector(certs);
             v.AddOptional(policies);
diff --git a/crypto/src/asn1/ess/SigningCertificateV2.cs b/crypto/src/asn1/ess/SigningCertificateV2.cs
index 4694098dd..557bede9c 100644
--- a/crypto/src/asn1/ess/SigningCertificateV2.cs
+++ b/crypto/src/asn1/ess/SigningCertificateV2.cs
@@ -65,25 +65,12 @@ namespace Org.BouncyCastle.Asn1.Ess
 
         public EssCertIDv2[] GetCerts()
         {
-            EssCertIDv2[] certIds = new EssCertIDv2[certs.Count];
-            for (int i = 0; i != certs.Count; i++)
-            {
-                certIds[i] = EssCertIDv2.GetInstance(certs[i]);
-            }
-            return certIds;
+            return certs.MapElements(EssCertIDv2.GetInstance);
         }
 
         public PolicyInformation[] GetPolicies()
         {
-            if (policies == null)
-                return null;
-
-            PolicyInformation[] policyInformations = new PolicyInformation[policies.Count];
-            for (int i = 0; i != policies.Count; i++)
-            {
-                policyInformations[i] = PolicyInformation.GetInstance(policies[i]);
-            }
-            return policyInformations;
+            return policies?.MapElements(PolicyInformation.GetInstance);
         }
 
         /**
diff --git a/crypto/src/asn1/isismtt/x509/AdmissionSyntax.cs b/crypto/src/asn1/isismtt/x509/AdmissionSyntax.cs
index 424d73c69..d7f4779d6 100644
--- a/crypto/src/asn1/isismtt/x509/AdmissionSyntax.cs
+++ b/crypto/src/asn1/isismtt/x509/AdmissionSyntax.cs
@@ -261,14 +261,7 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
         */
         public virtual Admissions[] GetContentsOfAdmissions()
         {
-            Admissions[] result = new Admissions[contentsOfAdmissions.Count];
-
-            for (int i = 0; i < contentsOfAdmissions.Count; ++i)
-            {
-                result[i] = Admissions.GetInstance(contentsOfAdmissions[i]);
-            }
-
-            return result;
+            return contentsOfAdmissions.MapElements(Admissions.GetInstance);
         }
     }
 }
diff --git a/crypto/src/asn1/isismtt/x509/ProfessionInfo.cs b/crypto/src/asn1/isismtt/x509/ProfessionInfo.cs
index 32ad31d9a..87e2aaa4f 100644
--- a/crypto/src/asn1/isismtt/x509/ProfessionInfo.cs
+++ b/crypto/src/asn1/isismtt/x509/ProfessionInfo.cs
@@ -326,45 +326,26 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 			get { return namingAuthority; }
 		}
 
-		/**
+        /**
 		* @return Returns the professionItems.
 		*/
-		public virtual DirectoryString[] GetProfessionItems()
-		{
-			DirectoryString[] result = new DirectoryString[professionItems.Count];
-
-			for (int i = 0; i < professionItems.Count; ++i)
-			{
-				result[i] = DirectoryString.GetInstance(professionItems[i]);
-			}
-
-			return result;
-		}
+        public virtual DirectoryString[] GetProfessionItems()
+        {
+            return professionItems.MapElements(DirectoryString.GetInstance);
+        }
 
-		/**
+        /**
 		* @return Returns the professionOids.
 		*/
-		public virtual DerObjectIdentifier[] GetProfessionOids()
-		{
-			if (professionOids == null)
-			{
-				return new DerObjectIdentifier[0];
-			}
-
-			DerObjectIdentifier[] result = new DerObjectIdentifier[professionOids.Count];
-
-			for (int i = 0; i < professionOids.Count; ++i)
-			{
-				result[i] = DerObjectIdentifier.GetInstance(professionOids[i]);
-			}
-
-			return result;
-		}
+        public virtual DerObjectIdentifier[] GetProfessionOids()
+        {
+            return professionOids?.MapElements(DerObjectIdentifier.GetInstance) ?? new DerObjectIdentifier[0];
+        }
 
-		/**
+        /**
 		* @return Returns the registrationNumber.
 		*/
-		public virtual string RegistrationNumber
+        public virtual string RegistrationNumber
 		{
 			get { return registrationNumber; }
 		}
diff --git a/crypto/src/asn1/x509/AuthorityInformationAccess.cs b/crypto/src/asn1/x509/AuthorityInformationAccess.cs
index c601322c5..488f8b8a9 100644
--- a/crypto/src/asn1/x509/AuthorityInformationAccess.cs
+++ b/crypto/src/asn1/x509/AuthorityInformationAccess.cs
@@ -43,18 +43,12 @@ namespace Org.BouncyCastle.Asn1.X509
 
         private readonly AccessDescription[] descriptions;
 
-        private AuthorityInformationAccess(
-            Asn1Sequence seq)
+        private AuthorityInformationAccess(Asn1Sequence seq)
         {
             if (seq.Count < 1)
                 throw new ArgumentException("sequence may not be empty");
 
-            this.descriptions = new AccessDescription[seq.Count];
-
-            for (int i = 0; i < seq.Count; ++i)
-            {
-                descriptions[i] = AccessDescription.GetInstance(seq[i]);
-            }
+            this.descriptions = seq.MapElements(AccessDescription.GetInstance);
         }
 
         public AuthorityInformationAccess(
diff --git a/crypto/src/asn1/x509/CRLDistPoint.cs b/crypto/src/asn1/x509/CRLDistPoint.cs
index 518bf3f00..889769f29 100644
--- a/crypto/src/asn1/x509/CRLDistPoint.cs
+++ b/crypto/src/asn1/x509/CRLDistPoint.cs
@@ -41,24 +41,17 @@ namespace Org.BouncyCastle.Asn1.X509
 			seq = new DerSequence(points);
         }
 
-		/**
+        /**
          * Return the distribution points making up the sequence.
          *
          * @return DistributionPoint[]
          */
         public DistributionPoint[] GetDistributionPoints()
         {
-            DistributionPoint[] dp = new DistributionPoint[seq.Count];
-
-			for (int i = 0; i != seq.Count; ++i)
-            {
-                dp[i] = DistributionPoint.GetInstance(seq[i]);
-            }
-
-			return dp;
+            return seq.MapElements(DistributionPoint.GetInstance);
         }
 
-		/**
+        /**
          * Produce an object suitable for an Asn1OutputStream.
          * 
          * CrlDistPoint ::= Sequence SIZE {1..MAX} OF DistributionPoint
diff --git a/crypto/src/asn1/x509/CertificatePolicies.cs b/crypto/src/asn1/x509/CertificatePolicies.cs
index 97214bd3f..1deef19d3 100644
--- a/crypto/src/asn1/x509/CertificatePolicies.cs
+++ b/crypto/src/asn1/x509/CertificatePolicies.cs
@@ -49,12 +49,7 @@ namespace Org.BouncyCastle.Asn1.X509
 
         private CertificatePolicies(Asn1Sequence seq)
         {
-            this.policyInformation = new PolicyInformation[seq.Count];
-
-            for (int i = 0; i < seq.Count; ++i)
-            {
-                policyInformation[i] = PolicyInformation.GetInstance(seq[i]);
-            }
+            this.policyInformation = seq.MapElements(PolicyInformation.GetInstance);
         }
 
         public virtual PolicyInformation[] GetPolicyInformation()
diff --git a/crypto/src/asn1/x509/TargetInformation.cs b/crypto/src/asn1/x509/TargetInformation.cs
index 2bf218977..c51e1e891 100644
--- a/crypto/src/asn1/x509/TargetInformation.cs
+++ b/crypto/src/asn1/x509/TargetInformation.cs
@@ -56,32 +56,25 @@ namespace Org.BouncyCastle.Asn1.X509
 			this.targets = targets;
 		}
 
-		/**
+        /**
 		 * Returns the targets in this target information extension.
 		 * 

* The ArrayList is cloned before it is returned.

* * @return Returns the targets. */ - public virtual Targets[] GetTargetsObjects() - { - Targets[] result = new Targets[targets.Count]; - - for (int i = 0; i < targets.Count; ++i) - { - result[i] = Targets.GetInstance(targets[i]); - } + public virtual Targets[] GetTargetsObjects() + { + return targets.MapElements(Targets.GetInstance); + } - return result; - } - - /** + /** * Constructs a target information from a single targets element. * According to RFC 3281 only one targets element must be produced. * * @param targets A Targets instance. */ - public TargetInformation( + public TargetInformation( Targets targets) { this.targets = new DerSequence(targets); diff --git a/crypto/src/asn1/x509/Targets.cs b/crypto/src/asn1/x509/Targets.cs index 0387e1f6b..93af8006d 100644 --- a/crypto/src/asn1/x509/Targets.cs +++ b/crypto/src/asn1/x509/Targets.cs @@ -85,26 +85,19 @@ namespace Org.BouncyCastle.Asn1.X509 this.targets = new DerSequence(targets); } - /** + /** * Returns the targets in an ArrayList. *

* The ArrayList is cloned before it is returned.

* * @return Returns the targets. */ - public virtual Target[] GetTargets() - { - Target[] result = new Target[targets.Count]; - - for (int i = 0; i < targets.Count; ++i) - { - result[i] = Target.GetInstance(targets[i]); - } - - return result; - } + public virtual Target[] GetTargets() + { + return targets.MapElements(Target.GetInstance); + } - /** + /** * Produce an object suitable for an Asn1OutputStream. * * Returns: @@ -115,7 +108,7 @@ namespace Org.BouncyCastle.Asn1.X509 * * @return an Asn1Object */ - public override Asn1Object ToAsn1Object() + public override Asn1Object ToAsn1Object() { return targets; } diff --git a/crypto/src/asn1/x509/qualified/SemanticsInformation.cs b/crypto/src/asn1/x509/qualified/SemanticsInformation.cs index 1106e10b1..36a15edec 100644 --- a/crypto/src/asn1/x509/qualified/SemanticsInformation.cs +++ b/crypto/src/asn1/x509/qualified/SemanticsInformation.cs @@ -61,18 +61,13 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified } } - if (obj != null) + if (obj != null) { - Asn1Sequence generalNameSeq = Asn1Sequence.GetInstance(obj); - nameRegistrationAuthorities = new GeneralName[generalNameSeq.Count]; - for (int i= 0; i < generalNameSeq.Count; i++) - { - nameRegistrationAuthorities[i] = GeneralName.GetInstance(generalNameSeq[i]); - } + this.nameRegistrationAuthorities = Asn1Sequence.GetInstance(obj).MapElements(GeneralName.GetInstance); } } - public SemanticsInformation( + public SemanticsInformation( DerObjectIdentifier semanticsIdentifier, GeneralName[] generalNames) { diff --git a/crypto/src/ocsp/BasicOCSPResp.cs b/crypto/src/ocsp/BasicOCSPResp.cs index 3e22931af..9986136ce 100644 --- a/crypto/src/ocsp/BasicOCSPResp.cs +++ b/crypto/src/ocsp/BasicOCSPResp.cs @@ -65,23 +65,12 @@ namespace Org.BouncyCastle.Ocsp get { return data.ProducedAt.ToDateTime(); } } - public SingleResp[] Responses - { - get - { - Asn1Sequence s = data.Responses; - SingleResp[] rs = new SingleResp[s.Count]; - - for (int i = 0; i != rs.Length; i++) - { - rs[i] = new SingleResp(SingleResponse.GetInstance(s[i])); - } - - return rs; - } - } + public SingleResp[] Responses + { + get { return data.Responses.MapElements(element => new SingleResp(SingleResponse.GetInstance(element))); } + } - public X509Extensions ResponseExtensions + public X509Extensions ResponseExtensions { get { return data.ResponseExtensions; } } diff --git a/crypto/src/ocsp/OCSPReq.cs b/crypto/src/ocsp/OCSPReq.cs index 194b6c9c7..51cb93b58 100644 --- a/crypto/src/ocsp/OCSPReq.cs +++ b/crypto/src/ocsp/OCSPReq.cs @@ -109,20 +109,12 @@ namespace Org.BouncyCastle.Ocsp get { return GeneralName.GetInstance(req.TbsRequest.RequestorName); } } - public Req[] GetRequestList() - { - Asn1Sequence seq = req.TbsRequest.RequestList; - Req[] requests = new Req[seq.Count]; - - for (int i = 0; i != requests.Length; i++) - { - requests[i] = new Req(Request.GetInstance(seq[i])); - } - - return requests; - } + public Req[] GetRequestList() + { + return req.TbsRequest.RequestList.MapElements(element => new Req(Request.GetInstance(element))); + } - public X509Extensions RequestExtensions + public X509Extensions RequestExtensions { get { return X509Extensions.GetInstance(req.TbsRequest.RequestExtensions); } } diff --git a/crypto/src/ocsp/RespData.cs b/crypto/src/ocsp/RespData.cs index 00de1cd59..e132c45b1 100644 --- a/crypto/src/ocsp/RespData.cs +++ b/crypto/src/ocsp/RespData.cs @@ -33,20 +33,12 @@ namespace Org.BouncyCastle.Ocsp get { return data.ProducedAt.ToDateTime(); } } - public SingleResp[] GetResponses() - { - Asn1Sequence s = data.Responses; - SingleResp[] rs = new SingleResp[s.Count]; - - for (int i = 0; i != rs.Length; i++) - { - rs[i] = new SingleResp(SingleResponse.GetInstance(s[i])); - } - - return rs; - } + public SingleResp[] GetResponses() + { + return data.Responses.MapElements(element => new SingleResp(SingleResponse.GetInstance(element))); + } - public X509Extensions ResponseExtensions + public X509Extensions ResponseExtensions { get { return data.ResponseExtensions; } } diff --git a/crypto/src/x509/X509V2AttributeCertificate.cs b/crypto/src/x509/X509V2AttributeCertificate.cs index 980b9f3d5..84bf0db54 100644 --- a/crypto/src/x509/X509V2AttributeCertificate.cs +++ b/crypto/src/x509/X509V2AttributeCertificate.cs @@ -214,20 +214,12 @@ namespace Org.BouncyCastle.X509 return cert.ACInfo.Extensions; } - public virtual X509Attribute[] GetAttributes() - { - Asn1Sequence seq = cert.ACInfo.Attributes; - X509Attribute[] attrs = new X509Attribute[seq.Count]; - - for (int i = 0; i != seq.Count; i++) - { - attrs[i] = new X509Attribute((Asn1Encodable)seq[i]); - } - - return attrs; - } + public virtual X509Attribute[] GetAttributes() + { + return cert.ACInfo.Attributes.MapElements(element => new X509Attribute(element)); + } - public virtual X509Attribute[] GetAttributes( + public virtual X509Attribute[] GetAttributes( string oid) { Asn1Sequence seq = cert.ACInfo.Attributes; diff --git a/crypto/test/src/asn1/test/CertificateTest.cs b/crypto/test/src/asn1/test/CertificateTest.cs index 52c5ed1dc..ad1fc8232 100644 --- a/crypto/test/src/asn1/test/CertificateTest.cs +++ b/crypto/test/src/asn1/test/CertificateTest.cs @@ -399,11 +399,7 @@ namespace Org.BouncyCastle.Asn1.Tests // Attributes Asn1Sequence attribSeq = acInfo.Attributes; - AttributeX509[] att = new AttributeX509[attribSeq.Count]; - for (int i = 0; i < attribSeq.Count; i++) - { - att[i] = AttributeX509.GetInstance(attribSeq[i]); - } + AttributeX509[] att = attribSeq.MapElements(AttributeX509.GetInstance); // IssuerUniqueId // TODO, how to best test? @@ -465,11 +461,7 @@ namespace Org.BouncyCastle.Asn1.Tests // Attributes Asn1Sequence attribSeq = acInfo.Attributes; - AttributeX509[] att = new AttributeX509[attribSeq.Count]; - for (int i = 0; i < attribSeq.Count; i++) - { - att[i] = AttributeX509.GetInstance(attribSeq[i]); - } + AttributeX509[] att = attribSeq.MapElements(AttributeX509.GetInstance); // IssuerUniqueId // TODO, how to best test? -- cgit 1.4.1