Skip to content

make CertificateParams::signed_by accept impl PublicKeyData rather than KeyPair for the key being signed #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

kwantam
Copy link
Contributor

@kwantam kwantam commented Aug 21, 2024

CertificateParams::signed_by does not need the secret corresponding to the public key being certified (though of course it needs the secret for the CA key signing the certificate!). More discussion about this in #282.

This PR:

  • changes the first arg of signed_by to impl PublicKeyData (which maintains compatibility because KeyPair implements this trait)
  • adds a new type, SubjectPublicKey, that can be parsed from a PEM or DER output by this crate (or, e.g., by openssl) and also impls PublicKeyData. This type is only available when the x509-parser feature is enabled (because that's what is required to parse).

I added a quick test that the SubjectPublicKey type's PublicKeyData impl produces the same output as KeyPair would for the same key, at least for the key types that can be generated using the crate. (So this means, no test right now for RSA keys, since those seemed to give an error when I tried to generate a keypair.)

@kwantam kwantam mentioned this pull request Aug 21, 2024
Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

Initial round of feedback.

@@ -36,7 +36,7 @@ x509-parser = { workspace = true, features = ["verify"], optional = true }
zeroize = { version = "1.2", optional = true }

[features]
default = ["crypto", "pem", "ring"]
default = ["crypto", "pem", "ring", "x509-parser"]
Copy link
Member

Choose a reason for hiding this comment

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

We'll want to revert this.

self,
key_pair: &KeyPair,
key_pair: &K,
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I'd prefer to spell this as &impl PublicKeyData.

@@ -57,6 +57,90 @@ impl fmt::Debug for KeyPairKind {
}
}

/// A public key
#[cfg(feature = "x509-parser")]
Copy link
Member

Choose a reason for hiding this comment

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

There's no need to guard the type directly.

}

/// Create a `SubjectPublicKey` value from DER-encoded SubjectPublicKeyInfo bytes
pub fn from_der(spki_der: &[u8]) -> Result<Self, Error> {
Copy link
Member

Choose a reason for hiding this comment

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

We should guard this constructor.

#[cfg(feature = "x509-parser")]
impl SubjectPublicKey {
/// Create a `SubjectPublicKey` value from a PEM-encoded SubjectPublicKeyInfo string
pub fn from_pem(pem_str: &str) -> Result<Self, Error> {
Copy link
Member

Choose a reason for hiding this comment

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

This needs the feature guards from from_der() and also another one for pem.

}
}

#[cfg(feature = "x509-parser")]
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need the guard.

@@ -689,7 +773,7 @@ impl<T> ExternalError<T> for Result<T, pem::PemError> {
}
}

pub(crate) trait PublicKeyData {
pub trait PublicKeyData {
Copy link
Member

Choose a reason for hiding this comment

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

Needs some tweaks, as discussed here, please put those in a separate commit.

#282 (comment)

Also needs documentation.

@@ -45,6 +45,9 @@ pub enum Error {
#[cfg(not(feature = "crypto"))]
/// Missing serial number
MissingSerialNumber,
/// X509 parsing error
#[cfg(feature = "x509-parser")]
X509,
Copy link
Member

Choose a reason for hiding this comment

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

I think we might want to include a String representation of the nested error here.

Copy link
Member

@est31 est31 left a comment

Choose a reason for hiding this comment

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

LGTM if the review comments are addressed

Copy link
Member

@cpu cpu left a comment

Choose a reason for hiding this comment

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

djc's feedback covered most bases, I just had a couple nits to consider.

Comment on lines +112 to +113
}
fn raw_bytes(&self) -> &[u8] {
Copy link
Member

Choose a reason for hiding this comment

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

nit: whitespace between fns

/// A public key
#[cfg(feature = "x509-parser")]
#[derive(Debug)]
pub struct SubjectPublicKey {
Copy link
Member

Choose a reason for hiding this comment

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

bike-shedding: I think this would be better called SubjectPublicKeyInfo. It's the pairing of an algorithm identifier and the public key.

@cpu
Copy link
Member

cpu commented Sep 16, 2024

@kwantam Are you interested in completing this pull-request by addressing the feedback received?

@djc
Copy link
Member

djc commented Sep 24, 2024

I've taken this branch and addressed the comments so far in #291.

@cpu
Copy link
Member

cpu commented Sep 24, 2024

Closing in favour of #291. Thanks for getting the ball rolling Kwantam

@cpu cpu closed this Sep 24, 2024
@kwantam
Copy link
Contributor Author

kwantam commented Sep 25, 2024

Thanks for the comments and @djc for getting this across the finish line! Apologies that I've been buried in other stuff...

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.

4 participants