Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/x509/x509_att.c

raw
1 /*
2 * Copyright 1995-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 "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 const 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_data(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE,
127 "name=%s", OBJ_nid2sn(OBJ_obj2nid(attr->object)));
128 return NULL;
129 }
130
131 return ossl_x509at_add1_attr(x, attr);
132 }
133
134 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x,
135 const ASN1_OBJECT *obj,
136 int type,
137 const unsigned char *bytes,
138 int len)
139 {
140 X509_ATTRIBUTE *attr;
141 STACK_OF(X509_ATTRIBUTE) *ret;
142
143 attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
144 if (attr == NULL)
145 return 0;
146 ret = ossl_x509at_add1_attr(x, attr);
147 X509_ATTRIBUTE_free(attr);
148 return ret;
149 }
150
151 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
152 **x,
153 const ASN1_OBJECT *obj,
154 int type,
155 const unsigned char *bytes,
156 int len)
157 {
158 if (x == NULL || obj == NULL) {
159 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
160 return NULL;
161 }
162 if (*x != NULL && X509at_get_attr_by_OBJ(*x, obj, -1) != -1) {
163 ERR_raise_data(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE,
164 "name=%s", OBJ_nid2sn(OBJ_obj2nid(obj)));
165 return NULL;
166 }
167
168 return ossl_x509at_add1_attr_by_OBJ(x, obj, type, bytes, len);
169 }
170
171 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x,
172 int nid, int type,
173 const unsigned char *bytes,
174 int len)
175 {
176 X509_ATTRIBUTE *attr;
177 STACK_OF(X509_ATTRIBUTE) *ret;
178
179 attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
180 if (attr == NULL)
181 return 0;
182 ret = ossl_x509at_add1_attr(x, attr);
183 X509_ATTRIBUTE_free(attr);
184 return ret;
185 }
186
187 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
188 **x,
189 int nid, int type,
190 const unsigned char *bytes,
191 int len)
192 {
193 if (x == NULL) {
194 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
195 return NULL;
196 }
197 if (*x != NULL && X509at_get_attr_by_NID(*x, nid, -1) != -1) {
198 ERR_raise_data(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE,
199 "name=%s", OBJ_nid2sn(nid));
200 return NULL;
201 }
202
203 return ossl_x509at_add1_attr_by_NID(x, nid, type, bytes, len);
204 }
205
206 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x,
207 const char *attrname,
208 int type,
209 const unsigned char *bytes,
210 int len)
211 {
212 X509_ATTRIBUTE *attr;
213 STACK_OF(X509_ATTRIBUTE) *ret;
214
215 attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
216 if (attr == NULL)
217 return 0;
218 ret = ossl_x509at_add1_attr(x, attr);
219 X509_ATTRIBUTE_free(attr);
220 return ret;
221 }
222
223 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
224 **x,
225 const char *attrname,
226 int type,
227 const unsigned char *bytes,
228 int len)
229 {
230 X509_ATTRIBUTE *attr;
231 STACK_OF(X509_ATTRIBUTE) *ret;
232
233 attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
234 if (attr == NULL)
235 return 0;
236 ret = X509at_add1_attr(x, attr);
237 X509_ATTRIBUTE_free(attr);
238 return ret;
239 }
240
241 void *X509at_get0_data_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *x,
242 const ASN1_OBJECT *obj, int lastpos, int type)
243 {
244 int i = X509at_get_attr_by_OBJ(x, obj, lastpos);
245 X509_ATTRIBUTE *at;
246
247 if (i == -1)
248 return NULL;
249 if (lastpos <= -2 && X509at_get_attr_by_OBJ(x, obj, i) != -1)
250 return NULL;
251 at = X509at_get_attr(x, i);
252 if (lastpos <= -3 && X509_ATTRIBUTE_count(at) != 1)
253 return NULL;
254 return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
255 }
256
257 STACK_OF(X509_ATTRIBUTE) *ossl_x509at_dup(const STACK_OF(X509_ATTRIBUTE) *x)
258 {
259 int i, n = sk_X509_ATTRIBUTE_num(x);
260 STACK_OF(X509_ATTRIBUTE) *sk = NULL;
261
262 for (i = 0; i < n; ++i) {
263 if (X509at_add1_attr(&sk, sk_X509_ATTRIBUTE_value(x, i)) == NULL) {
264 sk_X509_ATTRIBUTE_pop_free(sk, X509_ATTRIBUTE_free);
265 return NULL;
266 }
267 }
268 return sk;
269 }
270
271 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
272 int atrtype, const void *data,
273 int len)
274 {
275 ASN1_OBJECT *obj = OBJ_nid2obj(nid);
276 X509_ATTRIBUTE *ret;
277
278 if (obj == NULL) {
279 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
280 return NULL;
281 }
282 ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
283 if (ret == NULL)
284 ASN1_OBJECT_free(obj);
285 return ret;
286 }
287
288 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
289 const ASN1_OBJECT *obj,
290 int atrtype, const void *data,
291 int len)
292 {
293 X509_ATTRIBUTE *ret;
294
295 if (attr == NULL || *attr == NULL) {
296 if ((ret = X509_ATTRIBUTE_new()) == NULL) {
297 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
298 return NULL;
299 }
300 } else {
301 ret = *attr;
302 }
303
304 if (!X509_ATTRIBUTE_set1_object(ret, obj))
305 goto err;
306 if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
307 goto err;
308
309 if (attr != NULL && *attr == NULL)
310 *attr = ret;
311 return ret;
312 err:
313 if (attr == NULL || ret != *attr)
314 X509_ATTRIBUTE_free(ret);
315 return NULL;
316 }
317
318 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
319 const char *atrname, int type,
320 const unsigned char *bytes,
321 int len)
322 {
323 ASN1_OBJECT *obj = OBJ_txt2obj(atrname, 0);
324 X509_ATTRIBUTE *nattr;
325
326 if (obj == NULL) {
327 ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_FIELD_NAME,
328 "name=%s", atrname);
329 return NULL;
330 }
331 nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
332 ASN1_OBJECT_free(obj);
333 return nattr;
334 }
335
336 int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
337 {
338 if (attr == NULL || obj == NULL) {
339 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
340 return 0;
341 }
342 ASN1_OBJECT_free(attr->object);
343 attr->object = OBJ_dup(obj);
344 return attr->object != NULL;
345 }
346
347 int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
348 const void *data, int len)
349 {
350 ASN1_TYPE *ttmp = NULL;
351 ASN1_STRING *stmp = NULL;
352 int atype = 0;
353
354 if (attr == NULL) {
355 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
356 return 0;
357 }
358 if ((attrtype & MBSTRING_FLAG) != 0) {
359 stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
360 OBJ_obj2nid(attr->object));
361 if (stmp == NULL) {
362 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
363 return 0;
364 }
365 atype = stmp->type;
366 } else if (len != -1) {
367 if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL
368 || !ASN1_STRING_set(stmp, data, len)) {
369 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
370 goto err;
371 }
372 atype = attrtype;
373 }
374 /*
375 * This is a bit naughty because the attribute should really have at
376 * least one value but some types use and zero length SET and require
377 * this.
378 */
379 if (attrtype == 0) {
380 ASN1_STRING_free(stmp);
381 return 1;
382 }
383 if ((ttmp = ASN1_TYPE_new()) == NULL) {
384 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
385 goto err;
386 }
387 if (len == -1 && (attrtype & MBSTRING_FLAG) == 0) {
388 if (!ASN1_TYPE_set1(ttmp, attrtype, data)) {
389 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
390 goto err;
391 }
392 } else {
393 ASN1_TYPE_set(ttmp, atype, stmp);
394 stmp = NULL;
395 }
396 if (!sk_ASN1_TYPE_push(attr->set, ttmp)) {
397 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
398 goto err;
399 }
400 return 1;
401 err:
402 ASN1_TYPE_free(ttmp);
403 ASN1_STRING_free(stmp);
404 return 0;
405 }
406
407 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
408 {
409 if (attr == NULL)
410 return 0;
411 return sk_ASN1_TYPE_num(attr->set);
412 }
413
414 ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
415 {
416 if (attr == NULL) {
417 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
418 return NULL;
419 }
420 return attr->object;
421 }
422
423 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
424 int atrtype, void *data)
425 {
426 ASN1_TYPE *ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
427
428 if (ttmp == NULL)
429 return NULL;
430 if (atrtype == V_ASN1_BOOLEAN
431 || atrtype == V_ASN1_NULL
432 || atrtype != ASN1_TYPE_get(ttmp)) {
433 ERR_raise(ERR_LIB_X509, X509_R_WRONG_TYPE);
434 return NULL;
435 }
436 return ttmp->value.ptr;
437 }
438
439 ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
440 {
441 if (attr == NULL) {
442 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
443 return NULL;
444 }
445 return sk_ASN1_TYPE_value(attr->set, idx);
446 }