/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.70M | #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 | 153M | } ASN1_SEQUENCE_END(X509_NAME_ENTRY) |
50 | 153M | |
51 | 153M | IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY) |
52 | 153M | IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY) |
53 | 153M | |
54 | 153M | /* |
55 | 153M | * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so |
56 | 153M | * declare two template wrappers for this |
57 | 153M | */ |
58 | 153M | |
59 | 153M | ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY) |
60 | 231M | 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.81M | 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 | 19.6M | 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 | 904k | { |
92 | 904k | X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret)); |
93 | | |
94 | 904k | if (ret == NULL) |
95 | 0 | goto memerr; |
96 | 904k | if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) |
97 | 0 | goto memerr; |
98 | 904k | if ((ret->bytes = BUF_MEM_new()) == NULL) |
99 | 0 | goto memerr; |
100 | 904k | ret->modified = 1; |
101 | 904k | *val = (ASN1_VALUE *)ret; |
102 | 904k | 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 | 904k | } |
112 | | |
113 | | static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
114 | 5.35M | { |
115 | 5.35M | X509_NAME *a; |
116 | | |
117 | 5.35M | if (pval == NULL || *pval == NULL) |
118 | 0 | return; |
119 | 5.35M | a = (X509_NAME *)*pval; |
120 | | |
121 | 5.35M | BUF_MEM_free(a->bytes); |
122 | 5.35M | sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free); |
123 | 5.35M | OPENSSL_free(a->canon_enc); |
124 | 5.35M | OPENSSL_free(a); |
125 | 5.35M | *pval = NULL; |
126 | 5.35M | } |
127 | | |
128 | | static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) |
129 | 101M | { |
130 | 101M | sk_X509_NAME_ENTRY_free(ne); |
131 | 101M | } |
132 | | |
133 | | static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) |
134 | 22.9M | { |
135 | 22.9M | sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); |
136 | 22.9M | } |
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.70M | { |
143 | 2.70M | const unsigned char *p = *in, *q; |
144 | 2.70M | union { |
145 | 2.70M | STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; |
146 | 2.70M | ASN1_VALUE *a; |
147 | 2.70M | } intname = { |
148 | 2.70M | NULL |
149 | 2.70M | }; |
150 | 2.70M | union { |
151 | 2.70M | X509_NAME *x; |
152 | 2.70M | ASN1_VALUE *a; |
153 | 2.70M | } nm = { |
154 | 2.70M | NULL |
155 | 2.70M | }; |
156 | 2.70M | int i, j, ret; |
157 | 2.70M | STACK_OF(X509_NAME_ENTRY) *entries; |
158 | 2.70M | X509_NAME_ENTRY *entry; |
159 | | |
160 | 2.70M | if (len > X509_NAME_MAX) |
161 | 0 | len = X509_NAME_MAX; |
162 | 2.70M | q = p; |
163 | | |
164 | | /* Get internal representation of Name */ |
165 | 2.70M | ret = ASN1_item_ex_d2i(&intname.a, |
166 | 2.70M | &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL), |
167 | 2.70M | tag, aclass, opt, ctx); |
168 | | |
169 | 2.70M | if (ret <= 0) |
170 | 282k | return ret; |
171 | | |
172 | 2.42M | if (*val) |
173 | 2.12M | x509_name_ex_free(val, NULL); |
174 | 2.42M | if (!x509_name_ex_new(&nm.a, NULL)) |
175 | 0 | goto err; |
176 | | /* We've decoded it: now cache encoding */ |
177 | 2.42M | if (!BUF_MEM_grow(nm.x->bytes, p - q)) |
178 | 0 | goto err; |
179 | 2.42M | memcpy(nm.x->bytes->data, q, p - q); |
180 | | |
181 | | /* Convert internal representation to X509_NAME structure */ |
182 | 114M | for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) { |
183 | 112M | entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i); |
184 | 131M | for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) { |
185 | 18.7M | entry = sk_X509_NAME_ENTRY_value(entries, j); |
186 | 18.7M | entry->set = i; |
187 | 18.7M | if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry)) |
188 | 0 | goto err; |
189 | 18.7M | (void)sk_X509_NAME_ENTRY_set(entries, j, NULL); |
190 | 18.7M | } |
191 | 112M | } |
192 | 2.42M | ret = x509_name_canon(nm.x); |
193 | 2.42M | if (!ret) |
194 | 27.6k | goto err; |
195 | 2.39M | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, |
196 | 2.39M | local_sk_X509_NAME_ENTRY_free); |
197 | 2.39M | nm.x->modified = 0; |
198 | 2.39M | *val = nm.a; |
199 | 2.39M | *in = p; |
200 | 2.39M | return ret; |
201 | | |
202 | 27.6k | err: |
203 | 27.6k | if (nm.x != NULL) |
204 | 27.6k | X509_NAME_free(nm.x); |
205 | 27.6k | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, |
206 | 27.6k | local_sk_X509_NAME_ENTRY_pop_free); |
207 | 27.6k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
208 | 27.6k | return 0; |
209 | 2.42M | } |
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 | 841k | { |
214 | 841k | int ret; |
215 | 841k | X509_NAME *a = (X509_NAME *)*val; |
216 | | |
217 | 841k | if (a->modified) { |
218 | 55.9k | ret = x509_name_encode(a); |
219 | 55.9k | if (ret < 0) |
220 | 0 | return ret; |
221 | 55.9k | ret = x509_name_canon(a); |
222 | 55.9k | if (!ret) |
223 | 2.88k | return -1; |
224 | 55.9k | } |
225 | 838k | ret = a->bytes->length; |
226 | 838k | if (out != NULL) { |
227 | 174k | memcpy(*out, a->bytes->data, ret); |
228 | 174k | *out += ret; |
229 | 174k | } |
230 | 838k | return ret; |
231 | 841k | } |
232 | | |
233 | | static int x509_name_encode(X509_NAME *a) |
234 | 9.92k | { |
235 | 9.92k | union { |
236 | 9.92k | STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; |
237 | 9.92k | const ASN1_VALUE *a; |
238 | 9.92k | } intname = { |
239 | 9.92k | NULL |
240 | 9.92k | }; |
241 | 9.92k | int len; |
242 | 9.92k | unsigned char *p; |
243 | 9.92k | STACK_OF(X509_NAME_ENTRY) *entries = NULL; |
244 | 9.92k | X509_NAME_ENTRY *entry; |
245 | 9.92k | int i, set = -1; |
246 | | |
247 | 9.92k | intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null(); |
248 | 9.92k | if (!intname.s) |
249 | 0 | goto memerr; |
250 | 812k | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
251 | 802k | entry = sk_X509_NAME_ENTRY_value(a->entries, i); |
252 | 802k | if (entry->set != set) { |
253 | 335k | entries = sk_X509_NAME_ENTRY_new_null(); |
254 | 335k | if (!entries) |
255 | 0 | goto memerr; |
256 | 335k | 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 | 335k | set = entry->set; |
261 | 335k | } |
262 | 802k | if (!sk_X509_NAME_ENTRY_push(entries, entry)) |
263 | 0 | goto memerr; |
264 | 802k | } |
265 | 9.92k | len = ASN1_item_ex_i2d(&intname.a, NULL, |
266 | 9.92k | ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); |
267 | 9.92k | if (!BUF_MEM_grow(a->bytes, len)) |
268 | 0 | goto memerr; |
269 | 9.92k | p = (unsigned char *)a->bytes->data; |
270 | 9.92k | ASN1_item_ex_i2d(&intname.a, |
271 | 9.92k | &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); |
272 | 9.92k | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, |
273 | 9.92k | local_sk_X509_NAME_ENTRY_free); |
274 | 9.92k | a->modified = 0; |
275 | 9.92k | 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 | 9.92k | } |
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 | 55.3k | { |
287 | 55.3k | if (X509_NAME_print_ex(out, (const X509_NAME *)*pval, |
288 | 55.3k | indent, pctx->nm_flags) |
289 | 55.3k | <= 0) |
290 | 185 | return 0; |
291 | 55.1k | return 2; |
292 | 55.3k | } |
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.47M | { |
307 | 2.47M | unsigned char *p; |
308 | 2.47M | STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname; |
309 | 2.47M | STACK_OF(X509_NAME_ENTRY) *entries = NULL; |
310 | 2.47M | X509_NAME_ENTRY *entry, *tmpentry = NULL; |
311 | 2.47M | int i, set = -1, ret = 0, len; |
312 | | |
313 | 2.47M | OPENSSL_free(a->canon_enc); |
314 | 2.47M | a->canon_enc = NULL; |
315 | | /* Special case: empty X509_NAME => null encoding */ |
316 | 2.47M | if (sk_X509_NAME_ENTRY_num(a->entries) == 0) { |
317 | 1.75M | a->canon_enclen = 0; |
318 | 1.75M | return 1; |
319 | 1.75M | } |
320 | 727k | intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); |
321 | 727k | if (intname == NULL) { |
322 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
323 | 0 | goto err; |
324 | 0 | } |
325 | 23.3M | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
326 | 22.6M | entry = sk_X509_NAME_ENTRY_value(a->entries, i); |
327 | 22.6M | if (entry->set != set) { |
328 | 9.77M | entries = sk_X509_NAME_ENTRY_new_null(); |
329 | 9.77M | if (entries == NULL) |
330 | 0 | goto err; |
331 | 9.77M | 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 | 9.77M | set = entry->set; |
337 | 9.77M | } |
338 | 22.6M | tmpentry = X509_NAME_ENTRY_new(); |
339 | 22.6M | if (tmpentry == NULL) { |
340 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
341 | 0 | goto err; |
342 | 0 | } |
343 | 22.6M | tmpentry->object = OBJ_dup(entry->object); |
344 | 22.6M | if (tmpentry->object == NULL) { |
345 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
346 | 0 | goto err; |
347 | 0 | } |
348 | 22.6M | if (!asn1_string_canon(tmpentry->value, entry->value)) |
349 | 30.4k | goto err; |
350 | 22.6M | 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 | 22.6M | tmpentry = NULL; |
355 | 22.6M | } |
356 | | |
357 | | /* Finally generate encoding */ |
358 | 697k | len = i2d_name_canon(intname, NULL); |
359 | 697k | if (len < 0) |
360 | 0 | goto err; |
361 | 697k | a->canon_enclen = len; |
362 | | |
363 | 697k | p = OPENSSL_malloc(a->canon_enclen); |
364 | 697k | if (p == NULL) { |
365 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
366 | 0 | goto err; |
367 | 0 | } |
368 | | |
369 | 697k | a->canon_enc = p; |
370 | | |
371 | 697k | i2d_name_canon(intname, &p); |
372 | | |
373 | 697k | ret = 1; |
374 | | |
375 | 727k | err: |
376 | 727k | X509_NAME_ENTRY_free(tmpentry); |
377 | 727k | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, |
378 | 727k | local_sk_X509_NAME_ENTRY_pop_free); |
379 | 727k | return ret; |
380 | 697k | } |
381 | | |
382 | | /* Bitmap of all the types of string that will be canonicalized. */ |
383 | | |
384 | | #define ASN1_MASK_CANON \ |
385 | 17.5M | (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \ |
386 | 17.5M | | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \ |
387 | 17.5M | | B_ASN1_VISIBLESTRING) |
388 | | |
389 | | static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in) |
390 | 17.5M | { |
391 | 17.5M | unsigned char *to, *from; |
392 | 17.5M | int len, i; |
393 | | |
394 | | /* If type not in bitmask just copy string across */ |
395 | 17.5M | if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) { |
396 | 9.71M | if (!ASN1_STRING_copy(out, in)) |
397 | 0 | return 0; |
398 | 9.71M | return 1; |
399 | 9.71M | } |
400 | | |
401 | 7.79M | out->type = V_ASN1_UTF8STRING; |
402 | 7.79M | out->length = ASN1_STRING_to_UTF8(&out->data, in); |
403 | 7.79M | if (out->length == -1) |
404 | 23.6k | return 0; |
405 | | |
406 | 7.77M | to = out->data; |
407 | 7.77M | from = to; |
408 | | |
409 | 7.77M | 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 | 7.85M | while (len > 0 && ossl_isspace(*from)) { |
420 | 82.8k | from++; |
421 | 82.8k | len--; |
422 | 82.8k | } |
423 | | |
424 | 7.77M | to = from + len; |
425 | | |
426 | | /* Ignore trailing spaces */ |
427 | 7.79M | while (len > 0 && ossl_isspace(to[-1])) { |
428 | 15.1k | to--; |
429 | 15.1k | len--; |
430 | 15.1k | } |
431 | | |
432 | 7.77M | to = out->data; |
433 | | |
434 | 7.77M | i = 0; |
435 | 871M | while (i < len) { |
436 | | /* If not ASCII set just copy across */ |
437 | 863M | if (!ossl_isascii(*from)) { |
438 | 744M | *to++ = *from++; |
439 | 744M | i++; |
440 | 744M | } |
441 | | /* Collapse multiple spaces */ |
442 | 119M | else if (ossl_isspace(*from)) { |
443 | | /* Copy one space across */ |
444 | 3.23M | *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 | 9.24M | do { |
451 | 9.24M | from++; |
452 | 9.24M | i++; |
453 | 9.24M | } while (ossl_isspace(*from)); |
454 | 116M | } else { |
455 | 116M | *to++ = ossl_tolower(*from); |
456 | 116M | from++; |
457 | 116M | i++; |
458 | 116M | } |
459 | 863M | } |
460 | | |
461 | 7.77M | out->length = to - out->data; |
462 | | |
463 | 7.77M | return 1; |
464 | 7.79M | } |
465 | | |
466 | | static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, |
467 | | unsigned char **in) |
468 | 1.39M | { |
469 | 1.39M | int i, len, ltmp; |
470 | 1.39M | const ASN1_VALUE *v; |
471 | 1.39M | STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname; |
472 | | |
473 | 1.39M | len = 0; |
474 | 20.8M | for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) { |
475 | 19.4M | v = sk_ASN1_VALUE_value(intname, i); |
476 | 19.4M | ltmp = ASN1_item_ex_i2d(&v, in, |
477 | 19.4M | ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1); |
478 | 19.4M | if (ltmp < 0 || len > INT_MAX - ltmp) |
479 | 0 | return -1; |
480 | 19.4M | len += ltmp; |
481 | 19.4M | } |
482 | 1.39M | return len; |
483 | 1.39M | } |
484 | | |
485 | | int X509_NAME_set(X509_NAME **xn, const X509_NAME *name) |
486 | 63.1k | { |
487 | 63.1k | X509_NAME *name_copy; |
488 | | |
489 | 63.1k | if (*xn == name) |
490 | 0 | return *xn != NULL; |
491 | 63.1k | if ((name_copy = X509_NAME_dup(name)) == NULL) |
492 | 0 | return 0; |
493 | 63.1k | X509_NAME_free(*xn); |
494 | 63.1k | *xn = name_copy; |
495 | 63.1k | return 1; |
496 | 63.1k | } |
497 | | |
498 | | int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) |
499 | 177k | { |
500 | 177k | char *s, *c, *b; |
501 | 177k | int i; |
502 | | |
503 | 177k | b = X509_NAME_oneline(name, NULL, 0); |
504 | 177k | if (b == NULL) |
505 | 629 | return 0; |
506 | 176k | if (*b == '\0') { |
507 | 118k | OPENSSL_free(b); |
508 | 118k | return 1; |
509 | 118k | } |
510 | 58.0k | s = b + 1; /* skip the first slash */ |
511 | | |
512 | 58.0k | c = s; |
513 | 821M | for (;;) { |
514 | 821M | if (((*s == '/') && (ossl_isupper(s[1]) && ((s[2] == '=') || (ossl_isupper(s[2]) && (s[3] == '='))))) || (*s == '\0')) { |
515 | 171k | i = s - c; |
516 | 171k | if (BIO_write(bp, c, i) != i) |
517 | 0 | goto err; |
518 | 171k | c = s + 1; /* skip following slash */ |
519 | 171k | if (*s != '\0') { |
520 | 113k | if (BIO_write(bp, ", ", 2) != 2) |
521 | 0 | goto err; |
522 | 113k | } |
523 | 171k | } |
524 | 821M | if (*s == '\0') |
525 | 58.0k | break; |
526 | 821M | s++; |
527 | 821M | } |
528 | | |
529 | 58.0k | OPENSSL_free(b); |
530 | 58.0k | 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 | 58.0k | } |
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 | } |