PKCS7_ADD_ATTRIBUTE(3)
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 theNID_pkcs9_contentTypeattribute tocoid, which specifies the content type of theContentInfovalue to be signed. this attribute is mandatory and automatically added byPKCS7_sign(3)andPKCS7_sign_add_signer(3)unless thePKCS7_NOATTRflag is present. objects suitable ascoidarguments can for example be obtained withOBJ_nid2obj(3). ifcoidisNULL, the content type defaults toNID_pkcs7_data. βPKCS7_add1_attrib_digest()β sets or replaces theNID_pkcs9_messageDigestattribute, which is the message digest of the contents octets of the DER-encoding of the content field of theContentInfovalue being signed, to a copy ofmd, which is assumed to bemdlenbytes long. ifmdlenis-1, thenstrlen(md)is used instead ofmdlen. this attribute is mandatory and automatically added byPKCS7_dataFinal(3)andPKCS7_final(3). π₯PKCS7_add0_attrib_signing_time()β sets or replaces the optionalNID_pkcs9_signingTimeattribute tot, specifying the time at which the signer performed the signing process. ownership oftis transferred into the new attribute object, so the calling code must notfree(3)t. iftisNULL, a newASN1_TIMEstructure is allocated. this attribute is automatically added byPKCS7_dataFinal(3)andPKCS7_final(3). β°PKCS7_add_attrib_smimecap()β sets or replaces the optionalNID_SMIMECapabilitiesattribute, indicating algorithms the sender is prepared to handle. thecappointer is not stored in the new attribute object and can be passed tosk_X509_ALGOR_pop_free()after the call. this attribute is automatically added byPKCS7_sign(3)andPKCS7_sign_add_signer(3)unless thePKCS7_NOATTRorPKCS7_NOSMIMECAPflag 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(), andPKCS7_add_attrib_smimecap()return1on success or0on failure. the most common reason for failure is lack of memory.PKCS7_add_attribute()andPKCS7_add_signed_attribute()also fail ifnidis invalid, andPKCS7_add_attrib_content_type()fails ifsialready contains an authenticated attribute of typeNID_pkcs9_contentType.PKCS7_get_attribute()andPKCS7_get_signed_attribute()return an internal pointer to an ASN.1 ANY object orNULLon failure. they fail ifnidis invalid, if the respective list insicontains 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:
ASN1_TIME_new(3)ASN1_TYPE_new(3)OBJ_nid2obj(3)PKCS7_final(3)PKCS7_get_signer_info(3)PKCS7_new(3)PKCS7_sign(3)PKCS7_sign_add_signer(3)STACK_OF(3)X509_ALGOR_new(3)X509_ATTRIBUTE_new(3)
π STANDARDS π
RFC 2315: PKCS #7: Cryptographic Message Syntax Version 1.5, section 9.2: SignerInfo typeRFC 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/MIMERFC 5652: Cryptographic Message Syntax (CMS), section 5.3: SignerInfo Type and section 11: Useful AttributesRFC 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(), andPKCS7_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(), andPKCS7_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 thatskcontains the PKCS #9 content type and message digest attributes required by RFC 2315. it succeeds even whenskis empty, leavingsiin a state that violates the standard. π β οΈPKCS7_add0_attrib_signing_time()does not validatetbeyond 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
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2020 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms