xref: /openbsd/lib/libcrypto/x509/x509_att.c (revision 4bdff4be)
1 /* $OpenBSD: x509_att.c,v 1.22 2023/02/16 08:38:17 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/err.h>
63 #include <openssl/evp.h>
64 #include <openssl/objects.h>
65 #include <openssl/stack.h>
66 #include <openssl/x509.h>
67 #include <openssl/x509v3.h>
68 
69 #include "x509_local.h"
70 
71 int
72 X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
73 {
74 	return sk_X509_ATTRIBUTE_num(x);
75 }
76 LCRYPTO_ALIAS(X509at_get_attr_count);
77 
78 int
79 X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid, int lastpos)
80 {
81 	ASN1_OBJECT *obj;
82 
83 	obj = OBJ_nid2obj(nid);
84 	if (obj == NULL)
85 		return (-2);
86 	return (X509at_get_attr_by_OBJ(x, obj, lastpos));
87 }
88 LCRYPTO_ALIAS(X509at_get_attr_by_NID);
89 
90 int
91 X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
92     const ASN1_OBJECT *obj, int lastpos)
93 {
94 	int n;
95 	X509_ATTRIBUTE *ex;
96 
97 	if (sk == NULL)
98 		return (-1);
99 	lastpos++;
100 	if (lastpos < 0)
101 		lastpos = 0;
102 	n = sk_X509_ATTRIBUTE_num(sk);
103 	for (; lastpos < n; lastpos++) {
104 		ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
105 		if (OBJ_cmp(ex->object, obj) == 0)
106 			return (lastpos);
107 	}
108 	return (-1);
109 }
110 LCRYPTO_ALIAS(X509at_get_attr_by_OBJ);
111 
112 X509_ATTRIBUTE *
113 X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
114 {
115 	if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
116 		return NULL;
117 	else
118 		return sk_X509_ATTRIBUTE_value(x, loc);
119 }
120 LCRYPTO_ALIAS(X509at_get_attr);
121 
122 X509_ATTRIBUTE *
123 X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
124 {
125 	X509_ATTRIBUTE *ret;
126 
127 	if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
128 		return (NULL);
129 	ret = sk_X509_ATTRIBUTE_delete(x, loc);
130 	return (ret);
131 }
132 LCRYPTO_ALIAS(X509at_delete_attr);
133 
134 STACK_OF(X509_ATTRIBUTE) *
135 X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x, X509_ATTRIBUTE *attr)
136 {
137 	X509_ATTRIBUTE *new_attr = NULL;
138 	STACK_OF(X509_ATTRIBUTE) *sk = NULL;
139 
140 	if (x == NULL) {
141 		X509error(ERR_R_PASSED_NULL_PARAMETER);
142 		return (NULL);
143 	}
144 
145 	if (*x == NULL) {
146 		if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
147 			goto err;
148 	} else
149 		sk = *x;
150 
151 	if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
152 		goto err2;
153 	if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
154 		goto err;
155 	if (*x == NULL)
156 		*x = sk;
157 	return (sk);
158 
159 err:
160 	X509error(ERR_R_MALLOC_FAILURE);
161 err2:
162 	if (new_attr != NULL)
163 		X509_ATTRIBUTE_free(new_attr);
164 	if (sk != NULL && sk != *x)
165 		sk_X509_ATTRIBUTE_free(sk);
166 	return (NULL);
167 }
168 LCRYPTO_ALIAS(X509at_add1_attr);
169 
170 STACK_OF(X509_ATTRIBUTE) *
171 X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x, const ASN1_OBJECT *obj,
172     int type, const unsigned char *bytes, int len)
173 {
174 	X509_ATTRIBUTE *attr;
175 	STACK_OF(X509_ATTRIBUTE) *ret;
176 
177 	attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
178 	if (!attr)
179 		return 0;
180 	ret = X509at_add1_attr(x, attr);
181 	X509_ATTRIBUTE_free(attr);
182 	return ret;
183 }
184 LCRYPTO_ALIAS(X509at_add1_attr_by_OBJ);
185 
186 STACK_OF(X509_ATTRIBUTE) *
187 X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x, int nid, int type,
188     const unsigned char *bytes, int len)
189 {
190 	X509_ATTRIBUTE *attr;
191 	STACK_OF(X509_ATTRIBUTE) *ret;
192 
193 	attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
194 	if (!attr)
195 		return 0;
196 	ret = X509at_add1_attr(x, attr);
197 	X509_ATTRIBUTE_free(attr);
198 	return ret;
199 }
200 LCRYPTO_ALIAS(X509at_add1_attr_by_NID);
201 
202 STACK_OF(X509_ATTRIBUTE) *
203 X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x, const char *attrname,
204     int type, const unsigned char *bytes, int len)
205 {
206 	X509_ATTRIBUTE *attr;
207 	STACK_OF(X509_ATTRIBUTE) *ret;
208 
209 	attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
210 	if (!attr)
211 		return 0;
212 	ret = X509at_add1_attr(x, attr);
213 	X509_ATTRIBUTE_free(attr);
214 	return ret;
215 }
216 LCRYPTO_ALIAS(X509at_add1_attr_by_txt);
217 
218 void *
219 X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x, const ASN1_OBJECT *obj,
220     int lastpos, int type)
221 {
222 	int i;
223 	X509_ATTRIBUTE *at;
224 
225 	i = X509at_get_attr_by_OBJ(x, obj, lastpos);
226 	if (i == -1)
227 		return NULL;
228 	if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
229 		return NULL;
230 	at = X509at_get_attr(x, i);
231 	if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
232 		return NULL;
233 	return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
234 }
235 LCRYPTO_ALIAS(X509at_get0_data_by_OBJ);
236 
237 X509_ATTRIBUTE *
238 X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid, int atrtype,
239     const void *data, int len)
240 {
241 	ASN1_OBJECT *obj;
242 	X509_ATTRIBUTE *ret;
243 
244 	obj = OBJ_nid2obj(nid);
245 	if (obj == NULL) {
246 		X509error(X509_R_UNKNOWN_NID);
247 		return (NULL);
248 	}
249 	ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
250 	if (ret == NULL)
251 		ASN1_OBJECT_free(obj);
252 	return (ret);
253 }
254 LCRYPTO_ALIAS(X509_ATTRIBUTE_create_by_NID);
255 
256 X509_ATTRIBUTE *
257 X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr, const ASN1_OBJECT *obj,
258     int atrtype, const void *data, int len)
259 {
260 	X509_ATTRIBUTE *ret;
261 
262 	if ((attr == NULL) || (*attr == NULL)) {
263 		if ((ret = X509_ATTRIBUTE_new()) == NULL) {
264 			X509error(ERR_R_MALLOC_FAILURE);
265 			return (NULL);
266 		}
267 	} else
268 		ret= *attr;
269 
270 	if (!X509_ATTRIBUTE_set1_object(ret, obj))
271 		goto err;
272 	if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
273 		goto err;
274 
275 	if ((attr != NULL) && (*attr == NULL))
276 		*attr = ret;
277 	return (ret);
278 
279 err:
280 	if ((attr == NULL) || (ret != *attr))
281 		X509_ATTRIBUTE_free(ret);
282 	return (NULL);
283 }
284 LCRYPTO_ALIAS(X509_ATTRIBUTE_create_by_OBJ);
285 
286 X509_ATTRIBUTE *
287 X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr, const char *atrname,
288     int type, const unsigned char *bytes, int len)
289 {
290 	ASN1_OBJECT *obj;
291 	X509_ATTRIBUTE *nattr;
292 
293 	obj = OBJ_txt2obj(atrname, 0);
294 	if (obj == NULL) {
295 		X509error(X509_R_INVALID_FIELD_NAME);
296 		ERR_asprintf_error_data("name=%s", atrname);
297 		return (NULL);
298 	}
299 	nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
300 	ASN1_OBJECT_free(obj);
301 	return nattr;
302 }
303 LCRYPTO_ALIAS(X509_ATTRIBUTE_create_by_txt);
304 
305 int
306 X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
307 {
308 	if ((attr == NULL) || (obj == NULL))
309 		return (0);
310 	ASN1_OBJECT_free(attr->object);
311 	attr->object = OBJ_dup(obj);
312 	return attr->object != NULL;
313 }
314 LCRYPTO_ALIAS(X509_ATTRIBUTE_set1_object);
315 
316 int
317 X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data,
318     int len)
319 {
320 	ASN1_TYPE *ttmp = NULL;
321 	ASN1_STRING *stmp = NULL;
322 	int atype = 0;
323 
324 	if (!attr)
325 		return 0;
326 	if (attrtype & MBSTRING_FLAG) {
327 		stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
328 		    OBJ_obj2nid(attr->object));
329 		if (!stmp) {
330 			X509error(ERR_R_ASN1_LIB);
331 			return 0;
332 		}
333 		atype = stmp->type;
334 	} else if (len != -1){
335 		if (!(stmp = ASN1_STRING_type_new(attrtype)))
336 			goto err;
337 		if (!ASN1_STRING_set(stmp, data, len))
338 			goto err;
339 		atype = attrtype;
340 	}
341 	/*
342 	 * This is a bit naughty because the attribute should really have
343 	 * at least one value but some types use and zero length SET and
344 	 * require this.
345 	 */
346 	if (attrtype == 0) {
347 		ASN1_STRING_free(stmp);
348 		return 1;
349 	}
350 
351 	if (!(ttmp = ASN1_TYPE_new()))
352 		goto err;
353 	if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
354 		if (!ASN1_TYPE_set1(ttmp, attrtype, data))
355 			goto err;
356 	} else
357 		ASN1_TYPE_set(ttmp, atype, stmp);
358 	if (!sk_ASN1_TYPE_push(attr->set, ttmp))
359 		goto err;
360 	return 1;
361 
362 err:
363 	ASN1_TYPE_free(ttmp);
364 	ASN1_STRING_free(stmp);
365 	X509error(ERR_R_MALLOC_FAILURE);
366 	return 0;
367 }
368 LCRYPTO_ALIAS(X509_ATTRIBUTE_set1_data);
369 
370 int
371 X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
372 {
373 	if (attr == NULL)
374 		return 0;
375 
376 	return sk_ASN1_TYPE_num(attr->set);
377 }
378 LCRYPTO_ALIAS(X509_ATTRIBUTE_count);
379 
380 ASN1_OBJECT *
381 X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
382 {
383 	if (attr == NULL)
384 		return (NULL);
385 	return (attr->object);
386 }
387 LCRYPTO_ALIAS(X509_ATTRIBUTE_get0_object);
388 
389 void *
390 X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int atrtype, void *data)
391 {
392 	ASN1_TYPE *ttmp;
393 
394 	ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
395 	if (!ttmp)
396 		return NULL;
397 	if (atrtype != ASN1_TYPE_get(ttmp)){
398 		X509error(X509_R_WRONG_TYPE);
399 		return NULL;
400 	}
401 	return ttmp->value.ptr;
402 }
403 LCRYPTO_ALIAS(X509_ATTRIBUTE_get0_data);
404 
405 ASN1_TYPE *
406 X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
407 {
408 	if (attr == NULL)
409 		return (NULL);
410 
411 	return sk_ASN1_TYPE_value(attr->set, idx);
412 }
413 LCRYPTO_ALIAS(X509_ATTRIBUTE_get0_type);
414