Skip to content

Commit 8fb481a

Browse files
Merge pull request #60 from fangpenlin/add-encryption-key-config-option
Fix #59 add encryption key config option
2 parents 736b88b + c7fcb22 commit 8fb481a

File tree

6 files changed

+91
-5
lines changed

6 files changed

+91
-5
lines changed

src/main/java/com/mastercard/developer/encryption/EncryptionConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mastercard.developer.encryption;
22

33
import java.security.PrivateKey;
4+
import java.security.PublicKey;
45
import java.security.cert.Certificate;
56
import java.util.Collections;
67
import java.util.Map;
@@ -35,6 +36,11 @@ public enum Scheme {
3536
*/
3637
Certificate encryptionCertificate;
3738

39+
/**
40+
* A public key will be used for request encryption.
41+
*/
42+
PublicKey encryptionKey;
43+
3844
/**
3945
* A private key object to be used for decryption.
4046
*/
@@ -77,6 +83,13 @@ public Certificate getEncryptionCertificate() {
7783
return encryptionCertificate;
7884
}
7985

86+
public PublicKey getEncryptionKey() {
87+
if (encryptionKey != null) {
88+
return encryptionKey;
89+
}
90+
return encryptionCertificate.getPublicKey();
91+
}
92+
8093
public PrivateKey getDecryptionKey() {
8194
return decryptionKey;
8295
}

src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.security.MessageDigest;
66
import java.security.NoSuchAlgorithmException;
77
import java.security.PrivateKey;
8+
import java.security.PublicKey;
89
import java.security.cert.Certificate;
910
import java.util.HashMap;
1011
import java.util.Map;
@@ -15,6 +16,7 @@
1516
abstract class EncryptionConfigBuilder {
1617

1718
protected Certificate encryptionCertificate;
19+
protected PublicKey encryptionKey;
1820
protected String encryptionKeyFingerprint;
1921
protected PrivateKey decryptionKey;
2022
protected Map<String, String> encryptionPaths = new HashMap<>();
@@ -23,11 +25,20 @@ abstract class EncryptionConfigBuilder {
2325

2426
void computeEncryptionKeyFingerprintWhenNeeded() throws EncryptionException {
2527
try {
26-
if (encryptionCertificate == null || !isNullOrEmpty(encryptionKeyFingerprint)) {
27-
// No encryption certificate set or key fingerprint already provided
28+
if ((encryptionCertificate == null && encryptionKey == null) || !isNullOrEmpty(encryptionKeyFingerprint)) {
29+
// No encryption certificate / encryption key set or key fingerprint already provided
2830
return;
2931
}
30-
byte[] keyFingerprintBytes = sha256digestBytes(encryptionCertificate.getPublicKey().getEncoded());
32+
if (encryptionKey != null && encryptionCertificate != null) {
33+
throw new IllegalArgumentException("You can only supply either an encryption key or an encryption certificate");
34+
}
35+
final PublicKey publicKey;
36+
if (encryptionKey != null) {
37+
publicKey = encryptionKey;
38+
} else {
39+
publicKey = encryptionCertificate.getPublicKey();
40+
}
41+
final byte[] keyFingerprintBytes = sha256digestBytes(publicKey.getEncoded());
3142
encryptionKeyFingerprint = encodeBytes(keyFingerprintBytes, FieldLevelEncryptionConfig.FieldValueEncoding.HEX);
3243
} catch (Exception e) {
3344
throw new EncryptionException("Failed to compute encryption key fingerprint!", e);

src/main/java/com/mastercard/developer/encryption/JweConfigBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mastercard.developer.encryption;
22

33
import java.security.PrivateKey;
4+
import java.security.PublicKey;
45
import java.security.cert.Certificate;
56
import java.util.Collections;
67

@@ -25,6 +26,7 @@ public JweConfig build() throws EncryptionException {
2526

2627
JweConfig config = new JweConfig();
2728
config.encryptionCertificate = this.encryptionCertificate;
29+
config.encryptionKey = this.encryptionKey;
2830
config.encryptionKeyFingerprint = this.encryptionKeyFingerprint;
2931
config.decryptionKey = this.decryptionKey;
3032
config.encryptionPaths = this.encryptionPaths.isEmpty() ? Collections.singletonMap("$", "$") : this.encryptionPaths;
@@ -38,10 +40,24 @@ public JweConfig build() throws EncryptionException {
3840
* See: {@link EncryptionConfig#encryptionCertificate}.
3941
*/
4042
public JweConfigBuilder withEncryptionCertificate(Certificate encryptionCertificate) {
43+
if (this.encryptionKey != null) {
44+
throw new IllegalArgumentException("You have already supplied an encryption key");
45+
}
4146
this.encryptionCertificate = encryptionCertificate;
4247
return this;
4348
}
4449

50+
/**
51+
* See: {@link EncryptionConfig#encryptionKey}.
52+
*/
53+
public JweConfigBuilder withEncryptionKey(PublicKey encryptionKey) {
54+
if (this.encryptionCertificate != null) {
55+
throw new IllegalArgumentException("You have already supplied an encryption certificate");
56+
}
57+
this.encryptionKey = encryptionKey;
58+
return this;
59+
}
60+
4561
/**
4662
* See: {@link EncryptionConfig#decryptionKey}.
4763
*/

src/main/java/com/mastercard/developer/encryption/jwe/JweObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String decrypt(JweConfig config) throws EncryptionException, GeneralSecur
5757

5858
public static String encrypt(JweConfig config, String payload, JweHeader header) throws EncryptionException, GeneralSecurityException {
5959
SecretKeySpec cek = AESEncryption.generateCek(256);
60-
byte[] encryptedSecretKeyBytes = RSA.wrapSecretKey(config.getEncryptionCertificate().getPublicKey(), cek, "SHA-256");
60+
byte[] encryptedSecretKeyBytes = RSA.wrapSecretKey(config.getEncryptionKey(), cek, "SHA-256");
6161
String encryptedKey = EncodingUtils.base64UrlEncode(encryptedSecretKeyBytes);
6262

6363
byte[] iv = AESEncryption.generateIv().getIV();

src/test/java/com/mastercard/developer/encryption/JweConfigBuilderTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ public void testBuild_Nominal() throws Exception {
3131
Assert.assertEquals(Collections.singletonMap("$", "$"), config.getEncryptionPaths());
3232
}
3333

34+
@Test
35+
public void testBuild_EncryptionKeyFromCertificate() throws Exception {
36+
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
37+
.withEncryptionCertificate(TestUtils.getTestEncryptionCertificate())
38+
.withDecryptionKey(TestUtils.getTestDecryptionKey())
39+
.build();
40+
Assert.assertEquals(TestUtils.getTestEncryptionCertificate().getPublicKey(), config.getEncryptionKey());
41+
}
42+
43+
@Test
44+
public void testBuild_EncryptionKeyFromEncryptionKey() throws Exception {
45+
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
46+
.withEncryptionKey(TestUtils.getTestEncryptionCertificate().getPublicKey())
47+
.withDecryptionKey(TestUtils.getTestDecryptionKey())
48+
.build();
49+
Assert.assertEquals(TestUtils.getTestEncryptionCertificate().getPublicKey(), config.getEncryptionKey());
50+
}
51+
3452
@Test
3553
public void testBuild_ResultShouldBeAssignableToGenericEncryptionConfig() throws Exception {
3654
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
@@ -129,6 +147,24 @@ public void testBuild_ShouldThrowIllegalArgumentException_WhenMultipleWildcardsO
129147
.build();
130148
}
131149

150+
@Test
151+
public void testBuild_ShouldComputeCertificateKeyFingerprin() throws Exception {
152+
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
153+
.withEncryptionCertificate(TestUtils.getTestEncryptionCertificate())
154+
.withDecryptionKey(TestUtils.getTestDecryptionKey())
155+
.build();
156+
Assert.assertEquals("761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79", config.getEncryptionKeyFingerprint());
157+
}
158+
159+
@Test
160+
public void testBuild_ShouldComputeEncryptionKeyFingerprin() throws Exception {
161+
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
162+
.withEncryptionKey(TestUtils.getTestEncryptionCertificate().getPublicKey())
163+
.withDecryptionKey(TestUtils.getTestDecryptionKey())
164+
.build();
165+
Assert.assertEquals("761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79", config.getEncryptionKeyFingerprint());
166+
}
167+
132168
@Test
133169
public void testBuild_ShouldNotComputeCertificateKeyFingerprint_WhenFingerprintSet() throws Exception {
134170
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
@@ -138,4 +174,14 @@ public void testBuild_ShouldNotComputeCertificateKeyFingerprint_WhenFingerprintS
138174
.build();
139175
Assert.assertEquals("2f4lvi26vJWzkzAIaiR2G0YsJAQ=", config.getEncryptionKeyFingerprint());
140176
}
177+
178+
@Test
179+
public void testBuild_ShouldNotComputeEncryptionKeyFingerprint_WhenFingerprintSet() throws Exception {
180+
EncryptionConfig config = JweConfigBuilder.aJweEncryptionConfig()
181+
.withEncryptionKey(TestUtils.getTestEncryptionCertificate().getPublicKey())
182+
.withDecryptionKey(TestUtils.getTestDecryptionKey())
183+
.withEncryptionKeyFingerprint("2f4lvi26vJWzkzAIaiR2G0YsJAQ=")
184+
.build();
185+
Assert.assertEquals("2f4lvi26vJWzkzAIaiR2G0YsJAQ=", config.getEncryptionKeyFingerprint());
186+
}
141187
}

src/test/java/com/mastercard/developer/encryption/rsa/RSATest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void testWrapUnwrapSecretKey_ShouldReturnTheOriginalKey() throws Exceptio
2424
SecretKey originalKey = new SecretKeySpec(originalKeyBytes, 0, originalKeyBytes.length, SYMMETRIC_KEY_TYPE);
2525

2626
// WHEN
27-
byte[] wrappedKeyBytes = RSA.wrapSecretKey(config.getEncryptionCertificate().getPublicKey(), originalKey, "SHA-256");
27+
byte[] wrappedKeyBytes = RSA.wrapSecretKey(config.getEncryptionKey(), originalKey, "SHA-256");
2828
Key unwrappedKey = RSA.unwrapSecretKey(config.getDecryptionKey(), wrappedKeyBytes, "SHA-256");
2929

3030
// THEN

0 commit comments

Comments
 (0)