BcDefaultTlsCredentialedSigner.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /// <summary>Credentialed class for generating signatures based on the use of primitives from the BC light-weight API.</summary>
  10. public class BcDefaultTlsCredentialedSigner
  11. : DefaultTlsCredentialedSigner
  12. {
  13. private static BcTlsCertificate GetEndEntity(BcTlsCrypto crypto, Certificate certificate)
  14. {
  15. if (certificate == null || certificate.IsEmpty)
  16. throw new ArgumentException("No certificate");
  17. return BcTlsCertificate.Convert(crypto, certificate.GetCertificateAt(0));
  18. }
  19. private static TlsSigner MakeSigner(BcTlsCrypto crypto, AsymmetricKeyParameter privateKey,
  20. Certificate certificate, SignatureAndHashAlgorithm signatureAndHashAlgorithm)
  21. {
  22. TlsSigner signer;
  23. if (privateKey is RsaKeyParameters)
  24. {
  25. RsaKeyParameters privKeyRsa = (RsaKeyParameters)privateKey;
  26. if (signatureAndHashAlgorithm != null)
  27. {
  28. int signatureScheme = SignatureScheme.From(signatureAndHashAlgorithm);
  29. if (SignatureScheme.IsRsaPss(signatureScheme))
  30. {
  31. return new BcTlsRsaPssSigner(crypto, privKeyRsa, signatureScheme);
  32. }
  33. }
  34. RsaKeyParameters pubKeyRsa = GetEndEntity(crypto, certificate).GetPubKeyRsa();
  35. signer = new BcTlsRsaSigner(crypto, privKeyRsa, pubKeyRsa);
  36. }
  37. else if (privateKey is DsaPrivateKeyParameters)
  38. {
  39. signer = new BcTlsDsaSigner(crypto, (DsaPrivateKeyParameters)privateKey);
  40. }
  41. else if (privateKey is ECPrivateKeyParameters)
  42. {
  43. ECPrivateKeyParameters privKeyEC = (ECPrivateKeyParameters)privateKey;
  44. if (signatureAndHashAlgorithm != null)
  45. {
  46. int signatureScheme = SignatureScheme.From(signatureAndHashAlgorithm);
  47. if (SignatureScheme.IsECDsa(signatureScheme))
  48. {
  49. return new BcTlsECDsa13Signer(crypto, privKeyEC, signatureScheme);
  50. }
  51. }
  52. signer = new BcTlsECDsaSigner(crypto, privKeyEC);
  53. }
  54. else if (privateKey is Ed25519PrivateKeyParameters)
  55. {
  56. signer = new BcTlsEd25519Signer(crypto, (Ed25519PrivateKeyParameters)privateKey);
  57. }
  58. else if (privateKey is Ed448PrivateKeyParameters)
  59. {
  60. signer = new BcTlsEd448Signer(crypto, (Ed448PrivateKeyParameters)privateKey);
  61. }
  62. else
  63. {
  64. throw new ArgumentException("'privateKey' type not supported: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(privateKey));
  65. }
  66. return signer;
  67. }
  68. public BcDefaultTlsCredentialedSigner(TlsCryptoParameters cryptoParams, BcTlsCrypto crypto,
  69. AsymmetricKeyParameter privateKey, Certificate certificate,
  70. SignatureAndHashAlgorithm signatureAndHashAlgorithm)
  71. : base(cryptoParams, MakeSigner(crypto, privateKey, certificate, signatureAndHashAlgorithm), certificate,
  72. signatureAndHashAlgorithm)
  73. {
  74. }
  75. }
  76. }
  77. #pragma warning restore
  78. #endif