TlsDsaSigner.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  9. {
  10. public abstract class TlsDsaSigner
  11. : AbstractTlsSigner
  12. {
  13. public override byte[] GenerateRawSignature(SignatureAndHashAlgorithm algorithm,
  14. AsymmetricKeyParameter privateKey, byte[] hash)
  15. {
  16. ISigner signer = MakeSigner(algorithm, true, true,
  17. new ParametersWithRandom(privateKey, this.mContext.SecureRandom));
  18. if (algorithm == null)
  19. {
  20. // Note: Only use the SHA1 part of the (MD5/SHA1) hash
  21. signer.BlockUpdate(hash, 16, 20);
  22. }
  23. else
  24. {
  25. signer.BlockUpdate(hash, 0, hash.Length);
  26. }
  27. return signer.GenerateSignature();
  28. }
  29. public override bool VerifyRawSignature(SignatureAndHashAlgorithm algorithm, byte[] sigBytes,
  30. AsymmetricKeyParameter publicKey, byte[] hash)
  31. {
  32. ISigner signer = MakeSigner(algorithm, true, false, publicKey);
  33. if (algorithm == null)
  34. {
  35. // Note: Only use the SHA1 part of the (MD5/SHA1) hash
  36. signer.BlockUpdate(hash, 16, 20);
  37. }
  38. else
  39. {
  40. signer.BlockUpdate(hash, 0, hash.Length);
  41. }
  42. return signer.VerifySignature(sigBytes);
  43. }
  44. public override ISigner CreateSigner(SignatureAndHashAlgorithm algorithm, AsymmetricKeyParameter privateKey)
  45. {
  46. return MakeSigner(algorithm, false, true, privateKey);
  47. }
  48. public override ISigner CreateVerifyer(SignatureAndHashAlgorithm algorithm, AsymmetricKeyParameter publicKey)
  49. {
  50. return MakeSigner(algorithm, false, false, publicKey);
  51. }
  52. protected virtual ICipherParameters MakeInitParameters(bool forSigning, ICipherParameters cp)
  53. {
  54. return cp;
  55. }
  56. protected virtual ISigner MakeSigner(SignatureAndHashAlgorithm algorithm, bool raw, bool forSigning,
  57. ICipherParameters cp)
  58. {
  59. if ((algorithm != null) != TlsUtilities.IsTlsV12(mContext))
  60. throw new InvalidOperationException();
  61. if (algorithm != null && algorithm.Signature != SignatureAlgorithm)
  62. throw new InvalidOperationException();
  63. byte hashAlgorithm = algorithm == null ? HashAlgorithm.sha1 : algorithm.Hash;
  64. IDigest d = raw ? new NullDigest() : TlsUtilities.CreateHash(hashAlgorithm);
  65. ISigner s = new DsaDigestSigner(CreateDsaImpl(hashAlgorithm), d);
  66. s.Init(forSigning, MakeInitParameters(forSigning, cp));
  67. return s;
  68. }
  69. protected abstract byte SignatureAlgorithm { get; }
  70. protected abstract IDsa CreateDsaImpl(byte hashAlgorithm);
  71. }
  72. }
  73. #pragma warning restore
  74. #endif