BcDefaultTlsCredentialedAgreement.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  8. {
  9. /// <summay>Credentialed class generating agreed secrets from a peer's public key for our end of the TLS connection
  10. /// using the BC light-weight API.</summay>
  11. public class BcDefaultTlsCredentialedAgreement
  12. : TlsCredentialedAgreement
  13. {
  14. protected readonly TlsCredentialedAgreement m_agreementCredentials;
  15. public BcDefaultTlsCredentialedAgreement(BcTlsCrypto crypto, Certificate certificate,
  16. AsymmetricKeyParameter privateKey)
  17. {
  18. if (crypto == null)
  19. throw new ArgumentNullException("crypto");
  20. if (certificate == null)
  21. throw new ArgumentNullException("certificate");
  22. if (certificate.IsEmpty)
  23. throw new ArgumentException("cannot be empty", "certificate");
  24. if (privateKey == null)
  25. throw new ArgumentNullException("privateKey");
  26. if (!privateKey.IsPrivate)
  27. throw new ArgumentException("must be private", "privateKey");
  28. if (privateKey is DHPrivateKeyParameters)
  29. {
  30. this.m_agreementCredentials = new DHCredentialedAgreement(crypto, certificate,
  31. (DHPrivateKeyParameters)privateKey);
  32. }
  33. else if (privateKey is ECPrivateKeyParameters)
  34. {
  35. this.m_agreementCredentials = new ECCredentialedAgreement(crypto, certificate,
  36. (ECPrivateKeyParameters)privateKey);
  37. }
  38. else
  39. {
  40. throw new ArgumentException("'privateKey' type not supported: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(privateKey));
  41. }
  42. }
  43. public virtual Certificate Certificate
  44. {
  45. get { return m_agreementCredentials.Certificate; }
  46. }
  47. public virtual TlsSecret GenerateAgreement(TlsCertificate peerCertificate)
  48. {
  49. return m_agreementCredentials.GenerateAgreement(peerCertificate);
  50. }
  51. private sealed class DHCredentialedAgreement
  52. : TlsCredentialedAgreement
  53. {
  54. private readonly BcTlsCrypto m_crypto;
  55. private readonly Certificate m_certificate;
  56. private readonly DHPrivateKeyParameters m_privateKey;
  57. internal DHCredentialedAgreement(BcTlsCrypto crypto, Certificate certificate,
  58. DHPrivateKeyParameters privateKey)
  59. {
  60. this.m_crypto = crypto;
  61. this.m_certificate = certificate;
  62. this.m_privateKey = privateKey;
  63. }
  64. public TlsSecret GenerateAgreement(TlsCertificate peerCertificate)
  65. {
  66. BcTlsCertificate bcCert = BcTlsCertificate.Convert(m_crypto, peerCertificate);
  67. DHPublicKeyParameters peerPublicKey = bcCert.GetPubKeyDH();
  68. return BcTlsDHDomain.CalculateDHAgreement(m_crypto, m_privateKey, peerPublicKey, false);
  69. }
  70. public Certificate Certificate
  71. {
  72. get { return m_certificate; }
  73. }
  74. }
  75. private sealed class ECCredentialedAgreement
  76. : TlsCredentialedAgreement
  77. {
  78. private readonly BcTlsCrypto m_crypto;
  79. private readonly Certificate m_certificate;
  80. private readonly ECPrivateKeyParameters m_privateKey;
  81. internal ECCredentialedAgreement(BcTlsCrypto crypto, Certificate certificate,
  82. ECPrivateKeyParameters privateKey)
  83. {
  84. this.m_crypto = crypto;
  85. this.m_certificate = certificate;
  86. this.m_privateKey = privateKey;
  87. }
  88. public TlsSecret GenerateAgreement(TlsCertificate peerCertificate)
  89. {
  90. BcTlsCertificate bcCert = BcTlsCertificate.Convert(m_crypto, peerCertificate);
  91. ECPublicKeyParameters peerPublicKey = bcCert.GetPubKeyEC();
  92. return BcTlsECDomain.CalculateBasicAgreement(m_crypto, m_privateKey, peerPublicKey);
  93. }
  94. public Certificate Certificate
  95. {
  96. get { return m_certificate; }
  97. }
  98. }
  99. }
  100. }
  101. #pragma warning restore
  102. #endif