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