Skip to content

Commit 2f98a3a

Browse files
fix(core): Add support for certs (#131)
We support certs in the go sdk, we should have the same support here
1 parent a9c5098 commit 2f98a3a

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

sdk/src/main/java/io/opentdf/platform/sdk/AsymEncryption.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import javax.crypto.Cipher;
55
import javax.crypto.IllegalBlockSizeException;
66
import javax.crypto.NoSuchPaddingException;
7+
8+
import java.io.ByteArrayInputStream;
79
import java.security.*;
10+
import java.security.cert.CertificateException;
11+
import java.security.cert.CertificateFactory;
12+
import java.security.cert.X509Certificate;
813
import java.security.spec.InvalidKeySpecException;
914
import java.security.spec.X509EncodedKeySpec;
1015
import java.util.Base64;
@@ -14,6 +19,8 @@ public class AsymEncryption {
1419
private final PublicKey publicKey;
1520
private static final String PUBLIC_KEY_HEADER = "-----BEGIN PUBLIC KEY-----";
1621
private static final String PUBLIC_KEY_FOOTER = "-----END PUBLIC KEY-----";
22+
private static final String PEM_HEADER = "-----BEGIN (.*)-----";
23+
private static final String PEM_FOOTER = "-----END (.*)-----";
1724
private static final String CIPHER_TRANSFORM = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
1825

1926
/**
@@ -22,24 +29,51 @@ public class AsymEncryption {
2229
* @param publicKeyInPem a Public Key in PEM format
2330
*/
2431
public AsymEncryption(String publicKeyInPem) {
25-
publicKeyInPem = publicKeyInPem
26-
.replace(PUBLIC_KEY_HEADER, "")
27-
.replace(PUBLIC_KEY_FOOTER, "")
28-
.replaceAll("\\s", "");
2932

30-
byte[] decoded = Base64.getDecoder().decode(publicKeyInPem);
31-
X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
32-
KeyFactory kf;
33-
try {
34-
kf = KeyFactory.getInstance("RSA");
35-
} catch (NoSuchAlgorithmException e) {
36-
throw new SDKException("RSA is not a valid algorithm!!!???!!!", e);
33+
PublicKey pubKey = null;
34+
35+
String base64EncodedPem= publicKeyInPem
36+
.replaceAll(PEM_HEADER, "")
37+
.replaceAll(PEM_FOOTER, "")
38+
.replaceAll("\\s", "")
39+
.replaceAll("\r\n", "")
40+
.replaceAll("\n", "")
41+
.trim();
42+
43+
44+
byte[] decoded = Base64.getDecoder().decode(base64EncodedPem);
45+
46+
// Check if the PEM contains a certificate
47+
if (publicKeyInPem.contains("BEGIN CERTIFICATE")) {
48+
try {
49+
// Parse the certificate
50+
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
51+
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(decoded));
52+
pubKey = cert.getPublicKey();
53+
} catch (CertificateException e) {
54+
throw new SDKException("x509.ParseCertificate failed: " + e.getMessage(), e);
55+
}
56+
} else {
57+
// Otherwise, treat it as a PKIX public key
58+
X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
59+
KeyFactory keyFactory;
60+
try {
61+
keyFactory = KeyFactory.getInstance("RSA");
62+
} catch (NoSuchAlgorithmException e) {
63+
throw new SDKException("RSA is not a valid algorithm!!!???!!!", e);
64+
}
65+
try {
66+
pubKey = keyFactory.generatePublic(spec);
67+
} catch (InvalidKeySpecException e) {
68+
throw new SDKException("error creating asymmetric encryption", e);
69+
}
3770
}
3871

39-
try {
40-
this.publicKey = kf.generatePublic(spec);
41-
} catch (InvalidKeySpecException e) {
42-
throw new SDKException("error creating asymmetric encryption", e);
72+
// Check if the public key is RSA
73+
if (pubKey instanceof java.security.interfaces.RSAPublicKey) {
74+
this.publicKey = pubKey;
75+
} else {
76+
throw new SDKException("Not an RSA PEM formatted public key");
4377
}
4478
}
4579

sdk/src/test/java/io/opentdf/platform/sdk/AsymEncryptionTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ void encryptionWithValidPublicKey() throws Exception {
3030
assertNotNull(cipherText);
3131
}
3232

33+
void encryptionWithValidCert() throws Exception {
34+
String certInPem = "-----BEGIN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YBs5mDEpKwGC0DxW4tg0FIsb3bbAvqy8ETklExkOh0VfJP4a\n" +
50+
"CMz9WjmCfS15t0mPzofK8ir20kF0u0sWvviVVlun+8KYdFOG/wzS100cPNn/wqug\n" +
51+
"4w==\n" +
52+
"-----END CERTIFICATE-----";;
53+
AsymEncryption asymEncryption = new AsymEncryption(certInPem);
54+
byte[] plaintext = "Virtru, JavaSDK!".getBytes();
55+
56+
byte[] cipherText = asymEncryption.encrypt(plaintext);
57+
58+
assertNotNull(cipherText);
59+
}
60+
3361
@Test
3462
void encryptionWithInvalidPublicKey() {
3563
String publicKeyInPem = "InvalidPublicKey";

0 commit comments

Comments
 (0)