-
Notifications
You must be signed in to change notification settings - Fork 122
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
Changes from 21 commits
b3df03b
62b36df
5ebec3e
b9b64ed
e326d12
85da442
3aad539
da0938b
a0c93ec
db1fc9d
39883c2
9590174
ece073c
27a4459
fb23a71
5e5c2db
ff29cee
566975a
7b7b703
d89ff00
22c3e7d
3dd630c
07f60a6
ad9f4af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.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 = {0}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the fix we made to checking the version, we need to make sure this is a valid version (i.e. |
||
ParsedCiphertext pCt = new ParsedCiphertext(cipherText); | ||
lavaleri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@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(); | ||
byte[] incompleteCipherText = Arrays.copyOfRange(cipherText, 1, cipherText.length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't testing what you think because it is dropping the first byte of the header while maintaining the result of the ciphertext. This will result in a complete but invalid) header. That isn't what you are trying to test. Try this:
|
||
|
||
final ParsedCiphertext pCt = new ParsedCiphertext(incompleteCipherText); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.