Skip to content

Commit

Permalink
Merge pull request #116 from josecorella/cipherTextHeader
Browse files Browse the repository at this point in the history
Adding a validate version error handling as discussed in issue #113
  • Loading branch information
lizroth authored Aug 16, 2019
2 parents 221800b + 5b0dfec commit 325bf21
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
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 (version_ != VersionInfo.CURRENT_CIPHERTEXT_VERSION) {
throw new BadCiphertextException("Invalid version ");
}
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ 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);

//set version to invalid type 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

0 comments on commit 325bf21

Please sign in to comment.