PssSigner.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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.Digests;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  9. {
  10. /// <summary> RSA-PSS as described in Pkcs# 1 v 2.1.
  11. /// <p>
  12. /// Note: the usual value for the salt length is the number of
  13. /// bytes in the hash function.</p>
  14. /// </summary>
  15. public class PssSigner
  16. : ISigner
  17. {
  18. public const byte TrailerImplicit = 0xBC;
  19. private readonly IDigest contentDigest1, contentDigest2;
  20. private readonly IDigest mgfDigest;
  21. private readonly IAsymmetricBlockCipher cipher;
  22. private SecureRandom random;
  23. private int hLen;
  24. private int mgfhLen;
  25. private int sLen;
  26. private bool sSet;
  27. private int emBits;
  28. private byte[] salt;
  29. private byte[] mDash;
  30. private byte[] block;
  31. private byte trailer;
  32. public static PssSigner CreateRawSigner(IAsymmetricBlockCipher cipher, IDigest digest)
  33. {
  34. return new PssSigner(cipher, new NullDigest(), digest, digest, digest.GetDigestSize(), null, TrailerImplicit);
  35. }
  36. public static PssSigner CreateRawSigner(IAsymmetricBlockCipher cipher, IDigest contentDigest, IDigest mgfDigest,
  37. int saltLen, byte trailer)
  38. {
  39. return new PssSigner(cipher, new NullDigest(), contentDigest, mgfDigest, saltLen, null, trailer);
  40. }
  41. public static PssSigner CreateRawSigner(IAsymmetricBlockCipher cipher, IDigest contentDigest, IDigest mgfDigest,
  42. byte[] salt, byte trailer)
  43. {
  44. return new PssSigner(cipher, new NullDigest(), contentDigest, mgfDigest, salt.Length, salt, trailer);
  45. }
  46. public PssSigner(
  47. IAsymmetricBlockCipher cipher,
  48. IDigest digest)
  49. : this(cipher, digest, digest.GetDigestSize())
  50. {
  51. }
  52. /// <summary>Basic constructor</summary>
  53. /// <param name="cipher">the asymmetric cipher to use.</param>
  54. /// <param name="digest">the digest to use.</param>
  55. /// <param name="saltLen">the length of the salt to use (in bytes).</param>
  56. public PssSigner(
  57. IAsymmetricBlockCipher cipher,
  58. IDigest digest,
  59. int saltLen)
  60. : this(cipher, digest, saltLen, TrailerImplicit)
  61. {
  62. }
  63. /// <summary>Basic constructor</summary>
  64. /// <param name="cipher">the asymmetric cipher to use.</param>
  65. /// <param name="digest">the digest to use.</param>
  66. /// <param name="salt">the fixed salt to be used.</param>
  67. public PssSigner(
  68. IAsymmetricBlockCipher cipher,
  69. IDigest digest,
  70. byte[] salt)
  71. : this(cipher, digest, digest, digest, salt.Length, salt, TrailerImplicit)
  72. {
  73. }
  74. public PssSigner(
  75. IAsymmetricBlockCipher cipher,
  76. IDigest contentDigest,
  77. IDigest mgfDigest,
  78. int saltLen)
  79. : this(cipher, contentDigest, mgfDigest, saltLen, TrailerImplicit)
  80. {
  81. }
  82. public PssSigner(
  83. IAsymmetricBlockCipher cipher,
  84. IDigest contentDigest,
  85. IDigest mgfDigest,
  86. byte[] salt)
  87. : this(cipher, contentDigest, contentDigest, mgfDigest, salt.Length, salt, TrailerImplicit)
  88. {
  89. }
  90. public PssSigner(
  91. IAsymmetricBlockCipher cipher,
  92. IDigest digest,
  93. int saltLen,
  94. byte trailer)
  95. : this(cipher, digest, digest, saltLen, trailer)
  96. {
  97. }
  98. public PssSigner(
  99. IAsymmetricBlockCipher cipher,
  100. IDigest contentDigest,
  101. IDigest mgfDigest,
  102. int saltLen,
  103. byte trailer)
  104. : this(cipher, contentDigest, contentDigest, mgfDigest, saltLen, null, trailer)
  105. {
  106. }
  107. private PssSigner(
  108. IAsymmetricBlockCipher cipher,
  109. IDigest contentDigest1,
  110. IDigest contentDigest2,
  111. IDigest mgfDigest,
  112. int saltLen,
  113. byte[] salt,
  114. byte trailer)
  115. {
  116. this.cipher = cipher;
  117. this.contentDigest1 = contentDigest1;
  118. this.contentDigest2 = contentDigest2;
  119. this.mgfDigest = mgfDigest;
  120. this.hLen = contentDigest2.GetDigestSize();
  121. this.mgfhLen = mgfDigest.GetDigestSize();
  122. this.sLen = saltLen;
  123. this.sSet = salt != null;
  124. if (sSet)
  125. {
  126. this.salt = salt;
  127. }
  128. else
  129. {
  130. this.salt = new byte[saltLen];
  131. }
  132. this.mDash = new byte[8 + saltLen + hLen];
  133. this.trailer = trailer;
  134. }
  135. public virtual string AlgorithmName
  136. {
  137. get { return mgfDigest.AlgorithmName + "withRSAandMGF1"; }
  138. }
  139. public virtual void Init(bool forSigning, ICipherParameters parameters)
  140. {
  141. if (parameters is ParametersWithRandom withRandom)
  142. {
  143. parameters = withRandom.Parameters;
  144. random = withRandom.Random;
  145. }
  146. else
  147. {
  148. if (forSigning)
  149. {
  150. random = CryptoServicesRegistrar.GetSecureRandom();
  151. }
  152. }
  153. cipher.Init(forSigning, parameters);
  154. RsaKeyParameters kParam;
  155. if (parameters is RsaBlindingParameters)
  156. {
  157. kParam = ((RsaBlindingParameters)parameters).PublicKey;
  158. }
  159. else
  160. {
  161. kParam = (RsaKeyParameters)parameters;
  162. }
  163. emBits = kParam.Modulus.BitLength - 1;
  164. if (emBits < (8 * hLen + 8 * sLen + 9))
  165. throw new ArgumentException("key too small for specified hash and salt lengths");
  166. block = new byte[(emBits + 7) / 8];
  167. }
  168. /// <summary> clear possible sensitive data</summary>
  169. private void ClearBlock(
  170. byte[] block)
  171. {
  172. Array.Clear(block, 0, block.Length);
  173. }
  174. public virtual void Update(byte input)
  175. {
  176. contentDigest1.Update(input);
  177. }
  178. public virtual void BlockUpdate(byte[] input, int inOff, int inLen)
  179. {
  180. contentDigest1.BlockUpdate(input, inOff, inLen);
  181. }
  182. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  183. public virtual void BlockUpdate(ReadOnlySpan<byte> input)
  184. {
  185. contentDigest1.BlockUpdate(input);
  186. }
  187. #endif
  188. public virtual void Reset()
  189. {
  190. contentDigest1.Reset();
  191. }
  192. public virtual byte[] GenerateSignature()
  193. {
  194. if (contentDigest1.GetDigestSize() != hLen)
  195. throw new InvalidOperationException();
  196. contentDigest1.DoFinal(mDash, mDash.Length - hLen - sLen);
  197. if (sLen != 0)
  198. {
  199. if (!sSet)
  200. {
  201. random.NextBytes(salt);
  202. }
  203. salt.CopyTo(mDash, mDash.Length - sLen);
  204. }
  205. byte[] h = new byte[hLen];
  206. contentDigest2.BlockUpdate(mDash, 0, mDash.Length);
  207. contentDigest2.DoFinal(h, 0);
  208. block[block.Length - sLen - 1 - hLen - 1] = (byte) (0x01);
  209. salt.CopyTo(block, block.Length - sLen - hLen - 1);
  210. byte[] dbMask = MaskGeneratorFunction(h, 0, h.Length, block.Length - hLen - 1);
  211. for (int i = 0; i != dbMask.Length; i++)
  212. {
  213. block[i] ^= dbMask[i];
  214. }
  215. h.CopyTo(block, block.Length - hLen - 1);
  216. uint firstByteMask = 0xFFU >> ((block.Length * 8) - emBits);
  217. block[0] &= (byte)firstByteMask;
  218. block[block.Length - 1] = trailer;
  219. byte[] b = cipher.ProcessBlock(block, 0, block.Length);
  220. ClearBlock(block);
  221. return b;
  222. }
  223. public virtual bool VerifySignature(byte[] signature)
  224. {
  225. if (contentDigest1.GetDigestSize() != hLen)
  226. throw new InvalidOperationException();
  227. contentDigest1.DoFinal(mDash, mDash.Length - hLen - sLen);
  228. byte[] b = cipher.ProcessBlock(signature, 0, signature.Length);
  229. Arrays.Fill(block, 0, block.Length - b.Length, 0);
  230. b.CopyTo(block, block.Length - b.Length);
  231. uint firstByteMask = 0xFFU >> ((block.Length * 8) - emBits);
  232. if (block[0] != (byte)(block[0] & firstByteMask)
  233. || block[block.Length - 1] != trailer)
  234. {
  235. ClearBlock(block);
  236. return false;
  237. }
  238. byte[] dbMask = MaskGeneratorFunction(block, block.Length - hLen - 1, hLen, block.Length - hLen - 1);
  239. for (int i = 0; i != dbMask.Length; i++)
  240. {
  241. block[i] ^= dbMask[i];
  242. }
  243. block[0] &= (byte)firstByteMask;
  244. for (int i = 0; i != block.Length - hLen - sLen - 2; i++)
  245. {
  246. if (block[i] != 0)
  247. {
  248. ClearBlock(block);
  249. return false;
  250. }
  251. }
  252. if (block[block.Length - hLen - sLen - 2] != 0x01)
  253. {
  254. ClearBlock(block);
  255. return false;
  256. }
  257. if (sSet)
  258. {
  259. Array.Copy(salt, 0, mDash, mDash.Length - sLen, sLen);
  260. }
  261. else
  262. {
  263. Array.Copy(block, block.Length - sLen - hLen - 1, mDash, mDash.Length - sLen, sLen);
  264. }
  265. contentDigest2.BlockUpdate(mDash, 0, mDash.Length);
  266. contentDigest2.DoFinal(mDash, mDash.Length - hLen);
  267. for (int i = block.Length - hLen - 1, j = mDash.Length - hLen; j != mDash.Length; i++, j++)
  268. {
  269. if ((block[i] ^ mDash[j]) != 0)
  270. {
  271. ClearBlock(mDash);
  272. ClearBlock(block);
  273. return false;
  274. }
  275. }
  276. ClearBlock(mDash);
  277. ClearBlock(block);
  278. return true;
  279. }
  280. /// <summary> int to octet string.</summary>
  281. private void ItoOSP(
  282. int i,
  283. byte[] sp)
  284. {
  285. sp[0] = (byte)((uint) i >> 24);
  286. sp[1] = (byte)((uint) i >> 16);
  287. sp[2] = (byte)((uint) i >> 8);
  288. sp[3] = (byte)((uint) i >> 0);
  289. }
  290. private byte[] MaskGeneratorFunction(
  291. byte[] Z,
  292. int zOff,
  293. int zLen,
  294. int length)
  295. {
  296. if (mgfDigest is IXof)
  297. {
  298. byte[] mask = new byte[length];
  299. mgfDigest.BlockUpdate(Z, zOff, zLen);
  300. ((IXof)mgfDigest).OutputFinal(mask, 0, mask.Length);
  301. return mask;
  302. }
  303. else
  304. {
  305. return MaskGeneratorFunction1(Z, zOff, zLen, length);
  306. }
  307. }
  308. /// <summary> mask generator function, as described in Pkcs1v2.</summary>
  309. private byte[] MaskGeneratorFunction1(
  310. byte[] Z,
  311. int zOff,
  312. int zLen,
  313. int length)
  314. {
  315. byte[] mask = new byte[length];
  316. byte[] hashBuf = new byte[mgfhLen];
  317. byte[] C = new byte[4];
  318. int counter = 0;
  319. mgfDigest.Reset();
  320. while (counter < (length / mgfhLen))
  321. {
  322. ItoOSP(counter, C);
  323. mgfDigest.BlockUpdate(Z, zOff, zLen);
  324. mgfDigest.BlockUpdate(C, 0, C.Length);
  325. mgfDigest.DoFinal(hashBuf, 0);
  326. hashBuf.CopyTo(mask, counter * mgfhLen);
  327. ++counter;
  328. }
  329. if ((counter * mgfhLen) < length)
  330. {
  331. ItoOSP(counter, C);
  332. mgfDigest.BlockUpdate(Z, zOff, zLen);
  333. mgfDigest.BlockUpdate(C, 0, C.Length);
  334. mgfDigest.DoFinal(hashBuf, 0);
  335. Array.Copy(hashBuf, 0, mask, counter * mgfhLen, mask.Length - (counter * mgfhLen));
  336. }
  337. return mask;
  338. }
  339. }
  340. }
  341. #pragma warning restore
  342. #endif