Skip to content

Commit ed30f4f

Browse files
committed
Remove dependency on Commons Codec, use Base64 codec in BouncyCastle, bump version to 1.6.0
1 parent 755c274 commit ed30f4f

File tree

12 files changed

+82
-45
lines changed

12 files changed

+82
-45
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 1.6.0 -- 2019-05-31
4+
5+
### Minor Changes
6+
* Remove dependency on Apache Commons Codec 1.12.
7+
* Use Base64 encoder from Bouncy Castle.
8+
* Introduce and use utility methods for Base64 encoding/decoding so that
9+
switching the codec provider needs to be done only in one place next time.
10+
311
## 1.5.0 -- 2019-05-30
412

513
### Minor Changes

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can get the latest release from Maven:
4545
<dependency>
4646
<groupId>com.amazonaws</groupId>
4747
<artifactId>aws-encryption-sdk-java</artifactId>
48-
<version>1.5.0</version>
48+
<version>1.6.0</version>
4949
</dependency>
5050
```
5151

pom.xml

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.amazonaws</groupId>
66
<artifactId>aws-encryption-sdk-java</artifactId>
7-
<version>1.5.0</version>
7+
<version>1.6.0</version>
88
<packaging>jar</packaging>
99

1010
<name>aws-encryption-sdk-java</name>
@@ -92,12 +92,6 @@
9292
<artifactId>commons-lang3</artifactId>
9393
<version>3.9</version>
9494
</dependency>
95-
96-
<dependency>
97-
<groupId>commons-codec</groupId>
98-
<artifactId>commons-codec</artifactId>
99-
<version>1.12</version>
100-
</dependency>
10195
</dependencies>
10296

10397
<!--Custom repository:-->

src/main/java/com/amazonaws/encryptionsdk/AwsCrypto.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22-
import org.apache.commons.codec.binary.Base64;
23-
2422
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
2523
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
2624
import com.amazonaws.encryptionsdk.internal.DecryptionHandler;
@@ -308,7 +306,7 @@ public <K extends MasterKey<K>> CryptoResult<String, K> encryptString(
308306
plaintext.getBytes(StandardCharsets.UTF_8),
309307
encryptionContext
310308
);
311-
return new CryptoResult<>(Base64.encodeBase64String(ctBytes.getResult()),
309+
return new CryptoResult<>(Utils.encodeBase64String(ctBytes.getResult()),
312310
ctBytes.getMasterKeys(), ctBytes.getHeaders());
313311
}
314312

@@ -424,7 +422,7 @@ public <K extends MasterKey<K>> CryptoResult<String, K> decryptString(
424422
Utils.assertNonNull(provider, "provider");
425423
final byte[] ciphertextBytes;
426424
try {
427-
ciphertextBytes = Base64.decodeBase64(Utils.assertNonNull(ciphertext, "ciphertext"));
425+
ciphertextBytes = Utils.decodeBase64String(Utils.assertNonNull(ciphertext, "ciphertext"));
428426
} catch (final IllegalArgumentException ex) {
429427
throw new BadCiphertextException("Invalid base 64", ex);
430428
}

src/main/java/com/amazonaws/encryptionsdk/internal/TrailingSignatureAlgorithm.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
1515
import org.bouncycastle.math.ec.ECPoint;
1616

17-
import org.apache.commons.codec.binary.Base64;
18-
1917
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
2018

2119
import static com.amazonaws.encryptionsdk.internal.BouncyCastleConfiguration.INTERNAL_BOUNCY_CASTLE_PROVIDER;
@@ -70,7 +68,7 @@ public String getRawSignatureAlgorithm() {
7068

7169
@Override
7270
public PublicKey deserializePublicKey(String keyString) {
73-
final ECPoint q = ecSpec.getCurve().decodePoint(Base64.decodeBase64(keyString));
71+
final ECPoint q = ecSpec.getCurve().decodePoint(Utils.decodeBase64String(keyString));
7472

7573
ECPublicKeyParameters keyParams = new ECPublicKeyParameters(
7674
q,
@@ -82,7 +80,7 @@ public PublicKey deserializePublicKey(String keyString) {
8280

8381
@Override
8482
public String serializePublicKey(PublicKey key) {
85-
return Base64.encodeBase64String(((ECPublicKey)key).getQ().getEncoded(true));
83+
return Utils.encodeBase64String(((ECPublicKey)key).getQ().getEncoded(true));
8684
}
8785

8886
@Override

src/main/java/com/amazonaws/encryptionsdk/internal/Utils.java

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.WeakHashMap;
2424
import java.util.concurrent.atomic.AtomicLong;
2525

26+
import org.bouncycastle.util.encoders.Base64;
27+
2628
/**
2729
* Internal utility methods.
2830
*/
@@ -257,4 +259,24 @@ public static ByteBuffer limit(final ByteBuffer buff, final int newLimit) {
257259
((Buffer) buff).limit(newLimit);
258260
return buff;
259261
}
262+
263+
/**
264+
* Takes a Base64-encoded String, decodes it, and returns contents as a byte array.
265+
*
266+
* @param encoded Base64 encoded String
267+
* @return decoded data as a byte array
268+
*/
269+
public static byte[] decodeBase64String(final String encoded) {
270+
return Base64.decode(encoded);
271+
}
272+
273+
/**
274+
* Takes data in a byte array, encodes them in Base64, and returns the result as a String.
275+
*
276+
* @param data The data to encode.
277+
* @return Base64 string that encodes the {@code data}.
278+
*/
279+
public static String encodeBase64String(final byte[] data) {
280+
return Base64.toBase64String(data);
281+
}
260282
}

src/test/java/com/amazonaws/encryptionsdk/AwsCryptoTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@
4141
import org.junit.Before;
4242
import org.junit.Test;
4343

44-
import org.apache.commons.codec.binary.Base64;
45-
4644
import com.amazonaws.encryptionsdk.caching.CachingCryptoMaterialsManager;
4745
import com.amazonaws.encryptionsdk.caching.LocalCryptoMaterialsCache;
4846
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
4947
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
5048
import com.amazonaws.encryptionsdk.internal.StaticMasterKey;
5149
import com.amazonaws.encryptionsdk.internal.TestIOUtils;
50+
import com.amazonaws.encryptionsdk.internal.Utils;
5251
import com.amazonaws.encryptionsdk.model.CiphertextType;
5352
import com.amazonaws.encryptionsdk.model.DecryptionMaterials;
5453
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsRequest;
@@ -450,7 +449,7 @@ public void encryptBytesDecryptString() {
450449
encryptionContext).getResult();
451450
final String decryptedText = encryptionClient_.decryptString(
452451
masterKeyProvider,
453-
Base64.encodeBase64String(cipherText)).getResult();
452+
Utils.encodeBase64String(cipherText)).getResult();
454453

455454
assertEquals(plaintext, decryptedText);
456455
}
@@ -470,7 +469,7 @@ public void encryptStringDecryptBytes() {
470469
encryptionContext).getResult();
471470
final byte[] decryptedText = encryptionClient_.decryptData(
472471
masterKeyProvider,
473-
Base64.decodeBase64(ciphertext)).getResult();
472+
Utils.decodeBase64String(ciphertext)).getResult();
474473

475474
assertArrayEquals(plaintextString.getBytes(StandardCharsets.UTF_8), decryptedText);
476475
}

src/test/java/com/amazonaws/encryptionsdk/UtilsTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.amazonaws.encryptionsdk;
22

3+
import static org.junit.Assert.assertArrayEquals;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertNotEquals;
56
import static org.junit.Assert.assertTrue;
67

8+
import java.nio.charset.StandardCharsets;
79
import java.util.Arrays;
810

911
import org.junit.Test;
@@ -74,5 +76,25 @@ public void testSaturatingAdd() {
7476
assertEquals(Long.MAX_VALUE, Utils.saturatingAdd(Long.MAX_VALUE, Long.MAX_VALUE));
7577
assertEquals(Long.MIN_VALUE, Utils.saturatingAdd(Long.MIN_VALUE, Long.MIN_VALUE));
7678
}
79+
80+
/**
81+
* Basic sanity check for our Base64 helper methods.
82+
*/
83+
@Test
84+
public void base64empty() {
85+
assertEquals("", Utils.encodeBase64String(new byte[]{}));
86+
assertArrayEquals(new byte[]{}, Utils.decodeBase64String(""));
87+
}
88+
89+
/**
90+
* Basic sanity check for our Base64 helper methods.
91+
*/
92+
@Test
93+
public void base64something() {
94+
byte[] data = "Lorem ipsum dolor sit amet".getBytes(StandardCharsets.UTF_8);
95+
String encoded = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ=";
96+
assertEquals(encoded, Utils.encodeBase64String(data));
97+
assertArrayEquals(data, Utils.decodeBase64String(encoded));
98+
}
7799
}
78100

src/test/java/com/amazonaws/encryptionsdk/XCompatDecryptTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,17 @@
3232
import javax.crypto.spec.SecretKeySpec;
3333

3434
import org.apache.commons.lang3.StringUtils;
35-
import org.apache.commons.codec.binary.Base64;
3635

3736
import org.bouncycastle.util.io.pem.PemReader;
3837

3938
import static org.junit.Assert.assertArrayEquals;
4039

41-
import org.junit.Assume;
4240
import org.junit.Test;
4341
import org.junit.runner.RunWith;
4442
import org.junit.runners.Parameterized;
4543
import org.junit.runners.Parameterized.Parameters;
4644

45+
import com.amazonaws.encryptionsdk.internal.Utils;
4746
import com.fasterxml.jackson.databind.ObjectMapper;
4847
import com.fasterxml.jackson.core.type.TypeReference;
4948

@@ -120,7 +119,7 @@ public static Collection<Object[]> data() throws Exception{
120119
byte[] keyBytes;
121120
switch ((String)thisKey.get("encoding")) {
122121
case "base64":
123-
keyBytes = Base64.decodeBase64(keyRaw);
122+
keyBytes = Utils.decodeBase64String(keyRaw);
124123
break;
125124
case "pem":
126125
PemReader pemReader = new PemReader(new StringReader(keyRaw));

src/test/java/com/amazonaws/encryptionsdk/caching/CacheIdentifierTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
import org.bouncycastle.util.encoders.Hex;
1919
import org.junit.Test;
20-
import org.apache.commons.codec.binary.Base64;
2120

2221
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
2322
import com.amazonaws.encryptionsdk.CryptoMaterialsManager;
23+
import com.amazonaws.encryptionsdk.internal.Utils;
2424
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsRequest;
2525
import com.amazonaws.encryptionsdk.model.EncryptionMaterialsRequest;
2626
import com.amazonaws.encryptionsdk.model.KeyBlob;
@@ -84,7 +84,7 @@ void assertDecryptId(String partitionName, CryptoAlgorithm algo, List<KeyBlob> b
8484

8585
byte[] id = getCacheIdentifier(getCMM(partitionName), request);
8686

87-
assertEquals(expect, Base64.encodeBase64String(id));
87+
assertEquals(expect, Utils.encodeBase64String(id));
8888
}
8989

9090
void assertEncryptId(String partitionName, CryptoAlgorithm algo, Map<String, String> context, String expect) throws Exception {
@@ -95,7 +95,7 @@ void assertEncryptId(String partitionName, CryptoAlgorithm algo, Map<String, Str
9595

9696
byte[] id = getCacheIdentifier(getCMM(partitionName), request);
9797

98-
assertEquals(expect, Base64.encodeBase64String(id));
98+
assertEquals(expect, Utils.encodeBase64String(id));
9999
}
100100

101101
@Test

src/test/java/com/amazonaws/encryptionsdk/internal/StaticMasterKey.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import javax.crypto.SecretKey;
2121
import javax.crypto.spec.SecretKeySpec;
2222

23-
import org.apache.commons.codec.binary.Base64;
24-
2523
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
2624
import com.amazonaws.encryptionsdk.DataKey;
2725
import com.amazonaws.encryptionsdk.EncryptedDataKey;
@@ -181,8 +179,8 @@ public DataKey<StaticMasterKey> decryptDataKey(CryptoAlgorithm algorithm,
181179
/**
182180
* Statically configured private key.
183181
*/
184-
private static final byte[] privateKey_v1 = Base64.decodeBase64(
185-
("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKLpwqjYtYExVilW/Hg0ogWv9xZ+"
182+
private static final byte[] privateKey_v1 = Utils.decodeBase64String(
183+
"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKLpwqjYtYExVilW/Hg0ogWv9xZ+"
186184
+ "THj4IzvISLlPtK8W6KXMcqukfuxdYmndPv8UD1DbdHFYSSistdqoBN32vVQOQnJZyYm45i2TDOV0"
187185
+ "M2DtHtR6aMMlBLGtdPeeaT88nQfI1ORjRDyR1byMwomvmKifZYga6FjLt/sgqfSE9BUnAgMBAAEC"
188186
+ "gYAqnewGL2qLuVRIzDCPYXVg938zqyZmHsNYyDP+BhPGGcASX0FAFW/+dQ9hkjcAk0bOaBo17Fp3"
@@ -193,15 +191,14 @@ public DataKey<StaticMasterKey> decryptDataKey(CryptoAlgorithm algorithm,
193191
+ "4mSYYs9UZ0S1DAMhl6amPpqIANYX98NJyZUsjtNV9MK2qoUSF/xXqDFvxG1lAkBhP5Ow2Zn3U1mT"
194192
+ "Y/XQxSZjjjwr3vyt1neHjQsEMwa3iGPXJbLSmVBVZfUZoGOBDsvVQoCIiFOlGuKyBpA45MkZAkAH"
195193
+ "ksUrS9xLrDIUOI2BzMNRsK0bH7KJ+PFxm2SBgJOF9+Uf2A9LIP4IvESZq+ufp6c8YaqgR6Id1vws"
196-
+ "7rUyGoa5").getBytes(StandardCharsets.UTF_8));
194+
+ "7rUyGoa5");
197195

198196
/**
199197
* Statically configured public key.
200198
*/
201-
private static final byte[] publicKey_v1 = Base64.decodeBase64(
202-
("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCi6cKo2LWBMVYpVvx4NKIFr/cWfkx4+CM7yEi5"
199+
private static final byte[] publicKey_v1 = Utils.decodeBase64String(
200+
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCi6cKo2LWBMVYpVvx4NKIFr/cWfkx4+CM7yEi5"
203201
+ "T7SvFuilzHKrpH7sXWJp3T7/FA9Q23RxWEkorLXaqATd9r1UDkJyWcmJuOYtkwzldDNg7R7UemjD"
204-
+ "JQSxrXT3nmk/PJ0HyNTkY0Q8kdW8jMKJr5ion2WIGuhYy7f7IKn0hPQVJwIDAQAB")
205-
.getBytes(StandardCharsets.UTF_8));
202+
+ "JQSxrXT3nmk/PJ0HyNTkY0Q8kdW8jMKJr5ion2WIGuhYy7f7IKn0hPQVJwIDAQAB");
206203

207204
}

src/test/java/com/amazonaws/encryptionsdk/model/ByteFormatCheckValues.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
package com.amazonaws.encryptionsdk.model;
1515

16-
import org.apache.commons.codec.binary.Base64;
16+
import com.amazonaws.encryptionsdk.internal.Utils;
1717

1818
public class ByteFormatCheckValues {
1919
private static final String base64MessageId_ = "NQ/NXvg4mMN5zm5JFZHUWw==";
@@ -29,38 +29,38 @@ public class ByteFormatCheckValues {
2929
private static final String base64FinalFrameHeaderHash_ = "/b2fVFOxvnaM5vXDMGyyFPNTWMjuU/c/48qeH3uTHj0=";
3030

3131
public static byte[] getMessageId() {
32-
return Base64.decodeBase64(base64MessageId_);
32+
return Utils.decodeBase64String(base64MessageId_);
3333
}
3434

3535
public static byte[] getEncryptedKey() {
36-
return Base64.decodeBase64(base64EncryptedKey_);
36+
return Utils.decodeBase64String(base64EncryptedKey_);
3737
}
3838

3939
public static byte[] getPlaintextKey() {
40-
return Base64.decodeBase64(base64PlaintextKey_);
40+
return Utils.decodeBase64String(base64PlaintextKey_);
4141
}
4242

4343
public static byte[] getCiphertextHeaderHash() {
44-
return Base64.decodeBase64(base64CiphertextHeaderHash_);
44+
return Utils.decodeBase64String(base64CiphertextHeaderHash_);
4545
}
4646

4747
public static byte[] getCipherBlockHeaderHash() {
48-
return Base64.decodeBase64(base64BlockHeaderHash_);
48+
return Utils.decodeBase64String(base64BlockHeaderHash_);
4949
}
5050

5151
public static byte[] getCipherFrameHeaderHash() {
52-
return Base64.decodeBase64(base64FrameHeaderHash_);
52+
return Utils.decodeBase64String(base64FrameHeaderHash_);
5353
}
5454

5555
public static byte[] getCipherFinalFrameHeaderHash() {
56-
return Base64.decodeBase64(base64FinalFrameHeaderHash_);
56+
return Utils.decodeBase64String(base64FinalFrameHeaderHash_);
5757
}
5858

5959
public static byte[] getNonce() {
60-
return Base64.decodeBase64(base64Nonce_);
60+
return Utils.decodeBase64String(base64Nonce_);
6161
}
6262

6363
public static byte[] getTag() {
64-
return Base64.decodeBase64(base64Tag_);
64+
return Utils.decodeBase64String(base64Tag_);
6565
}
6666
}

0 commit comments

Comments
 (0)