TlsRsaSigner.cs 3.9 KB

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