xref: /openbsd-src/lib/libcrypto/x509/x509name.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: x509name.c,v 1.12 2014/07/11 08:44:49 jsing 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 #include <string.h>
61 
62 #include <openssl/asn1.h>
63 #include <openssl/err.h>
64 #include <openssl/evp.h>
65 #include <openssl/objects.h>
66 #include <openssl/stack.h>
67 #include <openssl/x509.h>
68 
69 int
70 X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len)
71 {
72 	ASN1_OBJECT *obj;
73 
74 	obj = OBJ_nid2obj(nid);
75 	if (obj == NULL)
76 		return (-1);
77 	return (X509_NAME_get_text_by_OBJ(name, obj, buf, len));
78 }
79 
80 int
81 X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, char *buf,
82     int len)
83 {
84 	int i;
85 	ASN1_STRING *data;
86 
87 	i = X509_NAME_get_index_by_OBJ(name, obj, -1);
88 	if (i < 0)
89 		return (-1);
90 	data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
91 	i = (data->length > (len - 1)) ? (len - 1) : data->length;
92 	if (buf == NULL)
93 		return (data->length);
94 	memcpy(buf, data->data, i);
95 	buf[i] = '\0';
96 	return (i);
97 }
98 
99 int
100 X509_NAME_entry_count(X509_NAME *name)
101 {
102 	if (name == NULL)
103 		return (0);
104 	return (sk_X509_NAME_ENTRY_num(name->entries));
105 }
106 
107 int
108 X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos)
109 {
110 	ASN1_OBJECT *obj;
111 
112 	obj = OBJ_nid2obj(nid);
113 	if (obj == NULL)
114 		return (-2);
115 	return (X509_NAME_get_index_by_OBJ(name, obj, lastpos));
116 }
117 
118 /* NOTE: you should be passsing -1, not 0 as lastpos */
119 int
120 X509_NAME_get_index_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int lastpos)
121 {
122 	int n;
123 	X509_NAME_ENTRY *ne;
124 	STACK_OF(X509_NAME_ENTRY) *sk;
125 
126 	if (name == NULL)
127 		return (-1);
128 	if (lastpos < 0)
129 		lastpos = -1;
130 	sk = name->entries;
131 	n = sk_X509_NAME_ENTRY_num(sk);
132 	for (lastpos++; lastpos < n; lastpos++) {
133 		ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
134 		if (OBJ_cmp(ne->object, obj) == 0)
135 			return (lastpos);
136 	}
137 	return (-1);
138 }
139 
140 X509_NAME_ENTRY *
141 X509_NAME_get_entry(X509_NAME *name, int loc)
142 {
143 	if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc ||
144 	    loc < 0)
145 		return (NULL);
146 	else
147 		return (sk_X509_NAME_ENTRY_value(name->entries, loc));
148 }
149 
150 X509_NAME_ENTRY *
151 X509_NAME_delete_entry(X509_NAME *name, int loc)
152 {
153 	X509_NAME_ENTRY *ret;
154 	int i, n, set_prev, set_next;
155 	STACK_OF(X509_NAME_ENTRY) *sk;
156 
157 	if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc ||
158 	    loc < 0)
159 		return (NULL);
160 	sk = name->entries;
161 	ret = sk_X509_NAME_ENTRY_delete(sk, loc);
162 	n = sk_X509_NAME_ENTRY_num(sk);
163 	name->modified = 1;
164 	if (loc == n)
165 		return (ret);
166 
167 	/* else we need to fixup the set field */
168 	if (loc != 0)
169 		set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
170 	else
171 		set_prev = ret->set - 1;
172 	set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
173 
174 	/* set_prev is the previous set
175 	 * set is the current set
176 	 * set_next is the following
177 	 * prev  1 1	1 1	1 1	1 1
178 	 * set   1	1	2	2
179 	 * next  1 1	2 2	2 2	3 2
180 	 * so basically only if prev and next differ by 2, then
181 	 * re-number down by 1 */
182 	if (set_prev + 1 < set_next)
183 		for (i = loc; i < n; i++)
184 			sk_X509_NAME_ENTRY_value(sk, i)->set--;
185 	return (ret);
186 }
187 
188 int
189 X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
190     unsigned char *bytes, int len, int loc, int set)
191 {
192 	X509_NAME_ENTRY *ne;
193 	int ret;
194 
195 	ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
196 	if (!ne)
197 		return 0;
198 	ret = X509_NAME_add_entry(name, ne, loc, set);
199 	X509_NAME_ENTRY_free(ne);
200 	return ret;
201 }
202 
203 int
204 X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
205     unsigned char *bytes, int len, int loc, int set)
206 {
207 	X509_NAME_ENTRY *ne;
208 	int ret;
209 
210 	ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
211 	if (!ne)
212 		return 0;
213 	ret = X509_NAME_add_entry(name, ne, loc, set);
214 	X509_NAME_ENTRY_free(ne);
215 	return ret;
216 }
217 
218 int
219 X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
220     const unsigned char *bytes, int len, int loc, int set)
221 {
222 	X509_NAME_ENTRY *ne;
223 	int ret;
224 
225 	ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
226 	if (!ne)
227 		return 0;
228 	ret = X509_NAME_add_entry(name, ne, loc, set);
229 	X509_NAME_ENTRY_free(ne);
230 	return ret;
231 }
232 
233 /* if set is -1, append to previous set, 0 'a new one', and 1,
234  * prepend to the guy we are about to stomp on. */
235 int
236 X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc, int set)
237 {
238 	X509_NAME_ENTRY *new_name = NULL;
239 	int n, i, inc;
240 	STACK_OF(X509_NAME_ENTRY) *sk;
241 
242 	if (name == NULL)
243 		return (0);
244 	sk = name->entries;
245 	n = sk_X509_NAME_ENTRY_num(sk);
246 	if (loc > n)
247 		loc = n;
248 	else if (loc < 0)
249 		loc = n;
250 
251 	name->modified = 1;
252 
253 	if (set == -1) {
254 		if (loc == 0) {
255 			set = 0;
256 			inc = 1;
257 		} else {
258 			set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
259 			inc = 0;
260 		}
261 	} else /* if (set >= 0) */ {
262 		if (loc >= n) {
263 			if (loc != 0)
264 				set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1;
265 			else
266 				set = 0;
267 		} else
268 			set = sk_X509_NAME_ENTRY_value(sk, loc)->set;
269 		inc = (set == 0) ? 1 : 0;
270 	}
271 
272 	if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL)
273 		goto err;
274 	new_name->set = set;
275 	if (!sk_X509_NAME_ENTRY_insert(sk, new_name, loc)) {
276 		X509err(X509_F_X509_NAME_ADD_ENTRY, ERR_R_MALLOC_FAILURE);
277 		goto err;
278 	}
279 	if (inc) {
280 		n = sk_X509_NAME_ENTRY_num(sk);
281 		for (i = loc + 1; i < n; i++)
282 			sk_X509_NAME_ENTRY_value(sk, i - 1)->set += 1;
283 	}
284 	return (1);
285 
286 err:
287 	if (new_name != NULL)
288 		X509_NAME_ENTRY_free(new_name);
289 	return (0);
290 }
291 
292 X509_NAME_ENTRY *
293 X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
294     const char *field, int type, const unsigned char *bytes, int len)
295 {
296 	ASN1_OBJECT *obj;
297 	X509_NAME_ENTRY *nentry;
298 
299 	obj = OBJ_txt2obj(field, 0);
300 	if (obj == NULL) {
301 		X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_TXT,
302 		    X509_R_INVALID_FIELD_NAME);
303 		ERR_asprintf_error_data("name=%s", field);
304 		return (NULL);
305 	}
306 	nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
307 	ASN1_OBJECT_free(obj);
308 	return nentry;
309 }
310 
311 X509_NAME_ENTRY *
312 X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid, int type,
313     unsigned char *bytes, int len)
314 {
315 	ASN1_OBJECT *obj;
316 	X509_NAME_ENTRY *nentry;
317 
318 	obj = OBJ_nid2obj(nid);
319 	if (obj == NULL) {
320 		X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,
321 		    X509_R_UNKNOWN_NID);
322 		return (NULL);
323 	}
324 	nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
325 	ASN1_OBJECT_free(obj);
326 	return nentry;
327 }
328 
329 X509_NAME_ENTRY *
330 X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne, ASN1_OBJECT *obj, int type,
331     const unsigned char *bytes, int len)
332 {
333 	X509_NAME_ENTRY *ret;
334 
335 	if ((ne == NULL) || (*ne == NULL)) {
336 		if ((ret = X509_NAME_ENTRY_new()) == NULL)
337 			return (NULL);
338 	} else
339 		ret= *ne;
340 
341 	if (!X509_NAME_ENTRY_set_object(ret, obj))
342 		goto err;
343 	if (!X509_NAME_ENTRY_set_data(ret, type, bytes, len))
344 		goto err;
345 
346 	if ((ne != NULL) && (*ne == NULL))
347 		*ne = ret;
348 	return (ret);
349 
350 err:
351 	if ((ne == NULL) || (ret != *ne))
352 		X509_NAME_ENTRY_free(ret);
353 	return (NULL);
354 }
355 
356 int
357 X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, ASN1_OBJECT *obj)
358 {
359 	if ((ne == NULL) || (obj == NULL)) {
360 		X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,
361 		    ERR_R_PASSED_NULL_PARAMETER);
362 		return (0);
363 	}
364 	ASN1_OBJECT_free(ne->object);
365 	ne->object = OBJ_dup(obj);
366 	return ((ne->object == NULL) ? 0 : 1);
367 }
368 
369 int
370 X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
371     const unsigned char *bytes, int len)
372 {
373 	int i;
374 
375 	if ((ne == NULL) || ((bytes == NULL) && (len != 0)))
376 		return (0);
377 	if ((type > 0) && (type & MBSTRING_FLAG))
378 		return ASN1_STRING_set_by_NID(&ne->value, bytes, len, type,
379 		    OBJ_obj2nid(ne->object)) ? 1 : 0;
380 	if (len < 0)
381 		len = strlen((const char *)bytes);
382 	i = ASN1_STRING_set(ne->value, bytes, len);
383 	if (!i)
384 		return (0);
385 	if (type != V_ASN1_UNDEF) {
386 		if (type == V_ASN1_APP_CHOOSE)
387 			ne->value->type = ASN1_PRINTABLE_type(bytes, len);
388 		else
389 			ne->value->type = type;
390 	}
391 	return (1);
392 }
393 
394 ASN1_OBJECT *
395 X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne)
396 {
397 	if (ne == NULL)
398 		return (NULL);
399 	return (ne->object);
400 }
401 
402 ASN1_STRING *
403 X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne)
404 {
405 	if (ne == NULL)
406 		return (NULL);
407 	return (ne->value);
408 }
409