TlsCrypto.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto
  9. {
  10. /// <summary>Service and object creation interface for the primitive types and services that are associated with
  11. /// cryptography in the API.</summary>
  12. public interface TlsCrypto
  13. {
  14. /// <summary>Return true if this TlsCrypto would use a stream verifier for any of the passed in algorithms.
  15. /// </summary>
  16. /// <remarks>This method is only relevant to handshakes negotiating (D)TLS 1.2.</remarks>
  17. /// <param name="signatureAndHashAlgorithms">A <see cref="IList{T}">list</see> of
  18. /// <see cref="SignatureAndHashAlgorithm"/> values.</param>
  19. /// <returns>true if this instance would use a stream verifier for any of the passed in algorithms, otherwise
  20. /// false.</returns>
  21. bool HasAnyStreamVerifiers(IList<SignatureAndHashAlgorithm> signatureAndHashAlgorithms);
  22. /// <summary>Return true if this TlsCrypto would use a stream verifier for any of the passed in algorithms.
  23. /// </summary>
  24. /// <remarks>This method is only relevant to handshakes negotiating (D)TLS versions older than 1.2.</remarks>
  25. /// <param name="clientCertificateTypes">An array of <see cref="ClientCertificateType"/> values.</param>
  26. /// <returns>true if this instance would use a stream verifier for any of the passed in algorithms, otherwise
  27. /// false.</returns>
  28. bool HasAnyStreamVerifiersLegacy(short[] clientCertificateTypes);
  29. /// <summary>Return true if this TlsCrypto can support the passed in hash algorithm.</summary>
  30. /// <param name="cryptoHashAlgorithm">the algorithm of interest.</param>
  31. /// <returns>true if cryptoHashAlgorithm is supported, false otherwise.</returns>
  32. bool HasCryptoHashAlgorithm(int cryptoHashAlgorithm);
  33. /// <summary>Return true if this TlsCrypto can support the passed in signature algorithm (not necessarily in
  34. /// combination with EVERY hash algorithm).</summary>
  35. /// <param name="cryptoSignatureAlgorithm">the algorithm of interest.</param>
  36. /// <returns>true if cryptoSignatureAlgorithm is supported, false otherwise.</returns>
  37. bool HasCryptoSignatureAlgorithm(int cryptoSignatureAlgorithm);
  38. /// <summary>Return true if this TlsCrypto can support DH key agreement.</summary>
  39. /// <returns>true if this instance can support DH key agreement, false otherwise.</returns>
  40. bool HasDHAgreement();
  41. /// <summary>Return true if this TlsCrypto can support ECDH key agreement.</summary>
  42. /// <returns>true if this instance can support ECDH key agreement, false otherwise.</returns>
  43. bool HasECDHAgreement();
  44. /// <summary>Return true if this TlsCrypto can support the passed in block/stream encryption algorithm.
  45. /// </summary>
  46. /// <param name="encryptionAlgorithm">the algorithm of interest.</param>
  47. /// <returns>true if encryptionAlgorithm is supported, false otherwise.</returns>
  48. bool HasEncryptionAlgorithm(int encryptionAlgorithm);
  49. /// <summary>Return true if this TlsCrypto can support HKDF with the passed in hash algorithm.</summary>
  50. /// <param name="cryptoHashAlgorithm">the algorithm of interest.</param>
  51. /// <returns>true if HKDF is supported with cryptoHashAlgorithm, false otherwise.</returns>
  52. bool HasHkdfAlgorithm(int cryptoHashAlgorithm);
  53. /// <summary>Return true if this TlsCrypto can support the passed in MAC algorithm.</summary>
  54. /// <param name="macAlgorithm">the algorithm of interest.</param>
  55. /// <returns>true if macAlgorithm is supported, false otherwise.</returns>
  56. bool HasMacAlgorithm(int macAlgorithm);
  57. /// <summary>Return true if this TlsCrypto supports the passed in <see cref="NamedGroup">named group</see>
  58. /// value.</summary>
  59. /// <returns>true if this instance supports the passed in <see cref="NamedGroup">named group</see> value.
  60. /// </returns>
  61. bool HasNamedGroup(int namedGroup);
  62. /// <summary>Return true if this TlsCrypto can support RSA encryption/decryption.</summary>
  63. /// <returns>true if this instance can support RSA encryption/decryption, false otherwise.</returns>
  64. bool HasRsaEncryption();
  65. /// <summary>Return true if this TlsCrypto can support the passed in signature algorithm (not necessarily in
  66. /// combination with EVERY hash algorithm).</summary>
  67. /// <returns>true if signatureAlgorithm is supported, false otherwise.</returns>
  68. bool HasSignatureAlgorithm(short signatureAlgorithm);
  69. /// <summary>Return true if this TlsCrypto can support the passed in signature algorithm.</summary>
  70. /// <param name="sigAndHashAlgorithm">the algorithm of interest.</param>
  71. /// <returns>true if sigAndHashAlgorithm is supported, false otherwise.</returns>
  72. bool HasSignatureAndHashAlgorithm(SignatureAndHashAlgorithm sigAndHashAlgorithm);
  73. /// <summary>Return true if this TlsCrypto can support the passed in signature scheme.</summary>
  74. /// <param name="signatureScheme">the scheme of interest.</param>
  75. /// <returns>true if signatureScheme is supported, false otherwise.</returns>
  76. bool HasSignatureScheme(int signatureScheme);
  77. /// <summary>Return true if this TlsCrypto can support SRP authentication.</summary>
  78. /// <returns>true if this instance can support SRP authentication, false otherwise.</returns>
  79. bool HasSrpAuthentication();
  80. /// <summary>Create a TlsSecret object based on provided data.</summary>
  81. /// <param name="data">the data to base the TlsSecret on.</param>
  82. /// <returns>a TlsSecret based on the provided data.</returns>
  83. TlsSecret CreateSecret(byte[] data);
  84. /// <summary>Create a TlsSecret object containing a randomly-generated RSA PreMasterSecret</summary>
  85. /// <param name="clientVersion">the client version to place in the first 2 bytes</param>
  86. /// <returns>a TlsSecret containing the PreMasterSecret.</returns>
  87. TlsSecret GenerateRsaPreMasterSecret(ProtocolVersion clientVersion);
  88. /// <summary>Return the primary (safest) SecureRandom for this crypto.</summary>
  89. /// <returns>a SecureRandom suitable for key generation.</returns>
  90. SecureRandom SecureRandom { get; }
  91. /// <summary>Create a TlsCertificate from an ASN.1 binary encoding of an X.509 certificate.</summary>
  92. /// <param name="encoding">DER/BER encoding of the certificate of interest.</param>
  93. /// <returns>a TlsCertificate.</returns>
  94. /// <exception cref="IOException">if there is an issue on decoding or constructing the certificate.</exception>
  95. TlsCertificate CreateCertificate(byte[] encoding);
  96. /// <summary>Create a TlsCertificate from an ASN.1 binary encoding of a certificate.</summary>
  97. /// <param name="type">Certificate type as per IANA TLS Certificate Types registry.</param>
  98. /// <param name="encoding">DER/BER encoding of the certificate of interest.</param>
  99. /// <returns>a TlsCertificate.</returns>
  100. /// <exception cref="IOException">if there is an issue on decoding or constructing the certificate.</exception>
  101. TlsCertificate CreateCertificate(short type, byte[] encoding);
  102. /// <summary>Create a cipher for the specified encryption and MAC algorithms.</summary>
  103. /// <remarks>
  104. /// See enumeration classes <see cref="EncryptionAlgorithm"/>, <see cref="MacAlgorithm"/> for appropriate
  105. /// argument values.
  106. /// </remarks>
  107. /// <param name="cryptoParams">context specific parameters.</param>
  108. /// <param name="encryptionAlgorithm">the encryption algorithm to be employed by the cipher.</param>
  109. /// <param name="macAlgorithm">the MAC algorithm to be employed by the cipher.</param>
  110. /// <returns>a <see cref="TlsCipher"/> implementing the encryption and MAC algorithms.</returns>
  111. /// <exception cref="IOException"/>
  112. TlsCipher CreateCipher(TlsCryptoParameters cryptoParams, int encryptionAlgorithm, int macAlgorithm);
  113. /// <summary>Create a domain object supporting the domain parameters described in dhConfig.</summary>
  114. /// <param name="dhConfig">the config describing the DH parameters to use.</param>
  115. /// <returns>a TlsDHDomain supporting the parameters in dhConfig.</returns>
  116. TlsDHDomain CreateDHDomain(TlsDHConfig dhConfig);
  117. /// <summary>Create a domain object supporting the domain parameters described in ecConfig.</summary>
  118. /// <param name="ecConfig">the config describing the EC parameters to use.</param>
  119. /// <returns>a TlsECDomain supporting the parameters in ecConfig.</returns>
  120. TlsECDomain CreateECDomain(TlsECConfig ecConfig);
  121. /// <summary>Adopt the passed in secret, creating a new copy of it.</summary>
  122. /// <param name="secret">the secret to make a copy of.</param>
  123. /// <returns>a TlsSecret based on the original secret.</returns>
  124. TlsSecret AdoptSecret(TlsSecret secret);
  125. /// <summary>Create a suitable hash for the hash algorithm identifier passed in.</summary>
  126. /// <remarks>
  127. /// See enumeration class <see cref="CryptoHashAlgorithm"/> for appropriate argument values.
  128. /// </remarks>
  129. /// <param name="cryptoHashAlgorithm">the hash algorithm the hash needs to implement.</param>
  130. /// <returns>a <see cref="TlsHash"/>.</returns>
  131. TlsHash CreateHash(int cryptoHashAlgorithm);
  132. /// <summary>Create a suitable HMAC for the MAC algorithm identifier passed in.</summary>
  133. /// <remarks>
  134. /// See enumeration class <see cref="MacAlgorithm"/> for appropriate argument values.
  135. /// </remarks>
  136. /// <param name="macAlgorithm">the MAC algorithm the HMAC needs to match.</param>
  137. /// <returns>a <see cref="TlsHmac"/>.</returns>
  138. TlsHmac CreateHmac(int macAlgorithm);
  139. /// <summary>Create a suitable HMAC using the hash algorithm identifier passed in.</summary>
  140. /// <remarks>
  141. /// See enumeration class <see cref="CryptoHashAlgorithm"/> for appropriate argument values.
  142. /// </remarks>
  143. /// <param name="cryptoHashAlgorithm">the hash algorithm the HMAC should use.</param>
  144. /// <returns>a <see cref="TlsHmac"/>.</returns>
  145. TlsHmac CreateHmacForHash(int cryptoHashAlgorithm);
  146. /// <summary>Create a nonce generator.</summary>
  147. /// <remarks>
  148. /// Each call should construct a new generator, and the generator should be returned from this call only after
  149. /// automatically seeding from this <see cref="TlsCrypto"/>'s entropy source, and from the provided additional
  150. /// seed material. The output of each returned generator must be completely independent of the others.
  151. /// </remarks>
  152. /// <param name="additionalSeedMaterial">context-specific seed material</param>
  153. /// <returns>a <see cref="TlsNonceGenerator"/>.</returns>
  154. TlsNonceGenerator CreateNonceGenerator(byte[] additionalSeedMaterial);
  155. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  156. TlsNonceGenerator CreateNonceGenerator(ReadOnlySpan<byte> additionalSeedMaterial);
  157. #endif
  158. /// <summary>Create an SRP-6 client.</summary>
  159. /// <param name="srpConfig">client config.</param>
  160. /// <returns>an initialised SRP6 client object.</returns>
  161. TlsSrp6Client CreateSrp6Client(TlsSrpConfig srpConfig);
  162. /// <summary>Create an SRP-6 server.</summary>
  163. /// <param name="srpConfig">server config.</param>
  164. /// <param name="srpVerifier">the SRP6 verifier value.</param>
  165. /// <returns>an initialised SRP6 server object.</returns>
  166. TlsSrp6Server CreateSrp6Server(TlsSrpConfig srpConfig, BigInteger srpVerifier);
  167. /// <summary>Create an SRP-6 verifier generator.</summary>
  168. /// <param name="srpConfig">generator config.</param>
  169. /// <returns>an initialized SRP6 verifier generator.</returns>
  170. TlsSrp6VerifierGenerator CreateSrp6VerifierGenerator(TlsSrpConfig srpConfig);
  171. /// <summary>Setup an initial "secret" for a chain of HKDF calls (RFC 5869), containing a string of HashLen
  172. /// zeroes.</summary>
  173. /// <param name="cryptoHashAlgorithm">the hash algorithm to instantiate HMAC with. See
  174. /// <see cref="CryptoHashAlgorithm"/> for values.</param>
  175. TlsSecret HkdfInit(int cryptoHashAlgorithm);
  176. }
  177. }
  178. #pragma warning restore
  179. #endif