2
0

asymmetric.service.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. cryptolib.modules.asymmetric.service = function(box) {
  10. 'use strict';
  11. box.asymmetric = box.asymmetric || {};
  12. /**
  13. * A module that holds certificates functionalities.
  14. * @exports asymmetric/service
  15. */
  16. box.asymmetric.service = {};
  17. var keypairGeneratorFactory, signerFactory, xmlSignerFactory,
  18. cipherFactory;
  19. cryptolib(
  20. 'asymmetric.signer', 'asymmetric.xmlsigner', 'asymmetric.keypair',
  21. 'asymmetric.cipher', 'commons.exceptions', function(box) {
  22. keypairGeneratorFactory =
  23. new box.asymmetric.keypair.factory.KeyPairGeneratorFactory();
  24. signerFactory = new box.asymmetric.signer.factory.SignerFactory();
  25. xmlSignerFactory =
  26. new box.asymmetric.xmlsigner.factory.XmlSignerFactory();
  27. cipherFactory =
  28. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  29. });
  30. /**
  31. * Generates a {@link asymmetric/keypair.KeyPair} to be used for encrypt data.
  32. * @function
  33. * @returns {asymmetric/keypair.KeyPair} it can be used for encrypt data.
  34. */
  35. box.asymmetric.service.getKeyPairForEncryption = function() {
  36. var cryptoKeyPairGenerator =
  37. keypairGeneratorFactory.getEncryptionCryptoKeyPairGenerator();
  38. return cryptoKeyPairGenerator.generate();
  39. };
  40. /**
  41. * Encrypts the given plain text using the given
  42. * public key.
  43. * @function
  44. * @param publicKeyPem
  45. * {String} public key, as string in PEM format.
  46. * @param dataBase64
  47. * {String} data to encrypt, as a string in Base64 encoded
  48. * format.
  49. * @return encrypted data in Base64 encoded format.
  50. */
  51. box.asymmetric.service.encrypt = function(publicKeyPem, dataBase64) {
  52. var cipher = cipherFactory.getCryptoAsymmetricCipher();
  53. return cipher.encrypt(publicKeyPem, dataBase64);
  54. };
  55. /**
  56. * Decrypts the given cipher text with the given
  57. * private key.
  58. * @function
  59. * @param privateKeyPem
  60. * {String} private key, as string in PEM format.
  61. * @param encryptedDataBase64
  62. * {String} data to decrypt, as a string in Base64 encoded
  63. * format.
  64. * @return a string in Base64 encoded format.
  65. */
  66. box.asymmetric.service.decrypt = function(
  67. privateKeyPem, encryptedDataBase64) {
  68. var cipher = cipherFactory.getCryptoAsymmetricCipher();
  69. return cipher.decrypt(privateKeyPem, encryptedDataBase64);
  70. };
  71. /**
  72. * Signs the given message using the given private key.
  73. * @function
  74. * @param privateKeyPem
  75. * {String} private key, as string in PEM format.
  76. * @param arrayDataBase64
  77. * {String} data to sign, as an array of string in Base64 encoded
  78. * format.
  79. * @return signature, in Base64 encoded format.
  80. */
  81. box.asymmetric.service.sign = function(privateKeyPem, ArrayDataBase64) {
  82. var cryptoSigner = signerFactory.getCryptoSigner();
  83. return cryptoSigner.sign(privateKeyPem, ArrayDataBase64);
  84. };
  85. /**
  86. * Verifies that the given signature is indeed the signature of the given
  87. * bytes, using the given public key.
  88. * @function
  89. * @param signatureB64
  90. * {String} signature, as string in Base64 encoded format.
  91. * @param publicKeyPem
  92. * {String} public key, as string in PEM format.
  93. * @param arrayDataBase64
  94. * {String} data that was signed, as an array of string in Base64
  95. * encoded format.
  96. *
  97. * @return boolean indicating whether signature verification was successful.
  98. */
  99. box.asymmetric.service.verifySignature = function(
  100. signatureBase64, publicKeyPem, arrayDataBase64) {
  101. var cryptoSigner = signerFactory.getCryptoSigner();
  102. return cryptoSigner.verify(signatureBase64, publicKeyPem, arrayDataBase64);
  103. };
  104. /**
  105. * Verifies the signature of some data that is in XML format.
  106. * @function
  107. * @param publicKey
  108. * {Object} Public key.
  109. * @param signedXml
  110. * {string} XML with selfcontained signature, to verify
  111. * @param signatureParentNode
  112. * {string} Node where the signature is
  113. *
  114. * @return boolean indicating whether signature verification was successful.
  115. */
  116. box.asymmetric.service.verifyXmlSignature = function(
  117. publicKey, signedXml, signatureParentNode) {
  118. var cryptoXmlSigner = xmlSignerFactory.getCryptoXmlSigner();
  119. return cryptoXmlSigner.verify(publicKey, signedXml, signatureParentNode);
  120. };
  121. };