Debian Codegraph @

external/boringssl/crypto/x509/x509name.c

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