1515
1616import static org .junit .Assert .assertArrayEquals ;
1717import static org .junit .Assert .assertEquals ;
18- import static org .junit .Assert .assertFalse ;
1918
20- import java .nio .ByteBuffer ;
2119import java .nio .charset .StandardCharsets ;
20+ import java .util .Arrays ;
2221import java .util .HashMap ;
2322import java .util .Map ;
2423
2827import com .amazonaws .encryptionsdk .CryptoAlgorithm ;
2928import com .amazonaws .encryptionsdk .DataKey ;
3029import com .amazonaws .encryptionsdk .exception .AwsCryptoException ;
30+ import com .amazonaws .encryptionsdk .internal .Constants ;
3131import com .amazonaws .encryptionsdk .internal .RandomBytesGenerator ;
3232import com .amazonaws .encryptionsdk .internal .StaticMasterKey ;
3333
@@ -78,7 +78,7 @@ public void overlyLargeKeyProviderIdLen() {
7878
7979 final DataKey <StaticMasterKey > mockDataKey = masterKeyProvider_ .generateDataKey (ALGORITHM , encryptionContext );
8080
81- final int providerId_Len = Short . MAX_VALUE + 1 ;
81+ final int providerId_Len = Constants . UNSIGNED_SHORT_MAX_VAL + 1 ;
8282 final byte [] providerId_Bytes = RandomBytesGenerator .generate (providerId_Len );
8383 final String providerId_ = new String (providerId_Bytes , StandardCharsets .UTF_8 );
8484
@@ -95,15 +95,15 @@ public void overlyLargeKeyProviderInfoLen() {
9595
9696 final DataKey <StaticMasterKey > mockDataKey = masterKeyProvider_ .generateDataKey (ALGORITHM , encryptionContext );
9797
98- final int providerInfo_Len = Short . MAX_VALUE + 1 ;
98+ final int providerInfo_Len = Constants . UNSIGNED_SHORT_MAX_VAL + 1 ;
9999 final byte [] providerInfo_ = RandomBytesGenerator .generate (providerInfo_Len );
100100
101101 new KeyBlob (providerId_ , providerInfo_ , mockDataKey .getEncryptedDataKey ());
102102 }
103103
104104 @ Test (expected = AwsCryptoException .class )
105105 public void overlyLargeKey () {
106- final int keyLen = Short . MAX_VALUE + 1 ;
106+ final int keyLen = Constants . UNSIGNED_SHORT_MAX_VAL + 1 ;
107107 final byte [] encryptedKeyBytes = RandomBytesGenerator .generate (keyLen );
108108
109109 new KeyBlob (providerId_ , providerInfo_ .getBytes (StandardCharsets .UTF_8 ), encryptedKeyBytes );
@@ -173,75 +173,51 @@ public void checkKeyLen() {
173173 assertEquals (mockDataKey_ .getEncryptedDataKey ().length , reconstructedKeyBlob .getEncryptedDataKeyLen ());
174174 }
175175
176- private byte [] negativeKeyProviderIdLenTestVector () {
177- // key provider id len of -1, key provider info len of 2, and key len of 3
178- return new byte []{
179- (byte )0xff , (byte )0xff , (byte )0x01 , (byte )0x00 , (byte )0x02 , (byte )0x02 , (byte )0x03 ,
180- (byte )0x00 , (byte )0x03 , (byte )0x04 , (byte )0x05 , (byte )0x06
181- };
182- }
183-
184- private byte [] negativeKeyProviderInfoLenTestVector () {
185- // key provider id len of 1, key provider info len of -2, key len of 3
186- return new byte [] {
187- (byte )0x00 , (byte )0x01 , (byte )0x01 , (byte )0xff , (byte )0xfe , (byte )0x02 , (byte )0x03 ,
188- (byte )0x00 , (byte )0x03 , (byte )0x04 , (byte )0x05 , (byte )0x06
189- };
190- }
176+ private KeyBlob generateRandomKeyBlob (int idLen , int infoLen , int keyLen ) {
177+ final byte [] idBytes = RandomBytesGenerator .generate (idLen );
178+ // negative bytes translate into U+FFFD, so no thanks
179+ for (int i = 0 ; i < idBytes .length ; i ++) {
180+ if (idBytes [i ] < 0 ) {
181+ idBytes [i ] = (byte ) (idBytes [i ] - Byte .MIN_VALUE );
182+ }
183+ }
184+ final byte [] infoBytes = RandomBytesGenerator .generate (infoLen );
185+ final byte [] keyBytes = RandomBytesGenerator .generate (keyLen );
191186
192- private byte [] negativeKeyLenTestVector () {
193- // key provider id len of 1, key provider info len of 2, key len of -3
194- return new byte [] {
195- (byte )0x00 , (byte )0x01 , (byte )0x01 , (byte )0x00 , (byte )0x00 , (byte )0x02 , (byte )0x03 ,
196- (byte )0xff , (byte )0xfd , (byte )0x04 , (byte )0x05 , (byte )0x06
197- };
187+ return new KeyBlob (new String (idBytes , StandardCharsets .UTF_8 ), infoBytes , keyBytes );
198188 }
199189
200- private void assertIncomplete (final byte [] vector ) {
201- assertFalse (deserialize (vector ).isComplete ());
190+ private void assertKeyBlobsEqual (KeyBlob b1 , KeyBlob b2 ) {
191+ assertArrayEquals (b1 .getProviderId ().getBytes (StandardCharsets .UTF_8 ),
192+ b2 .getProviderId ().getBytes (StandardCharsets .UTF_8 ));
193+ assertArrayEquals (b1 .getProviderInformation (), b2 .getProviderInformation ());
194+ assertArrayEquals (b1 .getEncryptedDataKey (), b2 .getEncryptedDataKey ());
202195 }
203196
204197 @ Test
205- public void checkNegativeKeyProviderIdLen () {
206- final byte [] keyBlobBytes = createKeyBlobBytes ();
207-
208- // manually set the keyProviderIdLen to negative
209- final byte [] negativeKeyProviderIdLen = ByteBuffer .allocate (Short .BYTES )
210- .putShort ((short ) -1 ).array ();
211- System .arraycopy (negativeKeyProviderIdLen , 0 , keyBlobBytes , 0 , Short .BYTES );
198+ public void checkKeyProviderIdLenUnsigned () {
199+ // provider id length is too large for a signed short but fits in unsigned
200+ final KeyBlob blob = generateRandomKeyBlob (Short .MAX_VALUE + 1 , Short .MAX_VALUE , Short .MAX_VALUE );
201+ final byte [] arr = blob .toByteArray ();
212202
213- // a negative field length throws a parse exception, so deserialization is incomplete
214- assertIncomplete (keyBlobBytes );
215- assertIncomplete (negativeKeyProviderIdLenTestVector ());
203+ assertKeyBlobsEqual (deserialize (arr ), blob );
216204 }
217205
218206 @ Test
219- public void checkNegativeKeyProviderInfoLen () {
220- final byte [] keyBlobBytes = createKeyBlobBytes ();
221-
222- // manually set the keyProviderInfoLen to negative
223- final byte [] negativeKeyProviderInfoLen = ByteBuffer .allocate (Short .BYTES )
224- .putShort ((short ) -1 ).array ();
225- int offset = Short .BYTES + providerId_ .length ();
226- System .arraycopy (negativeKeyProviderInfoLen , 0 , keyBlobBytes , offset , Short .BYTES );
207+ public void checkKeyProviderInfoLenUnsigned () {
208+ // provider info length is too large for a signed short but fits in unsigned
209+ final KeyBlob blob = generateRandomKeyBlob (Short .MAX_VALUE , Short .MAX_VALUE + 2 , Short .MAX_VALUE );
210+ final byte [] arr = blob .toByteArray ();
227211
228- // a negative field length throws a parse exception, so deserialization is incomplete
229- assertIncomplete (keyBlobBytes );
230- assertIncomplete (negativeKeyProviderInfoLenTestVector ());
212+ assertKeyBlobsEqual (deserialize (arr ), blob );
231213 }
232214
233215 @ Test
234216 public void checkNegativeKeyLen () {
235- final byte [] keyBlobBytes = createKeyBlobBytes ();
236-
237- // we will manually set the keyLen to negative
238- final byte [] negativeKeyLen = ByteBuffer .allocate (Short .BYTES )
239- .putShort ((short ) -1 ).array ();
240- int offset = Short .BYTES + providerId_ .length () + Short .BYTES + providerInfo_ .length ();
241- System .arraycopy (negativeKeyLen , 0 , keyBlobBytes , offset , Short .BYTES );
217+ // key length is too large for a signed short but fits in unsigned
218+ final KeyBlob blob = generateRandomKeyBlob (Short .MAX_VALUE , Short .MAX_VALUE , Short .MAX_VALUE + 3 );
219+ final byte [] arr = blob .toByteArray ();
242220
243- // negative key len throws parse exception so deserialization is incomplete
244- assertIncomplete (keyBlobBytes );
245- assertIncomplete (negativeKeyLenTestVector ());
221+ assertKeyBlobsEqual (deserialize (arr ), blob );
246222 }
247223}
0 commit comments