Skip to content

Commit bc3d212

Browse files
committed
cleanup
1 parent 78442f2 commit bc3d212

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

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

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static class NanoTDFRewrapRequestBody {
152152
@Override
153153
public byte[] unwrap(Manifest.KeyAccess keyAccess, String policy, KeyType sessionKeyType) {
154154
ECKeyPair ecKeyPair = null;
155-
155+
156156
if (sessionKeyType.isEc()) {
157157
var curveName = sessionKeyType.getCurveName();
158158
ecKeyPair = new ECKeyPair(curveName, ECKeyPair.ECAlgorithm.ECDH);
@@ -191,40 +191,37 @@ public byte[] unwrap(Manifest.KeyAccess keyAccess, String policy, KeyType sessi
191191
.setSignedRequestToken(jwt.serialize())
192192
.build();
193193
RewrapResponse response;
194+
var req = getStub(keyAccess.url).rewrapBlocking(request, Collections.emptyMap()).execute();
194195
try {
195-
var req = getStub(keyAccess.url).rewrapBlocking(request, Collections.emptyMap()).execute();
196-
try {
197-
response = getOrThrow(req);
198-
} catch (Exception e) {
199-
throw new SDKException("error unwrapping key", e);
200-
}
201-
var wrappedKey = response.getEntityWrappedKey().toByteArray();
202-
if (sessionKeyType != KeyType.RSA2048Key) {
203-
204-
if (ecKeyPair == null) {
205-
throw new SDKException("ECKeyPair is null. Unable to proceed with the unwrap operation.");
206-
}
207-
208-
var kasEphemeralPublicKey = response.getSessionPublicKey();
209-
var publicKey = ECKeyPair.publicKeyFromPem(kasEphemeralPublicKey);
210-
byte[] symKey = ECKeyPair.computeECDHKey(publicKey, ecKeyPair.getPrivateKey());
211-
212-
var sessionKey = ECKeyPair.calculateHKDF(GLOBAL_KEY_SALT, symKey);
213-
214-
AesGcm gcm = new AesGcm(sessionKey);
215-
AesGcm.Encrypted encrypted = new AesGcm.Encrypted(wrappedKey);
216-
return gcm.decrypt(encrypted);
217-
} else {
218-
return decryptor.decrypt(wrappedKey);
219-
}
196+
response = getOrThrow(req);
220197
} catch (StatusRuntimeException e) {
221198
if (e.getStatus().getCode() == Status.Code.INVALID_ARGUMENT) {
222199
// 400 Bad Request
223-
throw new KasBadRequestException("rewrap request 400: " + e.toString());
200+
throw new KasBadRequestException("rewrap request 400: " + e);
224201
}
225202
throw e;
203+
} catch (Exception e) {
204+
throw new SDKException("error unwrapping key", e);
205+
}
206+
var wrappedKey = response.getEntityWrappedKey().toByteArray();
207+
if (sessionKeyType != KeyType.RSA2048Key) {
208+
209+
if (ecKeyPair == null) {
210+
throw new SDKException("ECKeyPair is null. Unable to proceed with the unwrap operation.");
211+
}
212+
213+
var kasEphemeralPublicKey = response.getSessionPublicKey();
214+
var publicKey = ECKeyPair.publicKeyFromPem(kasEphemeralPublicKey);
215+
byte[] symKey = ECKeyPair.computeECDHKey(publicKey, ecKeyPair.getPrivateKey());
216+
217+
var sessionKey = ECKeyPair.calculateHKDF(GLOBAL_KEY_SALT, symKey);
218+
219+
AesGcm gcm = new AesGcm(sessionKey);
220+
AesGcm.Encrypted encrypted = new AesGcm.Encrypted(wrappedKey);
221+
return gcm.decrypt(encrypted);
222+
} else {
223+
return decryptor.decrypt(wrappedKey);
226224
}
227-
228225
}
229226

230227
public byte[] unwrapNanoTDF(NanoTDFType.ECCurve curve, String header, String kasURL) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private ProtocolClient getUnauthenticatedProtocolClient(String endpoint, Interce
279279
NetworkProtocol.GRPC,
280280
null,
281281
GETConfiguration.Enabled.INSTANCE,
282-
authInterceptor == null ? Collections.emptyList() : List.of((_config) -> authInterceptor)
282+
authInterceptor == null ? Collections.emptyList() : List.of(_config -> authInterceptor)
283283
);
284284

285285
return new ProtocolClient(new ConnectOkHttpClient(httpClient.build()), protocolClientConfig);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.security.*;
2828
import java.text.ParseException;
2929
import java.util.*;
30-
import java.util.concurrent.ExecutionException;
3130

3231
/**
3332
* The TDF class is responsible for handling operations related to
@@ -476,7 +475,7 @@ private static byte[] calculateSignature(byte[] data, byte[] secret, Config.Inte
476475
public TDFObject createTDF(InputStream payload,
477476
OutputStream outputStream,
478477
Config.TDFConfig tdfConfig, SDK.KAS kas, AttributesServiceClient attributesServiceClient)
479-
throws IOException, JOSEException, AutoConfigureException, InterruptedException, ExecutionException, DecoderException {
478+
throws IOException, JOSEException, AutoConfigureException, DecoderException {
480479

481480
if (tdfConfig.autoconfigure) {
482481
Autoconfigure.Granter granter = new Autoconfigure.Granter(new ArrayList<>());

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public AuthHeaders getAuthHeaders(URL url, String method) {
8282
SignedJWT proof = dpopFactory.createDPoPJWT(method, url.toURI(), t);
8383
dpopProof = proof.serialize();
8484
} catch (URISyntaxException e) {
85-
throw new RuntimeException("Invalid URI syntax for DPoP proof creation", e);
85+
throw new SDKException("Invalid URI syntax for DPoP proof creation", e);
8686
} catch (JOSEException e) {
87-
throw new RuntimeException("Error creating DPoP proof", e);
87+
throw new SDKException("Error creating DPoP proof", e);
8888
}
8989

9090
return new AuthHeaders(
@@ -127,7 +127,6 @@ private synchronized AccessToken getToken() {
127127
throw new RuntimeException("Token request failed: " + error);
128128
}
129129

130-
131130
var tokens = tokenResponse.toSuccessResponse().getTokens();
132131
if (tokens.getDPoPAccessToken() != null) {
133132
logger.trace("retrieved a new DPoP access token");

0 commit comments

Comments
 (0)