-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathDecryptionHandlerTest.java
785 lines (699 loc) · 31.3 KB
/
DecryptionHandlerTest.java
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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazonaws.encryptionsdk.internal;
import static com.amazonaws.encryptionsdk.TestUtils.assertThrows;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import com.amazonaws.encryptionsdk.AwsCrypto;
import com.amazonaws.encryptionsdk.CommitmentPolicy;
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
import com.amazonaws.encryptionsdk.DefaultCryptoMaterialsManager;
import com.amazonaws.encryptionsdk.MasterKey;
import com.amazonaws.encryptionsdk.MasterKeyProvider;
import com.amazonaws.encryptionsdk.ParsedCiphertext;
import com.amazonaws.encryptionsdk.TestUtils;
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
import com.amazonaws.encryptionsdk.jce.JceMasterKey;
import com.amazonaws.encryptionsdk.model.CiphertextHeaders;
import com.amazonaws.encryptionsdk.model.CiphertextType;
import com.amazonaws.encryptionsdk.model.EncryptionMaterials;
import com.amazonaws.encryptionsdk.model.EncryptionMaterialsRequest;
import com.amazonaws.encryptionsdk.multi.MultipleProviderFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
public class DecryptionHandlerTest {
private StaticMasterKey masterKeyProvider_;
private final CommitmentPolicy commitmentPolicy = TestUtils.DEFAULT_TEST_COMMITMENT_POLICY;
private final CommitmentPolicy requireReadPolicy = CommitmentPolicy.RequireEncryptRequireDecrypt;
private final List<CommitmentPolicy> allowReadPolicies =
Arrays.asList(
CommitmentPolicy.RequireEncryptAllowDecrypt, CommitmentPolicy.ForbidEncryptAllowDecrypt);
private final SignaturePolicy signaturePolicy = SignaturePolicy.AllowEncryptAllowDecrypt;
@Before
public void init() {
masterKeyProvider_ = new StaticMasterKey("testmaterial");
}
@Test(expected = NullPointerException.class)
public void nullMasterKey() {
DecryptionHandler.create(
(MasterKey) null,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
}
@Test
public void nullCommitment() {
final byte[] ciphertext =
getTestHeaders(
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
CommitmentPolicy.ForbidEncryptAllowDecrypt);
assertThrows(
NullPointerException.class,
() ->
DecryptionHandler.create(
masterKeyProvider_,
new ParsedCiphertext(ciphertext),
null,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS));
assertThrows(
NullPointerException.class,
() ->
DecryptionHandler.create(
masterKeyProvider_,
null,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS));
}
@Test
public void nullSignaturePolicy() {
final byte[] ciphertext =
getTestHeaders(
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
CommitmentPolicy.ForbidEncryptAllowDecrypt);
assertThrows(
NullPointerException.class,
() ->
DecryptionHandler.create(
masterKeyProvider_,
new ParsedCiphertext(ciphertext),
commitmentPolicy,
null,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS));
assertThrows(
NullPointerException.class,
() ->
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
null,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS));
}
@Test(expected = AwsCryptoException.class)
public void invalidLenProcessBytes() {
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] in = new byte[1];
final byte[] out = new byte[1];
decryptionHandler.processBytes(in, 0, -1, out, 0);
}
@Test(expected = AwsCryptoException.class)
public void maxLenProcessBytes() {
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
// Create input of size 3 bytes: 1 byte containing version, 1 byte
// containing type, and 1 byte containing half of the algoId short
// primitive. Only 1 byte of the algoId is provided because this
// forces the decryption handler to buffer that 1 byte while waiting for
// the other byte. We do this so we can specify an input of max
// value and the total bytes to parse will become max value + 1.
final byte[] in = new byte[3];
final byte[] out = new byte[3];
in[1] = CiphertextType.CUSTOMER_AUTHENTICATED_ENCRYPTED_DATA.getValue();
decryptionHandler.processBytes(in, 0, in.length, out, 0);
decryptionHandler.processBytes(in, 0, Integer.MAX_VALUE, out, 0);
}
@Test
public void maxInputLength() {
final byte[] testMessage =
getTestMessage(
TestUtils.DEFAULT_TEST_CRYPTO_ALG, CommitmentPolicy.RequireEncryptRequireDecrypt);
final byte[] out = new byte[100];
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
decryptionHandler.setMaxInputLength(testMessage.length - 1);
assertThrows(
IllegalStateException.class,
() -> decryptionHandler.processBytes(testMessage, 0, testMessage.length, out, 0));
}
@Test
public void maxInputLengthIncludingParsedCiphertext() {
final byte[] testMessage =
getTestMessage(
TestUtils.DEFAULT_TEST_CRYPTO_ALG, CommitmentPolicy.RequireEncryptRequireDecrypt);
final byte[] out = new byte[100];
ParsedCiphertext parsedHeaders = new ParsedCiphertext(testMessage);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
parsedHeaders,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
decryptionHandler.setMaxInputLength(testMessage.length - 1);
assertThrows(
IllegalStateException.class,
() ->
decryptionHandler.processBytes(
testMessage,
parsedHeaders.getOffset(),
testMessage.length - parsedHeaders.getOffset(),
out,
0));
}
@Test
public void maxInputLengthIncludingCiphertextHeaders() {
final byte[] testMessage =
getTestMessage(
TestUtils.DEFAULT_TEST_CRYPTO_ALG, CommitmentPolicy.RequireEncryptRequireDecrypt);
final byte[] out = new byte[100];
ParsedCiphertext parsedHeaders = new ParsedCiphertext(testMessage);
CiphertextHeaders headers = new CiphertextHeaders();
headers.deserialize(
parsedHeaders.getCiphertext(), 0, CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
headers,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
decryptionHandler.setMaxInputLength(testMessage.length - 1);
assertThrows(
IllegalStateException.class,
() ->
decryptionHandler.processBytes(
testMessage,
parsedHeaders.getOffset(),
testMessage.length - parsedHeaders.getOffset(),
out,
0));
}
@Test(expected = BadCiphertextException.class)
public void headerIntegrityFailure() {
byte[] ciphertext = getTestHeaders();
// tamper the fifth byte in the header which corresponds to the first
// byte of the message identifier. We do this because tampering the
// first four bytes will be detected as invalid values during parsing.
ciphertext[5] += 1;
// attempt to decrypt with the tampered header.
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(ciphertext.length);
final byte[] plaintext = new byte[plaintextLen];
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
}
@Test(expected = BadCiphertextException.class)
public void invalidVersion() {
byte[] ciphertext = getTestHeaders();
// set byte containing version to invalid value.
ciphertext[0] = 0; // NOTE: This will need to be updated should 0 ever be a valid version
// attempt to decrypt with the tampered header.
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(ciphertext.length);
final byte[] plaintext = new byte[plaintextLen];
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
}
@Test(expected = AwsCryptoException.class)
public void invalidCMK() {
final byte[] ciphertext = getTestHeaders();
masterKeyProvider_.setKeyId(masterKeyProvider_.getKeyId() + "nonsense");
// attempt to decrypt with the tampered header.
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(ciphertext.length);
final byte[] plaintext = new byte[plaintextLen];
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
}
@Test
public void validAlgForCommitmentPolicyCreate() {
// ensure we can decrypt non-committing algs with the policies that allow it
final CryptoAlgorithm nonCommittingAlg = CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA256;
for (CommitmentPolicy policy : allowReadPolicies) {
final byte[] ciphertext =
getTestHeaders(nonCommittingAlg, CommitmentPolicy.ForbidEncryptAllowDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
policy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
// expected plaintext is zero length
final byte[] plaintext = new byte[0];
ProcessingSummary processingSummary =
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
assertEquals(ciphertext.length, processingSummary.getBytesProcessed());
assertArrayEquals(new byte[0], plaintext);
}
// ensure we can decrypt committing algs with all policies
final CryptoAlgorithm committingAlg = CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY;
for (CommitmentPolicy policy : CommitmentPolicy.values()) {
final byte[] ciphertext =
getTestHeaders(committingAlg, CommitmentPolicy.RequireEncryptRequireDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
policy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
// expected plaintext is zero length
final byte[] plaintext = new byte[0];
ProcessingSummary processingSummary =
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
assertEquals(ciphertext.length, processingSummary.getBytesProcessed());
assertArrayEquals(new byte[0], plaintext);
}
}
@Test
public void invalidAlgForCommitmentPolicyCreateWithoutHeaders() {
final CryptoAlgorithm nonCommittingAlg =
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384;
final byte[] ciphertext =
getTestHeaders(nonCommittingAlg, CommitmentPolicy.ForbidEncryptAllowDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
requireReadPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(ciphertext.length);
final byte[] plaintext = new byte[plaintextLen];
assertThrows(
AwsCryptoException.class,
() -> decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0));
}
@Test
public void invalidAlgForCommitmentPolicyCreateWithHeaders() {
final CryptoAlgorithm nonCommittingAlg =
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384;
final byte[] ciphertext =
getTestHeaders(nonCommittingAlg, CommitmentPolicy.ForbidEncryptAllowDecrypt);
assertThrows(
AwsCryptoException.class,
() ->
DecryptionHandler.create(
masterKeyProvider_,
new ParsedCiphertext(ciphertext),
requireReadPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS));
}
@Test
public void validAlgForSignaturePolicyCreate() {
// ensure we can decrypt non-signing algs with the policy that allows it
final CryptoAlgorithm nonSigningAlg = CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY;
final byte[] ciphertext = getTestHeaders(nonSigningAlg, commitmentPolicy);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
SignaturePolicy.AllowEncryptAllowDecrypt,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
// expected plaintext is zero length
final byte[] plaintext = new byte[0];
ProcessingSummary processingSummary =
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
assertEquals(ciphertext.length, processingSummary.getBytesProcessed());
assertArrayEquals(new byte[0], plaintext);
}
@Test
public void invalidAlgForSignaturePolicyCreateWithoutHeaders() {
final CryptoAlgorithm signingAlg =
CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384;
final byte[] ciphertext = getTestHeaders(signingAlg, commitmentPolicy);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
SignaturePolicy.AllowEncryptForbidDecrypt,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(ciphertext.length);
final byte[] plaintext = new byte[plaintextLen];
assertThrows(
AwsCryptoException.class,
() -> decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0));
}
@Test
public void invalidAlgForSignaturePolicyCreateWithHeaders() {
final CryptoAlgorithm signingAlg =
CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384;
final byte[] ciphertext = getTestHeaders(signingAlg, commitmentPolicy);
assertThrows(
AwsCryptoException.class,
() ->
DecryptionHandler.create(
masterKeyProvider_,
new ParsedCiphertext(ciphertext),
commitmentPolicy,
SignaturePolicy.AllowEncryptForbidDecrypt,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS));
}
private byte[] getTestHeaders() {
return getTestHeaders(
TestUtils.DEFAULT_TEST_CRYPTO_ALG, TestUtils.DEFAULT_TEST_COMMITMENT_POLICY, 1);
}
private byte[] getTestHeaders(CryptoAlgorithm cryptoAlgorithm, CommitmentPolicy policy) {
return getTestHeaders(cryptoAlgorithm, policy, 1);
}
private byte[] getTestHeaders(int numEdks) {
return getTestHeaders(
TestUtils.DEFAULT_TEST_CRYPTO_ALG, TestUtils.DEFAULT_TEST_COMMITMENT_POLICY, numEdks);
}
private byte[] getTestHeaders(
CryptoAlgorithm cryptoAlgorithm, CommitmentPolicy policy, int numEdks) {
// Note that it's questionable to assume that failing to call doFinal() on the encryption
// handler
// always results in only outputting the header!
return getTestMessage(cryptoAlgorithm, policy, numEdks, false);
}
private byte[] getTestMessage(CryptoAlgorithm cryptoAlgorithm, CommitmentPolicy policy) {
return getTestMessage(cryptoAlgorithm, policy, 1, true);
}
private byte[] getTestMessage(
CryptoAlgorithm cryptoAlgorithm, CommitmentPolicy policy, int numEdks, boolean doFinal) {
final int frameSize_ = AwsCrypto.getDefaultFrameSize();
final Map<String, String> encryptionContext = Collections.<String, String>emptyMap();
final EncryptionMaterialsRequest encryptionMaterialsRequest =
EncryptionMaterialsRequest.newBuilder()
.setContext(encryptionContext)
.setRequestedAlgorithm(cryptoAlgorithm)
.setCommitmentPolicy(policy)
.build();
List<MasterKeyProvider<?>> providers = new ArrayList<>();
for (int i = 0; i < numEdks; i++) {
providers.add(masterKeyProvider_);
}
MasterKeyProvider<?> provider = MultipleProviderFactory.buildMultiProvider(providers);
final EncryptionMaterials encryptionMaterials =
new DefaultCryptoMaterialsManager(provider)
.getMaterialsForEncrypt(encryptionMaterialsRequest);
final EncryptionHandler encryptionHandler =
new EncryptionHandler(frameSize_, encryptionMaterials, policy);
// create the ciphertext headers by calling encryption handler.
final byte[] in = new byte[0];
final int ciphertextLen = encryptionHandler.estimateOutputSize(in.length);
final byte[] ciphertext = new byte[ciphertextLen];
ProcessingSummary summary = encryptionHandler.processBytes(in, 0, in.length, ciphertext, 0);
if (doFinal) {
encryptionHandler.doFinal(ciphertext, summary.getBytesWritten());
}
return ciphertext;
}
@Test(expected = AwsCryptoException.class)
public void invalidOffsetProcessBytes() {
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] in = new byte[1];
final byte[] out = new byte[1];
decryptionHandler.processBytes(in, -1, in.length, out, 0);
}
@Test(expected = BadCiphertextException.class)
public void incompleteCiphertext() {
byte[] ciphertext = getTestHeaders();
CiphertextHeaders h = new CiphertextHeaders();
h.deserialize(ciphertext, 0, CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
commitmentPolicy,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] out = new byte[1];
// Note the " - 1" is a bit deceptive: the ciphertext SHOULD already be incomplete because we
// called getTestHeaders() above, so the whole body is missing!
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length - 1, out, 0);
decryptionHandler.doFinal(out, 0);
}
@Test
public void incompleteCiphertextV2() {
byte[] ciphertext = Utils.decodeBase64String(TestUtils.messageWithCommitKeyBase64);
final DecryptionHandler<JceMasterKey> decryptionHandler =
DecryptionHandler.create(
TestUtils.messageWithCommitKeyMasterKey,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] out = new byte[1];
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length - 1, out, 0);
assertThrows(
BadCiphertextException.class,
"Unable to process entire ciphertext.",
() -> decryptionHandler.doFinal(out, 0));
}
@Test
public void incompleteCiphertextSigned() {
byte[] ciphertext =
getTestMessage(
CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
CommitmentPolicy.RequireEncryptRequireDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] out = new byte[1];
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length - 1, out, 0);
assertThrows(
BadCiphertextException.class,
"Unable to process entire ciphertext.",
() -> decryptionHandler.doFinal(out, 0));
}
@Test
public void headerV2HeaderIntegrityFailure() {
byte[] ciphertext = Utils.decodeBase64String(TestUtils.messageWithCommitKeyBase64);
// Tamper the bytes that corresponds to the frame length.
// This is the only reasonable way to tamper with this handcrafted message's
// header which can still be successfully parsed.
ciphertext[134] += 1;
// attempt to decrypt with the tampered header.
final DecryptionHandler<JceMasterKey> decryptionHandler =
DecryptionHandler.create(
TestUtils.messageWithCommitKeyMasterKey,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(ciphertext.length);
final byte[] plaintext = new byte[plaintextLen];
assertThrows(
BadCiphertextException.class,
"Header integrity check failed",
() -> decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0));
}
@Test
public void headerV2BodyIntegrityFailure() {
byte[] ciphertext = Utils.decodeBase64String(TestUtils.messageWithCommitKeyBase64);
// Tamper the bytes that corresponds to the body auth
ciphertext[ciphertext.length - 1] += 1;
// attempt to decrypt with the tampered header.
final DecryptionHandler<JceMasterKey> decryptionHandler =
DecryptionHandler.create(
TestUtils.messageWithCommitKeyMasterKey,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(ciphertext.length);
final byte[] plaintext = new byte[plaintextLen];
assertThrows(
BadCiphertextException.class,
"Tag mismatch",
() -> decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0));
}
@Test
public void withLessThanMaxEdks() {
byte[] header = getTestHeaders(2);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_, CommitmentPolicy.RequireEncryptAllowDecrypt, signaturePolicy, 3);
final int plaintextLen = decryptionHandler.estimateOutputSize(header.length);
final byte[] plaintext = new byte[plaintextLen];
decryptionHandler.processBytes(header, 0, header.length, plaintext, 0);
}
@Test
public void withMaxEdks() {
byte[] header = getTestHeaders(3);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_, CommitmentPolicy.RequireEncryptAllowDecrypt, signaturePolicy, 3);
final int plaintextLen = decryptionHandler.estimateOutputSize(header.length);
final byte[] plaintext = new byte[plaintextLen];
decryptionHandler.processBytes(header, 0, header.length, plaintext, 0);
}
@Test
public void withMoreThanMaxEdks() {
byte[] header = getTestHeaders(4);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_, CommitmentPolicy.RequireEncryptAllowDecrypt, signaturePolicy, 3);
final int plaintextLen = decryptionHandler.estimateOutputSize(header.length);
final byte[] plaintext = new byte[plaintextLen];
assertThrows(
AwsCryptoException.class,
"Ciphertext encrypted data keys exceed maxEncryptedDataKeys",
() -> decryptionHandler.processBytes(header, 0, header.length, plaintext, 0));
}
@Test
public void withNoMaxEdks() {
byte[] header = getTestHeaders(1 << 16 - 1);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptAllowDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final int plaintextLen = decryptionHandler.estimateOutputSize(header.length);
final byte[] plaintext = new byte[plaintextLen];
decryptionHandler.processBytes(header, 0, header.length, plaintext, 0);
}
@Test
public void validSignatureAcrossMultipleBlocks() {
byte[] ciphertext =
getTestMessage(
CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
CommitmentPolicy.RequireEncryptRequireDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] out = new byte[1];
// Parse the header and body
final int headerLength = 388;
decryptionHandler.processBytes(ciphertext, 0, headerLength, out, 0);
// Parse the footer across two calls
// This used to fail to verify the signature because partial bytes were dropped instead.
// The overall decryption would still succeed because the completeness check in doFinal
// used to not include the footer.
// The number of bytes read in the first chunk is completely arbitrary. The
// parameterized CryptoOutputStreamTest tests covers lots of possible chunk
// sizes much more thoroughly. This is just a very explicit regression unit test for a known
// issue that is now fixed.
final int firstChunkLength = 12;
final int firstChunkOffset = headerLength;
final int secondChunkOffset = headerLength + firstChunkLength;
final int secondChunkLength = ciphertext.length - secondChunkOffset;
decryptionHandler.processBytes(ciphertext, firstChunkOffset, firstChunkLength, out, 0);
decryptionHandler.processBytes(ciphertext, secondChunkOffset, secondChunkLength, out, 0);
decryptionHandler.doFinal(out, 0);
}
@Test
public void invalidSignatureAcrossMultipleBlocks() {
byte[] ciphertext =
getTestMessage(
CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
CommitmentPolicy.RequireEncryptRequireDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] out = new byte[1];
// Parse the header and body
decryptionHandler.processBytes(ciphertext, 0, 388, out, 0);
// Process extra bytes before processing the actual signature bytes.
// This used to actually work because the handler failed to buffer the unparsed bytes
// across calls. To regression test this properly we have to parse the two bytes for the
// length...
decryptionHandler.processBytes(ciphertext, 388, 2, out, 0);
// ...and after that any bytes fewer than that length would previously be dropped.
decryptionHandler.processBytes(new byte[10], 0, 10, out, 0);
assertThrows(
BadCiphertextException.class,
"Bad trailing signature",
() -> decryptionHandler.processBytes(ciphertext, 390, ciphertext.length - 390, out, 0));
}
@Test
public void setMaxInputLength() {
byte[] ciphertext =
getTestMessage(
CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
CommitmentPolicy.RequireEncryptRequireDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
decryptionHandler.setMaxInputLength(ciphertext.length - 1);
assertEquals(decryptionHandler.getMaxInputLength(), (long) ciphertext.length - 1);
final byte[] out = new byte[1];
assertThrows(
IllegalStateException.class,
"Ciphertext size exceeds size bound",
() -> decryptionHandler.processBytes(ciphertext, 0, ciphertext.length, out, 0));
}
@Test
public void setMaxInputLengthThrowsIfAlreadyOver() {
byte[] ciphertext =
getTestMessage(
CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384,
CommitmentPolicy.RequireEncryptRequireDecrypt);
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
final byte[] out = new byte[1];
decryptionHandler.processBytes(ciphertext, 0, ciphertext.length - 1, out, 0);
assertFalse(decryptionHandler.isComplete());
assertThrows(
IllegalStateException.class,
"Ciphertext size exceeds size bound",
() -> decryptionHandler.setMaxInputLength(ciphertext.length - 2));
}
@Test
public void setMaxInputLengthAcceptsSmallerValue() {
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
decryptionHandler.setMaxInputLength(100);
assertEquals(decryptionHandler.getMaxInputLength(), 100);
decryptionHandler.setMaxInputLength(10);
assertEquals(decryptionHandler.getMaxInputLength(), 10);
}
@Test
public void setMaxInputLengthIgnoresLargerValue() {
final DecryptionHandler<StaticMasterKey> decryptionHandler =
DecryptionHandler.create(
masterKeyProvider_,
CommitmentPolicy.RequireEncryptRequireDecrypt,
signaturePolicy,
CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS);
decryptionHandler.setMaxInputLength(10);
assertEquals(decryptionHandler.getMaxInputLength(), 10);
decryptionHandler.setMaxInputLength(100);
assertEquals(decryptionHandler.getMaxInputLength(), 10);
}
}