TlsRsaKeyExchange.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Asn1;
  6. using Org.BouncyCastle.Asn1.X509;
  7. using Org.BouncyCastle.Crypto.Encodings;
  8. using Org.BouncyCastle.Crypto.Engines;
  9. using Org.BouncyCastle.Crypto.Parameters;
  10. using Org.BouncyCastle.Security;
  11. using Org.BouncyCastle.Utilities.IO;
  12. namespace Org.BouncyCastle.Crypto.Tls
  13. {
  14. /// <summary>(D)TLS and SSLv3 RSA key exchange.</summary>
  15. public class TlsRsaKeyExchange
  16. : AbstractTlsKeyExchange
  17. {
  18. protected AsymmetricKeyParameter mServerPublicKey = null;
  19. protected RsaKeyParameters mRsaServerPublicKey = null;
  20. protected TlsEncryptionCredentials mServerCredentials = null;
  21. protected byte[] mPremasterSecret;
  22. public TlsRsaKeyExchange(IList supportedSignatureAlgorithms)
  23. : base(KeyExchangeAlgorithm.RSA, supportedSignatureAlgorithms)
  24. {
  25. }
  26. public override void SkipServerCredentials()
  27. {
  28. throw new TlsFatalAlert(AlertDescription.unexpected_message);
  29. }
  30. public override void ProcessServerCredentials(TlsCredentials serverCredentials)
  31. {
  32. if (!(serverCredentials is TlsEncryptionCredentials))
  33. throw new TlsFatalAlert(AlertDescription.internal_error);
  34. ProcessServerCertificate(serverCredentials.Certificate);
  35. this.mServerCredentials = (TlsEncryptionCredentials)serverCredentials;
  36. }
  37. public override void ProcessServerCertificate(Certificate serverCertificate)
  38. {
  39. if (serverCertificate.IsEmpty)
  40. throw new TlsFatalAlert(AlertDescription.bad_certificate);
  41. X509CertificateStructure x509Cert = serverCertificate.GetCertificateAt(0);
  42. SubjectPublicKeyInfo keyInfo = x509Cert.SubjectPublicKeyInfo;
  43. try
  44. {
  45. this.mServerPublicKey = PublicKeyFactory.CreateKey(keyInfo);
  46. }
  47. catch (Exception e)
  48. {
  49. throw new TlsFatalAlert(AlertDescription.unsupported_certificate, e);
  50. }
  51. // Sanity check the PublicKeyFactory
  52. if (this.mServerPublicKey.IsPrivate)
  53. throw new TlsFatalAlert(AlertDescription.internal_error);
  54. this.mRsaServerPublicKey = ValidateRsaPublicKey((RsaKeyParameters)this.mServerPublicKey);
  55. TlsUtilities.ValidateKeyUsage(x509Cert, KeyUsage.KeyEncipherment);
  56. base.ProcessServerCertificate(serverCertificate);
  57. }
  58. public override void ValidateCertificateRequest(CertificateRequest certificateRequest)
  59. {
  60. byte[] types = certificateRequest.CertificateTypes;
  61. for (int i = 0; i < types.Length; ++i)
  62. {
  63. switch (types[i])
  64. {
  65. case ClientCertificateType.rsa_sign:
  66. case ClientCertificateType.dss_sign:
  67. case ClientCertificateType.ecdsa_sign:
  68. break;
  69. default:
  70. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  71. }
  72. }
  73. }
  74. public override void ProcessClientCredentials(TlsCredentials clientCredentials)
  75. {
  76. if (!(clientCredentials is TlsSignerCredentials))
  77. throw new TlsFatalAlert(AlertDescription.internal_error);
  78. }
  79. public override void GenerateClientKeyExchange(Stream output)
  80. {
  81. this.mPremasterSecret = TlsRsaUtilities.GenerateEncryptedPreMasterSecret(mContext, mRsaServerPublicKey, output);
  82. }
  83. public override void ProcessClientKeyExchange(Stream input)
  84. {
  85. byte[] encryptedPreMasterSecret;
  86. if (TlsUtilities.IsSsl(mContext))
  87. {
  88. // TODO Do any SSLv3 clients actually include the length?
  89. encryptedPreMasterSecret = Streams.ReadAll(input);
  90. }
  91. else
  92. {
  93. encryptedPreMasterSecret = TlsUtilities.ReadOpaque16(input);
  94. }
  95. this.mPremasterSecret = mServerCredentials.DecryptPreMasterSecret(encryptedPreMasterSecret);
  96. }
  97. public override byte[] GeneratePremasterSecret()
  98. {
  99. if (this.mPremasterSecret == null)
  100. throw new TlsFatalAlert(AlertDescription.internal_error);
  101. byte[] tmp = this.mPremasterSecret;
  102. this.mPremasterSecret = null;
  103. return tmp;
  104. }
  105. protected virtual RsaKeyParameters ValidateRsaPublicKey(RsaKeyParameters key)
  106. {
  107. // TODO What is the minimum bit length required?
  108. // key.Modulus.BitLength;
  109. if (!key.Exponent.IsProbablePrime(2))
  110. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  111. return key;
  112. }
  113. }
  114. }
  115. #endif