PssSigner.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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.Security;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
  8. {
  9. /// <summary> RSA-PSS as described in Pkcs# 1 v 2.1.
  10. /// <p>
  11. /// Note: the usual value for the salt length is the number of
  12. /// bytes in the hash function.</p>
  13. /// </summary>
  14. public class PssSigner
  15. : ISigner
  16. {
  17. public const byte TrailerImplicit = (byte)0xBC;
  18. private readonly IDigest contentDigest1, contentDigest2;
  19. private readonly IDigest mgfDigest;
  20. private readonly IAsymmetricBlockCipher cipher;
  21. private SecureRandom random;
  22. private int hLen;
  23. private int mgfhLen;
  24. private int sLen;
  25. private bool sSet;
  26. private int emBits;
  27. private byte[] salt;
  28. private byte[] mDash;
  29. private byte[] block;
  30. private byte trailer;
  31. public static PssSigner CreateRawSigner(
  32. IAsymmetricBlockCipher cipher,
  33. IDigest digest)
  34. {
  35. return new PssSigner(cipher, new NullDigest(), digest, digest, digest.GetDigestSize(), null, TrailerImplicit);
  36. }
  37. public static PssSigner CreateRawSigner(
  38. IAsymmetricBlockCipher cipher,
  39. IDigest contentDigest,
  40. IDigest mgfDigest,
  41. int saltLen,
  42. byte trailer)
  43. {
  44. return new PssSigner(cipher, new NullDigest(), contentDigest, mgfDigest, saltLen, null, 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, TrailerImplicit)
  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(
  140. bool forSigning,
  141. ICipherParameters parameters)
  142. {
  143. if (parameters is ParametersWithRandom)
  144. {
  145. ParametersWithRandom p = (ParametersWithRandom) parameters;
  146. parameters = p.Parameters;
  147. random = p.Random;
  148. }
  149. else
  150. {
  151. if (forSigning)
  152. {
  153. random = new SecureRandom();
  154. }
  155. }
  156. cipher.Init(forSigning, parameters);
  157. RsaKeyParameters kParam;
  158. if (parameters is RsaBlindingParameters)
  159. {
  160. kParam = ((RsaBlindingParameters) parameters).PublicKey;
  161. }
  162. else
  163. {
  164. kParam = (RsaKeyParameters) parameters;
  165. }
  166. emBits = kParam.Modulus.BitLength - 1;
  167. if (emBits < (8 * hLen + 8 * sLen + 9))
  168. throw new ArgumentException("key too small for specified hash and salt lengths");
  169. block = new byte[(emBits + 7) / 8];
  170. }
  171. /// <summary> clear possible sensitive data</summary>
  172. private void ClearBlock(
  173. byte[] block)
  174. {
  175. Array.Clear(block, 0, block.Length);
  176. }
  177. /// <summary> update the internal digest with the byte b</summary>
  178. public virtual void Update(
  179. byte input)
  180. {
  181. contentDigest1.Update(input);
  182. }
  183. /// <summary> update the internal digest with the byte array in</summary>
  184. public virtual void BlockUpdate(
  185. byte[] input,
  186. int inOff,
  187. int length)
  188. {
  189. contentDigest1.BlockUpdate(input, inOff, length);
  190. }
  191. /// <summary> reset the internal state</summary>
  192. public virtual void Reset()
  193. {
  194. contentDigest1.Reset();
  195. }
  196. /// <summary> Generate a signature for the message we've been loaded with using
  197. /// the key we were initialised with.
  198. /// </summary>
  199. public virtual byte[] GenerateSignature()
  200. {
  201. contentDigest1.DoFinal(mDash, mDash.Length - hLen - sLen);
  202. if (sLen != 0)
  203. {
  204. if (!sSet)
  205. {
  206. random.NextBytes(salt);
  207. }
  208. salt.CopyTo(mDash, mDash.Length - sLen);
  209. }
  210. byte[] h = new byte[hLen];
  211. contentDigest2.BlockUpdate(mDash, 0, mDash.Length);
  212. contentDigest2.DoFinal(h, 0);
  213. block[block.Length - sLen - 1 - hLen - 1] = (byte) (0x01);
  214. salt.CopyTo(block, block.Length - sLen - hLen - 1);
  215. byte[] dbMask = MaskGeneratorFunction1(h, 0, h.Length, block.Length - hLen - 1);
  216. for (int i = 0; i != dbMask.Length; i++)
  217. {
  218. block[i] ^= dbMask[i];
  219. }
  220. block[0] &= (byte) ((0xff >> ((block.Length * 8) - emBits)));
  221. h.CopyTo(block, block.Length - hLen - 1);
  222. block[block.Length - 1] = trailer;
  223. byte[] b = cipher.ProcessBlock(block, 0, block.Length);
  224. ClearBlock(block);
  225. return b;
  226. }
  227. /// <summary> return true if the internal state represents the signature described
  228. /// in the passed in array.
  229. /// </summary>
  230. public virtual bool VerifySignature(
  231. byte[] signature)
  232. {
  233. contentDigest1.DoFinal(mDash, mDash.Length - hLen - sLen);
  234. byte[] b = cipher.ProcessBlock(signature, 0, signature.Length);
  235. b.CopyTo(block, block.Length - b.Length);
  236. if (block[block.Length - 1] != trailer)
  237. {
  238. ClearBlock(block);
  239. return false;
  240. }
  241. byte[] dbMask = MaskGeneratorFunction1(block, block.Length - hLen - 1, hLen, block.Length - hLen - 1);
  242. for (int i = 0; i != dbMask.Length; i++)
  243. {
  244. block[i] ^= dbMask[i];
  245. }
  246. block[0] &= (byte) ((0xff >> ((block.Length * 8) - emBits)));
  247. for (int i = 0; i != block.Length - hLen - sLen - 2; i++)
  248. {
  249. if (block[i] != 0)
  250. {
  251. ClearBlock(block);
  252. return false;
  253. }
  254. }
  255. if (block[block.Length - hLen - sLen - 2] != 0x01)
  256. {
  257. ClearBlock(block);
  258. return false;
  259. }
  260. if (sSet)
  261. {
  262. Array.Copy(salt, 0, mDash, mDash.Length - sLen, sLen);
  263. }
  264. else
  265. {
  266. Array.Copy(block, block.Length - sLen - hLen - 1, mDash, mDash.Length - sLen, sLen);
  267. }
  268. contentDigest2.BlockUpdate(mDash, 0, mDash.Length);
  269. contentDigest2.DoFinal(mDash, mDash.Length - hLen);
  270. for (int i = block.Length - hLen - 1, j = mDash.Length - hLen; j != mDash.Length; i++, j++)
  271. {
  272. if ((block[i] ^ mDash[j]) != 0)
  273. {
  274. ClearBlock(mDash);
  275. ClearBlock(block);
  276. return false;
  277. }
  278. }
  279. ClearBlock(mDash);
  280. ClearBlock(block);
  281. return true;
  282. }
  283. /// <summary> int to octet string.</summary>
  284. private void ItoOSP(
  285. int i,
  286. byte[] sp)
  287. {
  288. sp[0] = (byte)((uint) i >> 24);
  289. sp[1] = (byte)((uint) i >> 16);
  290. sp[2] = (byte)((uint) i >> 8);
  291. sp[3] = (byte)((uint) i >> 0);
  292. }
  293. /// <summary> mask generator function, as described in Pkcs1v2.</summary>
  294. private byte[] MaskGeneratorFunction1(
  295. byte[] Z,
  296. int zOff,
  297. int zLen,
  298. int length)
  299. {
  300. byte[] mask = new byte[length];
  301. byte[] hashBuf = new byte[mgfhLen];
  302. byte[] C = new byte[4];
  303. int counter = 0;
  304. mgfDigest.Reset();
  305. while (counter < (length / mgfhLen))
  306. {
  307. ItoOSP(counter, C);
  308. mgfDigest.BlockUpdate(Z, zOff, zLen);
  309. mgfDigest.BlockUpdate(C, 0, C.Length);
  310. mgfDigest.DoFinal(hashBuf, 0);
  311. hashBuf.CopyTo(mask, counter * mgfhLen);
  312. ++counter;
  313. }
  314. if ((counter * mgfhLen) < length)
  315. {
  316. ItoOSP(counter, C);
  317. mgfDigest.BlockUpdate(Z, zOff, zLen);
  318. mgfDigest.BlockUpdate(C, 0, C.Length);
  319. mgfDigest.DoFinal(hashBuf, 0);
  320. Array.Copy(hashBuf, 0, mask, counter * mgfhLen, mask.Length - (counter * mgfhLen));
  321. }
  322. return mask;
  323. }
  324. }
  325. }
  326. #pragma warning restore
  327. #endif