Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/cms/cms_att.c

raw
1 /*
2 * Copyright 2008-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 <openssl/asn1t.h>
11 #include <openssl/pem.h>
12 #include <openssl/x509v3.h>
13 #include <openssl/err.h>
14 #include <openssl/cms.h>
15 #include "internal/nelem.h"
16 #include "crypto/x509.h"
17 #include "cms_local.h"
18
19 /*-
20 * Attribute flags.
21 * CMS attribute restrictions are discussed in
22 * - RFC 5652 Section 11.
23 * ESS attribute restrictions are discussed in
24 * - RFC 2634 Section 1.3.4 AND
25 * - RFC 5035 Section 5.4
26 */
27 /* This is a signed attribute */
28 #define CMS_ATTR_F_SIGNED 0x01
29 /* This is an unsigned attribute */
30 #define CMS_ATTR_F_UNSIGNED 0x02
31 /* Must be present if there are any other attributes of the same type */
32 #define CMS_ATTR_F_REQUIRED_COND 0x10
33 /* There can only be one instance of this attribute */
34 #define CMS_ATTR_F_ONLY_ONE 0x20
35 /* The Attribute's value must have exactly one entry */
36 #define CMS_ATTR_F_ONE_ATTR_VALUE 0x40
37
38 /* Attributes rules for different attributes */
39 static const struct {
40 int nid; /* The attribute id */
41 int flags;
42 } cms_attribute_properties[] = {
43 /* See RFC Section 11 */
44 { NID_pkcs9_contentType, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE | CMS_ATTR_F_REQUIRED_COND },
45 { NID_pkcs9_messageDigest, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE | CMS_ATTR_F_REQUIRED_COND },
46 { NID_pkcs9_signingTime, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE },
47 { NID_pkcs9_countersignature, CMS_ATTR_F_UNSIGNED },
48 /* ESS */
49 { NID_id_smime_aa_signingCertificate, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE },
50 { NID_id_smime_aa_signingCertificateV2, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE },
51 { NID_id_smime_aa_receiptRequest, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE }
52 };
53
54 /* CMS SignedData Attribute utilities */
55
56 int CMS_signed_get_attr_count(const CMS_SignerInfo *si)
57 {
58 return X509at_get_attr_count(si->signedAttrs);
59 }
60
61 int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
62 {
63 return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos);
64 }
65
66 int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
67 int lastpos)
68 {
69 return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos);
70 }
71
72 X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc)
73 {
74 return X509at_get_attr(si->signedAttrs, loc);
75 }
76
77 X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc)
78 {
79 return X509at_delete_attr(si->signedAttrs, loc);
80 }
81
82 int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
83 {
84 if (ossl_x509at_add1_attr(&si->signedAttrs, attr))
85 return 1;
86 return 0;
87 }
88
89 int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
90 const ASN1_OBJECT *obj, int type,
91 const void *bytes, int len)
92 {
93 if (ossl_x509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len))
94 return 1;
95 return 0;
96 }
97
98 int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
99 int nid, int type, const void *bytes, int len)
100 {
101 if (ossl_x509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len))
102 return 1;
103 return 0;
104 }
105
106 int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
107 const char *attrname, int type,
108 const void *bytes, int len)
109 {
110 if (ossl_x509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes,
111 len))
112 return 1;
113 return 0;
114 }
115
116 void *CMS_signed_get0_data_by_OBJ(const CMS_SignerInfo *si,
117 const ASN1_OBJECT *oid,
118 int lastpos, int type)
119 {
120 return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
121 }
122
123 int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
124 {
125 return X509at_get_attr_count(si->unsignedAttrs);
126 }
127
128 int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
129 int lastpos)
130 {
131 return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
132 }
133
134 int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
135 const ASN1_OBJECT *obj, int lastpos)
136 {
137 return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
138 }
139
140 X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
141 {
142 return X509at_get_attr(si->unsignedAttrs, loc);
143 }
144
145 X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
146 {
147 return X509at_delete_attr(si->unsignedAttrs, loc);
148 }
149
150 int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
151 {
152 if (ossl_x509at_add1_attr(&si->unsignedAttrs, attr))
153 return 1;
154 return 0;
155 }
156
157 int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
158 const ASN1_OBJECT *obj, int type,
159 const void *bytes, int len)
160 {
161 if (ossl_x509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
162 return 1;
163 return 0;
164 }
165
166 int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
167 int nid, int type,
168 const void *bytes, int len)
169 {
170 if (ossl_x509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
171 return 1;
172 return 0;
173 }
174
175 int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
176 const char *attrname, int type,
177 const void *bytes, int len)
178 {
179 if (ossl_x509at_add1_attr_by_txt(&si->unsignedAttrs, attrname,
180 type, bytes, len))
181 return 1;
182 return 0;
183 }
184
185 void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
186 int lastpos, int type)
187 {
188 return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
189 }
190
191 /*
192 * Retrieve an attribute by nid from a stack of attributes starting at index
193 * *lastpos + 1.
194 * Returns the attribute or NULL if there is no attribute.
195 * If an attribute was found *lastpos returns the index of the found attribute.
196 */
197 static X509_ATTRIBUTE *cms_attrib_get(int nid,
198 const STACK_OF(X509_ATTRIBUTE) *attrs,
199 int *lastpos)
200 {
201 X509_ATTRIBUTE *at;
202 int loc;
203
204 loc = X509at_get_attr_by_NID(attrs, nid, *lastpos);
205 if (loc < 0)
206 return NULL;
207
208 at = X509at_get_attr(attrs, loc);
209 *lastpos = loc;
210 return at;
211 }
212
213 static int cms_check_attribute(int nid, int flags, int type,
214 const STACK_OF(X509_ATTRIBUTE) *attrs,
215 int have_attrs)
216 {
217 int lastpos = -1;
218 X509_ATTRIBUTE *at = cms_attrib_get(nid, attrs, &lastpos);
219
220 if (at != NULL) {
221 int count = X509_ATTRIBUTE_count(at);
222
223 /* Is this attribute allowed? */
224 if (((flags & type) == 0)
225 /* check if multiple attributes of the same type are allowed */
226 || (((flags & CMS_ATTR_F_ONLY_ONE) != 0)
227 && cms_attrib_get(nid, attrs, &lastpos) != NULL)
228 /* Check if attribute should have exactly one value in its set */
229 || (((flags & CMS_ATTR_F_ONE_ATTR_VALUE) != 0)
230 && count != 1)
231 /* There should be at least one value */
232 || count == 0)
233 return 0;
234 } else {
235 /* fail if a required attribute is missing */
236 if (have_attrs
237 && ((flags & CMS_ATTR_F_REQUIRED_COND) != 0)
238 && (flags & type) != 0)
239 return 0;
240 }
241 return 1;
242 }
243
244 /*
245 * Check that the signerinfo attributes obey the attribute rules which includes
246 * the following checks
247 * - If any signed attributes exist then there must be a Content Type
248 * and Message Digest attribute in the signed attributes.
249 * - The countersignature attribute is an optional unsigned attribute only.
250 * - Content Type, Message Digest, and Signing time attributes are signed
251 * attributes. Only one instance of each is allowed, with each of these
252 * attributes containing a single attribute value in its set.
253 */
254 int ossl_cms_si_check_attributes(const CMS_SignerInfo *si)
255 {
256 int i;
257 int have_signed_attrs = (CMS_signed_get_attr_count(si) > 0);
258 int have_unsigned_attrs = (CMS_unsigned_get_attr_count(si) > 0);
259
260 for (i = 0; i < (int)OSSL_NELEM(cms_attribute_properties); ++i) {
261 int nid = cms_attribute_properties[i].nid;
262 int flags = cms_attribute_properties[i].flags;
263
264 if (!cms_check_attribute(nid, flags, CMS_ATTR_F_SIGNED,
265 si->signedAttrs, have_signed_attrs)
266 || !cms_check_attribute(nid, flags, CMS_ATTR_F_UNSIGNED,
267 si->unsignedAttrs, have_unsigned_attrs)) {
268 ERR_raise(ERR_LIB_CMS, CMS_R_ATTRIBUTE_ERROR);
269 return 0;
270 }
271 }
272 return 1;
273 }