Skip to content

feat(sdk): DSPX-3309 add hybrid post-quantum key wrapping for KAS (X-Wing, ECDH+ML-KEM)#368

Open
sujankota wants to merge 1 commit into
mainfrom
DSPX-3309-hybrid-pq-key-wrapping
Open

feat(sdk): DSPX-3309 add hybrid post-quantum key wrapping for KAS (X-Wing, ECDH+ML-KEM)#368
sujankota wants to merge 1 commit into
mainfrom
DSPX-3309-hybrid-pq-key-wrapping

Conversation

@sujankota
Copy link
Copy Markdown
Contributor

@sujankota sujankota commented May 18, 2026

Add hybrid post-quantum key wrapping to the Java SDK so TDFs can be protected against "harvest now, decrypt later" attacks while preserving classical security
guarantees during the PQC transition.

Introduces three new KeyType values backed by hybrid KEMs:

  • HybridXWingKey (hpqt:xwing) — X-Wing (X25519 + ML-KEM-768)
  • HybridSecp256r1MLKEM768Key (hpqt:secp256r1-mlkem768)
  • HybridSecp384r1MLKEM1024Key (hpqt:secp384r1-mlkem1024)

When a KAS advertises one of these algorithms, TDF.upsertAndGetNewKeyAccess routes through HybridCrypto.wrapDEK, which performs both a classical ECDH/X25519 key
agreement and an ML-KEM encapsulation, combines the two shared secrets, and uses the result to wrap the DEK. A new hybrid-wrapped key-access type is emitted; the
ephemeral classical public key and ML-KEM ciphertext are packaged together inside an ASN.1 envelope stored in wrappedKey (rather than the separate ephemeralPublicKey
field used for EC-wrapped keys).

New supporting classes: HybridCrypto, HybridNISTKeyPair, XWingKeyPair, plus unit and TDF round-trip tests.

Summary by CodeRabbit

  • New Features
    • Introduced hybrid post-quantum key wrapping algorithms, including X-Wing and NIST hybrid variants (P-256+ML-KEM-768, P-384+ML-KEM-1024), providing enhanced cryptographic security against emerging quantum threats.

Review Change Stack

@sujankota sujankota requested review from a team as code owners May 18, 2026 19:21
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 18, 2026

📝 Walkthrough

Walkthrough

This PR adds hybrid post-quantum cryptography support to the Java TDF SDK by introducing X-Wing and NIST ECDH+ML-KEM key wrapping schemes. It includes implementations for both key schemes, shared envelope marshaling and key derivation utilities, integration into TDF manifest creation, and comprehensive unit and integration test coverage.

Changes

Hybrid Post-Quantum Cryptography

Layer / File(s) Summary
KeyType enum - Hybrid key support
sdk/src/main/java/io/opentdf/platform/sdk/KeyType.java
Three hybrid key type constants are added (HybridXWingKey, HybridSecp256r1MLKEM768Key, HybridSecp384r1MLKEM1024Key) along with a new isHybrid() method.
HybridCrypto utility infrastructure
sdk/src/main/java/io/opentdf/platform/sdk/HybridCrypto.java
Package-private helper provides a wrapDEK dispatcher routing to scheme-specific implementations, DER SEQUENCE envelope marshaling with strict trailing-byte rejection, HKDF-SHA256 key derivation using SHA-256("TDF") as default salt, and strict PEM encoding/decoding with size validation.
XWingKeyPair - X-Wing hybrid implementation
sdk/src/main/java/io/opentdf/platform/sdk/XWingKeyPair.java
X-Wing (X25519 + ML-KEM-768) keypair generation via BouncyCastle with fixed sizes. PEM serialization, DEK wrapping via X-Wing encapsulation and AES-GCM encryption with derived wrap key, and DEK unwrapping via decapsulation and decryption with strict validation.
HybridNISTKeyPair - NIST+ML-KEM hybrid implementation
sdk/src/main/java/io/opentdf/platform/sdk/HybridNISTKeyPair.java
Two hybrid variants combining NIST ECDH (P-256 or P-384) with ML-KEM (768 or 1024) using rejection-sampled EC scalar generation and BouncyCastle ML-KEM encapsulation. Supports PEM encoding/decoding of concatenated EC and ML-KEM material. DEK wrapping generates ephemeral EC point, derives ECDH secret with left-padding, encapsulates ML-KEM secret, and encrypts DEK.
TDF manifest - Hybrid key access creation
sdk/src/main/java/io/opentdf/platform/sdk/TDF.java
createKeyAccess adds a new kHybridWrapped constant and branch that checks keyType.isHybrid(), wraps the DEK via HybridCrypto.wrapDEK dispatcher, Base64-encodes result into keyAccess.wrappedKey, and sets keyAccess.keyType to kHybridWrapped with ephemeralPublicKey unset.
HybridCryptoTest - Unit tests for hybrid schemes
sdk/src/test/java/io/opentdf/platform/sdk/HybridCryptoTest.java
Full round-trip wrap/unwrap tests for XWing, P-256+MLKEM768, and P-384+MLKEM1024; PEM format and key length validation; wrap randomness assertion; cross-scheme unwrap rejection; malformed PEM handling (block type mismatch, truncation, size mismatch); dispatcher routing and rejection of non-hybrid types; and envelope truncation rejection.
TDFHybridTest - Integration tests for hybrid TDF creation
sdk/src/test/java/io/opentdf/platform/sdk/TDFHybridTest.java
Verifies hybrid KeyTypes produce manifest KeyAccess with keyType=="hybrid-wrapped" and ephemeralPublicKey==null. Tests XWing, P-256+MLKEM768, and P-384+MLKEM1024 variants by Base64-decoding wrappedKey and round-tripping unwrap to recover a 32-byte symmetric key. Uses mocked KAS registry and fake KAS with public key material.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 Post-quantum secrets wrapped in hybrid dance,
X-Wing and NIST pair in cryptographic trance,
Envelopes marshaled, keys derived with care,
TDF manifests now breathe PQC air! 🔐

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat(sdk): DSPX-3309 add hybrid post-quantum key wrapping for KAS (X-Wing, ECDH+ML-KEM)' clearly and specifically summarizes the main change: adding hybrid post-quantum key wrapping support with two specific algorithm variants, matching the actual changeset which introduces HybridCrypto, HybridNISTKeyPair, XWingKeyPair classes and KeyType enhancements.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch DSPX-3309-hybrid-pq-key-wrapping

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
D Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for hybrid post-quantum key wrapping, specifically X-Wing (X25519 + ML-KEM-768) and NIST hybrid schemes (P-256/P-384 + ML-KEM). It adds new classes for cryptographic operations and ASN.1 envelope management, updates the KeyType enum, and integrates these capabilities into the TDF creation process. Feedback recommends specifying the UTF-8 character set when converting strings to bytes and suggests explicitly referencing the BouncyCastle provider in cryptographic calls to ensure platform consistency and avoid provider ambiguity.

static byte[] defaultTDFSalt() {
try {
MessageDigest d = MessageDigest.getInstance("SHA-256");
d.update("TDF".getBytes());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is recommended to specify a character set when calling getBytes() to ensure consistent behavior across different platforms. Using StandardCharsets.UTF_8 is a best practice.

Suggested change
d.update("TDF".getBytes());
d.update("TDF".getBytes(java.nio.charset.StandardCharsets.UTF_8));

PublicKey peerPub = kf.generatePublic(peerSpec);
PrivateKey myPriv = kf.generatePrivate(mySpec);

KeyAgreement ka = KeyAgreement.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When using KeyAgreement.getInstance, it is generally safer to use the provider instance directly if it has already been loaded, or ensure that the provider name is correctly referenced. While "BC" is standard for BouncyCastle, consider if the provider should be explicitly passed to avoid ambiguity in environments with multiple providers.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
sdk/src/main/java/io/opentdf/platform/sdk/XWingKeyPair.java (1)

12-13: ⚡ Quick win

Clear the derived secrets after wrap/unwrap.

sharedSecret and wrapKey stay live until GC on both paths. In the wrapping primitive itself, this is worth clearing in a finally block once AES-GCM completes.

💡 Suggested fix
 import java.security.SecureRandom;
+import java.util.Arrays;
@@
-        SecretWithEncapsulation enc = new XWingKEMGenerator(new SecureRandom()).generateEncapsulated(pub);
-        byte[] sharedSecret = enc.getSecret();
-        byte[] ciphertext = enc.getEncapsulation();
-
-        byte[] wrapKey = HybridCrypto.deriveWrapKey(sharedSecret, null, null);
-        byte[] encryptedDek = new AesGcm(wrapKey).encrypt(dek).asBytes();
-        return HybridCrypto.marshalEnvelope(ciphertext, encryptedDek);
+        SecretWithEncapsulation enc = new XWingKEMGenerator(new SecureRandom()).generateEncapsulated(pub);
+        byte[] sharedSecret = enc.getSecret();
+        byte[] ciphertext = enc.getEncapsulation();
+        byte[] wrapKey = null;
+        try {
+            wrapKey = HybridCrypto.deriveWrapKey(sharedSecret, null, null);
+            byte[] encryptedDek = new AesGcm(wrapKey).encrypt(dek).asBytes();
+            return HybridCrypto.marshalEnvelope(ciphertext, encryptedDek);
+        } finally {
+            Arrays.fill(sharedSecret, (byte) 0);
+            if (wrapKey != null) {
+                Arrays.fill(wrapKey, (byte) 0);
+            }
+        }
@@
-        byte[] sharedSecret = new XWingKEMExtractor(priv).extractSecret(ciphertext);
-        byte[] wrapKey = HybridCrypto.deriveWrapKey(sharedSecret, null, null);
-        return new AesGcm(wrapKey).decrypt(new AesGcm.Encrypted(encryptedDek));
+        byte[] sharedSecret = new XWingKEMExtractor(priv).extractSecret(ciphertext);
+        byte[] wrapKey = null;
+        try {
+            wrapKey = HybridCrypto.deriveWrapKey(sharedSecret, null, null);
+            return new AesGcm(wrapKey).decrypt(new AesGcm.Encrypted(encryptedDek));
+        } finally {
+            Arrays.fill(sharedSecret, (byte) 0);
+            if (wrapKey != null) {
+                Arrays.fill(wrapKey, (byte) 0);
+            }
+        }

Also applies to: 67-73, 87-90

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@sdk/src/main/java/io/opentdf/platform/sdk/XWingKeyPair.java` around lines 12
- 13, In XWingKeyPair.java ensure derived secrets are explicitly cleared after
use: locate the methods that perform wrapping/unwrapping (e.g., the wrap/unwrap
primitives around sharedSecret and wrapKey) and add try { ... } finally {
Arrays.fill(sharedSecret, (byte)0); Arrays.fill(wrapKey, (byte)0); sharedSecret
= null; wrapKey = null; } (or equivalent) so both success and exception paths
wipe and null out the byte[] secrets; apply the same pattern to the other
occurrences noted around the blocks at the other wrap/unwrap usages (lines
referenced 67-73 and 87-90).
sdk/src/main/java/io/opentdf/platform/sdk/HybridNISTKeyPair.java (1)

203-207: ⚡ Quick win

Zero the hybrid shared secrets on both paths.

ecdhSecret, mlSecret, combinedSecret, and wrapKey all remain in heap memory after the DEK is processed. Given this sits at the core crypto boundary, clear them in finally blocks.

💡 Suggested fix
-        byte[] combinedSecret = concat(ecdhSecret, mlSecret);
-        byte[] hybridCt = concat(ephemeralEcPub, mlCiphertext);
-        byte[] wrapKey = HybridCrypto.deriveWrapKey(combinedSecret, null, null);
-        byte[] encryptedDek = new AesGcm(wrapKey).encrypt(dek).asBytes();
-        return HybridCrypto.marshalEnvelope(hybridCt, encryptedDek);
+        byte[] combinedSecret = null;
+        byte[] wrapKey = null;
+        try {
+            combinedSecret = concat(ecdhSecret, mlSecret);
+            byte[] hybridCt = concat(ephemeralEcPub, mlCiphertext);
+            wrapKey = HybridCrypto.deriveWrapKey(combinedSecret, null, null);
+            byte[] encryptedDek = new AesGcm(wrapKey).encrypt(dek).asBytes();
+            return HybridCrypto.marshalEnvelope(hybridCt, encryptedDek);
+        } finally {
+            Arrays.fill(ecdhSecret, (byte) 0);
+            Arrays.fill(mlSecret, (byte) 0);
+            if (combinedSecret != null) {
+                Arrays.fill(combinedSecret, (byte) 0);
+            }
+            if (wrapKey != null) {
+                Arrays.fill(wrapKey, (byte) 0);
+            }
+        }
@@
-        byte[] combinedSecret = concat(ecdhSecret, mlSecret);
-        byte[] wrapKey = HybridCrypto.deriveWrapKey(combinedSecret, null, null);
-        return new AesGcm(wrapKey).decrypt(new AesGcm.Encrypted(encryptedDek));
+        byte[] combinedSecret = null;
+        byte[] wrapKey = null;
+        try {
+            combinedSecret = concat(ecdhSecret, mlSecret);
+            wrapKey = HybridCrypto.deriveWrapKey(combinedSecret, null, null);
+            return new AesGcm(wrapKey).decrypt(new AesGcm.Encrypted(encryptedDek));
+        } finally {
+            Arrays.fill(ecdhSecret, (byte) 0);
+            Arrays.fill(mlSecret, (byte) 0);
+            if (combinedSecret != null) {
+                Arrays.fill(combinedSecret, (byte) 0);
+            }
+            if (wrapKey != null) {
+                Arrays.fill(wrapKey, (byte) 0);
+            }
+        }

Also applies to: 231-235

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@sdk/src/main/java/io/opentdf/platform/sdk/HybridNISTKeyPair.java` around
lines 203 - 207, The shared-secret bytes (ecdhSecret, mlSecret, combinedSecret,
wrapKey) in HybridNISTKeyPair must be zeroed after use; wrap the encryption and
the corresponding decryption path (the block around deriveWrapKey/encrypt and
the block referenced at 231-235) in try/finally so that in each finally you
overwrite each secret byte[] (e.g., Arrays.fill(..., (byte)0) or equivalent) and
null out references to avoid lingering heap data; ensure you zero ecdhSecret,
mlSecret, combinedSecret, and wrapKey regardless of success or exception and do
so in the same methods that call HybridCrypto.deriveWrapKey, AesGcm.encrypt, and
HybridCrypto.unmarshal/unwrap to guarantee cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@sdk/src/main/java/io/opentdf/platform/sdk/HybridCrypto.java`:
- Around line 74-100: The ASN.1 parsing can throw IllegalArgumentException and
IllegalStateException (e.g., ASN1ParsingException) which currently escape
normalization; update the code to catch and wrap those in SDKException.
Specifically, in unmarshalEnvelope around the
ASN1InputStream/ASN1Sequence.getInstance calls add handlers for
IllegalArgumentException and IllegalStateException (or a multi-catch alongside
IOException) and rethrow new SDKException(..., e). Also update
readImplicitOctetString to guard the ASN1TaggedObject.getInstance and
ASN1OctetString.getInstance calls with a try/catch for
IllegalArgumentException/IllegalStateException and throw an SDKException with a
clear message; refer to the methods unmarshalEnvelope and
readImplicitOctetString and the BouncyCastle calls ASN1Sequence.getInstance,
ASN1TaggedObject.getInstance, and ASN1OctetString.getInstance when locating
changes.

In `@sdk/src/main/java/io/opentdf/platform/sdk/KeyType.java`:
- Around line 18-20: Add the three new hybrid enum constants to both factory
switch statements so fromAlgorithm(...) and fromPublicKeyAlgorithm(...) return
the correct KeyType for HybridXWingKey, HybridSecp256r1MLKEM768Key, and
HybridSecp384r1MLKEM1024Key; locate the switch in the KeyType enum's
fromAlgorithm(...) method and the switch in fromPublicKeyAlgorithm(...) and add
matching case entries that map the corresponding protobuf algorithm enum values
to these KeyType constants to avoid IllegalArgumentException when those protobuf
values are encountered.

---

Nitpick comments:
In `@sdk/src/main/java/io/opentdf/platform/sdk/HybridNISTKeyPair.java`:
- Around line 203-207: The shared-secret bytes (ecdhSecret, mlSecret,
combinedSecret, wrapKey) in HybridNISTKeyPair must be zeroed after use; wrap the
encryption and the corresponding decryption path (the block around
deriveWrapKey/encrypt and the block referenced at 231-235) in try/finally so
that in each finally you overwrite each secret byte[] (e.g., Arrays.fill(...,
(byte)0) or equivalent) and null out references to avoid lingering heap data;
ensure you zero ecdhSecret, mlSecret, combinedSecret, and wrapKey regardless of
success or exception and do so in the same methods that call
HybridCrypto.deriveWrapKey, AesGcm.encrypt, and HybridCrypto.unmarshal/unwrap to
guarantee cleanup.

In `@sdk/src/main/java/io/opentdf/platform/sdk/XWingKeyPair.java`:
- Around line 12-13: In XWingKeyPair.java ensure derived secrets are explicitly
cleared after use: locate the methods that perform wrapping/unwrapping (e.g.,
the wrap/unwrap primitives around sharedSecret and wrapKey) and add try { ... }
finally { Arrays.fill(sharedSecret, (byte)0); Arrays.fill(wrapKey, (byte)0);
sharedSecret = null; wrapKey = null; } (or equivalent) so both success and
exception paths wipe and null out the byte[] secrets; apply the same pattern to
the other occurrences noted around the blocks at the other wrap/unwrap usages
(lines referenced 67-73 and 87-90).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d2649e03-5d69-4cad-bbe5-02e23e6fc142

📥 Commits

Reviewing files that changed from the base of the PR and between 9991b07 and 38943bc.

📒 Files selected for processing (7)
  • sdk/src/main/java/io/opentdf/platform/sdk/HybridCrypto.java
  • sdk/src/main/java/io/opentdf/platform/sdk/HybridNISTKeyPair.java
  • sdk/src/main/java/io/opentdf/platform/sdk/KeyType.java
  • sdk/src/main/java/io/opentdf/platform/sdk/TDF.java
  • sdk/src/main/java/io/opentdf/platform/sdk/XWingKeyPair.java
  • sdk/src/test/java/io/opentdf/platform/sdk/HybridCryptoTest.java
  • sdk/src/test/java/io/opentdf/platform/sdk/TDFHybridTest.java

Comment on lines +74 to +100
static byte[][] unmarshalEnvelope(byte[] der) {
try (ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(der))) {
ASN1Primitive prim = in.readObject();
if (prim == null) {
throw new SDKException("hybrid wrapped key envelope is empty");
}
if (in.readObject() != null) {
throw new SDKException("hybrid wrapped key envelope has trailing bytes");
}
ASN1Sequence seq = ASN1Sequence.getInstance(prim);
if (seq.size() != 2) {
throw new SDKException("hybrid wrapped key envelope must have 2 elements, got " + seq.size());
}
byte[] hybridCt = readImplicitOctetString(seq.getObjectAt(0), 0);
byte[] encDek = readImplicitOctetString(seq.getObjectAt(1), 1);
return new byte[][] { hybridCt, encDek };
} catch (IOException e) {
throw new SDKException("failed to decode hybrid wrapped key envelope", e);
}
}

private static byte[] readImplicitOctetString(org.bouncycastle.asn1.ASN1Encodable enc, int expectedTag) {
ASN1TaggedObject tagged = ASN1TaggedObject.getInstance(enc);
if (tagged.getTagNo() != expectedTag) {
throw new SDKException("expected context tag " + expectedTag + " but got " + tagged.getTagNo());
}
return org.bouncycastle.asn1.ASN1OctetString.getInstance(tagged, false).getOctets();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

In Bouncy Castle ASN.1, what exceptions can ASN1Sequence.getInstance(Object)andASN1OctetString.getInstance(ASN1TaggedObject, boolean) throw when the ASN.1 input is the wrong type?

💡 Result:

In the Bouncy Castle ASN.1 library, both ASN1Sequence.getInstance(Object) and ASN1OctetString.getInstance(ASN1TaggedObject, boolean) are designed to throw a java.lang.IllegalArgumentException when the provided input cannot be converted to the expected type [1][2]. When these methods are called with an object that does not match the required ASN.1 structure (for example, passing a non-sequence object to ASN1Sequence.getInstance), the library explicitly throws an IllegalArgumentException [1][2]. This exception often includes a descriptive message, such as "unknown object in getInstance" or similar, indicating that the input type is incompatible with the requested ASN.1 class [3][4]. While IllegalArgumentException is the standard exception for type mismatches in these factory methods, other ASN.1-related exceptions may occur during broader parsing operations: 1. ASN1ParsingException: This is a runtime exception (extending IllegalStateException) thrown when the ASN.1 data is correctly encoded but contains unexpected data while building an object [5][6]. 2. ASN1Exception: This is an IOException thrown when the library encounters corrupted or unexpected data within an ASN.1 stream [7][6]. In summary, for the specific getInstance methods mentioned, you should expect and handle java.lang.IllegalArgumentException when the input type is incorrect [1][2].

Citations:


Normalize all ASN.1 shape failures to SDKException.

The catch block only handles IOException, but Bouncy Castle's ASN.1 getInstance(...) methods throw IllegalArgumentException when the input type is incorrect, and ASN1ParsingException (extends IllegalStateException) when the ASN.1 is validly encoded but structurally unexpected. These exceptions will propagate uncaught from the parser, bypassing SDK exception normalization on untrusted envelope bytes.

Suggested fix
-        } catch (IOException e) {
+        } catch (IOException | IllegalArgumentException | IllegalStateException e) {
             throw new SDKException("failed to decode hybrid wrapped key envelope", e);
         }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@sdk/src/main/java/io/opentdf/platform/sdk/HybridCrypto.java` around lines 74
- 100, The ASN.1 parsing can throw IllegalArgumentException and
IllegalStateException (e.g., ASN1ParsingException) which currently escape
normalization; update the code to catch and wrap those in SDKException.
Specifically, in unmarshalEnvelope around the
ASN1InputStream/ASN1Sequence.getInstance calls add handlers for
IllegalArgumentException and IllegalStateException (or a multi-catch alongside
IOException) and rethrow new SDKException(..., e). Also update
readImplicitOctetString to guard the ASN1TaggedObject.getInstance and
ASN1OctetString.getInstance calls with a try/catch for
IllegalArgumentException/IllegalStateException and throw an SDKException with a
clear message; refer to the methods unmarshalEnvelope and
readImplicitOctetString and the BouncyCastle calls ASN1Sequence.getInstance,
ASN1TaggedObject.getInstance, and ASN1OctetString.getInstance when locating
changes.

Comment on lines +18 to +20
HybridXWingKey("hpqt:xwing"),
HybridSecp256r1MLKEM768Key("hpqt:secp256r1-mlkem768"),
HybridSecp384r1MLKEM1024Key("hpqt:secp384r1-mlkem1024");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Hybrid algorithm enum values =="
rg -n --type java 'ALGORITHM_.*(XWING|MLKEM)|KAS_PUBLIC_KEY_ALG_ENUM_.*(XWING|MLKEM)'

echo
echo "== Call sites of KeyType.fromAlgorithm / fromPublicKeyAlgorithm =="
rg -n --type java '\bKeyType\.(fromAlgorithm|fromPublicKeyAlgorithm)\s*\('

Repository: opentdf/java-sdk

Length of output: 95


🏁 Script executed:

cat -n sdk/src/main/java/io/opentdf/platform/sdk/KeyType.java

Repository: opentdf/java-sdk

Length of output: 4297


🏁 Script executed:

fd -e proto | head -20

Repository: opentdf/java-sdk

Length of output: 42


🏁 Script executed:

rg -l "enum Algorithm|class Algorithm" --type java | head -10

Repository: opentdf/java-sdk

Length of output: 42


🏁 Script executed:

rg "import io.opentdf.platform.policy.Algorithm|import io.opentdf.platform.policy.KasPublicKeyAlgEnum" --type java -l | head -10

Repository: opentdf/java-sdk

Length of output: 410


🏁 Script executed:

rg "fromAlgorithm|fromPublicKeyAlgorithm" --type java -B 2 -A 2

Repository: opentdf/java-sdk

Length of output: 5904


🏁 Script executed:

find . -name "*.proto" | xargs cat | grep -A 30 "enum Algorithm\|enum KasPublicKeyAlgEnum" | head -80

Repository: opentdf/java-sdk

Length of output: 42


🏁 Script executed:

rg "enum ALGORITHM_|enum KAS_PUBLIC_KEY_ALG_ENUM_" --type java -A 5 | head -50

Repository: opentdf/java-sdk

Length of output: 42


🏁 Script executed:

rg "ALGORITHM_|KAS_PUBLIC_KEY_ALG_ENUM_" --type java | grep -E "(case|=)" | head -30

Repository: opentdf/java-sdk

Length of output: 1511


🏁 Script executed:

find . -path ./build -prune -o -type f -name "*.java" -exec grep -l "ALGORITHM_.*XWING\|ALGORITHM_.*MLKEM\|KAS_PUBLIC_KEY_ALG_ENUM.*XWING\|KAS_PUBLIC_KEY_ALG_ENUM.*MLKEM" {} \;

Repository: opentdf/java-sdk

Length of output: 42


Add hybrid key type support to fromAlgorithm() and fromPublicKeyAlgorithm() factory methods to maintain API completeness.

The three new hybrid enum constants (HybridXWingKey, HybridSecp256r1MLKEM768Key, HybridSecp384r1MLKEM1024Key) are missing from both factory method switches. These methods are actively called in Config.java and Autoconfigure.java to map from protobuf enums to KeyType. When the corresponding protobuf enum values are added, any code path using those typed conversions will fail with IllegalArgumentException. Add matching cases to both switches to ensure complete coverage per the coding guideline to "keep public SDK APIs stable and additive where possible."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@sdk/src/main/java/io/opentdf/platform/sdk/KeyType.java` around lines 18 - 20,
Add the three new hybrid enum constants to both factory switch statements so
fromAlgorithm(...) and fromPublicKeyAlgorithm(...) return the correct KeyType
for HybridXWingKey, HybridSecp256r1MLKEM768Key, and HybridSecp384r1MLKEM1024Key;
locate the switch in the KeyType enum's fromAlgorithm(...) method and the switch
in fromPublicKeyAlgorithm(...) and add matching case entries that map the
corresponding protobuf algorithm enum values to these KeyType constants to avoid
IllegalArgumentException when those protobuf values are encountered.

@github-actions
Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant