forked from tink-crypto/tink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_test.js
1019 lines (891 loc) · 30.1 KB
/
registry_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
goog.module('tink.RegistryTest');
goog.setTestOnly('tink.RegistryTest');
const {Aead} = goog.require('google3.third_party.tink.javascript.aead.internal.aead');
const AeadConfig = goog.require('tink.aead.AeadConfig');
const AeadKeyTemplates = goog.require('tink.aead.AeadKeyTemplates');
const AesCtrHmacAeadKeyManager = goog.require('tink.aead.AesCtrHmacAeadKeyManager');
const EncryptThenAuthenticate = goog.require('tink.subtle.EncryptThenAuthenticate');
const HybridConfig = goog.require('tink.hybrid.HybridConfig');
const HybridKeyTemplates = goog.require('tink.hybrid.HybridKeyTemplates');
const KeyManager = goog.require('tink.KeyManager');
const {Mac} = goog.require('google3.third_party.tink.javascript.mac.internal.mac');
const PrimitiveSet = goog.require('tink.PrimitiveSet');
const PrimitiveWrapper = goog.require('tink.PrimitiveWrapper');
const Registry = goog.require('tink.Registry');
const {SecurityException} = goog.require('google3.third_party.tink.javascript.exception.security_exception');
const {PbAesCtrHmacAeadKey, PbAesCtrHmacAeadKeyFormat, PbAesCtrKey, PbAesCtrKeyFormat, PbAesCtrParams, PbEciesAeadHkdfPrivateKey, PbEciesAeadHkdfPublicKey, PbHashType, PbHmacKeyFormat, PbHmacParams, PbKeyData, PbKeyTemplate, PbMessage} = goog.require('google3.third_party.tink.javascript.internal.proto');
////////////////////////////////////////////////////////////////////////////////
// tests
////////////////////////////////////////////////////////////////////////////////
describe('registry test', function() {
afterEach(function() {
Registry.reset();
});
/////////////////////////////////////////////////////////////////////////////
// tests for registerPrimitiveWrapper method
it('register primitive wrapper, overwriting with same class', function() {
Registry.registerPrimitiveWrapper(new DummyPrimitiveWrapper1(
new DummyPrimitive1Impl1(), DummyPrimitive1));
Registry.registerPrimitiveWrapper(new DummyPrimitiveWrapper1(
new DummyPrimitive1Impl2(), DummyPrimitive1));
});
it('register primitive wrapper, overwriting with different class',
function() {
/** @implements {PrimitiveWrapper<DummyPrimitive1>} */
class DummyPrimitiveWrapper1Alternative {
/** @override */
wrap() {
throw new Error();
}
/** @override */
getPrimitiveType() {
return DummyPrimitive1;
}
}
Registry.registerPrimitiveWrapper(new DummyPrimitiveWrapper1(
new DummyPrimitive1Impl1(), DummyPrimitive1));
try {
Registry.registerPrimitiveWrapper(
new DummyPrimitiveWrapper1Alternative());
fail('An exception should be thrown.');
} catch (e) {
expect(e.toString())
.toBe(
'SecurityException: primitive wrapper for type ' +
DummyPrimitive1 +
' has already been registered and cannot be overwritten');
}
});
/////////////////////////////////////////////////////////////////////////////
// tests for wrap method
it('wrap, should work', function() {
const p1 = new DummyPrimitive1Impl1();
const p2 = new DummyPrimitive2Impl();
Registry.registerPrimitiveWrapper(
new DummyPrimitiveWrapper1(p1, DummyPrimitive1));
Registry.registerPrimitiveWrapper(
new DummyPrimitiveWrapper2(p2, DummyPrimitive2));
expect(Registry.wrap(new PrimitiveSet.PrimitiveSet(DummyPrimitive1)))
.toBe(p1);
expect(Registry.wrap(new PrimitiveSet.PrimitiveSet(DummyPrimitive2)))
.toBe(p2);
});
it('wrap, not registered primitive type', function() {
expect(() => {
Registry.wrap(new PrimitiveSet.PrimitiveSet(DummyPrimitive1));
}).toThrowError('no primitive wrapper found for type ' +
DummyPrimitive1);
});
/////////////////////////////////////////////////////////////////////////////
// tests for registerKeyManager method
it('register key manager, overwriting attempt', function() {
const keyType = 'someKeyType';
try {
Registry.registerKeyManager(new DummyKeyManager1(keyType));
Registry.registerKeyManager(new DummyKeyManager2(keyType));
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.keyManagerOverwrittingAttempt(keyType));
return;
}
fail('An exception should be thrown.');
});
// Testing newKeyAllowed behavior -- should hold the most restrictive setting.
it('register key manager, more restrictive new key allowed',
async function() {
const keyType = 'someTypeUrl';
const keyManager1 = new DummyKeyManager1(keyType);
const keyTemplate = new PbKeyTemplate().setTypeUrl(keyType);
// Register the key manager with new_key_allowed and test that it is
// possible to create a new key data.
Registry.registerKeyManager(keyManager1);
await Registry.newKeyData(keyTemplate);
// Restrict the key manager and test that new key data cannot be created.
Registry.registerKeyManager(keyManager1, false);
try {
await Registry.newKeyData(keyTemplate);
} catch (e) {
expect(e.toString()).toBe(ExceptionText.newKeyForbidden(keyType));
return;
}
fail('An exception should be thrown.');
});
it('register key manager, less restrictive new key allowed',
async function() {
const keyType = 'someTypeUrl';
const keyManager1 = new DummyKeyManager1(keyType);
const keyTemplate = new PbKeyTemplate().setTypeUrl(keyType);
Registry.registerKeyManager(keyManager1, false);
// Re-registering key manager with less restrictive setting should not be
// possible and the restriction has to be still true (i.e. new key data
// cannot be created).
try {
Registry.registerKeyManager(keyManager1);
fail('An exception should be thrown.');
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.prohibitedChangeToLessRestricted(
keyManager1.getKeyType()));
}
try {
await Registry.newKeyData(keyTemplate);
} catch (e) {
expect(e.toString()).toBe(ExceptionText.newKeyForbidden(keyType));
return;
}
fail('An exception should be thrown.');
});
/////////////////////////////////////////////////////////////////////////////
// tests for getKeyManager method
it('get key manager, should work', function() {
const numberOfKeyManagers = 10;
let keyManagers1 = [];
let keyManagers2 = [];
for (let i = 0; i < numberOfKeyManagers; i++) {
keyManagers1.push(new DummyKeyManager1('someKeyType' + i.toString()));
keyManagers2.push(new DummyKeyManager2('otherKeyType' + i.toString()));
Registry.registerKeyManager(keyManagers1[i]);
Registry.registerKeyManager(keyManagers2[i]);
}
let result;
for (let i = 0; i < numberOfKeyManagers; i++) {
result = Registry.getKeyManager(keyManagers1[i].getKeyType());
expect(result).toEqual(keyManagers1[i]);
result = Registry.getKeyManager(keyManagers2[i].getKeyType());
expect(result).toEqual(keyManagers2[i]);
}
});
it('get key manager, not registered key type', function() {
const keyType = 'some_key_type';
try {
Registry.getKeyManager(keyType);
} catch (e) {
expect(e.toString()).toBe(ExceptionText.notRegisteredKeyType(keyType));
return;
}
fail('An exception should be thrown.');
});
/////////////////////////////////////////////////////////////////////////////
// tests for newKeyData method
it('new key data, no manager for given key type', async function() {
const keyManager1 = new DummyKeyManager1('someKeyType');
const differentKeyType = 'otherKeyType';
const keyTemplate = new PbKeyTemplate().setTypeUrl(differentKeyType);
Registry.registerKeyManager(keyManager1);
try {
await Registry.newKeyData(keyTemplate);
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.notRegisteredKeyType(differentKeyType));
return;
}
fail('An exception should be thrown.');
});
it('new key data, new key disallowed', async function() {
const keyManager1 = new DummyKeyManager1('someKeyType');
const keyTemplate =
new PbKeyTemplate().setTypeUrl(keyManager1.getKeyType());
Registry.registerKeyManager(keyManager1, false);
try {
await Registry.newKeyData(keyTemplate);
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.newKeyForbidden(keyManager1.getKeyType()));
return;
}
fail('An exception should be thrown.');
});
it('new key data, new key allowed', async function() {
const /** !Array<string> */ keyTypes = [];
for (let i = 0; i < 10; i++) {
keyTypes.push('someKeyType' + i.toString());
}
const keyTypesLength = keyTypes.length;
for (let i = 0; i < keyTypesLength; i++) {
Registry.registerKeyManager(new DummyKeyManager1(keyTypes[i]), true);
}
for (let i = 0; i < keyTypesLength; i++) {
const keyTemplate = new PbKeyTemplate().setTypeUrl(keyTypes[i]);
const result = await Registry.newKeyData(keyTemplate);
expect(result.getTypeUrl()).toBe(keyTypes[i]);
}
});
it('new key data, new key is allowed automatically', async function() {
const /** !Array<string> */ keyTypes = [];
for (let i = 0; i < 10; i++) {
keyTypes.push('someKeyType' + i.toString());
}
const keyTypesLength = keyTypes.length;
for (let i = 0; i < keyTypesLength; i++) {
Registry.registerKeyManager(new DummyKeyManager1(keyTypes[i]));
}
for (let i = 0; i < keyTypesLength; i++) {
const keyTemplate = new PbKeyTemplate().setTypeUrl(keyTypes[i]);
const result = await Registry.newKeyData(keyTemplate);
expect(result.getTypeUrl()).toBe(keyTypes[i]);
}
});
it('new key data, with aes ctr hmac aead key', async function() {
const manager = new AesCtrHmacAeadKeyManager();
Registry.registerKeyManager(manager);
const keyTemplate = createAesCtrHmacAeadTestKeyTemplate();
const keyData = await Registry.newKeyData(keyTemplate);
// Checks that correct AES CTR HMAC AEAD key was returned.
const keyFormat =
PbAesCtrHmacAeadKeyFormat.deserializeBinary(keyTemplate.getValue());
const key = PbAesCtrHmacAeadKey.deserializeBinary(keyData.getValue());
// Check AES CTR key.
expect(keyFormat.getAesCtrKeyFormat().getKeySize())
.toBe(key.getAesCtrKey().getKeyValue().length);
expect(keyFormat.getAesCtrKeyFormat().getParams())
.toEqual(key.getAesCtrKey().getParams());
// Check HMAC key.
expect(keyFormat.getHmacKeyFormat().getKeySize())
.toBe(key.getHmacKey().getKeyValue().length);
expect(keyFormat.getHmacKeyFormat().getParams())
.toEqual(key.getHmacKey().getParams());
});
/////////////////////////////////////////////////////////////////////////////
// tests for newKey method
it('new key, no manager for given key type', async function() {
const notRegisteredKeyType = 'not_registered_key_type';
const keyTemplate = new PbKeyTemplate().setTypeUrl(notRegisteredKeyType);
try {
await Registry.newKey(keyTemplate);
fail('An exception should be thrown.');
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.notRegisteredKeyType(notRegisteredKeyType));
}
});
it('new key, new key disallowed', async function() {
const keyManager = new DummyKeyManagerForNewKeyTests('someKeyType');
const keyTemplate = new PbKeyTemplate().setTypeUrl(keyManager.getKeyType());
Registry.registerKeyManager(keyManager, /* opt_newKeyAllowed = */ false);
try {
await Registry.newKey(keyTemplate);
fail('An exception should be thrown.');
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.newKeyForbidden(keyManager.getKeyType()));
}
});
it('new key, should work', async function() {
const /** !Array<string> */ keyTypes = [];
const /** !Array<!Uint8Array> */ newKeyMethodResult = [];
const keyTypesLength = 10;
// Add some keys to Registry.
for (let i = 0; i < keyTypesLength; i++) {
keyTypes.push('someKeyType' + i.toString());
newKeyMethodResult.push(new Uint8Array([i + 1]));
Registry.registerKeyManager(
new DummyKeyManagerForNewKeyTests(keyTypes[i], newKeyMethodResult[i]),
/* newKeyAllowed = */ true);
}
// For every keyType verify that it calls new key method of the
// corresponding KeyManager (KeyFactory).
for (let i = 0; i < keyTypesLength; i++) {
const keyTemplate = new PbKeyTemplate().setTypeUrl(keyTypes[i]);
const key =
/** @type {!PbAesCtrKey} */ (await Registry.newKey(keyTemplate));
// The new key method of DummyKeyFactory returns an AesCtrKey which
// KeyValue is set to corresponding value in newKeyMethodResult.
expect(key.getKeyValue()).toBe(newKeyMethodResult[i]);
}
});
it('new key, with aes ctr hmac aead key', async function() {
const manager = new AesCtrHmacAeadKeyManager();
Registry.registerKeyManager(manager);
const keyTemplate = AeadKeyTemplates.aes256CtrHmacSha256();
const key =
/** @type{!PbAesCtrHmacAeadKey} */ (await Registry.newKey(keyTemplate));
// Checks that correct AES CTR HMAC AEAD key was returned.
const keyFormat =
PbAesCtrHmacAeadKeyFormat.deserializeBinary(keyTemplate.getValue());
// Check AES CTR key.
expect(keyFormat.getAesCtrKeyFormat().getKeySize())
.toBe(key.getAesCtrKey().getKeyValue().length);
expect(keyFormat.getAesCtrKeyFormat().getParams())
.toEqual(key.getAesCtrKey().getParams());
// Check HMAC key.
expect(keyFormat.getHmacKeyFormat().getKeySize())
.toBe(key.getHmacKey().getKeyValue().length);
expect(keyFormat.getHmacKeyFormat().getParams())
.toEqual(key.getHmacKey().getParams());
});
/////////////////////////////////////////////////////////////////////////////
// tests for getPrimitive method
it('get primitive, different key types', async function() {
const keyDataType = 'key_data_key_type_url';
const anotherType = 'another_key_type_url';
const keyData = new PbKeyData().setTypeUrl(keyDataType);
try {
await Registry.getPrimitive(Aead, keyData, anotherType);
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.keyTypesAreNotMatching(keyDataType, anotherType));
return;
}
fail('An exception should be thrown.');
});
it('get primitive, without defining key type', async function() {
// Get primitive from key proto without key type.
try {
await Registry.getPrimitive(Aead, new PbMessage);
fail('An exception should be thrown.');
} catch (e) {
expect(e.toString()).toBe(ExceptionText.keyTypeNotDefined());
}
});
it('get primitive, missing key manager', async function() {
const keyDataType = 'key_data_key_type_url';
const keyData = new PbKeyData().setTypeUrl(keyDataType);
try {
await Registry.getPrimitive(Aead, keyData);
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.notRegisteredKeyType(keyDataType));
return;
}
fail('An exception should be thrown.');
});
it('get primitive, from aes ctr hmac aead key data', async function() {
const manager = new AesCtrHmacAeadKeyManager();
Registry.registerKeyManager(manager);
let keyTemplate = createAesCtrHmacAeadTestKeyTemplate();
const keyData = await Registry.newKeyData(keyTemplate);
const primitive =
await Registry.getPrimitive(manager.getPrimitiveType(), keyData);
expect(primitive instanceof EncryptThenAuthenticate).toBe(true);
});
it('get primitive, from aes ctr hmac aead key', async function() {
const manager = new AesCtrHmacAeadKeyManager();
Registry.registerKeyManager(manager);
let keyTemplate = createAesCtrHmacAeadTestKeyTemplate();
const keyData = await Registry.newKeyData(keyTemplate);
const key = PbAesCtrHmacAeadKey.deserializeBinary(keyData.getValue());
const primitive = await Registry.getPrimitive(
manager.getPrimitiveType(), key, keyData.getTypeUrl());
expect(primitive instanceof EncryptThenAuthenticate).toBe(true);
});
it('get primitive, mac from aes ctr hmac aead key', async function() {
const manager = new AesCtrHmacAeadKeyManager();
Registry.registerKeyManager(manager);
let keyTemplate = createAesCtrHmacAeadTestKeyTemplate();
const keyData = await Registry.newKeyData(keyTemplate);
const key = PbAesCtrHmacAeadKey.deserializeBinary(keyData.getValue());
try {
await Registry.getPrimitive(Mac, key, keyData.getTypeUrl());
} catch (e) {
expect(e.toString().includes(ExceptionText.getPrimitiveBadPrimitive()))
.toBe(true);
return;
}
fail('An exception should be thrown.');
});
describe('get public key data', function() {
it('not private key factory', function() {
AeadConfig.register();
const notPrivateTypeUrl = AeadConfig.AES_GCM_TYPE_URL;
try {
Registry.getPublicKeyData(notPrivateTypeUrl, new Uint8Array(8));
fail('An exception should be thrown.');
} catch (e) {
expect(e.toString())
.toBe(ExceptionText.notPrivateKeyFactory(notPrivateTypeUrl));
}
});
it('invalid private key proto serialization', function() {
HybridConfig.register();
const typeUrl = HybridConfig.ECIES_AEAD_HKDF_PRIVATE_KEY_TYPE;
try {
Registry.getPublicKeyData(typeUrl, new Uint8Array(10));
fail('An exception should be thrown.');
} catch (e) {
expect(e.toString()).toBe(ExceptionText.couldNotParse(typeUrl));
}
});
it('should work', async function() {
HybridConfig.register();
const privateKeyData = await Registry.newKeyData(
HybridKeyTemplates.eciesP256HkdfHmacSha256Aes128Gcm());
const privateKey = PbEciesAeadHkdfPrivateKey.deserializeBinary(
privateKeyData.getValue());
const publicKeyData = Registry.getPublicKeyData(
privateKeyData.getTypeUrl(), privateKeyData.getValue_asU8());
expect(HybridConfig.ECIES_AEAD_HKDF_PUBLIC_KEY_TYPE)
.toBe(publicKeyData.getTypeUrl());
expect(PbKeyData.KeyMaterialType.ASYMMETRIC_PUBLIC)
.toBe(publicKeyData.getKeyMaterialType());
const expectedPublicKey = privateKey.getPublicKey();
const publicKey = PbEciesAeadHkdfPublicKey.deserializeBinary(
publicKeyData.getValue_asU8());
expect(publicKey).toEqual(expectedPublicKey);
});
});
});
////////////////////////////////////////////////////////////////////////////////
// helper functions and classes for tests
////////////////////////////////////////////////////////////////////////////////
/**
* Class which holds texts for each type of exception.
* @final
*/
class ExceptionText {
/** @return {string} */
static notImplemented() {
return 'SecurityException: Not implemented yet.';
}
/**
* @param {string} keyType
*
* @return {string}
*/
static newKeyForbidden(keyType) {
return 'SecurityException: New key operation is forbidden for key type: ' +
keyType + '.';
}
/**
* @param {string} keyType
*
* @return {string}
*/
static notRegisteredKeyType(keyType) {
return 'SecurityException: Key manager for key type ' + keyType +
' has not been registered.';
}
/**
* @return {string}
*/
static nullKeyManager() {
return 'SecurityException: Key manager cannot be null.';
}
/**
* @return {string}
*/
static undefinedKeyType() {
return 'SecurityException: Key type has to be defined.';
}
/**
* @param {string} keyType
*
* @return {string}
*/
static keyManagerOverwrittingAttempt(keyType) {
return 'SecurityException: Key manager for key type ' + keyType +
' has already been registered and cannot be overwritten.';
}
/**
* @param {string} givenKeyType
*
* @return {string}
*/
static notSupportedKey(givenKeyType) {
return 'SecurityException: The provided key manager does not support ' +
'key type ' + givenKeyType + '.';
}
/**
* @param {string} keyType
*
* @return {string}
*/
static prohibitedChangeToLessRestricted(keyType) {
return 'SecurityException: Key manager for key type ' + keyType +
' has already been registered with forbidden new key operation.';
}
/**
* @param {string} keyTypeFromKeyData
* @param {string} keyTypeParam
*
* @return {string}
*/
static keyTypesAreNotMatching(keyTypeFromKeyData, keyTypeParam) {
return 'SecurityException: Key type is ' + keyTypeParam +
', but it is expected to be ' + keyTypeFromKeyData + ' or undefined.';
}
/** @return {string} */
static keyTypeNotDefined() {
return 'SecurityException: Key type has to be specified.';
}
/** @return {string} */
static nullKeysetHandle() {
return 'SecurityException: Keyset handle has to be non-null.';
}
/**
* @return {string}
*/
static getPrimitiveBadPrimitive() {
return 'Requested primitive type which is not supported by this ' +
'key manager.';
}
/**
* @param {string} typeUrl
* @return {string}
*/
static notPrivateKeyFactory(typeUrl) {
return 'SecurityException: Key manager for key type ' + typeUrl +
' does not have a private key factory.';
}
/**
* @param {string} typeUrl
* @return {string}
*/
static couldNotParse(typeUrl) {
return 'SecurityException: Input cannot be parsed as ' + typeUrl +
' key-proto.';
}
}
/**
* Creates AES CTR HMAC AEAD key format which can be used in tests
*
* @return {!PbKeyTemplate}
*/
const createAesCtrHmacAeadTestKeyTemplate = function() {
const KEY_SIZE = 16;
const IV_SIZE = 12;
const TAG_SIZE = 16;
let keyFormat = new PbAesCtrHmacAeadKeyFormat().setAesCtrKeyFormat(
new PbAesCtrKeyFormat());
keyFormat.getAesCtrKeyFormat().setKeySize(KEY_SIZE);
keyFormat.getAesCtrKeyFormat().setParams(new PbAesCtrParams());
keyFormat.getAesCtrKeyFormat().getParams().setIvSize(IV_SIZE);
// set HMAC key
keyFormat.setHmacKeyFormat(new PbHmacKeyFormat());
keyFormat.getHmacKeyFormat().setKeySize(KEY_SIZE);
keyFormat.getHmacKeyFormat().setParams(new PbHmacParams());
keyFormat.getHmacKeyFormat().getParams().setHash(PbHashType.SHA1);
keyFormat.getHmacKeyFormat().getParams().setTagSize(TAG_SIZE);
let keyTemplate =
new PbKeyTemplate()
.setTypeUrl(
'type.googleapis.com/google.crypto.tink.AesCtrHmacAeadKey')
.setValue(keyFormat.serializeBinary());
return keyTemplate;
};
// Key factory and key manager classes used in tests
/**
* @final
* @implements {KeyManager.KeyFactory}
*/
class DummyKeyFactory {
/**
* @param {string} keyType
* @param {?Uint8Array=} opt_newKeyMethodResult
*/
constructor(keyType, opt_newKeyMethodResult) {
/**
* @const @private {string}
*/
this.KEY_TYPE_ = keyType;
if (!opt_newKeyMethodResult) {
opt_newKeyMethodResult = new Uint8Array(10);
}
/**
* @const @private {!Uint8Array}
*/
this.NEW_KEY_METHOD_RESULT_ = opt_newKeyMethodResult;
}
/**
* @override
*/
newKey(keyFormat) {
const key = new PbAesCtrKey().setKeyValue(this.NEW_KEY_METHOD_RESULT_);
return key;
}
/**
* @override
*/
newKeyData(serializedKeyFormat) {
let keyData =
new PbKeyData()
.setTypeUrl(this.KEY_TYPE_)
.setValue(this.NEW_KEY_METHOD_RESULT_)
.setKeyMaterialType(PbKeyData.KeyMaterialType.UNKNOWN_KEYMATERIAL);
return keyData;
}
}
// Primitive abstract types for testing purposes.
/** @record */
class DummyPrimitive1 {
/** @return {number} */
operation1() {}
}
/** @record */
class DummyPrimitive2 {
/** @return {string} */
operation2() {}
}
// Primitive implementations for testing purposes.
/** @implements {DummyPrimitive1} */
class DummyPrimitive1Impl1 {
/** @override */
operation1() {
return 1;
}
}
/** @implements {DummyPrimitive1} */
class DummyPrimitive1Impl2 {
/** @override */
operation1() {
return 2;
}
}
/** @implements {DummyPrimitive2} */
class DummyPrimitive2Impl {
/** @override */
operation2() {
return 'dummy';
}
}
const DEFAULT_PRIMITIVE_TYPE = Aead;
/**
* @final
* @implements {KeyManager.KeyManager<!DummyPrimitive1>}
*/
class DummyKeyManager1 {
/**
* @param {string} keyType
* @param {?DummyPrimitive1=} opt_primitive
* @param {?Object=} opt_primitiveType
*/
constructor(keyType, opt_primitive, opt_primitiveType) {
/**
* @private @const {string}
*/
this.KEY_TYPE_ = keyType;
if (!opt_primitive) {
opt_primitive = new DummyPrimitive1Impl1();
}
/**
* @private @const {!DummyPrimitive1}
*/
this.PRIMITIVE_ = opt_primitive;
/**
* @private @const {!KeyManager.KeyFactory}
*/
this.KEY_FACTORY_ = new DummyKeyFactory(keyType);
if (!opt_primitiveType) {
opt_primitiveType = DEFAULT_PRIMITIVE_TYPE;
}
/**
* @private @const {!Object}
*/
this.PRIMITIVE_TYPE_ = opt_primitiveType;
}
/** @override */
async getPrimitive(primitiveType, key) {
if (primitiveType !== this.PRIMITIVE_TYPE_) {
throw new SecurityException(
'Requested primitive type which is not ' +
'supported by this key manager.');
}
return this.PRIMITIVE_;
}
/** @override */
doesSupport(keyType) {
return keyType === this.getKeyType();
}
/** @override */
getKeyType() {
return this.KEY_TYPE_;
}
/** @override */
getPrimitiveType() {
return this.PRIMITIVE_TYPE_;
}
/** @override */
getVersion() {
throw new SecurityException('Not implemented, only for testing purposes.');
}
/** @override */
getKeyFactory() {
return this.KEY_FACTORY_;
}
}
/**
* @final
* @implements {KeyManager.KeyManager<!DummyPrimitive2>}
*/
class DummyKeyManager2 {
/**
* @param {string} keyType
* @param {!DummyPrimitive2=} opt_primitive
* @param {?Object=} opt_primitiveType
*/
constructor(keyType, opt_primitive, opt_primitiveType) {
/**
* @private @const {string}
*/
this.KEY_TYPE_ = keyType;
if (!opt_primitive) {
opt_primitive = new DummyPrimitive2Impl();
}
/**
* @private @const {!DummyPrimitive2}
*/
this.PRIMITIVE_ = opt_primitive;
/**
* @private @const {!KeyManager.KeyFactory}
*/
this.KEY_FACTORY_ = new DummyKeyFactory(keyType);
if (!opt_primitiveType) {
opt_primitiveType = DEFAULT_PRIMITIVE_TYPE;
}
/**
* @private @const {!Object}
*/
this.PRIMITIVE_TYPE_ = opt_primitiveType;
}
/** @override */
async getPrimitive(primitiveType, key) {
if (primitiveType !== this.PRIMITIVE_TYPE_) {
throw new SecurityException(
'Requested primitive type which is not ' +
'supported by this key manager.');
}
return this.PRIMITIVE_;
}
/** @override */
doesSupport(keyType) {
return keyType === this.getKeyType();
}
/** @override */
getKeyType() {
return this.KEY_TYPE_;
}
/** @override */
getPrimitiveType() {
return this.PRIMITIVE_TYPE_;
}
/** @override */
getVersion() {
throw new SecurityException('Not implemented, only for testing purposes.');
}
/** @override */
getKeyFactory() {
return this.KEY_FACTORY_;
}
}
/**
* @final
* @implements {KeyManager.KeyManager<string>}
*/
class DummyKeyManagerForNewKeyTests {
/**
* @param {string} keyType
* @param {?Uint8Array=} opt_newKeyMethodResult
*/
constructor(keyType, opt_newKeyMethodResult) {
/**
* @private @const {string}
*/
this.KEY_TYPE_ = keyType;
/**
* @private @const {!KeyManager.KeyFactory}
*/
this.KEY_FACTORY_ = new DummyKeyFactory(keyType, opt_newKeyMethodResult);
}
/** @override */
async getPrimitive(primitiveType, key) {
throw new SecurityException('Not implemented, function is not needed.');
}
/** @override */
doesSupport(keyType) {
return keyType === this.getKeyType();
}
/** @override */
getKeyType() {
return this.KEY_TYPE_;
}
/** @override */
getPrimitiveType() {
throw new SecurityException('Not implemented, function is not needed.');
}
/** @override */
getVersion() {
throw new SecurityException('Not implemented, function is not needed.');
}
/** @override */
getKeyFactory() {
return this.KEY_FACTORY_;
}
}
// PrimitiveWrapper classes for testing purposes
/**
* @final
* @implements {PrimitiveWrapper<DummyPrimitive1>}
*/
class DummyPrimitiveWrapper1 {
/**
* @param {!DummyPrimitive1} primitive
* @param {!Object} primitiveType
*/
constructor(primitive, primitiveType) {
/**
* @private @const {!DummyPrimitive1}
*/
this.PRIMITIVE_ = primitive;
/**
* @private @const {!Object}
*/
this.PRIMITIVE_TYPE_ = primitiveType;
}
/**
* @override
*/
wrap(primitiveSet) {
return this.PRIMITIVE_;
}
/**
* @override
*/
getPrimitiveType() {
return this.PRIMITIVE_TYPE_;
}
}
// PrimitiveWrapper classes for testing purposes
/**
* @final
* @implements {PrimitiveWrapper<DummyPrimitive2>}
*/
class DummyPrimitiveWrapper2 {
/**
* @param {!DummyPrimitive2} primitive
* @param {!Object} primitiveType
*/
constructor(primitive, primitiveType) {
/**
* @private @const {!DummyPrimitive2}
*/
this.PRIMITIVE_ = primitive;
/**