AbstractTlsKeyExchange.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  7. {
  8. public abstract class AbstractTlsKeyExchange
  9. : TlsKeyExchange
  10. {
  11. protected readonly int mKeyExchange;
  12. protected IList mSupportedSignatureAlgorithms;
  13. protected TlsContext mContext;
  14. protected AbstractTlsKeyExchange(int keyExchange, IList supportedSignatureAlgorithms)
  15. {
  16. this.mKeyExchange = keyExchange;
  17. this.mSupportedSignatureAlgorithms = supportedSignatureAlgorithms;
  18. }
  19. protected virtual DigitallySigned ParseSignature(Stream input)
  20. {
  21. DigitallySigned signature = DigitallySigned.Parse(mContext, input);
  22. SignatureAndHashAlgorithm signatureAlgorithm = signature.Algorithm;
  23. if (signatureAlgorithm != null)
  24. {
  25. TlsUtilities.VerifySupportedSignatureAlgorithm(mSupportedSignatureAlgorithms, signatureAlgorithm);
  26. }
  27. return signature;
  28. }
  29. public virtual void Init(TlsContext context)
  30. {
  31. this.mContext = context;
  32. ProtocolVersion clientVersion = context.ClientVersion;
  33. if (TlsUtilities.IsSignatureAlgorithmsExtensionAllowed(clientVersion))
  34. {
  35. /*
  36. * RFC 5246 7.4.1.4.1. If the client does not send the signature_algorithms extension,
  37. * the server MUST do the following:
  38. *
  39. * - If the negotiated key exchange algorithm is one of (RSA, DHE_RSA, DH_RSA, RSA_PSK,
  40. * ECDH_RSA, ECDHE_RSA), behave as if client had sent the value {sha1,rsa}.
  41. *
  42. * - If the negotiated key exchange algorithm is one of (DHE_DSS, DH_DSS), behave as if
  43. * the client had sent the value {sha1,dsa}.
  44. *
  45. * - If the negotiated key exchange algorithm is one of (ECDH_ECDSA, ECDHE_ECDSA),
  46. * behave as if the client had sent value {sha1,ecdsa}.
  47. */
  48. if (this.mSupportedSignatureAlgorithms == null)
  49. {
  50. switch (mKeyExchange)
  51. {
  52. case KeyExchangeAlgorithm.DH_DSS:
  53. case KeyExchangeAlgorithm.DHE_DSS:
  54. case KeyExchangeAlgorithm.SRP_DSS:
  55. {
  56. this.mSupportedSignatureAlgorithms = TlsUtilities.GetDefaultDssSignatureAlgorithms();
  57. break;
  58. }
  59. case KeyExchangeAlgorithm.ECDH_ECDSA:
  60. case KeyExchangeAlgorithm.ECDHE_ECDSA:
  61. {
  62. this.mSupportedSignatureAlgorithms = TlsUtilities.GetDefaultECDsaSignatureAlgorithms();
  63. break;
  64. }
  65. case KeyExchangeAlgorithm.DH_RSA:
  66. case KeyExchangeAlgorithm.DHE_RSA:
  67. case KeyExchangeAlgorithm.ECDH_RSA:
  68. case KeyExchangeAlgorithm.ECDHE_RSA:
  69. case KeyExchangeAlgorithm.RSA:
  70. case KeyExchangeAlgorithm.RSA_PSK:
  71. case KeyExchangeAlgorithm.SRP_RSA:
  72. {
  73. this.mSupportedSignatureAlgorithms = TlsUtilities.GetDefaultRsaSignatureAlgorithms();
  74. break;
  75. }
  76. case KeyExchangeAlgorithm.DHE_PSK:
  77. case KeyExchangeAlgorithm.ECDHE_PSK:
  78. case KeyExchangeAlgorithm.PSK:
  79. case KeyExchangeAlgorithm.SRP:
  80. break;
  81. default:
  82. throw new InvalidOperationException("unsupported key exchange algorithm");
  83. }
  84. }
  85. }
  86. else if (this.mSupportedSignatureAlgorithms != null)
  87. {
  88. throw new InvalidOperationException("supported_signature_algorithms not allowed for " + clientVersion);
  89. }
  90. }
  91. public abstract void SkipServerCredentials();
  92. public virtual void ProcessServerCertificate(Certificate serverCertificate)
  93. {
  94. if (mSupportedSignatureAlgorithms == null)
  95. {
  96. /*
  97. * TODO RFC 2246 7.4.2. Unless otherwise specified, the signing algorithm for the
  98. * certificate must be the same as the algorithm for the certificate key.
  99. */
  100. }
  101. else
  102. {
  103. /*
  104. * TODO RFC 5246 7.4.2. If the client provided a "signature_algorithms" extension, then
  105. * all certificates provided by the server MUST be signed by a hash/signature algorithm
  106. * pair that appears in that extension.
  107. */
  108. }
  109. }
  110. public virtual void ProcessServerCredentials(TlsCredentials serverCredentials)
  111. {
  112. ProcessServerCertificate(serverCredentials.Certificate);
  113. }
  114. public virtual bool RequiresServerKeyExchange
  115. {
  116. get { return false; }
  117. }
  118. public virtual byte[] GenerateServerKeyExchange()
  119. {
  120. if (RequiresServerKeyExchange)
  121. throw new TlsFatalAlert(AlertDescription.internal_error);
  122. return null;
  123. }
  124. public virtual void SkipServerKeyExchange()
  125. {
  126. if (RequiresServerKeyExchange)
  127. throw new TlsFatalAlert(AlertDescription.unexpected_message);
  128. }
  129. public virtual void ProcessServerKeyExchange(Stream input)
  130. {
  131. if (!RequiresServerKeyExchange)
  132. {
  133. throw new TlsFatalAlert(AlertDescription.unexpected_message);
  134. }
  135. }
  136. public abstract void ValidateCertificateRequest(CertificateRequest certificateRequest);
  137. public virtual void SkipClientCredentials()
  138. {
  139. }
  140. public abstract void ProcessClientCredentials(TlsCredentials clientCredentials);
  141. public virtual void ProcessClientCertificate(Certificate clientCertificate)
  142. {
  143. }
  144. public abstract void GenerateClientKeyExchange(Stream output);
  145. public virtual void ProcessClientKeyExchange(Stream input)
  146. {
  147. // Key exchange implementation MUST support client key exchange
  148. throw new TlsFatalAlert(AlertDescription.internal_error);
  149. }
  150. public abstract byte[] GeneratePremasterSecret();
  151. }
  152. }
  153. #pragma warning restore
  154. #endif