X509Utilities.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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;
  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.X509
  20. {
  21. internal class X509Utilities
  22. {
  23. private static readonly IDictionary algorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  24. private static readonly IDictionary exParams = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  25. private static readonly ISet noParams = new HashSet();
  26. static X509Utilities()
  27. {
  28. algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  29. algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  30. algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  31. algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  32. algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  33. algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  34. algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  35. algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  36. algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  37. algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  38. algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  39. algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  40. algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  41. algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  42. algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  43. algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  44. algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  45. algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  46. algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  47. algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  48. algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  49. algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  50. algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  51. algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  52. algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  53. algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  54. algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  55. algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  56. algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  57. algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  58. algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  59. algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  60. algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  61. algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  62. algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  63. algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  64. algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  65. algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  66. algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  67. algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  68. algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  69. algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  70. algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  71. algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
  72. algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
  73. algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
  74. algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
  75. algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384);
  76. algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512);
  77. algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
  78. algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
  79. algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
  80. algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
  81. algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
  82. algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
  83. algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  84. algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  85. algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  86. algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  87. algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  88. //
  89. // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
  90. // The parameters field SHALL be NULL for RSA based signature algorithms.
  91. //
  92. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
  93. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
  94. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
  95. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
  96. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
  97. noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
  98. noParams.Add(OiwObjectIdentifiers.DsaWithSha1);
  99. noParams.Add(NistObjectIdentifiers.DsaWithSha224);
  100. noParams.Add(NistObjectIdentifiers.DsaWithSha256);
  101. noParams.Add(NistObjectIdentifiers.DsaWithSha384);
  102. noParams.Add(NistObjectIdentifiers.DsaWithSha512);
  103. //
  104. // RFC 4491
  105. //
  106. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  107. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  108. //
  109. // explicit params
  110. //
  111. AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
  112. exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20));
  113. AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
  114. exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28));
  115. AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
  116. exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32));
  117. AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
  118. exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48));
  119. AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
  120. exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64));
  121. }
  122. private static RsassaPssParameters CreatePssParams(
  123. AlgorithmIdentifier hashAlgId,
  124. int saltSize)
  125. {
  126. return new RsassaPssParameters(
  127. hashAlgId,
  128. new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hashAlgId),
  129. new DerInteger(saltSize),
  130. new DerInteger(1));
  131. }
  132. internal static DerObjectIdentifier GetAlgorithmOid(
  133. string algorithmName)
  134. {
  135. algorithmName = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithmName);
  136. if (algorithms.Contains(algorithmName))
  137. {
  138. return (DerObjectIdentifier) algorithms[algorithmName];
  139. }
  140. return new DerObjectIdentifier(algorithmName);
  141. }
  142. internal static AlgorithmIdentifier GetSigAlgID(
  143. DerObjectIdentifier sigOid,
  144. string algorithmName)
  145. {
  146. if (noParams.Contains(sigOid))
  147. {
  148. return new AlgorithmIdentifier(sigOid);
  149. }
  150. algorithmName = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithmName);
  151. if (exParams.Contains(algorithmName))
  152. {
  153. return new AlgorithmIdentifier(sigOid, (Asn1Encodable) exParams[algorithmName]);
  154. }
  155. return new AlgorithmIdentifier(sigOid, DerNull.Instance);
  156. }
  157. internal static IEnumerable GetAlgNames()
  158. {
  159. return new EnumerableProxy(algorithms.Keys);
  160. }
  161. internal static byte[] GetSignatureForObject(
  162. DerObjectIdentifier sigOid, // TODO Redundant now?
  163. string sigName,
  164. AsymmetricKeyParameter privateKey,
  165. SecureRandom random,
  166. Asn1Encodable ae)
  167. {
  168. if (sigOid == null)
  169. throw new ArgumentNullException("sigOid");
  170. ISigner sig = SignerUtilities.GetSigner(sigName);
  171. if (random != null)
  172. {
  173. sig.Init(true, new ParametersWithRandom(privateKey, random));
  174. }
  175. else
  176. {
  177. sig.Init(true, privateKey);
  178. }
  179. byte[] encoded = ae.GetDerEncoded();
  180. sig.BlockUpdate(encoded, 0, encoded.Length);
  181. return sig.GenerateSignature();
  182. }
  183. }
  184. }
  185. #pragma warning restore
  186. #endif