Skip to content

ParsedCiphertext throws an error if it is not complete #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b3df03b
Adding check for if ciphertext is complete in the ParsedCiphertext co…
shayvana Jul 23, 2019
62b36df
Wrote test for check for if ciphertext is complete in the ParsedCiphe…
shayvana Jul 23, 2019
5ebec3e
Fixed ParsedCiphertext bug
shayvana Jul 24, 2019
b9b64ed
Code does not break build
shayvana Jul 25, 2019
e326d12
Removed test in CiphertextHeadersTest
shayvana Jul 31, 2019
85da442
Created test file for ParsedCiphertext and wrote a test for initializ…
shayvana Aug 1, 2019
3aad539
Wrote happy test for ParsedCiphertext constructor
shayvana Aug 1, 2019
da0938b
Removed CiphertextHeadersTest file from changes
shayvana Aug 12, 2019
a0c93ec
Removed declaration of RuntimeException in a throws statement on the …
shayvana Aug 14, 2019
db1fc9d
Wrote tests for incomplete ciphertexts of zero and single byte length
shayvana Aug 14, 2019
39883c2
Wrote test for encrypting and decrypting with ParsedCiphertext
shayvana Aug 14, 2019
9590174
Added test for good ParsedCipherText
shayvana Aug 16, 2019
ece073c
Added test cases for constructing an incomplete ParsedCipherText with…
shayvana Aug 16, 2019
27a4459
Updated copyright
shayvana Aug 16, 2019
fb23a71
Added back test file I accidentally deleted
shayvana Aug 16, 2019
5e5c2db
Removed assertEquals check
shayvana Aug 16, 2019
ff29cee
Asserted that offset is greater than 0
shayvana Aug 16, 2019
566975a
Merge branch 'master' into ParsedCiphertext-bug-fix
shayvana Aug 16, 2019
7b7b703
Removed unneeded import statement and CiphertextHeadersTest from changes
shayvana Aug 16, 2019
d89ff00
Merge branch 'ParsedCiphertext-bug-fix' of github.com:sprovine/aws-en…
shayvana Aug 16, 2019
22c3e7d
Updated offset check in good ciphertext test
shayvana Aug 16, 2019
3dd630c
Updated test for almost valid ciphertext input for ParsedCipherText
shayvana Aug 16, 2019
07f60a6
Set incomplete single byte ciphertext to a valid version
shayvana Aug 16, 2019
ad9f4af
Merge branch 'master' into ParsedCiphertext-bug-fix
SalusaSecondus Oct 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
* in compliance with the License. A copy of the License is located at
*
*
* http://aws.amazon.com/apache2.0
*
*
* or in the "license" file accompanying this file. This file 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.
Expand All @@ -15,6 +15,7 @@

import com.amazonaws.encryptionsdk.internal.Utils;
import com.amazonaws.encryptionsdk.model.CiphertextHeaders;
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
* in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

package com.amazonaws.encryptionsdk;

import com.amazonaws.encryptionsdk.internal.StaticMasterKey;
import com.amazonaws.encryptionsdk.internal.VersionInfo;
import com.amazonaws.encryptionsdk.model.CiphertextHeaders;
import org.junit.Before;
import org.junit.Test;

import com.amazonaws.encryptionsdk.exception.BadCiphertextException;

import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;

import static org.junit.Assert.*;
import static org.mockito.Mockito.spy;

public class ParsedCiphertextTest extends CiphertextHeaders {
private StaticMasterKey masterKeyProvider;
private AwsCrypto encryptionClient_;

@Before
public void init() {
masterKeyProvider = spy(new StaticMasterKey("testmaterial"));

encryptionClient_ = new AwsCrypto();
encryptionClient_.setEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256);
}

@Test()
public void goodParsedCiphertext() {
final int byteSize = 0;
final int frameSize = 0;
final byte[] plaintextBytes = new byte[byteSize];

final Map<String, String> encryptionContext = new HashMap<String, String>(1);
encryptionContext.put("ENC1", "ParsedCiphertext test with %d" + byteSize);

encryptionClient_.setEncryptionFrameSize(frameSize);

final byte[] cipherText = encryptionClient_.encryptData(
masterKeyProvider,
plaintextBytes,
encryptionContext).getResult();
final ParsedCiphertext pCt = new ParsedCiphertext(cipherText);

assertNotNull(pCt.getCiphertext());
assertTrue(pCt.getOffset() > 0);
}

@Test(expected = BadCiphertextException.class)
public void incompleteZeroByteCiphertext() {
final byte[] cipherText = {};
ParsedCiphertext pCt = new ParsedCiphertext(cipherText);
}

@Test(expected = BadCiphertextException.class)
public void incompleteSingleByteCiphertext() {
final byte[] cipherText = {VersionInfo.CURRENT_CIPHERTEXT_VERSION};
ParsedCiphertext pCt = new ParsedCiphertext(cipherText);
}

@Test(expected = BadCiphertextException.class)
public void incompleteCiphertext() {
final int byteSize = 0;
final int frameSize = 0;
final byte[] plaintextBytes = new byte[byteSize];

final Map<String, String> encryptionContext = new HashMap<String, String>(1);
encryptionContext.put("ENC1", "ParsedCiphertext test with %d" + byteSize);

encryptionClient_.setEncryptionFrameSize(frameSize);

final byte[] cipherText = encryptionClient_.encryptData(
masterKeyProvider,
plaintextBytes,
encryptionContext).getResult();
ParsedCiphertext pCt = new ParsedCiphertext(cipherText);

byte[] incompleteCiphertext = Arrays.copyOf(pCt.getCiphertext(), pCt.getOffset() - 1);
ParsedCiphertext badPCt = new ParsedCiphertext(incompleteCiphertext);
}
}