summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorMW <megan@cryptoworkshop.com>2019-01-19 13:41:32 +1100
committerMW <megan@cryptoworkshop.com>2019-01-19 13:41:32 +1100
commit8403b2552fbf65a960deb4f86c9ffbc49c79d31d (patch)
treed9c7315aa2eca17235288e73d8a50e9b8d31f771 /crypto
parentfixed nl (diff)
downloadBouncyCastle.NET-ed25519-8403b2552fbf65a960deb4f86c9ffbc49c79d31d.tar.xz
Provider logic for transformation names.
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/operators/Asn1KeyWrapper.cs96
-rw-r--r--crypto/src/crypto/operators/CmsKeyTransRecipientInfoGenerator.cs2
-rw-r--r--crypto/test/src/crmf/test/CrmfTest.cs4
3 files changed, 92 insertions, 10 deletions
diff --git a/crypto/src/crypto/operators/Asn1KeyWrapper.cs b/crypto/src/crypto/operators/Asn1KeyWrapper.cs
index 3fe8de8d5..8f77201ca 100644
--- a/crypto/src/crypto/operators/Asn1KeyWrapper.cs
+++ b/crypto/src/crypto/operators/Asn1KeyWrapper.cs
@@ -1,53 +1,133 @@
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Text;
 using Org.BouncyCastle.X509;
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Pkcs;
 using Org.BouncyCastle.Asn1.X509;
+using Org.BouncyCastle.Crypto.Digests;
 using Org.BouncyCastle.Crypto.Encodings;
 using Org.BouncyCastle.Crypto.Engines;
+using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crypto.Operators
 {
+
+    public class KeyWrapperUtil
+    {
+        //
+        // Provider 
+        //
+        private static readonly IDictionary providerMap = Platform.CreateHashtable();
+
+        static KeyWrapperUtil()
+        {
+            providerMap["RSA/NONE/OAEPPADDING"] = new WrapperCreator(RsaOaepWrapper.Rsa_None_OaepPadding);
+            providerMap["RSA/NONE/OAEPWITHSHA256ANDMGF1PADDING"] = new WrapperCreator(RsaOaepWrapper.Rsa_None_OaepWithSha256andMGF1Padding);          
+        }
+
+        public static IKeyWrapper WrapperForName(string algorithm)
+        {
+            WrapperProvider provider = (WrapperProvider)providerMap[Strings.ToUpperCase(algorithm)];
+
+            if (provider == null)
+            {
+                throw new ArgumentException("could not resolve " + algorithm + " to a KeyWrapper");
+            }
+
+            return (IKeyWrapper)provider.createWrapper();
+        }
+
+        public static IKeyUnwrapper UnWrapperForName(string algorithm)
+        {
+            WrapperProvider provider = (WrapperProvider)providerMap[Strings.ToUpperCase(algorithm)];
+            if (provider == null)
+            {
+                throw new ArgumentException("could not resolve " + algorithm + " to a KeyUnWrapper");
+            }
+
+            return (IKeyUnwrapper)provider.createWrapper();
+        }
+    }
+
+
     public class Asn1KeyWrapper : IKeyWrapper
     {
         private X509Certificate cert;
         private string algorithm;
+        private IKeyWrapper wrapper;
+
+      
 
         public Asn1KeyWrapper(string algorithm, X509Certificate cert)
         {
             this.algorithm = algorithm;
             this.cert = cert;
+            wrapper = KeyWrapperUtil.WrapperForName(algorithm);
         }
 
         public object AlgorithmDetails
         {
-            get
-            {
-                throw new NotImplementedException();
-            }
+            get { return wrapper.AlgorithmDetails; }
         }
 
         public IBlockResult Wrap(byte[] keyData)
         {
-            throw new NotImplementedException();
+            return wrapper.Wrap(keyData);
         }
     }
 
-    internal interface WapperProvider
+    internal delegate object WrapperCreatorDelegate();
+
+    /// <summary>
+    /// Wraps delegate and implements the WrapperProvider Interface.
+    /// </summary>
+    internal class WrapperCreator : WrapperProvider
+    {
+        private readonly WrapperCreatorDelegate creator;
+
+        public WrapperCreator(WrapperCreatorDelegate creator)
+        {
+            this.creator = creator;
+        }
+
+
+        public object createWrapper()
+        {
+            return this.creator.Invoke();
+        }
+    }
+
+
+
+    internal interface WrapperProvider
     {
         object createWrapper();
     }
 
+   
+
     internal class RsaOaepWrapper : IKeyWrapper, IKeyUnwrapper
     {
+
+        internal static object Rsa_None_OaepPadding()
+        {
+            return new RsaOaepWrapper(new Sha1Digest(),PkcsObjectIdentifiers.IdRsaesOaep);
+        }
+
+        internal static object Rsa_None_OaepWithSha256andMGF1Padding()
+        {
+            return new RsaOaepWrapper(new Sha256Digest(), PkcsObjectIdentifiers.IdRsaesOaep);
+        }
+
+
         private readonly AlgorithmIdentifier algId;
         private readonly IAsymmetricBlockCipher engine;
 
-        RsaOaepWrapper(IDigest digest, DerObjectIdentifier digestOid)
+        public RsaOaepWrapper(IDigest digest, DerObjectIdentifier digestOid)
         {
-            AlgorithmIdentifier digestAlgId = new AlgorithmIdentifier(digestOid, DerNull.Instance);
+            AlgorithmIdentifier digestAlgId = new AlgorithmIdentifier(digestOid, DerNull.Instance);       
 
             this.algId = new AlgorithmIdentifier(
                 PkcsObjectIdentifiers.IdRsaesOaep,
diff --git a/crypto/src/crypto/operators/CmsKeyTransRecipientInfoGenerator.cs b/crypto/src/crypto/operators/CmsKeyTransRecipientInfoGenerator.cs
index 048014f22..997231b6e 100644
--- a/crypto/src/crypto/operators/CmsKeyTransRecipientInfoGenerator.cs
+++ b/crypto/src/crypto/operators/CmsKeyTransRecipientInfoGenerator.cs
@@ -13,6 +13,8 @@ namespace Org.BouncyCastle.Operators
         public CmsKeyTransRecipientInfoGenerator(X509Certificate recipCert, IKeyWrapper keyWrapper): base(new Asn1.Cms.IssuerAndSerialNumber(recipCert.IssuerDN, new DerInteger(recipCert.SerialNumber)))
         {
             this.keyWrapper = keyWrapper;
+            this.RecipientCert = recipCert;
+            this.RecipientPublicKey = recipCert.GetPublicKey();
         }
 
         public CmsKeyTransRecipientInfoGenerator(byte[] subjectKeyID, IKeyWrapper keyWrapper) : base(subjectKeyID)
diff --git a/crypto/test/src/crmf/test/CrmfTest.cs b/crypto/test/src/crmf/test/CrmfTest.cs
index 7d459ee79..5e05695f4 100644
--- a/crypto/test/src/crmf/test/CrmfTest.cs
+++ b/crypto/test/src/crmf/test/CrmfTest.cs
@@ -40,7 +40,7 @@ namespace Org.BouncyCastle.Crmf.Tests
             TestBasicMessageWithArchiveControl();
             TestBasicMessageWithArchiveControlJVMGenerated();
         }
-
+        
         [Test]
         public void TestFromJVM()
         {
@@ -103,7 +103,7 @@ namespace Org.BouncyCastle.Crmf.Tests
                 SignatureAlgorithm = "Sha1WithRSAEncryption"
             };
 
-            var cert = tcb.Build(rsaKeyPair.Private);
+            var cert = tcb.Build(rsaKeyPair.Private);         
 
             var publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(rsaKeyPair.Public);
             var privateInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(rsaKeyPair.Private);