SignatureScheme.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. public abstract class SignatureScheme
  9. {
  10. /*
  11. * RFC 8446
  12. */
  13. public const int rsa_pkcs1_sha1 = 0x0201;
  14. public const int ecdsa_sha1 = 0x0203;
  15. public const int rsa_pkcs1_sha256 = 0x0401;
  16. public const int rsa_pkcs1_sha384 = 0x0501;
  17. public const int rsa_pkcs1_sha512 = 0x0601;
  18. public const int ecdsa_secp256r1_sha256 = 0x0403;
  19. public const int ecdsa_secp384r1_sha384 = 0x0503;
  20. public const int ecdsa_secp521r1_sha512 = 0x0603;
  21. public const int rsa_pss_rsae_sha256 = 0x0804;
  22. public const int rsa_pss_rsae_sha384 = 0x0805;
  23. public const int rsa_pss_rsae_sha512 = 0x0806;
  24. public const int ed25519 = 0x0807;
  25. public const int ed448 = 0x0808;
  26. public const int rsa_pss_pss_sha256 = 0x0809;
  27. public const int rsa_pss_pss_sha384 = 0x080A;
  28. public const int rsa_pss_pss_sha512 = 0x080B;
  29. /*
  30. * RFC 8734
  31. */
  32. public const int ecdsa_brainpoolP256r1tls13_sha256 = 0x081A;
  33. public const int ecdsa_brainpoolP384r1tls13_sha384 = 0x081B;
  34. public const int ecdsa_brainpoolP512r1tls13_sha512 = 0x081C;
  35. /*
  36. * RFC 8998
  37. */
  38. public const int sm2sig_sm3 = 0x0708;
  39. /*
  40. * RFC 8446 reserved for private use (0xFE00..0xFFFF)
  41. */
  42. public static int From(SignatureAndHashAlgorithm sigAndHashAlg)
  43. {
  44. if (null == sigAndHashAlg)
  45. throw new ArgumentNullException();
  46. return From(sigAndHashAlg.Hash, sigAndHashAlg.Signature);
  47. }
  48. public static int From(short hashAlgorithm, short signatureAlgorithm)
  49. {
  50. return ((hashAlgorithm & 0xFF) << 8) | (signatureAlgorithm & 0xFF);
  51. }
  52. public static int GetCryptoHashAlgorithm(int signatureScheme)
  53. {
  54. switch (signatureScheme)
  55. {
  56. case ed25519:
  57. case ed448:
  58. return -1;
  59. case ecdsa_brainpoolP256r1tls13_sha256:
  60. case rsa_pss_pss_sha256:
  61. case rsa_pss_rsae_sha256:
  62. return CryptoHashAlgorithm.sha256;
  63. case ecdsa_brainpoolP384r1tls13_sha384:
  64. case rsa_pss_pss_sha384:
  65. case rsa_pss_rsae_sha384:
  66. return CryptoHashAlgorithm.sha384;
  67. case ecdsa_brainpoolP512r1tls13_sha512:
  68. case rsa_pss_pss_sha512:
  69. case rsa_pss_rsae_sha512:
  70. return CryptoHashAlgorithm.sha512;
  71. case sm2sig_sm3:
  72. return CryptoHashAlgorithm.sm3;
  73. default:
  74. {
  75. short hashAlgorithm = GetHashAlgorithm(signatureScheme);
  76. if (HashAlgorithm.Intrinsic == hashAlgorithm || !HashAlgorithm.IsRecognized(hashAlgorithm))
  77. return -1;
  78. return TlsCryptoUtilities.GetHash(hashAlgorithm);
  79. }
  80. }
  81. }
  82. public static int GetCryptoHashAlgorithm(SignatureAndHashAlgorithm signatureAndHashAlgorithm)
  83. {
  84. return GetCryptoHashAlgorithm(From(signatureAndHashAlgorithm));
  85. }
  86. public static string GetName(int signatureScheme)
  87. {
  88. switch (signatureScheme)
  89. {
  90. case rsa_pkcs1_sha1:
  91. return "rsa_pkcs1_sha1";
  92. case ecdsa_sha1:
  93. return "ecdsa_sha1";
  94. case rsa_pkcs1_sha256:
  95. return "rsa_pkcs1_sha256";
  96. case rsa_pkcs1_sha384:
  97. return "rsa_pkcs1_sha384";
  98. case rsa_pkcs1_sha512:
  99. return "rsa_pkcs1_sha512";
  100. case ecdsa_secp256r1_sha256:
  101. return "ecdsa_secp256r1_sha256";
  102. case ecdsa_secp384r1_sha384:
  103. return "ecdsa_secp384r1_sha384";
  104. case ecdsa_secp521r1_sha512:
  105. return "ecdsa_secp521r1_sha512";
  106. case rsa_pss_rsae_sha256:
  107. return "rsa_pss_rsae_sha256";
  108. case rsa_pss_rsae_sha384:
  109. return "rsa_pss_rsae_sha384";
  110. case rsa_pss_rsae_sha512:
  111. return "rsa_pss_rsae_sha512";
  112. case ed25519:
  113. return "ed25519";
  114. case ed448:
  115. return "ed448";
  116. case rsa_pss_pss_sha256:
  117. return "rsa_pss_pss_sha256";
  118. case rsa_pss_pss_sha384:
  119. return "rsa_pss_pss_sha384";
  120. case rsa_pss_pss_sha512:
  121. return "rsa_pss_pss_sha512";
  122. case ecdsa_brainpoolP256r1tls13_sha256:
  123. return "ecdsa_brainpoolP256r1tls13_sha256";
  124. case ecdsa_brainpoolP384r1tls13_sha384:
  125. return "ecdsa_brainpoolP384r1tls13_sha384";
  126. case ecdsa_brainpoolP512r1tls13_sha512:
  127. return "ecdsa_brainpoolP512r1tls13_sha512";
  128. case sm2sig_sm3:
  129. return "sm2sig_sm3";
  130. default:
  131. return "UNKNOWN";
  132. }
  133. }
  134. /**
  135. * For TLS 1.3+ usage, some signature schemes are constrained to use a particular
  136. * ({@link NamedGroup}. Not relevant for TLS 1.2 and below.
  137. */
  138. public static int GetNamedGroup(int signatureScheme)
  139. {
  140. switch (signatureScheme)
  141. {
  142. case ecdsa_brainpoolP256r1tls13_sha256:
  143. return NamedGroup.brainpoolP256r1tls13;
  144. case ecdsa_brainpoolP384r1tls13_sha384:
  145. return NamedGroup.brainpoolP384r1tls13;
  146. case ecdsa_brainpoolP512r1tls13_sha512:
  147. return NamedGroup.brainpoolP512r1tls13;
  148. case ecdsa_secp256r1_sha256:
  149. return NamedGroup.secp256r1;
  150. case ecdsa_secp384r1_sha384:
  151. return NamedGroup.secp384r1;
  152. case ecdsa_secp521r1_sha512:
  153. return NamedGroup.secp521r1;
  154. case sm2sig_sm3:
  155. return NamedGroup.curveSM2;
  156. default:
  157. return -1;
  158. }
  159. }
  160. public static short GetHashAlgorithm(int signatureScheme)
  161. {
  162. // TODO[RFC 8998] sm2sig_sm3
  163. return (short)((signatureScheme >> 8) & 0xFF);
  164. }
  165. public static short GetSignatureAlgorithm(int signatureScheme)
  166. {
  167. // TODO[RFC 8998] sm2sig_sm3
  168. return (short)(signatureScheme & 0xFF);
  169. }
  170. public static SignatureAndHashAlgorithm GetSignatureAndHashAlgorithm(int signatureScheme)
  171. {
  172. return SignatureAndHashAlgorithm.GetInstance(
  173. GetHashAlgorithm(signatureScheme),
  174. GetSignatureAlgorithm(signatureScheme));
  175. }
  176. public static string GetText(int signatureScheme)
  177. {
  178. string hex = Convert.ToString(signatureScheme, 16).ToUpperInvariant();
  179. return GetName(signatureScheme) + "(0x" + hex + ")";
  180. }
  181. public static bool IsPrivate(int signatureScheme)
  182. {
  183. return (signatureScheme >> 9) == 0xFE;
  184. }
  185. public static bool IsECDsa(int signatureScheme)
  186. {
  187. switch (signatureScheme)
  188. {
  189. case ecdsa_brainpoolP256r1tls13_sha256:
  190. case ecdsa_brainpoolP384r1tls13_sha384:
  191. case ecdsa_brainpoolP512r1tls13_sha512:
  192. return true;
  193. default:
  194. return SignatureAlgorithm.ecdsa == GetSignatureAlgorithm(signatureScheme);
  195. }
  196. }
  197. public static bool IsRsaPss(int signatureScheme)
  198. {
  199. switch (signatureScheme)
  200. {
  201. case rsa_pss_rsae_sha256:
  202. case rsa_pss_rsae_sha384:
  203. case rsa_pss_rsae_sha512:
  204. case rsa_pss_pss_sha256:
  205. case rsa_pss_pss_sha384:
  206. case rsa_pss_pss_sha512:
  207. return true;
  208. default:
  209. return false;
  210. }
  211. }
  212. }
  213. }
  214. #pragma warning restore
  215. #endif