Repository navigation
Conversation
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.
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.
Binary size
|
|
At the very least, the logic for parsing and decrypting encrypted PKCS#8 structures should be decoupled from As I understand it, the primary purpose of Functionality such as PBES2, PBKDF2, AES-CBC, and PEM parsing is independent of the TLS backend itself. Implementing these directly within It would be preferable to implement these features in separate crates under the In the future, these crates might be used by |
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.
|
Thanks, done. PEM reading, PBES2 (PBKDF2 and AES-CBC) and |
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. |
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.
|
@nabetti1720 No need to apologize, and it is now one crate, |
|
@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 :) |
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod fixtures { |
There was a problem hiding this comment.
we tend to call call this test and not fixtures
| prf: Option<AlgorithmIdentifier<'a>>, | ||
| } | ||
|
|
||
| mod oid { |
There was a problem hiding this comment.
i think these should be in const oid crate as we use everywhere else
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.
|
@richarddd Thanks, reworked from the ground up: the change is now +292/−43, down from 1800+.
The description lists the supported encryptions and the errors. |
richarddd
left a comment
There was a problem hiding this comment.
Small comments, then we're good
| format!("Error: {ENCRYPTED_KEY_ERROR}") | ||
| format!("Error: {UNSUPPORTED_KEY_ERROR}") | ||
| }; | ||
| assert_eq!(result, [expected.clone(), expected]); | ||
| assert_eq!(result, [hello, traditional]); | ||
| }) | ||
| }) |
There was a problem hiding this comment.
i think w format! slipped through here, can you fix?
There was a problem hiding this comment.
Done, along with the other two in this PR's test changes.
| let decrypt = || -> pkcs8::Result<SecretDocument> { | ||
| let key = | ||
| EncryptedPrivateKeyInfoRef::try_from(der.secret_pkcs8_der())?.decrypt(passphrase)?; | ||
| PrivateKeyInfoRef::try_from(key.as_bytes())?; | ||
| Ok(key) | ||
| }; |
There was a problem hiding this comment.
we do we need this closure here?
There was a problem hiding this comment.
It let one map_err cover the three fallible steps. It's a plain function now.
Issue # (if available)
Part of #492 (step 2 of the plan in #1925)
Description of changes
TLDR: on the rustls backends,
https.Agentnow loads encrypted PKCS#8 keys withpassphrase. The RustCryptopkcs8crate decrypts them.llrt_tlsgetsrustls_key.rs, the rustls counterpart ofopenssl_key.rs. It unwraps theENCRYPTED PRIVATE KEYblock with rustls's PEM reader, decrypts it withpkcs8and checks that the result is a PKCS#8 key. rustls's reader skips that label, so a copy of the block is relabelledPRIVATE KEYfirst; 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 -aes256and Node'sKeyObject.export({ cipher, passphrase })write.Errors match the OpenSSL backend where Node has one:
ERR_OSSL_BAD_DECRYPT;ERR_OSSL_CRYPTO_INTERRUPTED_OR_CANCELLED;ERR_OSSL_UNSUPPORTED.Other encryptions, including traditional
Proc-TypePEM, 3DES and the MD5 and SHA-3 PRFs, throwInvalid key: unsupported encryption algorithm.The rustls
tls-*features pull inpkcs8with itssha1-insecurefeature (PBES2 decryption, including the HMAC-SHA1 PRF). The OpenSSL backend decrypts keys itself and doesn't use it.Cargo.lockgainspkcs5,scryptandsalsa20.INTERRUPTED_OR_CANCELLEDmoves fromopenssl_key.rstoerror.rs, so both backends share it.Tests:
llrt_tls:llrt_fetch: mTLS with the encrypted client key works on rustls, and the error expectations are updated for each backend.generate.sh keys.Checklist
tests/unitand/or in Rust for my feature if neededmake fixto format JS and apply Clippy auto fixesmake checktypes/directoryBy 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