TlsDheKeyExchange.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Crypto.Parameters;
  6. using Org.BouncyCastle.Math;
  7. using Org.BouncyCastle.Security;
  8. using Org.BouncyCastle.Utilities.IO;
  9. namespace Org.BouncyCastle.Crypto.Tls
  10. {
  11. public class TlsDheKeyExchange
  12. : TlsDHKeyExchange
  13. {
  14. protected TlsSignerCredentials mServerCredentials = null;
  15. public TlsDheKeyExchange(int keyExchange, IList supportedSignatureAlgorithms, DHParameters dhParameters)
  16. : base(keyExchange, supportedSignatureAlgorithms, dhParameters)
  17. {
  18. }
  19. public override void ProcessServerCredentials(TlsCredentials serverCredentials)
  20. {
  21. if (!(serverCredentials is TlsSignerCredentials))
  22. throw new TlsFatalAlert(AlertDescription.internal_error);
  23. ProcessServerCertificate(serverCredentials.Certificate);
  24. this.mServerCredentials = (TlsSignerCredentials)serverCredentials;
  25. }
  26. public override byte[] GenerateServerKeyExchange()
  27. {
  28. if (this.mDHParameters == null)
  29. throw new TlsFatalAlert(AlertDescription.internal_error);
  30. DigestInputBuffer buf = new DigestInputBuffer();
  31. this.mDHAgreePrivateKey = TlsDHUtilities.GenerateEphemeralServerKeyExchange(mContext.SecureRandom,
  32. this.mDHParameters, buf);
  33. /*
  34. * RFC 5246 4.7. digitally-signed element needs SignatureAndHashAlgorithm from TLS 1.2
  35. */
  36. SignatureAndHashAlgorithm signatureAndHashAlgorithm = TlsUtilities.GetSignatureAndHashAlgorithm(
  37. mContext, mServerCredentials);
  38. IDigest d = TlsUtilities.CreateHash(signatureAndHashAlgorithm);
  39. SecurityParameters securityParameters = mContext.SecurityParameters;
  40. d.BlockUpdate(securityParameters.clientRandom, 0, securityParameters.clientRandom.Length);
  41. d.BlockUpdate(securityParameters.serverRandom, 0, securityParameters.serverRandom.Length);
  42. buf.UpdateDigest(d);
  43. byte[] hash = DigestUtilities.DoFinal(d);
  44. byte[] signature = mServerCredentials.GenerateCertificateSignature(hash);
  45. DigitallySigned signed_params = new DigitallySigned(signatureAndHashAlgorithm, signature);
  46. signed_params.Encode(buf);
  47. return buf.ToArray();
  48. }
  49. public override void ProcessServerKeyExchange(Stream input)
  50. {
  51. SecurityParameters securityParameters = mContext.SecurityParameters;
  52. SignerInputBuffer buf = new SignerInputBuffer();
  53. Stream teeIn = new TeeInputStream(input, buf);
  54. ServerDHParams dhParams = ServerDHParams.Parse(teeIn);
  55. DigitallySigned signed_params = ParseSignature(input);
  56. ISigner signer = InitVerifyer(mTlsSigner, signed_params.Algorithm, securityParameters);
  57. buf.UpdateSigner(signer);
  58. if (!signer.VerifySignature(signed_params.Signature))
  59. throw new TlsFatalAlert(AlertDescription.decrypt_error);
  60. this.mDHAgreePublicKey = TlsDHUtilities.ValidateDHPublicKey(dhParams.PublicKey);
  61. this.mDHParameters = ValidateDHParameters(mDHAgreePublicKey.Parameters);
  62. }
  63. protected virtual ISigner InitVerifyer(TlsSigner tlsSigner, SignatureAndHashAlgorithm algorithm,
  64. SecurityParameters securityParameters)
  65. {
  66. ISigner signer = tlsSigner.CreateVerifyer(algorithm, this.mServerPublicKey);
  67. signer.BlockUpdate(securityParameters.clientRandom, 0, securityParameters.clientRandom.Length);
  68. signer.BlockUpdate(securityParameters.serverRandom, 0, securityParameters.serverRandom.Length);
  69. return signer;
  70. }
  71. }
  72. }
  73. #endif