GenericSigner.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  8. {
  9. public class GenericSigner
  10. : ISigner
  11. {
  12. private readonly IAsymmetricBlockCipher engine;
  13. private readonly IDigest digest;
  14. private bool forSigning;
  15. public GenericSigner(
  16. IAsymmetricBlockCipher engine,
  17. IDigest digest)
  18. {
  19. this.engine = engine;
  20. this.digest = digest;
  21. }
  22. public virtual string AlgorithmName
  23. {
  24. get { return "Generic(" + engine.AlgorithmName + "/" + digest.AlgorithmName + ")"; }
  25. }
  26. /**
  27. * initialise the signer for signing or verification.
  28. *
  29. * @param forSigning
  30. * true if for signing, false otherwise
  31. * @param parameters
  32. * necessary parameters.
  33. */
  34. public virtual void Init(bool forSigning, ICipherParameters parameters)
  35. {
  36. this.forSigning = forSigning;
  37. AsymmetricKeyParameter k;
  38. if (parameters is ParametersWithRandom)
  39. {
  40. k = (AsymmetricKeyParameter)((ParametersWithRandom)parameters).Parameters;
  41. }
  42. else
  43. {
  44. k = (AsymmetricKeyParameter)parameters;
  45. }
  46. if (forSigning && !k.IsPrivate)
  47. throw new InvalidKeyException("Signing requires private key.");
  48. if (!forSigning && k.IsPrivate)
  49. throw new InvalidKeyException("Verification requires public key.");
  50. Reset();
  51. engine.Init(forSigning, parameters);
  52. }
  53. public virtual void Update(byte input)
  54. {
  55. digest.Update(input);
  56. }
  57. public virtual void BlockUpdate(byte[] input, int inOff, int inLen)
  58. {
  59. digest.BlockUpdate(input, inOff, inLen);
  60. }
  61. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  62. public virtual void BlockUpdate(ReadOnlySpan<byte> input)
  63. {
  64. digest.BlockUpdate(input);
  65. }
  66. #endif
  67. public virtual byte[] GenerateSignature()
  68. {
  69. if (!forSigning)
  70. throw new InvalidOperationException("GenericSigner not initialised for signature generation.");
  71. byte[] hash = new byte[digest.GetDigestSize()];
  72. digest.DoFinal(hash, 0);
  73. return engine.ProcessBlock(hash, 0, hash.Length);
  74. }
  75. public virtual bool VerifySignature(byte[] signature)
  76. {
  77. if (forSigning)
  78. throw new InvalidOperationException("GenericSigner not initialised for verification");
  79. byte[] hash = new byte[digest.GetDigestSize()];
  80. digest.DoFinal(hash, 0);
  81. try
  82. {
  83. byte[] sig = engine.ProcessBlock(signature, 0, signature.Length);
  84. // Extend with leading zeroes to match the digest size, if necessary.
  85. if (sig.Length < hash.Length)
  86. {
  87. byte[] tmp = new byte[hash.Length];
  88. Array.Copy(sig, 0, tmp, tmp.Length - sig.Length, sig.Length);
  89. sig = tmp;
  90. }
  91. return Arrays.ConstantTimeAreEqual(sig, hash);
  92. }
  93. catch (Exception)
  94. {
  95. return false;
  96. }
  97. }
  98. public virtual void Reset()
  99. {
  100. digest.Reset();
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif