Asn1Signature.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  19. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators
  20. {
  21. internal class X509Utilities
  22. {
  23. private static readonly Asn1Null derNull = DerNull.Instance;
  24. private static readonly IDictionary algorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  25. private static readonly IDictionary exParams = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  26. private static readonly ISet noParams = new HashSet();
  27. static X509Utilities()
  28. {
  29. algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  30. algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  31. algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  32. algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  33. algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  34. algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  35. algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  36. algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  37. algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  38. algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  39. algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  40. algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  41. algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  42. algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  43. algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  44. algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  45. algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  46. algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  47. algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  48. algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  49. algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  50. algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  51. algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  52. algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  53. algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  54. algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
  55. algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
  56. algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
  57. algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
  58. algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384);
  59. algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512);
  60. algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
  61. algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
  62. algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
  63. algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
  64. algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
  65. algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
  66. algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  67. algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  68. algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  69. algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  70. algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  71. //
  72. // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
  73. // The parameters field SHALL be NULL for RSA based signature algorithms.
  74. //
  75. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
  76. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
  77. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
  78. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
  79. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
  80. noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
  81. noParams.Add(NistObjectIdentifiers.DsaWithSha224);
  82. noParams.Add(NistObjectIdentifiers.DsaWithSha256);
  83. noParams.Add(NistObjectIdentifiers.DsaWithSha384);
  84. noParams.Add(NistObjectIdentifiers.DsaWithSha512);
  85. //
  86. // RFC 4491
  87. //
  88. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  89. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  90. //
  91. // explicit params
  92. //
  93. AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
  94. exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20));
  95. AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
  96. exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28));
  97. AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
  98. exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32));
  99. AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
  100. exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48));
  101. AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
  102. exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64));
  103. }
  104. /**
  105. * Return the digest algorithm using one of the standard JCA string
  106. * representations rather than the algorithm identifier (if possible).
  107. */
  108. private static string GetDigestAlgName(
  109. DerObjectIdentifier digestAlgOID)
  110. {
  111. if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOID))
  112. {
  113. return "MD5";
  114. }
  115. else if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOID))
  116. {
  117. return "SHA1";
  118. }
  119. else if (NistObjectIdentifiers.IdSha224.Equals(digestAlgOID))
  120. {
  121. return "SHA224";
  122. }
  123. else if (NistObjectIdentifiers.IdSha256.Equals(digestAlgOID))
  124. {
  125. return "SHA256";
  126. }
  127. else if (NistObjectIdentifiers.IdSha384.Equals(digestAlgOID))
  128. {
  129. return "SHA384";
  130. }
  131. else if (NistObjectIdentifiers.IdSha512.Equals(digestAlgOID))
  132. {
  133. return "SHA512";
  134. }
  135. else if (TeleTrusTObjectIdentifiers.RipeMD128.Equals(digestAlgOID))
  136. {
  137. return "RIPEMD128";
  138. }
  139. else if (TeleTrusTObjectIdentifiers.RipeMD160.Equals(digestAlgOID))
  140. {
  141. return "RIPEMD160";
  142. }
  143. else if (TeleTrusTObjectIdentifiers.RipeMD256.Equals(digestAlgOID))
  144. {
  145. return "RIPEMD256";
  146. }
  147. else if (CryptoProObjectIdentifiers.GostR3411.Equals(digestAlgOID))
  148. {
  149. return "GOST3411";
  150. }
  151. else
  152. {
  153. return digestAlgOID.Id;
  154. }
  155. }
  156. internal static string GetSignatureName(AlgorithmIdentifier sigAlgId)
  157. {
  158. Asn1Encodable parameters = sigAlgId.Parameters;
  159. if (parameters != null && !derNull.Equals(parameters))
  160. {
  161. if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
  162. {
  163. RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(parameters);
  164. return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
  165. }
  166. if (sigAlgId.Algorithm.Equals(X9ObjectIdentifiers.ECDsaWithSha2))
  167. {
  168. Asn1Sequence ecDsaParams = Asn1Sequence.GetInstance(parameters);
  169. return GetDigestAlgName((DerObjectIdentifier)ecDsaParams[0]) + "withECDSA";
  170. }
  171. }
  172. return sigAlgId.Algorithm.Id;
  173. }
  174. private static RsassaPssParameters CreatePssParams(
  175. AlgorithmIdentifier hashAlgId,
  176. int saltSize)
  177. {
  178. return new RsassaPssParameters(
  179. hashAlgId,
  180. new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hashAlgId),
  181. new DerInteger(saltSize),
  182. new DerInteger(1));
  183. }
  184. internal static DerObjectIdentifier GetAlgorithmOid(
  185. string algorithmName)
  186. {
  187. algorithmName = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithmName);
  188. if (algorithms.Contains(algorithmName))
  189. {
  190. return (DerObjectIdentifier) algorithms[algorithmName];
  191. }
  192. return new DerObjectIdentifier(algorithmName);
  193. }
  194. internal static AlgorithmIdentifier GetSigAlgID(
  195. DerObjectIdentifier sigOid,
  196. string algorithmName)
  197. {
  198. if (noParams.Contains(sigOid))
  199. {
  200. return new AlgorithmIdentifier(sigOid);
  201. }
  202. algorithmName = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithmName);
  203. if (exParams.Contains(algorithmName))
  204. {
  205. return new AlgorithmIdentifier(sigOid, (Asn1Encodable) exParams[algorithmName]);
  206. }
  207. return new AlgorithmIdentifier(sigOid, DerNull.Instance);
  208. }
  209. internal static IEnumerable GetAlgNames()
  210. {
  211. return new EnumerableProxy(algorithms.Keys);
  212. }
  213. }
  214. /// <summary>
  215. /// Calculator factory class for signature generation in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
  216. /// signature algorithm details.
  217. /// </summary>
  218. public class Asn1SignatureFactory
  219. : ISignatureFactory
  220. {
  221. private readonly AlgorithmIdentifier algID;
  222. private readonly string algorithm;
  223. private readonly AsymmetricKeyParameter privateKey;
  224. private readonly SecureRandom random;
  225. /// <summary>
  226. /// Base constructor.
  227. /// </summary>
  228. /// <param name="algorithm">The name of the signature algorithm to use.</param>
  229. /// <param name="privateKey">The private key to be used in the signing operation.</param>
  230. public Asn1SignatureFactory (string algorithm, AsymmetricKeyParameter privateKey)
  231. : this(algorithm, privateKey, null)
  232. {
  233. }
  234. /// <summary>
  235. /// Constructor which also specifies a source of randomness to be used if one is required.
  236. /// </summary>
  237. /// <param name="algorithm">The name of the signature algorithm to use.</param>
  238. /// <param name="privateKey">The private key to be used in the signing operation.</param>
  239. /// <param name="random">The source of randomness to be used in signature calculation.</param>
  240. public Asn1SignatureFactory(string algorithm, AsymmetricKeyParameter privateKey, SecureRandom random)
  241. {
  242. if (algorithm == null)
  243. throw new ArgumentNullException("algorithm");
  244. if (privateKey == null)
  245. throw new ArgumentNullException("privateKey");
  246. if (!privateKey.IsPrivate)
  247. throw new ArgumentException("Key for signing must be private", "privateKey");
  248. DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid(algorithm);
  249. this.algorithm = algorithm;
  250. this.privateKey = privateKey;
  251. this.random = random;
  252. this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
  253. }
  254. public Object AlgorithmDetails
  255. {
  256. get { return this.algID; }
  257. }
  258. public IStreamCalculator CreateCalculator()
  259. {
  260. ISigner signer = SignerUtilities.InitSigner(algorithm, true, privateKey, random);
  261. return new DefaultSignatureCalculator(signer);
  262. }
  263. /// <summary>
  264. /// Allows enumeration of the signature names supported by the verifier provider.
  265. /// </summary>
  266. public static IEnumerable SignatureAlgNames
  267. {
  268. get { return X509Utilities.GetAlgNames(); }
  269. }
  270. }
  271. /// <summary>
  272. /// Verifier class for signature verification in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
  273. /// signature algorithm details.
  274. /// </summary>
  275. public class Asn1VerifierFactory
  276. : IVerifierFactory
  277. {
  278. private readonly AlgorithmIdentifier algID;
  279. private readonly AsymmetricKeyParameter publicKey;
  280. /// <summary>
  281. /// Base constructor.
  282. /// </summary>
  283. /// <param name="algorithm">The name of the signature algorithm to use.</param>
  284. /// <param name="publicKey">The public key to be used in the verification operation.</param>
  285. public Asn1VerifierFactory(string algorithm, AsymmetricKeyParameter publicKey)
  286. {
  287. if (algorithm == null)
  288. throw new ArgumentNullException("algorithm");
  289. if (publicKey == null)
  290. throw new ArgumentNullException("publicKey");
  291. if (publicKey.IsPrivate)
  292. throw new ArgumentException("Key for verifying must be public", "publicKey");
  293. DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid(algorithm);
  294. this.publicKey = publicKey;
  295. this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
  296. }
  297. public Asn1VerifierFactory(AlgorithmIdentifier algorithm, AsymmetricKeyParameter publicKey)
  298. {
  299. this.publicKey = publicKey;
  300. this.algID = algorithm;
  301. }
  302. public Object AlgorithmDetails
  303. {
  304. get { return this.algID; }
  305. }
  306. public IStreamCalculator CreateCalculator()
  307. {
  308. ISigner verifier = SignerUtilities.InitSigner(X509Utilities.GetSignatureName(algID), false, publicKey, null);
  309. return new DefaultVerifierCalculator(verifier);
  310. }
  311. }
  312. /// <summary>
  313. /// Provider class which supports dynamic creation of signature verifiers.
  314. /// </summary>
  315. public class Asn1VerifierFactoryProvider: IVerifierFactoryProvider
  316. {
  317. private readonly AsymmetricKeyParameter publicKey;
  318. /// <summary>
  319. /// Base constructor - specify the public key to be used in verification.
  320. /// </summary>
  321. /// <param name="publicKey">The public key to be used in creating verifiers provided by this object.</param>
  322. public Asn1VerifierFactoryProvider(AsymmetricKeyParameter publicKey)
  323. {
  324. this.publicKey = publicKey;
  325. }
  326. public IVerifierFactory CreateVerifierFactory(Object algorithmDetails)
  327. {
  328. return new Asn1VerifierFactory((AlgorithmIdentifier)algorithmDetails, publicKey);
  329. }
  330. /// <summary>
  331. /// Allows enumeration of the signature names supported by the verifier provider.
  332. /// </summary>
  333. public IEnumerable SignatureAlgNames
  334. {
  335. get { return X509Utilities.GetAlgNames(); }
  336. }
  337. }
  338. }
  339. #pragma warning restore
  340. #endif