Repository navigation
Conversation
dc2ce63 to
957b1be
Compare
djc
left a comment
There was a problem hiding this comment.
I think there's probably too much going on in the single commit here.
Can it be split into two or three commits? Maybe start by introducing the new algorithm type and replacing only one (the simplest) usage site, then do one commit per additional usage site?
@jgreeer do you think you can figure that out? There's a little more guidance on what we typically look for here.
957b1be to
8d48370
Compare
sure thing, I split the changes into 4 commits. let me know if we should split these changes further or not |
I'll be tied up at a conference for the next few days but this is on my list. |
Thanks for the ping! I have to admit I forgot about this one 😅 I'll take a look shortly. |
cpu
left a comment
There was a problem hiding this comment.
Here's a first pass of review. Sorry for the delay!
I think this is 90% of the way where we want, but I think there might still be something to pull at w.r.t the SignatureAlgorithm and SigningKey trait and whether it's possible to represent a valid case like a P-256 key signing messages using ECDSA/SHA-384 like the new openssl.rs test. In that case the signature algorithm identifier is ecdsca-with-SHA384 and names no specific curve, and the public key algorithm is ECDSA_P256.
If I were to implement a SigningKey for that scenario, I can correctly return key_alg::ECDSA_P256 from key_algorithm() but I think I'm stuck for an ECDSA_SHA384 signature constant. I'd have to use ECDSA_P384_SHA384 and that's claiming a curve that doesn't match the public key alg. It feels like we're still conflating the two.
I think we need to be looking at something more akin to the internal x509 logic in Go's signingParamsForKey? Does that make sense?
| .ok_or(Error::UnsupportedSignatureAlgorithm) | ||
| } | ||
|
|
||
| /// The algorithm of a public key that produces signatures with this algorithm |
There was a problem hiding this comment.
nit: might read a little clearer as something like:
The public key algorithm associated with public keys used for this signature algorithm.
There was a problem hiding this comment.
tried to fix, but i think this depdends on what we do with signature algorithm
| let info = &csr.certification_request_info; | ||
|
|
||
| let public_key = PublicKey { | ||
| raw: info.subject_pki.subject_public_key.data.to_vec(), |
There was a problem hiding this comment.
I think the way this worked previously we called from_oid(), then searched this algorithm list for a match, and returned UnsupportedSignatureAlgorithm for RSA-PSS after I removed them in f8ef6ee
We probably want to continue to reject RSA-PSS on this codepath right?
There was a problem hiding this comment.
yep, good catch. worth noting since csr.verify_signature() was called before from_oid the error you'd get was InvalidCertificationRequestSignature instead of UnsupportedSignatureAlgorithm because RSA-PSS is not supported by verify_signature(). But since sha1 was supported by verify_signature() it would fall through to from_oid and give the correct error. I added supports_oid() to SignatureAlgorithm which happens before signature verifcation, so now both get the same error.
8d48370 to
65023da
Compare
Hey, thanks for the review! That's a good point, should we drop the curve from SignatureAlgorithm then? That would align the struct closer to go and what is actually written into the cert. I think the problem is that SignatureAlgorithim is overloaded, since it's also used as the argument to generate a key. You'd probably have to make a new struct that serves as an argument to |
98a9bfb to
cee48f3
Compare
|
Sorry about the lag 😓
Yeah, exactly. I think we want to break that overloading. Increasing the scope of the PR to achieve that seems reasonable to me. I'd rather do this properly in one go vs continue to try and mix concepts up across pub key identification, key generation, and signature alg identification/encoding. |
|
Summary of changes:
Tests: I've added five commits on top of the existing 6 to describe these changes, some of them undo work from the previous commits (like removing key_alg from |
Suggest to squash them now, since that will be easier to review. |
fcc9de6 to
7b1cd91
Compare
Sure, squashed it back to 5 commits. |
cpu
left a comment
There was a problem hiding this comment.
Thanks again for the continued iteration. I think the old API was in pretty rough shape in this department and it's a tricky problem space but I'm happy with where we're getting to. I had a bit more feedback but I think (🤞) it's the last structural work needed.
|
(Also might be nice to update the PR desc when you have a chance) |
7b1cd91 to
1d1e0e2
Compare
Updated |
1d1e0e2 to
2c72756
Compare
2c72756 to
6f6ce24
Compare
Summary
This PR splits
SignatureAlgorithminto three separate types. It was being used to identify the key's SPKI algorithm, the signature algorithm, and the backend primitive forKeyPairall at once. That led to us inferring a key's curve from its signature algorithm when parsing CSRs (#448), made valid combinations like a P-256 key signing with ecdsa-with-SHA384 impossible to represent, and gated the P-521 and ML-DSA identifiers onaws_lc_rseven for custom signers (#460). We also move closer to removingKeyPairfor #450, since key generation now has its own type. This change is inspired by the crypto/x509 package in go.PublicKeyAlgorithm(key_alg::*) - the SPKI algorithm, e.g.ECDSA_P256SignatureAlgorithm(sig_alg::*) - the signature algorithm without a curve, e.g.ECDSA_SHA384KeyPairAlgorithm- pairs the two for generating and loading aKeyPair. The 12 top-level constants likeECDSA_P256_SHA256are now this typeNo DER output changes for anything that worked before.
Changes
sign_algo.rsPublicKeyAlgorithmstruct and thekey_algmodule, which stores the static public key types.from_alg_id()moved here fromSignatureAlgorithmSignatureAlgorithmchangesoids_sign_algandsign_alg. It now only holds the signature OID and parameters, so it doesn't depend on ring/aws-lc-rs anymorefrom_oid(). For ECDSA, some of the values had the same OIDs, so they were being shadowed.from_oid(ecdsa-with-SHA256)always returnedECDSA_P256_SHA256sig_algmodule with the curve free signature algorithmsKeyPairAlgorithm. The P-521 and ML-DSA constants are no longer gated onaws_lc_rs, on ring they compile but generating or loading them returnsKeyPairAlgorithmUnavailableKeyFamilyenum toPublicKeyAlgorithmandSignatureAlgorithm, so we can tell when a signature algorithm can't be used with a keykey_pair.rssigning_alg(), a table that maps aKeyPairAlgorithmto the backend types. This replaces the if/else chains in thefrom_*loaders, so anything a backend doesn't support returnsKeyPairAlgorithmUnavailableinstead of panicking. As a side effectfrom_der_and_sign_algocan now loadECDSA_P521_SHA256andECDSA_P521_SHA384keys, which used to panicKeyPairAPIs (generate_for,generate_rsa_for,from_*_and_sign_algo,is_compatible,compatible_algs) take aKeyPairAlgorithmPublicKeyData::algorithm()is renamed tokey_algorithm()and now returns aPublicKeyAlgorithm. The signature algorithm lives in theSigningKeytrait asalgorithm(). The names have to differ becauseSigningKey: PublicKeyData, so a sharedalgorithm()is ambiguous at the call site.KeyPair::algorithm()now returns the curve free signature algorithm, e.g.sig_alg::ECDSA_SHA256sign_der()checks that the signature algorithm and the key algorithm are in the sameKeyFamily, and returnsSignatureAlgorithmMismatchif not. This covers certs, CSRs, and CRLsSubjectPublicKeyInfonow stores aPublicKeyAlgorithmand derivesHashKeyPairAlgorithmUnavailableon ring, and a test that thefrom_*_and_sign_algoloaders load each algorithm's key back correctlycsr.rsPublicKey,CertificateSigningRequestParams::public_keyis now aSubjectPublicKeyInfofrom_der(), we read the public key algorithm from the SPKI's AlgorithmIdentifier instead of from the csr's signature algorithmsupports_oid()check before signature verification, so CSRs signed with SHA-1 or RSA-PSS still returnUnsupportedSignatureAlgorithmNULLis now rejected.crl.rserror.rsUnsupportedPublicKeyAlgorithm,KeyPairAlgorithmUnavailable, andSignatureAlgorithmMismatchvariantsrustls-cert-gen/src/cert.rsFrom<KeyPairAlgorithm>now converts torcgen::KeyPairAlgorithm, and the test usesis_compatible()lib.rsPublicKeyAlgorithm,KeyPairAlgorithm,key_algandsig_alg;algo_hash_agrees_with_eqcovers the new typesverify-testsopenssl.rs- Added a test for CertificateSigningRequestParams::from_der can parse the wrong key type #448, and a test for a P-256 key signing with ecdsa-with-SHA384generic.rs- Added a test that CSRs signed with SHA-1 or RSA-PSS are rejectedwebpki.rs- updated forPublicKeyAlgorithmandKeyPairAlgorithmFixes #448
Fixes #460