Skip to content

x509-cert: document builder mistakes #1777

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
153 changes: 153 additions & 0 deletions x509-cert/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,42 @@ pub trait Builder: Sized {
S::VerifyingKey: EncodePublicKey;

/// Run the object through the signer and build it.
///
/// # Notes
///
/// When using ECDSA signers, the `Signature` parameter will need to be explicit
/// as multiple implementation of [`signature::Signer`] with various signature
/// are available.
///
/// This would look like:
#[cfg_attr(feature = "std", doc = "```no_run")]
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
/// # use rand::rng;
/// # use std::{
/// # str::FromStr,
/// # time::Duration
/// # };
/// # use x509_cert::{
/// # builder::{self, CertificateBuilder, Builder},
/// # name::Name,
/// # serial_number::SerialNumber,
/// # spki::SubjectPublicKeyInfo,
/// # time::Validity
/// # };
/// #
/// # let mut rng = rng();
/// # let signer = p256::ecdsa::SigningKey::random(&mut rng);
/// # let builder = CertificateBuilder::new(
/// # builder::profile::cabf::Root::new(
/// # false,
/// # Name::from_str("CN=World domination corporation").unwrap()
/// # ).unwrap(),
/// # SerialNumber::from(42u32),
/// # Validity::from_now(Duration::new(5, 0)).unwrap(),
/// # SubjectPublicKeyInfo::from_key(signer.verifying_key()).unwrap()
/// # ).unwrap();
/// let certificate = builder.build::<_, ecdsa::der::Signature<_>>(&signer).unwrap();
/// ```
fn build<S, Signature>(mut self, signer: &S) -> Result<Self::Output>
where
S: Signer<Signature>,
Expand All @@ -258,6 +294,45 @@ pub trait Builder: Sized {
}

/// Run the object through the signer and build it.
///
/// # Notes
///
/// When using ECDSA signers, the `Signature` parameter will need to be explicit
/// as multiple implementation of [`signature::Signer`] with various signature
/// are available.
///
/// This would look like:
#[cfg_attr(feature = "std", doc = "```no_run")]
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
/// # use rand::rng;
/// # use std::{
/// # str::FromStr,
/// # time::Duration
/// # };
/// # use x509_cert::{
/// # builder::{self, CertificateBuilder, Builder},
/// # name::Name,
/// # serial_number::SerialNumber,
/// # spki::SubjectPublicKeyInfo,
/// # time::Validity
/// # };
/// #
/// # let mut rng = rng();
/// # let signer = p256::ecdsa::SigningKey::random(&mut rng);
/// # let builder = CertificateBuilder::new(
/// # builder::profile::cabf::Root::new(
/// # false,
/// # Name::from_str("CN=World domination corporation").unwrap()
/// # ).unwrap(),
/// # SerialNumber::from(42u32),
/// # Validity::from_now(Duration::new(5, 0)).unwrap(),
/// # SubjectPublicKeyInfo::from_key(signer.verifying_key()).unwrap()
/// # ).unwrap();
/// let certificate = builder.build_with_rng::<_, ecdsa::der::Signature<_>, _>(
/// &signer,
/// &mut rng
/// ).unwrap();
/// ```
fn build_with_rng<S, Signature, R>(mut self, signer: &S, rng: &mut R) -> Result<Self::Output>
where
S: RandomizedSigner<Signature>,
Expand Down Expand Up @@ -351,6 +426,45 @@ pub trait AsyncBuilder: Sized {
S::VerifyingKey: EncodePublicKey;

/// Run the object through the signer and build it.
///
/// # Notes
///
/// When using ECDSA signers, the `Signature` parameter will need to be explicit
/// as multiple implementation of [`signature::AsyncSigner`] with various signature
/// are available.
///
/// This would look like:
#[cfg_attr(feature = "std", doc = "```no_run")]
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
/// # use rand::rng;
/// # use std::{
/// # str::FromStr,
/// # time::Duration
/// # };
/// # use x509_cert::{
/// # builder::{self, CertificateBuilder, AsyncBuilder},
/// # name::Name,
/// # serial_number::SerialNumber,
/// # spki::SubjectPublicKeyInfo,
/// # time::Validity
/// # };
/// #
/// # async fn build() -> builder::Result<()> {
/// # let mut rng = rng();
/// # let signer = p256::ecdsa::SigningKey::random(&mut rng);
/// # let builder = CertificateBuilder::new(
/// # builder::profile::cabf::Root::new(
/// # false,
/// # Name::from_str("CN=World domination corporation").unwrap()
/// # ).unwrap(),
/// # SerialNumber::from(42u32),
/// # Validity::from_now(Duration::new(5, 0)).unwrap(),
/// # SubjectPublicKeyInfo::from_key(signer.verifying_key()).unwrap()
/// # ).unwrap();
/// let certificate = builder.build_async::<_, ecdsa::der::Signature<_>>(&signer).await?;
/// # Ok(())
/// # }
/// ```
async fn build_async<S, Signature>(mut self, signer: &S) -> Result<Self::Output>
where
S: AsyncSigner<Signature>,
Expand All @@ -366,6 +480,45 @@ pub trait AsyncBuilder: Sized {
}

/// Run the object through the signer and build it.
///
/// # Notes
///
/// When using ECDSA signers, the `Signature` parameter will need to be explicit
/// as multiple implementation of [`signature::AsyncSigner`] with various signature
/// are available.
///
/// This would look like:
#[cfg_attr(feature = "std", doc = "```no_run")]
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
/// # use rand::rng;
/// # use std::{
/// # str::FromStr,
/// # time::Duration
/// # };
/// # use x509_cert::{
/// # builder::{self, CertificateBuilder, AsyncBuilder},
/// # name::Name,
/// # serial_number::SerialNumber,
/// # spki::SubjectPublicKeyInfo,
/// # time::Validity
/// # };
/// #
/// # async fn build() -> builder::Result<()> {
/// # let mut rng = rng();
/// # let signer = p256::ecdsa::SigningKey::random(&mut rng);
/// # let builder = CertificateBuilder::new(
/// # builder::profile::cabf::Root::new(
/// # false,
/// # Name::from_str("CN=World domination corporation").unwrap()
/// # ).unwrap(),
/// # SerialNumber::from(42u32),
/// # Validity::from_now(Duration::new(5, 0)).unwrap(),
/// # SubjectPublicKeyInfo::from_key(signer.verifying_key()).unwrap()
/// # ).unwrap();
/// let certificate = builder.build_with_rng_async::<_, ecdsa::der::Signature<_>, _>(&signer, &mut rng).await?;
/// # Ok(())
/// # }
/// ```
async fn build_with_rng_async<S, Signature, R>(
mut self,
signer: &S,
Expand Down