Skip to content

Commit c33d514

Browse files
committed
Add a CertifiedIssuer
1 parent 67b6995 commit c33d514

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

rcgen/src/lib.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ use std::net::IpAddr;
4141
use std::net::{Ipv4Addr, Ipv6Addr};
4242
use std::ops::Deref;
4343

44-
#[cfg(feature = "x509-parser")]
44+
#[cfg(feature = "pem")]
45+
use pem::Pem;
4546
use pki_types::CertificateDer;
4647
use time::{OffsetDateTime, Time};
4748
use yasna::models::ObjectIdentifier;
@@ -136,6 +137,55 @@ pub fn generate_simple_self_signed(
136137
Ok(CertifiedKey { cert, signing_key })
137138
}
138139

140+
/// An [`Issuer`] wrapper that also contains the issuer's [`Certificate`].
141+
pub struct CertifiedIssuer<'a, S> {
142+
certificate: Certificate,
143+
issuer: Issuer<'a, S>,
144+
}
145+
146+
impl<'a, S: SigningKey> CertifiedIssuer<'a, S> {
147+
/// Create a new issuer from the given parameters and key, with a self-signed certificate.
148+
pub fn self_signed(params: CertificateParams, signing_key: S) -> Result<Self, Error> {
149+
Ok(Self {
150+
certificate: params.self_signed(&signing_key)?,
151+
issuer: Issuer::new(params, signing_key),
152+
})
153+
}
154+
155+
/// Create a new issuer from the given parameters and key, signed by the given `issuer`.
156+
pub fn signed_by(
157+
params: CertificateParams,
158+
signing_key: S,
159+
issuer: &Issuer<'_, impl SigningKey>,
160+
) -> Result<Self, Error> {
161+
Ok(Self {
162+
certificate: params.signed_by(&signing_key, issuer)?,
163+
issuer: Issuer::new(params, signing_key),
164+
})
165+
}
166+
167+
/// Get the certificate in PEM encoded format.
168+
#[cfg(feature = "pem")]
169+
pub fn pem(&self) -> String {
170+
pem::encode_config(&Pem::new("CERTIFICATE", self.der().to_vec()), ENCODE_CONFIG)
171+
}
172+
173+
/// Get the certificate in DER encoded format.
174+
///
175+
/// See also [`Certificate::der()`]
176+
pub fn der(&self) -> &CertificateDer<'static> {
177+
self.certificate.der()
178+
}
179+
}
180+
181+
impl<'a, S> Deref for CertifiedIssuer<'a, S> {
182+
type Target = Issuer<'a, S>;
183+
184+
fn deref(&self) -> &Self::Target {
185+
&self.issuer
186+
}
187+
}
188+
139189
/// An issuer that can sign certificates.
140190
///
141191
/// Encapsulates the distinguished name, key identifier method, key usages and signing key

0 commit comments

Comments
 (0)