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