From 37d57b5bb953e02d4fb1017bdc4d4a32eb141c97 Mon Sep 17 00:00:00 2001 From: Benjamin Farley Date: Tue, 26 Oct 2021 13:20:21 -0600 Subject: [PATCH 1/2] chore: Add back removed CiphertextHeaders.deserialize method --- .../model/CiphertextHeaders.java | 35 ++++++++++++++++--- .../model/CiphertextHeadersTest.java | 23 +++++++++++- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java b/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java index 94a35cc21..fee151ba2 100644 --- a/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java +++ b/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java @@ -503,20 +503,37 @@ private int parseComplete(final byte[] b, final int off) throws ParseException { return 0; } + /** * Deserialize the provided bytes starting at the specified offset to construct an instance of - * this class. + * this class. Uses the default value for maxEncryptedDataKeys, which results in no limit. * - *

This method parses the provided bytes for the individual fields in this class. This methods + *

This method parses the provided bytes for the individual fields in this class. This method * also supports partial parsing where not all the bytes required for parsing the fields * successfully are available. * * @param b the byte array to deserialize. * @param off the offset in the byte array to use for deserialization. - * @param maxEncryptedDataKeys the maximum number of EDKs to deserialize; zero indicates no - * maximum * @return the number of bytes consumed in deserialization. */ + public int deserialize(final byte[] b, final int off) throws ParseException { + return deserialize(b, off, NO_MAX_ENCRYPTED_DATA_KEYS); + } + + /** + * Deserialize the provided bytes starting at the specified offset to construct an instance of + * this class. + * + *

This method parses the provided bytes for the individual fields in this class. This method + * also supports partial parsing where not all the bytes required for parsing the fields + * successfully are available. + * + * @param b the byte array to deserialize. + * @param off the offset in the byte array to use for deserialization. + * @param maxEncryptedDataKeys the maximum number of EDKs to deserialize; zero indicates no + * maximum + * @return the number of bytes consumed in deserialization. + */ public int deserialize(final byte[] b, final int off, int maxEncryptedDataKeys) throws ParseException { if (b == null) { @@ -835,6 +852,16 @@ public void setSuiteData(byte[] suiteData) { suiteData_ = suiteData.clone(); } + /** + * Return max encrypted data keys + * Package scope for unit testing. + * + * @return int + */ + int getMaxEncryptedDataKeys() { + return maxEncryptedDataKeys_; + } + private static class PartialParseException extends Exception { private static final long serialVersionUID = 1L; final int bytesParsed_; diff --git a/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java b/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java index 8dc1cac5d..13c20a42b 100644 --- a/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java +++ b/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java @@ -51,6 +51,8 @@ public class CiphertextHeadersTest { @Test public void serializeDeserialize() { + int maxEncryptedDataKeys = 42; + Map encryptionContext = new HashMap(1); encryptionContext.put("ENC", "CiphertextHeader Test"); @@ -60,9 +62,28 @@ public void serializeDeserialize() { final byte[] headerBytes = ciphertextHeaders.toByteArray(); final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders(); reconstructedHeaders.deserialize( - headerBytes, 0, CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS); + headerBytes, 0, maxEncryptedDataKeys); + final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray(); + + assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), maxEncryptedDataKeys); + assertArrayEquals(headerBytes, reconstructedHeaderBytes); + } + } + + @Test + public void serializeDeserializeDefaultMaxEncryptedDataKeys() { + Map encryptionContext = new HashMap(1); + encryptionContext.put("ENC", "CiphertextHeader Test"); + + for (CryptoAlgorithm alg : testAlgs) { + final CiphertextHeaders ciphertextHeaders = createCiphertextHeaders(encryptionContext, alg); + + final byte[] headerBytes = ciphertextHeaders.toByteArray(); + final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders(); + reconstructedHeaders.deserialize(headerBytes, 0); final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray(); + assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS); assertArrayEquals(headerBytes, reconstructedHeaderBytes); } } From 9470ce153e8087fe7ae99843fb9d005029087295 Mon Sep 17 00:00:00 2001 From: Benjamin Farley Date: Tue, 26 Oct 2021 16:44:40 -0600 Subject: [PATCH 2/2] Fix static analysis --- .../model/CiphertextHeaders.java | 32 +++++++++---------- .../model/CiphertextHeadersTest.java | 7 ++-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java b/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java index fee151ba2..7015e8cc4 100644 --- a/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java +++ b/src/main/java/com/amazonaws/encryptionsdk/model/CiphertextHeaders.java @@ -503,7 +503,6 @@ private int parseComplete(final byte[] b, final int off) throws ParseException { return 0; } - /** * Deserialize the provided bytes starting at the specified offset to construct an instance of * this class. Uses the default value for maxEncryptedDataKeys, which results in no limit. @@ -520,20 +519,20 @@ public int deserialize(final byte[] b, final int off) throws ParseException { return deserialize(b, off, NO_MAX_ENCRYPTED_DATA_KEYS); } - /** - * Deserialize the provided bytes starting at the specified offset to construct an instance of - * this class. - * - *

This method parses the provided bytes for the individual fields in this class. This method - * also supports partial parsing where not all the bytes required for parsing the fields - * successfully are available. - * - * @param b the byte array to deserialize. - * @param off the offset in the byte array to use for deserialization. - * @param maxEncryptedDataKeys the maximum number of EDKs to deserialize; zero indicates no - * maximum - * @return the number of bytes consumed in deserialization. - */ + /** + * Deserialize the provided bytes starting at the specified offset to construct an instance of + * this class. + * + *

This method parses the provided bytes for the individual fields in this class. This method + * also supports partial parsing where not all the bytes required for parsing the fields + * successfully are available. + * + * @param b the byte array to deserialize. + * @param off the offset in the byte array to use for deserialization. + * @param maxEncryptedDataKeys the maximum number of EDKs to deserialize; zero indicates no + * maximum + * @return the number of bytes consumed in deserialization. + */ public int deserialize(final byte[] b, final int off, int maxEncryptedDataKeys) throws ParseException { if (b == null) { @@ -853,8 +852,7 @@ public void setSuiteData(byte[] suiteData) { } /** - * Return max encrypted data keys - * Package scope for unit testing. + * Return max encrypted data keys. Package scope for unit testing. * * @return int */ diff --git a/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java b/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java index 13c20a42b..58ef933c4 100644 --- a/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java +++ b/src/test/java/com/amazonaws/encryptionsdk/model/CiphertextHeadersTest.java @@ -61,8 +61,7 @@ public void serializeDeserialize() { final byte[] headerBytes = ciphertextHeaders.toByteArray(); final CiphertextHeaders reconstructedHeaders = new CiphertextHeaders(); - reconstructedHeaders.deserialize( - headerBytes, 0, maxEncryptedDataKeys); + reconstructedHeaders.deserialize(headerBytes, 0, maxEncryptedDataKeys); final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray(); assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), maxEncryptedDataKeys); @@ -83,7 +82,9 @@ public void serializeDeserializeDefaultMaxEncryptedDataKeys() { reconstructedHeaders.deserialize(headerBytes, 0); final byte[] reconstructedHeaderBytes = reconstructedHeaders.toByteArray(); - assertEquals(reconstructedHeaders.getMaxEncryptedDataKeys(), CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS); + assertEquals( + reconstructedHeaders.getMaxEncryptedDataKeys(), + CiphertextHeaders.NO_MAX_ENCRYPTED_DATA_KEYS); assertArrayEquals(headerBytes, reconstructedHeaderBytes); } }