xref: /openssl/crypto/pkcs7/pk7_attr.c (revision 7ed6de99)
1 /*
2  * Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <openssl/bio.h>
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/pem.h>
16 #include <openssl/pkcs7.h>
17 #include <openssl/x509.h>
18 #include <openssl/err.h>
19 
PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO * si,STACK_OF (X509_ALGOR)* cap)20 int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
21                               STACK_OF(X509_ALGOR) *cap)
22 {
23     ASN1_STRING *seq;
24 
25     if ((seq = ASN1_STRING_new()) == NULL) {
26         ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
27         return 0;
28     }
29     seq->length = ASN1_item_i2d((ASN1_VALUE *)cap, &seq->data,
30                                 ASN1_ITEM_rptr(X509_ALGORS));
31     if (seq->length <= 0 || seq->data == NULL) {
32         ASN1_STRING_free(seq);
33         return 1;
34     }
35     if (!PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
36                                     V_ASN1_SEQUENCE, seq)) {
37         ASN1_STRING_free(seq);
38         return 0;
39     }
40     return 1;
41 }
42 
STACK_OF(X509_ALGOR)43 STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
44 {
45     ASN1_TYPE *cap;
46     const unsigned char *p;
47 
48     cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
49     if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
50         return NULL;
51     p = cap->value.sequence->data;
52     return (STACK_OF(X509_ALGOR) *)
53         ASN1_item_d2i(NULL, &p, cap->value.sequence->length,
54                       ASN1_ITEM_rptr(X509_ALGORS));
55 }
56 
57 /* Basic smime-capabilities OID and optional integer arg */
PKCS7_simple_smimecap(STACK_OF (X509_ALGOR)* sk,int nid,int arg)58 int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
59 {
60     ASN1_INTEGER *nbit = NULL;
61     X509_ALGOR *alg;
62 
63     if ((alg = X509_ALGOR_new()) == NULL) {
64         ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
65         return 0;
66     }
67     ASN1_OBJECT_free(alg->algorithm);
68     alg->algorithm = OBJ_nid2obj(nid);
69     if (arg > 0) {
70         if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
71             ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
72             goto err;
73         }
74         if ((nbit = ASN1_INTEGER_new()) == NULL) {
75             ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
76             goto err;
77         }
78         if (!ASN1_INTEGER_set(nbit, arg)) {
79             ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
80             goto err;
81         }
82         alg->parameter->value.integer = nbit;
83         alg->parameter->type = V_ASN1_INTEGER;
84         nbit = NULL;
85     }
86     if (!sk_X509_ALGOR_push(sk, alg)) {
87         ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
88         goto err;
89     }
90     return 1;
91 err:
92     ASN1_INTEGER_free(nbit);
93     X509_ALGOR_free(alg);
94     return 0;
95 }
96 
PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO * si,ASN1_OBJECT * coid)97 int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
98 {
99     if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
100         return 0;
101     if (!coid)
102         coid = OBJ_nid2obj(NID_pkcs7_data);
103     return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
104                                       V_ASN1_OBJECT, coid);
105 }
106 
PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO * si,ASN1_TIME * t)107 int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
108 {
109     ASN1_TIME *tmp = NULL;
110 
111     if (t == NULL && (tmp = t = X509_gmtime_adj(NULL, 0)) == NULL) {
112         ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
113         return 0;
114     }
115     if (!PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
116                                     V_ASN1_UTCTIME, t)) {
117         ASN1_TIME_free(tmp);
118         return 0;
119     }
120     return 1;
121 }
122 
PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO * si,const unsigned char * md,int mdlen)123 int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
124                              const unsigned char *md, int mdlen)
125 {
126     ASN1_OCTET_STRING *os;
127     os = ASN1_OCTET_STRING_new();
128     if (os == NULL)
129         return 0;
130     if (!ASN1_STRING_set(os, md, mdlen)
131         || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
132                                        V_ASN1_OCTET_STRING, os)) {
133         ASN1_OCTET_STRING_free(os);
134         return 0;
135     }
136     return 1;
137 }
138