Crypto module

X509Certificate

RAII wrapper for an OpenSSL X509 certificate with PEM loading and inspection.

#include <icy/crypto/x509certificate.h>

class X509Certificate

Defined in src/crypto/include/icy/crypto/x509certificate.h:33

Public Methods

ReturnNameDescription
X509Certificate explicitConstructs an X509Certificate by parsing a PEM-encoded certificate from memory.
X509Certificate explicitConstructs an X509Certificate by reading a PEM-encoded certificate from a file.
X509Certificate explicitConstructs an X509Certificate taking ownership of an existing OpenSSL X509 object.
X509CertificateConstructs an X509Certificate from an existing OpenSSL X509 object, optionally sharing ownership via reference count increment.
X509CertificateCopy-constructs an X509Certificate by duplicating the underlying X509 object.
X509Certificate noexceptMove-constructs an X509Certificate, transferring ownership from cert.
X509Certificate &operator=Copy-assigns a certificate, duplicating the underlying X509 object.
X509Certificate &operator= noexceptMove-assigns a certificate, transferring ownership from cert.
voidswapSwaps this certificate with cert.
~X509CertificateDestroys the X509Certificate and releases the underlying OpenSSL X509 object.
const std::string &issuerName constReturns the full distinguished name of the certificate issuer.
std::stringissuerName constExtracts a single field from the certificate issuer's distinguished name.
const std::string &subjectName constReturns the full distinguished name of the certificate subject.
std::stringsubjectName constExtracts a single field from the certificate subject's distinguished name.
std::stringcommonName constReturns the common name (CN) from the certificate subject.
voidextractNames constExtracts the common name and the set of Subject Alternative Name (SAN) DNS entries from the certificate.
DateTimevalidFrom constReturns the date and time from which the certificate is valid.
DateTimeexpiresOn constReturns the date and time at which the certificate expires.
voidsave constWrites the certificate in PEM format to an output stream.
voidsave constWrites the certificate in PEM format to a file.
boolissuedBy constVerifies whether this certificate was signed by the given issuer.
const X509 *certificate constReturns a const pointer to the underlying OpenSSL X509 object.
X509 *certificateReturns a mutable pointer to the underlying OpenSSL X509 object.

X509Certificate

explicit

explicit X509Certificate(const char * data, size_t length)

Defined in src/crypto/include/icy/crypto/x509certificate.h:55

Constructs an X509Certificate by parsing a PEM-encoded certificate from memory.

Parameters

ParameterTypeDescription
dataconst char *Pointer to a buffer containing the PEM-encoded certificate.
lengthsize_tNumber of bytes in data.

Exceptions

ExceptionDescription
std::runtime_errorif the BIO cannot be created or PEM parsing fails.

X509Certificate

explicit

explicit X509Certificate(const std::string & path)

Defined in src/crypto/include/icy/crypto/x509certificate.h:61

Constructs an X509Certificate by reading a PEM-encoded certificate from a file.

Parameters

ParameterTypeDescription
pathconst std::string &Filesystem path to the PEM certificate file.

Exceptions

ExceptionDescription
std::runtime_errorif the file cannot be opened or PEM parsing fails.

X509Certificate

explicit

explicit X509Certificate(X509 * pCert)

Defined in src/crypto/include/icy/crypto/x509certificate.h:68

Constructs an X509Certificate taking ownership of an existing OpenSSL X509 object.

Parameters

ParameterTypeDescription
pCertX509 *Non-null pointer to an OpenSSL X509 certificate. This object takes ownership and will call X509_free on destruction.

Exceptions

ExceptionDescription
std::runtime_errorif pCert is null.

X509Certificate

X509Certificate(X509 * pCert, bool shared)

Defined in src/crypto/include/icy/crypto/x509certificate.h:79

Constructs an X509Certificate from an existing OpenSSL X509 object, optionally sharing ownership via reference count increment.

Parameters

ParameterTypeDescription
pCertX509 *Non-null pointer to an OpenSSL X509 certificate. Ownership is always taken (X509_free called on destruction).
sharedboolIf true, increments the certificate's reference count via X509_up_ref before taking ownership, so the original pointer remains valid after this object is destroyed.

Exceptions

ExceptionDescription
std::runtime_errorif pCert is null.

X509Certificate

X509Certificate(const X509Certificate & cert)

Defined in src/crypto/include/icy/crypto/x509certificate.h:84

Copy-constructs an X509Certificate by duplicating the underlying X509 object.

Parameters

ParameterTypeDescription
certconst X509Certificate &The certificate to copy.

X509Certificate

noexcept

X509Certificate(X509Certificate && cert) noexcept

Defined in src/crypto/include/icy/crypto/x509certificate.h:89

Move-constructs an X509Certificate, transferring ownership from cert.

Parameters

ParameterTypeDescription
certX509Certificate &&The certificate to move from; left in a valid but empty state.

operator=

X509Certificate & operator=(const X509Certificate & cert)

Defined in src/crypto/include/icy/crypto/x509certificate.h:95

Copy-assigns a certificate, duplicating the underlying X509 object.

Returns

Reference to this object.

Parameters

ParameterTypeDescription
certconst X509Certificate &The certificate to copy.

operator=

noexcept

X509Certificate & operator=(X509Certificate && cert) noexcept

Defined in src/crypto/include/icy/crypto/x509certificate.h:101

Move-assigns a certificate, transferring ownership from cert.

Returns

Reference to this object.

Parameters

ParameterTypeDescription
certX509Certificate &&The certificate to move from; left in a valid but empty state.

swap

void swap(X509Certificate & cert)

Defined in src/crypto/include/icy/crypto/x509certificate.h:106

Swaps this certificate with cert.

Parameters

ParameterTypeDescription
certX509Certificate &The certificate to swap with.

~X509Certificate

~X509Certificate()

Defined in src/crypto/include/icy/crypto/x509certificate.h:109

Destroys the X509Certificate and releases the underlying OpenSSL X509 object.


issuerName

const

const std::string & issuerName() const

Defined in src/crypto/include/icy/crypto/x509certificate.h:114

Returns the full distinguished name of the certificate issuer.

Returns

One-line string representation produced by X509_NAME_oneline.


issuerName

const

std::string issuerName(NID nid) const

Defined in src/crypto/include/icy/crypto/x509certificate.h:120

Extracts a single field from the certificate issuer's distinguished name.

Returns

Field value, or an empty string if the field is absent.

Parameters

ParameterTypeDescription
nidNIDThe field to extract (e.g. NID_COMMON_NAME).

subjectName

const

const std::string & subjectName() const

Defined in src/crypto/include/icy/crypto/x509certificate.h:125

Returns the full distinguished name of the certificate subject.

Returns

One-line string representation produced by X509_NAME_oneline.


subjectName

const

std::string subjectName(NID nid) const

Defined in src/crypto/include/icy/crypto/x509certificate.h:131

Extracts a single field from the certificate subject's distinguished name.

Returns

Field value, or an empty string if the field is absent.

Parameters

ParameterTypeDescription
nidNIDThe field to extract (e.g. NID_ORGANIZATION_NAME).

commonName

const

std::string commonName() const

Defined in src/crypto/include/icy/crypto/x509certificate.h:138

Returns the common name (CN) from the certificate subject.

Convenience wrapper for subjectName(NID_COMMON_NAME).

Returns

Common name string, or empty if absent.


extractNames

const

void extractNames(std::string & commonName, std::set< std::string > & domainNames) const

Defined in src/crypto/include/icy/crypto/x509certificate.h:148

Extracts the common name and the set of Subject Alternative Name (SAN) DNS entries from the certificate.

If no SAN DNS entries are present and the common name is non-empty, the common name is added to domainNames as a fallback.

Parameters

ParameterTypeDescription
commonNamestd::string &Receives the certificate's common name.
domainNamesstd::set< std::string > &Receives all DNS SAN entries (cleared before population).

validFrom

const

DateTime validFrom() const

Defined in src/crypto/include/icy/crypto/x509certificate.h:156

Returns the date and time from which the certificate is valid.

Parsed from the X509 notBefore field.

Returns

UTC DateTime representing the start of the validity period.


expiresOn

const

DateTime expiresOn() const

Defined in src/crypto/include/icy/crypto/x509certificate.h:163

Returns the date and time at which the certificate expires.

Parsed from the X509 notAfter field.

Returns

UTC DateTime representing the end of the validity period.


save

const

void save(std::ostream & stream) const

Defined in src/crypto/include/icy/crypto/x509certificate.h:169

Writes the certificate in PEM format to an output stream.

Parameters

ParameterTypeDescription
streamstd::ostream &Destination stream to write to.

Exceptions

ExceptionDescription
std::runtime_errorif the BIO cannot be created or write fails.

save

const

void save(const std::string & path) const

Defined in src/crypto/include/icy/crypto/x509certificate.h:175

Writes the certificate in PEM format to a file.

Parameters

ParameterTypeDescription
pathconst std::string &Filesystem path of the output file (created or truncated).

Exceptions

ExceptionDescription
std::runtime_errorif the file cannot be opened or write fails.

issuedBy

const

bool issuedBy(const X509Certificate & issuerCertificate) const

Defined in src/crypto/include/icy/crypto/x509certificate.h:186

Verifies whether this certificate was signed by the given issuer.

Extracts the public key from issuerCertificate and calls X509_verify. Use this to validate links in a certificate chain.

Returns

true if this certificate's signature verifies against the issuer's public key, false otherwise.

Parameters

ParameterTypeDescription
issuerCertificateconst X509Certificate &The certificate of the purported issuer.

Exceptions

ExceptionDescription
std::invalid_argumentif the issuer certificate has no public key.

certificate

const

const X509 * certificate() const

Defined in src/crypto/include/icy/crypto/x509certificate.h:191

Returns a const pointer to the underlying OpenSSL X509 object.

Returns

Pointer valid for the lifetime of this X509Certificate.


certificate

X509 * certificate()

Defined in src/crypto/include/icy/crypto/x509certificate.h:196

Returns a mutable pointer to the underlying OpenSSL X509 object.

Returns

Pointer valid for the lifetime of this X509Certificate.

Protected Methods

ReturnNameDescription
voidloadParses a PEM-encoded certificate from a memory buffer and stores it.
voidloadReads a PEM-encoded certificate from a file and stores it.
voidinitPopulates _issuerName and _subjectName from the loaded certificate.

load

void load(const char * data, size_t length)

Defined in src/crypto/include/icy/crypto/x509certificate.h:205

Parses a PEM-encoded certificate from a memory buffer and stores it.

Parameters

ParameterTypeDescription
dataconst char *Pointer to PEM data.
lengthsize_tNumber of bytes in data.

Exceptions

ExceptionDescription
std::logic_errorif a certificate is already loaded.
std::runtime_errorif BIO creation or PEM parsing fails.

load

void load(const std::string & path)

Defined in src/crypto/include/icy/crypto/x509certificate.h:212

Reads a PEM-encoded certificate from a file and stores it.

Parameters

ParameterTypeDescription
pathconst std::string &Filesystem path to the PEM certificate file.

Exceptions

ExceptionDescription
std::logic_errorif a certificate is already loaded.
std::runtime_errorif the file cannot be opened or PEM parsing fails.

init

void init()

Defined in src/crypto/include/icy/crypto/x509certificate.h:217

Populates _issuerName and _subjectName from the loaded certificate.

Called after each successful load or construction from an X509 pointer.

Public Types

NameDescription
NIDName identifier for extracting fields from a certificate's distinguished name.

NID

enum NID

Defined in src/crypto/include/icy/crypto/x509certificate.h:40

Name identifier for extracting fields from a certificate's distinguished name.

Values correspond to OpenSSL NID constants used with X509_NAME_get_text_by_NID.

ValueDescription
NID_COMMON_NAMECommon name (CN field).
NID_COUNTRYCountry code (C field).
NID_LOCALITY_NAMELocality / city (L field).
NID_STATE_OR_PROVINCEState or province (ST field).
NID_ORGANIZATION_NAMEOrganization name (O field).
NID_ORGANIZATION_UNIT_NAMEOrganizational unit (OU field).

Private Attributes

ReturnNameDescription
std::string_issuerName
std::string_subjectName
X509Ptr_certificate

_issuerName

std::string _issuerName

Defined in src/crypto/include/icy/crypto/x509certificate.h:225


_subjectName

std::string _subjectName

Defined in src/crypto/include/icy/crypto/x509certificate.h:226


_certificate

X509Ptr _certificate

Type: X509Ptr

Defined in src/crypto/include/icy/crypto/x509certificate.h:227