SignatureScheme.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.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(GetHashAlgorithm(signatureScheme));
  79. }
  80. }
  81. }
  82. public static string GetName(int signatureScheme)
  83. {
  84. switch (signatureScheme)
  85. {
  86. case rsa_pkcs1_sha1:
  87. return "rsa_pkcs1_sha1";
  88. case ecdsa_sha1:
  89. return "ecdsa_sha1";
  90. case rsa_pkcs1_sha256:
  91. return "rsa_pkcs1_sha256";
  92. case rsa_pkcs1_sha384:
  93. return "rsa_pkcs1_sha384";
  94. case rsa_pkcs1_sha512:
  95. return "rsa_pkcs1_sha512";
  96. case ecdsa_secp256r1_sha256:
  97. return "ecdsa_secp256r1_sha256";
  98. case ecdsa_secp384r1_sha384:
  99. return "ecdsa_secp384r1_sha384";
  100. case ecdsa_secp521r1_sha512:
  101. return "ecdsa_secp521r1_sha512";
  102. case rsa_pss_rsae_sha256:
  103. return "rsa_pss_rsae_sha256";
  104. case rsa_pss_rsae_sha384:
  105. return "rsa_pss_rsae_sha384";
  106. case rsa_pss_rsae_sha512:
  107. return "rsa_pss_rsae_sha512";
  108. case ed25519:
  109. return "ed25519";
  110. case ed448:
  111. return "ed448";
  112. case rsa_pss_pss_sha256:
  113. return "rsa_pss_pss_sha256";
  114. case rsa_pss_pss_sha384:
  115. return "rsa_pss_pss_sha384";
  116. case rsa_pss_pss_sha512:
  117. return "rsa_pss_pss_sha512";
  118. case ecdsa_brainpoolP256r1tls13_sha256:
  119. return "ecdsa_brainpoolP256r1tls13_sha256";
  120. case ecdsa_brainpoolP384r1tls13_sha384:
  121. return "ecdsa_brainpoolP384r1tls13_sha384";
  122. case ecdsa_brainpoolP512r1tls13_sha512:
  123. return "ecdsa_brainpoolP512r1tls13_sha512";
  124. case sm2sig_sm3:
  125. return "sm2sig_sm3";
  126. default:
  127. return "UNKNOWN";
  128. }
  129. }
  130. /**
  131. * For TLS 1.3+ usage, some signature schemes are constrained to use a particular
  132. * ({@link NamedGroup}. Not relevant for TLS 1.2 and below.
  133. */
  134. public static int GetNamedGroup(int signatureScheme)
  135. {
  136. switch (signatureScheme)
  137. {
  138. case ecdsa_brainpoolP256r1tls13_sha256:
  139. return NamedGroup.brainpoolP256r1tls13;
  140. case ecdsa_brainpoolP384r1tls13_sha384:
  141. return NamedGroup.brainpoolP384r1tls13;
  142. case ecdsa_brainpoolP512r1tls13_sha512:
  143. return NamedGroup.brainpoolP512r1tls13;
  144. case ecdsa_secp256r1_sha256:
  145. return NamedGroup.secp256r1;
  146. case ecdsa_secp384r1_sha384:
  147. return NamedGroup.secp384r1;
  148. case ecdsa_secp521r1_sha512:
  149. return NamedGroup.secp521r1;
  150. case sm2sig_sm3:
  151. return NamedGroup.curveSM2;
  152. default:
  153. return -1;
  154. }
  155. }
  156. public static short GetHashAlgorithm(int signatureScheme)
  157. {
  158. // TODO[RFC 8998] sm2sig_sm3
  159. return (short)((signatureScheme >> 8) & 0xFF);
  160. }
  161. public static short GetSignatureAlgorithm(int signatureScheme)
  162. {
  163. // TODO[RFC 8998] sm2sig_sm3
  164. return (short)(signatureScheme & 0xFF);
  165. }
  166. public static SignatureAndHashAlgorithm GetSignatureAndHashAlgorithm(int signatureScheme)
  167. {
  168. return SignatureAndHashAlgorithm.GetInstance(
  169. GetHashAlgorithm(signatureScheme),
  170. GetSignatureAlgorithm(signatureScheme));
  171. }
  172. public static string GetText(int signatureScheme)
  173. {
  174. string hex = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(Convert.ToString(signatureScheme, 16));
  175. return GetName(signatureScheme) + "(0x" + hex + ")";
  176. }
  177. public static bool IsPrivate(int signatureScheme)
  178. {
  179. return (signatureScheme >> 9) == 0xFE;
  180. }
  181. public static bool IsECDsa(int signatureScheme)
  182. {
  183. switch (signatureScheme)
  184. {
  185. case ecdsa_brainpoolP256r1tls13_sha256:
  186. case ecdsa_brainpoolP384r1tls13_sha384:
  187. case ecdsa_brainpoolP512r1tls13_sha512:
  188. return true;
  189. default:
  190. return SignatureAlgorithm.ecdsa == GetSignatureAlgorithm(signatureScheme);
  191. }
  192. }
  193. public static bool IsRsaPss(int signatureScheme)
  194. {
  195. switch (signatureScheme)
  196. {
  197. case rsa_pss_rsae_sha256:
  198. case rsa_pss_rsae_sha384:
  199. case rsa_pss_rsae_sha512:
  200. case rsa_pss_pss_sha256:
  201. case rsa_pss_pss_sha384:
  202. case rsa_pss_pss_sha512:
  203. return true;
  204. default:
  205. return false;
  206. }
  207. }
  208. }
  209. }
  210. #pragma warning restore
  211. #endif