pem_password_cb, PEM_read_bio_PrivateKey, PEM_read_PrivateKey, PEM_write_bio_PrivateKey, PEM_write_bio_PrivateKey_traditional, PEM_write_PrivateKey, PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey, PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid, PEM_read_bio_PUBKEY, PEM_read_PUBKEY, PEM_write_bio_PUBKEY, PEM_write_PUBKEY, PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey, PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey, PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey, PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY, PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey, PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey, PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY, PEM_write_DSA_PUBKEY, PEM_read_bio_DSAparams, PEM_read_DSAparams, PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams, PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams, PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509, PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX, PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ, PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW, PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL, PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7, PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines

The example 'C' program certverify.c demonstrates how to perform a basic certificate validation against a root certificate authority, using the OpenSSL library functions.

With the latest revision of ssl-cert-check I get the following errors for some (though not all) of the servers I check regularly via ssl-cert-check. Unable to load certificate 57072:error:0906D06C:PEM routines:PEMreadbio:no s. PEMreadbioX509CRL does the same thing as PEMX509CRLread but uses a BIO instead of a file pointer. The following macros are provided for the convenience of the user.



The PEM functions read or write structures in PEM format. In this sense PEM format is simply base64 encoded data surrounded by header lines.

For more details about the meaning of arguments see the PEM FUNCTION ARGUMENTS section.

Each operation has four functions associated with it. For clarity the term 'foobar functions' will be used to collectively refer to the PEM_read_bio_foobar(), PEM_read_foobar(), PEM_write_bio_foobar() and PEM_write_foobar() functions.


The PrivateKey functions read or write a private key in PEM format using an EVP_PKEY structure. The write routines use PKCS#8 private key format and are equivalent to PEM_write_bio_PKCS8PrivateKey().The read functions transparently handle traditional and PKCS#8 format encrypted and unencrypted keys.

PEM_write_bio_PrivateKey_traditional() writes out a private key in legacy 'traditional' format.

PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a private key in an EVP_PKEY structure in PKCS#8 EncryptedPrivateKeyInfo format using PKCS#5 v2.0 password based encryption algorithms. The cipher argument specifies the encryption algorithm to use: unlike some other PEM routines the encryption is applied at the PKCS#8 level and not in the PEM headers. If cipher is NULL then no encryption is used and a PKCS#8 PrivateKeyInfo structure is used instead.

PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid() also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm to use is specified in the nid parameter and should be the NID of the corresponding OBJECT IDENTIFIER (see NOTES section).

The PUBKEY functions process a public key using an EVP_PKEY structure. The public key is encoded as a SubjectPublicKeyInfo structure.

The RSAPrivateKey functions process an RSA private key using an RSA structure. The write routines uses traditional format. The read routines handles the same formats as the PrivateKey functions but an error occurs if the private key is not RSA.

Pem_read_bio_x509 Return Null

The RSAPublicKey functions process an RSA public key using an RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey structure.

The RSA_PUBKEY functions also process an RSA public key using an RSA structure. However the public key is encoded using a SubjectPublicKeyInfo structure and an error occurs if the public key is not RSA.

The DSAPrivateKey functions process a DSA private key using a DSA structure. The write routines uses traditional format. The read routines handles the same formats as the PrivateKey functions but an error occurs if the private key is not DSA.

The DSA_PUBKEY functions process a DSA public key using a DSA structure. The public key is encoded using a SubjectPublicKeyInfo structure and an error occurs if the public key is not DSA.

The DSAparams functions process DSA parameters using a DSA structure. The parameters are encoded using a Dss-Parms structure as defined in RFC2459.

The DHparams functions process DH parameters using a DH structure. The parameters are encoded using a PKCS#3 DHparameter structure.

The X509 functions process an X509 certificate using an X509 structure. They will also process a trusted X509 certificate but any trust settings are discarded.

The X509_AUX functions process a trusted X509 certificate using an X509 structure.

The X509_REQ and X509_REQ_NEW functions process a PKCS#10 certificate request using an X509_REQ structure. The X509_REQ write functions use CERTIFICATE REQUEST in the header whereas the X509_REQ_NEW functions use NEW CERTIFICATE REQUEST (as required by some CAs). The X509_REQ read functions will handle either form so there are no X509_REQ_NEW read functions.

The X509_CRL functions process an X509 CRL using an X509_CRL structure.

The PKCS7 functions process a PKCS#7 ContentInfo using a PKCS7 structure.


The PEM functions have many common arguments.

The bp BIO parameter (if present) specifies the BIO to read from or write to.

The fp FILE parameter (if present) specifies the FILE pointer to read from or write to.

The PEM read functions all take an argument TYPE **x and return a TYPE * pointer. Where TYPE is whatever structure the function uses. If x is NULL then the parameter is ignored. If x is not NULL but *x is NULL then the structure returned will be written to *x. If neither x nor *x is NULL then an attempt is made to reuse the structure at *x (but see BUGS and EXAMPLES sections). Irrespective of the value of x a pointer to the structure is always returned (or NULL if an error occurred).

The PEM functions which write private keys take an enc parameter which specifies the encryption algorithm to use, encryption is done at the PEM level. If this parameter is set to NULL then the private key is written in unencrypted form.

The cb argument is the callback to use when querying for the pass phrase used for encrypted PEM structures (normally only private keys).

For the PEM write routines if the kstr parameter is not NULL then klen bytes at kstr are used as the passphrase and cb is ignored.

If the cb parameters is set to NULL and the u parameter is not NULL then the u parameter is interpreted as a null terminated string to use as the passphrase. If both cb and u are NULL then the default callback routine is used which will typically prompt for the passphrase on the current terminal with echoing turned off.

The default passphrase callback is sometimes inappropriate (for example in a GUI application) so an alternative can be supplied. The callback routine has the following form:

buf is the buffer to write the passphrase to. size is the maximum length of the passphrase (i.e. the size of buf). rwflag is a flag which is set to 0 when reading and 1 when writing. A typical routine will ask the user to verify the passphrase (for example by prompting for it twice) if rwflag is 1. The u parameter has the same value as the u parameter passed to the PEM routine. It allows arbitrary data to be passed to the callback by the application (for example a window handle in a GUI application). The callback must return the number of characters in the passphrase or -1 if an error occurred.


Although the PEM routines take several arguments in almost all applications most of them are set to 0 or NULL.

Read a certificate in PEM format from a BIO:

Alternative method:

Write a certificate to a BIO:

Write a private key (using traditional format) to a BIO using triple DES encryption, the pass phrase is prompted for:

Write a private key (using PKCS#8 format) to a BIO using triple DES encryption, using the pass phrase 'hello':

Read a private key from a BIO using a pass phrase callback:

Skeleton pass phrase callback:


The old PrivateKey write routines are retained for compatibility. New applications should write private keys using the PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines because they are more secure (they use an iteration count of 2048 whereas the traditional routines use a count of 1) unless compatibility with older versions of OpenSSL is important.

The PrivateKey read routines can be used in all applications because they handle all formats transparently.

A frequent cause of problems is attempting to use the PEM routines like this:

Pem_read_bio_x509 Example

this is a bug because an attempt will be made to reuse the data at x which is an uninitialised pointer.


These old PrivateKey routines use a non standard technique for encryption.

The private key (or other data) takes the following form:

The line beginning with Proc-Type contains the version and the protection on the encapsulated data. The line beginning DEK-Info contains two comma separated values: the encryption algorithm name as used by EVP_get_cipherbyname() and an initialization vector used by the cipher encoded as a set of hexadecimal digits. After those two lines is the base64-encoded encrypted data.

The encryption key is derived using EVP_BytesToKey(). The cipher's initialization vector is passed to EVP_BytesToKey() as the salt parameter. Internally, PKCS5_SALT_LEN bytes of the salt are used (regardless of the size of the initialization vector). The user's password is passed to EVP_BytesToKey() using the data and datal parameters. Finally, the library uses an iteration count of 1 for EVP_BytesToKey().

The key derived by EVP_BytesToKey() along with the original initialization vector is then used to decrypt the encrypted data. The iv produced by EVP_BytesToKey() is not utilized or needed, and NULL should be passed to the function.

The pseudo code to derive the key would look similar to:


The PEM read routines in some versions of OpenSSL will not correctly reuse an existing structure. Therefore the following:

where x already contains a valid certificate, may not work, whereas:

is guaranteed to work.


The read routines return either a pointer to the structure read or NULL if an error occurred.

The write routines return 1 for success or 0 for failure.


The old Netscape certificate sequences were no longer documented in OpenSSL 1.1; applications should use the PKCS7 standard instead as they will be formally deprecated in a future releases.


EVP_EncryptInit(3), EVP_BytesToKey(3)


Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the OpenSSL license (the 'License'). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at

An X509 public key certificate.


impl X509[src]

pub fn builder() -> Result<X509Builder, ErrorStack>[src]

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

Deserializes a PEM-encoded X509 structure.

The input should have a header of -----BEGIN CERTIFICATE-----.

This corresponds to PEM_read_bio_X509.

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

Deserializes a DER-encoded X509 structure.

This corresponds to d2i_X509.

pub fn stack_from_pem(pem: &[u8]) -> Result<Vec<X509>, ErrorStack>[src]

Deserializes a list of PEM-formatted certificates.

Methods from Deref<Target = X509Ref>

pub fn subject_name(&self) -> &X509NameRef[src]

Returns this certificate’s subject name.

This corresponds to X509_get_subject_name.

pub fn subject_name_hash(&self) -> u32[src]

Returns the hash of the certificates subject

This corresponds to X509_subject_name_hash.

pub fn issuer_name(&self) -> &X509NameRef[src]

Returns this certificate’s issuer name.

This corresponds to X509_get_issuer_name.

pub fn subject_alt_names(&self) -> Option<Stack<GeneralName>>[src]

Returns this certificate’s subject alternative name entries, if they exist.

This corresponds to X509_get_ext_d2i called with NID_subject_alt_name.

pub fn issuer_alt_names(&self) -> Option<Stack<GeneralName>>[src]

Returns this certificate’s issuer alternative name entries, if they exist.

This corresponds to X509_get_ext_d2i called with NID_issuer_alt_name.

pub fn authority_info(&self) -> Option<Stack<AccessDescription>>[src]

Returns this certificate’s authority information access entries, if they exist.

This corresponds to X509_get_ext_d2i called with NID_info_access.

pub fn public_key(&self) -> Result<PKey<Public>, ErrorStack>[src]

pub fn digest(
hash_type: MessageDigest
) -> Result<DigestBytes, ErrorStack>

Returns a digest of the DER representation of the certificate.

This corresponds to X509_digest.

pub fn fingerprint(
hash_type: MessageDigest
) -> Result<Vec<u8>, ErrorStack>

pub fn not_after(&self) -> &Asn1TimeRef[src]

Returns the certificate’s Not After validity period.

pub fn not_before(&self) -> &Asn1TimeRef[src]

Returns the certificate’s Not Before validity period.

pub fn signature(&self) -> &Asn1BitStringRef[src]

pub fn signature_algorithm(&self) -> &X509AlgorithmRef[src]

Returns the certificate’s signature algorithm.


pub fn ocsp_responders(&self) -> Result<Stack<OpensslString>, ErrorStack>[src]

Returns the list of OCSP responder URLs specified in the certificate’s Authority InformationAccess field.

pub fn issued(&self, subject: &X509Ref) -> X509VerifyResult[src]

pub fn version(&self) -> i32[src]

Returns certificate version. If this certificate has no explicit version set, it defaults toversion 1.

Note that 0 return value stands for version 1, 1 for version 2 and so on.

This corresponds to X509_get_version.

pub fn verify<T>(&self, key: &PKeyRef<T>) -> Result<bool, ErrorStack> where
T: HasPublic,

Check if the certificate is signed using the given public key.

Only the signature is checked: no other checks (such as certificate chain validity)are performed.

Returns true if verification succeeds.


This corresponds to [`X509_verify“].

pub fn serial_number(&self) -> &Asn1IntegerRef[src]

Returns this certificate’s serial number.

This corresponds to X509_get_serialNumber.

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


Serializes the certificate into a PEM-encoded X509 structure.

The output will have a header of -----BEGIN CERTIFICATE-----.

This corresponds to PEM_write_bio_X509.

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

Serializes the certificate into a DER-encoded X509 structure.


This corresponds to i2d_X509.

Trait Implementations

impl AsRef<X509Ref> for X509[src]

fn as_ref(&self) -> &X509Ref[src]

impl Borrow<X509Ref> for X509[src]

fn borrow(&self) -> &X509Ref[src]

impl Clone for X509[src]

fn clone(&self) -> X509[src]

pub fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for X509[src]


fn fmt(&self, formatter: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Deref for X509[src]

type Target = X509Ref

fn deref(&self) -> &X509Ref[src]

Dereferences the value.


impl DerefMut for X509[src]

fn deref_mut(&mut self) -> &mut X509Ref[src]

impl Drop for X509[src]

fn drop(&mut self)[src]

impl ForeignType for X509[src]

type CType = X509

type Ref = X509Ref

The type representing a reference to this type.

unsafe fn from_ptr(ptr: *mut X509) -> X509[src]

Constructs an instance of this type from its raw type.

fn as_ptr(&self) -> *mut X509[src]

impl Send for X509[src]

impl Stackable for X509[src]

type StackType = stack_st_X509

impl Sync for X509[src]

Auto Trait Implementations

impl RefUnwindSafe for X509

impl Unpin for X509

impl UnwindSafe for X509

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

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

pub fn borrow(&self) -> &T[src]

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

pub fn borrow_mut(&mut self) -> &mut T[src]

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

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

pub fn into(self) -> U[src]

impl<T> ToOwned for T where
T: Clone,

type Owned = T

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

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

The type returned in the event of a conversion error.

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