1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <string.h>
58
59 #include <openssl/asn1.h>
60 #include <openssl/err.h>
61 #include <openssl/evp.h>
62 #include <openssl/obj.h>
63 #include <openssl/stack.h>
64 #include <openssl/x509.h>
65
66 #include "../internal.h"
67 #include "internal.h"
68
69
X509_NAME_get_text_by_NID(const X509_NAME * name,int nid,char * buf,int len)70 int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid, char *buf,
71 int len)
72 {
73 const ASN1_OBJECT *obj;
74
75 obj = OBJ_nid2obj(nid);
76 if (obj == NULL)
77 return (-1);
78 return (X509_NAME_get_text_by_OBJ(name, obj, buf, len));
79 }
80
X509_NAME_get_text_by_OBJ(const X509_NAME * name,const ASN1_OBJECT * obj,char * buf,int len)81 int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
82 char *buf, 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 OPENSSL_memcpy(buf, data->data, i);
95 buf[i] = '\0';
96 return (i);
97 }
98
X509_NAME_entry_count(const X509_NAME * name)99 int X509_NAME_entry_count(const X509_NAME *name)
100 {
101 if (name == NULL)
102 return (0);
103 return (sk_X509_NAME_ENTRY_num(name->entries));
104 }
105
X509_NAME_get_index_by_NID(const X509_NAME * name,int nid,int lastpos)106 int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos)
107 {
108 const ASN1_OBJECT *obj;
109
110 obj = OBJ_nid2obj(nid);
111 if (obj == NULL)
112 return (-2);
113 return (X509_NAME_get_index_by_OBJ(name, obj, lastpos));
114 }
115
116 /* NOTE: you should be passsing -1, not 0 as lastpos */
X509_NAME_get_index_by_OBJ(const X509_NAME * name,const ASN1_OBJECT * obj,int lastpos)117 int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
118 int lastpos)
119 {
120 int n;
121 X509_NAME_ENTRY *ne;
122 STACK_OF(X509_NAME_ENTRY) *sk;
123
124 if (name == NULL)
125 return (-1);
126 if (lastpos < 0)
127 lastpos = -1;
128 sk = name->entries;
129 n = sk_X509_NAME_ENTRY_num(sk);
130 for (lastpos++; lastpos < n; lastpos++) {
131 ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
132 if (OBJ_cmp(ne->object, obj) == 0)
133 return (lastpos);
134 }
135 return (-1);
136 }
137
X509_NAME_get_entry(const X509_NAME * name,int loc)138 X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc)
139 {
140 if (name == NULL || loc < 0
141 || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t)loc)
142 return (NULL);
143 else
144 return (sk_X509_NAME_ENTRY_value(name->entries, loc));
145 }
146
X509_NAME_delete_entry(X509_NAME * name,int loc)147 X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc)
148 {
149 X509_NAME_ENTRY *ret;
150 int i, n, set_prev, set_next;
151 STACK_OF(X509_NAME_ENTRY) *sk;
152
153 if (name == NULL || loc < 0
154 || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t)loc)
155 return (NULL);
156 sk = name->entries;
157 ret = sk_X509_NAME_ENTRY_delete(sk, loc);
158 n = sk_X509_NAME_ENTRY_num(sk);
159 name->modified = 1;
160 if (loc == n)
161 return (ret);
162
163 /* else we need to fixup the set field */
164 if (loc != 0)
165 set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
166 else
167 set_prev = ret->set - 1;
168 set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
169
170 /*
171 * set_prev is the previous set set is the current set set_next is the
172 * following prev 1 1 1 1 1 1 1 1 set 1 1 2 2 next 1 1 2 2 2 2 3 2 so
173 * basically only if prev and next differ by 2, then re-number down by 1
174 */
175 if (set_prev + 1 < set_next)
176 for (i = loc; i < n; i++)
177 sk_X509_NAME_ENTRY_value(sk, i)->set--;
178 return (ret);
179 }
180
X509_NAME_add_entry_by_OBJ(X509_NAME * name,ASN1_OBJECT * obj,int type,const unsigned char * bytes,int len,int loc,int set)181 int X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
182 const unsigned char *bytes, int len, int loc,
183 int set)
184 {
185 X509_NAME_ENTRY *ne;
186 int ret;
187 ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
188 if (!ne)
189 return 0;
190 ret = X509_NAME_add_entry(name, ne, loc, set);
191 X509_NAME_ENTRY_free(ne);
192 return ret;
193 }
194
X509_NAME_add_entry_by_NID(X509_NAME * name,int nid,int type,const unsigned char * bytes,int len,int loc,int set)195 int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
196 const unsigned char *bytes, int len, int loc,
197 int set)
198 {
199 X509_NAME_ENTRY *ne;
200 int ret;
201 ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
202 if (!ne)
203 return 0;
204 ret = X509_NAME_add_entry(name, ne, loc, set);
205 X509_NAME_ENTRY_free(ne);
206 return ret;
207 }
208
X509_NAME_add_entry_by_txt(X509_NAME * name,const char * field,int type,const unsigned char * bytes,int len,int loc,int set)209 int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
210 const unsigned char *bytes, int len, int loc,
211 int set)
212 {
213 X509_NAME_ENTRY *ne;
214 int ret;
215 ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
216 if (!ne)
217 return 0;
218 ret = X509_NAME_add_entry(name, ne, loc, set);
219 X509_NAME_ENTRY_free(ne);
220 return ret;
221 }
222
223 /*
224 * if set is -1, append to previous set, 0 'a new one', and 1, prepend to the
225 * guy we are about to stomp on.
226 */
X509_NAME_add_entry(X509_NAME * name,X509_NAME_ENTRY * ne,int loc,int set)227 int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
228 int set)
229 {
230 X509_NAME_ENTRY *new_name = NULL;
231 int n, i, inc;
232 STACK_OF(X509_NAME_ENTRY) *sk;
233
234 if (name == NULL)
235 return (0);
236 sk = name->entries;
237 n = sk_X509_NAME_ENTRY_num(sk);
238 if (loc > n)
239 loc = n;
240 else if (loc < 0)
241 loc = n;
242
243 inc = (set == 0);
244 name->modified = 1;
245
246 if (set == -1) {
247 if (loc == 0) {
248 set = 0;
249 inc = 1;
250 } else {
251 set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
252 }
253 } else { /* if (set >= 0) */
254
255 if (loc >= n) {
256 if (loc != 0)
257 set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1;
258 else
259 set = 0;
260 } else
261 set = sk_X509_NAME_ENTRY_value(sk, loc)->set;
262 }
263
264 if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL)
265 goto err;
266 new_name->set = set;
267 if (!sk_X509_NAME_ENTRY_insert(sk, new_name, loc)) {
268 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
269 goto err;
270 }
271 if (inc) {
272 n = sk_X509_NAME_ENTRY_num(sk);
273 for (i = loc + 1; i < n; i++)
274 sk_X509_NAME_ENTRY_value(sk, i)->set += 1;
275 }
276 return (1);
277 err:
278 if (new_name != NULL)
279 X509_NAME_ENTRY_free(new_name);
280 return (0);
281 }
282
X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY ** ne,const char * field,int type,const unsigned char * bytes,int len)283 X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
284 const char *field, int type,
285 const unsigned char *bytes,
286 int len)
287 {
288 ASN1_OBJECT *obj;
289 X509_NAME_ENTRY *nentry;
290
291 obj = OBJ_txt2obj(field, 0);
292 if (obj == NULL) {
293 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_NAME);
294 ERR_add_error_data(2, "name=", field);
295 return (NULL);
296 }
297 nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
298 ASN1_OBJECT_free(obj);
299 return nentry;
300 }
301
X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY ** ne,int nid,int type,const unsigned char * bytes,int len)302 X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
303 int type,
304 const unsigned char *bytes,
305 int len)
306 {
307 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
308 if (obj == NULL) {
309 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID);
310 return NULL;
311 }
312 return X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
313 }
314
X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY ** ne,const ASN1_OBJECT * obj,int type,const unsigned char * bytes,int len)315 X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
316 const ASN1_OBJECT *obj,
317 int type,
318 const unsigned char *bytes,
319 int len)
320 {
321 X509_NAME_ENTRY *ret;
322
323 if ((ne == NULL) || (*ne == NULL)) {
324 if ((ret = X509_NAME_ENTRY_new()) == NULL)
325 return (NULL);
326 } else
327 ret = *ne;
328
329 if (!X509_NAME_ENTRY_set_object(ret, obj))
330 goto err;
331 if (!X509_NAME_ENTRY_set_data(ret, type, bytes, len))
332 goto err;
333
334 if ((ne != NULL) && (*ne == NULL))
335 *ne = ret;
336 return (ret);
337 err:
338 if ((ne == NULL) || (ret != *ne))
339 X509_NAME_ENTRY_free(ret);
340 return (NULL);
341 }
342
X509_NAME_ENTRY_set_object(X509_NAME_ENTRY * ne,const ASN1_OBJECT * obj)343 int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj)
344 {
345 if ((ne == NULL) || (obj == NULL)) {
346 OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
347 return (0);
348 }
349 ASN1_OBJECT_free(ne->object);
350 ne->object = OBJ_dup(obj);
351 return ((ne->object == NULL) ? 0 : 1);
352 }
353
X509_NAME_ENTRY_set_data(X509_NAME_ENTRY * ne,int type,const unsigned char * bytes,int len)354 int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
355 const unsigned char *bytes, int len)
356 {
357 int i;
358
359 if ((ne == NULL) || ((bytes == NULL) && (len != 0)))
360 return (0);
361 if ((type > 0) && (type & MBSTRING_FLAG))
362 return ASN1_STRING_set_by_NID(&ne->value, bytes,
363 len, type,
364 OBJ_obj2nid(ne->object)) ? 1 : 0;
365 if (len < 0)
366 len = strlen((const char *)bytes);
367 i = ASN1_STRING_set(ne->value, bytes, len);
368 if (!i)
369 return (0);
370 if (type != V_ASN1_UNDEF) {
371 ne->value->type = type;
372 }
373 return (1);
374 }
375
X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY * ne)376 ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne)
377 {
378 if (ne == NULL)
379 return (NULL);
380 return (ne->object);
381 }
382
X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY * ne)383 ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne)
384 {
385 if (ne == NULL)
386 return (NULL);
387 return (ne->value);
388 }
389