TlsCryptoUtilities.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto
  6. {
  7. public abstract class TlsCryptoUtilities
  8. {
  9. // "tls13 "
  10. private static readonly byte[] Tls13Prefix = new byte[] { 0x74, 0x6c, 0x73, 0x31, 0x33, 0x20 };
  11. public static int GetHash(short hashAlgorithm)
  12. {
  13. switch (hashAlgorithm)
  14. {
  15. case HashAlgorithm.md5:
  16. return CryptoHashAlgorithm.md5;
  17. case HashAlgorithm.sha1:
  18. return CryptoHashAlgorithm.sha1;
  19. case HashAlgorithm.sha224:
  20. return CryptoHashAlgorithm.sha224;
  21. case HashAlgorithm.sha256:
  22. return CryptoHashAlgorithm.sha256;
  23. case HashAlgorithm.sha384:
  24. return CryptoHashAlgorithm.sha384;
  25. case HashAlgorithm.sha512:
  26. return CryptoHashAlgorithm.sha512;
  27. default:
  28. throw new ArgumentException("specified HashAlgorithm invalid: " + HashAlgorithm.GetText(hashAlgorithm));
  29. }
  30. }
  31. public static int GetHashForHmac(int macAlgorithm)
  32. {
  33. switch (macAlgorithm)
  34. {
  35. case MacAlgorithm.hmac_md5:
  36. return CryptoHashAlgorithm.md5;
  37. case MacAlgorithm.hmac_sha1:
  38. return CryptoHashAlgorithm.sha1;
  39. case MacAlgorithm.hmac_sha256:
  40. return CryptoHashAlgorithm.sha256;
  41. case MacAlgorithm.hmac_sha384:
  42. return CryptoHashAlgorithm.sha384;
  43. case MacAlgorithm.hmac_sha512:
  44. return CryptoHashAlgorithm.sha512;
  45. default:
  46. throw new ArgumentException("specified MacAlgorithm not an HMAC: " + MacAlgorithm.GetText(macAlgorithm));
  47. }
  48. }
  49. public static int GetHashForPrf(int prfAlgorithm)
  50. {
  51. switch (prfAlgorithm)
  52. {
  53. case PrfAlgorithm.ssl_prf_legacy:
  54. case PrfAlgorithm.tls_prf_legacy:
  55. throw new ArgumentException("legacy PRF not a valid algorithm");
  56. case PrfAlgorithm.tls_prf_sha256:
  57. case PrfAlgorithm.tls13_hkdf_sha256:
  58. return CryptoHashAlgorithm.sha256;
  59. case PrfAlgorithm.tls_prf_sha384:
  60. case PrfAlgorithm.tls13_hkdf_sha384:
  61. return CryptoHashAlgorithm.sha384;
  62. case PrfAlgorithm.tls13_hkdf_sm3:
  63. return CryptoHashAlgorithm.sm3;
  64. default:
  65. throw new ArgumentException("unknown PrfAlgorithm: " + PrfAlgorithm.GetText(prfAlgorithm));
  66. }
  67. }
  68. public static int GetHashOutputSize(int cryptoHashAlgorithm)
  69. {
  70. switch (cryptoHashAlgorithm)
  71. {
  72. case CryptoHashAlgorithm.md5:
  73. return 16;
  74. case CryptoHashAlgorithm.sha1:
  75. return 20;
  76. case CryptoHashAlgorithm.sha224:
  77. return 28;
  78. case CryptoHashAlgorithm.sha256:
  79. case CryptoHashAlgorithm.sm3:
  80. return 32;
  81. case CryptoHashAlgorithm.sha384:
  82. return 48;
  83. case CryptoHashAlgorithm.sha512:
  84. return 64;
  85. default:
  86. throw new ArgumentException();
  87. }
  88. }
  89. public static int GetSignature(short signatureAlgorithm)
  90. {
  91. switch (signatureAlgorithm)
  92. {
  93. case SignatureAlgorithm.rsa:
  94. return CryptoSignatureAlgorithm.rsa;
  95. case SignatureAlgorithm.dsa:
  96. return CryptoSignatureAlgorithm.dsa;
  97. case SignatureAlgorithm.ecdsa:
  98. return CryptoSignatureAlgorithm.ecdsa;
  99. case SignatureAlgorithm.rsa_pss_rsae_sha256:
  100. return CryptoSignatureAlgorithm.rsa_pss_rsae_sha256;
  101. case SignatureAlgorithm.rsa_pss_rsae_sha384:
  102. return CryptoSignatureAlgorithm.rsa_pss_rsae_sha384;
  103. case SignatureAlgorithm.rsa_pss_rsae_sha512:
  104. return CryptoSignatureAlgorithm.rsa_pss_rsae_sha512;
  105. case SignatureAlgorithm.ed25519:
  106. return CryptoSignatureAlgorithm.ed25519;
  107. case SignatureAlgorithm.ed448:
  108. return CryptoSignatureAlgorithm.ed448;
  109. case SignatureAlgorithm.rsa_pss_pss_sha256:
  110. return CryptoSignatureAlgorithm.rsa_pss_pss_sha256;
  111. case SignatureAlgorithm.rsa_pss_pss_sha384:
  112. return CryptoSignatureAlgorithm.rsa_pss_pss_sha384;
  113. case SignatureAlgorithm.rsa_pss_pss_sha512:
  114. return CryptoSignatureAlgorithm.rsa_pss_pss_sha512;
  115. case SignatureAlgorithm.gostr34102012_256:
  116. return CryptoSignatureAlgorithm.gostr34102012_256;
  117. case SignatureAlgorithm.gostr34102012_512:
  118. return CryptoSignatureAlgorithm.gostr34102012_512;
  119. default:
  120. throw new ArgumentException("specified SignatureAlgorithm invalid: "
  121. + SignatureAlgorithm.GetText(signatureAlgorithm));
  122. }
  123. }
  124. /// <exception cref="IOException"/>
  125. public static TlsSecret HkdfExpandLabel(TlsSecret secret, int cryptoHashAlgorithm, string label,
  126. byte[] context, int length)
  127. {
  128. int labelLength = label.Length;
  129. if (labelLength < 1)
  130. throw new TlsFatalAlert(AlertDescription.internal_error);
  131. int contextLength = context.Length;
  132. int expandedLabelLength = Tls13Prefix.Length + labelLength;
  133. byte[] hkdfLabel = new byte[2 + (1 + expandedLabelLength) + (1 + contextLength)];
  134. // uint16 length
  135. {
  136. TlsUtilities.CheckUint16(length);
  137. TlsUtilities.WriteUint16(length, hkdfLabel, 0);
  138. }
  139. // opaque label<7..255>
  140. {
  141. TlsUtilities.CheckUint8(expandedLabelLength);
  142. TlsUtilities.WriteUint8(expandedLabelLength, hkdfLabel, 2);
  143. Array.Copy(Tls13Prefix, 0, hkdfLabel, 2 + 1, Tls13Prefix.Length);
  144. int labelPos = 2 + (1 + Tls13Prefix.Length);
  145. for (int i = 0; i < labelLength; ++i)
  146. {
  147. char c = label[i];
  148. hkdfLabel[labelPos + i] = (byte)c;
  149. }
  150. }
  151. // context
  152. {
  153. TlsUtilities.WriteOpaque8(context, hkdfLabel, 2 + (1 + expandedLabelLength));
  154. }
  155. return secret.HkdfExpand(cryptoHashAlgorithm, hkdfLabel, length);
  156. }
  157. }
  158. }
  159. #pragma warning restore
  160. #endif