Skip to content
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

Adding a validate version error handling as discussed in issue #113 #116

Merged
merged 14 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ target/
.classpath
/bin/
.idea/
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.amazonaws.encryptionsdk.internal.Constants;
import com.amazonaws.encryptionsdk.internal.EncryptionContextSerializer;
import com.amazonaws.encryptionsdk.internal.PrimitivesParser;
import com.amazonaws.encryptionsdk.internal.VersionInfo;

/**
* This class implements the headers for the message (ciphertext) produced by
Expand Down Expand Up @@ -179,6 +180,9 @@ public Boolean isComplete() {
*/
private int parseVersion(final byte[] b, final int off) throws ParseException {
version_ = PrimitivesParser.parseByte(b, off);
if (CiphertextType.deserialize(version_).getValue() != VersionInfo.CURRENT_CIPHERTEXT_VERSION ) {
throw new BadCiphertextException("Invalid version type.");
}
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ private void readUptoNonceLen(final ByteBuffer headerBuff) {
headerBuff.get();
}

@Test(expected = BadCiphertextException.class)
public void invalidVersion(){
final Map<String, String> encryptionContext = new HashMap<String, String>(1);
encryptionContext.put("ENC", "CiphertextHeader Streaming Test");

final CiphertextHeaders ciphertextHeaders = createCiphertextHeaders(encryptionContext);
final byte[] headerBytes = ciphertextHeaders.toByteArray();
final ByteBuffer headerBuff = ByteBuffer.wrap(headerBytes);

readUptoType(headerBuff);

//set version to invalid value of 0.
headerBuff.put((byte) 0);

final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders();
reconstructedHeaders.deserialize(headerBuff.array(), 0);
}

@Test(expected = BadCiphertextException.class)
public void invalidType() {
final Map<String, String> encryptionContext = new HashMap<String, String>(1);
Expand Down