Initial Commit
This commit is contained in:
32
iTechSharp/srcbc/pkcs/AsymmetricKeyEntry.cs
Normal file
32
iTechSharp/srcbc/pkcs/AsymmetricKeyEntry.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Collections;
|
||||
|
||||
using Org.BouncyCastle.Crypto;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class AsymmetricKeyEntry
|
||||
: Pkcs12Entry
|
||||
{
|
||||
private readonly AsymmetricKeyParameter key;
|
||||
|
||||
public AsymmetricKeyEntry(
|
||||
AsymmetricKeyParameter key)
|
||||
: base(new Hashtable())
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public AsymmetricKeyEntry(
|
||||
AsymmetricKeyParameter key,
|
||||
Hashtable attributes)
|
||||
: base(attributes)
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public AsymmetricKeyParameter Key
|
||||
{
|
||||
get { return this.key; }
|
||||
}
|
||||
}
|
||||
}
|
75
iTechSharp/srcbc/pkcs/EncryptedPrivateKeyInfoFactory.cs
Normal file
75
iTechSharp/srcbc/pkcs/EncryptedPrivateKeyInfoFactory.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public sealed class EncryptedPrivateKeyInfoFactory
|
||||
{
|
||||
private EncryptedPrivateKeyInfoFactory()
|
||||
{
|
||||
}
|
||||
|
||||
public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
|
||||
DerObjectIdentifier algorithm,
|
||||
char[] passPhrase,
|
||||
byte[] salt,
|
||||
int iterationCount,
|
||||
AsymmetricKeyParameter key)
|
||||
{
|
||||
return CreateEncryptedPrivateKeyInfo(
|
||||
algorithm.Id, passPhrase, salt, iterationCount,
|
||||
PrivateKeyInfoFactory.CreatePrivateKeyInfo(key));
|
||||
}
|
||||
|
||||
public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
|
||||
string algorithm,
|
||||
char[] passPhrase,
|
||||
byte[] salt,
|
||||
int iterationCount,
|
||||
AsymmetricKeyParameter key)
|
||||
{
|
||||
return CreateEncryptedPrivateKeyInfo(
|
||||
algorithm, passPhrase, salt, iterationCount,
|
||||
PrivateKeyInfoFactory.CreatePrivateKeyInfo(key));
|
||||
}
|
||||
|
||||
public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
|
||||
string algorithm,
|
||||
char[] passPhrase,
|
||||
byte[] salt,
|
||||
int iterationCount,
|
||||
PrivateKeyInfo keyInfo)
|
||||
{
|
||||
if (!PbeUtilities.IsPbeAlgorithm(algorithm))
|
||||
throw new ArgumentException("attempt to use non-Pbe algorithm with Pbe EncryptedPrivateKeyInfo generation");
|
||||
|
||||
IBufferedCipher cipher = PbeUtilities.CreateEngine(algorithm) as IBufferedCipher;
|
||||
|
||||
if (cipher == null)
|
||||
{
|
||||
// TODO Throw exception?
|
||||
}
|
||||
|
||||
Asn1Encodable parameters = PbeUtilities.GenerateAlgorithmParameters(
|
||||
algorithm, salt, iterationCount);
|
||||
|
||||
ICipherParameters keyParameters = PbeUtilities.GenerateCipherParameters(
|
||||
algorithm, passPhrase, parameters);
|
||||
|
||||
cipher.Init(true, keyParameters);
|
||||
|
||||
byte[] keyBytes = keyInfo.GetEncoded();
|
||||
byte[] encoding = cipher.DoFinal(keyBytes);
|
||||
|
||||
DerObjectIdentifier oid = PbeUtilities.GetObjectIdentifier(algorithm);
|
||||
AlgorithmIdentifier algID = new AlgorithmIdentifier(oid, parameters);
|
||||
|
||||
return new EncryptedPrivateKeyInfo(algID, encoding);
|
||||
}
|
||||
}
|
||||
}
|
447
iTechSharp/srcbc/pkcs/Pkcs10CertificationRequest.cs
Normal file
447
iTechSharp/srcbc/pkcs/Pkcs10CertificationRequest.cs
Normal file
@@ -0,0 +1,447 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.CryptoPro;
|
||||
using Org.BouncyCastle.Asn1.Nist;
|
||||
using Org.BouncyCastle.Asn1.Oiw;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.TeleTrust;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Asn1.X9;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
using Org.BouncyCastle.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
/// <remarks>
|
||||
/// A class for verifying and creating Pkcs10 Certification requests.
|
||||
/// </remarks>
|
||||
/// <code>
|
||||
/// CertificationRequest ::= Sequence {
|
||||
/// certificationRequestInfo CertificationRequestInfo,
|
||||
/// signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},
|
||||
/// signature BIT STRING
|
||||
/// }
|
||||
///
|
||||
/// CertificationRequestInfo ::= Sequence {
|
||||
/// version Integer { v1(0) } (v1,...),
|
||||
/// subject Name,
|
||||
/// subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
|
||||
/// attributes [0] Attributes{{ CRIAttributes }}
|
||||
/// }
|
||||
///
|
||||
/// Attributes { ATTRIBUTE:IOSet } ::= Set OF Attr{{ IOSet }}
|
||||
///
|
||||
/// Attr { ATTRIBUTE:IOSet } ::= Sequence {
|
||||
/// type ATTRIBUTE.&id({IOSet}),
|
||||
/// values Set SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{\@type})
|
||||
/// }
|
||||
/// </code>
|
||||
/// see <a href="http://www.rsasecurity.com/rsalabs/node.asp?id=2132"/>
|
||||
public class Pkcs10CertificationRequest
|
||||
: CertificationRequest
|
||||
{
|
||||
private static readonly Hashtable algorithms = new Hashtable();
|
||||
private static readonly Hashtable exParams = new Hashtable();
|
||||
private static readonly Hashtable keyAlgorithms = new Hashtable();
|
||||
private static readonly Hashtable oids = new Hashtable();
|
||||
private static readonly ISet noParams = new HashSet();
|
||||
|
||||
static Pkcs10CertificationRequest()
|
||||
{
|
||||
algorithms.Add("MD2WITHRSAENCRYPTION", new DerObjectIdentifier("1.2.840.113549.1.1.2"));
|
||||
algorithms.Add("MD2WITHRSA", new DerObjectIdentifier("1.2.840.113549.1.1.2"));
|
||||
algorithms.Add("MD5WITHRSAENCRYPTION", new DerObjectIdentifier("1.2.840.113549.1.1.4"));
|
||||
algorithms.Add("MD5WITHRSA", new DerObjectIdentifier("1.2.840.113549.1.1.4"));
|
||||
algorithms.Add("RSAWITHMD5", new DerObjectIdentifier("1.2.840.113549.1.1.4"));
|
||||
algorithms.Add("SHA1WITHRSAENCRYPTION", new DerObjectIdentifier("1.2.840.113549.1.1.5"));
|
||||
algorithms.Add("SHA1WITHRSA", new DerObjectIdentifier("1.2.840.113549.1.1.5"));
|
||||
algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
algorithms.Add("RSAWITHSHA1", new DerObjectIdentifier("1.2.840.113549.1.1.5"));
|
||||
algorithms.Add("RIPEMD160WITHRSAENCRYPTION", new DerObjectIdentifier("1.3.36.3.3.1.2"));
|
||||
algorithms.Add("RIPEMD160WITHRSA", new DerObjectIdentifier("1.3.36.3.3.1.2"));
|
||||
algorithms.Add("SHA1WITHDSA", new DerObjectIdentifier("1.2.840.10040.4.3"));
|
||||
algorithms.Add("DSAWITHSHA1", new DerObjectIdentifier("1.2.840.10040.4.3"));
|
||||
algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
|
||||
algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
|
||||
algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
|
||||
algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
|
||||
algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
|
||||
algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
|
||||
algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
|
||||
algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
|
||||
algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
|
||||
algorithms.Add("GOST3410WITHGOST3411", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
|
||||
algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
|
||||
//
|
||||
// reverse mappings
|
||||
//
|
||||
oids.Add(new DerObjectIdentifier("1.2.840.113549.1.1.5"), "SHA1WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512WITHRSA");
|
||||
oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411WITHGOST3410");
|
||||
oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, "GOST3411WITHECGOST3410");
|
||||
|
||||
oids.Add(new DerObjectIdentifier("1.2.840.113549.1.1.4"), "MD5WITHRSA");
|
||||
oids.Add(new DerObjectIdentifier("1.2.840.113549.1.1.2"), "MD2WITHRSA");
|
||||
oids.Add(new DerObjectIdentifier("1.2.840.10040.4.3"), "SHA1WITHDSA");
|
||||
oids.Add(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1WITHECDSA");
|
||||
oids.Add(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224WITHECDSA");
|
||||
oids.Add(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256WITHECDSA");
|
||||
oids.Add(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384WITHECDSA");
|
||||
oids.Add(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512WITHECDSA");
|
||||
oids.Add(OiwObjectIdentifiers.Sha1WithRsa, "SHA1WITHRSA");
|
||||
oids.Add(OiwObjectIdentifiers.DsaWithSha1, "SHA1WITHDSA");
|
||||
oids.Add(NistObjectIdentifiers.DsaWithSha224, "SHA224WITHDSA");
|
||||
oids.Add(NistObjectIdentifiers.DsaWithSha256, "SHA256WITHDSA");
|
||||
|
||||
//
|
||||
// key types
|
||||
//
|
||||
keyAlgorithms.Add(PkcsObjectIdentifiers.RsaEncryption, "RSA");
|
||||
keyAlgorithms.Add(X9ObjectIdentifiers.IdDsa, "DSA");
|
||||
|
||||
//
|
||||
// According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
|
||||
// The parameters field SHALL be NULL for RSA based signature algorithms.
|
||||
//
|
||||
noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
|
||||
noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
|
||||
noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
|
||||
noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
|
||||
noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
|
||||
noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
|
||||
noParams.Add(NistObjectIdentifiers.DsaWithSha224);
|
||||
noParams.Add(NistObjectIdentifiers.DsaWithSha256);
|
||||
|
||||
//
|
||||
// RFC 4491
|
||||
//
|
||||
noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
|
||||
noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
|
||||
//
|
||||
// explicit params
|
||||
//
|
||||
AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
|
||||
exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20));
|
||||
|
||||
AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
|
||||
exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28));
|
||||
|
||||
AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
|
||||
exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32));
|
||||
|
||||
AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
|
||||
exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48));
|
||||
|
||||
AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
|
||||
exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64));
|
||||
}
|
||||
|
||||
private static RsassaPssParameters CreatePssParams(
|
||||
AlgorithmIdentifier hashAlgId,
|
||||
int saltSize)
|
||||
{
|
||||
return new RsassaPssParameters(
|
||||
hashAlgId,
|
||||
new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hashAlgId),
|
||||
new DerInteger(saltSize),
|
||||
new DerInteger(1));
|
||||
}
|
||||
|
||||
public Pkcs10CertificationRequest(
|
||||
byte[] encoded)
|
||||
: base((Asn1Sequence) Asn1Object.FromByteArray(encoded))
|
||||
{
|
||||
}
|
||||
|
||||
public Pkcs10CertificationRequest(
|
||||
Asn1Sequence seq)
|
||||
: base(seq)
|
||||
{
|
||||
}
|
||||
|
||||
public Pkcs10CertificationRequest(
|
||||
Stream input)
|
||||
: base((Asn1Sequence) Asn1Object.FromStream(input))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
|
||||
/// </summary>
|
||||
///<param name="signatureAlgorithm">Name of Sig Alg.</param>
|
||||
/// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
|
||||
/// <param name="publicKey">Public Key to be included in cert reqest.</param>
|
||||
/// <param name="attributes">ASN1Set of Attributes.</param>
|
||||
/// <param name="signingKey">Matching Private key for nominated (above) public key to be used to sign the request.</param>
|
||||
public Pkcs10CertificationRequest(
|
||||
string signatureAlgorithm,
|
||||
X509Name subject,
|
||||
AsymmetricKeyParameter publicKey,
|
||||
Asn1Set attributes,
|
||||
AsymmetricKeyParameter signingKey)
|
||||
{
|
||||
if (signatureAlgorithm == null)
|
||||
throw new ArgumentNullException("signatureAlgorithm");
|
||||
if (subject == null)
|
||||
throw new ArgumentNullException("subject");
|
||||
if (publicKey == null)
|
||||
throw new ArgumentNullException("publicKey");
|
||||
if (publicKey.IsPrivate)
|
||||
throw new ArgumentException("expected public key", "publicKey");
|
||||
if (!signingKey.IsPrivate)
|
||||
throw new ArgumentException("key for signing must be private", "signingKey");
|
||||
|
||||
// DerObjectIdentifier sigOid = SignerUtilities.GetObjectIdentifier(signatureAlgorithm);
|
||||
string algorithmName = signatureAlgorithm.ToUpper(CultureInfo.InvariantCulture);
|
||||
DerObjectIdentifier sigOid = (DerObjectIdentifier) algorithms[algorithmName];
|
||||
|
||||
if (sigOid == null)
|
||||
throw new ArgumentException("Unknown signature type requested");
|
||||
|
||||
if (noParams.Contains(sigOid))
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid);
|
||||
}
|
||||
else if (exParams.ContainsKey(algorithmName))
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid, (Asn1Encodable) exParams[algorithmName]);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid, null);
|
||||
}
|
||||
|
||||
SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
|
||||
|
||||
this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
|
||||
|
||||
ISigner sig = SignerUtilities.GetSigner(signatureAlgorithm);
|
||||
|
||||
sig.Init(true, signingKey);
|
||||
|
||||
try
|
||||
{
|
||||
// Encode.
|
||||
byte[] b = reqInfo.GetDerEncoded();
|
||||
sig.BlockUpdate(b, 0, b.Length);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ArgumentException("exception encoding TBS cert request", e);
|
||||
}
|
||||
|
||||
// Generate Signature.
|
||||
sigBits = new DerBitString(sig.GenerateSignature());
|
||||
}
|
||||
|
||||
// internal Pkcs10CertificationRequest(
|
||||
// Asn1InputStream seqStream)
|
||||
// {
|
||||
// Asn1Sequence seq = (Asn1Sequence) seqStream.ReadObject();
|
||||
// try
|
||||
// {
|
||||
// this.reqInfo = CertificationRequestInfo.GetInstance(seq[0]);
|
||||
// this.sigAlgId = AlgorithmIdentifier.GetInstance(seq[1]);
|
||||
// this.sigBits = (DerBitString) seq[2];
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// throw new ArgumentException("Create From Asn1Sequence: " + ex.Message);
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Get the public key.
|
||||
/// </summary>
|
||||
/// <returns>The public key.</returns>
|
||||
public AsymmetricKeyParameter GetPublicKey()
|
||||
{
|
||||
return PublicKeyFactory.CreateKey(reqInfo.SubjectPublicKeyInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify Pkcs10 Cert Request is valid.
|
||||
/// </summary>
|
||||
/// <returns>true = valid.</returns>
|
||||
public bool Verify()
|
||||
{
|
||||
return Verify(this.GetPublicKey());
|
||||
}
|
||||
|
||||
public bool Verify(
|
||||
AsymmetricKeyParameter publicKey)
|
||||
{
|
||||
ISigner sig;
|
||||
|
||||
try
|
||||
{
|
||||
sig = SignerUtilities.GetSigner(GetSignatureName(sigAlgId));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// try an alternate
|
||||
string alt = (string) oids[sigAlgId.ObjectID];
|
||||
|
||||
if (alt != null)
|
||||
{
|
||||
sig = SignerUtilities.GetSigner(alt);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
SetSignatureParameters(sig, sigAlgId.Parameters);
|
||||
|
||||
sig.Init(false, publicKey);
|
||||
|
||||
try
|
||||
{
|
||||
byte[] b = reqInfo.GetDerEncoded();
|
||||
sig.BlockUpdate(b, 0, b.Length);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new SignatureException("exception encoding TBS cert request", e);
|
||||
}
|
||||
|
||||
return sig.VerifySignature(sigBits.GetBytes());
|
||||
}
|
||||
|
||||
// /// <summary>
|
||||
// /// Get the Der Encoded Pkcs10 Certification Request.
|
||||
// /// </summary>
|
||||
// /// <returns>A byte array.</returns>
|
||||
// public byte[] GetEncoded()
|
||||
// {
|
||||
// return new CertificationRequest(reqInfo, sigAlgId, sigBits).GetDerEncoded();
|
||||
// }
|
||||
|
||||
// TODO Figure out how to set parameters on an ISigner
|
||||
private void SetSignatureParameters(
|
||||
ISigner signature,
|
||||
Asn1Encodable asn1Params)
|
||||
{
|
||||
if (asn1Params != null && !(asn1Params is Asn1Null))
|
||||
{
|
||||
// AlgorithmParameters sigParams = AlgorithmParameters.GetInstance(signature.getAlgorithm());
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// sigParams.init(asn1Params.ToAsn1Object().GetDerEncoded());
|
||||
// }
|
||||
// catch (IOException e)
|
||||
// {
|
||||
// throw new SignatureException("IOException decoding parameters: " + e.Message);
|
||||
// }
|
||||
|
||||
if (signature.AlgorithmName.EndsWith("MGF1"))
|
||||
{
|
||||
throw Platform.CreateNotImplementedException("signature algorithm with MGF1");
|
||||
|
||||
// try
|
||||
// {
|
||||
// signature.setParameter(sigParams.getParameterSpec(PSSParameterSpec.class));
|
||||
// }
|
||||
// catch (GeneralSecurityException e)
|
||||
// {
|
||||
// throw new SignatureException("Exception extracting parameters: " + e.getMessage());
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GetSignatureName(
|
||||
AlgorithmIdentifier sigAlgId)
|
||||
{
|
||||
Asn1Encodable asn1Params = sigAlgId.Parameters;
|
||||
|
||||
if (asn1Params != null && !(asn1Params is Asn1Null))
|
||||
{
|
||||
if (sigAlgId.ObjectID.Equals(PkcsObjectIdentifiers.IdRsassaPss))
|
||||
{
|
||||
RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(asn1Params);
|
||||
return GetDigestAlgName(rsaParams.HashAlgorithm.ObjectID) + "withRSAandMGF1";
|
||||
}
|
||||
}
|
||||
|
||||
return sigAlgId.ObjectID.Id;
|
||||
}
|
||||
|
||||
private static string GetDigestAlgName(
|
||||
DerObjectIdentifier digestAlgOID)
|
||||
{
|
||||
if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOID))
|
||||
{
|
||||
return "MD5";
|
||||
}
|
||||
else if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOID))
|
||||
{
|
||||
return "SHA1";
|
||||
}
|
||||
else if (NistObjectIdentifiers.IdSha224.Equals(digestAlgOID))
|
||||
{
|
||||
return "SHA224";
|
||||
}
|
||||
else if (NistObjectIdentifiers.IdSha256.Equals(digestAlgOID))
|
||||
{
|
||||
return "SHA256";
|
||||
}
|
||||
else if (NistObjectIdentifiers.IdSha384.Equals(digestAlgOID))
|
||||
{
|
||||
return "SHA384";
|
||||
}
|
||||
else if (NistObjectIdentifiers.IdSha512.Equals(digestAlgOID))
|
||||
{
|
||||
return "SHA512";
|
||||
}
|
||||
else if (TeleTrusTObjectIdentifiers.RipeMD128.Equals(digestAlgOID))
|
||||
{
|
||||
return "RIPEMD128";
|
||||
}
|
||||
else if (TeleTrusTObjectIdentifiers.RipeMD160.Equals(digestAlgOID))
|
||||
{
|
||||
return "RIPEMD160";
|
||||
}
|
||||
else if (TeleTrusTObjectIdentifiers.RipeMD256.Equals(digestAlgOID))
|
||||
{
|
||||
return "RIPEMD256";
|
||||
}
|
||||
else if (CryptoProObjectIdentifiers.GostR3411.Equals(digestAlgOID))
|
||||
{
|
||||
return "GOST3411";
|
||||
}
|
||||
else
|
||||
{
|
||||
return digestAlgOID.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
64
iTechSharp/srcbc/pkcs/Pkcs12Entry.cs
Normal file
64
iTechSharp/srcbc/pkcs/Pkcs12Entry.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public abstract class Pkcs12Entry
|
||||
{
|
||||
private readonly Hashtable attributes;
|
||||
|
||||
protected internal Pkcs12Entry(
|
||||
Hashtable attributes)
|
||||
{
|
||||
this.attributes = attributes;
|
||||
|
||||
foreach (DictionaryEntry entry in attributes)
|
||||
{
|
||||
if (!(entry.Key is string))
|
||||
throw new ArgumentException("Attribute keys must be of type: " + typeof(string).FullName, "attributes");
|
||||
if (!(entry.Value is Asn1Encodable))
|
||||
throw new ArgumentException("Attribute values must be of type: " + typeof(Asn1Encodable).FullName, "attributes");
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Use 'object[index]' syntax instead")]
|
||||
public Asn1Encodable GetBagAttribute(
|
||||
DerObjectIdentifier oid)
|
||||
{
|
||||
return (Asn1Encodable)this.attributes[oid.Id];
|
||||
}
|
||||
|
||||
[Obsolete("Use 'object[index]' syntax instead")]
|
||||
public Asn1Encodable GetBagAttribute(
|
||||
string oid)
|
||||
{
|
||||
return (Asn1Encodable)this.attributes[oid];
|
||||
}
|
||||
|
||||
[Obsolete("Use 'BagAttributeKeys' property")]
|
||||
public IEnumerator GetBagAttributeKeys()
|
||||
{
|
||||
return this.attributes.Keys.GetEnumerator();
|
||||
}
|
||||
|
||||
public Asn1Encodable this[
|
||||
DerObjectIdentifier oid]
|
||||
{
|
||||
get { return (Asn1Encodable) this.attributes[oid.Id]; }
|
||||
}
|
||||
|
||||
public Asn1Encodable this[
|
||||
string oid]
|
||||
{
|
||||
get { return (Asn1Encodable) this.attributes[oid]; }
|
||||
}
|
||||
|
||||
public IEnumerable BagAttributeKeys
|
||||
{
|
||||
get { return new EnumerableProxy(this.attributes.Keys); }
|
||||
}
|
||||
}
|
||||
}
|
1129
iTechSharp/srcbc/pkcs/Pkcs12Store.cs
Normal file
1129
iTechSharp/srcbc/pkcs/Pkcs12Store.cs
Normal file
File diff suppressed because it is too large
Load Diff
211
iTechSharp/srcbc/pkcs/PrivateKeyInfoFactory.cs
Normal file
211
iTechSharp/srcbc/pkcs/PrivateKeyInfoFactory.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
using System;
|
||||
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.CryptoPro;
|
||||
using Org.BouncyCastle.Asn1.Oiw;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.Sec;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Asn1.X9;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public sealed class PrivateKeyInfoFactory
|
||||
{
|
||||
private PrivateKeyInfoFactory()
|
||||
{
|
||||
}
|
||||
|
||||
public static PrivateKeyInfo CreatePrivateKeyInfo(
|
||||
AsymmetricKeyParameter key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
if (!key.IsPrivate)
|
||||
throw new ArgumentException("Public key passed - private key expected", "key");
|
||||
|
||||
if (key is ElGamalPrivateKeyParameters)
|
||||
{
|
||||
ElGamalPrivateKeyParameters _key = (ElGamalPrivateKeyParameters)key;
|
||||
return new PrivateKeyInfo(
|
||||
new AlgorithmIdentifier(
|
||||
OiwObjectIdentifiers.ElGamalAlgorithm,
|
||||
new ElGamalParameter(
|
||||
_key.Parameters.P,
|
||||
_key.Parameters.G).ToAsn1Object()),
|
||||
new DerInteger(_key.X));
|
||||
}
|
||||
|
||||
if (key is DsaPrivateKeyParameters)
|
||||
{
|
||||
DsaPrivateKeyParameters _key = (DsaPrivateKeyParameters)key;
|
||||
return new PrivateKeyInfo(
|
||||
new AlgorithmIdentifier(
|
||||
X9ObjectIdentifiers.IdDsa,
|
||||
new DsaParameter(
|
||||
_key.Parameters.P,
|
||||
_key.Parameters.Q,
|
||||
_key.Parameters.G).ToAsn1Object()),
|
||||
new DerInteger(_key.X));
|
||||
}
|
||||
|
||||
if (key is DHPrivateKeyParameters)
|
||||
{
|
||||
/*
|
||||
Process DH private key.
|
||||
The value for L was set to zero implicitly.
|
||||
This is the same action as found in JCEDHPrivateKey GetEncoded method.
|
||||
*/
|
||||
|
||||
DHPrivateKeyParameters _key = (DHPrivateKeyParameters)key;
|
||||
|
||||
DHParameter withNewL = new DHParameter(
|
||||
_key.Parameters.P, _key.Parameters.G, 0);
|
||||
|
||||
return new PrivateKeyInfo(
|
||||
new AlgorithmIdentifier(
|
||||
PkcsObjectIdentifiers.DhKeyAgreement,
|
||||
withNewL.ToAsn1Object()),
|
||||
new DerInteger(_key.X));
|
||||
}
|
||||
|
||||
if (key is RsaKeyParameters)
|
||||
{
|
||||
AlgorithmIdentifier algID = new AlgorithmIdentifier(
|
||||
PkcsObjectIdentifiers.RsaEncryption, DerNull.Instance);
|
||||
|
||||
RsaPrivateKeyStructure keyStruct;
|
||||
if (key is RsaPrivateCrtKeyParameters)
|
||||
{
|
||||
RsaPrivateCrtKeyParameters _key = (RsaPrivateCrtKeyParameters)key;
|
||||
|
||||
keyStruct = new RsaPrivateKeyStructure(
|
||||
_key.Modulus,
|
||||
_key.PublicExponent,
|
||||
_key.Exponent,
|
||||
_key.P,
|
||||
_key.Q,
|
||||
_key.DP,
|
||||
_key.DQ,
|
||||
_key.QInv);
|
||||
}
|
||||
else
|
||||
{
|
||||
RsaKeyParameters _key = (RsaKeyParameters) key;
|
||||
|
||||
keyStruct = new RsaPrivateKeyStructure(
|
||||
_key.Modulus,
|
||||
BigInteger.Zero,
|
||||
_key.Exponent,
|
||||
BigInteger.Zero,
|
||||
BigInteger.Zero,
|
||||
BigInteger.Zero,
|
||||
BigInteger.Zero,
|
||||
BigInteger.Zero);
|
||||
}
|
||||
|
||||
return new PrivateKeyInfo(algID, keyStruct.ToAsn1Object());
|
||||
}
|
||||
|
||||
if (key is ECPrivateKeyParameters)
|
||||
{
|
||||
ECPrivateKeyParameters _key = (ECPrivateKeyParameters)key;
|
||||
AlgorithmIdentifier algID;
|
||||
|
||||
if (_key.AlgorithmName == "ECGOST3410")
|
||||
{
|
||||
if (_key.PublicKeyParamSet == null)
|
||||
throw Platform.CreateNotImplementedException("Not a CryptoPro parameter set");
|
||||
|
||||
Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
|
||||
_key.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet);
|
||||
|
||||
algID = new AlgorithmIdentifier(
|
||||
CryptoProObjectIdentifiers.GostR3410x2001,
|
||||
gostParams.ToAsn1Object());
|
||||
}
|
||||
else
|
||||
{
|
||||
X9ECParameters ecP = new X9ECParameters(
|
||||
_key.Parameters.Curve,
|
||||
_key.Parameters.G,
|
||||
_key.Parameters.N,
|
||||
_key.Parameters.H,
|
||||
_key.Parameters.GetSeed());
|
||||
|
||||
X962Parameters x962 = new X962Parameters(ecP);
|
||||
|
||||
algID = new AlgorithmIdentifier(
|
||||
X9ObjectIdentifiers.IdECPublicKey,
|
||||
x962.ToAsn1Object());
|
||||
}
|
||||
|
||||
return new PrivateKeyInfo(algID, new ECPrivateKeyStructure(_key.D).ToAsn1Object());
|
||||
}
|
||||
|
||||
if (key is Gost3410PrivateKeyParameters)
|
||||
{
|
||||
Gost3410PrivateKeyParameters _key = (Gost3410PrivateKeyParameters)key;
|
||||
|
||||
if (_key.PublicKeyParamSet == null)
|
||||
throw Platform.CreateNotImplementedException("Not a CryptoPro parameter set");
|
||||
|
||||
byte[] keyEnc = _key.X.ToByteArrayUnsigned();
|
||||
byte[] keyBytes = new byte[keyEnc.Length];
|
||||
|
||||
for (int i = 0; i != keyBytes.Length; i++)
|
||||
{
|
||||
keyBytes[i] = keyEnc[keyEnc.Length - 1 - i]; // must be little endian
|
||||
}
|
||||
|
||||
Gost3410PublicKeyAlgParameters algParams = new Gost3410PublicKeyAlgParameters(
|
||||
_key.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet, null);
|
||||
|
||||
AlgorithmIdentifier algID = new AlgorithmIdentifier(
|
||||
CryptoProObjectIdentifiers.GostR3410x94,
|
||||
algParams.ToAsn1Object());
|
||||
|
||||
return new PrivateKeyInfo(algID, new DerOctetString(keyBytes));
|
||||
}
|
||||
|
||||
throw new ArgumentException("Class provided is not convertible: " + key.GetType().FullName);
|
||||
}
|
||||
|
||||
public static PrivateKeyInfo CreatePrivateKeyInfo(
|
||||
char[] passPhrase,
|
||||
EncryptedPrivateKeyInfo encInfo)
|
||||
{
|
||||
return CreatePrivateKeyInfo(passPhrase, false, encInfo);
|
||||
}
|
||||
|
||||
public static PrivateKeyInfo CreatePrivateKeyInfo(
|
||||
char[] passPhrase,
|
||||
bool wrongPkcs12Zero,
|
||||
EncryptedPrivateKeyInfo encInfo)
|
||||
{
|
||||
AlgorithmIdentifier algID = encInfo.EncryptionAlgorithm;
|
||||
IBufferedCipher cipher = PbeUtilities.CreateEngine(algID.ObjectID) as IBufferedCipher;
|
||||
|
||||
if (cipher == null)
|
||||
{
|
||||
// TODO Throw exception?
|
||||
}
|
||||
|
||||
ICipherParameters keyParameters = PbeUtilities.GenerateCipherParameters(
|
||||
algID.ObjectID, passPhrase, wrongPkcs12Zero, algID.Parameters);
|
||||
|
||||
cipher.Init(false, keyParameters);
|
||||
|
||||
byte[] keyBytes = encInfo.GetEncryptedData();
|
||||
byte[] encoding = cipher.DoFinal(keyBytes);
|
||||
Asn1Object asn1Data = Asn1Object.FromByteArray(encoding);
|
||||
|
||||
return PrivateKeyInfo.GetInstance(asn1Data);
|
||||
}
|
||||
}
|
||||
}
|
32
iTechSharp/srcbc/pkcs/X509CertificateEntry.cs
Normal file
32
iTechSharp/srcbc/pkcs/X509CertificateEntry.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Collections;
|
||||
|
||||
using Org.BouncyCastle.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class X509CertificateEntry
|
||||
: Pkcs12Entry
|
||||
{
|
||||
private readonly X509Certificate cert;
|
||||
|
||||
public X509CertificateEntry(
|
||||
X509Certificate cert)
|
||||
: base(new Hashtable())
|
||||
{
|
||||
this.cert = cert;
|
||||
}
|
||||
|
||||
public X509CertificateEntry(
|
||||
X509Certificate cert,
|
||||
Hashtable attributes)
|
||||
: base(attributes)
|
||||
{
|
||||
this.cert = cert;
|
||||
}
|
||||
|
||||
public X509Certificate Certificate
|
||||
{
|
||||
get { return this.cert; }
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user