Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/x_name.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <ctype.h>
5 #include <string.h>
6
7 #include <openssl/asn1.h>
8 #include <openssl/asn1t.h>
9 #include <openssl/buf.h>
10 #include <openssl/err.h>
11 #include <openssl/mem.h>
12 #include <openssl/obj.h>
13 #include <openssl/stack.h>
14 #include <openssl/x509.h>
15
16 #include "../asn1/internal.h"
17 #include "../internal.h"
18 #include "internal.h"
19
20
21 typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
22 DEFINE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
23
24 // Maximum length of X509_NAME: much larger than anything we should
25 // ever see in practice.
26
27 #define X509_NAME_MAX (1024 * 1024)
28
29 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
30 long len, const ASN1_ITEM *it, int tag, int aclass,
31 char opt, ASN1_TLC *ctx);
32
33 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
34 const ASN1_ITEM *it, int tag, int aclass);
35 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
36 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
37
38 static int x509_name_encode(X509_NAME *a);
39 static int x509_name_canon(X509_NAME *a);
40 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
41 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname,
42 unsigned char **in);
43
44 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
45 ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
46 ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE),
47 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
48
49 IMPLEMENT_ASN1_FUNCTIONS_const(X509_NAME_ENTRY)
50 IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_NAME_ENTRY)
51
52 // For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
53 // declare two template wrappers for this
54
55 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF,
56 0, RDNS,
57 X509_NAME_ENTRY)
58 ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
59
60 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
61 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
62 ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
63
64 // Normally that's where it would end: we'd have two nested STACK structures
65 // representing the ASN1. Unfortunately X509_NAME uses a completely different
66 // form and caches encodings so we have to process the internal form and
67 // convert to the external form.
68
69 static const ASN1_EXTERN_FUNCS x509_name_ff = {
70 NULL,
71 x509_name_ex_new,
72 x509_name_ex_free,
73 x509_name_ex_d2i,
74 x509_name_ex_i2d,
75 NULL,
76 };
77
78 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
79
80 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
81
82 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
83
84 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) {
85 X509_NAME *ret = NULL;
86 ret = OPENSSL_malloc(sizeof(X509_NAME));
87 if (!ret) {
88 goto memerr;
89 }
90 if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) {
91 goto memerr;
92 }
93 if ((ret->bytes = BUF_MEM_new()) == NULL) {
94 goto memerr;
95 }
96 ret->canon_enc = NULL;
97 ret->canon_enclen = 0;
98 ret->modified = 1;
99 *val = (ASN1_VALUE *)ret;
100 return 1;
101
102 memerr:
103 if (ret) {
104 if (ret->entries) {
105 sk_X509_NAME_ENTRY_free(ret->entries);
106 }
107 OPENSSL_free(ret);
108 }
109 return 0;
110 }
111
112 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
113 X509_NAME *a;
114 if (!pval || !*pval) {
115 return;
116 }
117 a = (X509_NAME *)*pval;
118
119 BUF_MEM_free(a->bytes);
120 sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
121 if (a->canon_enc) {
122 OPENSSL_free(a->canon_enc);
123 }
124 OPENSSL_free(a);
125 *pval = NULL;
126 }
127
128 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) {
129 sk_X509_NAME_ENTRY_free(ne);
130 }
131
132 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) {
133 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
134 }
135
136 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
137 long len, const ASN1_ITEM *it, int tag, int aclass,
138 char opt, ASN1_TLC *ctx) {
139 const unsigned char *p = *in, *q;
140 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
141 X509_NAME *nm = NULL;
142 size_t i, j;
143 int ret;
144 STACK_OF(X509_NAME_ENTRY) *entries;
145 X509_NAME_ENTRY *entry;
146 // Bound the size of an X509_NAME we are willing to parse.
147 if (len > X509_NAME_MAX) {
148 len = X509_NAME_MAX;
149 }
150 q = p;
151
152 // Get internal representation of Name
153 ASN1_VALUE *intname_val = NULL;
154 ret = ASN1_item_ex_d2i(&intname_val, &p, len,
155 ASN1_ITEM_rptr(X509_NAME_INTERNAL), tag, aclass, opt,
156 ctx);
157 if (ret <= 0) {
158 return ret;
159 }
160 intname = (STACK_OF(STACK_OF_X509_NAME_ENTRY) *)intname_val;
161
162 if (*val) {
163 x509_name_ex_free(val, NULL);
164 }
165 ASN1_VALUE *nm_val = NULL;
166 if (!x509_name_ex_new(&nm_val, NULL)) {
167 goto err;
168 }
169 nm = (X509_NAME *)nm_val;
170 // We've decoded it: now cache encoding
171 if (!BUF_MEM_grow(nm->bytes, p - q)) {
172 goto err;
173 }
174 OPENSSL_memcpy(nm->bytes->data, q, p - q);
175
176 // Convert internal representation to X509_NAME structure
177 for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname); i++) {
178 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname, i);
179 for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
180 entry = sk_X509_NAME_ENTRY_value(entries, j);
181 entry->set = (int)i;
182 if (!sk_X509_NAME_ENTRY_push(nm->entries, entry)) {
183 goto err;
184 }
185 (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
186 }
187 }
188 ret = x509_name_canon(nm);
189 if (!ret) {
190 goto err;
191 }
192 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
193 nm->modified = 0;
194 *val = (ASN1_VALUE *)nm;
195 *in = p;
196 return ret;
197 err:
198 X509_NAME_free(nm);
199 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
200 local_sk_X509_NAME_ENTRY_pop_free);
201 OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
202 return 0;
203 }
204
205 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
206 const ASN1_ITEM *it, int tag, int aclass) {
207 X509_NAME *a = (X509_NAME *)*val;
208 if (a->modified && (!x509_name_encode(a) || !x509_name_canon(a))) {
209 return -1;
210 }
211 int ret = a->bytes->length;
212 if (out != NULL) {
213 OPENSSL_memcpy(*out, a->bytes->data, ret);
214 *out += ret;
215 }
216 return ret;
217 }
218
219 static int x509_name_encode(X509_NAME *a) {
220 int len;
221 unsigned char *p;
222 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
223 X509_NAME_ENTRY *entry;
224 int set = -1;
225 size_t i;
226 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname =
227 sk_STACK_OF_X509_NAME_ENTRY_new_null();
228 if (!intname) {
229 goto err;
230 }
231 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
232 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
233 if (entry->set != set) {
234 entries = sk_X509_NAME_ENTRY_new_null();
235 if (!entries) {
236 goto err;
237 }
238 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
239 sk_X509_NAME_ENTRY_free(entries);
240 goto err;
241 }
242 set = entry->set;
243 }
244 if (!sk_X509_NAME_ENTRY_push(entries, entry)) {
245 goto err;
246 }
247 }
248 ASN1_VALUE *intname_val = (ASN1_VALUE *)intname;
249 len = ASN1_item_ex_i2d(&intname_val, NULL, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
250 /*tag=*/-1, /*aclass=*/0);
251 if (len <= 0) {
252 goto err;
253 }
254 if (!BUF_MEM_grow(a->bytes, len)) {
255 goto err;
256 }
257 p = (unsigned char *)a->bytes->data;
258 if (ASN1_item_ex_i2d(&intname_val, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
259 /*tag=*/-1, /*aclass=*/0) <= 0) {
260 goto err;
261 }
262 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
263 a->modified = 0;
264 return 1;
265 err:
266 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
267 return 0;
268 }
269
270 // This function generates the canonical encoding of the Name structure. In
271 // it all strings are converted to UTF8, leading, trailing and multiple
272 // spaces collapsed, converted to lower case and the leading SEQUENCE header
273 // removed. In future we could also normalize the UTF8 too. By doing this
274 // comparison of Name structures can be rapidly perfomed by just using
275 // OPENSSL_memcmp() of the canonical encoding. By omitting the leading SEQUENCE
276 // name constraints of type dirName can also be checked with a simple
277 // OPENSSL_memcmp().
278
279 static int x509_name_canon(X509_NAME *a) {
280 unsigned char *p;
281 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
282 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
283 X509_NAME_ENTRY *entry, *tmpentry = NULL;
284 int set = -1, ret = 0, len;
285 size_t i;
286
287 if (a->canon_enc) {
288 OPENSSL_free(a->canon_enc);
289 a->canon_enc = NULL;
290 }
291 // Special case: empty X509_NAME => null encoding
292 if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
293 a->canon_enclen = 0;
294 return 1;
295 }
296 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
297 if (!intname) {
298 goto err;
299 }
300 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
301 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
302 if (entry->set != set) {
303 entries = sk_X509_NAME_ENTRY_new_null();
304 if (!entries) {
305 goto err;
306 }
307 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
308 sk_X509_NAME_ENTRY_free(entries);
309 goto err;
310 }
311 set = entry->set;
312 }
313 tmpentry = X509_NAME_ENTRY_new();
314 if (tmpentry == NULL) {
315 goto err;
316 }
317 tmpentry->object = OBJ_dup(entry->object);
318 if (!asn1_string_canon(tmpentry->value, entry->value)) {
319 goto err;
320 }
321 if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
322 goto err;
323 }
324 tmpentry = NULL;
325 }
326
327 // Finally generate encoding
328
329 len = i2d_name_canon(intname, NULL);
330 if (len < 0) {
331 goto err;
332 }
333 a->canon_enclen = len;
334
335 p = OPENSSL_malloc(a->canon_enclen);
336
337 if (!p) {
338 goto err;
339 }
340
341 a->canon_enc = p;
342
343 if (i2d_name_canon(intname, &p) < 0) {
344 goto err;
345 }
346
347 ret = 1;
348
349 err:
350
351 if (tmpentry) {
352 X509_NAME_ENTRY_free(tmpentry);
353 }
354 if (intname) {
355 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
356 local_sk_X509_NAME_ENTRY_pop_free);
357 }
358 return ret;
359 }
360
361 // Bitmap of all the types of string that will be canonicalized.
362
363 #define ASN1_MASK_CANON \
364 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING | \
365 B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING | \
366 B_ASN1_VISIBLESTRING)
367
368 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) {
369 unsigned char *to, *from;
370 int len, i;
371
372 // If type not in bitmask just copy string across
373 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
374 if (!ASN1_STRING_copy(out, in)) {
375 return 0;
376 }
377 return 1;
378 }
379
380 out->type = V_ASN1_UTF8STRING;
381 out->length = ASN1_STRING_to_UTF8(&out->data, in);
382 if (out->length == -1) {
383 return 0;
384 }
385
386 to = out->data;
387 from = to;
388
389 len = out->length;
390
391 // Convert string in place to canonical form.
392
393 // Ignore leading spaces
394 while ((len > 0) && OPENSSL_isspace(*from)) {
395 from++;
396 len--;
397 }
398
399 to = from + len;
400
401 // Ignore trailing spaces
402 while ((len > 0) && OPENSSL_isspace(to[-1])) {
403 to--;
404 len--;
405 }
406
407 to = out->data;
408
409 i = 0;
410 while (i < len) {
411 // Collapse multiple spaces
412 if (OPENSSL_isspace(*from)) {
413 // Copy one space across
414 *to++ = ' ';
415 // Ignore subsequent spaces. Note: don't need to check len here
416 // because we know the last character is a non-space so we can't
417 // overflow.
418 do {
419 from++;
420 i++;
421 } while (OPENSSL_isspace(*from));
422 } else {
423 *to++ = OPENSSL_tolower(*from);
424 from++;
425 i++;
426 }
427 }
428
429 out->length = to - out->data;
430
431 return 1;
432 }
433
434 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname,
435 unsigned char **in) {
436 int len, ltmp;
437 size_t i;
438 ASN1_VALUE *v;
439 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
440
441 len = 0;
442 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
443 v = sk_ASN1_VALUE_value(intname, i);
444 ltmp = ASN1_item_ex_i2d(&v, in, ASN1_ITEM_rptr(X509_NAME_ENTRIES),
445 /*tag=*/-1, /*aclass=*/0);
446 if (ltmp < 0) {
447 return ltmp;
448 }
449 len += ltmp;
450 }
451 return len;
452 }
453
454 int X509_NAME_set(X509_NAME **xn, X509_NAME *name) {
455 if ((name = X509_NAME_dup(name)) == NULL) {
456 return 0;
457 }
458 X509_NAME_free(*xn);
459 *xn = name;
460 return 1;
461 }
462
463 int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) { return ne->set; }
464
465 int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **out_der,
466 size_t *out_der_len) {
467 // Make sure encoding is valid
468 if (i2d_X509_NAME(nm, NULL) <= 0) {
469 return 0;
470 }
471 if (out_der != NULL) {
472 *out_der = (unsigned char *)nm->bytes->data;
473 }
474 if (out_der_len != NULL) {
475 *out_der_len = nm->bytes->length;
476 }
477 return 1;
478 }