Allow duplicate CMS attributes
[openssl.git] / crypto / x509 / x509_att.c
1 /*
2  * Copyright 1995-2023 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 "internal/cryptlib.h"
12 #include <openssl/safestack.h>
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include "crypto/x509.h"
19 #include "x509_local.h"
20
21 int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
22 {
23     return sk_X509_ATTRIBUTE_num(x);
24 }
25
26 int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
27                            int lastpos)
28 {
29     const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
30
31     if (obj == NULL)
32         return -2;
33     return X509at_get_attr_by_OBJ(x, obj, lastpos);
34 }
35
36 int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
37                            const ASN1_OBJECT *obj, int lastpos)
38 {
39     int n;
40     X509_ATTRIBUTE *ex;
41
42     if (sk == NULL)
43         return -1;
44     lastpos++;
45     if (lastpos < 0)
46         lastpos = 0;
47     n = sk_X509_ATTRIBUTE_num(sk);
48     for (; lastpos < n; lastpos++) {
49         ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
50         if (OBJ_cmp(ex->object, obj) == 0)
51             return lastpos;
52     }
53     return -1;
54 }
55
56 X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
57 {
58     if (x == NULL) {
59         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
60         return NULL;
61     }
62     if (sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0) {
63         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_INVALID_ARGUMENT);
64         return NULL;
65     }
66     return sk_X509_ATTRIBUTE_value(x, loc);
67 }
68
69 X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
70 {
71     if (x == NULL) {
72         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
73         return NULL;
74     }
75     if (sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0) {
76         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_INVALID_ARGUMENT);
77         return NULL;
78     }
79     return sk_X509_ATTRIBUTE_delete(x, loc);
80 }
81
82 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
83                                                 X509_ATTRIBUTE *attr)
84 {
85     X509_ATTRIBUTE *new_attr = NULL;
86     STACK_OF(X509_ATTRIBUTE) *sk = NULL;
87
88     if (x == NULL || attr == NULL) {
89         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
90         return NULL;
91     }
92
93     if (*x == NULL) {
94         if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL) {
95             ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
96             goto err;
97         }
98     } else {
99         sk = *x;
100     }
101
102     if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
103         goto err;
104     if (!sk_X509_ATTRIBUTE_push(sk, new_attr)) {
105         ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
106         goto err;
107     }
108     if (*x == NULL)
109         *x = sk;
110     return sk;
111  err:
112     X509_ATTRIBUTE_free(new_attr);
113     if (*x == NULL)
114         sk_X509_ATTRIBUTE_free(sk);
115     return NULL;
116 }
117
118 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
119                                            X509_ATTRIBUTE *attr)
120 {
121     if (x == NULL || attr == NULL) {
122         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
123         return NULL;
124     }
125     if (*x != NULL && X509at_get_attr_by_OBJ(*x, attr->object, -1) != -1) {
126         ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
127         return NULL;
128     }
129
130     return ossl_x509at_add1_attr(x, attr);
131 }
132
133 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x,
134                                                        const ASN1_OBJECT *obj,
135                                                        int type,
136                                                        const unsigned char *bytes,
137                                                        int len)
138 {
139     X509_ATTRIBUTE *attr;
140     STACK_OF(X509_ATTRIBUTE) *ret;
141
142     attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
143     if (attr == NULL)
144         return 0;
145     ret = ossl_x509at_add1_attr(x, attr);
146     X509_ATTRIBUTE_free(attr);
147     return ret;
148 }
149
150 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
151                                                   **x, const ASN1_OBJECT *obj,
152                                                   int type,
153                                                   const unsigned char *bytes,
154                                                   int len)
155 {
156     if (x == NULL || obj == NULL) {
157         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
158         return NULL;
159     }
160     if (*x != NULL && X509at_get_attr_by_OBJ(*x, obj, -1) != -1) {
161         ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
162         return NULL;
163     }
164
165     return ossl_x509at_add1_attr_by_OBJ(x, obj, type, bytes, len);
166 }
167
168 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x,
169                                                        int nid, int type,
170                                                        const unsigned char *bytes,
171                                                        int len)
172 {
173     X509_ATTRIBUTE *attr;
174     STACK_OF(X509_ATTRIBUTE) *ret;
175
176     attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
177     if (attr == NULL)
178         return 0;
179     ret = ossl_x509at_add1_attr(x, attr);
180     X509_ATTRIBUTE_free(attr);
181     return ret;
182 }
183
184 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
185                                                   **x, int nid, int type,
186                                                   const unsigned char *bytes,
187                                                   int len)
188 {
189     if (x == NULL) {
190         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
191         return NULL;
192     }
193     if (*x != NULL && X509at_get_attr_by_NID(*x, nid, -1) != -1) {
194         ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
195         return NULL;
196     }
197
198     return ossl_x509at_add1_attr_by_NID(x, nid, type, bytes, len);
199 }
200
201 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x,
202                                                        const char *attrname,
203                                                        int type,
204                                                        const unsigned char *bytes,
205                                                        int len)
206 {
207     X509_ATTRIBUTE *attr;
208     STACK_OF(X509_ATTRIBUTE) *ret;
209
210     attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
211     if (attr == NULL)
212         return 0;
213     ret = ossl_x509at_add1_attr(x, attr);
214     X509_ATTRIBUTE_free(attr);
215     return ret;
216 }
217
218 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
219                                                   **x, const char *attrname,
220                                                   int type,
221                                                   const unsigned char *bytes,
222                                                   int len)
223 {
224     X509_ATTRIBUTE *attr;
225     STACK_OF(X509_ATTRIBUTE) *ret;
226
227     attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
228     if (attr == NULL)
229         return 0;
230     ret = X509at_add1_attr(x, attr);
231     X509_ATTRIBUTE_free(attr);
232     return ret;
233 }
234
235 void *X509at_get0_data_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *x,
236                               const ASN1_OBJECT *obj, int lastpos, int type)
237 {
238     int i = X509at_get_attr_by_OBJ(x, obj, lastpos);
239     X509_ATTRIBUTE *at;
240
241     if (i == -1)
242         return NULL;
243     if (lastpos <= -2 && X509at_get_attr_by_OBJ(x, obj, i) != -1)
244         return NULL;
245     at = X509at_get_attr(x, i);
246     if (lastpos <= -3 && X509_ATTRIBUTE_count(at) != 1)
247         return NULL;
248     return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
249 }
250
251 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_dup(const STACK_OF(X509_ATTRIBUTE) *x)
252 {
253     int i, n = sk_X509_ATTRIBUTE_num(x);
254     STACK_OF(X509_ATTRIBUTE) *sk = NULL;
255
256     for (i = 0; i < n; ++i) {
257         if (X509at_add1_attr(&sk, sk_X509_ATTRIBUTE_value(x, i)) == NULL) {
258             sk_X509_ATTRIBUTE_pop_free(sk, X509_ATTRIBUTE_free);
259             return NULL;
260         }
261     }
262     return sk;
263 }
264
265 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
266                                              int atrtype, const void *data,
267                                              int len)
268 {
269     ASN1_OBJECT *obj = OBJ_nid2obj(nid);
270     X509_ATTRIBUTE *ret;
271
272     if (obj == NULL) {
273         ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
274         return NULL;
275     }
276     ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
277     if (ret == NULL)
278         ASN1_OBJECT_free(obj);
279     return ret;
280 }
281
282 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
283                                              const ASN1_OBJECT *obj,
284                                              int atrtype, const void *data,
285                                              int len)
286 {
287     X509_ATTRIBUTE *ret;
288
289     if (attr == NULL || *attr == NULL) {
290         if ((ret = X509_ATTRIBUTE_new()) == NULL) {
291             ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
292             return NULL;
293         }
294     } else {
295         ret = *attr;
296     }
297
298     if (!X509_ATTRIBUTE_set1_object(ret, obj))
299         goto err;
300     if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
301         goto err;
302
303     if (attr != NULL && *attr == NULL)
304         *attr = ret;
305     return ret;
306  err:
307     if (attr == NULL || ret != *attr)
308         X509_ATTRIBUTE_free(ret);
309     return NULL;
310 }
311
312 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
313                                              const char *atrname, int type,
314                                              const unsigned char *bytes,
315                                              int len)
316 {
317     ASN1_OBJECT *obj = OBJ_txt2obj(atrname, 0);
318     X509_ATTRIBUTE *nattr;
319
320     if (obj == NULL) {
321         ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_FIELD_NAME,
322                        "name=%s", atrname);
323         return NULL;
324     }
325     nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
326     ASN1_OBJECT_free(obj);
327     return nattr;
328 }
329
330 int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
331 {
332     if (attr == NULL || obj == NULL) {
333         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
334         return 0;
335     }
336     ASN1_OBJECT_free(attr->object);
337     attr->object = OBJ_dup(obj);
338     return attr->object != NULL;
339 }
340
341 int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
342                              const void *data, int len)
343 {
344     ASN1_TYPE *ttmp = NULL;
345     ASN1_STRING *stmp = NULL;
346     int atype = 0;
347
348     if (attr == NULL) {
349         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
350         return 0;
351     }
352     if ((attrtype & MBSTRING_FLAG) != 0) {
353         stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
354                                       OBJ_obj2nid(attr->object));
355         if (stmp == NULL) {
356             ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
357             return 0;
358         }
359         atype = stmp->type;
360     } else if (len != -1) {
361         if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL
362             || !ASN1_STRING_set(stmp, data, len)) {
363             ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
364             goto err;
365         }
366         atype = attrtype;
367     }
368     /*
369      * This is a bit naughty because the attribute should really have at
370      * least one value but some types use and zero length SET and require
371      * this.
372      */
373     if (attrtype == 0) {
374         ASN1_STRING_free(stmp);
375         return 1;
376     }
377     if ((ttmp = ASN1_TYPE_new()) == NULL) {
378         ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
379         goto err;
380     }
381     if (len == -1 && (attrtype & MBSTRING_FLAG) == 0) {
382         if (!ASN1_TYPE_set1(ttmp, attrtype, data)) {
383             ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
384             goto err;
385         }
386     } else {
387         ASN1_TYPE_set(ttmp, atype, stmp);
388         stmp = NULL;
389     }
390     if (!sk_ASN1_TYPE_push(attr->set, ttmp)) {
391         ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
392         goto err;
393     }
394     return 1;
395  err:
396     ASN1_TYPE_free(ttmp);
397     ASN1_STRING_free(stmp);
398     return 0;
399 }
400
401 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
402 {
403     if (attr == NULL)
404         return 0;
405     return sk_ASN1_TYPE_num(attr->set);
406 }
407
408 ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
409 {
410     if (attr == NULL) {
411         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
412         return NULL;
413     }
414     return attr->object;
415 }
416
417 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
418                                int atrtype, void *data)
419 {
420     ASN1_TYPE *ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
421
422     if (ttmp == NULL)
423         return NULL;
424     if (atrtype == V_ASN1_BOOLEAN
425             || atrtype == V_ASN1_NULL
426             || atrtype != ASN1_TYPE_get(ttmp)) {
427         ERR_raise(ERR_LIB_X509, X509_R_WRONG_TYPE);
428         return NULL;
429     }
430     return ttmp->value.ptr;
431 }
432
433 ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
434 {
435     if (attr == NULL) {
436         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
437         return NULL;
438     }
439     return sk_ASN1_TYPE_value(attr->set, idx);
440 }