Skip to content

Commit 94c672b

Browse files
fix(nano): Store key ids if found (#134)
Co-authored-by: Tyler Biscoe <biscoe@virtru.com>
1 parent 1df897b commit 94c672b

File tree

6 files changed

+54
-13
lines changed

6 files changed

+54
-13
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,42 @@ public enum IntegrityAlgorithm {
2929

3030
public static final int K_HTTP_OK = 200;
3131

32-
public static class KASInfo {
32+
public static class KASInfo implements Cloneable {
3333
public String URL;
3434
public String PublicKey;
3535
public String KID;
3636
public Boolean Default;
3737
public String Algorithm;
38+
39+
@Override
40+
public KASInfo clone() {
41+
try {
42+
return (KASInfo) super.clone();
43+
} catch (CloneNotSupportedException e) {
44+
throw new RuntimeException(e);
45+
}
46+
}
47+
48+
@Override
49+
public String toString() {
50+
var sb = new StringBuilder("KASInfo{");
51+
if (this.URL != null) {
52+
sb.append("URL:\"").append(this.URL).append("\",");
53+
}
54+
if (this.PublicKey != null) {
55+
sb.append("PublicKey:\"").append(this.PublicKey).append("\",");
56+
}
57+
if (this.KID != null) {
58+
sb.append("KID:\"").append(this.KID).append("\",");
59+
}
60+
if (this.Default != null) {
61+
sb.append("Default:").append(this.Default).append(",");
62+
}
63+
if (this.Algorithm != null) {
64+
sb.append("Algorithm:\"").append(this.Algorithm).append("\",");
65+
}
66+
return sb.append("}").toString();
67+
}
3868
}
3969

4070

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.opentdf.platform.kas.PublicKeyRequest;
1414
import io.opentdf.platform.kas.PublicKeyResponse;
1515
import io.opentdf.platform.kas.RewrapRequest;
16+
import io.opentdf.platform.sdk.Config.KASInfo;
1617
import io.opentdf.platform.sdk.nanotdf.ECKeyPair;
1718
import io.opentdf.platform.sdk.nanotdf.NanoTDFType;
1819

@@ -58,10 +59,13 @@ public KASClient(Function <String, ManagedChannel> channelFactory, RSAKey dpopKe
5859
}
5960

6061
@Override
61-
public String getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve) {
62-
return getStub(kasInfo.URL)
63-
.publicKey(PublicKeyRequest.newBuilder().setAlgorithm(String.format("ec:%s", curve.toString())).build())
64-
.getPublicKey();
62+
public KASInfo getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve) {
63+
var r = getStub(kasInfo.URL)
64+
.publicKey(PublicKeyRequest.newBuilder().setAlgorithm(String.format("ec:%s", curve.toString())).build());
65+
var k2 = kasInfo.clone();
66+
k2.KID = r.getKid();
67+
k2.PublicKey = r.getPublicKey();
68+
return k2;
6569
}
6670

6771
@Override

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ public int createNanoTDF(ByteBuffer data, OutputStream outputStream,
6363

6464
Config.KASInfo kasInfo = nanoTDFConfig.kasInfoList.get(0);
6565
String url = kasInfo.URL;
66-
String kasPublicKeyAsPem = kasInfo.PublicKey;
67-
if (kasPublicKeyAsPem == null || kasPublicKeyAsPem.isEmpty()) {
66+
if (kasInfo.PublicKey == null || kasInfo.PublicKey.isEmpty()) {
6867
logger.info("no public key provided for KAS at {}, retrieving", url);
69-
kasPublicKeyAsPem = kas.getECPublicKey(kasInfo, nanoTDFConfig.eccMode.getEllipticCurveType());
68+
kasInfo = kas.getECPublicKey(kasInfo, nanoTDFConfig.eccMode.getEllipticCurveType());
7069
}
7170

7271
// Kas url resource locator
@@ -76,7 +75,7 @@ public int createNanoTDF(ByteBuffer data, OutputStream outputStream,
7675
ECKeyPair keyPair = new ECKeyPair(nanoTDFConfig.eccMode.getCurveName(), ECKeyPair.ECAlgorithm.ECDSA);
7776

7877
// Generate symmetric key
79-
ECPublicKey kasPublicKey = ECKeyPair.publicKeyFromPem(kasPublicKeyAsPem);
78+
ECPublicKey kasPublicKey = ECKeyPair.publicKeyFromPem(kasInfo.PublicKey);
8079
byte[] symmetricKey = ECKeyPair.computeECDHKey(kasPublicKey, keyPair.getPrivateKey());
8180

8281
// Generate HKDF key

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void close() throws Exception {
3939

4040
public interface KAS extends AutoCloseable {
4141
Config.KASInfo getPublicKey(Config.KASInfo kasInfo);
42-
String getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve);
42+
Config.KASInfo getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve);
4343
byte[] unwrap(Manifest.KeyAccess keyAccess, String policy);
4444
byte[] unwrapNanoTDF(NanoTDFType.ECCurve curve, String header, String kasURL);
4545
KASKeyCache getKeyCache();

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.opentdf.platform.sdk;
22

3+
import io.opentdf.platform.sdk.Config.KASInfo;
34
import io.opentdf.platform.sdk.nanotdf.ECKeyPair;
45
import io.opentdf.platform.sdk.nanotdf.Header;
56
import io.opentdf.platform.sdk.nanotdf.NanoTDFType;
@@ -49,8 +50,14 @@ public Config.KASInfo getPublicKey(Config.KASInfo kasInfo) {
4950
}
5051

5152
@Override
52-
public String getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve) {
53-
return kasPublicKey;
53+
public KASInfo getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve) {
54+
if (kasInfo.Algorithm != null && !"ec:secp256r1".equals(kasInfo.Algorithm)) {
55+
throw new IllegalArgumentException("Unexpected algorithm: " + kasInfo);
56+
}
57+
var k2 = kasInfo.clone();
58+
k2.KID = KID;
59+
k2.PublicKey = kasPublicKey;
60+
return k2;
5461
}
5562

5663
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.opentdf.platform.policy.attributes.GetAttributeValuesByFqnsResponse;
1313
import io.opentdf.platform.policy.attributes.AttributesServiceGrpc;
1414
import io.opentdf.platform.policy.attributes.AttributesServiceGrpc.AttributesServiceFutureStub;
15+
import io.opentdf.platform.sdk.Config.KASInfo;
1516
import io.opentdf.platform.sdk.nanotdf.NanoTDFType;
1617
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
1718
import org.junit.jupiter.api.BeforeAll;
@@ -73,7 +74,7 @@ public byte[] unwrap(Manifest.KeyAccess keyAccess, String policy) {
7374
}
7475

7576
@Override
76-
public String getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve) {
77+
public KASInfo getECPublicKey(Config.KASInfo kasInfo, NanoTDFType.ECCurve curve) {
7778
return null;
7879
}
7980

0 commit comments

Comments
 (0)