2
0

asymmetric.signer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. cryptolib.modules.asymmetric = cryptolib.modules.asymmetric || {};
  9. /** @namespace asymmetric/signer */
  10. cryptolib.modules.asymmetric.signer = function(box) {
  11. 'use strict';
  12. box.asymmetric = box.asymmetric || {};
  13. box.asymmetric.signer = {};
  14. /**
  15. * A module that holds asymmetric signature functionalities.
  16. *
  17. * @exports asymmetric/signer/factory
  18. */
  19. box.asymmetric.signer.factory = {};
  20. var policies = {
  21. algorithm: box.policies.asymmetric.signer.algorithm,
  22. provider: box.policies.asymmetric.signer.provider,
  23. hash: box.policies.asymmetric.signer.hash,
  24. padding: box.policies.asymmetric.signer.padding,
  25. publicExponent: box.policies.asymmetric.signer.publicExponent
  26. };
  27. var converters, exceptions, randomFactory;
  28. var f = function(box) {
  29. converters = new box.commons.utils.Converters();
  30. exceptions = box.commons.exceptions;
  31. randomFactory =
  32. new box.primitives.securerandom.factory.SecureRandomFactory();
  33. };
  34. f.policies = {
  35. primitives: {
  36. secureRandom:
  37. {provider: box.policies.asymmetric.signer.secureRandom.provider}
  38. }
  39. };
  40. cryptolib('commons', 'primitives.securerandom', f);
  41. /**
  42. * A factory class for creating a digital signer.
  43. *
  44. * @class
  45. */
  46. box.asymmetric.signer.factory.SignerFactory = function() {};
  47. box.asymmetric.signer.factory.SignerFactory.prototype = {
  48. getCryptoSigner: function() {
  49. try {
  50. if (policies.provider === Config.asymmetric.signer.provider.FORGE) {
  51. var secureRandom = randomFactory.getCryptoRandomBytes();
  52. return this.getCryptoForgeSigner(secureRandom);
  53. } else {
  54. throw new exceptions.CryptoLibException(
  55. 'No suitable provider for the signer was provided.');
  56. }
  57. } catch (error) {
  58. throw new exceptions.CryptoLibException(
  59. 'A CryptoSigner could not be obtained.', error);
  60. }
  61. },
  62. /**
  63. * @function
  64. * @return {asymmetric/signer.CryptoForgeSigner}
  65. */
  66. getCryptoForgeSigner: function(secureRandom) {
  67. return new CryptoForgeSigner(secureRandom);
  68. }
  69. };
  70. /**
  71. * A digital signer.
  72. *
  73. * @class
  74. * @memberof asymmetric/signer
  75. * @param secureRandom
  76. */
  77. function CryptoForgeSigner(secureRandom) {
  78. this.digester = null;
  79. this.secureRandom = secureRandom;
  80. try {
  81. if (policies.algorithm !== Config.asymmetric.signer.algorithm.RSA) {
  82. throw new exceptions.CryptoLibException(
  83. 'The given algorithm is not supported');
  84. }
  85. if (policies.hash === Config.asymmetric.signer.hash.SHA256) {
  86. this.digester = forge.md.sha256.create();
  87. } else {
  88. var errorMessage = 'Message digester type \'' + policies.hash +
  89. '\' not recognized by signer.';
  90. throw new exceptions.CryptoLibException(errorMessage);
  91. }
  92. if (typeof(policies.padding) !== 'undefined' &&
  93. policies.padding.PSS.name === Config.asymmetric.signer.padding.PSS.name) {
  94. var paddingHash;
  95. if (policies.padding.PSS.hash ===
  96. Config.asymmetric.signer.padding.PSS.hash.SHA256) {
  97. paddingHash = forge.md.sha256.create();
  98. }
  99. var paddingMgf;
  100. if (policies.padding.PSS.mask.MGF1.name ===
  101. Config.asymmetric.signer.padding.PSS.mask.MGF1.name &&
  102. policies.padding.PSS.mask.MGF1.hash ===
  103. Config.asymmetric.signer.padding.PSS.mask.MGF1.hash.SHA256) {
  104. var mgfHash = forge.md.sha256.create();
  105. paddingMgf = forge.mgf.mgf1.create(mgfHash);
  106. }
  107. this.padding = forge.pss.create({
  108. md: paddingHash,
  109. mgf: paddingMgf,
  110. saltLength: policies.padding.PSS.saltLengthBytes,
  111. prng: this.secureRandom
  112. });
  113. }
  114. } catch (error) {
  115. throw new exceptions.CryptoLibException(
  116. 'CryptoForgeSigner could not be created.', error);
  117. }
  118. }
  119. CryptoForgeSigner.prototype = {
  120. /**
  121. * Digitally signs some data.
  122. *
  123. * @function
  124. * @param privateKeyPem
  125. * Private key, as string in PEM format.
  126. * @param arrayDataBase64
  127. * Data to sign, as an array of string in Base64 encoded
  128. * format.
  129. * @returns signature, in Base64 encoded format.
  130. */
  131. sign: function(privateKeyPem, arrayDataBase64) {
  132. try {
  133. var privateKey = forge.pki.privateKeyFromPem(privateKeyPem);
  134. if (privateKey.e.toString() !== policies.publicExponent.toString()) {
  135. throw new exceptions.CryptoLibException(
  136. 'Private key has not been generated with the required exponent : ' +
  137. policies.publicExponent);
  138. }
  139. if (!arrayDataBase64 || arrayDataBase64.length < 1) {
  140. throw new exceptions.CryptoLibException(
  141. 'The array of data in Base64 should contain at least one element.');
  142. }
  143. this.digester.start();
  144. var dataB64;
  145. var data;
  146. for (var i = 0; i < arrayDataBase64.length; i++) {
  147. dataB64 = arrayDataBase64[i];
  148. if (dataB64 === undefined || dataB64 === null) {
  149. throw new exceptions.CryptoLibException(
  150. 'The input data contained an element that was not defined or empty.');
  151. }
  152. data = converters.base64Decode(dataB64);
  153. this.digester.update(data);
  154. }
  155. var signature = privateKey.sign(this.digester, this.padding);
  156. return converters.base64Encode(signature);
  157. } catch (error) {
  158. throw new exceptions.CryptoLibException(
  159. 'Signature could not be generated.', error);
  160. }
  161. },
  162. /**
  163. * Verifies the digital signature of some data.
  164. *
  165. * @function
  166. *
  167. * @param signatureB64
  168. * Signature, as string in Base64 encoded format.
  169. * @param publicKeyPem
  170. * Public key, as string in PEM format.
  171. * @param arrayDataBase64
  172. * Data that was signed, as an array of string in Base64
  173. * encoded format.
  174. *
  175. * @returns boolean indicating whether signature verification was
  176. * successful.
  177. */
  178. verify: function(signatureB64, publicKeyPem, arrayDataBase64) {
  179. try {
  180. var publicKey = forge.pki.publicKeyFromPem(publicKeyPem);
  181. if (publicKey.e.toString() !== policies.publicExponent.toString()) {
  182. throw new exceptions.CryptoLibException(
  183. 'Public key has not been generated with the required exponent : ' +
  184. policies.publicExponent);
  185. }
  186. if (!arrayDataBase64 || arrayDataBase64.length < 1) {
  187. throw new exceptions.CryptoLibException(
  188. 'The array of data in Base64 should contain at least one element.');
  189. }
  190. this.digester.start();
  191. var dataB64;
  192. for (var i = 0; i < arrayDataBase64.length; i++) {
  193. dataB64 = arrayDataBase64[i];
  194. if (dataB64 === undefined || dataB64 === null) {
  195. throw new exceptions.CryptoLibException(
  196. 'The input data contained an element that was not defined or empty.');
  197. }
  198. var data = converters.base64Decode(dataB64);
  199. this.digester.update(data);
  200. }
  201. var signature = converters.base64Decode(signatureB64);
  202. return publicKey.verify(
  203. this.digester.digest().getBytes(), signature, this.padding);
  204. } catch (error) {
  205. throw new exceptions.CryptoLibException(
  206. 'Signature could not be verified.', error);
  207. }
  208. }
  209. };
  210. };