Skip to content

Commit

Permalink
Add missing documentation for X509_ATTRIBUTE related functions.
Browse files Browse the repository at this point in the history
Partial fix for openssl#8026

Reviewed-by: Tom Cosgrove <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#22656)

(cherry picked from commit f1f0731)
  • Loading branch information
slontis authored and t8m committed Jan 3, 2024
1 parent 41dd0e0 commit 9ee889f
Show file tree
Hide file tree
Showing 8 changed files with 727 additions and 56 deletions.
2 changes: 1 addition & 1 deletion crypto/x509/x509_req.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)

if (req == NULL) {
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
return 0;
return NULL;
}
attr = X509at_delete_attr(req->req_info.attributes, loc);
if (attr != NULL)
Expand Down
24 changes: 24 additions & 0 deletions doc/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ DEPEND[html/man3/CMS_sign_receipt.html]=man3/CMS_sign_receipt.pod
GENERATE[html/man3/CMS_sign_receipt.html]=man3/CMS_sign_receipt.pod
DEPEND[man/man3/CMS_sign_receipt.3]=man3/CMS_sign_receipt.pod
GENERATE[man/man3/CMS_sign_receipt.3]=man3/CMS_sign_receipt.pod
DEPEND[html/man3/CMS_signed_get_attr.html]=man3/CMS_signed_get_attr.pod
GENERATE[html/man3/CMS_signed_get_attr.html]=man3/CMS_signed_get_attr.pod
DEPEND[man/man3/CMS_signed_get_attr.3]=man3/CMS_signed_get_attr.pod
GENERATE[man/man3/CMS_signed_get_attr.3]=man3/CMS_signed_get_attr.pod
DEPEND[html/man3/CMS_uncompress.html]=man3/CMS_uncompress.pod
GENERATE[html/man3/CMS_uncompress.html]=man3/CMS_uncompress.pod
DEPEND[man/man3/CMS_uncompress.3]=man3/CMS_uncompress.pod
Expand Down Expand Up @@ -1239,6 +1243,10 @@ DEPEND[html/man3/EVP_PKEY_fromdata.html]=man3/EVP_PKEY_fromdata.pod
GENERATE[html/man3/EVP_PKEY_fromdata.html]=man3/EVP_PKEY_fromdata.pod
DEPEND[man/man3/EVP_PKEY_fromdata.3]=man3/EVP_PKEY_fromdata.pod
GENERATE[man/man3/EVP_PKEY_fromdata.3]=man3/EVP_PKEY_fromdata.pod
DEPEND[html/man3/EVP_PKEY_get_attr.html]=man3/EVP_PKEY_get_attr.pod
GENERATE[html/man3/EVP_PKEY_get_attr.html]=man3/EVP_PKEY_get_attr.pod
DEPEND[man/man3/EVP_PKEY_get_attr.3]=man3/EVP_PKEY_get_attr.pod
GENERATE[man/man3/EVP_PKEY_get_attr.3]=man3/EVP_PKEY_get_attr.pod
DEPEND[html/man3/EVP_PKEY_get_default_digest_nid.html]=man3/EVP_PKEY_get_default_digest_nid.pod
GENERATE[html/man3/EVP_PKEY_get_default_digest_nid.html]=man3/EVP_PKEY_get_default_digest_nid.pod
DEPEND[man/man3/EVP_PKEY_get_default_digest_nid.3]=man3/EVP_PKEY_get_default_digest_nid.pod
Expand Down Expand Up @@ -2655,6 +2663,10 @@ DEPEND[html/man3/X509_ALGOR_dup.html]=man3/X509_ALGOR_dup.pod
GENERATE[html/man3/X509_ALGOR_dup.html]=man3/X509_ALGOR_dup.pod
DEPEND[man/man3/X509_ALGOR_dup.3]=man3/X509_ALGOR_dup.pod
GENERATE[man/man3/X509_ALGOR_dup.3]=man3/X509_ALGOR_dup.pod
DEPEND[html/man3/X509_ATTRIBUTE.html]=man3/X509_ATTRIBUTE.pod
GENERATE[html/man3/X509_ATTRIBUTE.html]=man3/X509_ATTRIBUTE.pod
DEPEND[man/man3/X509_ATTRIBUTE.3]=man3/X509_ATTRIBUTE.pod
GENERATE[man/man3/X509_ATTRIBUTE.3]=man3/X509_ATTRIBUTE.pod
DEPEND[html/man3/X509_CRL_get0_by_serial.html]=man3/X509_CRL_get0_by_serial.pod
GENERATE[html/man3/X509_CRL_get0_by_serial.html]=man3/X509_CRL_get0_by_serial.pod
DEPEND[man/man3/X509_CRL_get0_by_serial.3]=man3/X509_CRL_get0_by_serial.pod
Expand Down Expand Up @@ -2699,6 +2711,10 @@ DEPEND[html/man3/X509_PUBKEY_new.html]=man3/X509_PUBKEY_new.pod
GENERATE[html/man3/X509_PUBKEY_new.html]=man3/X509_PUBKEY_new.pod
DEPEND[man/man3/X509_PUBKEY_new.3]=man3/X509_PUBKEY_new.pod
GENERATE[man/man3/X509_PUBKEY_new.3]=man3/X509_PUBKEY_new.pod
DEPEND[html/man3/X509_REQ_get_attr.html]=man3/X509_REQ_get_attr.pod
GENERATE[html/man3/X509_REQ_get_attr.html]=man3/X509_REQ_get_attr.pod
DEPEND[man/man3/X509_REQ_get_attr.3]=man3/X509_REQ_get_attr.pod
GENERATE[man/man3/X509_REQ_get_attr.3]=man3/X509_REQ_get_attr.pod
DEPEND[html/man3/X509_SIG_get0.html]=man3/X509_SIG_get0.pod
GENERATE[html/man3/X509_SIG_get0.html]=man3/X509_SIG_get0.pod
DEPEND[man/man3/X509_SIG_get0.3]=man3/X509_SIG_get0.pod
Expand Down Expand Up @@ -2974,6 +2990,7 @@ html/man3/CMS_get0_type.html \
html/man3/CMS_get1_ReceiptRequest.html \
html/man3/CMS_sign.html \
html/man3/CMS_sign_receipt.html \
html/man3/CMS_signed_get_attr.html \
html/man3/CMS_uncompress.html \
html/man3/CMS_verify.html \
html/man3/CMS_verify_receipt.html \
Expand Down Expand Up @@ -3073,6 +3090,7 @@ html/man3/EVP_PKEY_digestsign_supports_digest.html \
html/man3/EVP_PKEY_encapsulate.html \
html/man3/EVP_PKEY_encrypt.html \
html/man3/EVP_PKEY_fromdata.html \
html/man3/EVP_PKEY_get_attr.html \
html/man3/EVP_PKEY_get_default_digest_nid.html \
html/man3/EVP_PKEY_get_field_type.html \
html/man3/EVP_PKEY_get_group_name.html \
Expand Down Expand Up @@ -3427,6 +3445,7 @@ html/man3/UI_new.html \
html/man3/X509V3_get_d2i.html \
html/man3/X509V3_set_ctx.html \
html/man3/X509_ALGOR_dup.html \
html/man3/X509_ATTRIBUTE.html \
html/man3/X509_CRL_get0_by_serial.html \
html/man3/X509_EXTENSION_set_object.html \
html/man3/X509_LOOKUP.html \
Expand All @@ -3438,6 +3457,7 @@ html/man3/X509_NAME_get0_der.html \
html/man3/X509_NAME_get_index_by_NID.html \
html/man3/X509_NAME_print_ex.html \
html/man3/X509_PUBKEY_new.html \
html/man3/X509_REQ_get_attr.html \
html/man3/X509_SIG_get0.html \
html/man3/X509_STORE_CTX_get_error.html \
html/man3/X509_STORE_CTX_new.html \
Expand Down Expand Up @@ -3577,6 +3597,7 @@ man/man3/CMS_get0_type.3 \
man/man3/CMS_get1_ReceiptRequest.3 \
man/man3/CMS_sign.3 \
man/man3/CMS_sign_receipt.3 \
man/man3/CMS_signed_get_attr.3 \
man/man3/CMS_uncompress.3 \
man/man3/CMS_verify.3 \
man/man3/CMS_verify_receipt.3 \
Expand Down Expand Up @@ -3676,6 +3697,7 @@ man/man3/EVP_PKEY_digestsign_supports_digest.3 \
man/man3/EVP_PKEY_encapsulate.3 \
man/man3/EVP_PKEY_encrypt.3 \
man/man3/EVP_PKEY_fromdata.3 \
man/man3/EVP_PKEY_get_attr.3 \
man/man3/EVP_PKEY_get_default_digest_nid.3 \
man/man3/EVP_PKEY_get_field_type.3 \
man/man3/EVP_PKEY_get_group_name.3 \
Expand Down Expand Up @@ -4030,6 +4052,7 @@ man/man3/UI_new.3 \
man/man3/X509V3_get_d2i.3 \
man/man3/X509V3_set_ctx.3 \
man/man3/X509_ALGOR_dup.3 \
man/man3/X509_ATTRIBUTE.3 \
man/man3/X509_CRL_get0_by_serial.3 \
man/man3/X509_EXTENSION_set_object.3 \
man/man3/X509_LOOKUP.3 \
Expand All @@ -4041,6 +4064,7 @@ man/man3/X509_NAME_get0_der.3 \
man/man3/X509_NAME_get_index_by_NID.3 \
man/man3/X509_NAME_print_ex.3 \
man/man3/X509_PUBKEY_new.3 \
man/man3/X509_REQ_get_attr.3 \
man/man3/X509_SIG_get0.3 \
man/man3/X509_STORE_CTX_get_error.3 \
man/man3/X509_STORE_CTX_new.3 \
Expand Down
214 changes: 214 additions & 0 deletions doc/man3/CMS_signed_get_attr.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
=pod

=head1 NAME

CMS_signed_get_attr_count,
CMS_signed_get_attr_by_NID, CMS_signed_get_attr_by_OBJ, CMS_signed_get_attr,
CMS_signed_delete_attr,
CMS_signed_add1_attr, CMS_signed_add1_attr_by_OBJ,
CMS_signed_add1_attr_by_NID, CMS_signed_add1_attr_by_txt,
CMS_signed_get0_data_by_OBJ,
CMS_unsigned_get_attr_count,
CMS_unsigned_get_attr_by_NID, CMS_unsigned_get_attr_by_OBJ,
CMS_unsigned_get_attr, CMS_unsigned_delete_attr,
CMS_unsigned_add1_attr, CMS_unsigned_add1_attr_by_OBJ,
CMS_unsigned_add1_attr_by_NID, CMS_unsigned_add1_attr_by_txt,
CMS_unsigned_get0_data_by_OBJ
- CMS signed and unsigned attribute functions

=head1 SYNOPSIS

#include <openssl/cms.h>

int CMS_signed_get_attr_count(const CMS_SignerInfo *si);
int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
int lastpos);
int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
int lastpos);
X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc);
X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc);
int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr);
int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
const ASN1_OBJECT *obj, int type,
const void *bytes, int len);
int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
int nid, int type,
const void *bytes, int len);
int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
const char *attrname, int type,
const void *bytes, int len);
void *CMS_signed_get0_data_by_OBJ(const CMS_SignerInfo *si,
const ASN1_OBJECT *oid,
int lastpos, int type);

int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si);
int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
int lastpos);
int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
const ASN1_OBJECT *obj, int lastpos);
X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc);
X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc);
int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr);
int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
const ASN1_OBJECT *obj, int type,
const void *bytes, int len);
int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
int nid, int type,
const void *bytes, int len);
int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
const char *attrname, int type,
const void *bytes, int len);
void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
int lastpos, int type);

=head1 DESCRIPTION

CMS_signerInfo contains separate attribute lists for signed and unsigned
attributes. Each CMS_signed_XXX() function is used for signed attributes, and
each CMS_unsigned_XXX() function is used for unsigned attributes.
Since the CMS_unsigned_XXX() functions work in the same way as the
CMS_signed_XXX() equivalents, only the CMS_signed_XXX() functions are
described below.

CMS_signed_get_attr_by_OBJ() finds the location of the first matching object
I<obj> in the SignerInfo's I<si> signed attribute list. The search starts at the
position after I<lastpos>. If the returned value is positive then it can be used
on the next call to CMS_signed_get_attr_by_OBJ() as the value of I<lastpos> in
order to iterate through the remaining attributes. I<lastpos> can be set to any
negative value on the first call, in order to start searching from the start of
the signed attribute list.

CMS_signed_get_attr_by_NID() is similar to CMS_signed_get_attr_by_OBJ() except
that it passes the numerical identifier (NID) I<nid> associated with the object.
See <openssl/obj_mac.h> for a list of NID_*.

CMS_signed_get_attr() returns the B<X509_ATTRIBUTE> object at index I<loc> in the
I<si> signed attribute list. I<loc> should be in the range from 0 to
CMS_signed_get_attr_count() - 1.

CMS_signed_delete_attr() removes the B<X509_ATTRIBUTE> object at index I<loc> in
the I<si> signed attribute list. An error occurs if the I<si> attribute list
is NULL.

CMS_signed_add1_attr() pushes a copy of the passed in B<X509_ATTRIBUTE> object
to the I<si> signed attribute list. A new signed attribute list is created if
required. An error occurs if I<attr> is NULL.

CMS_signed_add1_attr_by_OBJ() creates a new signed B<X509_ATTRIBUTE> using
X509_ATTRIBUTE_set1_object() and X509_ATTRIBUTE_set1_data() to assign a new
I<obj> with type I<type> and data I<bytes> of length I<len> and then pushes it
to the I<key> object's attribute list.

CMS_signed_add1_attr_by_NID() is similar to CMS_signed_add1_attr_by_OBJ() except
that it passes the numerical identifier (NID) I<nid> associated with the object.
See <openssl/obj_mac.h> for a list of NID_*.

CMS_signed_add1_attr_by_txt() is similar to CMS_signed_add1_attr_by_OBJ()
except that it passes a name I<attrname> associated with the object.
See <openssl/obj_mac.h> for a list of SN_* names.

CMS_signed_get0_data_by_OBJ() finds the first attribute in a I<si> signed
attributes list that matches the I<obj> starting at index I<lastpos>
and returns the data retrieved from the found attributes first B<ASN1_TYPE>
object. An error will occur if the attribute type I<type> does not match the
type of the B<ASN1_TYPE> object OR if I<type> is either B<V_ASN1_BOOLEAN> or
B<V_ASN1_NULL> OR the attribute is not found.
If I<lastpos> is less than -1 then an error will occur if there are multiple
objects in the signed attribute list that match I<obj>.
If I<lastpos> is less than -2 then an error will occur if there is more than
one B<ASN1_TYPE> object in the found signed attribute.

Refer to L<X509_ATTRIBUTE(3)> for information related to attributes.

=head1 RETURN VALUES

The CMS_unsigned_XXX() functions return values are similar to those of the
equivalent CMS_signed_XXX() functions.

CMS_signed_get_attr_count() returns the number of signed attributes in the
SignerInfo I<si>, or -1 if the signed attribute list is NULL.

CMS_signed_get_attr_by_OBJ() returns -1 if either the signed attribute list of
I<si> is empty OR if I<obj> is not found, otherwise it returns the location of
the I<obj> in the SignerInfo's I<si> signed attribute list.

CMS_signed_get_attr_by_NID() is similar to CMS_signed_get_attr_by_OBJ() except
that it returns -2 if the I<nid> is not known by OpenSSL.

CMS_signed_get_attr() returns either a signed B<X509_ATTRIBUTE> or NULL on error.

CMS_signed_delete_attr() returns either the removed signed B<X509_ATTRIBUTE> or
NULL if there is a error.

CMS_signed_add1_attr(), CMS_signed_add1_attr_by_OBJ(),
CMS_signed_add1_attr_by_NID(), CMS_signed_add1_attr_by_txt(),
return 1 on success or 0 on error.

CMS_signed_get0_data_by_OBJ() returns the data retrieved from the found
signed attributes first B<ASN1_TYPE> object, or NULL if an error occurs.

=head1 NOTES

Some attributes are added automatically during the signing process.

Calling CMS_SignerInfo_sign() adds the NID_pkcs9_signingTime signed
attribute.

Calling CMS_final(), CMS_final_digest() or CMS_dataFinal() adds the
NID_pkcs9_messageDigest signed attribute.

The NID_pkcs9_contentType signed attribute is always added if the
NID_pkcs9_signingTime attribute is added.

Calling CMS_sign_ex(), CMS_sign_receipt() or CMS_add1_signer() may add
attributes depending on the flags parameter. See L<CMS_add1_signer(3)> for
more information.

OpenSSL applies special rules for the following attribute NIDs:

=over 4

=item CMS Signed Attributes

NID_pkcs9_contentType
NID_pkcs9_messageDigest
NID_pkcs9_signingTime

=item ESS Signed Attributes

NID_id_smime_aa_signingCertificate
NID_id_smime_aa_signingCertificateV2
NID_id_smime_aa_receiptRequest

=item CMS Unsigned Attributes

NID_pkcs9_countersignature

=back

CMS_signed_add1_attr(), CMS_signed_add1_attr_by_OBJ(),
CMS_signed_add1_attr_by_NID(), CMS_signed_add1_attr_by_txt()
and the equivalent CMS_unsigned_add1_attrXXX() functions allow
duplicate attributes to be added. The attribute rules are not checked
during these function calls, and are deferred until the sign or verify process
(i.e. during calls to any of CMS_sign_ex(), CMS_sign(), CMS_sign_receipt(),
CMS_add1_signer(), CMS_Final(), CMS_dataFinal(), CMS_final_digest(),
CMS_verify(), CMS_verify_receipt() or CMS_SignedData_verify()).

For CMS attribute rules see RFC 5652 Section 11.
For ESS attribute rules see RFC 2634 Section 1.3.4 and RFC 5035 Section 5.4.

=head1 SEE ALSO

L<X509_ATTRIBUTE(3)>

=head1 COPYRIGHT

Copyright 2023 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
L<https://www.openssl.org/source/license.html>.

=cut
Loading

0 comments on commit 9ee889f

Please sign in to comment.