summary refs log tree commit diff
path: root/crypto/src/cms
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-07-25 18:15:43 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-07-25 18:15:43 +0700
commit27f48072133d8ae91291907fab83130853c840c2 (patch)
tree5db594369bfe8611a3fa01040756ff6161b9c3e4 /crypto/src/cms
parentRestore deleted members as Obsolete (diff)
downloadBouncyCastle.NET-ed25519-27f48072133d8ae91291907fab83130853c840c2.tar.xz
Refactoring around algorithm finders
Diffstat (limited to 'crypto/src/cms')
-rw-r--r--crypto/src/cms/CMSSignedData.cs6
-rw-r--r--crypto/src/cms/CMSSignedDataGenerator.cs4
-rw-r--r--crypto/src/cms/CMSSignedGenerator.cs26
3 files changed, 14 insertions, 22 deletions
diff --git a/crypto/src/cms/CMSSignedData.cs b/crypto/src/cms/CMSSignedData.cs
index bfe4705e7..cd517085c 100644
--- a/crypto/src/cms/CMSSignedData.cs
+++ b/crypto/src/cms/CMSSignedData.cs
@@ -36,8 +36,6 @@ namespace Org.BouncyCastle.Cms
 	public class CmsSignedData
 	{
 		private static readonly CmsSignedHelper Helper = CmsSignedHelper.Instance;
-        internal static readonly DefaultDigestAlgorithmIdentifierFinder DigestAlgIDFinder =
-			new DefaultDigestAlgorithmIdentifierFinder();
 
 		private readonly CmsProcessable	signedContent;
 		private SignedData				signedData;
@@ -280,7 +278,7 @@ namespace Org.BouncyCastle.Cms
          * @return a new signed data object.
          */
         public static CmsSignedData AddDigestAlgorithm(CmsSignedData signedData, AlgorithmIdentifier digestAlgorithm) =>
-            AddDigestAlgorithm(signedData, digestAlgorithm, DigestAlgIDFinder);
+            AddDigestAlgorithm(signedData, digestAlgorithm, DefaultDigestAlgorithmIdentifierFinder.Instance);
 
         /**
          * Return a new CMSSignedData which guarantees to have the passed in digestAlgorithm
@@ -355,7 +353,7 @@ namespace Org.BouncyCastle.Cms
 		 */
         public static CmsSignedData ReplaceSigners(CmsSignedData signedData,
 			SignerInformationStore signerInformationStore) =>
-				ReplaceSigners(signedData, signerInformationStore, DigestAlgIDFinder);
+				ReplaceSigners(signedData, signerInformationStore, DefaultDigestAlgorithmIdentifierFinder.Instance);
 
         /**
          * Replace the SignerInformation store associated with this CMSSignedData object with the new one passed in
diff --git a/crypto/src/cms/CMSSignedDataGenerator.cs b/crypto/src/cms/CMSSignedDataGenerator.cs
index 015c540cd..ec8e28e47 100644
--- a/crypto/src/cms/CMSSignedDataGenerator.cs
+++ b/crypto/src/cms/CMSSignedDataGenerator.cs
@@ -88,7 +88,7 @@ namespace Org.BouncyCastle.Cms
                 this.outer = outer;
                 this.sigCalc = sigCalc;
                 this.signerIdentifier = signerIdentifier;
-                this.digestOID = new DefaultDigestAlgorithmIdentifierFinder().Find(
+                this.digestOID = DefaultDigestAlgorithmIdentifierFinder.Instance.Find(
 					(AlgorithmIdentifier)sigCalc.AlgorithmDetails).Algorithm.Id;
                 this.encOID = ((AlgorithmIdentifier)sigCalc.AlgorithmDetails).Algorithm.Id;
                 this.sAttr = sAttr;
@@ -457,7 +457,7 @@ namespace Org.BouncyCastle.Cms
             //
             foreach (SignerInformation signer in _signers)
             {
-                CmsUtilities.AddDigestAlgs(digestAlgs, signer, CmsSignedData.DigestAlgIDFinder);
+                CmsUtilities.AddDigestAlgs(digestAlgs, signer, DefaultDigestAlgorithmIdentifierFinder.Instance);
                 // TODO Verify the content type and calculated digest match the precalculated SignerInfo
                 signerInfos.Add(signer.ToSignerInfo());
             }
diff --git a/crypto/src/cms/CMSSignedGenerator.cs b/crypto/src/cms/CMSSignedGenerator.cs
index f49b1fb80..5cb56805f 100644
--- a/crypto/src/cms/CMSSignedGenerator.cs
+++ b/crypto/src/cms/CMSSignedGenerator.cs
@@ -28,6 +28,9 @@ namespace Org.BouncyCastle.Cms
     // TODO[api] Create API for this
     public class DefaultSignatureAlgorithmIdentifierFinder
     {
+        public static readonly DefaultSignatureAlgorithmIdentifierFinder Instance =
+            new DefaultSignatureAlgorithmIdentifierFinder();
+
         private static readonly Dictionary<string, DerObjectIdentifier> m_algorithms =
             new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
         private static readonly HashSet<DerObjectIdentifier> m_noParams = new HashSet<DerObjectIdentifier>();
@@ -509,8 +512,7 @@ namespace Org.BouncyCastle.Cms
         // TODO[api] Make virtual
         public AlgorithmIdentifier Find(string sigAlgName)
         {
-            string algorithmName = sigAlgName.ToUpperInvariant();
-            if (!m_algorithms.TryGetValue(algorithmName, out var sigAlgOid))
+            if (!m_algorithms.TryGetValue(sigAlgName, out var sigAlgOid))
                 throw new ArgumentException("Unknown signature type requested: " + sigAlgName, nameof(sigAlgName));
 
             AlgorithmIdentifier sigAlgID;
@@ -518,7 +520,7 @@ namespace Org.BouncyCastle.Cms
             {
                 sigAlgID = new AlgorithmIdentifier(sigAlgOid);
             }
-            else if (m_parameters.TryGetValue(algorithmName, out var parameters))
+            else if (m_parameters.TryGetValue(sigAlgName, out var parameters))
             {
                 sigAlgID = new AlgorithmIdentifier(sigAlgOid, parameters);
             }
@@ -533,6 +535,9 @@ namespace Org.BouncyCastle.Cms
     // TODO[api] Create API for this
     public class DefaultDigestAlgorithmIdentifierFinder
     {
+        public static readonly DefaultDigestAlgorithmIdentifierFinder Instance =
+            new DefaultDigestAlgorithmIdentifierFinder();
+
         private static readonly Dictionary<DerObjectIdentifier, DerObjectIdentifier> m_digestOids =
             new Dictionary<DerObjectIdentifier, DerObjectIdentifier>();
         private static readonly Dictionary<string, DerObjectIdentifier> m_digestNameToOids =
@@ -768,19 +773,8 @@ namespace Org.BouncyCastle.Cms
             m_shake256Oids.Add(BCObjectIdentifiers.falcon_1024);
         }
 
-        private static void AddDigestAlgID(DerObjectIdentifier oid, bool withNullParams)
-        {
-            AlgorithmIdentifier algID;
-            if (withNullParams)
-            {
-                algID = new AlgorithmIdentifier(oid, DerNull.Instance);
-            }
-            else
-            {
-                algID = new AlgorithmIdentifier(oid);
-            }
-            m_digestOidToAlgIDs.Add(oid, algID);
-        }
+        private static void AddDigestAlgID(DerObjectIdentifier oid, bool withNullParams) =>
+            m_digestOidToAlgIDs.Add(oid, new AlgorithmIdentifier(oid, withNullParams ? DerNull.Instance : null));
 
         // TODO[api] Make virtual
         public AlgorithmIdentifier Find(AlgorithmIdentifier sigAlgId)