Skip to content

Add a CertifiedIssuer #363

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

Merged
merged 1 commit into from
Jul 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions rcgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ use std::net::IpAddr;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::ops::Deref;

#[cfg(feature = "x509-parser")]
#[cfg(feature = "pem")]
use pem::Pem;
use pki_types::CertificateDer;
use time::{OffsetDateTime, Time};
use yasna::models::ObjectIdentifier;
Expand Down Expand Up @@ -136,6 +137,56 @@ pub fn generate_simple_self_signed(
Ok(CertifiedKey { cert, signing_key })
}

/// An [`Issuer`] wrapper that also contains the issuer's [`Certificate`].
#[derive(Debug)]
pub struct CertifiedIssuer<'a, S> {
certificate: Certificate,
issuer: Issuer<'a, S>,
}

impl<'a, S: SigningKey> CertifiedIssuer<'a, S> {
/// Create a new issuer from the given parameters and key, with a self-signed certificate.
pub fn self_signed(params: CertificateParams, signing_key: S) -> Result<Self, Error> {
Ok(Self {
certificate: params.self_signed(&signing_key)?,
issuer: Issuer::new(params, signing_key),
})
}

/// Create a new issuer from the given parameters and key, signed by the given `issuer`.
pub fn signed_by(
params: CertificateParams,
signing_key: S,
issuer: &Issuer<'_, impl SigningKey>,
) -> Result<Self, Error> {
Ok(Self {
certificate: params.signed_by(&signing_key, issuer)?,
issuer: Issuer::new(params, signing_key),
})
}

/// Get the certificate in PEM encoded format.
#[cfg(feature = "pem")]
pub fn pem(&self) -> String {
pem::encode_config(&Pem::new("CERTIFICATE", self.der().to_vec()), ENCODE_CONFIG)
}

/// Get the certificate in DER encoded format.
///
/// See also [`Certificate::der()`]
pub fn der(&self) -> &CertificateDer<'static> {
self.certificate.der()
}
}

impl<'a, S> Deref for CertifiedIssuer<'a, S> {
type Target = Issuer<'a, S>;

fn deref(&self) -> &Self::Target {
&self.issuer
}
}

/// An issuer that can sign certificates.
///
/// Encapsulates the distinguished name, key identifier method, key usages and signing key
Expand Down Expand Up @@ -210,7 +261,7 @@ impl<'a, S: SigningKey> Issuer<'a, S> {
}
}

impl<'a, S: SigningKey> fmt::Debug for Issuer<'a, S> {
impl<'a, S> fmt::Debug for Issuer<'a, S> {
/// Formats the issuer information without revealing the key pair.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// The key pair is omitted from the debug output as it contains secret information.
Expand Down