summary refs log tree commit diff
path: root/crypto/src/tsp
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/tsp
parentRefactoring around Platform (diff)
downloadBouncyCastle.NET-ed25519-435210f10fd927653ce8fbc04ec537ae5d8966b6.tar.xz
Generics migration complete
Diffstat (limited to 'crypto/src/tsp')
-rw-r--r--crypto/src/tsp/TSPAlgorithms.cs15
-rw-r--r--crypto/src/tsp/TSPUtil.cs132
-rw-r--r--crypto/src/tsp/TimeStampRequest.cs11
-rw-r--r--crypto/src/tsp/TimeStampResponseGenerator.cs20
-rw-r--r--crypto/src/tsp/TimeStampToken.cs5
-rw-r--r--crypto/src/tsp/TimeStampTokenGenerator.cs13
6 files changed, 81 insertions, 115 deletions
diff --git a/crypto/src/tsp/TSPAlgorithms.cs b/crypto/src/tsp/TSPAlgorithms.cs
index 928468ed7..8d7e12ee4 100644
--- a/crypto/src/tsp/TSPAlgorithms.cs
+++ b/crypto/src/tsp/TSPAlgorithms.cs
@@ -1,4 +1,4 @@
-using System.Collections;
+using System.Collections.Generic;
 
 using Org.BouncyCastle.Asn1.CryptoPro;
 using Org.BouncyCastle.Asn1.GM;
@@ -35,20 +35,15 @@ namespace Org.BouncyCastle.Tsp
 
         public static readonly string SM3 = GMObjectIdentifiers.sm3.Id;
 
-        public static readonly IList Allowed;
+        public static readonly List<string> Allowed;
 
 		static TspAlgorithms()
 		{
-			string[] algs = new string[]
+			Allowed = new List<string>()
 			{
-				Gost3411, Gost3411_2012_256, Gost3411_2012_512, MD5, RipeMD128, RipeMD160, RipeMD256, Sha1, Sha224, Sha256, Sha384, Sha512, SM3
+				Gost3411, Gost3411_2012_256, Gost3411_2012_512, MD5, RipeMD128, RipeMD160, RipeMD256, Sha1, Sha224,
+				Sha256, Sha384, Sha512, SM3
 			};
-
-			Allowed = Platform.CreateArrayList();
-			foreach (string alg in algs)
-			{
-				Allowed.Add(alg);
-			}
 		}
 	}
 }
diff --git a/crypto/src/tsp/TSPUtil.cs b/crypto/src/tsp/TSPUtil.cs
index 34ff53b60..a9402ac6d 100644
--- a/crypto/src/tsp/TSPUtil.cs
+++ b/crypto/src/tsp/TSPUtil.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Asn1;
@@ -22,50 +22,47 @@ namespace Org.BouncyCastle.Tsp
 {
 	public class TspUtil
 	{
-		private static ISet EmptySet = CollectionUtilities.ReadOnly(new HashSet());
-		private static IList EmptyList = CollectionUtilities.ReadOnly(Platform.CreateArrayList());
-
-		private static readonly IDictionary digestLengths = Platform.CreateHashtable();
-        private static readonly IDictionary digestNames = Platform.CreateHashtable();
+		private static readonly Dictionary<string, int> DigestLengths = new Dictionary<string, int>();
+        private static readonly Dictionary<string, string> DigestNames = new Dictionary<string, string>();
 
 		static TspUtil()
 		{
-            digestLengths.Add(PkcsObjectIdentifiers.MD5.Id, 16);
-            digestLengths.Add(OiwObjectIdentifiers.IdSha1.Id, 20);
-            digestLengths.Add(NistObjectIdentifiers.IdSha224.Id, 28);
-            digestLengths.Add(NistObjectIdentifiers.IdSha256.Id, 32);
-            digestLengths.Add(NistObjectIdentifiers.IdSha384.Id, 48);
-            digestLengths.Add(NistObjectIdentifiers.IdSha512.Id, 64);
-            digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, 16);
-            digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, 20);
-            digestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, 32);
-            digestLengths.Add(CryptoProObjectIdentifiers.GostR3411.Id, 32);
-            digestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, 32);
-            digestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, 64);
-            digestLengths.Add(GMObjectIdentifiers.sm3.Id, 32);
-
-            digestNames.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
-            digestNames.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
-            digestNames.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
-            digestNames.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
-            digestNames.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
-            digestNames.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
-            digestNames.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption.Id, "MD5");
-			digestNames.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id, "SHA1");
-            digestNames.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id, "SHA224");
-            digestNames.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, "SHA256");
-            digestNames.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id, "SHA384");
-            digestNames.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id, "SHA512");
-            digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
-            digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
-            digestNames.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
-            digestNames.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
-            digestNames.Add(OiwObjectIdentifiers.DsaWithSha1.Id, "SHA1");
-            digestNames.Add(OiwObjectIdentifiers.Sha1WithRsa.Id, "SHA1");
-            digestNames.Add(OiwObjectIdentifiers.MD5WithRsa.Id, "MD5");
-            digestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, "GOST3411-2012-256");
-            digestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, "GOST3411-2012-512");
-            digestNames.Add(GMObjectIdentifiers.sm3.Id, "SM3");
+			DigestLengths.Add(PkcsObjectIdentifiers.MD5.Id, 16);
+            DigestLengths.Add(OiwObjectIdentifiers.IdSha1.Id, 20);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha224.Id, 28);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha256.Id, 32);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha384.Id, 48);
+            DigestLengths.Add(NistObjectIdentifiers.IdSha512.Id, 64);
+            DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, 16);
+            DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, 20);
+            DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, 32);
+            DigestLengths.Add(CryptoProObjectIdentifiers.GostR3411.Id, 32);
+            DigestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, 32);
+            DigestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, 64);
+            DigestLengths.Add(GMObjectIdentifiers.sm3.Id, 32);
+
+            DigestNames.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
+            DigestNames.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
+            DigestNames.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
+            DigestNames.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
+            DigestNames.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
+            DigestNames.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
+            DigestNames.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption.Id, "MD5");
+			DigestNames.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id, "SHA1");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id, "SHA224");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, "SHA256");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id, "SHA384");
+            DigestNames.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id, "SHA512");
+            DigestNames.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
+            DigestNames.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
+            DigestNames.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
+            DigestNames.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
+            DigestNames.Add(OiwObjectIdentifiers.DsaWithSha1.Id, "SHA1");
+            DigestNames.Add(OiwObjectIdentifiers.Sha1WithRsa.Id, "SHA1");
+            DigestNames.Add(OiwObjectIdentifiers.MD5WithRsa.Id, "MD5");
+            DigestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, "GOST3411-2012-256");
+            DigestNames.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, "GOST3411-2012-512");
+            DigestNames.Add(GMObjectIdentifiers.sm3.Id, "SM3");
         }
 
 
@@ -78,10 +75,10 @@ namespace Org.BouncyCastle.Tsp
 	     * @return a collection of TimeStampToken objects
 	     * @throws TSPValidationException
 	     */
-		public static ICollection GetSignatureTimestamps(
+		public static IList<TimeStampToken> GetSignatureTimestamps(
 			SignerInformation signerInfo)
 		{
-			IList timestamps = Platform.CreateArrayList();
+			var timestamps = new List<TimeStampToken>();
 
 			Asn1.Cms.AttributeTable unsignedAttrs = signerInfo.UnsignedAttributes;
 			if (unsignedAttrs != null)
@@ -161,54 +158,45 @@ namespace Org.BouncyCastle.Tsp
 		/// Return the digest algorithm using one of the standard JCA string
 		/// representations rather than the algorithm identifier (if possible).
 		/// </summary>
-		internal static string GetDigestAlgName(
-			string digestAlgOID)
+		internal static string GetDigestAlgName(string digestAlgOid)
 		{
-			string digestName = (string) digestNames[digestAlgOID];
-
-			return digestName != null ? digestName : digestAlgOID;
+			return CollectionUtilities.GetValueOrKey(DigestNames, digestAlgOid);
 		}
 
-		internal static int GetDigestLength(
-			string digestAlgOID)
+		internal static int GetDigestLength(string digestAlgOid)
 		{
-			if (!digestLengths.Contains(digestAlgOID))
+			if (!DigestLengths.TryGetValue(digestAlgOid, out int length))
 				throw new TspException("digest algorithm cannot be found.");
 
-			return (int)digestLengths[digestAlgOID];
+			return length;
 		}
 
-		internal static IDigest CreateDigestInstance(
-			string digestAlgOID)
+		internal static IDigest CreateDigestInstance(string digestAlgOID)
 		{
 	        string digestName = GetDigestAlgName(digestAlgOID);
 
 			return DigestUtilities.GetDigest(digestName);
 		}
 
-		internal static ISet GetCriticalExtensionOids(X509Extensions extensions)
+		internal static ISet<DerObjectIdentifier> GetCriticalExtensionOids(X509Extensions extensions)
 		{
-			if (extensions == null)
-				return EmptySet;
-
-			return CollectionUtilities.ReadOnly(new HashSet(extensions.GetCriticalExtensionOids()));
+			return extensions == null
+				? new HashSet<DerObjectIdentifier>()
+				: new HashSet<DerObjectIdentifier>(extensions.GetCriticalExtensionOids());
 		}
 
-		internal static ISet GetNonCriticalExtensionOids(X509Extensions extensions)
+		internal static ISet<DerObjectIdentifier> GetNonCriticalExtensionOids(X509Extensions extensions)
 		{
-			if (extensions == null)
-				return EmptySet;
-
-			// TODO: should probably produce a set that imposes correct ordering
-			return CollectionUtilities.ReadOnly(new HashSet(extensions.GetNonCriticalExtensionOids()));
+			return extensions == null
+				? new HashSet<DerObjectIdentifier>()
+				: new HashSet<DerObjectIdentifier>(extensions.GetNonCriticalExtensionOids());
 		}
-		
-		internal static IList GetExtensionOids(X509Extensions extensions)
-		{
-			if (extensions == null)
-				return EmptyList;
 
-			return CollectionUtilities.ReadOnly(Platform.CreateArrayList(extensions.GetExtensionOids()));
+		internal static IList<DerObjectIdentifier> GetExtensionOids(X509Extensions extensions)
+		{
+			return extensions == null
+				? new List<DerObjectIdentifier>()
+				: new List<DerObjectIdentifier>(extensions.GetExtensionOids());
 		}
 	}
 }
diff --git a/crypto/src/tsp/TimeStampRequest.cs b/crypto/src/tsp/TimeStampRequest.cs
index f5c6a09e6..b05b58c0e 100644
--- a/crypto/src/tsp/TimeStampRequest.cs
+++ b/crypto/src/tsp/TimeStampRequest.cs
@@ -1,14 +1,12 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Cmp;
 using Org.BouncyCastle.Asn1.Tsp;
 using Org.BouncyCastle.Asn1.X509;
-using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Math;
-using Org.BouncyCastle.Security;
 using Org.BouncyCastle.X509;
 
 namespace Org.BouncyCastle.Tsp
@@ -124,10 +122,7 @@ namespace Org.BouncyCastle.Tsp
 		* @param extensions if non-null a set of extensions we are willing to accept.
 		* @throws TspException if the request is invalid, or processing fails.
 		*/
-		public void Validate(
-			IList algorithms,
-			IList policies,
-			IList extensions)
+		public void Validate(IList<string> algorithms, IList<string> policies, IList<string> extensions)
 		{
 			if (!algorithms.Contains(this.MessageImprintAlgOid))
 				throw new TspValidationException("request contains unknown algorithm", PkiFailureInfo.BadAlg);
@@ -173,7 +168,7 @@ namespace Org.BouncyCastle.Tsp
 			return extensions == null ? null : extensions.GetExtension(oid);
 		}
 
-		public virtual IList GetExtensionOids()
+		public virtual IList<DerObjectIdentifier> GetExtensionOids()
 		{
 			return TspUtil.GetExtensionOids(extensions);
 		}
diff --git a/crypto/src/tsp/TimeStampResponseGenerator.cs b/crypto/src/tsp/TimeStampResponseGenerator.cs
index a88320027..9a9c78678 100644
--- a/crypto/src/tsp/TimeStampResponseGenerator.cs
+++ b/crypto/src/tsp/TimeStampResponseGenerator.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Asn1;
@@ -23,30 +23,30 @@ namespace Org.BouncyCastle.Tsp
 
         private int failInfo;
         private TimeStampTokenGenerator tokenGenerator;
-        private IList acceptedAlgorithms;
-        private IList acceptedPolicies;
-        private IList acceptedExtensions;
+        private IList<string> acceptedAlgorithms;
+        private IList<string> acceptedPolicies;
+        private IList<string> acceptedExtensions;
 
         public TimeStampResponseGenerator(
             TimeStampTokenGenerator tokenGenerator,
-            IList acceptedAlgorithms)
+            IList<string> acceptedAlgorithms)
             : this(tokenGenerator, acceptedAlgorithms, null, null)
         {
         }
 
         public TimeStampResponseGenerator(
             TimeStampTokenGenerator tokenGenerator,
-            IList acceptedAlgorithms,
-            IList acceptedPolicy)
+            IList<string> acceptedAlgorithms,
+            IList<string> acceptedPolicy)
             : this(tokenGenerator, acceptedAlgorithms, acceptedPolicy, null)
         {
         }
 
         public TimeStampResponseGenerator(
             TimeStampTokenGenerator tokenGenerator,
-            IList acceptedAlgorithms,
-            IList acceptedPolicies,
-            IList acceptedExtensions)
+            IList<string> acceptedAlgorithms,
+            IList<string> acceptedPolicies,
+            IList<string> acceptedExtensions)
         {
             this.tokenGenerator = tokenGenerator;
             this.acceptedAlgorithms = acceptedAlgorithms;
diff --git a/crypto/src/tsp/TimeStampToken.cs b/crypto/src/tsp/TimeStampToken.cs
index 2b7187cad..93b372720 100644
--- a/crypto/src/tsp/TimeStampToken.cs
+++ b/crypto/src/tsp/TimeStampToken.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Collections;
 using System.IO;
 
 using Org.BouncyCastle.Asn1;
@@ -52,10 +51,10 @@ namespace Org.BouncyCastle.Tsp
 			}
 
 
-			IEnumerator signerEnum = signers.GetEnumerator();
+			var signerEnum = signers.GetEnumerator();
 
 			signerEnum.MoveNext();
-			tsaSignerInfo = (SignerInformation) signerEnum.Current;
+			tsaSignerInfo = signerEnum.Current;
 
 			try
 			{
diff --git a/crypto/src/tsp/TimeStampTokenGenerator.cs b/crypto/src/tsp/TimeStampTokenGenerator.cs
index 4289d7d24..0d6d102d3 100644
--- a/crypto/src/tsp/TimeStampTokenGenerator.cs
+++ b/crypto/src/tsp/TimeStampTokenGenerator.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
@@ -39,7 +38,7 @@ namespace Org.BouncyCastle.Tsp
         private IStore<X509Crl> x509Crls;
         private IStore<X509V2AttributeCertificate> x509AttrCerts;
         // TODO Port changes from bc-java
-        //private IDictionary otherRevoc = Platform.CreateHashtable();
+        //private Dictionary<> otherRevoc = new Dictionary<>();
         private SignerInfoGenerator signerInfoGenerator;
         IDigestFactory digestCalculator;
 
@@ -63,22 +62,18 @@ namespace Org.BouncyCastle.Tsp
         {
         }
 
-
         public TimeStampTokenGenerator(
             SignerInfoGenerator signerInfoGen,
             IDigestFactory digestCalculator,
             DerObjectIdentifier tsaPolicy,
             bool isIssuerSerialIncluded)
         {
-
             this.signerInfoGenerator = signerInfoGen;
             this.digestCalculator = digestCalculator;
             this.tsaPolicyOID = tsaPolicy;
 
             if (signerInfoGenerator.certificate == null)
-            {
                 throw new ArgumentException("SignerInfoGenerator must have an associated certificate");
-            }
 
             X509Certificate assocCert = signerInfoGenerator.certificate;
             TspUtil.ValidateCertificate(assocCert);
@@ -123,7 +118,6 @@ namespace Org.BouncyCastle.Tsp
                         .WithSignedAttributeGenerator(new TableGen2(signerInfoGen, essCertID))
                         .Build(signerInfoGen.contentSigner, signerInfoGen.certificate);
                 }
-
             }
             catch (Exception ex)
             {
@@ -147,17 +141,13 @@ namespace Org.BouncyCastle.Tsp
         {
         }
 
-
         internal static SignerInfoGenerator makeInfoGenerator(
           AsymmetricKeyParameter key,
           X509Certificate cert,
           string digestOID,
-
           Asn1.Cms.AttributeTable signedAttr,
           Asn1.Cms.AttributeTable unsignedAttr)
         {
-
-
             TspUtil.ValidateCertificate(cert);
 
             //
@@ -194,7 +184,6 @@ namespace Org.BouncyCastle.Tsp
             //    throw new TspException("Can't find a SHA-1 implementation.", e);
             //}
 
-
             string digestName = CmsSignedHelper.Instance.GetDigestAlgName(digestOID);
             string signatureName = digestName + "with" + CmsSignedHelper.Instance.GetEncryptionAlgName(CmsSignedHelper.Instance.GetEncOid(key, digestOID));