TlsECDHKeyExchange.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. /// <summary>(D)TLS ECDH key exchange (see RFC 4492).</summary>
  9. public class TlsECDHKeyExchange
  10. : AbstractTlsKeyExchange
  11. {
  12. private static int CheckKeyExchange(int keyExchange)
  13. {
  14. switch (keyExchange)
  15. {
  16. case KeyExchangeAlgorithm.ECDH_ECDSA:
  17. case KeyExchangeAlgorithm.ECDH_RSA:
  18. return keyExchange;
  19. default:
  20. throw new ArgumentException("unsupported key exchange algorithm", "keyExchange");
  21. }
  22. }
  23. protected TlsCredentialedAgreement m_agreementCredentials;
  24. protected TlsCertificate m_ecdhPeerCertificate;
  25. public TlsECDHKeyExchange(int keyExchange)
  26. : base(CheckKeyExchange(keyExchange))
  27. {
  28. }
  29. public override void SkipServerCredentials()
  30. {
  31. throw new TlsFatalAlert(AlertDescription.internal_error);
  32. }
  33. public override void ProcessServerCredentials(TlsCredentials serverCredentials)
  34. {
  35. this.m_agreementCredentials = TlsUtilities.RequireAgreementCredentials(serverCredentials);
  36. }
  37. public override void ProcessServerCertificate(Certificate serverCertificate)
  38. {
  39. this.m_ecdhPeerCertificate = serverCertificate.GetCertificateAt(0).CheckUsageInRole(
  40. TlsCertificateRole.ECDH);
  41. }
  42. public override short[] GetClientCertificateTypes()
  43. {
  44. /*
  45. * RFC 4492 3. [...] The ECDSA_fixed_ECDH and RSA_fixed_ECDH mechanisms are usable with
  46. * ECDH_ECDSA and ECDH_RSA. Their use with ECDHE_ECDSA and ECDHE_RSA is prohibited because
  47. * the use of a long-term ECDH client key would jeopardize the forward secrecy property of
  48. * these algorithms.
  49. */
  50. return new short[]{ ClientCertificateType.ecdsa_fixed_ecdh, ClientCertificateType.rsa_fixed_ecdh };
  51. }
  52. public override void SkipClientCredentials()
  53. {
  54. throw new TlsFatalAlert(AlertDescription.unexpected_message);
  55. }
  56. public override void ProcessClientCredentials(TlsCredentials clientCredentials)
  57. {
  58. this.m_agreementCredentials = TlsUtilities.RequireAgreementCredentials(clientCredentials);
  59. }
  60. public override void GenerateClientKeyExchange(Stream output)
  61. {
  62. // In this case, the Client Key Exchange message will be sent, but will be empty.
  63. }
  64. public override void ProcessClientCertificate(Certificate clientCertificate)
  65. {
  66. this.m_ecdhPeerCertificate = clientCertificate.GetCertificateAt(0).CheckUsageInRole(
  67. TlsCertificateRole.ECDH);
  68. }
  69. public override void ProcessClientKeyExchange(Stream input)
  70. {
  71. // For ecdsa_fixed_ecdh and rsa_fixed_ecdh, the key arrived in the client certificate
  72. }
  73. public override bool RequiresCertificateVerify
  74. {
  75. get { return false; }
  76. }
  77. public override TlsSecret GeneratePreMasterSecret()
  78. {
  79. return m_agreementCredentials.GenerateAgreement(m_ecdhPeerCertificate);
  80. }
  81. }
  82. }
  83. #pragma warning restore
  84. #endif