summary refs log tree commit diff
path: root/crypto/src/pkix
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:15:10 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:15:10 +0700
commit435210f10fd927653ce8fbc04ec537ae5d8966b6 (patch)
tree27b6ed1c029db271c3429ac57629d7f0156c5fed /crypto/src/pkix
parentRefactoring around Platform (diff)
downloadBouncyCastle.NET-ed25519-435210f10fd927653ce8fbc04ec537ae5d8966b6.tar.xz
Generics migration complete
Diffstat (limited to 'crypto/src/pkix')
-rw-r--r--crypto/src/pkix/PkixAttrCertPathBuilder.cs6
-rw-r--r--crypto/src/pkix/PkixCertPathBuilder.cs6
-rw-r--r--crypto/src/pkix/PkixParameters.cs97
-rw-r--r--crypto/src/pkix/Rfc3281CertPathUtilities.cs87
4 files changed, 70 insertions, 126 deletions
diff --git a/crypto/src/pkix/PkixAttrCertPathBuilder.cs b/crypto/src/pkix/PkixAttrCertPathBuilder.cs
index b10f64d6b..c583bff00 100644
--- a/crypto/src/pkix/PkixAttrCertPathBuilder.cs
+++ b/crypto/src/pkix/PkixAttrCertPathBuilder.cs
@@ -161,17 +161,17 @@ namespace Org.BouncyCastle.Pkix
 					}
 
 					// try to get the issuer certificate from one of the stores
-					ISet issuers = new HashSet();
+					ISet<X509Certificate> issuers;
 					try
 					{
-						issuers.AddAll(PkixCertPathValidatorUtilities.FindIssuerCerts(tbvCert, pkixParams));
+						issuers = PkixCertPathValidatorUtilities.FindIssuerCerts(tbvCert, pkixParams);
 					}
 					catch (Exception e)
 					{
 						throw new Exception("Cannot find issuer certificate for certificate in certification path.", e);
 					}
 
-					if (issuers.IsEmpty)
+					if (issuers.Count < 1)
 						throw new Exception("No issuer certificate for certificate in certification path found.");
 
 					foreach (X509Certificate issuer in issuers)
diff --git a/crypto/src/pkix/PkixCertPathBuilder.cs b/crypto/src/pkix/PkixCertPathBuilder.cs
index a0abcc888..908444a73 100644
--- a/crypto/src/pkix/PkixCertPathBuilder.cs
+++ b/crypto/src/pkix/PkixCertPathBuilder.cs
@@ -131,17 +131,17 @@ namespace Org.BouncyCastle.Pkix
 					}
 
 					// try to get the issuer certificate from one of the stores
-					HashSet issuers = new HashSet();
+					ISet<X509Certificate> issuers;
 					try
 					{
-						issuers.AddAll(PkixCertPathValidatorUtilities.FindIssuerCerts(tbvCert, pkixParams));
+						issuers = PkixCertPathValidatorUtilities.FindIssuerCerts(tbvCert, pkixParams);
 					}
 					catch (Exception e)
 					{
 						throw new Exception("Cannot find issuer certificate for certificate in certification path.", e);
 					}
 
-					if (issuers.IsEmpty)
+					if (issuers.Count < 1)
 						throw new Exception("No issuer certificate for certificate in certification path found.");
 
 					foreach (X509Certificate issuer in issuers)
diff --git a/crypto/src/pkix/PkixParameters.cs b/crypto/src/pkix/PkixParameters.cs
index cafa1115c..8e4c609ed 100644
--- a/crypto/src/pkix/PkixParameters.cs
+++ b/crypto/src/pkix/PkixParameters.cs
@@ -56,10 +56,10 @@ namespace Org.BouncyCastle.Pkix
 		private ISelector<X509Certificate> m_targetConstraintsCert;
 
 		private bool additionalLocationsEnabled;
-		private ISet trustedACIssuers;
-		private ISet necessaryACAttributes;
-		private ISet prohibitedACAttributes;
-		private ISet attrCertCheckers;
+		private ISet<TrustAnchor> trustedACIssuers;
+		private ISet<string> necessaryACAttributes;
+		private ISet<string> prohibitedACAttributes;
+		private ISet<PkixAttrCertChecker> attrCertCheckers;
 		private int validityModel = PkixValidityModel;
 		private bool useDeltas = false;
 
@@ -90,10 +90,10 @@ namespace Org.BouncyCastle.Pkix
 			this.m_storesAttrCert = new List<IStore<X509V2AttributeCertificate>>();
 			this.m_storesCert = new List<IStore<X509Certificate>>();
 			this.m_storesCrl = new List<IStore<X509Crl>>();
-			this.trustedACIssuers = new HashSet();
-			this.necessaryACAttributes = new HashSet();
-			this.prohibitedACAttributes = new HashSet();
-			this.attrCertCheckers = new HashSet();
+			this.trustedACIssuers = new HashSet<TrustAnchor>();
+			this.necessaryACAttributes = new HashSet<string>();
+			this.prohibitedACAttributes = new HashSet<string>();
+			this.attrCertCheckers = new HashSet<PkixAttrCertChecker>();
 		}
 
 //		// TODO implement for other keystores (see Java build)?
@@ -501,10 +501,10 @@ namespace Org.BouncyCastle.Pkix
 			validityModel = parameters.validityModel;
 			useDeltas = parameters.useDeltas;
 			additionalLocationsEnabled = parameters.additionalLocationsEnabled;
-			trustedACIssuers = new HashSet(parameters.trustedACIssuers);
-			prohibitedACAttributes = new HashSet(parameters.prohibitedACAttributes);
-			necessaryACAttributes = new HashSet(parameters.necessaryACAttributes);
-			attrCertCheckers = new HashSet(parameters.attrCertCheckers);
+			trustedACIssuers = new HashSet<TrustAnchor>(parameters.trustedACIssuers);
+			prohibitedACAttributes = new HashSet<string>(parameters.prohibitedACAttributes);
+			necessaryACAttributes = new HashSet<string>(parameters.necessaryACAttributes);
+			attrCertCheckers = new HashSet<PkixAttrCertChecker>(parameters.attrCertCheckers);
 		}
 
 		/**
@@ -637,9 +637,9 @@ namespace Org.BouncyCastle.Pkix
 		*
 		* @return Returns an immutable set of the trusted AC issuers.
 		*/
-		public virtual ISet GetTrustedACIssuers()
+		public virtual ISet<TrustAnchor> GetTrustedACIssuers()
 		{
-			return new HashSet(trustedACIssuers);
+			return new HashSet<TrustAnchor>(trustedACIssuers);
 		}
 
 		/**
@@ -657,24 +657,15 @@ namespace Org.BouncyCastle.Pkix
 		* @throws ClassCastException if an element of <code>stores</code> is not
 		*             a <code>TrustAnchor</code>.
 		*/
-		public virtual void SetTrustedACIssuers(
-			ISet trustedACIssuers)
+		public virtual void SetTrustedACIssuers(ISet<TrustAnchor> trustedACIssuers)
 		{
 			if (trustedACIssuers == null)
 			{
-				this.trustedACIssuers = new HashSet();
+				this.trustedACIssuers = new HashSet<TrustAnchor>();
 			}
 			else
 			{
-				foreach (object obj in trustedACIssuers)
-				{
-					if (!(obj is TrustAnchor))
-					{
-						throw new InvalidCastException("All elements of set must be "
-							+ "of type " + typeof(TrustAnchor).FullName + ".");
-					}
-				}
-				this.trustedACIssuers = new HashSet(trustedACIssuers);
+				this.trustedACIssuers = new HashSet<TrustAnchor>(trustedACIssuers);
 			}
 		}
 
@@ -688,9 +679,9 @@ namespace Org.BouncyCastle.Pkix
 		*
 		* @return Returns the necessary AC attributes.
 		*/
-		public virtual ISet GetNecessaryACAttributes()
+		public virtual ISet<string> GetNecessaryACAttributes()
 		{
-			return new HashSet(necessaryACAttributes);
+			return new HashSet<string>(necessaryACAttributes);
 		}
 
 		/**
@@ -707,24 +698,15 @@ namespace Org.BouncyCastle.Pkix
 		*             <code>necessaryACAttributes</code> is not a
 		*             <code>String</code>.
 		*/
-		public virtual void SetNecessaryACAttributes(
-			ISet necessaryACAttributes)
+		public virtual void SetNecessaryACAttributes(ISet<string> necessaryACAttributes)
 		{
 			if (necessaryACAttributes == null)
 			{
-				this.necessaryACAttributes = new HashSet();
+				this.necessaryACAttributes = new HashSet<string>();
 			}
 			else
 			{
-				foreach (object obj in necessaryACAttributes)
-				{
-					if (!(obj is string))
-					{
-						throw new InvalidCastException("All elements of set must be "
-							+ "of type string.");
-					}
-				}
-				this.necessaryACAttributes = new HashSet(necessaryACAttributes);
+				this.necessaryACAttributes = new HashSet<string>(necessaryACAttributes);
 			}
 		}
 
@@ -737,9 +719,9 @@ namespace Org.BouncyCastle.Pkix
 		*
 		* @return Returns the prohibited AC attributes. Is never <code>null</code>.
 		*/
-		public virtual ISet GetProhibitedACAttributes()
+		public virtual ISet<string> GetProhibitedACAttributes()
 		{
-			return new HashSet(prohibitedACAttributes);
+			return new HashSet<string>(prohibitedACAttributes);
 		}
 
 		/**
@@ -756,21 +738,15 @@ namespace Org.BouncyCastle.Pkix
 		*             <code>prohibitedACAttributes</code> is not a
 		*             <code>String</code>.
 		*/
-		public virtual void SetProhibitedACAttributes(
-			ISet prohibitedACAttributes)
+		public virtual void SetProhibitedACAttributes(ISet<string> prohibitedACAttributes)
 		{
 			if (prohibitedACAttributes == null)
 			{
-				this.prohibitedACAttributes = new HashSet();
+				this.prohibitedACAttributes = new HashSet<string>();
 			}
 			else
 			{
-				foreach (object obj in prohibitedACAttributes)
-				{
-					if (!(obj is string))
-						throw new InvalidCastException("All elements of set must be of type string.");
-				}
-				this.prohibitedACAttributes = new HashSet(prohibitedACAttributes);
+				this.prohibitedACAttributes = new HashSet<string>(prohibitedACAttributes);
 			}
 		}
 
@@ -781,9 +757,9 @@ namespace Org.BouncyCastle.Pkix
 		* @return Returns the attribute certificate checker. Is never
 		*         <code>null</code>.
 		*/
-		public virtual ISet GetAttrCertCheckers()
+		public virtual ISet<PkixAttrCertChecker> GetAttrCertCheckers()
 		{
-			return new HashSet(attrCertCheckers);
+			return new HashSet<PkixAttrCertChecker>(attrCertCheckers);
 		}
 
 		/**
@@ -800,24 +776,15 @@ namespace Org.BouncyCastle.Pkix
 		* @throws ClassCastException if an element of <code>attrCertCheckers</code>
 		*             is not a <code>PKIXAttrCertChecker</code>.
 		*/
-		public virtual void SetAttrCertCheckers(
-			ISet attrCertCheckers)
+		public virtual void SetAttrCertCheckers(ISet<PkixAttrCertChecker> attrCertCheckers)
 		{
 			if (attrCertCheckers == null)
 			{
-				this.attrCertCheckers = new HashSet();
+				this.attrCertCheckers = new HashSet<PkixAttrCertChecker>();
 			}
 			else
 			{
-				foreach (object obj in attrCertCheckers)
-				{
-					if (!(obj is PkixAttrCertChecker))
-					{
-						throw new InvalidCastException("All elements of set must be "
-							+ "of type " + typeof(PkixAttrCertChecker).FullName + ".");
-					}
-				}
-				this.attrCertCheckers = new HashSet(attrCertCheckers);
+				this.attrCertCheckers = new HashSet<PkixAttrCertChecker>(attrCertCheckers);
 			}
 		}
 	}
diff --git a/crypto/src/pkix/Rfc3281CertPathUtilities.cs b/crypto/src/pkix/Rfc3281CertPathUtilities.cs
index 686498b3e..4d12ad0c0 100644
--- a/crypto/src/pkix/Rfc3281CertPathUtilities.cs
+++ b/crypto/src/pkix/Rfc3281CertPathUtilities.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Collections;
 using System.Collections.Generic;
 
 using Org.BouncyCastle.Asn1.X509;
@@ -82,8 +81,8 @@ namespace Org.BouncyCastle.Pkix
             // check if revocation is available
             if (attrCert.GetExtensionValue(X509Extensions.NoRevAvail) != null)
             {
-                if (attrCert.GetExtensionValue(X509Extensions.CrlDistributionPoints) != null
-                    || attrCert.GetExtensionValue(X509Extensions.AuthorityInfoAccess) != null)
+                if (attrCert.GetExtensionValue(X509Extensions.CrlDistributionPoints) != null ||
+                    attrCert.GetExtensionValue(X509Extensions.AuthorityInfoAccess) != null)
                 {
                     throw new PkixCertPathValidatorException(
                         "No rev avail extension is set, but also an AC revocation pointer.");
@@ -92,22 +91,20 @@ namespace Org.BouncyCastle.Pkix
                 return;
             }
 
-            CrlDistPoint crldp = null;
+            CrlDistPoint crldp;
 			try
 			{
 				crldp = CrlDistPoint.GetInstance(
-					PkixCertPathValidatorUtilities.GetExtensionValue(
-						attrCert, X509Extensions.CrlDistributionPoints));
+					PkixCertPathValidatorUtilities.GetExtensionValue(attrCert, X509Extensions.CrlDistributionPoints));
 			}
 			catch (Exception e)
 			{
-				throw new PkixCertPathValidatorException(
-					"CRL distribution point extension could not be read.", e);
+				throw new PkixCertPathValidatorException("CRL distribution point extension could not be read.", e);
 			}
+
 			try
 			{
-				PkixCertPathValidatorUtilities
-					.AddAdditionalStoresFromCrlDistributionPoint(crldp, paramsPKIX);
+				PkixCertPathValidatorUtilities.AddAdditionalStoresFromCrlDistributionPoint(crldp, paramsPKIX);
 			}
 			catch (Exception e)
 			{
@@ -123,34 +120,30 @@ namespace Org.BouncyCastle.Pkix
 			// for each distribution point
 			if (crldp != null)
 			{
-				DistributionPoint[] dps = null;
+				DistributionPoint[] dps;
 				try
 				{
 					dps = crldp.GetDistributionPoints();
 				}
 				catch (Exception e)
 				{
-					throw new PkixCertPathValidatorException(
-						"Distribution points could not be read.", e);
+					throw new PkixCertPathValidatorException("Distribution points could not be read.", e);
 				}
 				try
 				{
-					for (int i = 0; i < dps.Length
-						&& certStatus.Status == CertStatus.Unrevoked
-						&& !reasonsMask.IsAllReasons; i++)
+					for (int i = 0;
+						i < dps.Length && certStatus.Status == CertStatus.Unrevoked && !reasonsMask.IsAllReasons;
+						i++)
 					{
-						PkixParameters paramsPKIXClone = (PkixParameters) paramsPKIX
-							.Clone();
-						CheckCrl(dps[i], attrCert, paramsPKIXClone,
-							validDate, issuerCert, certStatus, reasonsMask,
+						PkixParameters paramsPKIXClone = (PkixParameters)paramsPKIX.Clone();
+						CheckCrl(dps[i], attrCert, paramsPKIXClone,validDate, issuerCert, certStatus, reasonsMask,
 							certPathCerts);
 						validCrlFound = true;
 					}
 				}
 				catch (Exception e)
 				{
-					lastException = new Exception(
-						"No valid CRL for distribution point found.", e);
+					lastException = new Exception("No valid CRL for distribution point found.", e);
 				}
 			}
 
@@ -160,8 +153,7 @@ namespace Org.BouncyCastle.Pkix
 			* distribution point but issued by the certificate issuer.
 			*/
 
-			if (certStatus.Status == CertStatus.Unrevoked
-				&& !reasonsMask.IsAllReasons)
+			if (certStatus.Status == CertStatus.Unrevoked && !reasonsMask.IsAllReasons)
 			{
 				try
 				{
@@ -177,9 +169,7 @@ namespace Org.BouncyCastle.Pkix
                     }
                     catch (Exception e)
 					{
-						throw new Exception(
-							"Issuer from certificate for CRL could not be reencoded.",
-							e);
+						throw new Exception("Issuer from certificate for CRL could not be reencoded.", e);
 					}
 					DistributionPoint dp = new DistributionPoint(
 						new DistributionPointName(0, new GeneralNames(
@@ -191,24 +181,18 @@ namespace Org.BouncyCastle.Pkix
 				}
 				catch (Exception e)
 				{
-					lastException = new Exception(
-						"No valid CRL for distribution point found.", e);
+					lastException = new Exception("No valid CRL for distribution point found.", e);
 				}
 			}
 
 			if (!validCrlFound)
-			{
-				throw new PkixCertPathValidatorException(
-					"No valid CRL found.", lastException);
-			}
+				throw new PkixCertPathValidatorException("No valid CRL found.", lastException);
+
 			if (certStatus.Status != CertStatus.Unrevoked)
 			{
                 // This format is enforced by the NistCertPath tests
-                string formattedDate = certStatus.RevocationDate.Value.ToString(
-                    "ddd MMM dd HH:mm:ss K yyyy");
-                string message = "Attribute certificate revocation after "
-					+ formattedDate;
-				message += ", reason: "
+                string formattedDate = certStatus.RevocationDate.Value.ToString("ddd MMM dd HH:mm:ss K yyyy");
+                string message = "Attribute certificate revocation after " + formattedDate + ", reason: "
 					+ Rfc3280CertPathUtilities.CrlReasons[certStatus.Status];
 				throw new PkixCertPathValidatorException(message);
 			}
@@ -474,9 +458,7 @@ namespace Org.BouncyCastle.Pkix
 
 			DateTime currentDate = DateTime.UtcNow;
 			if (validDate.CompareTo(currentDate) > 0)
-			{
 				throw new Exception("Validation time is in future.");
-			}
 
 			// (a)
 			/*
@@ -485,11 +467,11 @@ namespace Org.BouncyCastle.Pkix
 			* CRLs must be enabled in the ExtendedPkixParameters and are in
 			* getAdditionalStore()
 			*/
-			ISet<X509Crl> crls = PkixCertPathValidatorUtilities.GetCompleteCrls(dp, attrCert, currentDate, paramsPKIX);
+			var crls = PkixCertPathValidatorUtilities.GetCompleteCrls(dp, attrCert, currentDate, paramsPKIX);
 			bool validCrlFound = false;
 			Exception lastException = null;
 
-			IEnumerator crl_iter = crls.GetEnumerator();
+			var crl_iter = crls.GetEnumerator();
 
 			while (crl_iter.MoveNext()
 				&& certStatus.Status == CertStatus.Unrevoked
@@ -497,7 +479,7 @@ namespace Org.BouncyCastle.Pkix
 			{
 				try
 				{
-					X509Crl crl = (X509Crl) crl_iter.Current;
+					X509Crl crl = crl_iter.Current;
 
 					// (d)
 					ReasonsMask interimReasonsMask = Rfc3280CertPathUtilities.ProcessCrlD(crl, dp);
@@ -509,13 +491,12 @@ namespace Org.BouncyCastle.Pkix
 					* must be ignored.
 					*/
 					if (!interimReasonsMask.HasNewReasons(reasonMask))
-					{
 						continue;
-					}
 
 					// (f)
-					var keys = Rfc3280CertPathUtilities.ProcessCrlF(crl, attrCert,
-						null, null, paramsPKIX, certPathCerts);
+					var keys = Rfc3280CertPathUtilities.ProcessCrlF(crl, attrCert,null, null, paramsPKIX,
+						certPathCerts);
+
 					// (g)
 					AsymmetricKeyParameter pubKey = Rfc3280CertPathUtilities.ProcessCrlG(crl, keys);
 
@@ -524,8 +505,8 @@ namespace Org.BouncyCastle.Pkix
 					if (paramsPKIX.IsUseDeltasEnabled)
 					{
 						// get delta CRLs
-						ISet<X509Crl> deltaCRLs = PkixCertPathValidatorUtilities.GetDeltaCrls(
-							currentDate, paramsPKIX, crl);
+						var deltaCRLs = PkixCertPathValidatorUtilities.GetDeltaCrls(currentDate, paramsPKIX, crl);
+
 						// we only want one valid delta CRL
 						// (h)
 						deltaCRL = Rfc3280CertPathUtilities.ProcessCrlH(deltaCRLs, pubKey);
@@ -551,10 +532,7 @@ namespace Org.BouncyCastle.Pkix
 						* first check is not done
 						*/
 						if (attrCert.NotAfter.CompareTo(crl.ThisUpdate) < 0)
-						{
-							throw new Exception(
-								"No valid CRL for current time found.");
-						}
+							throw new Exception("No valid CRL for current time found.");
 					}
 
 					Rfc3280CertPathUtilities.ProcessCrlB1(dp, attrCert, crl);
@@ -588,10 +566,9 @@ namespace Org.BouncyCastle.Pkix
 					lastException = e;
 				}
 			}
+
 			if (!validCrlFound)
-			{
 				throw lastException;
-			}
 		}
 	}
 }