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