仿真平台内核初版 -tlib库 包含<sparc arm riscv powerPC>
This commit is contained in:
60
lib/bc-csharp/crypto/src/pkcs/AsymmetricKeyEntry.cs
Normal file
60
lib/bc-csharp/crypto/src/pkcs/AsymmetricKeyEntry.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class AsymmetricKeyEntry
|
||||
: Pkcs12Entry
|
||||
{
|
||||
private readonly AsymmetricKeyParameter key;
|
||||
|
||||
public AsymmetricKeyEntry(
|
||||
AsymmetricKeyParameter key)
|
||||
: base(Platform.CreateHashtable())
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || PORTABLE)
|
||||
[Obsolete]
|
||||
public AsymmetricKeyEntry(
|
||||
AsymmetricKeyParameter key,
|
||||
Hashtable attributes)
|
||||
: base(attributes)
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
#endif
|
||||
|
||||
public AsymmetricKeyEntry(
|
||||
AsymmetricKeyParameter key,
|
||||
IDictionary attributes)
|
||||
: base(attributes)
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public AsymmetricKeyParameter Key
|
||||
{
|
||||
get { return this.key; }
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
AsymmetricKeyEntry other = obj as AsymmetricKeyEntry;
|
||||
|
||||
if (other == null)
|
||||
return false;
|
||||
|
||||
return key.Equals(other.key);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return ~key.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
102
lib/bc-csharp/crypto/src/pkcs/EncryptedPrivateKeyInfoFactory.cs
Normal file
102
lib/bc-csharp/crypto/src/pkcs/EncryptedPrivateKeyInfoFactory.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
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)
|
||||
{
|
||||
IBufferedCipher cipher = PbeUtilities.CreateEngine(algorithm) as IBufferedCipher;
|
||||
if (cipher == null)
|
||||
throw new Exception("Unknown encryption algorithm: " + algorithm);
|
||||
|
||||
Asn1Encodable pbeParameters = PbeUtilities.GenerateAlgorithmParameters(
|
||||
algorithm, salt, iterationCount);
|
||||
ICipherParameters cipherParameters = PbeUtilities.GenerateCipherParameters(
|
||||
algorithm, passPhrase, pbeParameters);
|
||||
cipher.Init(true, cipherParameters);
|
||||
byte[] encoding = cipher.DoFinal(keyInfo.GetEncoded());
|
||||
|
||||
DerObjectIdentifier oid = PbeUtilities.GetObjectIdentifier(algorithm);
|
||||
AlgorithmIdentifier algID = new AlgorithmIdentifier(oid, pbeParameters);
|
||||
return new EncryptedPrivateKeyInfo(algID, encoding);
|
||||
}
|
||||
|
||||
public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
|
||||
DerObjectIdentifier cipherAlgorithm,
|
||||
DerObjectIdentifier prfAlgorithm,
|
||||
char[] passPhrase,
|
||||
byte[] salt,
|
||||
int iterationCount,
|
||||
SecureRandom random,
|
||||
AsymmetricKeyParameter key)
|
||||
{
|
||||
return CreateEncryptedPrivateKeyInfo(
|
||||
cipherAlgorithm, prfAlgorithm, passPhrase, salt, iterationCount, random,
|
||||
PrivateKeyInfoFactory.CreatePrivateKeyInfo(key));
|
||||
}
|
||||
|
||||
public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
|
||||
DerObjectIdentifier cipherAlgorithm,
|
||||
DerObjectIdentifier prfAlgorithm,
|
||||
char[] passPhrase,
|
||||
byte[] salt,
|
||||
int iterationCount,
|
||||
SecureRandom random,
|
||||
PrivateKeyInfo keyInfo)
|
||||
{
|
||||
IBufferedCipher cipher = CipherUtilities.GetCipher(cipherAlgorithm) as IBufferedCipher;
|
||||
if (cipher == null)
|
||||
throw new Exception("Unknown encryption algorithm: " + cipherAlgorithm);
|
||||
|
||||
Asn1Encodable pbeParameters = PbeUtilities.GenerateAlgorithmParameters(
|
||||
cipherAlgorithm, prfAlgorithm, salt, iterationCount, random);
|
||||
ICipherParameters cipherParameters = PbeUtilities.GenerateCipherParameters(
|
||||
PkcsObjectIdentifiers.IdPbeS2, passPhrase, pbeParameters);
|
||||
cipher.Init(true, cipherParameters);
|
||||
byte[] encoding = cipher.DoFinal(keyInfo.GetEncoded());
|
||||
|
||||
AlgorithmIdentifier algID = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdPbeS2, pbeParameters);
|
||||
return new EncryptedPrivateKeyInfo(algID, encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
50
lib/bc-csharp/crypto/src/pkcs/PKCS12StoreBuilder.cs
Normal file
50
lib/bc-csharp/crypto/src/pkcs/PKCS12StoreBuilder.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class Pkcs12StoreBuilder
|
||||
{
|
||||
private DerObjectIdentifier keyAlgorithm = PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc;
|
||||
private DerObjectIdentifier certAlgorithm = PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc;
|
||||
private DerObjectIdentifier keyPrfAlgorithm = null;
|
||||
private DerObjectIdentifier certPrfAlgorithm = null;
|
||||
private bool useDerEncoding = false;
|
||||
|
||||
public Pkcs12StoreBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
public Pkcs12Store Build()
|
||||
{
|
||||
return new Pkcs12Store(keyAlgorithm, keyPrfAlgorithm, certAlgorithm, certPrfAlgorithm, useDerEncoding);
|
||||
}
|
||||
|
||||
public Pkcs12StoreBuilder SetCertAlgorithm(DerObjectIdentifier certAlgorithm)
|
||||
{
|
||||
this.certAlgorithm = certAlgorithm;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Pkcs12StoreBuilder SetKeyAlgorithm(DerObjectIdentifier keyAlgorithm)
|
||||
{
|
||||
this.keyAlgorithm = keyAlgorithm;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Specify a PKCS#5 Scheme 2 encryption for keys
|
||||
public Pkcs12StoreBuilder SetKeyAlgorithm(DerObjectIdentifier keyAlgorithm, DerObjectIdentifier keyPrfAlgorithm)
|
||||
{
|
||||
this.keyAlgorithm = keyAlgorithm;
|
||||
this.keyPrfAlgorithm = keyPrfAlgorithm;
|
||||
return this;
|
||||
}
|
||||
public Pkcs12StoreBuilder SetUseDerEncoding(bool useDerEncoding)
|
||||
{
|
||||
this.useDerEncoding = useDerEncoding;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
553
lib/bc-csharp/crypto/src/pkcs/Pkcs10CertificationRequest.cs
Normal file
553
lib/bc-csharp/crypto/src/pkcs/Pkcs10CertificationRequest.cs
Normal file
@@ -0,0 +1,553 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
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;
|
||||
using Org.BouncyCastle.Crypto.Operators;
|
||||
using Org.BouncyCastle.Asn1.Utilities;
|
||||
|
||||
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
|
||||
{
|
||||
protected static readonly IDictionary algorithms = Platform.CreateHashtable();
|
||||
protected static readonly IDictionary exParams = Platform.CreateHashtable();
|
||||
protected static readonly IDictionary keyAlgorithms = Platform.CreateHashtable();
|
||||
protected static readonly IDictionary oids = Platform.CreateHashtable();
|
||||
protected static readonly ISet noParams = new HashSet();
|
||||
|
||||
static Pkcs10CertificationRequest()
|
||||
{
|
||||
algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
|
||||
algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
|
||||
algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
|
||||
algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
|
||||
algorithms.Add("RSAWITHMD5", PkcsObjectIdentifiers.MD5WithRsaEncryption);
|
||||
algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
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", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
|
||||
algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
|
||||
algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
|
||||
algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
|
||||
algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
|
||||
algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
|
||||
algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
|
||||
algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
|
||||
algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
|
||||
algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
|
||||
algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384);
|
||||
algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512);
|
||||
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(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)WITHRSA");
|
||||
oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411WITHGOST3410");
|
||||
oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, "GOST3411WITHECGOST3410");
|
||||
|
||||
oids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5WITHRSA");
|
||||
oids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2WITHRSA");
|
||||
oids.Add(X9ObjectIdentifiers.IdDsaWithSha1, "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.MD5WithRsa, "MD5WITHRSA");
|
||||
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(OiwObjectIdentifiers.DsaWithSha1);
|
||||
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));
|
||||
}
|
||||
|
||||
protected Pkcs10CertificationRequest()
|
||||
{
|
||||
}
|
||||
|
||||
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)
|
||||
: this(new Asn1SignatureFactory(signatureAlgorithm, signingKey), subject, publicKey, attributes)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
|
||||
/// </summary>
|
||||
///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</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">Ignored.</param>
|
||||
[Obsolete("Use constructor without 'signingKey' parameter (ignored here)")]
|
||||
public Pkcs10CertificationRequest(
|
||||
ISignatureFactory signatureFactory,
|
||||
X509Name subject,
|
||||
AsymmetricKeyParameter publicKey,
|
||||
Asn1Set attributes,
|
||||
AsymmetricKeyParameter signingKey)
|
||||
: this(signatureFactory, subject, publicKey, attributes)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
|
||||
/// </summary>
|
||||
///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</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>
|
||||
public Pkcs10CertificationRequest(
|
||||
ISignatureFactory signatureFactory,
|
||||
X509Name subject,
|
||||
AsymmetricKeyParameter publicKey,
|
||||
Asn1Set attributes)
|
||||
{
|
||||
if (signatureFactory == null)
|
||||
throw new ArgumentNullException("signatureFactory");
|
||||
if (subject == null)
|
||||
throw new ArgumentNullException("subject");
|
||||
if (publicKey == null)
|
||||
throw new ArgumentNullException("publicKey");
|
||||
if (publicKey.IsPrivate)
|
||||
throw new ArgumentException("expected public key", "publicKey");
|
||||
|
||||
Init(signatureFactory, subject, publicKey, attributes);
|
||||
}
|
||||
|
||||
private void Init(
|
||||
ISignatureFactory signatureFactory,
|
||||
X509Name subject,
|
||||
AsymmetricKeyParameter publicKey,
|
||||
Asn1Set attributes)
|
||||
{
|
||||
this.sigAlgId = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
|
||||
|
||||
SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
|
||||
|
||||
this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
|
||||
|
||||
IStreamCalculator streamCalculator = signatureFactory.CreateCalculator();
|
||||
|
||||
byte[] reqInfoData = reqInfo.GetDerEncoded();
|
||||
|
||||
streamCalculator.Stream.Write(reqInfoData, 0, reqInfoData.Length);
|
||||
|
||||
Platform.Dispose(streamCalculator.Stream);
|
||||
|
||||
// Generate Signature.
|
||||
sigBits = new DerBitString(((IBlockResult)streamCalculator.GetResult()).Collect());
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
return Verify(new Asn1VerifierFactoryProvider(publicKey));
|
||||
}
|
||||
|
||||
public bool Verify(
|
||||
IVerifierFactoryProvider verifierProvider)
|
||||
{
|
||||
return Verify(verifierProvider.CreateVerifierFactory(sigAlgId));
|
||||
}
|
||||
|
||||
public bool Verify(
|
||||
IVerifierFactory verifier)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] b = reqInfo.GetDerEncoded();
|
||||
|
||||
IStreamCalculator streamCalculator = verifier.CreateCalculator();
|
||||
|
||||
streamCalculator.Stream.Write(b, 0, b.Length);
|
||||
|
||||
Platform.Dispose(streamCalculator.Stream);
|
||||
|
||||
return ((IVerifier)streamCalculator.GetResult()).IsVerified(sigBits.GetOctets());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new SignatureException("exception encoding TBS cert request", e);
|
||||
}
|
||||
}
|
||||
|
||||
// /// <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 (Platform.EndsWith(signature.AlgorithmName, "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.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
|
||||
{
|
||||
RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(asn1Params);
|
||||
return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
|
||||
}
|
||||
}
|
||||
|
||||
return sigAlgId.Algorithm.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 (NistObjectIdentifiers.IdSha512_224.Equals(digestAlgOID))
|
||||
{
|
||||
return "SHA512(224)";
|
||||
}
|
||||
else if (NistObjectIdentifiers.IdSha512_256.Equals(digestAlgOID))
|
||||
{
|
||||
return "SHA512(256)";
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns X509Extensions if the Extensions Request attribute can be found and returns the extensions block.
|
||||
/// </summary>
|
||||
/// <returns>X509Extensions block or null if one cannot be found.</returns>
|
||||
public X509Extensions GetRequestedExtensions()
|
||||
{
|
||||
if (reqInfo.Attributes != null)
|
||||
{
|
||||
foreach (Asn1Encodable item in reqInfo.Attributes)
|
||||
{
|
||||
AttributePkcs attributePkcs;
|
||||
try
|
||||
{
|
||||
attributePkcs = AttributePkcs.GetInstance(item);
|
||||
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
throw new ArgumentException("encountered non PKCS attribute in extensions block", ex);
|
||||
}
|
||||
|
||||
if (attributePkcs.AttrType.Equals(PkcsObjectIdentifiers.Pkcs9AtExtensionRequest))
|
||||
{
|
||||
X509ExtensionsGenerator generator = new X509ExtensionsGenerator();
|
||||
|
||||
Asn1Sequence extensionSequence = Asn1Sequence.GetInstance(attributePkcs.AttrValues[0]);
|
||||
|
||||
|
||||
foreach (Asn1Encodable seqItem in extensionSequence)
|
||||
{
|
||||
|
||||
Asn1Sequence itemSeq = Asn1Sequence.GetInstance(seqItem);
|
||||
if (itemSeq.Count == 2)
|
||||
{
|
||||
generator.AddExtension(DerObjectIdentifier.GetInstance(itemSeq[0]), false, Asn1OctetString.GetInstance(itemSeq[1]).GetOctets());
|
||||
}
|
||||
else if (itemSeq.Count == 3)
|
||||
{
|
||||
generator.AddExtension(DerObjectIdentifier.GetInstance(itemSeq[0]), DerBoolean.GetInstance(itemSeq[1]).IsTrue, Asn1OctetString.GetInstance(itemSeq[2]).GetOctets());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("incorrect sequence size of X509Extension got " + itemSeq.Count + " expected 2 or 3");
|
||||
}
|
||||
}
|
||||
|
||||
return generator.Generate();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
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 creating and verifying Pkcs10 Certification requests (this is an extension on <see cref="Pkcs10CertificationRequest"/>).
|
||||
/// The requests are made using delay signing. This is useful for situations where
|
||||
/// the private key is in another environment and not directly accessible (e.g. HSM)
|
||||
/// So the first step creates the request, then the signing is done outside this
|
||||
/// object and the signature is then used to complete the request.
|
||||
/// </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 Pkcs10CertificationRequestDelaySigned : Pkcs10CertificationRequest
|
||||
{
|
||||
protected Pkcs10CertificationRequestDelaySigned()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
public Pkcs10CertificationRequestDelaySigned(
|
||||
byte[] encoded)
|
||||
: base(encoded)
|
||||
{
|
||||
}
|
||||
public Pkcs10CertificationRequestDelaySigned(
|
||||
Asn1Sequence seq)
|
||||
: base(seq)
|
||||
{
|
||||
}
|
||||
public Pkcs10CertificationRequestDelaySigned(
|
||||
Stream input)
|
||||
: base(input)
|
||||
{
|
||||
}
|
||||
public Pkcs10CertificationRequestDelaySigned(
|
||||
string signatureAlgorithm,
|
||||
X509Name subject,
|
||||
AsymmetricKeyParameter publicKey,
|
||||
Asn1Set attributes,
|
||||
AsymmetricKeyParameter signingKey)
|
||||
: base(signatureAlgorithm, subject, publicKey, attributes, signingKey)
|
||||
{
|
||||
}
|
||||
/// <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>
|
||||
/// <remarks>
|
||||
/// After the object is constructed use the <see cref="GetDataToSign"/> and finally the
|
||||
/// SignRequest methods to finalize the request.
|
||||
/// </remarks>
|
||||
public Pkcs10CertificationRequestDelaySigned(
|
||||
string signatureAlgorithm,
|
||||
X509Name subject,
|
||||
AsymmetricKeyParameter publicKey,
|
||||
Asn1Set attributes)
|
||||
{
|
||||
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");
|
||||
// DerObjectIdentifier sigOid = SignerUtilities.GetObjectIdentifier(signatureAlgorithm);
|
||||
string algorithmName = Platform.ToUpperInvariant(signatureAlgorithm);
|
||||
DerObjectIdentifier sigOid = (DerObjectIdentifier) algorithms[algorithmName];
|
||||
if (sigOid == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
sigOid = new DerObjectIdentifier(algorithmName);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ArgumentException("Unknown signature type requested", e);
|
||||
}
|
||||
}
|
||||
if (noParams.Contains(sigOid))
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid);
|
||||
}
|
||||
else if (exParams.Contains(algorithmName))
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid, (Asn1Encodable) exParams[algorithmName]);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid, DerNull.Instance);
|
||||
}
|
||||
SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
|
||||
this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
|
||||
}
|
||||
public byte[] GetDataToSign()
|
||||
{
|
||||
return reqInfo.GetDerEncoded();
|
||||
}
|
||||
public void SignRequest(byte[] signedData)
|
||||
{
|
||||
//build the signature from the signed data
|
||||
sigBits = new DerBitString(signedData);
|
||||
}
|
||||
public void SignRequest(DerBitString signedData)
|
||||
{
|
||||
//build the signature from the signed data
|
||||
sigBits = signedData;
|
||||
}
|
||||
}
|
||||
}
|
||||
64
lib/bc-csharp/crypto/src/pkcs/Pkcs12Entry.cs
Normal file
64
lib/bc-csharp/crypto/src/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 IDictionary attributes;
|
||||
|
||||
protected internal Pkcs12Entry(
|
||||
IDictionary 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); }
|
||||
}
|
||||
}
|
||||
}
|
||||
1159
lib/bc-csharp/crypto/src/pkcs/Pkcs12Store.cs
Normal file
1159
lib/bc-csharp/crypto/src/pkcs/Pkcs12Store.cs
Normal file
File diff suppressed because it is too large
Load Diff
77
lib/bc-csharp/crypto/src/pkcs/Pkcs12Utilities.cs
Normal file
77
lib/bc-csharp/crypto/src/pkcs/Pkcs12Utilities.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
/**
|
||||
* Utility class for reencoding PKCS#12 files to definite length.
|
||||
*/
|
||||
public class Pkcs12Utilities
|
||||
{
|
||||
/**
|
||||
* Just re-encode the outer layer of the PKCS#12 file to definite length encoding.
|
||||
*
|
||||
* @param berPKCS12File - original PKCS#12 file
|
||||
* @return a byte array representing the DER encoding of the PFX structure
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] ConvertToDefiniteLength(
|
||||
byte[] berPkcs12File)
|
||||
{
|
||||
Pfx pfx = Pfx.GetInstance(berPkcs12File);
|
||||
|
||||
return pfx.GetEncoded(Asn1Encodable.Der);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-encode the PKCS#12 structure to definite length encoding at the inner layer
|
||||
* as well, recomputing the MAC accordingly.
|
||||
*
|
||||
* @param berPKCS12File - original PKCS12 file.
|
||||
* @param provider - provider to use for MAC calculation.
|
||||
* @return a byte array representing the DER encoding of the PFX structure.
|
||||
* @throws IOException on parsing, encoding errors.
|
||||
*/
|
||||
public static byte[] ConvertToDefiniteLength(
|
||||
byte[] berPkcs12File,
|
||||
char[] passwd)
|
||||
{
|
||||
Pfx pfx = Pfx.GetInstance(berPkcs12File);
|
||||
|
||||
ContentInfo info = pfx.AuthSafe;
|
||||
|
||||
Asn1OctetString content = Asn1OctetString.GetInstance(info.Content);
|
||||
Asn1Object obj = Asn1Object.FromByteArray(content.GetOctets());
|
||||
|
||||
info = new ContentInfo(info.ContentType, new DerOctetString(obj.GetEncoded(Asn1Encodable.Der)));
|
||||
|
||||
MacData mData = pfx.MacData;
|
||||
|
||||
try
|
||||
{
|
||||
int itCount = mData.IterationCount.IntValue;
|
||||
byte[] data = Asn1OctetString.GetInstance(info.Content).GetOctets();
|
||||
byte[] res = Pkcs12Store.CalculatePbeMac(
|
||||
mData.Mac.AlgorithmID.Algorithm, mData.GetSalt(), itCount, passwd, false, data);
|
||||
|
||||
AlgorithmIdentifier algId = new AlgorithmIdentifier(
|
||||
mData.Mac.AlgorithmID.Algorithm, DerNull.Instance);
|
||||
DigestInfo dInfo = new DigestInfo(algId, res);
|
||||
|
||||
mData = new MacData(dInfo, mData.GetSalt(), itCount);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException("error constructing MAC: " + e.ToString());
|
||||
}
|
||||
|
||||
pfx = new Pfx(info, mData);
|
||||
|
||||
return pfx.GetEncoded(Asn1Encodable.Der);
|
||||
}
|
||||
}
|
||||
}
|
||||
106
lib/bc-csharp/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs
Normal file
106
lib/bc-csharp/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
/// <summary>
|
||||
/// A holding class for a PKCS#8 encrypted private key info object that allows for its decryption.
|
||||
/// </summary>
|
||||
public class Pkcs8EncryptedPrivateKeyInfo
|
||||
{
|
||||
private EncryptedPrivateKeyInfo encryptedPrivateKeyInfo;
|
||||
|
||||
private static EncryptedPrivateKeyInfo parseBytes(byte[] pkcs8Encoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
return EncryptedPrivateKeyInfo.GetInstance(pkcs8Encoding);
|
||||
}
|
||||
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
throw new PkcsIOException("malformed data: " + e.Message, e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new PkcsIOException("malformed data: " + e.Message, e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base constructor from a PKCS#8 EncryptedPrivateKeyInfo object.
|
||||
/// </summary>
|
||||
/// <param name="encryptedPrivateKeyInfo">A PKCS#8 EncryptedPrivateKeyInfo object.</param>
|
||||
public Pkcs8EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfo encryptedPrivateKeyInfo)
|
||||
{
|
||||
this.encryptedPrivateKeyInfo = encryptedPrivateKeyInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base constructor from a BER encoding of a PKCS#8 EncryptedPrivateKeyInfo object.
|
||||
/// </summary>
|
||||
/// <param name="encryptedPrivateKeyInfo">A BER encoding of a PKCS#8 EncryptedPrivateKeyInfo objects.</param>
|
||||
public Pkcs8EncryptedPrivateKeyInfo(byte[] encryptedPrivateKeyInfo) : this(parseBytes(encryptedPrivateKeyInfo))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the underlying ASN.1 structure inside this object.
|
||||
/// </summary>
|
||||
/// <returns>Return the EncryptedPrivateKeyInfo structure in this object.</returns>
|
||||
public EncryptedPrivateKeyInfo ToAsn1Structure()
|
||||
{
|
||||
return encryptedPrivateKeyInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a copy of the encrypted data in this structure.
|
||||
/// </summary>
|
||||
/// <returns>Return a copy of the encrypted data in this object.</returns>
|
||||
public byte[] GetEncryptedData()
|
||||
{
|
||||
return encryptedPrivateKeyInfo.GetEncryptedData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a binary ASN.1 encoding of the EncryptedPrivateKeyInfo structure in this object.
|
||||
/// </summary>
|
||||
/// <returns>A byte array containing the encoded object.</returns>
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return encryptedPrivateKeyInfo.GetEncoded();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a decryptor from the passed in provider and decrypt the encrypted private key info, returning the result.
|
||||
/// </summary>
|
||||
/// <param name="inputDecryptorProvider">A provider to query for decryptors for the object.</param>
|
||||
/// <returns>The decrypted private key info structure.</returns>
|
||||
public PrivateKeyInfo DecryptPrivateKeyInfo(IDecryptorBuilderProvider inputDecryptorProvider)
|
||||
{
|
||||
try
|
||||
{
|
||||
ICipherBuilder decryptorBuilder = inputDecryptorProvider.CreateDecryptorBuilder(encryptedPrivateKeyInfo.EncryptionAlgorithm);
|
||||
|
||||
ICipher encIn = decryptorBuilder.BuildCipher(new MemoryInputStream(encryptedPrivateKeyInfo.GetEncryptedData()));
|
||||
|
||||
Stream strm = encIn.Stream;
|
||||
byte[] data = Streams.ReadAll(encIn.Stream);
|
||||
Platform.Dispose(strm);
|
||||
|
||||
return PrivateKeyInfo.GetInstance(data);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new PkcsException("unable to read encrypted data: " + e.Message, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class Pkcs8EncryptedPrivateKeyInfoBuilder
|
||||
{
|
||||
private PrivateKeyInfo privateKeyInfo;
|
||||
|
||||
public Pkcs8EncryptedPrivateKeyInfoBuilder(byte[] privateKeyInfo): this(PrivateKeyInfo.GetInstance(privateKeyInfo))
|
||||
{
|
||||
}
|
||||
|
||||
public Pkcs8EncryptedPrivateKeyInfoBuilder(PrivateKeyInfo privateKeyInfo)
|
||||
{
|
||||
this.privateKeyInfo = privateKeyInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create the encrypted private key info using the passed in encryptor.
|
||||
/// </summary>
|
||||
/// <param name="encryptor">The encryptor to use.</param>
|
||||
/// <returns>An encrypted private key info containing the original private key info.</returns>
|
||||
public Pkcs8EncryptedPrivateKeyInfo Build(
|
||||
ICipherBuilder encryptor)
|
||||
{
|
||||
try
|
||||
{
|
||||
MemoryStream bOut = new MemoryOutputStream();
|
||||
ICipher cOut = encryptor.BuildCipher(bOut);
|
||||
byte[] keyData = privateKeyInfo.GetEncoded();
|
||||
|
||||
Stream str = cOut.Stream;
|
||||
str.Write(keyData, 0, keyData.Length);
|
||||
Platform.Dispose(str);
|
||||
|
||||
return new Pkcs8EncryptedPrivateKeyInfo(new EncryptedPrivateKeyInfo((AlgorithmIdentifier)encryptor.AlgorithmDetails, bOut.ToArray()));
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
throw new InvalidOperationException("cannot encode privateKeyInfo");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
lib/bc-csharp/crypto/src/pkcs/PkcsException.cs
Normal file
21
lib/bc-csharp/crypto/src/pkcs/PkcsException.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
/// <summary>
|
||||
/// Base exception for PKCS related issues.
|
||||
/// </summary>
|
||||
public class PkcsException
|
||||
: Exception
|
||||
{
|
||||
public PkcsException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public PkcsException(string message, Exception underlying)
|
||||
: base(message, underlying)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
19
lib/bc-csharp/crypto/src/pkcs/PkcsIOException.cs
Normal file
19
lib/bc-csharp/crypto/src/pkcs/PkcsIOException.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
/// <summary>
|
||||
/// Base exception for parsing related issues in the PKCS namespace.
|
||||
/// </summary>
|
||||
public class PkcsIOException: IOException
|
||||
{
|
||||
public PkcsIOException(String message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public PkcsIOException(String message, Exception underlying) : base(message, underlying)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
291
lib/bc-csharp/crypto/src/pkcs/PrivateKeyInfoFactory.cs
Normal file
291
lib/bc-csharp/crypto/src/pkcs/PrivateKeyInfoFactory.cs
Normal file
@@ -0,0 +1,291 @@
|
||||
using System;
|
||||
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.CryptoPro;
|
||||
using Org.BouncyCastle.Asn1.EdEC;
|
||||
using Org.BouncyCastle.Asn1.Oiw;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Asn1.Rosstandart;
|
||||
using Org.BouncyCastle.Asn1.Sec;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Asn1.X9;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
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 privateKey)
|
||||
{
|
||||
return CreatePrivateKeyInfo(privateKey, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a PrivateKeyInfo representation of a private key with attributes.
|
||||
*
|
||||
* @param privateKey the key to be encoded into the info object.
|
||||
* @param attributes the set of attributes to be included.
|
||||
* @return the appropriate PrivateKeyInfo
|
||||
* @throws java.io.IOException on an error encoding the key
|
||||
*/
|
||||
public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes)
|
||||
{
|
||||
if (privateKey == null)
|
||||
throw new ArgumentNullException("privateKey");
|
||||
if (!privateKey.IsPrivate)
|
||||
throw new ArgumentException("Public key passed - private key expected", "privateKey");
|
||||
|
||||
if (privateKey is ElGamalPrivateKeyParameters)
|
||||
{
|
||||
ElGamalPrivateKeyParameters _key = (ElGamalPrivateKeyParameters)privateKey;
|
||||
ElGamalParameters egp = _key.Parameters;
|
||||
return new PrivateKeyInfo(
|
||||
new AlgorithmIdentifier(OiwObjectIdentifiers.ElGamalAlgorithm, new ElGamalParameter(egp.P, egp.G).ToAsn1Object()),
|
||||
new DerInteger(_key.X),
|
||||
attributes);
|
||||
}
|
||||
|
||||
if (privateKey is DsaPrivateKeyParameters)
|
||||
{
|
||||
DsaPrivateKeyParameters _key = (DsaPrivateKeyParameters)privateKey;
|
||||
DsaParameters dp = _key.Parameters;
|
||||
return new PrivateKeyInfo(
|
||||
new AlgorithmIdentifier(X9ObjectIdentifiers.IdDsa, new DsaParameter(dp.P, dp.Q, dp.G).ToAsn1Object()),
|
||||
new DerInteger(_key.X),
|
||||
attributes);
|
||||
}
|
||||
|
||||
if (privateKey is DHPrivateKeyParameters)
|
||||
{
|
||||
DHPrivateKeyParameters _key = (DHPrivateKeyParameters)privateKey;
|
||||
|
||||
DHParameter p = new DHParameter(
|
||||
_key.Parameters.P, _key.Parameters.G, _key.Parameters.L);
|
||||
|
||||
return new PrivateKeyInfo(
|
||||
new AlgorithmIdentifier(_key.AlgorithmOid, p.ToAsn1Object()),
|
||||
new DerInteger(_key.X),
|
||||
attributes);
|
||||
}
|
||||
|
||||
if (privateKey is RsaKeyParameters)
|
||||
{
|
||||
AlgorithmIdentifier algID = new AlgorithmIdentifier(
|
||||
PkcsObjectIdentifiers.RsaEncryption, DerNull.Instance);
|
||||
|
||||
RsaPrivateKeyStructure keyStruct;
|
||||
if (privateKey is RsaPrivateCrtKeyParameters)
|
||||
{
|
||||
RsaPrivateCrtKeyParameters _key = (RsaPrivateCrtKeyParameters)privateKey;
|
||||
|
||||
keyStruct = new RsaPrivateKeyStructure(
|
||||
_key.Modulus,
|
||||
_key.PublicExponent,
|
||||
_key.Exponent,
|
||||
_key.P,
|
||||
_key.Q,
|
||||
_key.DP,
|
||||
_key.DQ,
|
||||
_key.QInv);
|
||||
}
|
||||
else
|
||||
{
|
||||
RsaKeyParameters _key = (RsaKeyParameters) privateKey;
|
||||
|
||||
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(), attributes);
|
||||
}
|
||||
|
||||
if (privateKey is ECPrivateKeyParameters)
|
||||
{
|
||||
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) privateKey;
|
||||
DerBitString publicKey = new DerBitString(ECKeyPairGenerator.GetCorrespondingPublicKey(priv).Q.GetEncoded(false));
|
||||
|
||||
ECDomainParameters dp = priv.Parameters;
|
||||
|
||||
// ECGOST3410
|
||||
if (dp is ECGost3410Parameters)
|
||||
{
|
||||
ECGost3410Parameters domainParameters = (ECGost3410Parameters) dp;
|
||||
|
||||
Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
|
||||
(domainParameters).PublicKeyParamSet,
|
||||
(domainParameters).DigestParamSet,
|
||||
(domainParameters).EncryptionParamSet);
|
||||
|
||||
bool is512 = priv.D.BitLength > 256;
|
||||
DerObjectIdentifier identifier = (is512) ?
|
||||
RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512 :
|
||||
RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
|
||||
int size = (is512) ? 64 : 32;
|
||||
|
||||
byte[] encKey = new byte[size];
|
||||
|
||||
ExtractBytes(encKey, size, 0, priv.D);
|
||||
|
||||
return new PrivateKeyInfo(new AlgorithmIdentifier(identifier, gostParams), new DerOctetString(encKey));
|
||||
}
|
||||
|
||||
|
||||
int orderBitLength = dp.N.BitLength;
|
||||
|
||||
AlgorithmIdentifier algID;
|
||||
ECPrivateKeyStructure ec;
|
||||
|
||||
if (priv.AlgorithmName == "ECGOST3410")
|
||||
{
|
||||
if (priv.PublicKeyParamSet == null)
|
||||
throw Platform.CreateNotImplementedException("Not a CryptoPro parameter set");
|
||||
|
||||
Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
|
||||
priv.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet);
|
||||
|
||||
algID = new AlgorithmIdentifier(CryptoProObjectIdentifiers.GostR3410x2001, gostParams);
|
||||
|
||||
// TODO Do we need to pass any parameters here?
|
||||
ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
X962Parameters x962;
|
||||
if (priv.PublicKeyParamSet == null)
|
||||
{
|
||||
X9ECParameters ecP = new X9ECParameters(dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed());
|
||||
x962 = new X962Parameters(ecP);
|
||||
}
|
||||
else
|
||||
{
|
||||
x962 = new X962Parameters(priv.PublicKeyParamSet);
|
||||
}
|
||||
|
||||
ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, x962);
|
||||
|
||||
algID = new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, x962);
|
||||
}
|
||||
|
||||
return new PrivateKeyInfo(algID, ec, attributes);
|
||||
}
|
||||
|
||||
if (privateKey is Gost3410PrivateKeyParameters)
|
||||
{
|
||||
Gost3410PrivateKeyParameters _key = (Gost3410PrivateKeyParameters)privateKey;
|
||||
|
||||
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), attributes);
|
||||
}
|
||||
|
||||
if (privateKey is X448PrivateKeyParameters)
|
||||
{
|
||||
X448PrivateKeyParameters key = (X448PrivateKeyParameters)privateKey;
|
||||
|
||||
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448),
|
||||
new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
|
||||
}
|
||||
|
||||
if (privateKey is X25519PrivateKeyParameters)
|
||||
{
|
||||
X25519PrivateKeyParameters key = (X25519PrivateKeyParameters)privateKey;
|
||||
|
||||
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519),
|
||||
new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
|
||||
}
|
||||
|
||||
if (privateKey is Ed448PrivateKeyParameters)
|
||||
{
|
||||
Ed448PrivateKeyParameters key = (Ed448PrivateKeyParameters)privateKey;
|
||||
|
||||
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448),
|
||||
new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
|
||||
}
|
||||
|
||||
if (privateKey is Ed25519PrivateKeyParameters)
|
||||
{
|
||||
Ed25519PrivateKeyParameters key = (Ed25519PrivateKeyParameters)privateKey;
|
||||
|
||||
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519),
|
||||
new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
|
||||
}
|
||||
|
||||
throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey));
|
||||
}
|
||||
|
||||
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) as IBufferedCipher;
|
||||
if (cipher == null)
|
||||
throw new Exception("Unknown encryption algorithm: " + algID.Algorithm);
|
||||
|
||||
ICipherParameters cipherParameters = PbeUtilities.GenerateCipherParameters(
|
||||
algID, passPhrase, wrongPkcs12Zero);
|
||||
cipher.Init(false, cipherParameters);
|
||||
byte[] keyBytes = cipher.DoFinal(encInfo.GetEncryptedData());
|
||||
|
||||
return PrivateKeyInfo.GetInstance(keyBytes);
|
||||
}
|
||||
|
||||
private static void ExtractBytes(byte[] encKey, int size, int offSet, BigInteger bI)
|
||||
{
|
||||
byte[] val = bI.ToByteArray();
|
||||
if (val.Length < size)
|
||||
{
|
||||
byte[] tmp = new byte[size];
|
||||
Array.Copy(val, 0, tmp, tmp.Length - val.Length, val.Length);
|
||||
val = tmp;
|
||||
}
|
||||
|
||||
for (int i = 0; i != size; i++)
|
||||
{
|
||||
encKey[offSet + i] = val[val.Length - 1 - i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
60
lib/bc-csharp/crypto/src/pkcs/X509CertificateEntry.cs
Normal file
60
lib/bc-csharp/crypto/src/pkcs/X509CertificateEntry.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class X509CertificateEntry
|
||||
: Pkcs12Entry
|
||||
{
|
||||
private readonly X509Certificate cert;
|
||||
|
||||
public X509CertificateEntry(
|
||||
X509Certificate cert)
|
||||
: base(Platform.CreateHashtable())
|
||||
{
|
||||
this.cert = cert;
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || PORTABLE)
|
||||
[Obsolete]
|
||||
public X509CertificateEntry(
|
||||
X509Certificate cert,
|
||||
Hashtable attributes)
|
||||
: base(attributes)
|
||||
{
|
||||
this.cert = cert;
|
||||
}
|
||||
#endif
|
||||
|
||||
public X509CertificateEntry(
|
||||
X509Certificate cert,
|
||||
IDictionary attributes)
|
||||
: base(attributes)
|
||||
{
|
||||
this.cert = cert;
|
||||
}
|
||||
|
||||
public X509Certificate Certificate
|
||||
{
|
||||
get { return this.cert; }
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
X509CertificateEntry other = obj as X509CertificateEntry;
|
||||
|
||||
if (other == null)
|
||||
return false;
|
||||
|
||||
return cert.Equals(other.cert);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return ~cert.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user