Skip to content

feat(tls): load encrypted PKCS#8 keys on the rustls backends - #1953

Open
chessbyte wants to merge 12 commits into
awslabs:mainfrom
chessbyte:chessbyte/492-pr1a-ii-rustls-pkcs8
Open

chessbyte wants to merge 12 commits into
awslabs:mainfrom
chessbyte:chessbyte/492-pr1a-ii-rustls-pkcs8

Conversation

@chessbyte

@chessbyte chessbyte commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Issue # (if available)

Part of #492 (step 2 of the plan in #1925)

Description of changes

TLDR: on the rustls backends, https.Agent now loads encrypted PKCS#8 keys with passphrase. The RustCrypto pkcs8 crate decrypts them.

  • llrt_tls gets rustls_key.rs, the rustls counterpart of openssl_key.rs. It unwraps the ENCRYPTED PRIVATE KEY block with rustls's PEM reader, decrypts it with pkcs8 and checks that the result is a PKCS#8 key. rustls's reader skips that label, so a copy of the block is relabelled PRIVATE KEY first; this avoids a second PEM and base64 decoder in the binary.

  • Supported: PBES2 with PBKDF2 (HMAC-SHA1 or SHA-2) or scrypt, and AES-128/192/256-CBC. That is what openssl pkcs8 -topk8, openssl genpkey -aes256 and Node's KeyObject.export({ cipher, passphrase }) write.

  • Errors match the OpenSSL backend where Node has one:

    • a wrong or missing passphrase throws ERR_OSSL_BAD_DECRYPT;
    • a passphrase over 1024 bytes throws ERR_OSSL_CRYPTO_INTERRUPTED_OR_CANCELLED;
    • a malformed key throws ERR_OSSL_UNSUPPORTED.

    Other encryptions, including traditional Proc-Type PEM, 3DES and the MD5 and SHA-3 PRFs, throw Invalid key: unsupported encryption algorithm.

  • The rustls tls-* features pull in pkcs8 with its sha1-insecure feature (PBES2 decryption, including the HMAC-SHA1 PRF). The OpenSSL backend decrypts keys itself and doesn't use it. Cargo.lock gains pkcs5, scrypt and salsa20.

  • INTERRUPTED_OR_CANCELLED moves from openssl_key.rs to error.rs, so both backends share it.

Tests:

  • llrt_tls:
    • RSA, HMAC-SHA1 and scrypt keys decrypt;
    • wrong, missing and over-long passphrases throw the errors above;
    • unsupported encryptions and a malformed key are rejected.
  • llrt_fetch: mTLS with the encrypted client key works on rustls, and the error expectations are updated for each backend.
  • The new fixtures are a P-256 key and four encrypted copies of it, written by generate.sh keys.

Checklist

  • Created unit tests in tests/unit and/or in Rust for my feature if needed
  • Ran make fix to format JS and apply Clippy auto fixes
  • Made sure my code didn't add any additional warnings: make check
  • Added relevant type info in types/ directory
  • Updated documentation if needed (API.md/README.md/Other)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

🤖 Generated with Claude Code

Part of awslabs#492. On the rustls backends, https.Agent rejected every
encrypted private key. It now decrypts PKCS#8 keys encrypted with
PBES2, PBKDF2 and AES-CBC, and throws Node's errors for keys that Node
rejects.

- The first private-key PEM block is used, as OpenSSL's
  PEM_read_bio_PrivateKey does. Unencrypted keys take the existing path.
- PBKDF2 supports 12 PRF digests from OpenSSL's table (SHA-1, SHA-2,
  SHA-3 and MD5, under 14 OIDs) over AES-128/192/256-CBC, using crates
  already in the default build. They are optional dependencies enabled
  only by the rustls features.
- Errors carry Node's message, code, library and reason, without the
  "Failed to build TLS config." prefix: wrong, missing and
  over-1024-byte passphrases, PBES1 and PKCS#12 RC2/RC4, unknown
  schemes, KDFs, PRFs and ciphers, AES-GCM/CCM, malformed parameters,
  a wrong final block length and decrypted data that isn't DER.
- Encryptions Node supports and LLRT doesn't yet keep the prefix and
  name what is missing: "Invalid key: encrypted keys using <X> are not
  supported".
- INTERRUPTED_OR_CANCELLED moves to error.rs, shared by both backends.
- The PEM block reader copies the base64 body only for ENCRYPTED PRIVATE
  KEY blocks, the only ones that use it. Unencrypted keys no longer
  allocate it before going through from_pem_slice.
- not_supported reserves the message's exact length.
- Drop comments that repeat the code, in the decoder and in the tests.
@chessbyte
chessbyte marked this pull request as ready for review November 24, 2026 20:02
Windows runners check out the fixtures with CRLF line endings, so
splitting ec-encrypted-legacy.pem on "-----END ...-----\n" found one
block instead of three. Normalize the line endings in that test.
@chessbyte chessbyte closed this Nov 24, 2026
@chessbyte chessbyte reopened this Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Binary size

Binary Arch Base PR Δ Δ %
llrt (linux) x86_64 12,782,104 12,935,960 🔴 +153,856 +1.20%
llrt (lambda compressed) x86_64 5,764,160 5,813,568 🔴 +49,408 +0.86%
llrt (lambda) x86_64 16,298,008 16,447,512 🔴 +149,504 +0.92%
llrt (linux) aarch64 10,955,728 11,019,472 🔴 +63,744 +0.58%
llrt (lambda compressed) aarch64 5,425,608 5,456,352 🔴 +30,744 +0.57%
llrt (lambda) aarch64 14,467,280 14,531,024 🔴 +63,744 +0.44%

llrt (linux) = default Linux binary · llrt (lambda compressed) = packed lambda bootstrap · llrt (lambda) = uncompressed lambda/container binary

@nabetti1720

nabetti1720 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

At the very least, the logic for parsing and decrypting encrypted PKCS#8 structures should be decoupled from llrt_tls.

As I understand it, the primary purpose of llrt_tls is to provide a selection and abstraction layer for TLS backends—not to handle key format parsing or the implementation of cryptographic algorithms.

Functionality such as PBES2, PBKDF2, AES-CBC, and PEM parsing is independent of the TLS backend itself. Implementing these directly within llrt_tls increases cryptographic dependencies, bloats the binary size, and creates a tight coupling between key format support and the TLS abstraction layer.

It would be preferable to implement these features in separate crates under the libs directory, such as llrt_pbe. Furthermore, by placing these behind feature gates in llrt_tls, you can provide users with the flexibility to choose which ones to include.

In the future, these crates might be used by llrt_crypto. In that case, it could be worth considering further abstraction.

Review feedback on awslabs#1953: PEM parsing and PKCS#8/PBES2 decryption don't
belong in the TLS backend layer.

- libs/llrt_pem reads the first private-key PEM block (label, Proc-Type
  and DEK-Info headers, base64 body) as OpenSSL's PEM_read_bio_PrivateKey
  does.
- libs/llrt_pbe decrypts PBES2 (PBKDF2 with 12 digests, AES-CBC) and
  classifies the other PBE schemes, KDFs, PRFs and ciphers.
- libs/llrt_pkcs8 parses EncryptedPrivateKeyInfo, decrypts it through
  llrt_pbe and checks the result.
- The crates depend on no other LLRT crate and return their own error
  types. llrt_tls maps them to the same Node errors and messages.
- New llrt_tls feature `encrypted-keys`, on by default and forwarded
  through llrt_http, llrt_fetch, llrt_modules, llrt_core and llrt.
  Without it the rustls backends keep the previous "encrypted keys are
  not supported" error. The OpenSSL backend doesn't use it.
- CI's rustls test matrix, `make check` and `make test-rs` enable the
  feature; the docs list it.
@chessbyte

Copy link
Copy Markdown
Contributor Author

Thanks, done. PEM reading, PBES2 (PBKDF2 and AES-CBC) and EncryptedPrivateKeyInfo now live in libs/llrt_pem, libs/llrt_pbe and libs/llrt_pkcs8. They depend on no other LLRT crate and return their own error types, so llrt_crypto could reuse them later. llrt_tls only maps those errors to Node's and uses the crates behind a new encrypted-keys feature: it is on by default, and turning it off keeps the previous "encrypted keys are not supported" error (the OpenSSL backend doesn't need it).

@chessbyte
chessbyte marked this pull request as draft November 25, 2026 12:26
@nabetti1720

Copy link
Copy Markdown
Contributor

Thanks, done. PEM reading, PBES2 (PBKDF2 and AES-CBC) and EncryptedPrivateKeyInfo now live in libs/llrt_pem, libs/llrt_pbe and libs/llrt_pkcs8. They depend on no other LLRT crate and return their own error types, so llrt_crypto could reuse them later. llrt_tls only maps those errors to Node's and uses the crates behind a new encrypted-keys feature: it is on by default, and turning it off keeps the previous "encrypted keys are not supported" error (the OpenSSL backend doesn't need it).

The three crate names I mentioned are merely examples. I did not mean to imply that you must divide them into three; please accept my apologies.
Has splitting them into three made things overly complicated?

Review feedback on awslabs#1953.

- llrt_pem and llrt_pkcs8 merge into llrt_pbe as the pem and pkcs8
  modules, next to pbes2. One Error enum covers all three.
- llrt_pbe has a feature per PRF family (hmac-sha1, hmac-sha2, hmac-sha3,
  hmac-md5); a digest that is off isn't compiled in, and a key that needs
  it fails with Unimplemented, naming the digest.
- llrt_tls, llrt_http, llrt_fetch, llrt_modules, llrt_core and llrt get
  encrypted-keys-hmac-sha1, -sha2, -sha3 and -md5, each forwarded one
  level down; encrypted-keys groups the four and stays the default.
- The tests of the parsing, classification and decryption rules move
  into llrt_pbe, next to each module. llrt_tls keeps the error mapping,
  the passphrase limit, traditional PEM and incomplete blocks, and full
  key loads, and loses its dev-dependencies.
- CI's check job tests llrt_pbe and llrt_tls with only the SHA-2 PRFs.
  The build docs list the features.
@chessbyte

Copy link
Copy Markdown
Contributor Author

@nabetti1720 No need to apologize, and it is now one crate, llrt_pbe, with pem, pkcs8 and pbes2 modules. The gates are one feature per PBKDF2 digest family (encrypted-keys-hmac-sha1, -hmac-sha2, -hmac-sha3, -hmac-md5), and encrypted-keys enables all four by default.

@chessbyte
chessbyte marked this pull request as ready for review November 25, 2026 13:52
@richarddd

Copy link
Copy Markdown
Collaborator

@chessbyte thanks for this PR but i think this needs some serious rework, it shares a lot with other crypto support we have and i don't think it should be in its own package. it also doesn't follow the conventions we use here. also there is manual implementation of pem, oid and much more which we have crates and other usage for. rework from ground up so we can avoid this 1800+ lines of change, even though the feature is very useful :)

Comment thread libs/llrt_pbe/src/lib.rs Outdated
}

#[cfg(test)]
mod fixtures {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we tend to call call this test and not fixtures

Comment thread libs/llrt_pbe/src/pbes2.rs Outdated
prf: Option<AlgorithmIdentifier<'a>>,
}

mod oid {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i think these should be in const oid crate as we use everywhere else

@chessbyte
chessbyte marked this pull request as draft December 1, 2026 12:12
Replace the hand-written llrt_pbe crate with the RustCrypto pkcs8 crate
(pem, encryption and sha1-insecure features), called from a new
rustls_key.rs in llrt_tls.

It loads PBES2 keys with PBKDF2 (HMAC-SHA1 or SHA-2) or scrypt and
AES-CBC. A wrong passphrase throws ERR_OSSL_BAD_DECRYPT, a malformed key
ERR_OSSL_UNSUPPORTED, and other encryptions "Invalid key: unsupported
key encryption".

One encrypted-keys feature, on by default, replaces the per-PRF
features. The fixtures are cut to the keys the tests read.
Drop the encrypted-keys feature. Each rustls tls-* feature now pulls in
pkcs8, as it does rustls, so an OpenSSL-only llrt_tls doesn't compile it.
The crate manifests, CI, Makefile and build docs match main again, and
the feature-off code, test and error message are gone.
rustls's PEM reader skips ENCRYPTED PRIVATE KEY blocks, so pkcs8's `pem`
feature brought a second PEM and base64 decoder into the binary. Relabel
a copy of the block as PRIVATE KEY and let rustls's reader unwrap it;
pkcs8 then only parses and decrypts.
@chessbyte chessbyte closed this Dec 1, 2026
@chessbyte chessbyte reopened this Dec 1, 2026
@chessbyte chessbyte closed this Dec 1, 2026
@chessbyte chessbyte reopened this Dec 1, 2026
@chessbyte chessbyte closed this Dec 1, 2026
@chessbyte chessbyte reopened this Dec 1, 2026
@chessbyte chessbyte closed this Dec 2, 2026
@chessbyte chessbyte reopened this Dec 2, 2026
@chessbyte
chessbyte marked this pull request as ready for review December 2, 2026 17:28
@chessbyte

Copy link
Copy Markdown
Contributor Author

@richarddd Thanks, reworked from the ground up: the change is now +292/−43, down from 1800+.

  • llrt_pbe is gone and nothing lives in its own package. The decryption is one small file in llrt_tls, rustls_key.rs, built on the RustCrypto pkcs8 crate and rustls's PEM reader.
  • No hand-written PEM, OID, ASN.1 or PBES2 code is left, so both inline comments (fixtures, const-oid) are on code that no longer exists.
  • The test keys sit with the existing ones in llrt_test_tls.

The description lists the supported encryptions and the errors.

@richarddd richarddd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Small comments, then we're good

Comment on lines -1882 to 1886
format!("Error: {ENCRYPTED_KEY_ERROR}")
format!("Error: {UNSUPPORTED_KEY_ERROR}")
};
assert_eq!(result, [expected.clone(), expected]);
assert_eq!(result, [hello, traditional]);
})
})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i think w format! slipped through here, can you fix?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, along with the other two in this PR's test changes.

Comment thread modules/llrt_tls/src/rustls_key.rs Outdated
Comment on lines +32 to +37
let decrypt = || -> pkcs8::Result<SecretDocument> {
let key =
EncryptedPrivateKeyInfoRef::try_from(der.secret_pkcs8_der())?.decrypt(passphrase)?;
PrivateKeyInfoRef::try_from(key.as_bytes())?;
Ok(key)
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we do we need this closure here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It let one map_err cover the three fallible steps. It's a plain function now.

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.

3 participants