Skip to content

Separate public key, signature and key pair algorithms - #459

Open
jgreeer wants to merge 6 commits into
rustls:mainfrom
jgreeer:add-public-key-algorithm
Open

jgreeer wants to merge 6 commits into
rustls:mainfrom
jgreeer:add-public-key-algorithm

Conversation

@jgreeer

@jgreeer jgreeer commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR splits SignatureAlgorithm into three separate types. It was being used to identify the key's SPKI algorithm, the signature algorithm, and the backend primitive for KeyPair all 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 on aws_lc_rs even for custom signers (#460). We also move closer to removing KeyPair for #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_P256
  • SignatureAlgorithm (sig_alg::*) - the signature algorithm without a curve, e.g. ECDSA_SHA384
  • KeyPairAlgorithm - pairs the two for generating and loading a KeyPair. The 12 top-level constants like ECDSA_P256_SHA256 are now this type

No DER output changes for anything that worked before.

Changes

sign_algo.rs

  • Added the PublicKeyAlgorithm struct and the key_alg module, which stores the static public key types. from_alg_id() moved here from SignatureAlgorithm
  • SignatureAlgorithm changes
    • Removed oids_sign_alg and sign_alg. It now only holds the signature OID and parameters, so it doesn't depend on ring/aws-lc-rs anymore
    • Equality and hashing compare the OID and parameters
    • Removed from_oid(). For ECDSA, some of the values had the same OIDs, so they were being shadowed. from_oid(ecdsa-with-SHA256) always returned ECDSA_P256_SHA256
    • Added the sig_alg module with the curve free signature algorithms
  • Added KeyPairAlgorithm. The P-521 and ML-DSA constants are no longer gated on aws_lc_rs, on ring they compile but generating or loading them returns KeyPairAlgorithmUnavailable
  • Added a private KeyFamily enum to PublicKeyAlgorithm and SignatureAlgorithm, so we can tell when a signature algorithm can't be used with a key

key_pair.rs

  • Added signing_alg(), a table that maps a KeyPairAlgorithm to the backend types. This replaces the if/else chains in the from_* loaders, so anything a backend doesn't support returns KeyPairAlgorithmUnavailable instead of panicking. As a side effect from_der_and_sign_algo can now load ECDSA_P521_SHA256 and ECDSA_P521_SHA384 keys, which used to panic
  • KeyPair APIs (generate_for, generate_rsa_for, from_*_and_sign_algo, is_compatible, compatible_algs) take a KeyPairAlgorithm
  • PublicKeyData::algorithm() is renamed to key_algorithm() and now returns a PublicKeyAlgorithm. The signature algorithm lives in the SigningKey trait as algorithm(). The names have to differ because SigningKey: PublicKeyData, so a shared algorithm() is ambiguous at the call site.
  • KeyPair::algorithm() now returns the curve free signature algorithm, e.g. sig_alg::ECDSA_SHA256
  • sign_der() checks that the signature algorithm and the key algorithm are in the same KeyFamily, and returns SignatureAlgorithmMismatch if not. This covers certs, CSRs, and CRLs
  • SubjectPublicKeyInfo now stores a PublicKeyAlgorithm and derives Hash
  • Added tests for the mismatch check, for KeyPairAlgorithmUnavailable on ring, and a test that the from_*_and_sign_algo loaders load each algorithm's key back correctly

csr.rs

  • Removed PublicKey, CertificateSigningRequestParams::public_key is now a SubjectPublicKeyInfo
  • In from_der(), we read the public key algorithm from the SPKI's AlgorithmIdentifier instead of from the csr's signature algorithm
  • Added a supports_oid() check before signature verification, so CSRs signed with SHA-1 or RSA-PSS still return UnsupportedSignatureAlgorithm
  • We now read the SPKI's algorithm parameters, which the old code ignored. A nonconforming RSA key with absent parameters instead of the required NULL is now rejected.

crl.rs

  • changed the doc example for the new trait shape

error.rs

  • New UnsupportedPublicKeyAlgorithm, KeyPairAlgorithmUnavailable, and SignatureAlgorithmMismatch variants

rustls-cert-gen/src/cert.rs

  • From<KeyPairAlgorithm> now converts to rcgen::KeyPairAlgorithm, and the test uses is_compatible()

lib.rs

  • exports PublicKeyAlgorithm, KeyPairAlgorithm, key_alg and sig_alg; algo_hash_agrees_with_eq covers the new types

verify-tests

Fixes #448
Fixes #460

@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch from dc2ce63 to 957b1be Compare September 24, 2026 17:45

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread rcgen/src/key_pair.rs Resolved
Comment thread rcgen/src/key_pair.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch from 957b1be to 8d48370 Compare September 27, 2026 23:02
@jgreeer

jgreeer commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

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.

sure thing, I split the changes into 4 commits. let me know if we should split these changes further or not

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is looking much better, but I'd like to get a second opinion from @cpu!

Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/lib.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rustls-cert-gen/src/cert.rs Resolved
@cpu

cpu commented Sep 28, 2026

Copy link
Copy Markdown
Member

I'd like to get a second opinion from @cpu!

I'll be tied up at a conference for the next few days but this is on my list.

@jgreeer

jgreeer commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

I'd like to get a second opinion from @cpu!

I'll be tied up at a conference for the next few days but this is on my list.

@cpu bumping so this is on your radar 😄

@cpu

cpu commented Oct 12, 2026

Copy link
Copy Markdown
Member

@cpu bumping so this is on your radar 😄

Thanks for the ping! I have to admit I forgot about this one 😅 I'll take a look shortly.

@cpu cpu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/lib.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/sign_algo.rs Outdated
.ok_or(Error::UnsupportedSignatureAlgorithm)
}

/// The algorithm of a public key that produces signatures with this algorithm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: might read a little clearer as something like:

The public key algorithm associated with public keys used for this signature algorithm.

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.

tried to fix, but i think this depdends on what we do with signature algorithm

Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/csr.rs Resolved
Comment thread rcgen/src/csr.rs Outdated
let info = &csr.certification_request_info;

let public_key = PublicKey {
raw: info.subject_pki.subject_public_key.data.to_vec(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@jgreeer jgreeer Oct 13, 2026 •

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.

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.

Comment thread verify-tests/tests/openssl.rs Resolved
@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch from 8d48370 to 65023da Compare October 12, 2026 19:05
@jgreeer

jgreeer commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

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?

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. Certificate.signatureAlgorithm and tbsCertificate.signature are both ecdsa-with-SHA384, no curve. I assume this is what we want to take away from signingParamsForKey because they also allow you to decide the signatureAlgorithm at sign time, which is not something we want to do if i understand correctly.

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 generate. The scope of this PR would increase a bit. Am I understanding this correctly or do you have a smaller change in mind? Thanks!

@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch 2 times, most recently from 98a9bfb to cee48f3 Compare October 13, 2026 18:11
@cpu

cpu commented Oct 28, 2026

Copy link
Copy Markdown
Member

Sorry about the lag 😓

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 generate. The scope of this PR would increase a bit. Am I understanding this correctly or do you have a smaller change in mind? Thanks!

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.

@jgreeer

jgreeer commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Summary of changes:

  • Removed key_alg and sign_alg fields from SignatureAlgorithm
    • key_alg: decoupling keyalg from sigalg so we don't have to infer a curve when reading a sigalg
    • sign_alg: it held ring/aws-lc-rs types; removing it makes SignatureAlgorithm independent of the backend, so the types can be ungated and reused by future backends
  • Created a new public sig_alg module that has the curve free signature algorithms
  • Added signing_alg() as a translation layer in key_pair.rs to the specific backend types; anything a backend doesn't support (previously a panic! in the from_* loaders) now returns UnsupportedSignatureAlgorithm
  • Created a new struct: SigningKeyAlgorithm, which is a pairing of a SignatureAlgorithm and a PublicKeyAlgorithm.
    • This struct essentially takes the place of SignatureAlgorithm in key_pair.rs, most importantly the 12 top-level constants are now this type.
    • Ungated algo::ECDSA_P521_* and algo::ML_DSA_* so that future crypto backends can use these types
      • On ring you can now use them as generation arguments, but the translation layer will fail it with UnsupportedSignatureAlgorithm
  • Since SignatureAlgorithm changed KeyPair::algorithm() now returns the curve-free signature algorithm
  • Removed PublicKey from csr.rs since we have SubjectPublicKeyInfo

Tests:
- Added an openssl test test_openssl_p256_key_signing_with_sha384 in openssl.rs for the situation @cpu described
- Added an unsupported algorithm test for ring

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 SignatureAlgorithm. I've left it this way for now so it's easier to review, but i can squash them into a smaller amount of commits once reviewed.

@djc

djc commented Nov 4, 2026

Copy link
Copy Markdown
Member

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 SignatureAlgorithm. I've left it this way for now so it's easier to review, but i can squash them into a smaller amount of commits once reviewed.

Suggest to squash them now, since that will be easier to review.

@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch from fcc9de6 to 7b1cd91 Compare November 4, 2026 15:47
@jgreeer

jgreeer commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

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 SignatureAlgorithm. I've left it this way for now so it's easier to review, but i can squash them into a smaller amount of commits once reviewed.

Suggest to squash them now, since that will be easier to review.

Sure, squashed it back to 5 commits.

@cpu cpu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread rcgen/src/key_pair.rs Resolved
Comment thread rcgen/src/csr.rs Resolved
Comment thread rcgen/src/key_pair.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/sign_algo.rs Resolved
Comment thread rcgen/src/key_pair.rs Resolved
@cpu

cpu commented Nov 14, 2026

Copy link
Copy Markdown
Member

(Also might be nice to update the PR desc when you have a chance)

@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch from 7b1cd91 to 1d1e0e2 Compare November 16, 2026 19:01
@jgreeer jgreeer changed the title add public key algorithm Separate public key, signature and key pair algorithms Nov 16, 2026
@jgreeer

jgreeer commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

(Also might be nice to update the PR desc when you have a chance)

Updated

@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch from 1d1e0e2 to 2c72756 Compare November 16, 2026 21:36
@jgreeer
jgreeer force-pushed the add-public-key-algorithm branch from 2c72756 to 6f6ce24 Compare November 16, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants