mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交Unity 联机Pro
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System.Collections.Generic;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class AsymmetricKeyEntry
|
||||
: Pkcs12Entry
|
||||
{
|
||||
private readonly AsymmetricKeyParameter key;
|
||||
|
||||
public AsymmetricKeyEntry(AsymmetricKeyParameter key)
|
||||
: base(new Dictionary<DerObjectIdentifier, Asn1Encodable>())
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public AsymmetricKeyEntry(AsymmetricKeyParameter key,
|
||||
IDictionary<DerObjectIdentifier, Asn1Encodable> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0fc1e2e40052dc44899abad8920a4e5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,106 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ff4b19a3a083b346a18148d9cd0ba8d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,53 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class Pkcs12StoreBuilder
|
||||
{
|
||||
private DerObjectIdentifier keyAlgorithm = PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc;
|
||||
private DerObjectIdentifier certAlgorithm = PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc;
|
||||
private DerObjectIdentifier keyPrfAlgorithm = null;
|
||||
private bool useDerEncoding = false;
|
||||
|
||||
public Pkcs12StoreBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
public Pkcs12Store Build()
|
||||
{
|
||||
return new Pkcs12Store(keyAlgorithm, keyPrfAlgorithm, certAlgorithm, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43aa4e117903bc6489e2b003abb2af10
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,538 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.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
|
||||
{
|
||||
internal static readonly Dictionary<string, DerObjectIdentifier> m_algorithms =
|
||||
new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
|
||||
internal static readonly Dictionary<string, Asn1Encodable> m_exParams =
|
||||
new Dictionary<string, Asn1Encodable>(StringComparer.OrdinalIgnoreCase);
|
||||
internal static readonly Dictionary<DerObjectIdentifier, string> m_keyAlgorithms =
|
||||
new Dictionary<DerObjectIdentifier, string>();
|
||||
internal static readonly Dictionary<DerObjectIdentifier, string> m_oids =
|
||||
new Dictionary<DerObjectIdentifier, string>();
|
||||
internal static readonly HashSet<DerObjectIdentifier> m_noParams = new HashSet<DerObjectIdentifier>();
|
||||
|
||||
static Pkcs10CertificationRequest()
|
||||
{
|
||||
m_algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
|
||||
m_algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
|
||||
m_algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
|
||||
m_algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
|
||||
m_algorithms.Add("RSAWITHMD5", PkcsObjectIdentifiers.MD5WithRsaEncryption);
|
||||
m_algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
m_algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
m_algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
m_algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
|
||||
m_algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
m_algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
|
||||
m_algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
m_algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
|
||||
m_algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
m_algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
m_algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
|
||||
m_algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
m_algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
m_algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
m_algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
|
||||
m_algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
m_algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
m_algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
m_algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
|
||||
m_algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
m_algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
m_algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
m_algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
m_algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
|
||||
m_algorithms.Add("RSAWITHSHA1", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
|
||||
m_algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
|
||||
m_algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
|
||||
m_algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
|
||||
m_algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
|
||||
m_algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
|
||||
m_algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
|
||||
m_algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
|
||||
m_algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
|
||||
m_algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
|
||||
m_algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
|
||||
m_algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384);
|
||||
m_algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512);
|
||||
m_algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
|
||||
m_algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
|
||||
m_algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
|
||||
m_algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
|
||||
m_algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
|
||||
m_algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
|
||||
m_algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
|
||||
m_algorithms.Add("GOST3410WITHGOST3411", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
|
||||
m_algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
m_algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
m_algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
|
||||
//
|
||||
// reverse mappings
|
||||
//
|
||||
m_oids.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1WITHRSA");
|
||||
m_oids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224WITHRSA");
|
||||
m_oids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256WITHRSA");
|
||||
m_oids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384WITHRSA");
|
||||
m_oids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512WITHRSA");
|
||||
m_oids.Add(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)WITHRSA");
|
||||
m_oids.Add(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)WITHRSA");
|
||||
m_oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411WITHGOST3410");
|
||||
m_oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, "GOST3411WITHECGOST3410");
|
||||
|
||||
m_oids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5WITHRSA");
|
||||
m_oids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2WITHRSA");
|
||||
m_oids.Add(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1WITHDSA");
|
||||
m_oids.Add(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1WITHECDSA");
|
||||
m_oids.Add(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224WITHECDSA");
|
||||
m_oids.Add(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256WITHECDSA");
|
||||
m_oids.Add(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384WITHECDSA");
|
||||
m_oids.Add(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512WITHECDSA");
|
||||
m_oids.Add(OiwObjectIdentifiers.MD5WithRsa, "MD5WITHRSA");
|
||||
m_oids.Add(OiwObjectIdentifiers.Sha1WithRsa, "SHA1WITHRSA");
|
||||
m_oids.Add(OiwObjectIdentifiers.DsaWithSha1, "SHA1WITHDSA");
|
||||
m_oids.Add(NistObjectIdentifiers.DsaWithSha224, "SHA224WITHDSA");
|
||||
m_oids.Add(NistObjectIdentifiers.DsaWithSha256, "SHA256WITHDSA");
|
||||
|
||||
//
|
||||
// key types
|
||||
//
|
||||
m_keyAlgorithms.Add(PkcsObjectIdentifiers.RsaEncryption, "RSA");
|
||||
m_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.
|
||||
//
|
||||
m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
|
||||
m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
|
||||
m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
|
||||
m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
|
||||
m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
|
||||
m_noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
|
||||
m_noParams.Add(OiwObjectIdentifiers.DsaWithSha1);
|
||||
m_noParams.Add(NistObjectIdentifiers.DsaWithSha224);
|
||||
m_noParams.Add(NistObjectIdentifiers.DsaWithSha256);
|
||||
|
||||
//
|
||||
// RFC 4491
|
||||
//
|
||||
m_noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
|
||||
m_noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
|
||||
|
||||
//
|
||||
// explicit params
|
||||
//
|
||||
AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
|
||||
m_exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20));
|
||||
|
||||
AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
|
||||
m_exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28));
|
||||
|
||||
AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
|
||||
m_exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32));
|
||||
|
||||
AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
|
||||
m_exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48));
|
||||
|
||||
AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
|
||||
m_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>
|
||||
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<IBlockResult> streamCalculator = signatureFactory.CreateCalculator();
|
||||
using (Stream sigStream = streamCalculator.Stream)
|
||||
{
|
||||
reqInfo.EncodeTo(sigStream, Der);
|
||||
}
|
||||
|
||||
// Generate Signature.
|
||||
sigBits = new DerBitString(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<IVerifier> streamCalculator = verifier.CreateCalculator();
|
||||
using (var stream = streamCalculator.Stream)
|
||||
{
|
||||
stream.Write(b, 0, b.Length);
|
||||
}
|
||||
|
||||
return 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 (Org.BouncyCastle.Utilities.Platform.EndsWith(signature.AlgorithmName, "MGF1"))
|
||||
{
|
||||
throw new NotImplementedException("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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d42133b63178b014eb93bfce9e8655e6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,145 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.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 = CollectionUtilities.GetValueOrNull(m_algorithms, signatureAlgorithm);
|
||||
if (sigOid == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
sigOid = new DerObjectIdentifier(signatureAlgorithm);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ArgumentException("Unknown signature type requested", e);
|
||||
}
|
||||
}
|
||||
if (m_noParams.Contains(sigOid))
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid);
|
||||
}
|
||||
else if (m_exParams.TryGetValue(signatureAlgorithm, out var explicitParameters))
|
||||
{
|
||||
this.sigAlgId = new AlgorithmIdentifier(sigOid, explicitParameters);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 91787b3601b63654b83ed2aaadde6434
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,31 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System.Collections.Generic;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public abstract class Pkcs12Entry
|
||||
{
|
||||
private readonly IDictionary<DerObjectIdentifier, Asn1Encodable> m_attributes;
|
||||
|
||||
protected internal Pkcs12Entry(IDictionary<DerObjectIdentifier, Asn1Encodable> attributes)
|
||||
{
|
||||
m_attributes = attributes;
|
||||
}
|
||||
|
||||
public Asn1Encodable this[DerObjectIdentifier oid]
|
||||
{
|
||||
get { return CollectionUtilities.GetValueOrNull(m_attributes, oid); }
|
||||
}
|
||||
|
||||
public IEnumerable<DerObjectIdentifier> BagAttributeKeys
|
||||
{
|
||||
get { return CollectionUtilities.Proxy(m_attributes.Keys); }
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b33d7051f7d07254d99adcf4f11f449f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,994 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Misc;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class Pkcs12Store
|
||||
{
|
||||
public const string IgnoreUselessPasswordProperty = "BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs12.IgnoreUselessPassword";
|
||||
|
||||
private readonly Dictionary<string, AsymmetricKeyEntry> m_keys =
|
||||
new Dictionary<string, AsymmetricKeyEntry>(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, string> m_localIds = new Dictionary<string, string>();
|
||||
private readonly Dictionary<string, X509CertificateEntry> m_certs =
|
||||
new Dictionary<string, X509CertificateEntry>(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<CertId, X509CertificateEntry> m_chainCerts =
|
||||
new Dictionary<CertId, X509CertificateEntry>();
|
||||
private readonly Dictionary<string, X509CertificateEntry> m_keyCerts =
|
||||
new Dictionary<string, X509CertificateEntry>();
|
||||
private readonly DerObjectIdentifier keyAlgorithm;
|
||||
private readonly DerObjectIdentifier keyPrfAlgorithm;
|
||||
private readonly DerObjectIdentifier certAlgorithm;
|
||||
private readonly bool useDerEncoding;
|
||||
|
||||
private AsymmetricKeyEntry unmarkedKeyEntry = null;
|
||||
|
||||
private const int MinIterations = 1024;
|
||||
private const int SaltSize = 20;
|
||||
|
||||
private static SubjectKeyIdentifier CreateSubjectKeyID(AsymmetricKeyParameter pubKey)
|
||||
{
|
||||
return new SubjectKeyIdentifier(
|
||||
SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubKey));
|
||||
}
|
||||
|
||||
internal class CertId
|
||||
{
|
||||
private readonly byte[] id;
|
||||
|
||||
internal CertId(
|
||||
AsymmetricKeyParameter pubKey)
|
||||
{
|
||||
this.id = CreateSubjectKeyID(pubKey).GetKeyIdentifier();
|
||||
}
|
||||
|
||||
internal CertId(
|
||||
byte[] id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
internal byte[] Id
|
||||
{
|
||||
get { return id; }
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Arrays.GetHashCode(id);
|
||||
}
|
||||
|
||||
public override bool Equals(
|
||||
object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
return true;
|
||||
|
||||
CertId other = obj as CertId;
|
||||
|
||||
if (other == null)
|
||||
return false;
|
||||
|
||||
return Arrays.AreEqual(id, other.id);
|
||||
}
|
||||
}
|
||||
|
||||
internal Pkcs12Store(DerObjectIdentifier keyAlgorithm, DerObjectIdentifier keyPrfAlgorithm,
|
||||
DerObjectIdentifier certAlgorithm, bool useDerEncoding)
|
||||
{
|
||||
this.keyAlgorithm = keyAlgorithm;
|
||||
this.keyPrfAlgorithm = keyPrfAlgorithm;
|
||||
this.certAlgorithm = certAlgorithm;
|
||||
this.useDerEncoding = useDerEncoding;
|
||||
}
|
||||
|
||||
protected virtual void LoadKeyBag(PrivateKeyInfo privKeyInfo, Asn1Set bagAttributes)
|
||||
{
|
||||
AsymmetricKeyParameter privKey = PrivateKeyFactory.CreateKey(privKeyInfo);
|
||||
|
||||
var attributes = new Dictionary<DerObjectIdentifier, Asn1Encodable>();
|
||||
AsymmetricKeyEntry keyEntry = new AsymmetricKeyEntry(privKey, attributes);
|
||||
|
||||
string alias = null;
|
||||
Asn1OctetString localId = null;
|
||||
|
||||
if (bagAttributes != null)
|
||||
{
|
||||
foreach (Asn1Sequence sq in bagAttributes)
|
||||
{
|
||||
DerObjectIdentifier aOid = DerObjectIdentifier.GetInstance(sq[0]);
|
||||
Asn1Set attrSet = Asn1Set.GetInstance(sq[1]);
|
||||
Asn1Encodable attr = null;
|
||||
|
||||
if (attrSet.Count > 0)
|
||||
{
|
||||
// TODO We should be adding all attributes in the set
|
||||
attr = attrSet[0];
|
||||
|
||||
// TODO We might want to "merge" attribute sets with
|
||||
// the same OID - currently, differing values give an error
|
||||
if (attributes.TryGetValue(aOid, out var attributeValue))
|
||||
{
|
||||
// OK, but the value has to be the same
|
||||
if (!attributeValue.Equals(attr))
|
||||
throw new IOException("attempt to add existing attribute with different value");
|
||||
}
|
||||
else
|
||||
{
|
||||
attributes[aOid] = attr;
|
||||
}
|
||||
|
||||
if (aOid.Equals(PkcsObjectIdentifiers.Pkcs9AtFriendlyName))
|
||||
{
|
||||
alias = ((DerBmpString)attr).GetString();
|
||||
// TODO Do these in a separate loop, just collect aliases here
|
||||
m_keys[alias] = keyEntry;
|
||||
}
|
||||
else if (aOid.Equals(PkcsObjectIdentifiers.Pkcs9AtLocalKeyID))
|
||||
{
|
||||
localId = (Asn1OctetString)attr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (localId != null)
|
||||
{
|
||||
string name = Hex.ToHexString(localId.GetOctets());
|
||||
|
||||
if (alias == null)
|
||||
{
|
||||
m_keys[name] = keyEntry;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO There may have been more than one alias
|
||||
m_localIds[alias] = name;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
unmarkedKeyEntry = keyEntry;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void LoadPkcs8ShroudedKeyBag(EncryptedPrivateKeyInfo encPrivKeyInfo, Asn1Set bagAttributes,
|
||||
char[] password, bool wrongPkcs12Zero)
|
||||
{
|
||||
if (password != null)
|
||||
{
|
||||
PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(
|
||||
password, wrongPkcs12Zero, encPrivKeyInfo);
|
||||
|
||||
LoadKeyBag(privInfo, bagAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
public void Load(Stream input, char[] password)
|
||||
{
|
||||
if (input == null)
|
||||
throw new ArgumentNullException("input");
|
||||
|
||||
Pfx bag = Pfx.GetInstance(Asn1Object.FromStream(input));
|
||||
ContentInfo info = bag.AuthSafe;
|
||||
bool wrongPkcs12Zero = false;
|
||||
|
||||
if (bag.MacData != null) // check the mac code
|
||||
{
|
||||
if (password == null)
|
||||
throw new ArgumentNullException("password", "no password supplied when one expected");
|
||||
|
||||
MacData mData = bag.MacData;
|
||||
DigestInfo dInfo = mData.Mac;
|
||||
AlgorithmIdentifier algId = dInfo.AlgorithmID;
|
||||
byte[] salt = mData.GetSalt();
|
||||
int itCount = mData.IterationCount.IntValue;
|
||||
|
||||
byte[] data = Asn1OctetString.GetInstance(info.Content).GetOctets();
|
||||
|
||||
byte[] mac = CalculatePbeMac(algId.Algorithm, salt, itCount, password, false, data);
|
||||
byte[] dig = dInfo.GetDigest();
|
||||
|
||||
if (!Arrays.ConstantTimeAreEqual(mac, dig))
|
||||
{
|
||||
if (password.Length > 0)
|
||||
throw new IOException("PKCS12 key store MAC invalid - wrong password or corrupted file.");
|
||||
|
||||
// Try with incorrect zero length password
|
||||
mac = CalculatePbeMac(algId.Algorithm, salt, itCount, password, true, data);
|
||||
|
||||
if (!Arrays.ConstantTimeAreEqual(mac, dig))
|
||||
throw new IOException("PKCS12 key store MAC invalid - wrong password or corrupted file.");
|
||||
|
||||
wrongPkcs12Zero = true;
|
||||
}
|
||||
}
|
||||
else if (password != null)
|
||||
{
|
||||
string ignoreProperty = Org.BouncyCastle.Utilities.Platform.GetEnvironmentVariable(IgnoreUselessPasswordProperty);
|
||||
bool ignore = ignoreProperty != null && Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase("true", ignoreProperty);
|
||||
|
||||
if (!ignore)
|
||||
{
|
||||
throw new IOException("password supplied for keystore that does not require one");
|
||||
}
|
||||
}
|
||||
|
||||
m_keys.Clear();
|
||||
m_localIds.Clear();
|
||||
unmarkedKeyEntry = null;
|
||||
|
||||
var certBags = new List<SafeBag>();
|
||||
|
||||
if (info.ContentType.Equals(PkcsObjectIdentifiers.Data))
|
||||
{
|
||||
Asn1OctetString content = Asn1OctetString.GetInstance(info.Content);
|
||||
AuthenticatedSafe authSafe = AuthenticatedSafe.GetInstance(content.GetOctets());
|
||||
ContentInfo[] cis = authSafe.GetContentInfo();
|
||||
|
||||
foreach (ContentInfo ci in cis)
|
||||
{
|
||||
DerObjectIdentifier oid = ci.ContentType;
|
||||
|
||||
byte[] octets = null;
|
||||
if (oid.Equals(PkcsObjectIdentifiers.Data))
|
||||
{
|
||||
octets = Asn1OctetString.GetInstance(ci.Content).GetOctets();
|
||||
}
|
||||
else if (oid.Equals(PkcsObjectIdentifiers.EncryptedData))
|
||||
{
|
||||
if (password != null)
|
||||
{
|
||||
EncryptedData d = EncryptedData.GetInstance(ci.Content);
|
||||
octets = CryptPbeData(false, d.EncryptionAlgorithm,
|
||||
password, wrongPkcs12Zero, d.Content.GetOctets());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO Other data types
|
||||
}
|
||||
|
||||
if (octets != null)
|
||||
{
|
||||
Asn1Sequence seq = Asn1Sequence.GetInstance(octets);
|
||||
|
||||
foreach (Asn1Sequence subSeq in seq)
|
||||
{
|
||||
SafeBag b = SafeBag.GetInstance(subSeq);
|
||||
|
||||
if (b.BagID.Equals(PkcsObjectIdentifiers.CertBag))
|
||||
{
|
||||
certBags.Add(b);
|
||||
}
|
||||
else if (b.BagID.Equals(PkcsObjectIdentifiers.Pkcs8ShroudedKeyBag))
|
||||
{
|
||||
LoadPkcs8ShroudedKeyBag(EncryptedPrivateKeyInfo.GetInstance(b.BagValue),
|
||||
b.BagAttributes, password, wrongPkcs12Zero);
|
||||
}
|
||||
else if (b.BagID.Equals(PkcsObjectIdentifiers.KeyBag))
|
||||
{
|
||||
LoadKeyBag(PrivateKeyInfo.GetInstance(b.BagValue), b.BagAttributes);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO Other bag types
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_certs.Clear();
|
||||
m_chainCerts.Clear();
|
||||
m_keyCerts.Clear();
|
||||
|
||||
foreach (SafeBag b in certBags)
|
||||
{
|
||||
CertBag certBag = CertBag.GetInstance(b.BagValue);
|
||||
byte[] octets = ((Asn1OctetString)certBag.CertValue).GetOctets();
|
||||
X509Certificate cert = new X509CertificateParser().ReadCertificate(octets);
|
||||
|
||||
//
|
||||
// set the attributes
|
||||
//
|
||||
var attributes = new Dictionary<DerObjectIdentifier, Asn1Encodable>();
|
||||
Asn1OctetString localId = null;
|
||||
string alias = null;
|
||||
|
||||
if (b.BagAttributes != null)
|
||||
{
|
||||
foreach (Asn1Sequence sq in b.BagAttributes)
|
||||
{
|
||||
DerObjectIdentifier aOid = DerObjectIdentifier.GetInstance(sq[0]);
|
||||
Asn1Set attrSet = Asn1Set.GetInstance(sq[1]);
|
||||
|
||||
if (attrSet.Count > 0)
|
||||
{
|
||||
// TODO We should be adding all attributes in the set
|
||||
Asn1Encodable attr = attrSet[0];
|
||||
|
||||
// TODO We might want to "merge" attribute sets with
|
||||
// the same OID - currently, differing values give an error
|
||||
if (attributes.TryGetValue(aOid, out var attributeValue))
|
||||
{
|
||||
// we've found more than one - one might be incorrect
|
||||
if (PkcsObjectIdentifiers.Pkcs9AtLocalKeyID.Equals(aOid))
|
||||
{
|
||||
string id = Hex.ToHexString(Asn1OctetString.GetInstance(attr).GetOctets());
|
||||
if (!m_keys.ContainsKey(id) && !m_localIds.ContainsKey(id))
|
||||
continue; // ignore this one - it's not valid
|
||||
}
|
||||
|
||||
// OK, but the value has to be the same
|
||||
if (!attributeValue.Equals(attr))
|
||||
{
|
||||
throw new IOException("attempt to add existing attribute with different value");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
attributes[aOid] = attr;
|
||||
}
|
||||
|
||||
if (aOid.Equals(PkcsObjectIdentifiers.Pkcs9AtFriendlyName))
|
||||
{
|
||||
alias = ((DerBmpString)attr).GetString();
|
||||
}
|
||||
else if (aOid.Equals(PkcsObjectIdentifiers.Pkcs9AtLocalKeyID))
|
||||
{
|
||||
localId = (Asn1OctetString)attr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CertId certId = new CertId(cert.GetPublicKey());
|
||||
X509CertificateEntry certEntry = new X509CertificateEntry(cert, attributes);
|
||||
|
||||
m_chainCerts[certId] = certEntry;
|
||||
|
||||
if (unmarkedKeyEntry != null)
|
||||
{
|
||||
if (m_keyCerts.Count == 0)
|
||||
{
|
||||
string name = Hex.ToHexString(certId.Id);
|
||||
|
||||
m_keyCerts[name] = certEntry;
|
||||
m_keys[name] = unmarkedKeyEntry;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_keys["unmarked"] = unmarkedKeyEntry;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (localId != null)
|
||||
{
|
||||
string name = Hex.ToHexString(localId.GetOctets());
|
||||
|
||||
m_keyCerts[name] = certEntry;
|
||||
}
|
||||
|
||||
if (alias != null)
|
||||
{
|
||||
// TODO There may have been more than one alias
|
||||
m_certs[alias] = certEntry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public AsymmetricKeyEntry GetKey(string alias)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
|
||||
return CollectionUtilities.GetValueOrNull(m_keys, alias);
|
||||
}
|
||||
|
||||
public bool IsCertificateEntry(string alias)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
|
||||
return m_certs.ContainsKey(alias) && !m_keys.ContainsKey(alias);
|
||||
}
|
||||
|
||||
public bool IsKeyEntry(string alias)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
|
||||
return m_keys.ContainsKey(alias);
|
||||
}
|
||||
|
||||
public IEnumerable<string> Aliases
|
||||
{
|
||||
get
|
||||
{
|
||||
var aliases = new HashSet<string>(m_certs.Keys);
|
||||
aliases.UnionWith(m_keys.Keys);
|
||||
return CollectionUtilities.Proxy(aliases);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ContainsAlias(string alias)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
|
||||
return m_certs.ContainsKey(alias) || m_keys.ContainsKey(alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* simply return the cert entry for the private key
|
||||
*/
|
||||
public X509CertificateEntry GetCertificate(string alias)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
|
||||
if (m_certs.TryGetValue(alias, out var cert))
|
||||
return cert;
|
||||
|
||||
var keyCertKey = alias;
|
||||
if (m_localIds.TryGetValue(alias, out var localId))
|
||||
{
|
||||
keyCertKey = localId;
|
||||
}
|
||||
|
||||
return CollectionUtilities.GetValueOrNull(m_keyCerts, keyCertKey);
|
||||
}
|
||||
|
||||
public string GetCertificateAlias(X509Certificate cert)
|
||||
{
|
||||
if (cert == null)
|
||||
throw new ArgumentNullException(nameof(cert));
|
||||
|
||||
foreach (var entry in m_certs)
|
||||
{
|
||||
if (entry.Value.Certificate.Equals(cert))
|
||||
return entry.Key;
|
||||
}
|
||||
|
||||
foreach (var entry in m_keyCerts)
|
||||
{
|
||||
if (entry.Value.Certificate.Equals(cert))
|
||||
return entry.Key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public X509CertificateEntry[] GetCertificateChain(string alias)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
|
||||
if (!IsKeyEntry(alias))
|
||||
return null;
|
||||
|
||||
X509CertificateEntry c = GetCertificate(alias);
|
||||
if (c == null)
|
||||
return null;
|
||||
|
||||
var cs = new List<X509CertificateEntry>();
|
||||
|
||||
while (c != null)
|
||||
{
|
||||
X509Certificate x509c = c.Certificate;
|
||||
X509CertificateEntry nextC = null;
|
||||
|
||||
Asn1OctetString akiValue = x509c.GetExtensionValue(X509Extensions.AuthorityKeyIdentifier);
|
||||
if (akiValue != null)
|
||||
{
|
||||
AuthorityKeyIdentifier aki = AuthorityKeyIdentifier.GetInstance(akiValue.GetOctets());
|
||||
|
||||
byte[] keyID = aki.GetKeyIdentifier();
|
||||
if (keyID != null)
|
||||
{
|
||||
nextC = CollectionUtilities.GetValueOrNull(m_chainCerts, new CertId(keyID));
|
||||
}
|
||||
}
|
||||
|
||||
if (nextC == null)
|
||||
{
|
||||
//
|
||||
// no authority key id, try the Issuer DN
|
||||
//
|
||||
X509Name i = x509c.IssuerDN;
|
||||
X509Name s = x509c.SubjectDN;
|
||||
|
||||
if (!i.Equivalent(s))
|
||||
{
|
||||
foreach (var entry in m_chainCerts)
|
||||
{
|
||||
X509Certificate cert = entry.Value.Certificate;
|
||||
|
||||
if (cert.SubjectDN.Equivalent(i))
|
||||
{
|
||||
try
|
||||
{
|
||||
x509c.Verify(cert.GetPublicKey());
|
||||
|
||||
nextC = entry.Value;
|
||||
break;
|
||||
}
|
||||
catch (InvalidKeyException)
|
||||
{
|
||||
// TODO What if it doesn't verify?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cs.Add(c);
|
||||
if (nextC != c) // self signed - end of the chain
|
||||
{
|
||||
c = nextC;
|
||||
}
|
||||
else
|
||||
{
|
||||
c = null;
|
||||
}
|
||||
}
|
||||
|
||||
return cs.ToArray();
|
||||
}
|
||||
|
||||
public void SetCertificateEntry(string alias, X509CertificateEntry certEntry)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
if (certEntry == null)
|
||||
throw new ArgumentNullException(nameof(certEntry));
|
||||
if (m_keys.ContainsKey(alias))
|
||||
throw new ArgumentException("There is a key entry with the name " + alias + ".");
|
||||
|
||||
m_certs[alias] = certEntry;
|
||||
m_chainCerts[new CertId(certEntry.Certificate.GetPublicKey())] = certEntry;
|
||||
}
|
||||
|
||||
public void SetKeyEntry(string alias, AsymmetricKeyEntry keyEntry, X509CertificateEntry[] chain)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
if (keyEntry == null)
|
||||
throw new ArgumentNullException(nameof(keyEntry));
|
||||
if (keyEntry.Key.IsPrivate && chain == null)
|
||||
throw new ArgumentException("No certificate chain for private key");
|
||||
|
||||
if (m_keys.ContainsKey(alias))
|
||||
{
|
||||
DeleteEntry(alias);
|
||||
}
|
||||
|
||||
m_keys[alias] = keyEntry;
|
||||
m_certs[alias] = chain[0];
|
||||
|
||||
for (int i = 0; i != chain.Length; i++)
|
||||
{
|
||||
m_chainCerts[new CertId(chain[i].Certificate.GetPublicKey())] = chain[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteEntry(string alias)
|
||||
{
|
||||
if (alias == null)
|
||||
throw new ArgumentNullException(nameof(alias));
|
||||
|
||||
if (CollectionUtilities.Remove(m_certs, alias, out var cert))
|
||||
{
|
||||
m_chainCerts.Remove(new CertId(cert.Certificate.GetPublicKey()));
|
||||
}
|
||||
|
||||
if (m_keys.Remove(alias))
|
||||
{
|
||||
if (CollectionUtilities.Remove(m_localIds, alias, out var id))
|
||||
{
|
||||
if (CollectionUtilities.Remove(m_keyCerts, id, out var keyCert))
|
||||
{
|
||||
m_chainCerts.Remove(new CertId(keyCert.Certificate.GetPublicKey()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsEntryOfType(string alias, Type entryType)
|
||||
{
|
||||
if (entryType == typeof(X509CertificateEntry))
|
||||
return IsCertificateEntry(alias);
|
||||
|
||||
if (entryType == typeof(AsymmetricKeyEntry))
|
||||
return IsKeyEntry(alias) && GetCertificate(alias) != null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = m_certs.Count;
|
||||
|
||||
foreach (var key in m_keys.Keys)
|
||||
{
|
||||
if (!m_certs.ContainsKey(key))
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public void Save(Stream stream, char[] password, SecureRandom random)
|
||||
{
|
||||
if (stream == null)
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
if (random == null)
|
||||
throw new ArgumentNullException(nameof(random));
|
||||
|
||||
//
|
||||
// handle the keys
|
||||
//
|
||||
Asn1EncodableVector keyBags = new Asn1EncodableVector();
|
||||
foreach (var keyEntry in m_keys)
|
||||
{
|
||||
var name = keyEntry.Key;
|
||||
var privKey = keyEntry.Value;
|
||||
|
||||
byte[] kSalt = new byte[SaltSize];
|
||||
random.NextBytes(kSalt);
|
||||
|
||||
DerObjectIdentifier bagOid;
|
||||
Asn1Encodable bagData;
|
||||
|
||||
if (password == null)
|
||||
{
|
||||
bagOid = PkcsObjectIdentifiers.KeyBag;
|
||||
bagData = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privKey.Key);
|
||||
}
|
||||
else
|
||||
{
|
||||
bagOid = PkcsObjectIdentifiers.Pkcs8ShroudedKeyBag;
|
||||
if (keyPrfAlgorithm != null)
|
||||
{
|
||||
bagData = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(keyAlgorithm,
|
||||
keyPrfAlgorithm, password, kSalt, MinIterations, random, privKey.Key);
|
||||
}
|
||||
else
|
||||
{
|
||||
bagData = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(keyAlgorithm, password,
|
||||
kSalt, MinIterations, privKey.Key);
|
||||
}
|
||||
}
|
||||
|
||||
Asn1EncodableVector kName = new Asn1EncodableVector();
|
||||
|
||||
foreach (var oid in privKey.BagAttributeKeys)
|
||||
{
|
||||
// NB: Ignore any existing FriendlyName
|
||||
if (!PkcsObjectIdentifiers.Pkcs9AtFriendlyName.Equals(oid))
|
||||
{
|
||||
kName.Add(new DerSequence(oid, new DerSet(privKey[oid])));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// make sure we are using the local alias on store
|
||||
//
|
||||
// NB: We always set the FriendlyName based on 'name'
|
||||
//if (privKey[PkcsObjectIdentifiers.Pkcs9AtFriendlyName] == null)
|
||||
{
|
||||
kName.Add(
|
||||
new DerSequence(
|
||||
PkcsObjectIdentifiers.Pkcs9AtFriendlyName,
|
||||
new DerSet(new DerBmpString(name))));
|
||||
}
|
||||
|
||||
//
|
||||
// make sure we have a local key-id
|
||||
//
|
||||
if (privKey[PkcsObjectIdentifiers.Pkcs9AtLocalKeyID] == null)
|
||||
{
|
||||
X509CertificateEntry ct = GetCertificate(name);
|
||||
AsymmetricKeyParameter pubKey = ct.Certificate.GetPublicKey();
|
||||
SubjectKeyIdentifier subjectKeyID = CreateSubjectKeyID(pubKey);
|
||||
|
||||
kName.Add(
|
||||
new DerSequence(
|
||||
PkcsObjectIdentifiers.Pkcs9AtLocalKeyID,
|
||||
new DerSet(subjectKeyID)));
|
||||
}
|
||||
|
||||
keyBags.Add(new SafeBag(bagOid, bagData.ToAsn1Object(), new DerSet(kName)));
|
||||
}
|
||||
|
||||
byte[] keyBagsEncoding = new DerSequence(keyBags).GetDerEncoded();
|
||||
ContentInfo keysInfo = new ContentInfo(PkcsObjectIdentifiers.Data, new BerOctetString(keyBagsEncoding));
|
||||
|
||||
//
|
||||
// certificate processing
|
||||
//
|
||||
byte[] cSalt = new byte[SaltSize];
|
||||
|
||||
random.NextBytes(cSalt);
|
||||
|
||||
Asn1EncodableVector certBags = new Asn1EncodableVector();
|
||||
Pkcs12PbeParams cParams = new Pkcs12PbeParams(cSalt, MinIterations);
|
||||
AlgorithmIdentifier cAlgId = new AlgorithmIdentifier(certAlgorithm, cParams.ToAsn1Object());
|
||||
var doneCerts = new HashSet<X509Certificate>();
|
||||
|
||||
foreach (string name in m_keys.Keys)
|
||||
{
|
||||
X509CertificateEntry certEntry = GetCertificate(name);
|
||||
CertBag cBag = new CertBag(
|
||||
PkcsObjectIdentifiers.X509Certificate,
|
||||
new DerOctetString(certEntry.Certificate.GetEncoded()));
|
||||
|
||||
Asn1EncodableVector fName = new Asn1EncodableVector();
|
||||
|
||||
foreach (var oid in certEntry.BagAttributeKeys)
|
||||
{
|
||||
// NB: Ignore any existing FriendlyName
|
||||
if (!PkcsObjectIdentifiers.Pkcs9AtFriendlyName.Equals(oid))
|
||||
{
|
||||
fName.Add(new DerSequence(oid, new DerSet(certEntry[oid])));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// make sure we are using the local alias on store
|
||||
//
|
||||
// NB: We always set the FriendlyName based on 'name'
|
||||
//if (certEntry[PkcsObjectIdentifiers.Pkcs9AtFriendlyName] == null)
|
||||
{
|
||||
fName.Add(
|
||||
new DerSequence(
|
||||
PkcsObjectIdentifiers.Pkcs9AtFriendlyName,
|
||||
new DerSet(new DerBmpString(name))));
|
||||
}
|
||||
|
||||
//
|
||||
// make sure we have a local key-id
|
||||
//
|
||||
if (certEntry[PkcsObjectIdentifiers.Pkcs9AtLocalKeyID] == null)
|
||||
{
|
||||
AsymmetricKeyParameter pubKey = certEntry.Certificate.GetPublicKey();
|
||||
SubjectKeyIdentifier subjectKeyID = CreateSubjectKeyID(pubKey);
|
||||
|
||||
fName.Add(
|
||||
new DerSequence(
|
||||
PkcsObjectIdentifiers.Pkcs9AtLocalKeyID,
|
||||
new DerSet(subjectKeyID)));
|
||||
}
|
||||
|
||||
certBags.Add(new SafeBag(PkcsObjectIdentifiers.CertBag, cBag.ToAsn1Object(), new DerSet(fName)));
|
||||
|
||||
doneCerts.Add(certEntry.Certificate);
|
||||
}
|
||||
|
||||
foreach (var certEntry in m_certs)
|
||||
{
|
||||
var certId = certEntry.Key;
|
||||
var cert = certEntry.Value;
|
||||
|
||||
if (m_keys.ContainsKey(certId))
|
||||
continue;
|
||||
|
||||
CertBag cBag = new CertBag(
|
||||
PkcsObjectIdentifiers.X509Certificate,
|
||||
new DerOctetString(cert.Certificate.GetEncoded()));
|
||||
|
||||
Asn1EncodableVector fName = new Asn1EncodableVector();
|
||||
|
||||
foreach (var oid in cert.BagAttributeKeys)
|
||||
{
|
||||
// a certificate not immediately linked to a key doesn't require
|
||||
// a localKeyID and will confuse some PKCS12 implementations.
|
||||
//
|
||||
// If we find one, we'll prune it out.
|
||||
if (PkcsObjectIdentifiers.Pkcs9AtLocalKeyID.Equals(oid))
|
||||
continue;
|
||||
|
||||
// NB: Ignore any existing FriendlyName
|
||||
if (!PkcsObjectIdentifiers.Pkcs9AtFriendlyName.Equals(oid))
|
||||
{
|
||||
fName.Add(new DerSequence(oid, new DerSet(cert[oid])));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// make sure we are using the local alias on store
|
||||
//
|
||||
// NB: We always set the FriendlyName based on 'certId'
|
||||
//if (cert[PkcsObjectIdentifiers.Pkcs9AtFriendlyName] == null)
|
||||
{
|
||||
fName.Add(
|
||||
new DerSequence(
|
||||
PkcsObjectIdentifiers.Pkcs9AtFriendlyName,
|
||||
new DerSet(new DerBmpString(certId))));
|
||||
}
|
||||
|
||||
// the Oracle PKCS12 parser looks for a trusted key usage for named certificates as well
|
||||
if (cert[MiscObjectIdentifiers.id_oracle_pkcs12_trusted_key_usage] == null)
|
||||
{
|
||||
Asn1OctetString ext = cert.Certificate.GetExtensionValue(X509Extensions.ExtendedKeyUsage);
|
||||
|
||||
if (ext != null)
|
||||
{
|
||||
ExtendedKeyUsage usage = ExtendedKeyUsage.GetInstance(ext.GetOctets());
|
||||
Asn1EncodableVector v = new Asn1EncodableVector();
|
||||
IList<DerObjectIdentifier> usages = usage.GetAllUsages();
|
||||
for (int i = 0; i != usages.Count; i++)
|
||||
{
|
||||
v.Add(usages[i]);
|
||||
}
|
||||
|
||||
fName.Add(
|
||||
new DerSequence(
|
||||
MiscObjectIdentifiers.id_oracle_pkcs12_trusted_key_usage,
|
||||
new DerSet(v)));
|
||||
}
|
||||
else
|
||||
{
|
||||
fName.Add(
|
||||
new DerSequence(
|
||||
MiscObjectIdentifiers.id_oracle_pkcs12_trusted_key_usage,
|
||||
new DerSet(KeyPurposeID.AnyExtendedKeyUsage)));
|
||||
}
|
||||
}
|
||||
|
||||
certBags.Add(new SafeBag(PkcsObjectIdentifiers.CertBag, cBag.ToAsn1Object(), new DerSet(fName)));
|
||||
|
||||
doneCerts.Add(cert.Certificate);
|
||||
}
|
||||
|
||||
foreach (var chainCertEntry in m_chainCerts)
|
||||
{
|
||||
var certId = chainCertEntry.Key;
|
||||
var cert = chainCertEntry.Value;
|
||||
|
||||
if (doneCerts.Contains(cert.Certificate))
|
||||
continue;
|
||||
|
||||
CertBag cBag = new CertBag(
|
||||
PkcsObjectIdentifiers.X509Certificate,
|
||||
new DerOctetString(cert.Certificate.GetEncoded()));
|
||||
|
||||
Asn1EncodableVector fName = new Asn1EncodableVector();
|
||||
|
||||
foreach (var oid in cert.BagAttributeKeys)
|
||||
{
|
||||
// a certificate not immediately linked to a key doesn't require
|
||||
// a localKeyID and will confuse some PKCS12 implementations.
|
||||
//
|
||||
// If we find one, we'll prune it out.
|
||||
if (PkcsObjectIdentifiers.Pkcs9AtLocalKeyID.Equals(oid))
|
||||
continue;
|
||||
|
||||
fName.Add(new DerSequence(oid, new DerSet(cert[oid])));
|
||||
}
|
||||
|
||||
certBags.Add(new SafeBag(PkcsObjectIdentifiers.CertBag, cBag.ToAsn1Object(), new DerSet(fName)));
|
||||
}
|
||||
|
||||
byte[] certBagsEncoding = new DerSequence(certBags).GetDerEncoded();
|
||||
|
||||
ContentInfo certsInfo;
|
||||
if (password == null || certAlgorithm == null)
|
||||
{
|
||||
certsInfo = new ContentInfo(PkcsObjectIdentifiers.Data, new BerOctetString(certBagsEncoding));
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] certBytes = CryptPbeData(true, cAlgId, password, false, certBagsEncoding);
|
||||
EncryptedData cInfo = new EncryptedData(PkcsObjectIdentifiers.Data, cAlgId, new BerOctetString(certBytes));
|
||||
certsInfo = new ContentInfo(PkcsObjectIdentifiers.EncryptedData, cInfo.ToAsn1Object());
|
||||
}
|
||||
|
||||
ContentInfo[] info = new ContentInfo[]{ keysInfo, certsInfo };
|
||||
|
||||
byte[] data = new AuthenticatedSafe(info).GetEncoded(
|
||||
useDerEncoding ? Asn1Encodable.Der : Asn1Encodable.Ber);
|
||||
|
||||
ContentInfo mainInfo = new ContentInfo(PkcsObjectIdentifiers.Data, new BerOctetString(data));
|
||||
|
||||
//
|
||||
// create the mac
|
||||
//
|
||||
MacData macData = null;
|
||||
if (password != null)
|
||||
{
|
||||
byte[] mSalt = new byte[20];
|
||||
random.NextBytes(mSalt);
|
||||
|
||||
byte[] mac = CalculatePbeMac(OiwObjectIdentifiers.IdSha1,
|
||||
mSalt, MinIterations, password, false, data);
|
||||
|
||||
AlgorithmIdentifier algId = new AlgorithmIdentifier(
|
||||
OiwObjectIdentifiers.IdSha1, DerNull.Instance);
|
||||
DigestInfo dInfo = new DigestInfo(algId, mac);
|
||||
|
||||
macData = new MacData(dInfo, mSalt, MinIterations);
|
||||
}
|
||||
|
||||
//
|
||||
// output the Pfx
|
||||
//
|
||||
Pfx pfx = new Pfx(mainInfo, macData);
|
||||
|
||||
pfx.EncodeTo(stream, useDerEncoding ? Asn1Encodable.Der : Asn1Encodable.Ber);
|
||||
}
|
||||
|
||||
internal static byte[] CalculatePbeMac(
|
||||
DerObjectIdentifier oid,
|
||||
byte[] salt,
|
||||
int itCount,
|
||||
char[] password,
|
||||
bool wrongPkcs12Zero,
|
||||
byte[] data)
|
||||
{
|
||||
Asn1Encodable asn1Params = PbeUtilities.GenerateAlgorithmParameters(
|
||||
oid, salt, itCount);
|
||||
ICipherParameters cipherParams = PbeUtilities.GenerateCipherParameters(
|
||||
oid, password, wrongPkcs12Zero, asn1Params);
|
||||
|
||||
IMac mac = (IMac) PbeUtilities.CreateEngine(oid);
|
||||
mac.Init(cipherParams);
|
||||
return MacUtilities.DoFinal(mac, data);
|
||||
}
|
||||
|
||||
private static byte[] CryptPbeData(
|
||||
bool forEncryption,
|
||||
AlgorithmIdentifier algId,
|
||||
char[] password,
|
||||
bool wrongPkcs12Zero,
|
||||
byte[] data)
|
||||
{
|
||||
IBufferedCipher cipher = PbeUtilities.CreateEngine(algId) as IBufferedCipher;
|
||||
|
||||
if (cipher == null)
|
||||
throw new Exception("Unknown encryption algorithm: " + algId.Algorithm);
|
||||
|
||||
if (algId.Algorithm.Equals(PkcsObjectIdentifiers.IdPbeS2))
|
||||
{
|
||||
PbeS2Parameters pbeParameters = PbeS2Parameters.GetInstance(algId.Parameters);
|
||||
ICipherParameters cipherParams = PbeUtilities.GenerateCipherParameters(
|
||||
algId.Algorithm, password, pbeParameters);
|
||||
cipher.Init(forEncryption, cipherParams);
|
||||
return cipher.DoFinal(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
Pkcs12PbeParams pbeParameters = Pkcs12PbeParams.GetInstance(algId.Parameters);
|
||||
ICipherParameters cipherParams = PbeUtilities.GenerateCipherParameters(
|
||||
algId.Algorithm, password, wrongPkcs12Zero, pbeParameters);
|
||||
cipher.Init(forEncryption, cipherParams);
|
||||
return cipher.DoFinal(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 168dce1c98ed30a41b666da9816a25c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,81 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 351095fcda1b7be4fa4bc49f38ce24d7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,112 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.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()));
|
||||
|
||||
byte[] data;
|
||||
using (var strm = encIn.Stream)
|
||||
{
|
||||
data = Streams.ReadAll(encIn.Stream);
|
||||
}
|
||||
|
||||
return PrivateKeyInfo.GetInstance(data);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new PkcsException("unable to read encrypted data: " + e.Message, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49d3fa588f57c834d96ecd44ba6bad73
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,57 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.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();
|
||||
|
||||
using (var str = cOut.Stream)
|
||||
{
|
||||
str.Write(keyData, 0, keyData.Length);
|
||||
}
|
||||
|
||||
return new Pkcs8EncryptedPrivateKeyInfo(
|
||||
new EncryptedPrivateKeyInfo((AlgorithmIdentifier)encryptor.AlgorithmDetails, bOut.ToArray()));
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
throw new InvalidOperationException("cannot encode privateKeyInfo");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c340fab1cfc715642815622a28156614
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,35 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
/// <summary>Base exception for PKCS related issues.</summary>
|
||||
[Serializable]
|
||||
public class PkcsException
|
||||
: Exception
|
||||
{
|
||||
public PkcsException()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
public PkcsException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public PkcsException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected PkcsException(SerializationInfo info, StreamingContext context)
|
||||
: base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3c07dfe3ae380e44b0c278374a5f90b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,36 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
/// <summary>Base exception for parsing related issues in the PKCS namespace.</summary>
|
||||
[Serializable]
|
||||
public class PkcsIOException
|
||||
: IOException
|
||||
{
|
||||
public PkcsIOException()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
public PkcsIOException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public PkcsIOException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected PkcsIOException(SerializationInfo info, StreamingContext context)
|
||||
: base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a9c22099cc169f41866f0ed58031fd1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,292 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.EdEC;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public static class 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 new NotImplementedException("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, new X9ECPoint(dp.G, false), 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 new NotImplementedException("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: " + Org.BouncyCastle.Utilities.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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da0b98ec65ab9bd4bae18ff7fbf50ed5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,49 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System.Collections.Generic;
|
||||
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
|
||||
|
||||
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
|
||||
{
|
||||
public class X509CertificateEntry
|
||||
: Pkcs12Entry
|
||||
{
|
||||
private readonly X509Certificate cert;
|
||||
|
||||
public X509CertificateEntry(X509Certificate cert)
|
||||
: base(new Dictionary<DerObjectIdentifier, Asn1Encodable>())
|
||||
{
|
||||
this.cert = cert;
|
||||
}
|
||||
|
||||
public X509CertificateEntry(X509Certificate cert, IDictionary<DerObjectIdentifier, Asn1Encodable> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0cf80bddefc53c47951df6aac435e62
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user