Skip to content

Commit

Permalink
added initialisation to Asn1KeyWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dghbc committed Jan 19, 2019
1 parent 60b001d commit e00d3ae
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions crypto/src/crypto/operators/Asn1KeyWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)];

Expand All @@ -64,22 +62,22 @@ 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)
{
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);

/// <summary>
/// Wraps delegate and implements the WrapperProvider Interface.
Expand All @@ -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);

Expand All @@ -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
Expand Down

0 comments on commit e00d3ae

Please sign in to comment.