Skip to content

Commit

Permalink
Provider logic for transformation names.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcw committed Jan 19, 2019
1 parent cf29846 commit 8403b25
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
96 changes: 88 additions & 8 deletions crypto/src/crypto/operators/Asn1KeyWrapper.cs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class CmsKeyTransRecipientInfoGenerator: KeyTransRecipientInfoGenerator
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)
Expand Down
4 changes: 2 additions & 2 deletions crypto/test/src/crmf/test/CrmfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void PerformTest()
TestBasicMessageWithArchiveControl();
TestBasicMessageWithArchiveControlJVMGenerated();
}

[Test]
public void TestFromJVM()
{
Expand Down Expand Up @@ -103,7 +103,7 @@ public void TestBasicMessageWithArchiveControl()
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);
Expand Down

0 comments on commit 8403b25

Please sign in to comment.