TlsCrypto.cs 11 KB

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