TlsSecret.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto
  6. {
  7. /// <summary>Interface supporting the generation of key material and other SSL/TLS secret values from PRFs.
  8. /// </summary>
  9. public interface TlsSecret
  10. {
  11. /// <summary>Calculate an HMAC with this secret's data as the key.</summary>
  12. /// <param name="cryptoHashAlgorithm">the hash algorithm to instantiate HMAC with. See
  13. /// <see cref="CryptoHashAlgorithm"/> for values.</param>
  14. /// <param name="buf">array containing the input data.</param>
  15. /// <param name="off">offset into the input array the input starts at.</param>
  16. /// <param name="len">the length of the input data.</param>
  17. byte[] CalculateHmac(int cryptoHashAlgorithm, byte[] buf, int off, int len);
  18. /// <summary>Return a new secret based on applying a PRF to this one.</summary>
  19. /// <param name="prfAlgorithm">PRF algorithm to use.</param>
  20. /// <param name="label">the label details.</param>
  21. /// <param name="seed">the seed details.</param>
  22. /// <param name="length">the size (in bytes) of the secret to generate.</param>
  23. /// <returns>the new secret.</returns>
  24. TlsSecret DeriveUsingPrf(int prfAlgorithm, string label, byte[] seed, int length);
  25. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  26. TlsSecret DeriveUsingPrf(int prfAlgorithm, ReadOnlySpan<char> label, ReadOnlySpan<byte> seed, int length);
  27. #endif
  28. /// <summary>Destroy the internal state of the secret.</summary>
  29. /// <remarks>
  30. /// After this call, any attempt to use the <see cref="TlsSecret"/> will result in an
  31. /// <see cref="InvalidOperationException"/> being thrown.
  32. /// </remarks>
  33. void Destroy();
  34. /// <summary>Return an encrypted copy of the data this secret is based on.</summary>
  35. /// <param name="encryptor">the encryptor to use for protecting the internal data.</param>
  36. /// <returns>an encrypted copy of this secret's internal data.</returns>
  37. /// <exception cref="IOException"/>
  38. byte[] Encrypt(TlsEncryptor encryptor);
  39. /// <summary>Return the internal data from this secret.</summary>
  40. /// <remarks>
  41. /// The <see cref="TlsSecret"/> does not keep a copy of the data. After this call, any attempt to use the
  42. /// <see cref="TlsSecret"/> will result in an <see cref="InvalidOperationException"/> being thrown.
  43. /// </remarks>
  44. /// <returns>the secret's internal data.</returns>
  45. byte[] Extract();
  46. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  47. void ExtractTo(Span<byte> output);
  48. #endif
  49. /// <summary>RFC 5869 HKDF-Expand function, with this secret's data as the pseudo-random key ('prk').</summary>
  50. /// <param name="cryptoHashAlgorithm">the hash algorithm to instantiate HMAC with. See
  51. /// <see cref="CryptoHashAlgorithm"/> for values.</param>
  52. /// <param name="info">optional context and application specific information (can be zero-length).</param>
  53. /// <param name="length">length of output keying material in octets.</param>
  54. /// <returns> output keying material (of 'length' octets).</returns>
  55. TlsSecret HkdfExpand(int cryptoHashAlgorithm, byte[] info, int length);
  56. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  57. TlsSecret HkdfExpand(int cryptoHashAlgorithm, ReadOnlySpan<byte> info, int length);
  58. #endif
  59. /// <summary>RFC 5869 HKDF-Extract function, with this secret's data as the 'salt'.</summary>
  60. /// <remarks>
  61. /// The <see cref="TlsSecret"/> does not keep a copy of the data. After this call, any attempt to use
  62. /// the <see cref="TlsSecret"/> will result in an <see cref="InvalidOperationException"/> being thrown.
  63. /// </remarks>
  64. /// <param name="cryptoHashAlgorithm">the hash algorithm to instantiate HMAC with. See
  65. /// <see cref="CryptoHashAlgorithm"/> for values.</param>
  66. /// <param name="ikm">input keying material.</param>
  67. /// <returns>a pseudo-random key (of HashLen octets).</returns>
  68. TlsSecret HkdfExtract(int cryptoHashAlgorithm, TlsSecret ikm);
  69. bool IsAlive();
  70. int Length { get; }
  71. }
  72. }
  73. #pragma warning restore
  74. #endif