Skip to content

Commit 088d972

Browse files
committed
feat: Support EC private keys in PemSslContext
1 parent 9929b71 commit 088d972

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

docs/superpowers/plans/2026-05-21-https-support.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ SKIP=commitlint git commit -m "feat: Add PemSslContext for loading PEM cert + RS
323323
- Modify: `src/main/java/com/retailsvc/http/internal/PemSslContext.java`
324324
- Modify: `src/test/java/com/retailsvc/http/internal/PemSslContextTest.java`
325325

326-
- [ ] **Step 1: Add the failing EC test**
326+
- [x] **Step 1: Add the failing EC test**
327327

328328
Append to `PemSslContextTest`:
329329

@@ -340,15 +340,15 @@ Append to `PemSslContextTest`:
340340
}
341341
```
342342

343-
- [ ] **Step 2: Run, confirm it fails**
343+
- [x] **Step 2: Run, confirm it fails**
344344

345345
```bash
346346
mvn test -Dtest=PemSslContextTest#loadsEcPemPair
347347
```
348348

349349
Expected: FAIL — `InvalidKeySpecException` wrapped in `IllegalStateException` from RSA `KeyFactory` rejecting EC bytes.
350350

351-
- [ ] **Step 3: Extend `readPrivateKey` with EC fallback**
351+
- [x] **Step 3: Extend `readPrivateKey` with EC fallback**
352352

353353
In `PemSslContext.java`, replace the entire `readPrivateKey` method with:
354354

@@ -388,15 +388,15 @@ In `PemSslContext.java`, replace the entire `readPrivateKey` method with:
388388
}
389389
```
390390

391-
- [ ] **Step 4: Run both happy-path tests**
391+
- [x] **Step 4: Run both happy-path tests**
392392

393393
```bash
394394
mvn test -Dtest=PemSslContextTest
395395
```
396396

397397
Expected: 2 tests pass.
398398

399-
- [ ] **Step 5: Commit**
399+
- [x] **Step 5: Commit**
400400

401401
```bash
402402
git add src/main/java/com/retailsvc/http/internal/PemSslContext.java \

src/main/java/com/retailsvc/http/internal/PemSslContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ private static PrivateKey readPrivateKey(Path path) {
6666
try {
6767
return KeyFactory.getInstance("RSA").generatePrivate(spec);
6868
} catch (InvalidKeySpecException rsaFail) {
69-
throw new IllegalStateException("Failed to parse TLS private key from " + path, rsaFail);
69+
try {
70+
return KeyFactory.getInstance("EC").generatePrivate(spec);
71+
} catch (InvalidKeySpecException ecFail) {
72+
throw new IllegalStateException("Unsupported TLS private key algorithm in " + path, ecFail);
73+
} catch (GeneralSecurityException e) {
74+
throw new IllegalStateException("Failed to parse TLS private key from " + path, e);
75+
}
7076
} catch (GeneralSecurityException e) {
7177
throw new IllegalStateException("Failed to parse TLS private key from " + path, e);
7278
}

src/test/java/com/retailsvc/http/internal/PemSslContextTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class PemSslContextTest {
1010

1111
private static final Path RSA_CERT = Path.of("src/test/resources/tls/rsa-cert.pem");
1212
private static final Path RSA_KEY = Path.of("src/test/resources/tls/rsa-key.pem");
13+
private static final Path EC_CERT = Path.of("src/test/resources/tls/ec-cert.pem");
14+
private static final Path EC_KEY = Path.of("src/test/resources/tls/ec-key.pem");
1315

1416
@Test
1517
void loadsRsaPemPair() throws Exception {
@@ -19,4 +21,12 @@ void loadsRsaPemPair() throws Exception {
1921
assertThat(context.getProtocol()).isEqualTo("TLS");
2022
assertThat(context.getServerSocketFactory()).isNotNull();
2123
}
24+
25+
@Test
26+
void loadsEcPemPair() throws Exception {
27+
SSLContext context = PemSslContext.load(EC_CERT, EC_KEY);
28+
29+
assertThat(context).isNotNull();
30+
assertThat(context.getServerSocketFactory()).isNotNull();
31+
}
2232
}

0 commit comments

Comments
 (0)