]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
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. | |
0f113f3e | 7 | * |
d02b48c6 RE |
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). | |
0f113f3e | 14 | * |
d02b48c6 RE |
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. | |
0f113f3e | 21 | * |
d02b48c6 RE |
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 :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
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. | |
0f113f3e | 51 | * |
d02b48c6 RE |
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 | ||
58 | #include <stdio.h> | |
450ea834 | 59 | #include <ctype.h> |
b39fc560 | 60 | #include "internal/cryptlib.h" |
9d6b1ce6 | 61 | #include <openssl/asn1t.h> |
f0e8ae72 | 62 | #include <openssl/x509.h> |
2743e38c DSH |
63 | #include "internal/x509_int.h" |
64 | #include "internal/asn1_int.h" | |
4a1f3f27 | 65 | #include "x509_lcl.h" |
5ce278a7 | 66 | |
450ea834 | 67 | static int x509_name_ex_d2i(ASN1_VALUE **val, |
0f113f3e MC |
68 | const unsigned char **in, long len, |
69 | const ASN1_ITEM *it, | |
70 | int tag, int aclass, char opt, ASN1_TLC *ctx); | |
d02b48c6 | 71 | |
450ea834 | 72 | static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, |
0f113f3e | 73 | const ASN1_ITEM *it, int tag, int aclass); |
9d6b1ce6 DSH |
74 | static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it); |
75 | static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it); | |
d02b48c6 | 76 | |
9d6b1ce6 | 77 | static int x509_name_encode(X509_NAME *a); |
450ea834 DSH |
78 | static int x509_name_canon(X509_NAME *a); |
79 | static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in); | |
0f113f3e MC |
80 | static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname, |
81 | unsigned char **in); | |
1ef7acfe DSH |
82 | |
83 | static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, | |
0f113f3e MC |
84 | int indent, |
85 | const char *fname, const ASN1_PCTX *pctx); | |
1ef7acfe | 86 | |
9d6b1ce6 | 87 | ASN1_SEQUENCE(X509_NAME_ENTRY) = { |
0f113f3e MC |
88 | ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT), |
89 | ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE) | |
d339187b | 90 | } ASN1_SEQUENCE_END(X509_NAME_ENTRY) |
d02b48c6 | 91 | |
9d6b1ce6 | 92 | IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY) |
1241126a | 93 | IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY) |
d02b48c6 | 94 | |
0f113f3e MC |
95 | /* |
96 | * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so | |
97 | * declare two template wrappers for this | |
9d6b1ce6 | 98 | */ |
d02b48c6 | 99 | |
9d6b1ce6 | 100 | ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = |
0f113f3e | 101 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY) |
df2ee0e2 | 102 | static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES) |
d02b48c6 | 103 | |
9d6b1ce6 | 104 | ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) = |
0f113f3e | 105 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES) |
df2ee0e2 | 106 | static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL) |
d02b48c6 | 107 | |
0f113f3e MC |
108 | /* |
109 | * Normally that's where it would end: we'd have two nested STACK structures | |
9d6b1ce6 | 110 | * representing the ASN1. Unfortunately X509_NAME uses a completely different |
0f113f3e MC |
111 | * form and caches encodings so we have to process the internal form and |
112 | * convert to the external form. | |
9d6b1ce6 | 113 | */ |
d02b48c6 | 114 | |
df2ee0e2 | 115 | static const ASN1_EXTERN_FUNCS x509_name_ff = { |
0f113f3e MC |
116 | NULL, |
117 | x509_name_ex_new, | |
118 | x509_name_ex_free, | |
119 | 0, /* Default clear behaviour is OK */ | |
120 | x509_name_ex_d2i, | |
121 | x509_name_ex_i2d, | |
122 | x509_name_ex_print | |
9d6b1ce6 | 123 | }; |
d02b48c6 | 124 | |
0f113f3e | 125 | IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff) |
d02b48c6 | 126 | |
9d6b1ce6 | 127 | IMPLEMENT_ASN1_FUNCTIONS(X509_NAME) |
0f113f3e | 128 | |
1241126a | 129 | IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME) |
d02b48c6 | 130 | |
9d6b1ce6 DSH |
131 | static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) |
132 | { | |
64b25758 | 133 | X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret)); |
b4faea50 | 134 | |
90945fa3 | 135 | if (ret == NULL) |
0f113f3e MC |
136 | goto memerr; |
137 | if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) | |
138 | goto memerr; | |
139 | if ((ret->bytes = BUF_MEM_new()) == NULL) | |
140 | goto memerr; | |
0f113f3e MC |
141 | ret->modified = 1; |
142 | *val = (ASN1_VALUE *)ret; | |
143 | return 1; | |
bad40585 BM |
144 | |
145 | memerr: | |
0f113f3e MC |
146 | ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE); |
147 | if (ret) { | |
222561fe | 148 | sk_X509_NAME_ENTRY_free(ret->entries); |
0f113f3e MC |
149 | OPENSSL_free(ret); |
150 | } | |
151 | return 0; | |
9d6b1ce6 DSH |
152 | } |
153 | ||
154 | static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
155 | { | |
0f113f3e | 156 | X509_NAME *a; |
222561fe | 157 | |
0f113f3e MC |
158 | if (!pval || !*pval) |
159 | return; | |
160 | a = (X509_NAME *)*pval; | |
161 | ||
162 | BUF_MEM_free(a->bytes); | |
163 | sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free); | |
b548a1f1 | 164 | OPENSSL_free(a->canon_enc); |
0f113f3e MC |
165 | OPENSSL_free(a); |
166 | *pval = NULL; | |
9d6b1ce6 DSH |
167 | } |
168 | ||
450ea834 | 169 | static int x509_name_ex_d2i(ASN1_VALUE **val, |
0f113f3e MC |
170 | const unsigned char **in, long len, |
171 | const ASN1_ITEM *it, int tag, int aclass, | |
172 | char opt, ASN1_TLC *ctx) | |
9d6b1ce6 | 173 | { |
0f113f3e MC |
174 | const unsigned char *p = *in, *q; |
175 | union { | |
176 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; | |
177 | ASN1_VALUE *a; | |
178 | } intname = { | |
179 | NULL | |
180 | }; | |
181 | union { | |
182 | X509_NAME *x; | |
183 | ASN1_VALUE *a; | |
184 | } nm = { | |
185 | NULL | |
186 | }; | |
187 | int i, j, ret; | |
188 | STACK_OF(X509_NAME_ENTRY) *entries; | |
189 | X509_NAME_ENTRY *entry; | |
190 | q = p; | |
191 | ||
192 | /* Get internal representation of Name */ | |
193 | ret = ASN1_item_ex_d2i(&intname.a, | |
194 | &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL), | |
195 | tag, aclass, opt, ctx); | |
196 | ||
197 | if (ret <= 0) | |
198 | return ret; | |
199 | ||
200 | if (*val) | |
201 | x509_name_ex_free(val, NULL); | |
202 | if (!x509_name_ex_new(&nm.a, NULL)) | |
203 | goto err; | |
204 | /* We've decoded it: now cache encoding */ | |
205 | if (!BUF_MEM_grow(nm.x->bytes, p - q)) | |
206 | goto err; | |
207 | memcpy(nm.x->bytes->data, q, p - q); | |
208 | ||
209 | /* Convert internal representation to X509_NAME structure */ | |
210 | for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) { | |
211 | entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i); | |
212 | for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) { | |
213 | entry = sk_X509_NAME_ENTRY_value(entries, j); | |
214 | entry->set = i; | |
215 | if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry)) | |
216 | goto err; | |
217 | } | |
218 | sk_X509_NAME_ENTRY_free(entries); | |
219 | } | |
220 | sk_STACK_OF_X509_NAME_ENTRY_free(intname.s); | |
221 | ret = x509_name_canon(nm.x); | |
222 | if (!ret) | |
223 | goto err; | |
224 | nm.x->modified = 0; | |
225 | *val = nm.a; | |
226 | *in = p; | |
227 | return ret; | |
228 | err: | |
222561fe | 229 | X509_NAME_free(nm.x); |
0f113f3e MC |
230 | ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR); |
231 | return 0; | |
9d6b1ce6 DSH |
232 | } |
233 | ||
0f113f3e MC |
234 | static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, |
235 | const ASN1_ITEM *it, int tag, int aclass) | |
9d6b1ce6 | 236 | { |
0f113f3e MC |
237 | int ret; |
238 | X509_NAME *a = (X509_NAME *)*val; | |
239 | if (a->modified) { | |
240 | ret = x509_name_encode(a); | |
241 | if (ret < 0) | |
242 | return ret; | |
243 | ret = x509_name_canon(a); | |
244 | if (ret < 0) | |
245 | return ret; | |
246 | } | |
247 | ret = a->bytes->length; | |
248 | if (out != NULL) { | |
249 | memcpy(*out, a->bytes->data, ret); | |
250 | *out += ret; | |
251 | } | |
252 | return ret; | |
9d6b1ce6 | 253 | } |
d02b48c6 | 254 | |
5ce278a7 | 255 | static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) |
0f113f3e MC |
256 | { |
257 | sk_X509_NAME_ENTRY_free(ne); | |
258 | } | |
5ce278a7 | 259 | |
6cb9fca7 | 260 | static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) |
0f113f3e MC |
261 | { |
262 | sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); | |
263 | } | |
6cb9fca7 | 264 | |
9d6b1ce6 DSH |
265 | static int x509_name_encode(X509_NAME *a) |
266 | { | |
0f113f3e MC |
267 | union { |
268 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; | |
269 | ASN1_VALUE *a; | |
270 | } intname = { | |
271 | NULL | |
272 | }; | |
273 | int len; | |
274 | unsigned char *p; | |
275 | STACK_OF(X509_NAME_ENTRY) *entries = NULL; | |
276 | X509_NAME_ENTRY *entry; | |
277 | int i, set = -1; | |
278 | intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null(); | |
279 | if (!intname.s) | |
280 | goto memerr; | |
281 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | |
282 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); | |
283 | if (entry->set != set) { | |
284 | entries = sk_X509_NAME_ENTRY_new_null(); | |
285 | if (!entries) | |
286 | goto memerr; | |
287 | if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) | |
288 | goto memerr; | |
289 | set = entry->set; | |
290 | } | |
291 | if (!sk_X509_NAME_ENTRY_push(entries, entry)) | |
292 | goto memerr; | |
293 | } | |
294 | len = ASN1_item_ex_i2d(&intname.a, NULL, | |
295 | ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); | |
296 | if (!BUF_MEM_grow(a->bytes, len)) | |
297 | goto memerr; | |
298 | p = (unsigned char *)a->bytes->data; | |
299 | ASN1_item_ex_i2d(&intname.a, | |
300 | &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); | |
301 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | |
302 | local_sk_X509_NAME_ENTRY_free); | |
303 | a->modified = 0; | |
304 | return len; | |
305 | memerr: | |
306 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | |
307 | local_sk_X509_NAME_ENTRY_free); | |
308 | ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE); | |
309 | return -1; | |
9d6b1ce6 DSH |
310 | } |
311 | ||
1ef7acfe | 312 | static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, |
0f113f3e MC |
313 | int indent, |
314 | const char *fname, const ASN1_PCTX *pctx) | |
315 | { | |
316 | if (X509_NAME_print_ex(out, (X509_NAME *)*pval, | |
317 | indent, pctx->nm_flags) <= 0) | |
318 | return 0; | |
319 | return 2; | |
320 | } | |
321 | ||
322 | /* | |
323 | * This function generates the canonical encoding of the Name structure. In | |
324 | * it all strings are converted to UTF8, leading, trailing and multiple | |
325 | * spaces collapsed, converted to lower case and the leading SEQUENCE header | |
326 | * removed. In future we could also normalize the UTF8 too. By doing this | |
327 | * comparison of Name structures can be rapidly perfomed by just using | |
328 | * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name | |
329 | * constraints of type dirName can also be checked with a simple memcmp(). | |
450ea834 DSH |
330 | */ |
331 | ||
332 | static int x509_name_canon(X509_NAME *a) | |
0f113f3e MC |
333 | { |
334 | unsigned char *p; | |
335 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL; | |
336 | STACK_OF(X509_NAME_ENTRY) *entries = NULL; | |
337 | X509_NAME_ENTRY *entry, *tmpentry = NULL; | |
338 | int i, set = -1, ret = 0; | |
339 | ||
b548a1f1 RS |
340 | OPENSSL_free(a->canon_enc); |
341 | a->canon_enc = NULL; | |
0f113f3e MC |
342 | /* Special case: empty X509_NAME => null encoding */ |
343 | if (sk_X509_NAME_ENTRY_num(a->entries) == 0) { | |
344 | a->canon_enclen = 0; | |
345 | return 1; | |
346 | } | |
347 | intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); | |
348 | if (!intname) | |
349 | goto err; | |
350 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | |
351 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); | |
352 | if (entry->set != set) { | |
353 | entries = sk_X509_NAME_ENTRY_new_null(); | |
354 | if (!entries) | |
355 | goto err; | |
356 | if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) | |
357 | goto err; | |
358 | set = entry->set; | |
359 | } | |
360 | tmpentry = X509_NAME_ENTRY_new(); | |
90945fa3 | 361 | if (tmpentry == NULL) |
0f113f3e MC |
362 | goto err; |
363 | tmpentry->object = OBJ_dup(entry->object); | |
364 | if (!asn1_string_canon(tmpentry->value, entry->value)) | |
365 | goto err; | |
366 | if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) | |
367 | goto err; | |
368 | tmpentry = NULL; | |
369 | } | |
370 | ||
371 | /* Finally generate encoding */ | |
372 | ||
373 | a->canon_enclen = i2d_name_canon(intname, NULL); | |
374 | ||
375 | p = OPENSSL_malloc(a->canon_enclen); | |
376 | ||
90945fa3 | 377 | if (p == NULL) |
0f113f3e MC |
378 | goto err; |
379 | ||
380 | a->canon_enc = p; | |
381 | ||
382 | i2d_name_canon(intname, &p); | |
383 | ||
384 | ret = 1; | |
385 | ||
386 | err: | |
387 | ||
222561fe RS |
388 | X509_NAME_ENTRY_free(tmpentry); |
389 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, | |
390 | local_sk_X509_NAME_ENTRY_pop_free); | |
0f113f3e MC |
391 | return ret; |
392 | } | |
450ea834 DSH |
393 | |
394 | /* Bitmap of all the types of string that will be canonicalized. */ | |
395 | ||
0f113f3e MC |
396 | #define ASN1_MASK_CANON \ |
397 | (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \ | |
398 | | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \ | |
399 | | B_ASN1_VISIBLESTRING) | |
450ea834 DSH |
400 | |
401 | static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | |
0f113f3e MC |
402 | { |
403 | unsigned char *to, *from; | |
404 | int len, i; | |
405 | ||
406 | /* If type not in bitmask just copy string across */ | |
407 | if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) { | |
408 | if (!ASN1_STRING_copy(out, in)) | |
409 | return 0; | |
410 | return 1; | |
411 | } | |
412 | ||
413 | out->type = V_ASN1_UTF8STRING; | |
414 | out->length = ASN1_STRING_to_UTF8(&out->data, in); | |
415 | if (out->length == -1) | |
416 | return 0; | |
417 | ||
418 | to = out->data; | |
419 | from = to; | |
420 | ||
421 | len = out->length; | |
422 | ||
423 | /* | |
424 | * Convert string in place to canonical form. Ultimately we may need to | |
425 | * handle a wider range of characters but for now ignore anything with | |
426 | * MSB set and rely on the isspace() and tolower() functions. | |
427 | */ | |
428 | ||
429 | /* Ignore leading spaces */ | |
430 | while ((len > 0) && !(*from & 0x80) && isspace(*from)) { | |
431 | from++; | |
432 | len--; | |
433 | } | |
434 | ||
435 | to = from + len - 1; | |
436 | ||
437 | /* Ignore trailing spaces */ | |
438 | while ((len > 0) && !(*to & 0x80) && isspace(*to)) { | |
439 | to--; | |
440 | len--; | |
441 | } | |
442 | ||
443 | to = out->data; | |
444 | ||
445 | i = 0; | |
446 | while (i < len) { | |
447 | /* If MSB set just copy across */ | |
448 | if (*from & 0x80) { | |
449 | *to++ = *from++; | |
450 | i++; | |
451 | } | |
452 | /* Collapse multiple spaces */ | |
453 | else if (isspace(*from)) { | |
454 | /* Copy one space across */ | |
455 | *to++ = ' '; | |
456 | /* | |
457 | * Ignore subsequent spaces. Note: don't need to check len here | |
458 | * because we know the last character is a non-space so we can't | |
459 | * overflow. | |
460 | */ | |
461 | do { | |
462 | from++; | |
463 | i++; | |
464 | } | |
465 | while (!(*from & 0x80) && isspace(*from)); | |
466 | } else { | |
467 | *to++ = tolower(*from); | |
468 | from++; | |
469 | i++; | |
470 | } | |
471 | } | |
472 | ||
473 | out->length = to - out->data; | |
474 | ||
475 | return 1; | |
476 | ||
477 | } | |
478 | ||
479 | static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname, | |
480 | unsigned char **in) | |
481 | { | |
482 | int i, len, ltmp; | |
483 | ASN1_VALUE *v; | |
484 | STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname; | |
485 | ||
486 | len = 0; | |
487 | for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) { | |
488 | v = sk_ASN1_VALUE_value(intname, i); | |
489 | ltmp = ASN1_item_ex_i2d(&v, in, | |
490 | ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1); | |
491 | if (ltmp < 0) | |
492 | return ltmp; | |
493 | len += ltmp; | |
494 | } | |
495 | return len; | |
496 | } | |
d02b48c6 | 497 | |
6b691a5c | 498 | int X509_NAME_set(X509_NAME **xn, X509_NAME *name) |
0f113f3e MC |
499 | { |
500 | X509_NAME *in; | |
501 | ||
502 | if (!xn || !name) | |
503 | return (0); | |
504 | ||
505 | if (*xn != name) { | |
506 | in = X509_NAME_dup(name); | |
507 | if (in != NULL) { | |
508 | X509_NAME_free(*xn); | |
509 | *xn = in; | |
510 | } | |
511 | } | |
512 | return (*xn != NULL); | |
513 | } | |
0d0099ea DSH |
514 | |
515 | int X509_NAME_print(BIO *bp, X509_NAME *name, int obase) | |
516 | { | |
517 | char *s, *c, *b; | |
518 | int l, i; | |
519 | ||
520 | l = 80 - 2 - obase; | |
521 | ||
522 | b = X509_NAME_oneline(name, NULL, 0); | |
523 | if (!b) | |
524 | return 0; | |
525 | if (!*b) { | |
526 | OPENSSL_free(b); | |
527 | return 1; | |
528 | } | |
529 | s = b + 1; /* skip the first slash */ | |
530 | ||
531 | c = s; | |
532 | for (;;) { | |
533 | #ifndef CHARSET_EBCDIC | |
534 | if (((*s == '/') && | |
535 | ((s[1] >= 'A') && (s[1] <= 'Z') && ((s[2] == '=') || | |
536 | ((s[2] >= 'A') | |
537 | && (s[2] <= 'Z') | |
538 | && (s[3] == '=')) | |
539 | ))) || (*s == '\0')) | |
540 | #else | |
541 | if (((*s == '/') && | |
542 | (isupper(s[1]) && ((s[2] == '=') || | |
543 | (isupper(s[2]) && (s[3] == '=')) | |
544 | ))) || (*s == '\0')) | |
545 | #endif | |
546 | { | |
547 | i = s - c; | |
548 | if (BIO_write(bp, c, i) != i) | |
549 | goto err; | |
550 | c = s + 1; /* skip following slash */ | |
551 | if (*s != '\0') { | |
552 | if (BIO_write(bp, ", ", 2) != 2) | |
553 | goto err; | |
554 | } | |
555 | l--; | |
556 | } | |
557 | if (*s == '\0') | |
558 | break; | |
559 | s++; | |
560 | l--; | |
561 | } | |
562 | ||
563 | OPENSSL_free(b); | |
564 | return 1; | |
565 | err: | |
566 | X509err(X509_F_X509_NAME_PRINT, ERR_R_BUF_LIB); | |
567 | OPENSSL_free(b); | |
568 | return 0; | |
569 | } |