DefaultTlsEncryptionCredentials.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  8. {
  9. public class DefaultTlsEncryptionCredentials
  10. : AbstractTlsEncryptionCredentials
  11. {
  12. protected readonly TlsContext mContext;
  13. protected readonly Certificate mCertificate;
  14. protected readonly AsymmetricKeyParameter mPrivateKey;
  15. public DefaultTlsEncryptionCredentials(TlsContext context, Certificate certificate,
  16. AsymmetricKeyParameter privateKey)
  17. {
  18. if (certificate == null)
  19. throw new ArgumentNullException("certificate");
  20. if (certificate.IsEmpty)
  21. throw new ArgumentException("cannot be empty", "certificate");
  22. if (privateKey == null)
  23. throw new ArgumentNullException("'privateKey' cannot be null");
  24. if (!privateKey.IsPrivate)
  25. throw new ArgumentException("must be private", "privateKey");
  26. if (privateKey is RsaKeyParameters)
  27. {
  28. }
  29. else
  30. {
  31. throw new ArgumentException("type not supported: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(privateKey), "privateKey");
  32. }
  33. this.mContext = context;
  34. this.mCertificate = certificate;
  35. this.mPrivateKey = privateKey;
  36. }
  37. public override Certificate Certificate
  38. {
  39. get { return mCertificate; }
  40. }
  41. /// <exception cref="IOException"></exception>
  42. public override byte[] DecryptPreMasterSecret(byte[] encryptedPreMasterSecret)
  43. {
  44. return TlsRsaUtilities.SafeDecryptPreMasterSecret(mContext, (RsaKeyParameters)mPrivateKey, encryptedPreMasterSecret);
  45. }
  46. }
  47. }
  48. #pragma warning restore
  49. #endif