|
19 | 19 | import com.nimbusds.jose.crypto.RSASSAVerifier; |
20 | 20 | import com.nimbusds.jose.jwk.RSAKey; |
21 | 21 | import java.net.URI; |
| 22 | +import java.nio.ByteBuffer; |
22 | 23 | import java.nio.file.Files; |
23 | 24 | import java.nio.file.Path; |
24 | 25 | import java.security.KeyPairGenerator; |
| 26 | +import java.security.SecureRandom; |
25 | 27 | import java.security.interfaces.RSAPrivateCrtKey; |
26 | 28 | import java.util.ArrayList; |
27 | 29 | import java.util.Base64; |
28 | 30 | import java.util.HashMap; |
| 31 | +import java.util.HexFormat; |
29 | 32 | import java.util.List; |
30 | 33 | import java.util.Map; |
31 | 34 | import org.jspecify.annotations.Nullable; |
@@ -217,6 +220,36 @@ void pkcs1Key() throws Exception { |
217 | 220 | assertThat(loaded.getModulus().decodeToBigInteger()).isEqualTo(key.getModulus()); |
218 | 221 | } |
219 | 222 |
|
| 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 | + |
220 | 253 | @Test |
221 | 254 | @DisplayName("device row 22: one flow signs a fresh assertion per request, audienced to each endpoint") |
222 | 255 | void deviceFlow() throws Exception { |
@@ -314,18 +347,38 @@ private static Map<String, Object> verify(WebIdentity identity, String assertion |
314 | 347 | return new HashMap<>(jws.getPayload().toJSONObject()); |
315 | 348 | } |
316 | 349 |
|
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. */ |
318 | 351 | 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()); |
323 | 374 | } |
324 | 375 | } |
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; |
329 | 382 | } |
330 | 383 |
|
331 | 384 | /** Counts loads, to show nothing was signed. */ |
|
0 commit comments