AbstractTlsContext.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Threading;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  9. {
  10. internal abstract class AbstractTlsContext
  11. : TlsContext
  12. {
  13. private static long counter = Times.NanoTime();
  14. #if NETCF_1_0
  15. private static object counterLock = new object();
  16. private static long NextCounterValue()
  17. {
  18. lock (counterLock)
  19. {
  20. return ++counter;
  21. }
  22. }
  23. #else
  24. private static long NextCounterValue()
  25. {
  26. return Interlocked.Increment(ref counter);
  27. }
  28. #endif
  29. private readonly IRandomGenerator mNonceRandom;
  30. private readonly SecureRandom mSecureRandom;
  31. private readonly SecurityParameters mSecurityParameters;
  32. private ProtocolVersion mClientVersion = null;
  33. private ProtocolVersion mServerVersion = null;
  34. private TlsSession mSession = null;
  35. private object mUserObject = null;
  36. internal AbstractTlsContext(SecureRandom secureRandom, SecurityParameters securityParameters)
  37. {
  38. IDigest d = TlsUtilities.CreateHash(HashAlgorithm.sha256);
  39. byte[] seed = new byte[d.GetDigestSize()];
  40. secureRandom.NextBytes(seed);
  41. this.mNonceRandom = new DigestRandomGenerator(d);
  42. mNonceRandom.AddSeedMaterial(NextCounterValue());
  43. mNonceRandom.AddSeedMaterial(Times.NanoTime());
  44. mNonceRandom.AddSeedMaterial(seed);
  45. this.mSecureRandom = secureRandom;
  46. this.mSecurityParameters = securityParameters;
  47. }
  48. public virtual IRandomGenerator NonceRandomGenerator
  49. {
  50. get { return mNonceRandom; }
  51. }
  52. public virtual SecureRandom SecureRandom
  53. {
  54. get { return mSecureRandom; }
  55. }
  56. public virtual SecurityParameters SecurityParameters
  57. {
  58. get { return mSecurityParameters; }
  59. }
  60. public abstract bool IsServer { get; }
  61. public virtual ProtocolVersion ClientVersion
  62. {
  63. get { return mClientVersion; }
  64. }
  65. internal virtual void SetClientVersion(ProtocolVersion clientVersion)
  66. {
  67. this.mClientVersion = clientVersion;
  68. }
  69. public virtual ProtocolVersion ServerVersion
  70. {
  71. get { return mServerVersion; }
  72. }
  73. internal virtual void SetServerVersion(ProtocolVersion serverVersion)
  74. {
  75. this.mServerVersion = serverVersion;
  76. }
  77. public virtual TlsSession ResumableSession
  78. {
  79. get { return mSession; }
  80. }
  81. internal virtual void SetResumableSession(TlsSession session)
  82. {
  83. this.mSession = session;
  84. }
  85. public virtual object UserObject
  86. {
  87. get { return mUserObject; }
  88. set { this.mUserObject = value; }
  89. }
  90. public virtual byte[] ExportKeyingMaterial(string asciiLabel, byte[] context_value, int length)
  91. {
  92. if (context_value != null && !TlsUtilities.IsValidUint16(context_value.Length))
  93. throw new ArgumentException("must have length less than 2^16 (or be null)", "context_value");
  94. SecurityParameters sp = SecurityParameters;
  95. if (!sp.IsExtendedMasterSecret)
  96. {
  97. /*
  98. * RFC 7627 5.4. If a client or server chooses to continue with a full handshake without
  99. * the extended master secret extension, [..] the client or server MUST NOT export any
  100. * key material based on the new master secret for any subsequent application-level
  101. * authentication. In particular, it MUST disable [RFC5705] [..].
  102. */
  103. throw new InvalidOperationException("cannot export keying material without extended_master_secret");
  104. }
  105. byte[] cr = sp.ClientRandom, sr = sp.ServerRandom;
  106. int seedLength = cr.Length + sr.Length;
  107. if (context_value != null)
  108. {
  109. seedLength += (2 + context_value.Length);
  110. }
  111. byte[] seed = new byte[seedLength];
  112. int seedPos = 0;
  113. Array.Copy(cr, 0, seed, seedPos, cr.Length);
  114. seedPos += cr.Length;
  115. Array.Copy(sr, 0, seed, seedPos, sr.Length);
  116. seedPos += sr.Length;
  117. if (context_value != null)
  118. {
  119. TlsUtilities.WriteUint16(context_value.Length, seed, seedPos);
  120. seedPos += 2;
  121. Array.Copy(context_value, 0, seed, seedPos, context_value.Length);
  122. seedPos += context_value.Length;
  123. }
  124. if (seedPos != seedLength)
  125. throw new InvalidOperationException("error in calculation of seed for export");
  126. return TlsUtilities.PRF(this, sp.MasterSecret, asciiLabel, seed, length);
  127. }
  128. }
  129. }
  130. #pragma warning restore
  131. #endif