Skip to content

Commit fedad89

Browse files
devin-ai-integration[bot]devin-ai-keycardLarry-Osakwe
authored
test(oauth): parse the PKCS#8 wrapper in the PKCS#1 key test helper (#25)
* test(oauth): parse the PKCS#8 wrapper in the PKCS#1 key test helper Co-Authored-By: Larry Osakwe <larry@keycard.ai> * test(oauth): seed the generated-key loop deterministically Co-Authored-By: Larry Osakwe <larry@keycard.ai> --------- Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai> Co-authored-by: Larry Osakwe <larry@keycard.ai>
1 parent e677645 commit fedad89

1 file changed

Lines changed: 62 additions & 9 deletions

File tree

‎keycard-oauth/src/test/java/ai/keycard/oauth/conformance/WebIdentityConformanceTest.java‎

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import com.nimbusds.jose.crypto.RSASSAVerifier;
2020
import com.nimbusds.jose.jwk.RSAKey;
2121
import java.net.URI;
22+
import java.nio.ByteBuffer;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
2425
import java.security.KeyPairGenerator;
26+
import java.security.SecureRandom;
2527
import java.security.interfaces.RSAPrivateCrtKey;
2628
import java.util.ArrayList;
2729
import java.util.Base64;
2830
import java.util.HashMap;
31+
import java.util.HexFormat;
2932
import java.util.List;
3033
import java.util.Map;
3134
import org.jspecify.annotations.Nullable;
@@ -217,6 +220,36 @@ void pkcs1Key() throws Exception {
217220
assertThat(loaded.getModulus().decodeToBigInteger()).isEqualTo(key.getModulus());
218221
}
219222

223+
@Test
224+
@DisplayName("PKCS#1 extraction round-trips 200 generated RSA keys")
225+
void pkcs1GeneratedKeys() throws Exception {
226+
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
227+
random.setSeed(new byte[] {0});
228+
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
229+
generator.initialize(2048, random);
230+
Path file = dir.resolve("generated.pem");
231+
232+
for (int i = 0; i < 200; i++) {
233+
RSAPrivateCrtKey key =
234+
(RSAPrivateCrtKey) generator.generateKeyPair().getPrivate();
235+
String body = Base64.getEncoder().encodeToString(pkcs1(key.getEncoded()));
236+
Files.writeString(file, "-----BEGIN RSA PRIVATE KEY-----\n" + body + "\n-----END RSA PRIVATE KEY-----\n");
237+
RSAKey loaded = (RSAKey)
238+
identity("generated", "my-client").publicJwks().getKeys().get(0);
239+
assertThat(loaded.getModulus().decodeToBigInteger()).as("key %s", i).isEqualTo(key.getModulus());
240+
}
241+
}
242+
243+
@Test
244+
@DisplayName("PKCS#8 extraction treats a nested OCTET STRING marker as key data")
245+
void pkcs1IgnoresMarkerInKeyData() {
246+
String payload = "00".repeat(122) + "0482ffff0000";
247+
HexFormat hex = HexFormat.of();
248+
byte[] pkcs8 = hex.parseHex("308195020100300d06092a864886f70d0101010500048180" + payload);
249+
250+
assertThat(pkcs1(pkcs8)).isEqualTo(hex.parseHex(payload));
251+
}
252+
220253
@Test
221254
@DisplayName("device row 22: one flow signs a fresh assertion per request, audienced to each endpoint")
222255
void deviceFlow() throws Exception {
@@ -314,18 +347,38 @@ private static Map<String, Object> verify(WebIdentity identity, String assertion
314347
return new HashMap<>(jws.getPayload().toJSONObject());
315348
}
316349

317-
/** Pulls the PKCS#1 RSAPrivateKey out of a PKCS#8 encoding: the last OCTET STRING. */
350+
/** Pulls the privateKey OCTET STRING out of PKCS#8 PrivateKeyInfo. */
318351
private static byte[] pkcs1(byte[] pkcs8) {
319-
int i = pkcs8.length;
320-
for (int p = 0; p < pkcs8.length - 4; p++) {
321-
if (pkcs8[p] == 0x04 && (pkcs8[p + 1] & 0xff) == 0x82) {
322-
i = p;
352+
ByteBuffer sequence = readDerValue(ByteBuffer.wrap(pkcs8), 0x30);
353+
readDerValue(sequence, 0x02); // version
354+
readDerValue(sequence, 0x30); // AlgorithmIdentifier
355+
ByteBuffer privateKey = readDerValue(sequence, 0x04);
356+
byte[] out = new byte[privateKey.remaining()];
357+
privateKey.get(out);
358+
return out;
359+
}
360+
361+
private static ByteBuffer readDerValue(ByteBuffer der, int tag) {
362+
if (der.remaining() < 2 || Byte.toUnsignedInt(der.get()) != tag) {
363+
throw new IllegalArgumentException("expected DER tag " + tag);
364+
}
365+
int length = Byte.toUnsignedInt(der.get());
366+
if (length >= 0x80) {
367+
int lengthBytes = length & 0x7f;
368+
if (lengthBytes == 0 || lengthBytes > Integer.BYTES || lengthBytes > der.remaining()) {
369+
throw new IllegalArgumentException("invalid DER length field");
370+
}
371+
length = 0;
372+
for (int i = 0; i < lengthBytes; i++) {
373+
length = (length << 8) | Byte.toUnsignedInt(der.get());
323374
}
324375
}
325-
int length = ((pkcs8[i + 2] & 0xff) << 8) | (pkcs8[i + 3] & 0xff);
326-
byte[] out = new byte[length];
327-
System.arraycopy(pkcs8, i + 4, out, 0, length);
328-
return out;
376+
if (length < 0 || length > der.remaining()) {
377+
throw new IllegalArgumentException("DER value exceeds its container");
378+
}
379+
ByteBuffer value = der.slice(der.position(), length);
380+
der.position(der.position() + length);
381+
return value;
329382
}
330383

331384
/** Counts loads, to show nothing was signed. */

0 commit comments

Comments
 (0)