AbstractTlsClient.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  8. {
  9. public abstract class AbstractTlsClient
  10. : AbstractTlsPeer, TlsClient
  11. {
  12. protected TlsCipherFactory mCipherFactory;
  13. protected TlsClientContext mContext;
  14. protected IList mSupportedSignatureAlgorithms;
  15. protected int[] mNamedCurves;
  16. protected byte[] mClientECPointFormats, mServerECPointFormats;
  17. protected int mSelectedCipherSuite;
  18. protected short mSelectedCompressionMethod;
  19. public System.Collections.Generic.List<string> HostNames { get; set; }
  20. public AbstractTlsClient()
  21. : this(new DefaultTlsCipherFactory())
  22. {
  23. }
  24. public AbstractTlsClient(TlsCipherFactory cipherFactory)
  25. {
  26. this.mCipherFactory = cipherFactory;
  27. }
  28. protected virtual bool AllowUnexpectedServerExtension(int extensionType, byte[] extensionData)
  29. {
  30. switch (extensionType)
  31. {
  32. case ExtensionType.elliptic_curves:
  33. /*
  34. * Exception added based on field reports that some servers do send this, although the
  35. * Supported Elliptic Curves Extension is clearly intended to be client-only. If
  36. * present, we still require that it is a valid EllipticCurveList.
  37. */
  38. TlsEccUtilities.ReadSupportedEllipticCurvesExtension(extensionData);
  39. return true;
  40. case ExtensionType.ec_point_formats:
  41. /*
  42. * Exception added based on field reports that some servers send this even when they
  43. * didn't negotiate an ECC cipher suite. If present, we still require that it is a valid
  44. * ECPointFormatList.
  45. */
  46. TlsEccUtilities.ReadSupportedPointFormatsExtension(extensionData);
  47. return true;
  48. default:
  49. return false;
  50. }
  51. }
  52. protected virtual void CheckForUnexpectedServerExtension(IDictionary serverExtensions, int extensionType)
  53. {
  54. byte[] extensionData = TlsUtilities.GetExtensionData(serverExtensions, extensionType);
  55. if (extensionData != null && !AllowUnexpectedServerExtension(extensionType, extensionData))
  56. {
  57. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  58. }
  59. }
  60. public virtual void Init(TlsClientContext context)
  61. {
  62. this.mContext = context;
  63. }
  64. public virtual TlsSession GetSessionToResume()
  65. {
  66. return null;
  67. }
  68. public virtual ProtocolVersion ClientHelloRecordLayerVersion
  69. {
  70. get
  71. {
  72. // "{03,00}"
  73. //return ProtocolVersion.SSLv3;
  74. // "the lowest version number supported by the client"
  75. //return MinimumVersion;
  76. // "the value of ClientHello.client_version"
  77. return ClientVersion;
  78. }
  79. }
  80. public virtual ProtocolVersion ClientVersion
  81. {
  82. get { return ProtocolVersion.TLSv12; }
  83. }
  84. public virtual bool IsFallback
  85. {
  86. /*
  87. * RFC 7507 4. The TLS_FALLBACK_SCSV cipher suite value is meant for use by clients that
  88. * repeat a connection attempt with a downgraded protocol (perform a "fallback retry") in
  89. * order to work around interoperability problems with legacy servers.
  90. */
  91. get { return false; }
  92. }
  93. public virtual IDictionary GetClientExtensions()
  94. {
  95. IDictionary clientExtensions = null;
  96. ProtocolVersion clientVersion = mContext.ClientVersion;
  97. /*
  98. * RFC 5246 7.4.1.4.1. Note: this extension is not meaningful for TLS versions prior to 1.2.
  99. * Clients MUST NOT offer it if they are offering prior versions.
  100. */
  101. if (TlsUtilities.IsSignatureAlgorithmsExtensionAllowed(clientVersion))
  102. {
  103. // TODO Provide a way for the user to specify the acceptable hash/signature algorithms.
  104. this.mSupportedSignatureAlgorithms = TlsUtilities.GetDefaultSupportedSignatureAlgorithms();
  105. clientExtensions = TlsExtensionsUtilities.EnsureExtensionsInitialised(clientExtensions);
  106. TlsUtilities.AddSignatureAlgorithmsExtension(clientExtensions, mSupportedSignatureAlgorithms);
  107. }
  108. if (TlsEccUtilities.ContainsEccCipherSuites(GetCipherSuites()))
  109. {
  110. /*
  111. * RFC 4492 5.1. A client that proposes ECC cipher suites in its ClientHello message
  112. * appends these extensions (along with any others), enumerating the curves it supports
  113. * and the point formats it can parse. Clients SHOULD send both the Supported Elliptic
  114. * Curves Extension and the Supported Point Formats Extension.
  115. */
  116. /*
  117. * TODO Could just add all the curves since we support them all, but users may not want
  118. * to use unnecessarily large fields. Need configuration options.
  119. */
  120. this.mNamedCurves = new int[]{ NamedCurve.secp256r1, NamedCurve.secp384r1 };
  121. this.mClientECPointFormats = new byte[]{ ECPointFormat.uncompressed,
  122. ECPointFormat.ansiX962_compressed_prime, ECPointFormat.ansiX962_compressed_char2, };
  123. clientExtensions = TlsExtensionsUtilities.EnsureExtensionsInitialised(clientExtensions);
  124. TlsEccUtilities.AddSupportedEllipticCurvesExtension(clientExtensions, mNamedCurves);
  125. TlsEccUtilities.AddSupportedPointFormatsExtension(clientExtensions, mClientECPointFormats);
  126. }
  127. if (this.HostNames != null && this.HostNames.Count > 0)
  128. {
  129. var list = new System.Collections.Generic.List<ServerName>(this.HostNames.Count);
  130. for (int i = 0; i < this.HostNames.Count; ++i)
  131. list.Add(new ServerName(Tls.NameType.host_name, this.HostNames[i]));
  132. TlsExtensionsUtilities.AddServerNameExtension(clientExtensions, new ServerNameList(list));
  133. }
  134. return clientExtensions;
  135. }
  136. public virtual ProtocolVersion MinimumVersion
  137. {
  138. get { return ProtocolVersion.TLSv10; }
  139. }
  140. public virtual void NotifyServerVersion(ProtocolVersion serverVersion)
  141. {
  142. if (!MinimumVersion.IsEqualOrEarlierVersionOf(serverVersion))
  143. throw new TlsFatalAlert(AlertDescription.protocol_version);
  144. }
  145. public abstract int[] GetCipherSuites();
  146. public virtual byte[] GetCompressionMethods()
  147. {
  148. return new byte[]{ CompressionMethod.cls_null };
  149. }
  150. public virtual void NotifySessionID(byte[] sessionID)
  151. {
  152. // Currently ignored
  153. }
  154. public virtual void NotifySelectedCipherSuite(int selectedCipherSuite)
  155. {
  156. this.mSelectedCipherSuite = selectedCipherSuite;
  157. }
  158. public virtual void NotifySelectedCompressionMethod(byte selectedCompressionMethod)
  159. {
  160. this.mSelectedCompressionMethod = selectedCompressionMethod;
  161. }
  162. public virtual void ProcessServerExtensions(IDictionary serverExtensions)
  163. {
  164. /*
  165. * TlsProtocol implementation validates that any server extensions received correspond to
  166. * client extensions sent. By default, we don't send any, and this method is not called.
  167. */
  168. if (serverExtensions != null)
  169. {
  170. /*
  171. * RFC 5246 7.4.1.4.1. Servers MUST NOT send this extension.
  172. */
  173. CheckForUnexpectedServerExtension(serverExtensions, ExtensionType.signature_algorithms);
  174. CheckForUnexpectedServerExtension(serverExtensions, ExtensionType.elliptic_curves);
  175. if (TlsEccUtilities.IsEccCipherSuite(this.mSelectedCipherSuite))
  176. {
  177. this.mServerECPointFormats = TlsEccUtilities.GetSupportedPointFormatsExtension(serverExtensions);
  178. }
  179. else
  180. {
  181. CheckForUnexpectedServerExtension(serverExtensions, ExtensionType.ec_point_formats);
  182. }
  183. /*
  184. * RFC 7685 3. The server MUST NOT echo the extension.
  185. */
  186. CheckForUnexpectedServerExtension(serverExtensions, ExtensionType.padding);
  187. }
  188. }
  189. public virtual void ProcessServerSupplementalData(IList serverSupplementalData)
  190. {
  191. if (serverSupplementalData != null)
  192. throw new TlsFatalAlert(AlertDescription.unexpected_message);
  193. }
  194. public abstract TlsKeyExchange GetKeyExchange();
  195. public abstract TlsAuthentication GetAuthentication();
  196. public virtual IList GetClientSupplementalData()
  197. {
  198. return null;
  199. }
  200. public override TlsCompression GetCompression()
  201. {
  202. switch (mSelectedCompressionMethod)
  203. {
  204. case CompressionMethod.cls_null:
  205. return new TlsNullCompression();
  206. case CompressionMethod.DEFLATE:
  207. return new TlsDeflateCompression();
  208. default:
  209. /*
  210. * Note: internal error here; the TlsProtocol implementation verifies that the
  211. * server-selected compression method was in the list of client-offered compression
  212. * methods, so if we now can't produce an implementation, we shouldn't have offered it!
  213. */
  214. throw new TlsFatalAlert(AlertDescription.internal_error);
  215. }
  216. }
  217. public override TlsCipher GetCipher()
  218. {
  219. int encryptionAlgorithm = TlsUtilities.GetEncryptionAlgorithm(mSelectedCipherSuite);
  220. int macAlgorithm = TlsUtilities.GetMacAlgorithm(mSelectedCipherSuite);
  221. return mCipherFactory.CreateCipher(mContext, encryptionAlgorithm, macAlgorithm);
  222. }
  223. public virtual void NotifyNewSessionTicket(NewSessionTicket newSessionTicket)
  224. {
  225. }
  226. }
  227. }
  228. #pragma warning restore
  229. #endif