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