TlsSecret.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /// <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. /// <summary>Destroy the internal state of the secret.</summary>
  26. /// <remarks>
  27. /// After this call, any attempt to use the <see cref="TlsSecret"/> will result in an
  28. /// <see cref="InvalidOperationException"/> being thrown.
  29. /// </remarks>
  30. void Destroy();
  31. /// <summary>Return an encrypted copy of the data this secret is based on.</summary>
  32. /// <param name="encryptor">the encryptor to use for protecting the internal data.</param>
  33. /// <returns>an encrypted copy of this secret's internal data.</returns>
  34. /// <exception cref="IOException"/>
  35. byte[] Encrypt(TlsEncryptor encryptor);
  36. /// <summary>Return the internal data from this secret.</summary>
  37. /// <remarks>
  38. /// The <see cref="TlsSecret"/> does not keep a copy of the data. After this call, any attempt to use the
  39. /// <see cref="TlsSecret"/> will result in an <see cref="InvalidOperationException"/> being thrown.
  40. /// </remarks>
  41. /// <returns>the secret's internal data.</returns>
  42. byte[] Extract();
  43. /// <summary>RFC 5869 HKDF-Expand function, with this secret's data as the pseudo-random key ('prk').</summary>
  44. /// <param name="cryptoHashAlgorithm">the hash algorithm to instantiate HMAC with. See
  45. /// <see cref="CryptoHashAlgorithm"/> for values.</param>
  46. /// <param name="info">optional context and application specific information (can be zero-length).</param>
  47. /// <param name="length">length of output keying material in octets.</param>
  48. /// <returns> output keying material (of 'length' octets).</returns>
  49. TlsSecret HkdfExpand(int cryptoHashAlgorithm, byte[] info, int length);
  50. /// <summary>RFC 5869 HKDF-Extract function, with this secret's data as the 'salt'.</summary>
  51. /// <remarks>
  52. /// The <see cref="TlsSecret"/> does not keep a copy of the data. After this call, any attempt to use
  53. /// the <see cref="TlsSecret"/> will result in an <see cref="InvalidOperationException"/> being thrown.
  54. /// </remarks>
  55. /// <param name="cryptoHashAlgorithm">the hash algorithm to instantiate HMAC with. See
  56. /// <see cref="CryptoHashAlgorithm"/> for values.</param>
  57. /// <param name="ikm">input keying material.</param>
  58. /// <returns>a pseudo-random key (of HashLen octets).</returns>
  59. TlsSecret HkdfExtract(int cryptoHashAlgorithm, TlsSecret ikm);
  60. bool IsAlive();
  61. }
  62. }
  63. #pragma warning restore
  64. #endif