PKCS12_PBE_KEYIVGEN(3SSL)

πŸ’– PKCS12_PBE_KEYIVGEN β€” the OpenSSL 3SSL bestie guide πŸ’–

okay so you're here for PKCS12_PBE_KEYIVGEN(3SSL) and honestly? iconic choice. this is the PKCS#12 password-based-encryption page, and we're gonna get through it together. grab a strawberry drink πŸ“, we move.

✨ NAME ✨

PKCS12_PBE_keyivgen, PKCS12_PBE_keyivgen_ex, PKCS12_pbe_crypt, PKCS12_pbe_crypt_ex β€” PKCS#12 password based encryption πŸ’…

like. that's the whole vibe. passwords + encryption + the PKCS#12 of it all.


✨ SYNOPSIS ✨

first things first, you gotta let the compiler know:

#include <openssl/evp.h>

and then these four besties:

int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
                        ASN1_TYPE *param, const EVP_CIPHER *cipher,
                        const EVP_MD *md_type, int en_de);
int PKCS12_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
                           ASN1_TYPE *param, const EVP_CIPHER *cipher,
                           const EVP_MD *md_type, int en_de,
                           OSSL_LIB_CTX *libctx, const char *propq);
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
                                const char *pass, int passlen,
                                const unsigned char *in, int inlen,
                                unsigned char **data, int *datalen,
                                int en_de);
unsigned char *PKCS12_pbe_crypt_ex(const X509_ALGOR *algor,
                                   const char *pass, int passlen,
                                   const unsigned char *in, int inlen,
                                   unsigned char **data, int *datalen,
                                   int en_de, OSSL_LIB_CTX *libctx,
                                   const char *propq);

note how the _ex ones have libctx and propq bolted on the end. foreshadowing ✨.


✨ DESCRIPTION ✨

πŸŽ€ the keyivgen girls

PKCS12_PBE_keyivgen() and PKCS12_PBE_keyivgen_ex() take a password pass of length passlen, parameters param, and a message digest function md_type, and they do a key derivation according to PKCS#12. then that freshly-baked key gets used to initialise the cipher context ctx with a cipher cipher β€” for encryption when en_de=1, or decryption when en_de=0. that's it, that's the job 🫢

PKCS12_PBE_keyivgen_ex() is the extra version: it also lets your app specify a library context libctx and a property query propq, so you can pick which algorithm implementations you want. very "I choose my own destiny" of it.

πŸŽ€ the pbe_crypt girls

PKCS12_pbe_crypt() and PKCS12_pbe_crypt_ex() encrypt or decrypt a buffer, using the algorithm in algor and the password pass of length passlen. input comes from in with length inlen, and output goes into a malloc'd buffer that gets handed back in *data with length datalen. which direction it goes is decided by en_de β€” encryption (en_de=1) or decryption (en_de=0) πŸ”₯

PKCS12_pbe_crypt_ex() also lets the application specify a library context libctx and a property query propq to select appropriate algorithm implementations. same energy as above, no cap.

πŸŽ€ the deets on the params (iykyk)

  • pass β€” the password used in the derivation, of length passlen. it's optional, so it can legitimately be NULL. and if passlen is -1, the function will just compute the length of pass using strlen(). convenient πŸ’…
  • salt β€” the salt used in the derivation, of length saltlen. if the salt is NULL, then saltlen must be 0. the function will not try to figure out the length of the salt, because it does not assume the salt is NUL-terminated. so no guessing games here.
  • iter β€” the iteration count, and its value should be greater than or equal to 1. RFC 2898 suggests an iteration count of at least 1000. any iter less than 1 is treated as a single iteration. shortest possible glow-up ✨
  • digest β€” the message digest function used in the derivation.
  • functions ending in _ex() take the optional parameters libctx and propq, which are used to select appropriate algorithm implementations.

✨ NOTES ✨

these functions are typically used in PKCS#12 to encrypt objects. that's the whole point of them, bestie.

also, lowkey important: these functions make no assumption regarding the given password. it will simply be treated as a byte sequence. no parsing, no judgement, no vibes-checking your string. just bytes πŸ¦‹


✨ RETURN VALUES ✨

  • PKCS12_PBE_keyivgen(), PKCS12_PBE_keyivgen_ex() return 1 on success, or 0 on error.
  • PKCS12_pbe_crypt() and PKCS12_pbe_crypt_ex() return a buffer containing the output, or NULL if an error occurred. 😭
check your returns, bestie. always. NULL and 0 are out here and they will humble you.

✨ CONFORMING TO ✨

IETF RFC 7292 (<https://tools.ietf.org/html/rfc7292>) β€” we stay compliant, periodt. πŸ‘‘


✨ SEE ALSO ✨


✨ HISTORY ✨

PKCS12_PBE_keyivgen_ex() and PKCS12_pbe_crypt_ex() were added in OpenSSL 3.0. the _ex era began 🌈


Copyright 2014-2021 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>.


okay slay, that's the whole page πŸ’– this was OpenSSL 4.0.3, dated 2026-09-29, and yeah we're still on PKCS12_PBE_KEYIVGEN(3SSL) around here. go encrypt something responsibly, bestie πŸŽ€βœ¨

πŸ“¦ where this page comes from