PKCS8_PKEY_SET0(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸ’… PKCS8_PKEY_SET0(3)

okay so bestie, this is a page from the Library Functions Manual β€” yes the whole libcrypto situation β€” and it's literally all about peeking at and messing with PKCS#8 PrivateKeyInfo objects. private keys!! the main characters!! πŸ”βœ¨


🌸 NAME 🌸

PKCS8_pkey_set0, PKCS8_pkey_get0, PKCS8_pkey_add1_attr_by_NID, PKCS8_pkey_get0_attrs β€” these four icons change and inspect PKCS#8 PrivateKeyInfo objects 🫢


✨ SYNOPSIS ✨

so first you gotta pull in the library, it's giving <openssl/x509.h> energy πŸ’–

library β€œlibcrypto” #include <openssl/x509.h>

int
PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *keyinfo, ASN1_OBJECT *aobj,
    int version, int ptype, void *pval, unsigned char *data, int len);

int
PKCS8_pkey_get0(const ASN1_OBJECT **paobj, const unsigned char **pdata,
    int *plen, const X509_ALGOR **palgor,
    const PKCS8_PRIV_KEY_INFO *keyinfo);

int
PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *keyinfo, int nid,
    int type, const unsigned char *data, int len);

const STACK_OF(X509_ATTRIBUTE) *
PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *keyinfo);

πŸ¦‹ DESCRIPTION πŸ¦‹

πŸŽ€ PKCS8_pkey_set0()

this one initializes the keyinfo object, like setting up the whole vibe check from scratch πŸ’«

  • the algorithm gets set to aobj, with the associated parameter type ptype and parameter value pval, all via X509_ALGOR_set0(3) β€” and yes it replaces any previous info about the algorithm, no hoarding old data πŸ™ˆ
  • unless data is NULL, the encoded private key is set to the len bytes starting at data using ASN1_STRING_set0(3) β€” and ngl it does not perform any validation, so it's trusting you fr fr πŸ’€
  • if data is NULL, the key data stays unchanged, untouched, unbothered πŸ‘‘
  • if the version argument is greater than or equal to 0, it replaces any existing version; otherwise the version remains unchanged πŸ”₯
  • and if keyinfo contains any attributes, they remain unchanged too β€” they're safe, they're slaying, they're staying 🫢

🧚 PKCS8_pkey_get0()

this one retrieves some info from the keyinfo object πŸŽ€ internal pointers to the algorithm OID, the AlgorithmIdentifier, and the encoded private key get stored in *paobj, *palgor, and *pdata respectively ✨

  • NULL pointers can be passed for any of these three arguments if you don't need that respective info β€” lowkey efficient πŸ’…
  • unless pdata is NULL, *plen is set to the number of bytes in *pdata πŸ“

🌈 PKCS8_pkey_add1_attr_by_NID()

creates a brand new X.501 Attribute object using X509_ATTRIBUTE_create_by_NID(3) and appends it to the attributes of keyinfo β€” it's giving "and one more for the collection" πŸ“


⭐ RETURN VALUES ⭐

  • PKCS8_pkey_set0() and PKCS8_pkey_add1_attr_by_NID() return 1 for success or 0 for failure β€” classic binary slay πŸ’€βœ…
  • PKCS8_pkey_get0() always returns 1. iconic, unproblematic, never fails 😌
  • PKCS8_pkey_get0_attrs() returns an internal pointer to the array of attributes associated with keyinfo, or NULL if no attributes are set πŸ™ˆ

πŸ”₯ SEE ALSO πŸ”₯

go read these next bestie, they're all connected iykyk 🧚


πŸ’œ HISTORY πŸ’œ

  • PKCS8_pkey_set0() and PKCS8_pkey_get0() first appeared in OpenSSL 1.0.0 and have been available since OpenBSD 4.9 πŸŽ€
  • PKCS8_pkey_add1_attr_by_NID() and PKCS8_pkey_get0_attrs() first appeared in OpenSSL 1.1.0 and have been available since OpenBSD 6.4 🌸

okay that's the whole page, no cap, every detail intact πŸ’…βœ¨ this one came from Debian, dated June 8, 2025 β€” mwah, go forth and handle your PrivateKeyInfo objects like the main character you are πŸ«ΆπŸ‘‘πŸ’–

πŸ“¦ where this page comes from