You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.9 KiB
140 lines
3.9 KiB
1 year ago
|
#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.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;
|
||
|
|
||
|
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
|
||
|
{
|
||
|
internal class X509SignatureUtilities
|
||
|
{
|
||
|
private static readonly Asn1Null derNull = DerNull.Instance;
|
||
|
|
||
|
internal static void SetSignatureParameters(
|
||
|
ISigner signature,
|
||
|
Asn1Encodable parameters)
|
||
|
{
|
||
|
if (parameters != null && !derNull.Equals(parameters))
|
||
|
{
|
||
|
// TODO Put back in
|
||
|
// AlgorithmParameters sigParams = AlgorithmParameters.GetInstance(signature.getAlgorithm());
|
||
|
//
|
||
|
// try
|
||
|
// {
|
||
|
// sigParams.Init(parameters.ToAsn1Object().GetDerEncoded());
|
||
|
// }
|
||
|
// catch (IOException e)
|
||
|
// {
|
||
|
// throw new SignatureException("IOException decoding parameters: " + e.Message);
|
||
|
// }
|
||
|
//
|
||
|
// if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(signature.getAlgorithm(), "MGF1"))
|
||
|
// {
|
||
|
// try
|
||
|
// {
|
||
|
// signature.setParameter(sigParams.getParameterSpec(PSSParameterSpec.class));
|
||
|
// }
|
||
|
// catch (GeneralSecurityException e)
|
||
|
// {
|
||
|
// throw new SignatureException("Exception extracting parameters: " + e.Message);
|
||
|
// }
|
||
|
// }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal static string GetSignatureName(
|
||
|
AlgorithmIdentifier sigAlgId)
|
||
|
{
|
||
|
Asn1Encodable parameters = sigAlgId.Parameters;
|
||
|
|
||
|
if (parameters != null && !derNull.Equals(parameters))
|
||
|
{
|
||
|
if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
|
||
|
{
|
||
|
RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(parameters);
|
||
|
|
||
|
return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
|
||
|
}
|
||
|
if (sigAlgId.Algorithm.Equals(X9ObjectIdentifiers.ECDsaWithSha2))
|
||
|
{
|
||
|
Asn1Sequence ecDsaParams = Asn1Sequence.GetInstance(parameters);
|
||
|
|
||
|
return GetDigestAlgName((DerObjectIdentifier)ecDsaParams[0]) + "withECDSA";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
string sigName = SignerUtilities.GetEncodingName(sigAlgId.Algorithm);
|
||
|
if (null != sigName)
|
||
|
{
|
||
|
return sigName;
|
||
|
}
|
||
|
|
||
|
return sigAlgId.Algorithm.Id;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return the digest algorithm using one of the standard JCA string
|
||
|
* representations rather than the algorithm identifier (if possible).
|
||
|
*/
|
||
|
private static string GetDigestAlgName(
|
||
|
DerObjectIdentifier digestAlgOID)
|
||
|
{
|
||
|
if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "MD5";
|
||
|
}
|
||
|
else if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "SHA1";
|
||
|
}
|
||
|
else if (NistObjectIdentifiers.IdSha224.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "SHA224";
|
||
|
}
|
||
|
else if (NistObjectIdentifiers.IdSha256.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "SHA256";
|
||
|
}
|
||
|
else if (NistObjectIdentifiers.IdSha384.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "SHA384";
|
||
|
}
|
||
|
else if (NistObjectIdentifiers.IdSha512.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "SHA512";
|
||
|
}
|
||
|
else if (TeleTrusTObjectIdentifiers.RipeMD128.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "RIPEMD128";
|
||
|
}
|
||
|
else if (TeleTrusTObjectIdentifiers.RipeMD160.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "RIPEMD160";
|
||
|
}
|
||
|
else if (TeleTrusTObjectIdentifiers.RipeMD256.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "RIPEMD256";
|
||
|
}
|
||
|
else if (CryptoProObjectIdentifiers.GostR3411.Equals(digestAlgOID))
|
||
|
{
|
||
|
return "GOST3411";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return digestAlgOID.Id;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#pragma warning restore
|
||
|
#endif
|