OCSPUtil.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  14. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Ocsp
  15. {
  16. internal class OcspUtilities
  17. {
  18. private static readonly Dictionary<string, DerObjectIdentifier> Algorithms =
  19. new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
  20. private static readonly Dictionary<DerObjectIdentifier, string> Oids =
  21. new Dictionary<DerObjectIdentifier, string>();
  22. private static readonly HashSet<DerObjectIdentifier> NoParams = new HashSet<DerObjectIdentifier>();
  23. static OcspUtilities()
  24. {
  25. Algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  26. Algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  27. Algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  28. Algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  29. Algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  30. Algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  31. Algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  32. Algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  33. Algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  34. Algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  35. Algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  36. Algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  37. Algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  38. Algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  39. Algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  40. Algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  41. Algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  42. Algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  43. Algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  44. Algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  45. Algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  46. Algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  47. Algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  48. Algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  49. Algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  50. Algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  51. Algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  52. Algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  53. Algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  54. Algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  55. Algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  56. Algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  57. Algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  58. Algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  59. Algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  60. Algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  61. Algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  62. Algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  63. Algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
  64. Algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
  65. Algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
  66. Algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
  67. Algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
  68. Algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
  69. Algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
  70. Algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
  71. Algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
  72. Algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
  73. Algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  74. Algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  75. Oids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2WITHRSA");
  76. Oids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5WITHRSA");
  77. Oids.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1WITHRSA");
  78. Oids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224WITHRSA");
  79. Oids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256WITHRSA");
  80. Oids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384WITHRSA");
  81. Oids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512WITHRSA");
  82. Oids.Add(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)WITHRSA");
  83. Oids.Add(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)WITHRSA");
  84. Oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160, "RIPEMD160WITHRSA");
  85. Oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128, "RIPEMD128WITHRSA");
  86. Oids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256, "RIPEMD256WITHRSA");
  87. Oids.Add(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1WITHDSA");
  88. Oids.Add(NistObjectIdentifiers.DsaWithSha224, "SHA224WITHDSA");
  89. Oids.Add(NistObjectIdentifiers.DsaWithSha256, "SHA256WITHDSA");
  90. Oids.Add(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1WITHECDSA");
  91. Oids.Add(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224WITHECDSA");
  92. Oids.Add(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256WITHECDSA");
  93. Oids.Add(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384WITHECDSA");
  94. Oids.Add(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512WITHECDSA");
  95. Oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411WITHGOST3410");
  96. Oids.Add(OiwObjectIdentifiers.MD5WithRsa, "MD5WITHRSA");
  97. Oids.Add(OiwObjectIdentifiers.Sha1WithRsa, "SHA1WITHRSA");
  98. Oids.Add(OiwObjectIdentifiers.DsaWithSha1, "SHA1WITHDSA");
  99. //
  100. // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
  101. // The parameters field SHALL be NULL for RSA based signature algorithms.
  102. //
  103. NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
  104. NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
  105. NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
  106. NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
  107. NoParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
  108. NoParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
  109. NoParams.Add(OiwObjectIdentifiers.DsaWithSha1);
  110. NoParams.Add(NistObjectIdentifiers.DsaWithSha224);
  111. NoParams.Add(NistObjectIdentifiers.DsaWithSha256);
  112. }
  113. internal static DerObjectIdentifier GetAlgorithmOid(string algorithmName)
  114. {
  115. if (Algorithms.TryGetValue(algorithmName, out var oid))
  116. return oid;
  117. return new DerObjectIdentifier(algorithmName);
  118. }
  119. internal static string GetAlgorithmName(DerObjectIdentifier oid)
  120. {
  121. if (Oids.TryGetValue(oid, out var algorithmName))
  122. return algorithmName;
  123. return oid.Id;
  124. }
  125. internal static AlgorithmIdentifier GetSigAlgID(DerObjectIdentifier sigOid)
  126. {
  127. if (NoParams.Contains(sigOid))
  128. return new AlgorithmIdentifier(sigOid);
  129. return new AlgorithmIdentifier(sigOid, DerNull.Instance);
  130. }
  131. internal static IEnumerable<string> AlgNames
  132. {
  133. get { return CollectionUtilities.Proxy(Algorithms.Keys); }
  134. }
  135. }
  136. }
  137. #pragma warning restore
  138. #endif