X509Builder

Struct X509Builder 

Source
pub struct X509Builder { /* private fields */ }
Expand description

Builder for a new X.509 certificate.

let mut name = X509NameOwned::new()?;
name.add_entry_by_txt(c"CN", b"example.com")?;

let cert = X509Builder::new()?
    .set_version(2)?                    // X.509v3
    .set_serial_number(1)?
    .set_not_before_offset(0)?          // valid from now
    .set_not_after_offset(365 * 86400)? // valid for 1 year
    .set_subject_name(&name)?
    .set_issuer_name(&name)?            // self-signed
    .set_public_key(&pub_key)?
    .sign(&priv_key, None)?             // None → no digest (Ed25519)
    .build();

Implementations§

Source§

impl X509Builder

Source

pub fn new() -> Result<Self, ErrorStack>

Allocate a new, empty X509 structure.

§Errors
Source

pub fn set_version(self, version: i64) -> Result<Self, ErrorStack>

Set the X.509 version (0 = v1, 1 = v2, 2 = v3).

§Errors
Source

pub fn set_serial_number(self, n: i64) -> Result<Self, ErrorStack>

Set the serial number.

§Errors
Source

pub fn set_not_before_offset(self, offset_secs: i64) -> Result<Self, ErrorStack>

Set notBefore to now + offset_secs.

§Errors
Source

pub fn set_not_after_offset(self, offset_secs: i64) -> Result<Self, ErrorStack>

Set notAfter to now + offset_secs.

§Errors
Source

pub fn set_subject_name(self, name: &X509NameOwned) -> Result<Self, ErrorStack>

Set the subject distinguished name.

§Errors
Source

pub fn set_issuer_name(self, name: &X509NameOwned) -> Result<Self, ErrorStack>

Set the issuer distinguished name.

§Errors
Source

pub fn set_public_key<T: HasPublic>( self, key: &Pkey<T>, ) -> Result<Self, ErrorStack>

Set the public key.

§Errors
Source

pub fn sign( self, key: &Pkey<Private>, digest: Option<&DigestAlg>, ) -> Result<Self, ErrorStack>

Sign the certificate.

Pass digest = None for one-shot algorithms such as Ed25519. For ECDSA or RSA, pass the appropriate digest (e.g. SHA-256).

§Errors
Source

pub fn build(self) -> X509

Finalise and return the certificate.

Trait Implementations§

Source§

impl Drop for X509Builder

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.