TlsDsaSigner.cs 3.0 KB

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