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