Skip to content

Commit f2daec3

Browse files
Merge pull request #119 from sprovine/ParsedCiphertext-bug-fix
ParsedCiphertext throws an error if it is not complete
2 parents cbbcb1c + ad9f4af commit f2daec3

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

src/main/java/com/amazonaws/encryptionsdk/ParsedCiphertext.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
55
* in compliance with the License. A copy of the License is located at
6-
*
6+
*
77
* http://aws.amazon.com/apache2.0
8-
*
8+
*
99
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
1010
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111
* specific language governing permissions and limitations under the License.
@@ -15,6 +15,7 @@
1515

1616
import com.amazonaws.encryptionsdk.internal.Utils;
1717
import com.amazonaws.encryptionsdk.model.CiphertextHeaders;
18+
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
1819

1920
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2021

@@ -36,6 +37,9 @@ public class ParsedCiphertext extends CiphertextHeaders {
3637
public ParsedCiphertext(final byte[] ciphertext) {
3738
ciphertext_ = Utils.assertNonNull(ciphertext, "ciphertext");
3839
offset_ = deserialize(ciphertext_, 0);
40+
if (!this.isComplete()) {
41+
throw new BadCiphertextException("Incomplete ciphertext.");
42+
}
3943
}
4044

4145
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.amazonaws.encryptionsdk;
15+
16+
import com.amazonaws.encryptionsdk.internal.StaticMasterKey;
17+
import com.amazonaws.encryptionsdk.internal.VersionInfo;
18+
import com.amazonaws.encryptionsdk.model.CiphertextHeaders;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
import java.util.Arrays;
27+
28+
import static org.junit.Assert.*;
29+
import static org.mockito.Mockito.spy;
30+
31+
public class ParsedCiphertextTest extends CiphertextHeaders {
32+
private StaticMasterKey masterKeyProvider;
33+
private AwsCrypto encryptionClient_;
34+
35+
@Before
36+
public void init() {
37+
masterKeyProvider = spy(new StaticMasterKey("testmaterial"));
38+
39+
encryptionClient_ = new AwsCrypto();
40+
encryptionClient_.setEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256);
41+
}
42+
43+
@Test()
44+
public void goodParsedCiphertext() {
45+
final int byteSize = 0;
46+
final int frameSize = 0;
47+
final byte[] plaintextBytes = new byte[byteSize];
48+
49+
final Map<String, String> encryptionContext = new HashMap<String, String>(1);
50+
encryptionContext.put("ENC1", "ParsedCiphertext test with %d" + byteSize);
51+
52+
encryptionClient_.setEncryptionFrameSize(frameSize);
53+
54+
final byte[] cipherText = encryptionClient_.encryptData(
55+
masterKeyProvider,
56+
plaintextBytes,
57+
encryptionContext).getResult();
58+
final ParsedCiphertext pCt = new ParsedCiphertext(cipherText);
59+
60+
assertNotNull(pCt.getCiphertext());
61+
assertTrue(pCt.getOffset() > 0);
62+
}
63+
64+
@Test(expected = BadCiphertextException.class)
65+
public void incompleteZeroByteCiphertext() {
66+
final byte[] cipherText = {};
67+
ParsedCiphertext pCt = new ParsedCiphertext(cipherText);
68+
}
69+
70+
@Test(expected = BadCiphertextException.class)
71+
public void incompleteSingleByteCiphertext() {
72+
final byte[] cipherText = {VersionInfo.CURRENT_CIPHERTEXT_VERSION};
73+
ParsedCiphertext pCt = new ParsedCiphertext(cipherText);
74+
}
75+
76+
@Test(expected = BadCiphertextException.class)
77+
public void incompleteCiphertext() {
78+
final int byteSize = 0;
79+
final int frameSize = 0;
80+
final byte[] plaintextBytes = new byte[byteSize];
81+
82+
final Map<String, String> encryptionContext = new HashMap<String, String>(1);
83+
encryptionContext.put("ENC1", "ParsedCiphertext test with %d" + byteSize);
84+
85+
encryptionClient_.setEncryptionFrameSize(frameSize);
86+
87+
final byte[] cipherText = encryptionClient_.encryptData(
88+
masterKeyProvider,
89+
plaintextBytes,
90+
encryptionContext).getResult();
91+
ParsedCiphertext pCt = new ParsedCiphertext(cipherText);
92+
93+
byte[] incompleteCiphertext = Arrays.copyOf(pCt.getCiphertext(), pCt.getOffset() - 1);
94+
ParsedCiphertext badPCt = new ParsedCiphertext(incompleteCiphertext);
95+
}
96+
}

0 commit comments

Comments
 (0)