PKCS7_ADD_ATTRIBUTE(3)

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

okay bestie, welcome to PKCS7_ADD_ATTRIBUTE(3) from the Library Functions Manual β€” the page that's literally all about attributes of SignerInfo objects. no cap, this is the group chat for your PKCS #7 attribute needs. πŸ’…βœ¨

πŸ’– NAME πŸ’–

the whole squad on this page is: PKCS7_add_attribute, PKCS7_set_attributes, PKCS7_get_attribute, PKCS7_add_signed_attribute, PKCS7_set_signed_attributes, PKCS7_get_signed_attribute, PKCS7_add_attrib_content_type, PKCS7_add1_attrib_digest, PKCS7_add0_attrib_signing_time, and PKCS7_add_attrib_smimecap β€” and together they're all about attributes of SignerInfo objects. 🫢

🌸 SYNOPSIS 🌸

first, the library is libcrypto, and you're gonna need this include:

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

and here's the function lineup β€” signatures stay exactly as they are, because signatures are sacred:

int
PKCS7_add_attribute(PKCS7_SIGNER_INFO *si, int nid, int attrtype,
    void *value);

int
PKCS7_set_attributes(PKCS7_SIGNER_INFO *si,
    STACK_OF(X509_ATTRIBUTE) *sk);

ASN1_TYPE *
PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid);

int
PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *si, int nid, int attrtype,
    void *value);

int
PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *si,
    STACK_OF(X509_ATTRIBUTE) *sk);

ASN1_TYPE *
PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid);

int
PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid);

int
PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si, const unsigned char *md,
    int mdlen);

int
PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t);

int
PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
    STACK_OF(X509_ALGOR) *cap);

πŸŽ€ DESCRIPTION πŸŽ€

PKCS7_add_attribute() β€” appends a new attribute of type nid to the unauthenticatedAttributes list of si, and adds a new ASN.1 ANY object of type attrtype with the given value to the new attribute. ownership of value is transferred into the new attribute object, so the calling code must not free(3) the value. 🫢 if the list already contains an unauthenticated attribute of type nid before the call, the new attribute replaces the old one instead of being appended to the end of the list.

PKCS7_set_attributes() β€” frees the unauthenticatedAttributes list of si and all the attributes contained in it, and replaces it with a deep copy of sk. πŸ’…

PKCS7_get_attribute() β€” retrieves the first ASN.1 ANY member of the attribute of type nid from the unauthenticatedAttributes list of si. ✨

the behaviour of PKCS7_add_signed_attribute(), PKCS7_set_signed_attributes(), and PKCS7_get_signed_attribute() is identical except that they operate on the list of authenticatedAttributes. same energy, different list, periodt. πŸ’–

the normal way to use PKCS7_add_signed_attribute() is: first create a SignedInfo object with PKCS7_sign(3) using the PKCS7_PARTIAL or PKCS7_STREAM flag, retrieve the PKCS7_SIGNER_INFO object with PKCS7_get_signer_info(3) or add an additional one with PKCS7_sign_add_signer(3), call PKCS7_add_signed_attribute() for each desired additional attribute, then do the signing with PKCS7_final(3) or with another finalizing function. that's the whole little ritual. πŸ•―οΈ

the four remaining functions are wrappers around PKCS7_add_signed_attribute(). πŸŽ€

  • PKCS7_add_attrib_content_type() β€” sets the NID_pkcs9_contentType attribute to coid, which specifies the content type of the ContentInfo value to be signed. this attribute is mandatory and automatically added by PKCS7_sign(3) and PKCS7_sign_add_signer(3) unless the PKCS7_NOATTR flag is present. objects suitable as coid arguments can for example be obtained with OBJ_nid2obj(3). if coid is NULL, the content type defaults to NID_pkcs7_data. βœ…
  • PKCS7_add1_attrib_digest() β€” sets or replaces the NID_pkcs9_messageDigest attribute, which is the message digest of the contents octets of the DER-encoding of the content field of the ContentInfo value being signed, to a copy of md, which is assumed to be mdlen bytes long. if mdlen is -1, then strlen(md) is used instead of mdlen. this attribute is mandatory and automatically added by PKCS7_dataFinal(3) and PKCS7_final(3). πŸ”₯
  • PKCS7_add0_attrib_signing_time() β€” sets or replaces the optional NID_pkcs9_signingTime attribute to t, specifying the time at which the signer performed the signing process. ownership of t is transferred into the new attribute object, so the calling code must not free(3) t. if t is NULL, a new ASN1_TIME structure is allocated. this attribute is automatically added by PKCS7_dataFinal(3) and PKCS7_final(3). ⏰
  • PKCS7_add_attrib_smimecap() β€” sets or replaces the optional NID_SMIMECapabilities attribute, indicating algorithms the sender is prepared to handle. the cap pointer is not stored in the new attribute object and can be passed to sk_X509_ALGOR_pop_free() after the call. this attribute is automatically added by PKCS7_sign(3) and PKCS7_sign_add_signer(3) unless the PKCS7_NOATTR or PKCS7_NOSMIMECAP flag is present. πŸ“

✨ RETURN VALUES ✨

  • PKCS7_add_attribute(), PKCS7_set_attributes(), PKCS7_add_signed_attribute(), PKCS7_set_signed_attributes(), PKCS7_add_attrib_content_type(), PKCS7_add1_attrib_digest(), PKCS7_add0_attrib_signing_time(), and PKCS7_add_attrib_smimecap() return 1 on success or 0 on failure. the most common reason for failure is lack of memory. PKCS7_add_attribute() and PKCS7_add_signed_attribute() also fail if nid is invalid, and PKCS7_add_attrib_content_type() fails if si already contains an authenticated attribute of type NID_pkcs9_contentType.
  • PKCS7_get_attribute() and PKCS7_get_signed_attribute() return an internal pointer to an ASN.1 ANY object or NULL on failure. they fail if nid is invalid, if the respective list in si contains no attribute of the requested type, or if an invalid element is found in the list before finding the attribute of the requested type. 😭

πŸ¦‹ SEE ALSO πŸ¦‹

go give these pages a little kiss:

🌈 STANDARDS 🌈

  • RFC 2315: PKCS #7: Cryptographic Message Syntax Version 1.5, section 9.2: SignerInfo type
  • RFC 2985: PKCS #9: Selected Object Classes and Attribute Types Version 2.0, section 5.3: Attribute types for use in PKCS #7 data and section 5.6: Attributes defined in S/MIME
  • RFC 5652: Cryptographic Message Syntax (CMS), section 5.3: SignerInfo Type and section 11: Useful Attributes
  • RFC 8551: Secure/Multipurpose Internet Mail Extensions (S/MIME) Version 4.0 Message Specification, section 2.5.2: SMIMECapabilities Attribute

πŸ‘‘ HISTORY πŸ‘‘

  • PKCS7_add_attribute(), PKCS7_set_attributes(), PKCS7_get_attribute(), PKCS7_add_signed_attribute(), PKCS7_set_signed_attributes(), and PKCS7_get_signed_attribute() first appeared in OpenSSL 0.9.1 and have been available since OpenBSD 2.6.
  • PKCS7_add_attrib_smimecap() first appeared in OpenSSL 0.9.5 and has been available since OpenBSD 2.7.
  • PKCS7_add_attrib_content_type(), PKCS7_add1_attrib_digest(), and PKCS7_add0_attrib_signing_time() first appeared in OpenSSL 1.0.0 and have been available since OpenBSD 4.9.

πŸ’€ CAVEATS πŸ’€

⚠️ PKCS7_set_signed_attributes() does not validate that sk contains the PKCS #9 content type and message digest attributes required by RFC 2315. it succeeds even when sk is empty, leaving si in a state that violates the standard. 😭 ⚠️ PKCS7_add0_attrib_signing_time() does not validate t beyond checking that it is well-formed per RFC 5652, section 11.3. in particular, it may set the signing time to the future or to the remote past. so yeah, time crimes are possible πŸ™ˆ

πŸ› BUGS πŸ›

there does not appear to be a function to remove individual attributes from these lists. a program that wants to do that might have to manually iterate the fields auth_attr and unauth_attr of si, which are both of type STACK_OF(X509_ATTRIBUTE), using the facilities described in STACK_OF(3) and OPENSSL_sk_new(3). not me having to do manual stack surgery πŸ’…


xoxo, your Debian girlie πŸ’– β€” this one's from Debian, dated July 27, 2025, and it's the PKCS7_ADD_ATTRIBUTE(3) page. slay, stay safe, and may your attributes always be valid. ✨🌈

πŸ“¦ where this page comes from