提交Unity 联机Pro

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-17 14:27:18 +08:00
parent f00193b000
commit 894100ae37
7448 changed files with 854473 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
#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.Security
{
[Serializable]
public class EncryptionException
: IOException
{
public EncryptionException()
: base()
{
}
public EncryptionException(string message)
: base(message)
{
}
public EncryptionException(string message, Exception innerException)
: base(message, innerException)
{
}
protected EncryptionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4a424bf92daf4a94da4bbe8b216162ed
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,13 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl
{
public interface IPasswordFinder
{
char[] GetPassword();
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dd93d5ed8706db84582edeff6a96a3d6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,274 @@
#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.Pkcs;
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.Parameters;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem;
using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl
{
/**
* PEM generator for the original set of PEM objects used in Open SSL.
*/
public class MiscPemGenerator
: PemObjectGenerator
{
private readonly object obj;
private readonly string algorithm;
private readonly char[] password;
private readonly SecureRandom random;
public MiscPemGenerator(object obj)
{
this.obj = obj;
}
public MiscPemGenerator(
object obj,
string algorithm,
char[] password,
SecureRandom random)
{
this.obj = obj;
this.algorithm = algorithm;
this.password = password;
this.random = random;
}
private static PemObject CreatePemObject(object obj)
{
if (obj == null)
throw new ArgumentNullException("obj");
if (obj is AsymmetricCipherKeyPair keyPair)
{
return CreatePemObject(keyPair.Private);
}
string type;
byte[] encoding;
if (obj is PemObject pemObject)
return pemObject;
if (obj is PemObjectGenerator pemObjectGenerator)
return pemObjectGenerator.Generate();
if (obj is X509Certificate certificate)
{
// TODO Should we prefer "X509 CERTIFICATE" here?
type = "CERTIFICATE";
try
{
encoding = certificate.GetEncoded();
}
catch (CertificateEncodingException e)
{
throw new IOException("Cannot Encode object: " + e.ToString());
}
}
else if (obj is X509Crl crl)
{
type = "X509 CRL";
try
{
encoding = crl.GetEncoded();
}
catch (CrlException e)
{
throw new IOException("Cannot Encode object: " + e.ToString());
}
}
else if (obj is AsymmetricKeyParameter akp)
{
if (akp.IsPrivate)
{
encoding = EncodePrivateKey(akp, out type);
}
else
{
type = "PUBLIC KEY";
encoding = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(akp).GetDerEncoded();
}
}
else if (obj is X509V2AttributeCertificate attrCert)
{
type = "ATTRIBUTE CERTIFICATE";
encoding = attrCert.GetEncoded();
}
else if (obj is Pkcs10CertificationRequest certReq)
{
type = "CERTIFICATE REQUEST";
encoding = certReq.GetEncoded();
}
else if (obj is Asn1.Cms.ContentInfo contentInfo)
{
type = "PKCS7";
encoding = contentInfo.GetEncoded();
}
else
{
throw new PemGenerationException("Object type not supported: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
}
return new PemObject(type, encoding);
}
// private string GetHexEncoded(byte[] bytes)
// {
// bytes = Hex.Encode(bytes);
//
// char[] chars = new char[bytes.Length];
//
// for (int i = 0; i != bytes.Length; i++)
// {
// chars[i] = (char)bytes[i];
// }
//
// return new string(chars);
// }
private static PemObject CreatePemObject(
object obj,
string algorithm,
char[] password,
SecureRandom random)
{
if (obj == null)
throw new ArgumentNullException("obj");
if (algorithm == null)
throw new ArgumentNullException("algorithm");
if (password == null)
throw new ArgumentNullException("password");
if (random == null)
throw new ArgumentNullException("random");
if (obj is AsymmetricCipherKeyPair keyPair)
{
return CreatePemObject(keyPair.Private, algorithm, password, random);
}
string type = null;
byte[] keyData = null;
if (obj is AsymmetricKeyParameter akp)
{
if (akp.IsPrivate)
{
keyData = EncodePrivateKey(akp, out type);
}
}
if (type == null || keyData == null)
{
// TODO Support other types?
throw new PemGenerationException("Object type not supported: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
}
string dekAlgName = algorithm.ToUpperInvariant();
// Note: For backward compatibility
if (dekAlgName == "DESEDE")
{
dekAlgName = "DES-EDE3-CBC";
}
int ivLength = Org.BouncyCastle.Utilities.Platform.StartsWith(dekAlgName, "AES-") ? 16 : 8;
byte[] iv = new byte[ivLength];
random.NextBytes(iv);
byte[] encData = PemUtilities.Crypt(true, keyData, password, dekAlgName, iv);
var headers = new List<PemHeader>(2);
headers.Add(new PemHeader("Proc-Type", "4,ENCRYPTED"));
headers.Add(new PemHeader("DEK-Info", dekAlgName + "," + Hex.ToHexString(iv).ToUpperInvariant()));
return new PemObject(type, headers, encData);
}
private static byte[] EncodePrivateKey(
AsymmetricKeyParameter akp,
out string keyType)
{
PrivateKeyInfo info = PrivateKeyInfoFactory.CreatePrivateKeyInfo(akp);
AlgorithmIdentifier algID = info.PrivateKeyAlgorithm;
DerObjectIdentifier oid = algID.Algorithm;
if (oid.Equals(X9ObjectIdentifiers.IdDsa))
{
keyType = "DSA PRIVATE KEY";
DsaParameter p = DsaParameter.GetInstance(algID.Parameters);
BigInteger x = ((DsaPrivateKeyParameters) akp).X;
BigInteger y = p.G.ModPow(x, p.P);
// TODO Create an ASN1 object somewhere for this?
return new DerSequence(
new DerInteger(0),
new DerInteger(p.P),
new DerInteger(p.Q),
new DerInteger(p.G),
new DerInteger(y),
new DerInteger(x)).GetEncoded();
}
if (oid.Equals(PkcsObjectIdentifiers.RsaEncryption))
{
keyType = "RSA PRIVATE KEY";
return info.ParsePrivateKey().GetEncoded();
}
else if (oid.Equals(CryptoProObjectIdentifiers.GostR3410x2001)
|| oid.Equals(X9ObjectIdentifiers.IdECPublicKey))
{
keyType = "EC PRIVATE KEY";
return info.ParsePrivateKey().GetEncoded();
}
else
{
keyType = "PRIVATE KEY";
return info.GetEncoded();
}
}
public PemObject Generate()
{
try
{
if (algorithm != null)
{
return CreatePemObject(obj, algorithm, password, random);
}
return CreatePemObject(obj);
}
catch (IOException e)
{
throw new PemGenerationException("encoding exception", e);
}
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c0d8a624220f0fc41b80f7f2549e984a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,35 @@
#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.OpenSsl
{
[Serializable]
public class PemException
: IOException
{
public PemException()
: base()
{
}
public PemException(string message)
: base(message)
{
}
public PemException(string message, Exception innerException)
: base(message, innerException)
{
}
protected PemException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 02930c4db3958684397dcfe7708b464b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,393 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
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.Pkcs;
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.Utilities.IO.Pem;
using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl
{
/**
* Class for reading OpenSSL PEM encoded streams containing
* X509 certificates, PKCS8 encoded keys and PKCS7 objects.
* <p>
* In the case of PKCS7 objects the reader will return a CMS ContentInfo object. Keys and
* Certificates will be returned using the appropriate java.security type.</p>
*/
public class PemReader
: Utilities.IO.Pem.PemReader
{
//private static readonly Dictionary<string, PemObjectParser> Parsers = new Dictionary<string, PemObjectParser>();
static PemReader()
{
// Parsers.Add("CERTIFICATE REQUEST", new PKCS10CertificationRequestParser());
// Parsers.Add("NEW CERTIFICATE REQUEST", new PKCS10CertificationRequestParser());
// Parsers.Add("CERTIFICATE", new X509CertificateParser(provider));
// Parsers.Add("X509 CERTIFICATE", new X509CertificateParser(provider));
// Parsers.Add("X509 CRL", new X509CRLParser(provider));
// Parsers.Add("PKCS7", new PKCS7Parser());
// Parsers.Add("ATTRIBUTE CERTIFICATE", new X509AttributeCertificateParser());
// Parsers.Add("EC PARAMETERS", new ECNamedCurveSpecParser());
// Parsers.Add("PUBLIC KEY", new PublicKeyParser(provider));
// Parsers.Add("RSA PUBLIC KEY", new RSAPublicKeyParser(provider));
// Parsers.Add("RSA PRIVATE KEY", new RSAKeyPairParser(provider));
// Parsers.Add("DSA PRIVATE KEY", new DSAKeyPairParser(provider));
// Parsers.Add("EC PRIVATE KEY", new ECDSAKeyPairParser(provider));
// Parsers.Add("ENCRYPTED PRIVATE KEY", new EncryptedPrivateKeyParser(provider));
// Parsers.Add("PRIVATE KEY", new PrivateKeyParser(provider));
}
private readonly IPasswordFinder pFinder;
/**
* Create a new PemReader
*
* @param reader the Reader
*/
public PemReader(
TextReader reader)
: this(reader, null)
{
}
/**
* Create a new PemReader with a password finder
*
* @param reader the Reader
* @param pFinder the password finder
*/
public PemReader(
TextReader reader,
IPasswordFinder pFinder)
: base(reader)
{
this.pFinder = pFinder;
}
public object ReadObject()
{
PemObject obj = ReadPemObject();
if (obj == null)
return null;
// TODO Follow Java build and map to parser objects?
// if (parsers.Contains(obj.Type))
// return ((PemObjectParser)parsers[obj.Type]).ParseObject(obj);
if (Org.BouncyCastle.Utilities.Platform.EndsWith(obj.Type, "PRIVATE KEY"))
return ReadPrivateKey(obj);
switch (obj.Type)
{
case "PUBLIC KEY":
return ReadPublicKey(obj);
case "RSA PUBLIC KEY":
return ReadRsaPublicKey(obj);
case "CERTIFICATE REQUEST":
case "NEW CERTIFICATE REQUEST":
return ReadCertificateRequest(obj);
case "CERTIFICATE":
case "X509 CERTIFICATE":
return ReadCertificate(obj);
case "PKCS7":
case "CMS":
return ReadPkcs7(obj);
case "X509 CRL":
return ReadCrl(obj);
case "ATTRIBUTE CERTIFICATE":
return ReadAttributeCertificate(obj);
// TODO Add back in when tests done, and return type issue resolved
//case "EC PARAMETERS":
// return ReadECParameters(obj);
default:
throw new IOException("unrecognised object: " + obj.Type);
}
}
private AsymmetricKeyParameter ReadRsaPublicKey(PemObject pemObject)
{
RsaPublicKeyStructure rsaPubStructure = RsaPublicKeyStructure.GetInstance(
Asn1Object.FromByteArray(pemObject.Content));
return new RsaKeyParameters(
false, // not private
rsaPubStructure.Modulus,
rsaPubStructure.PublicExponent);
}
private AsymmetricKeyParameter ReadPublicKey(PemObject pemObject)
{
return PublicKeyFactory.CreateKey(pemObject.Content);
}
/**
* Reads in a X509Certificate.
*
* @return the X509Certificate
* @throws IOException if an I/O error occured
*/
private X509Certificate ReadCertificate(PemObject pemObject)
{
try
{
return new X509CertificateParser().ReadCertificate(pemObject.Content);
}
catch (Exception e)
{
throw new PemException("problem parsing cert: " + e.ToString());
}
}
/**
* Reads in a X509CRL.
*
* @return the X509Certificate
* @throws IOException if an I/O error occured
*/
private X509Crl ReadCrl(PemObject pemObject)
{
try
{
return new X509CrlParser().ReadCrl(pemObject.Content);
}
catch (Exception e)
{
throw new PemException("problem parsing cert: " + e.ToString());
}
}
/**
* Reads in a PKCS10 certification request.
*
* @return the certificate request.
* @throws IOException if an I/O error occured
*/
private Pkcs10CertificationRequest ReadCertificateRequest(PemObject pemObject)
{
try
{
return new Pkcs10CertificationRequest(pemObject.Content);
}
catch (Exception e)
{
throw new PemException("problem parsing cert: " + e.ToString());
}
}
/**
* Reads in a X509 Attribute Certificate.
*
* @return the X509 Attribute Certificate
* @throws IOException if an I/O error occured
*/
private X509V2AttributeCertificate ReadAttributeCertificate(PemObject pemObject)
{
return new X509V2AttributeCertificate(pemObject.Content);
}
/**
* Reads in a PKCS7 object. This returns a ContentInfo object suitable for use with the CMS
* API.
*
* @return the X509Certificate
* @throws IOException if an I/O error occured
*/
// TODO Consider returning Asn1.Pkcs.ContentInfo
private Asn1.Cms.ContentInfo ReadPkcs7(PemObject pemObject)
{
try
{
return Asn1.Cms.ContentInfo.GetInstance(
Asn1Object.FromByteArray(pemObject.Content));
}
catch (Exception e)
{
throw new PemException("problem parsing PKCS7 object: " + e.ToString());
}
}
/**
* Read a Key Pair
*/
private object ReadPrivateKey(PemObject pemObject)
{
//
// extract the key
//
Debug.Assert(Org.BouncyCastle.Utilities.Platform.EndsWith(pemObject.Type, "PRIVATE KEY"));
string type = pemObject.Type.Substring(0, pemObject.Type.Length - "PRIVATE KEY".Length).Trim();
byte[] keyBytes = pemObject.Content;
var fields = new Dictionary<string, string>();
foreach (PemHeader header in pemObject.Headers)
{
fields[header.Name] = header.Value;
}
string procType = CollectionUtilities.GetValueOrNull(fields, "Proc-Type");
if (procType == "4,ENCRYPTED")
{
if (pFinder == null)
throw new PasswordException("No password finder specified, but a password is required");
char[] password = pFinder.GetPassword();
if (password == null)
throw new PasswordException("Password is null, but a password is required");
if (!fields.TryGetValue("DEK-Info", out var dekInfo))
throw new PemException("missing DEK-info");
string[] tknz = dekInfo.Split(',');
string dekAlgName = tknz[0].Trim();
byte[] iv = Hex.Decode(tknz[1].Trim());
keyBytes = PemUtilities.Crypt(false, keyBytes, password, dekAlgName, iv);
}
try
{
AsymmetricKeyParameter pubSpec, privSpec;
Asn1Sequence seq = Asn1Sequence.GetInstance(keyBytes);
switch (type)
{
case "RSA":
{
if (seq.Count != 9)
throw new PemException("malformed sequence in RSA private key");
RsaPrivateKeyStructure rsa = RsaPrivateKeyStructure.GetInstance(seq);
pubSpec = new RsaKeyParameters(false, rsa.Modulus, rsa.PublicExponent);
privSpec = new RsaPrivateCrtKeyParameters(
rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent,
rsa.Prime1, rsa.Prime2, rsa.Exponent1, rsa.Exponent2,
rsa.Coefficient);
break;
}
case "DSA":
{
if (seq.Count != 6)
throw new PemException("malformed sequence in DSA private key");
// TODO Create an ASN1 object somewhere for this?
//DerInteger v = (DerInteger)seq[0];
DerInteger p = (DerInteger)seq[1];
DerInteger q = (DerInteger)seq[2];
DerInteger g = (DerInteger)seq[3];
DerInteger y = (DerInteger)seq[4];
DerInteger x = (DerInteger)seq[5];
DsaParameters parameters = new DsaParameters(p.Value, q.Value, g.Value);
privSpec = new DsaPrivateKeyParameters(x.Value, parameters);
pubSpec = new DsaPublicKeyParameters(y.Value, parameters);
break;
}
case "EC":
{
ECPrivateKeyStructure pKey = ECPrivateKeyStructure.GetInstance(seq);
AlgorithmIdentifier algId = new AlgorithmIdentifier(
X9ObjectIdentifiers.IdECPublicKey, pKey.GetParameters());
PrivateKeyInfo privInfo = new PrivateKeyInfo(algId, pKey.ToAsn1Object());
// TODO Are the keys returned here ECDSA, as Java version forces?
privSpec = PrivateKeyFactory.CreateKey(privInfo);
DerBitString pubKey = pKey.GetPublicKey();
if (pubKey != null)
{
SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(algId, pubKey.GetBytes());
// TODO Are the keys returned here ECDSA, as Java version forces?
pubSpec = PublicKeyFactory.CreateKey(pubInfo);
}
else
{
pubSpec = ECKeyPairGenerator.GetCorrespondingPublicKey(
(ECPrivateKeyParameters)privSpec);
}
break;
}
case "ENCRYPTED":
{
char[] password = pFinder.GetPassword();
if (password == null)
throw new PasswordException("Password is null, but a password is required");
return PrivateKeyFactory.DecryptKey(password, EncryptedPrivateKeyInfo.GetInstance(seq));
}
case "":
{
return PrivateKeyFactory.CreateKey(PrivateKeyInfo.GetInstance(seq));
}
default:
throw new ArgumentException("Unknown key type: " + type, "type");
}
return new AsymmetricCipherKeyPair(pubSpec, privSpec);
}
catch (IOException e)
{
throw e;
}
catch (Exception e)
{
throw new PemException(
"problem creating " + type + " private key: " + e.ToString());
}
}
// TODO Add an equivalent class for ECNamedCurveParameterSpec?
//private ECNamedCurveParameterSpec ReadECParameters(
// private X9ECParameters ReadECParameters(PemObject pemObject)
// {
// DerObjectIdentifier oid = (DerObjectIdentifier)Asn1Object.FromByteArray(pemObject.Content);
//
// //return ECNamedCurveTable.getParameterSpec(oid.Id);
// return GetCurveParameters(oid.Id);
// }
private static X9ECParameters GetCurveParameters(string name)
{
X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByName(name);
if (ecP == null)
throw new Exception("unknown curve name: " + name);
return ecP;
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ea2749aedc54590409ca3ca57e7e3a00
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,162 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
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.Security;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl
{
internal sealed class PemUtilities
{
private enum PemBaseAlg { AES_128, AES_192, AES_256, BF, DES, DES_EDE, DES_EDE3, RC2, RC2_40, RC2_64 };
private enum PemMode { CBC, CFB, ECB, OFB };
static PemUtilities()
{
// Signal to obfuscation tools not to change enum constants
Enums.GetArbitraryValue<PemBaseAlg>().ToString();
Enums.GetArbitraryValue<PemMode>().ToString();
}
private static void ParseDekAlgName(
string dekAlgName,
out PemBaseAlg baseAlg,
out PemMode mode)
{
try
{
mode = PemMode.ECB;
if (dekAlgName == "DES-EDE" || dekAlgName == "DES-EDE3")
{
baseAlg = Enums.GetEnumValue<PemBaseAlg>(dekAlgName);
return;
}
int pos = dekAlgName.LastIndexOf('-');
if (pos >= 0)
{
baseAlg = Enums.GetEnumValue<PemBaseAlg>(dekAlgName.Substring(0, pos));
mode = Enums.GetEnumValue<PemMode>(dekAlgName.Substring(pos + 1));
return;
}
}
catch (ArgumentException)
{
}
throw new EncryptionException("Unknown DEK algorithm: " + dekAlgName);
}
internal static byte[] Crypt(
bool encrypt,
byte[] bytes,
char[] password,
string dekAlgName,
byte[] iv)
{
PemBaseAlg baseAlg;
PemMode mode;
ParseDekAlgName(dekAlgName, out baseAlg, out mode);
string padding;
switch (mode)
{
case PemMode.CBC:
case PemMode.ECB:
padding = "PKCS5Padding";
break;
case PemMode.CFB:
case PemMode.OFB:
padding = "NoPadding";
break;
default:
throw new EncryptionException("Unknown DEK algorithm: " + dekAlgName);
}
string algorithm;
byte[] salt = iv;
switch (baseAlg)
{
case PemBaseAlg.AES_128:
case PemBaseAlg.AES_192:
case PemBaseAlg.AES_256:
algorithm = "AES";
if (salt.Length > 8)
{
salt = new byte[8];
Array.Copy(iv, 0, salt, 0, salt.Length);
}
break;
case PemBaseAlg.BF:
algorithm = "BLOWFISH";
break;
case PemBaseAlg.DES:
algorithm = "DES";
break;
case PemBaseAlg.DES_EDE:
case PemBaseAlg.DES_EDE3:
algorithm = "DESede";
break;
case PemBaseAlg.RC2:
case PemBaseAlg.RC2_40:
case PemBaseAlg.RC2_64:
algorithm = "RC2";
break;
default:
throw new EncryptionException("Unknown DEK algorithm: " + dekAlgName);
}
string cipherName = algorithm + "/" + mode + "/" + padding;
IBufferedCipher cipher = CipherUtilities.GetCipher(cipherName);
ICipherParameters cParams = GetCipherParameters(password, baseAlg, salt);
if (mode != PemMode.ECB)
{
cParams = new ParametersWithIV(cParams, iv);
}
cipher.Init(encrypt, cParams);
return cipher.DoFinal(bytes);
}
private static ICipherParameters GetCipherParameters(
char[] password,
PemBaseAlg baseAlg,
byte[] salt)
{
string algorithm;
int keyBits;
switch (baseAlg)
{
case PemBaseAlg.AES_128: keyBits = 128; algorithm = "AES128"; break;
case PemBaseAlg.AES_192: keyBits = 192; algorithm = "AES192"; break;
case PemBaseAlg.AES_256: keyBits = 256; algorithm = "AES256"; break;
case PemBaseAlg.BF: keyBits = 128; algorithm = "BLOWFISH"; break;
case PemBaseAlg.DES: keyBits = 64; algorithm = "DES"; break;
case PemBaseAlg.DES_EDE: keyBits = 128; algorithm = "DESEDE"; break;
case PemBaseAlg.DES_EDE3: keyBits = 192; algorithm = "DESEDE3"; break;
case PemBaseAlg.RC2: keyBits = 128; algorithm = "RC2"; break;
case PemBaseAlg.RC2_40: keyBits = 40; algorithm = "RC2"; break;
case PemBaseAlg.RC2_64: keyBits = 64; algorithm = "RC2"; break;
default:
return null;
}
OpenSslPbeParametersGenerator pGen = new OpenSslPbeParametersGenerator();
pGen.Init(PbeParametersGenerator.Pkcs5PasswordToBytes(password), salt);
return pGen.GenerateDerivedParameters(algorithm, keyBits);
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bb8051867251512499844467793cdf9f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,46 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System.IO;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem;
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl
{
/// <remarks>General purpose writer for OpenSSL PEM objects.</remarks>
public class PemWriter
: Utilities.IO.Pem.PemWriter
{
/// <param name="writer">The TextWriter object to write the output to.</param>
public PemWriter(TextWriter writer)
: base(writer)
{
}
public void WriteObject(object obj)
{
try
{
base.WriteObject(new MiscPemGenerator(obj));
}
catch (PemGenerationException e)
{
if (e.InnerException is IOException inner)
throw inner;
throw e;
}
}
public void WriteObject(
object obj,
string algorithm,
char[] password,
SecureRandom random)
{
base.WriteObject(new MiscPemGenerator(obj, algorithm, password, random));
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 23f95c4b40043494bb615ca11764ba46
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,35 @@
#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.Security
{
[Serializable]
public class PasswordException
: IOException
{
public PasswordException()
: base()
{
}
public PasswordException(string message)
: base(message)
{
}
public PasswordException(string message, Exception innerException)
: base(message, innerException)
{
}
protected PasswordException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1a0af6d81e309f746935cdd3c3287be7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,106 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem;
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl
{
public class Pkcs8Generator
: PemObjectGenerator
{
// FIXME See PbeUtilities static constructor
// public static readonly string Aes128Cbc = NistObjectIdentifiers.IdAes128Cbc.Id;
// public static readonly string Aes192Cbc = NistObjectIdentifiers.IdAes192Cbc.Id;
// public static readonly string Aes256Cbc = NistObjectIdentifiers.IdAes256Cbc.Id;
//
// public static readonly string Des3Cbc = PkcsObjectIdentifiers.DesEde3Cbc.Id;
public static readonly string PbeSha1_RC4_128 = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC4.Id;
public static readonly string PbeSha1_RC4_40 = PkcsObjectIdentifiers.PbeWithShaAnd40BitRC4.Id;
public static readonly string PbeSha1_3DES = PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc.Id;
public static readonly string PbeSha1_2DES = PkcsObjectIdentifiers.PbeWithShaAnd2KeyTripleDesCbc.Id;
public static readonly string PbeSha1_RC2_128 = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC2Cbc.Id;
public static readonly string PbeSha1_RC2_40 = PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc.Id;
private char[] password;
private string algorithm;
private int iterationCount;
private AsymmetricKeyParameter privKey;
private SecureRandom random;
/**
* Constructor for an unencrypted private key PEM object.
*
* @param key private key to be encoded.
*/
public Pkcs8Generator(AsymmetricKeyParameter privKey)
{
this.privKey = privKey;
}
/**
* Constructor for an encrypted private key PEM object.
*
* @param key private key to be encoded
* @param algorithm encryption algorithm to use
* @param provider provider to use
* @throws NoSuchAlgorithmException if algorithm/mode cannot be found
*/
public Pkcs8Generator(AsymmetricKeyParameter privKey, string algorithm)
{
// TODO Check privKey.IsPrivate
this.privKey = privKey;
this.algorithm = algorithm;
this.iterationCount = 2048;
}
public SecureRandom SecureRandom
{
set { this.random = value; }
}
public char[] Password
{
set { this.password = value; }
}
public int IterationCount
{
set { this.iterationCount = value; }
}
public PemObject Generate()
{
if (algorithm == null)
{
PrivateKeyInfo pki = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privKey);
return new PemObject("PRIVATE KEY", pki.GetEncoded());
}
// TODO Theoretically, the amount of salt needed depends on the algorithm
byte[] salt = new byte[20];
random = CryptoServicesRegistrar.GetSecureRandom(random);
random.NextBytes(salt);
try
{
EncryptedPrivateKeyInfo epki = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(
algorithm, password, salt, iterationCount, privKey);
return new PemObject("ENCRYPTED PRIVATE KEY", epki.GetEncoded());
}
catch (Exception e)
{
throw new PemGenerationException("Couldn't encrypt private key", e);
}
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8fd3426c3338f804185fac837f54726a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: