digitalenvelope.service.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. describe('Digital envelope service...', function() {
  9. 'use strict';
  10. var _publicKeysPem = [
  11. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQAB-----END PUBLIC KEY-----',
  12. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAngCJ2RYh3XOvghmAi2MT26/fx4Hx8gYkYS2zaf9c2H8Qns1hnIHO8pIlHze/stUmNzim6nlytbJLQHmVDu64Pf9dn1ClAW2caKqUo4ofjnOzDd6RocQByjTsvZ1nOcG5BMYtOWGua+QIVFrxvxZgL6T3ShQ+9J3/1LiQDUmWj0kMoct6vRJuNkJxVFegtIEqYZ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2JnP68Z4OeKZBx2tcrrOfwrRAoGAGyXTcOvhr+5ayXkQ36VAr2YcAUs2SeN0HwmYBP2ErCgd/KXri7RwWXqEQtvpvAOGJq/tXyc7P5BnBhlqQtUm9WX7kqTy/dIS+pJP0UMm02N9U+b02G0auPmBKM35LXO4YGhZep1Vig+MLycR8OSe0cd4gzk52u3Th9I3e0bV9Fw=-----END RSA PRIVATE KEY-----'
  17. ];
  18. var _data = 'Ox2fUJq1gAbX';
  19. var _digitalEnvelopeService;
  20. var _converters;
  21. var _dataBase64;
  22. // Initialize variables common to all digital envelope tests.
  23. beforeEach(function() {
  24. cryptolib('commons.utils', 'digitalenvelope.service', function(box) {
  25. _digitalEnvelopeService = box.digitalenvelope.service;
  26. _converters = new box.commons.utils.Converters();
  27. _dataBase64 = _converters.base64Encode(_data);
  28. });
  29. });
  30. describe('should be able to ..', function() {
  31. it('generate a digital envelope with a single public key and open the envelope with the corresponding private key',
  32. function() {
  33. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  34. _dataBase64, [_publicKeysPem[0]]);
  35. var retrievedDataBase64 = _digitalEnvelopeService.openDigitalEnvelope(
  36. digitalEnvelope, _privateKeysPem[0]);
  37. expect(retrievedDataBase64).toBe(_dataBase64);
  38. });
  39. it('and generate a digital envelope with multiple public keys and open the envelope with any corresponding private key',
  40. function() {
  41. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  42. _dataBase64, _publicKeysPem);
  43. var retrievedDataBase64;
  44. for (var i = 0; i < _privateKeysPem.length; i++) {
  45. retrievedDataBase64 = _digitalEnvelopeService.openDigitalEnvelope(
  46. digitalEnvelope, _privateKeysPem[i]);
  47. expect(retrievedDataBase64).toBe(_dataBase64);
  48. }
  49. });
  50. it('and serialize and deserialize a digital envelope generated with a single public key',
  51. function() {
  52. cryptolib('digitalenvelope', function(box) {
  53. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  54. _dataBase64, [_publicKeysPem[0]]);
  55. var serializedDigitalEnvelope = digitalEnvelope.stringify();
  56. var deserializedDigitalEnvelope =
  57. box.digitalenvelope.deserialize(serializedDigitalEnvelope);
  58. var retrievedDataBase64 =
  59. _digitalEnvelopeService.openDigitalEnvelope(
  60. deserializedDigitalEnvelope, _privateKeysPem[0]);
  61. expect(retrievedDataBase64).toBe(_dataBase64);
  62. });
  63. });
  64. it('and serialize and deserialize a digital envelope generated with multiple public keys',
  65. function() {
  66. cryptolib('digitalenvelope', function(box) {
  67. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  68. _dataBase64, _publicKeysPem);
  69. var serializedDigitalEnvelope = digitalEnvelope.stringify();
  70. var deserializedDigitalEnvelope =
  71. box.digitalenvelope.deserialize(serializedDigitalEnvelope);
  72. var retrievedDataBase64;
  73. for (var i = 0; i < _privateKeysPem.length; i++) {
  74. retrievedDataBase64 = _digitalEnvelopeService.openDigitalEnvelope(
  75. deserializedDigitalEnvelope, _privateKeysPem[i]);
  76. expect(retrievedDataBase64).toBe(_dataBase64);
  77. }
  78. });
  79. });
  80. it('and throw an exception when opening a digital envelope with the wrong private key',
  81. function() {
  82. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  83. _dataBase64, [_publicKeysPem[0]]);
  84. expect(function() {
  85. _digitalEnvelopeService.openDigitalEnvelope(
  86. digitalEnvelope, _privateKeysPem[1]);
  87. }).toThrow();
  88. });
  89. it('and throw an exception when opening a corrupted digital envelope',
  90. function() {
  91. cryptolib('digitalenvelope', function(box) {
  92. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  93. _dataBase64, [_publicKeysPem[0]]);
  94. var serializedDigitalEnvelope = digitalEnvelope.stringify();
  95. var corruptedSerializedDigitalEnvelope =
  96. serializedDigitalEnvelope.replace(
  97. '\"encryptedDataBase64\":\"', '\"encryptedDataBase64\":\"0');
  98. var corruptedDigitalEnvelope = box.digitalenvelope.deserialize(
  99. corruptedSerializedDigitalEnvelope);
  100. expect(function() {
  101. _digitalEnvelopeService.openDigitalEnvelope(
  102. corruptedDigitalEnvelope, _privateKeysPem[0]);
  103. }).toThrow();
  104. });
  105. });
  106. it('and throw an exception when generating a digital envelope with undefined data',
  107. function() {
  108. expect(function() {
  109. _digitalEnvelopeService.createDigitalEnvelope(
  110. undefined, [_publicKeysPem[0]]);
  111. }).toThrow();
  112. });
  113. it('and throw an exception when generating a digital envelope with empty data',
  114. function() {
  115. var dataBase64 = _converters.base64Encode('');
  116. expect(function() {
  117. _digitalEnvelopeService.createDigitalEnvelope(
  118. dataBase64, [_publicKeysPem[0]]);
  119. }).toThrow();
  120. });
  121. it('and throw an exception when generating a digital envelope with data that is not Base64 encoded',
  122. function() {
  123. var dataBase64 = 0;
  124. expect(function() {
  125. _digitalEnvelopeService.createDigitalEnvelope(
  126. dataBase64, [_publicKeysPem[0]]);
  127. }).toThrow();
  128. });
  129. it('and throw an exception when generating a digital envelope with undefined public keys',
  130. function() {
  131. expect(function() {
  132. _digitalEnvelopeService.createDigitalEnvelope(
  133. _dataBase64, undefined);
  134. }).toThrow();
  135. });
  136. it('and throw an exception when generating a digital envelope with public keys that are not of type Array',
  137. function() {
  138. expect(function() {
  139. _digitalEnvelopeService.createDigitalEnvelope(_dataBase64, 0);
  140. }).toThrow();
  141. });
  142. it('and throw an exception when generating a digital envelope with an empty public keys array',
  143. function() {
  144. expect(function() {
  145. _digitalEnvelopeService.createDigitalEnvelope(_dataBase64, []);
  146. }).toThrow();
  147. });
  148. it('and throw an exception when opening a digital envelope that is undefined',
  149. function() {
  150. expect(function() {
  151. _digitalEnvelopeService.openDigitalEnvelope(
  152. undefined, _privateKeysPem[0]);
  153. }).toThrow();
  154. });
  155. it('and throw an exception when opening a digital envelope that is not of type Object',
  156. function() {
  157. expect(function() {
  158. var digitalEnvelope = 0;
  159. _digitalEnvelopeService.openDigitalEnvelope(
  160. digitalEnvelope, _privateKeysPem[0]);
  161. }).toThrow();
  162. });
  163. it('and throw an exception when opening a digital envelope with an undefined private key',
  164. function() {
  165. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  166. _dataBase64, [_publicKeysPem[0]]);
  167. expect(function() {
  168. _digitalEnvelopeService.openDigitalEnvelope(
  169. digitalEnvelope, undefined);
  170. }).toThrow();
  171. });
  172. it('and throw an exception when opening a digital envelope with a private key PEM that is not of type String',
  173. function() {
  174. var digitalEnvelope = _digitalEnvelopeService.createDigitalEnvelope(
  175. _dataBase64, [_publicKeysPem[0]]);
  176. expect(function() {
  177. _digitalEnvelopeService.openDigitalEnvelope(digitalEnvelope, 0);
  178. }).toThrow();
  179. });
  180. it('and throw an exception when deserializing a digital envelope with an undefined serialized digital envelope',
  181. function() {
  182. cryptolib('digitalenvelope', function(box) {
  183. expect(function() {
  184. box.digitalenvelope.deserialize(undefined);
  185. }).toThrow();
  186. });
  187. });
  188. it('and throw an exception when deserializing a digital envelope with an serialized digital envelope that is not of type String',
  189. function() {
  190. cryptolib('digitalenvelope', function(box) {
  191. expect(function() {
  192. box.digitalenvelope.deserialize(0);
  193. }).toThrow();
  194. });
  195. });
  196. it('and throw an exception when deserializing a digital envelope with an empty serialized digital envelope',
  197. function() {
  198. cryptolib('digitalenvelope', function(box) {
  199. expect(function() {
  200. box.digitalenvelope.deserialize('');
  201. }).toThrow();
  202. });
  203. });
  204. });
  205. });