DefaultTlsSignerCredentials.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 DefaultTlsSignerCredentials
  10. : AbstractTlsSignerCredentials
  11. {
  12. protected readonly TlsContext mContext;
  13. protected readonly Certificate mCertificate;
  14. protected readonly AsymmetricKeyParameter mPrivateKey;
  15. protected readonly SignatureAndHashAlgorithm mSignatureAndHashAlgorithm;
  16. protected readonly TlsSigner mSigner;
  17. public DefaultTlsSignerCredentials(TlsContext context, Certificate certificate, AsymmetricKeyParameter privateKey)
  18. : this(context, certificate, privateKey, null)
  19. {
  20. }
  21. public DefaultTlsSignerCredentials(TlsContext context, Certificate certificate, AsymmetricKeyParameter privateKey,
  22. SignatureAndHashAlgorithm signatureAndHashAlgorithm)
  23. {
  24. if (certificate == null)
  25. throw new ArgumentNullException("certificate");
  26. if (certificate.IsEmpty)
  27. throw new ArgumentException("cannot be empty", "clientCertificate");
  28. if (privateKey == null)
  29. throw new ArgumentNullException("privateKey");
  30. if (!privateKey.IsPrivate)
  31. throw new ArgumentException("must be private", "privateKey");
  32. if (TlsUtilities.IsTlsV12(context) && signatureAndHashAlgorithm == null)
  33. throw new ArgumentException("cannot be null for (D)TLS 1.2+", "signatureAndHashAlgorithm");
  34. if (privateKey is RsaKeyParameters)
  35. {
  36. mSigner = new TlsRsaSigner();
  37. }
  38. else if (privateKey is DsaPrivateKeyParameters)
  39. {
  40. mSigner = new TlsDssSigner();
  41. }
  42. else if (privateKey is ECPrivateKeyParameters)
  43. {
  44. mSigner = new TlsECDsaSigner();
  45. }
  46. else
  47. {
  48. throw new ArgumentException("type not supported: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(privateKey), "privateKey");
  49. }
  50. this.mSigner.Init(context);
  51. this.mContext = context;
  52. this.mCertificate = certificate;
  53. this.mPrivateKey = privateKey;
  54. this.mSignatureAndHashAlgorithm = signatureAndHashAlgorithm;
  55. }
  56. public override Certificate Certificate
  57. {
  58. get { return mCertificate; }
  59. }
  60. /// <exception cref="IOException"></exception>
  61. public override byte[] GenerateCertificateSignature(byte[] hash)
  62. {
  63. try
  64. {
  65. if (TlsUtilities.IsTlsV12(mContext))
  66. {
  67. return mSigner.GenerateRawSignature(mSignatureAndHashAlgorithm, mPrivateKey, hash);
  68. }
  69. else
  70. {
  71. return mSigner.GenerateRawSignature(mPrivateKey, hash);
  72. }
  73. }
  74. catch (CryptoException e)
  75. {
  76. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  77. }
  78. }
  79. public override SignatureAndHashAlgorithm SignatureAndHashAlgorithm
  80. {
  81. get { return mSignatureAndHashAlgorithm; }
  82. }
  83. }
  84. }
  85. #pragma warning restore
  86. #endif