@@ -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 ) {
0 commit comments