TlsRsaSigner.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Encodings;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  12. {
  13. public class TlsRsaSigner
  14. : AbstractTlsSigner
  15. {
  16. public override byte[] GenerateRawSignature(SignatureAndHashAlgorithm algorithm,
  17. AsymmetricKeyParameter privateKey, byte[] hash)
  18. {
  19. ISigner signer = MakeSigner(algorithm, true, true,
  20. new ParametersWithRandom(privateKey, this.mContext.SecureRandom));
  21. signer.BlockUpdate(hash, 0, hash.Length);
  22. return signer.GenerateSignature();
  23. }
  24. public override bool VerifyRawSignature(SignatureAndHashAlgorithm algorithm, byte[] sigBytes,
  25. AsymmetricKeyParameter publicKey, byte[] hash)
  26. {
  27. ISigner signer = MakeSigner(algorithm, true, false, publicKey);
  28. signer.BlockUpdate(hash, 0, hash.Length);
  29. return signer.VerifySignature(sigBytes);
  30. }
  31. public override ISigner CreateSigner(SignatureAndHashAlgorithm algorithm, AsymmetricKeyParameter privateKey)
  32. {
  33. return MakeSigner(algorithm, false, true, new ParametersWithRandom(privateKey, this.mContext.SecureRandom));
  34. }
  35. public override ISigner CreateVerifyer(SignatureAndHashAlgorithm algorithm, AsymmetricKeyParameter publicKey)
  36. {
  37. return MakeSigner(algorithm, false, false, publicKey);
  38. }
  39. public override bool IsValidPublicKey(AsymmetricKeyParameter publicKey)
  40. {
  41. return publicKey is RsaKeyParameters && !publicKey.IsPrivate;
  42. }
  43. protected virtual ISigner MakeSigner(SignatureAndHashAlgorithm algorithm, bool raw, bool forSigning,
  44. ICipherParameters cp)
  45. {
  46. if ((algorithm != null) != TlsUtilities.IsTlsV12(mContext))
  47. throw new InvalidOperationException();
  48. if (algorithm != null && algorithm.Signature != SignatureAlgorithm.rsa)
  49. throw new InvalidOperationException();
  50. IDigest d;
  51. if (raw)
  52. {
  53. d = new NullDigest();
  54. }
  55. else if (algorithm == null)
  56. {
  57. d = new CombinedHash();
  58. }
  59. else
  60. {
  61. d = TlsUtilities.CreateHash(algorithm.Hash);
  62. }
  63. ISigner s;
  64. if (algorithm != null)
  65. {
  66. /*
  67. * RFC 5246 4.7. In RSA signing, the opaque vector contains the signature generated
  68. * using the RSASSA-PKCS1-v1_5 signature scheme defined in [PKCS1].
  69. */
  70. s = new RsaDigestSigner(d, TlsUtilities.GetOidForHashAlgorithm(algorithm.Hash));
  71. }
  72. else
  73. {
  74. /*
  75. * RFC 5246 4.7. Note that earlier versions of TLS used a different RSA signature scheme
  76. * that did not include a DigestInfo encoding.
  77. */
  78. s = new GenericSigner(CreateRsaImpl(), d);
  79. }
  80. s.Init(forSigning, cp);
  81. return s;
  82. }
  83. protected virtual IAsymmetricBlockCipher CreateRsaImpl()
  84. {
  85. /*
  86. * RFC 5246 7.4.7.1. Implementation note: It is now known that remote timing-based attacks
  87. * on TLS are possible, at least when the client and server are on the same LAN.
  88. * Accordingly, implementations that use static RSA keys MUST use RSA blinding or some other
  89. * anti-timing technique, as described in [TIMING].
  90. */
  91. return new Pkcs1Encoding(new RsaBlindedEngine());
  92. }
  93. }
  94. }
  95. #pragma warning restore
  96. #endif