From e00d3aee5f24da1827897c771e4a83c9d473b679 Mon Sep 17 00:00:00 2001 From: David Hook Date: Sat, 19 Jan 2019 15:55:06 +1100 Subject: [PATCH] added initialisation to Asn1KeyWrapper --- crypto/src/crypto/operators/Asn1KeyWrapper.cs | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/crypto/src/crypto/operators/Asn1KeyWrapper.cs b/crypto/src/crypto/operators/Asn1KeyWrapper.cs index 6e608b94b5..527e3df31e 100644 --- a/crypto/src/crypto/operators/Asn1KeyWrapper.cs +++ b/crypto/src/crypto/operators/Asn1KeyWrapper.cs @@ -17,15 +17,13 @@ namespace Org.BouncyCastle.Crypto.Operators { 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); + wrapper = KeyWrapperUtil.WrapperForName(algorithm, cert.GetPublicKey()); } public object AlgorithmDetails @@ -55,7 +53,7 @@ static KeyWrapperUtil() providerMap["RSA/NONE/OAEPWITHSHA512ANDMGF1PADDING"] = new WrapperCreator(RsaOaepWrapper.Rsa_Sha512_Oaep); } - public static IKeyWrapper WrapperForName(string algorithm) + public static IKeyWrapper WrapperForName(string algorithm, ICipherParameters parameters) { WrapperProvider provider = (WrapperProvider)providerMap[Strings.ToUpperCase(algorithm)]; @@ -64,10 +62,10 @@ public static IKeyWrapper WrapperForName(string algorithm) throw new ArgumentException("could not resolve " + algorithm + " to a KeyWrapper"); } - return (IKeyWrapper)provider.createWrapper(); + return (IKeyWrapper)provider.createWrapper(true, parameters); } - public static IKeyUnwrapper UnwrapperForName(string algorithm) + public static IKeyUnwrapper UnwrapperForName(string algorithm, ICipherParameters parameters) { WrapperProvider provider = (WrapperProvider)providerMap[Strings.ToUpperCase(algorithm)]; if (provider == null) @@ -75,11 +73,11 @@ public static IKeyUnwrapper UnwrapperForName(string algorithm) throw new ArgumentException("could not resolve " + algorithm + " to a KeyUnwrapper"); } - return (IKeyUnwrapper)provider.createWrapper(); + return (IKeyUnwrapper)provider.createWrapper(false, parameters); } } - internal delegate object WrapperCreatorDelegate(); + internal delegate object WrapperCreatorDelegate(bool forWrapping, ICipherParameters parameters); /// /// Wraps delegate and implements the WrapperProvider Interface. @@ -93,48 +91,48 @@ public WrapperCreator(WrapperCreatorDelegate creator) this.creator = creator; } - public object createWrapper() + public object createWrapper(bool forWrapping, ICipherParameters parameters) { - return this.creator.Invoke(); + return this.creator.Invoke(forWrapping, parameters); } } internal interface WrapperProvider { - object createWrapper(); + object createWrapper(bool forWrapping, ICipherParameters parameters); } internal class RsaOaepWrapper : IKeyWrapper, IKeyUnwrapper { - internal static object Rsa_Sha1_Oaep() + internal static object Rsa_Sha1_Oaep(bool forWrapping, ICipherParameters parameters) { - return new RsaOaepWrapper(OiwObjectIdentifiers.IdSha1, new Sha1Digest()); + return new RsaOaepWrapper(forWrapping, parameters, OiwObjectIdentifiers.IdSha1, new Sha1Digest()); } - internal static object Rsa_Sha224_Oaep() + internal static object Rsa_Sha224_Oaep(bool forWrapping, ICipherParameters parameters) { - return new RsaOaepWrapper(NistObjectIdentifiers.IdSha224, new Sha224Digest()); + return new RsaOaepWrapper(forWrapping, parameters, NistObjectIdentifiers.IdSha224, new Sha224Digest()); } - internal static object Rsa_Sha256_Oaep() + internal static object Rsa_Sha256_Oaep(bool forWrapping, ICipherParameters parameters) { - return new RsaOaepWrapper(NistObjectIdentifiers.IdSha256, new Sha256Digest()); + return new RsaOaepWrapper(forWrapping, parameters, NistObjectIdentifiers.IdSha256, new Sha256Digest()); } - internal static object Rsa_Sha384_Oaep() + internal static object Rsa_Sha384_Oaep(bool forWrapping, ICipherParameters parameters) { - return new RsaOaepWrapper(NistObjectIdentifiers.IdSha384, new Sha384Digest()); + return new RsaOaepWrapper(forWrapping, parameters, NistObjectIdentifiers.IdSha384, new Sha384Digest()); } - internal static object Rsa_Sha512_Oaep() + internal static object Rsa_Sha512_Oaep(bool forWrapping, ICipherParameters parameters) { - return new RsaOaepWrapper(NistObjectIdentifiers.IdSha512, new Sha512Digest()); + return new RsaOaepWrapper(forWrapping, parameters, NistObjectIdentifiers.IdSha512, new Sha512Digest()); } private readonly AlgorithmIdentifier algId; private readonly IAsymmetricBlockCipher engine; - public RsaOaepWrapper(DerObjectIdentifier digestOid, IDigest digest) + public RsaOaepWrapper(bool forWrapping, ICipherParameters parameters, DerObjectIdentifier digestOid, IDigest digest) { AlgorithmIdentifier digestAlgId = new AlgorithmIdentifier(digestOid, DerNull.Instance); @@ -145,6 +143,7 @@ public RsaOaepWrapper(DerObjectIdentifier digestOid, IDigest digest) new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, digestAlgId), RsaesOaepParameters.DefaultPSourceAlgorithm)); this.engine = new OaepEncoding(new RsaBlindedEngine()); + this.engine.Init(forWrapping, parameters); } public object AlgorithmDetails