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