Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,19 @@ public int doFinal(final byte[] out, final int outOff) throws BadCiphertextExcep
throw new IllegalStateException("Attempted to call doFinal twice");
}

int written = 0;
if (firstOperation_) {
// An empty message still needs its header written and included in the signature.
written = processBytes(new byte[0], 0, 0, out, outOff).getBytesWritten();
}

complete_ = true;

checkPlaintextSizeLimit(0);

int written = contentCryptoHandler_.doFinal(out, outOff);
updateTrailingSignature(out, outOff, written);
final int finalWritten = contentCryptoHandler_.doFinal(out, outOff + written);
updateTrailingSignature(out, outOff + written, finalWritten);
written += finalWritten;
if (cryptoAlgo_.getTrailingSignatureLength() > 0) {
try {
CiphertextFooters footer = new CiphertextFooters(signContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ public void checkInvalidValues() throws Exception {
final int frameSize = frameSizeToTest[i];
invalidWriteLen(frameSize);
invalidWriteOffset(frameSize);
noOpStream(frameSize);
}
}

Expand Down Expand Up @@ -494,22 +493,39 @@ private void invalidWriteOffset(final int frameSize)
() -> encryptionOutStream.write(writeBytes, writeBytes.length + 1, writeBytes.length));
}

private void noOpStream(final int frameSize) throws Exception {
@Test
public void closeWithoutWrite() throws Exception {
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
for (final int frameSize : new int[] {0, getDefaultFrameSize()}) {
noOpStream(cryptoAlg, frameSize);
}
}
}

private void noOpStream(final CryptoAlgorithm cryptoAlg, final int frameSize) throws Exception {
final AwsCrypto awsCrypto =
AwsCrypto.builder()
.withCommitmentPolicy(
cryptoAlg.isCommitting()
? CommitmentPolicy.RequireEncryptRequireDecrypt
: CommitmentPolicy.ForbidEncryptAllowDecrypt)
.withEncryptionAlgorithm(cryptoAlg)
.withEncryptionFrameSize(frameSize)
.build();
final Map<String, String> encryptionContext = new HashMap<String, String>(1);
encryptionContext.put("ENC", "noOpStream size " + frameSize);

final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
final OutputStream encryptionOutStream =
encryptionClient_.createEncryptingStream(customerMasterKey, outStream, encryptionContext);
awsCrypto.createEncryptingStream(customerMasterKey, outStream, encryptionContext);

encryptionOutStream.close();

testRoundTrip(
0,
crypto -> crypto.setEncryptionFrameSize(frameSize),
encryptWithContext(encryptionContext),
basicDecrypt(),
CommitmentPolicy.RequireEncryptRequireDecrypt);
final CryptoResult<byte[], JceMasterKey> result =
awsCrypto.decryptData(customerMasterKey, outStream.toByteArray());
assertArrayEquals(new byte[0], result.getResult());
assertEquals(cryptoAlg, result.getCryptoAlgorithm());
assertEquals(encryptionContext.get("ENC"), result.getEncryptionContext().get("ENC"));
}

@Test
Expand Down