SSL_CTX_USE_CERTIFICATE(3ossl)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’– SSL_CTX_USE_CERTIFICATE πŸ’– (that's the 3ossl manual, bestie)

okay so. this is the page about loading certificates and keys into your TLS setup β€” like, the actual identity stuff. it's a lot of functions but they're all cousins, i promise. πŸŽ€


✨ NAME ✨

SSL_CTX_use_certificate, SSL_CTX_use_certificate_ASN1, SSL_CTX_use_certificate_file, SSL_use_certificate, SSL_use_certificate_ASN1, SSL_use_certificate_file, SSL_CTX_use_certificate_chain_file, SSL_use_certificate_chain_file, SSL_CTX_use_PrivateKey, SSL_CTX_use_PrivateKey_ASN1, SSL_CTX_use_PrivateKey_file, SSL_CTX_use_RSAPrivateKey, SSL_CTX_use_RSAPrivateKey_ASN1, SSL_CTX_use_RSAPrivateKey_file, SSL_use_PrivateKey_file, SSL_use_PrivateKey_ASN1, SSL_use_PrivateKey, SSL_use_RSAPrivateKey, SSL_use_RSAPrivateKey_ASN1, SSL_use_RSAPrivateKey_file, SSL_CTX_check_private_key, SSL_check_private_key, SSL_CTX_use_cert_and_key, SSL_use_cert_and_key β€” literally all of them load certificate and key data. that's the whole vibe. πŸ¦‹


🌸 SYNOPSIS 🌸

#include <openssl/ssl.h>

int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x);
int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d);
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type);
int SSL_use_certificate(SSL *ssl, X509 *x);
int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len);
int SSL_use_certificate_file(SSL *ssl, const char *file, int type);

int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file);
int SSL_use_certificate_chain_file(SSL *ssl, const char *file);

int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey);
int SSL_CTX_use_PrivateKey_ASN1(int pk, SSL_CTX *ctx, const unsigned char *d,
                                long len);
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type);
int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa);
int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len);
int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type);
int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey);
int SSL_use_PrivateKey_ASN1(int pk, SSL *ssl, const unsigned char *d, long len);
int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type);
int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa);
int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len);
int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type);

int SSL_CTX_check_private_key(const SSL_CTX *ctx);
int SSL_check_private_key(const SSL *ssl);

int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x, EVP_PKEY *pkey, STACK_OF(X509) *chain, int override);
int SSL_use_cert_and_key(SSL *ssl, X509 *x, EVP_PKEY *pkey, STACK_OF(X509) *chain, int override);

πŸ’… DESCRIPTION πŸ’…

so these functions load certificates and private keys into the SSL_CTX or the SSL object β€” respectively, of course. 😌

🫢 the SSL_CTX_* girlies vs the SSL_* girlies

the SSL_CTX_* class of functions loads the certificates and keys into the SSL_CTX object ctx. important: that info is passed to SSL objects ssl created from ctx with SSL_new(3) by copying, so any changes you apply to ctx afterwards do not propagate to SSL objects that already exist. no retroactive glow-ups. πŸ’€

the SSL_* class of functions only loads certificates and keys into one specific SSL object. and good news: that specific info is kept when SSL_clear(3) is called for this SSL object. iconic behavior honestly. ✨

πŸŽ€ certificates

  • SSL_CTX_use_certificate() loads the certificate x into ctx, and SSL_use_certificate() loads x into ssl. the rest of the certificates needed to form the complete certificate chain can be specified using the SSL_CTX_add_extra_chain_cert(3) function. on success the reference counter of the x is incremented. πŸ“
  • SSL_CTX_use_certificate_ASN1() loads the ASN1 encoded certificate from the memory location d (with length len) into ctx, and SSL_use_certificate_ASN1() loads the ASN1 encoded certificate into ssl.
  • SSL_CTX_use_certificate_file() loads the first certificate stored in file into ctx. the formatting type of the certificate must be specified from the known types SSL_FILETYPE_PEM, SSL_FILETYPE_ASN1. SSL_use_certificate_file() loads the certificate from file into ssl. πŸ‘€ and pls check the NOTES section on why SSL_CTX_use_certificate_chain_file() should be preferred. like, it's for your own good.
  • SSL_CTX_use_certificate_chain_file() loads a certificate chain from file into ctx. the certificates must be in PEM format and must be sorted starting with the subject's certificate (the actual client or server certificate), followed by intermediate CA certificates if applicable, and ending at the highest level (root) CA. order matters, no cap. SSL_use_certificate_chain_file() is similar except it loads the certificate chain into ssl.

πŸ”‘ private keys

  • SSL_CTX_use_PrivateKey() adds pkey as private key to ctx. ctx MUST NOT be NULL. SSL_CTX_use_RSAPrivateKey() adds the private key rsa of type RSA to ctx. SSL_use_PrivateKey() adds pkey as private key to ssl; SSL_use_RSAPrivateKey() adds rsa as private key of type RSA to ssl.
  • ⚠️ if a certificate has already been set and the private key does not belong to the certificate, an error is returned. and to change a [certificate/private-key] pair, the new certificate needs to be set first with SSL_use_certificate() or SSL_CTX_use_certificate() before setting the private key with SSL_CTX_use_PrivateKey() or SSL_use_PrivateKey(). order of operations, bestie, it's giving rules. πŸ’…
  • on success the reference counter of the pkey/rsa is incremented. 🫢
  • SSL_CTX_use_PrivateKey_ASN1() adds the private key of type pk stored at memory location d (length len) to ctx. SSL_CTX_use_RSAPrivateKey_ASN1() adds the private key of type RSA stored at memory location d (length len) to ctx. SSL_use_PrivateKey_ASN1() and SSL_use_RSAPrivateKey_ASN1() add the private key to ssl.
  • SSL_CTX_use_PrivateKey_file() adds the first private key found in file to ctx. the formatting type of the private key must be specified from the known types SSL_FILETYPE_PEM, SSL_FILETYPE_ASN1. SSL_CTX_use_RSAPrivateKey_file() adds the first private RSA key found in file to ctx. SSL_use_PrivateKey_file() adds the first private key found in file to ssl; SSL_use_RSAPrivateKey_file() adds the first private RSA key found to ssl. ctx MUST NOT be NULL. (yes they said it twice, it's that serious.)

πŸ‘‘ the combo meal: use_cert_and_key

SSL_CTX_use_cert_and_key() and SSL_use_cert_and_key() assign the X.509 certificate x, private key key, and certificate chain onto the corresponding ssl or ctx. (yes, the manpage says key in the prose even though the signature calls it pkey β€” we don't make the rules, we just serve them πŸ’€)

the pkey argument must be the private key of the X.509 certificate x.

  • if the override argument is 0, then x, pkey and chain are set only if all were not previously set.
  • if override is non-0, then the certificate, private key and chain certs are always set.
  • if pkey is NULL, then the public key of x is used as the private key.

this is intended to be used with hardware (via the ENGINE interface) that stores the private key securely, such that it cannot be accessed by OpenSSL. so the key's like... in a vault. main character energy, honestly. πŸ”₯

the reference count of the public key is incremented (twice if there is no private key); it is not copied nor duplicated. this allows all private key validations checks to succeed without an actual private key being assigned via SSL_CTX_use_PrivateKey(), etc. 😭✨

πŸ” checking your work

SSL_CTX_check_private_key() checks the consistency of a private key with the corresponding certificate loaded into ctx. if more than one key/certificate pair (RSA/DSA) is installed, the last item installed will be checked. if e.g. the last item was an RSA certificate or key, the RSA key/certificate pair will be checked. SSL_check_private_key() performs the same check for ssl. if no key/certificate was explicitly added for this ssl, the last item added into ctx will be checked. vibe check, but for keys. 🧚


🌈 NOTES 🌈

the internal certificate store of OpenSSL can hold several private key/certificate pairs at a time. which certificate gets used depends on the cipher selected β€” see also SSL_CTX_set_cipher_list(3). so it's not a choose-your-own-adventure, it's a cipher-choose-your-cert-adventure. πŸ¦‹

when reading certificates and private keys from file, files of type SSL_FILETYPE_ASN1 (also known as DER, binary encoding) can only contain one certificate or private key. consequently SSL_CTX_use_certificate_chain_file() is only applicable to PEM formatting. files of type SSL_FILETYPE_PEM can contain more than one item. one at a time for DER, party-bus for PEM. πŸŽ€

SSL_CTX_use_certificate_chain_file() adds the first certificate found in the file to the certificate store. the other certificates are added to the store of chain certificates using SSL_CTX_add1_chain_cert(3).

πŸ•°οΈ history moment: versions of OpenSSL before 1.0.2 only had a single certificate chain store for all certificate types. OpenSSL 1.0.2 and later have a separate chain store for each type, which is honestly so much better.

SSL_CTX_use_certificate_chain_file() should be used instead of the SSL_CTX_use_certificate_file() function in order to allow the use of complete certificate chains even when no trusted CA storage is used or when the CA issuing the certificate shall not be added to the trusted CA storage. it's the superior option, i said what i said. πŸ’…

if additional certificates are needed to complete the chain during the TLS negotiation, CA certificates are additionally looked up in the locations of trusted CA certificates β€” see SSL_CTX_load_verify_locations(3). πŸ”

the private keys loaded from file can be encrypted. in order to successfully load encrypted keys, a function returning the passphrase must have been supplied β€” see SSL_CTX_set_default_passwd_cb(3). πŸ” (certificate files might be encrypted as well from the technical point of view, it however does not make sense as the data in the certificate is considered public anyway. so like... don't bother, bestie. πŸ™ˆ)

heads up on replacing stuff: all of the functions to set a new certificate will replace any existing certificate of the same type that has already been set. similarly, all of the functions to set a new private key will replace any private key that has already been set. no leftovers, no hoarding. πŸ’€

⭐ applications should call SSL_CTX_check_private_key(3) or SSL_check_private_key(3) as appropriate after loading a new certificate and private key to confirm that the certificate and key match. do it. for me. 🫢

βœ… RETURN VALUES βœ…

on success, the functions return 1. πŸŽ‰ otherwise, check out the error stack to find out the reason. it'll tell you what's wrong, i promise. 😭


πŸ¦‹ SEE ALSO πŸ¦‹

ssl(7), SSL_new(3), SSL_clear(3), SSL_CTX_load_verify_locations(3), SSL_CTX_set_default_passwd_cb(3), SSL_CTX_set_cipher_list(3), SSL_CTX_set_client_CA_list(3), SSL_CTX_set_client_cert_cb(3), SSL_CTX_add_extra_chain_cert(3)


Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (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 <https://www.openssl.org/source/license.html>.


✨ and that's the whole page, bestie ✨ this glorious little manual is from OpenSSL 3.5.6, dated 2026-04-07 β€” go forth and load your certs correctly, i believe in you. πŸ’–πŸŽ€πŸ’…

πŸ“¦ where this page comes from