LIBRESSL-SSL_CTX_SET_DEFAULT_PASSWD_CB(3)
π SSL_CTX_SET_DEFAULT_PASS(3) β like, the password-callback page, bestie
okay so we're in the Library Functions Manual and this one is titled SSL_CTX_SET_DEFAULT_PASS(3). Grab a strawberry drink π, because we're talking about how libssl/libcrypto asks you for a password when it's opening an encrypted PEM file. It's giving "let me in" energy. πβ¨
πΈ SYNOPSIS πΈ
library βlibsslβ libcrypto #include <openssl/ssl.h>
void
SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb);
void
SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *userdata);
pem_password_cb *
SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx);
void *
SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx);
β¨ DESCRIPTION β¨
So SSL_CTX_set_default_passwd_cb() is the function that sets the password callback β you know, the little function that gets called when the library needs to load a certificate or private key from encrypted PEM format and is like "hiiii what's the password π₯Ί".
Specifically, the callback gets used by all of these besties:
SSL_CTX_use_certificate_file(3)SSL_use_certificate_file(3)SSL_CTX_use_certificate_chain_file(3)SSL_use_certificate_chain_file(3)SSL_CTX_use_certificate_chain_mem(3)SSL_CTX_use_PrivateKey_file(3)SSL_use_PrivateKey_file(3)SSL_CTX_use_RSAPrivateKey_file(3)SSL_use_RSAPrivateKey_file(3)
The function pointer type of the cb argument is documented over in the pem_password_cb(3) manual page β go read that if you want the deets π. And here's the thing: if you never call SSL_CTX_set_default_passwd_cb() on ctx, or if you call it with a cb argument of NULL, then PEM_def_callback(3) gets used instead. Default behavior, iconic. π
SSL_CTX_set_default_passwd_cb_userdata() sets a pointer to the userdata that will get handed to the password callback when it's invoked. Basically: your little gift basket π that gets passed along to the callback.
Important vibe check: since the cb you pass to SSL_CTX_set_default_passwd_cb() will only be used for reading and decryption β not for writing and encryption β the library will only ever call it with a verify argument of 0. No cap. π₯
Now, if your app only needs to read and decrypt one single private key, it can be practical to have the callback handle the password dialog interactively. And this happens by default if neither SSL_CTX_set_default_passwd_cb() nor SSL_CTX_set_default_passwd_cb_userdata() is called β in that case the library uses PEM_def_callback(3) with a userdata argument of NULL. She's got defaults, she's self-sufficient. πββοΈ
If you've got several keys to handle, it can be practical to ask for the password once β like with UI_UTIL_read_pw_string(3) β then keep it in memory and use it several times by passing a pointer to it to SSL_CTX_set_default_passwd_cb_userdata(). And good news: PEM_def_callback(3) is able to handle this case too, so calling SSL_CTX_set_default_passwd_cb() is not needed in this case either. You literally don't have to do anything. Slay. π
Other items in PEM formatting (certificates) can also be encrypted β but it's however atypical, since certificate information is considered public. Like, why would you lock that, it's already on the timeline. π€·ββοΈ
π¦ RETURN VALUES π¦
SSL_CTX_get_default_passwd_cb()β returns a function pointer to the password callback currently set inctx, orNULLif none is set.SSL_CTX_get_default_passwd_cb_userdata()β returns a pointer to the userdata currently set inctx, orNULLif none is set.
Simple. Clean. We love a getter that just tells you the truth. π«Ά
π EXAMPLES π
The following example provides a subset of the functionality of PEM_def_callback(3) β except that PEM_def_callback(3) does not NUL-terminate and copies up to size rather than size - 1 bytes. It interprets userdata as a NUL-terminated string and copies it to the password buffer, truncating the copy if it does not fit.
int
trivial_passwd_cb(char *password, int size, int verify, void *userdata)
{
strlcpy(password, userdata, size);
return strlen(password);
}
Look at her. Tiny. Efficient. Lowkey doing the most with the least. β
π SEE ALSO π
π HISTORY π
SSL_CTX_set_default_passwd_cb()first appeared in SSLeay 0.6.2 and has been available since OpenBSD 2.4. Ancient. Legendary. Still here. πSSL_CTX_set_default_passwd_cb_userdata()first appeared in OpenSSL 0.9.4 and has been available since OpenBSD 2.6.SSL_CTX_get_default_passwd_cb()andSSL_CTX_get_default_passwd_cb_userdata()first appeared in OpenSSL 1.1.0 and have been available since OpenBSD 6.3. The getters are the babies of the family, and honestly? They ate. π
and that's the page!! mwah π This SSL_CTX_SET_DEFAULT_PASSWD_CB(3) page is brought to you by Debian, dated June 8, 2025 β go forth and set your passwd callbacks responsibly, bestie π§ββοΈβ¨
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2000, 2001, 2016 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms