X509

Struct X509 

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

An X.509 certificate (X509*).

Cloneable via EVP_X509_up_ref; wrapping in Arc<X509> is safe.

Implementations§

Source§

impl X509

Source

pub fn from_pem(pem: &[u8]) -> Result<Self, ErrorStack>

Load a certificate from PEM bytes.

§Errors
Source

pub fn from_pem_in(_ctx: &Arc<LibCtx>, pem: &[u8]) -> Result<Self, ErrorStack>

Load a certificate from PEM bytes, accepting a library context for API symmetry with other from_pem_in methods.

OpenSSL 3.5 does not expose a libctx-aware PEM_read_bio_X509_ex variant, so this calls the standard PEM_read_bio_X509. The ctx parameter is accepted but unused. Certificate parsing itself does not require provider dispatch; provider-bound operations use the context stored in the key extracted from the certificate.

§Errors
Source

pub fn new_in(ctx: &Arc<LibCtx>) -> Result<Self, ErrorStack>

Create a new, empty X509 object bound to the given library context.

Use this instead of the implicit-context X509Builder::new when you need the certificate to be associated with a FIPS-isolated or otherwise explicit LibCtx. The propq (property query) argument is NULL, meaning default provider properties are used.

§Errors

Returns Err if OpenSSL cannot allocate the certificate structure.

Source

pub fn from_der(der: &[u8]) -> Result<Self, ErrorStack>

Load a certificate from DER bytes.

Zero-copy: parses from the caller’s slice without an intermediate buffer.

§Errors

Returns Err if the DER is malformed, or if der.len() exceeds i64::MAX.

Source

pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack>

Serialise to PEM.

§Errors
Source

pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack>

Serialise to DER.

Zero-copy: writes into a freshly allocated Vec<u8> without going through an OpenSSL-owned buffer.

§Errors
Source

pub fn subject_name(&self) -> X509Name<'_>

Subject distinguished name (borrowed).

Source

pub fn issuer_name(&self) -> X509Name<'_>

Issuer distinguished name (borrowed).

Source

pub fn serial_number(&self) -> Option<i64>

Serial number as a signed 64-bit integer.

Returns None if the serial number is too large to fit in i64.

Source

pub fn serial_number_bytes(&self) -> Option<Vec<u8>>

Serial number as a big-endian byte slice.

Complementary to Self::serial_number for serials that exceed i64::MAX. The bytes are the raw content octets of the DER INTEGER, not including the tag or length.

Returns None if the serial field is absent.

Source

pub fn not_before_str(&self) -> Option<String>

Validity notBefore as a human-readable UTC string.

The format is "Mmm DD HH:MM:SS YYYY GMT" (OpenSSL default). Returns None if the field is absent or cannot be printed.

Source

pub fn not_after_str(&self) -> Option<String>

Validity notAfter as a human-readable UTC string.

Returns None if the field is absent or cannot be printed.

Source

pub fn not_before_tm(&self) -> Option<BrokenDownTime>

Validity notBefore as a structured BrokenDownTime in UTC.

Returns None if the field is absent or cannot be parsed.

Source

pub fn not_after_tm(&self) -> Option<BrokenDownTime>

Validity notAfter as a structured BrokenDownTime in UTC.

Returns None if the field is absent or cannot be parsed.

Source

pub fn is_valid_now(&self) -> bool

Returns true if the current time is within [notBefore, notAfter].

Source

pub fn public_key(&self) -> Result<Pkey<Public>, ErrorStack>

Extract the public key (owned Pkey<Public>).

Calls X509_get_pubkey — the returned key is independently reference-counted.

§Errors
Source

pub fn public_key_is_a(&self, alg: &CStr) -> bool

Check whether the certificate’s public key uses the named algorithm.

Uses X509_get0_pubkey — no reference-count increment. Call Self::public_key if you need an owned crate::pkey::Pkey handle.

Returns false if the certificate has no public key or if the algorithm name does not match.

Source

pub fn public_key_bits(&self) -> Option<u32>

Bit size of the certificate’s public key.

Uses X509_get0_pubkey — no reference-count increment.

Returns None if the certificate has no public key.

Source

pub fn signature_info(&self) -> Result<SignatureInfo, ErrorStack>

Inspect the signature algorithm used in this certificate.

Calls X509_get_signature_info to decode the signature algorithm fields embedded in the certificate’s signatureAlgorithm and signature structures.

md_nid is 0 (NID_undef) for algorithms that have no separate pre-hash step, such as Ed25519 and ML-DSA (post-quantum lattice signatures defined in FIPS 204). Always check for 0 before using md_nid as a digest identifier.

§Errors

Returns Err if OpenSSL cannot decode the signature algorithm (e.g. the certificate’s signature field is absent or uses an unrecognised OID).

Source

pub fn verify(&self, key: &Pkey<Public>) -> Result<bool, ErrorStack>

Verify this certificate was signed by key.

Returns Ok(true) if the signature is valid, Ok(false) if not, or Err on a fatal error.

§Errors
Source

pub fn is_self_signed(&self) -> bool

Returns true if the certificate is self-signed.

Source

pub fn extension_count(&self) -> usize

Number of extensions in this certificate.

Source

pub fn extension(&self, idx: usize) -> Option<X509Extension<'_>>

Access extension by index (0-based).

Returns None if idx is out of range.

Source

pub fn extension_by_nid(&self, nid: i32) -> Option<X509Extension<'_>>

Find the first extension with the given NID.

Returns None if no such extension exists.

Source

pub fn extension_der(&self, nid: i32) -> Option<&[u8]>

Return the DER-encoded value of the first extension with the given NID.

Returns None if the extension is not present. The returned byte slice is borrowed from the certificate’s internal storage — zero-copy, no allocation — and is valid for 'self’s lifetime.

To iterate all extensions or access criticality flags, use Self::extension_by_nid or Self::extension instead.

Trait Implementations§

Source§

impl Clone for X509

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for X509

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for X509

Source§

impl Sync for X509

Auto Trait Implementations§

§

impl Freeze for X509

§

impl RefUnwindSafe for X509

§

impl Unpin for X509

§

impl UnwindSafe for X509

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.