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