xref: /aosp_15_r20/external/boringssl/src/crypto/x509/x_name.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <ctype.h>
58 #include <string.h>
59 
60 #include <openssl/asn1.h>
61 #include <openssl/asn1t.h>
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 #include <openssl/obj.h>
66 #include <openssl/stack.h>
67 #include <openssl/x509.h>
68 
69 #include "../asn1/internal.h"
70 #include "../internal.h"
71 #include "internal.h"
72 
73 
74 typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
75 DEFINE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
76 
77 // Maximum length of X509_NAME: much larger than anything we should
78 // ever see in practice.
79 
80 #define X509_NAME_MAX (1024 * 1024)
81 
82 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
83                             long len, const ASN1_ITEM *it, int opt,
84                             ASN1_TLC *ctx);
85 
86 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
87                             const ASN1_ITEM *it);
88 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
89 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
90 
91 static int x509_name_encode(X509_NAME *a);
92 static int x509_name_canon(X509_NAME *a);
93 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
94 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname,
95                           unsigned char **in);
96 
97 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
98     ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
99     ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE),
100 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
101 
102 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X509_NAME_ENTRY)
103 IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_NAME_ENTRY)
104 
105 // For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
106 // declare two template wrappers for this
107 
108 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF,
109                                                               0, RDNS,
110                                                               X509_NAME_ENTRY)
111 ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
112 
113 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
114     ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
115 ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
116 
117 // Normally that's where it would end: we'd have two nested STACK structures
118 // representing the ASN1. Unfortunately X509_NAME uses a completely different
119 // form and caches encodings so we have to process the internal form and
120 // convert to the external form.
121 
122 static const ASN1_EXTERN_FUNCS x509_name_ff = {
123     x509_name_ex_new,
124     x509_name_ex_free,
125     x509_name_ex_d2i,
126     x509_name_ex_i2d,
127 };
128 
IMPLEMENT_EXTERN_ASN1(X509_NAME,V_ASN1_SEQUENCE,x509_name_ff)129 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
130 
131 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
132 
133 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
134 
135 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) {
136   X509_NAME *ret = NULL;
137   ret = OPENSSL_malloc(sizeof(X509_NAME));
138   if (!ret) {
139     goto memerr;
140   }
141   if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) {
142     goto memerr;
143   }
144   if ((ret->bytes = BUF_MEM_new()) == NULL) {
145     goto memerr;
146   }
147   ret->canon_enc = NULL;
148   ret->canon_enclen = 0;
149   ret->modified = 1;
150   *val = (ASN1_VALUE *)ret;
151   return 1;
152 
153 memerr:
154   if (ret) {
155     if (ret->entries) {
156       sk_X509_NAME_ENTRY_free(ret->entries);
157     }
158     OPENSSL_free(ret);
159   }
160   return 0;
161 }
162 
x509_name_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)163 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
164   X509_NAME *a;
165   if (!pval || !*pval) {
166     return;
167   }
168   a = (X509_NAME *)*pval;
169 
170   BUF_MEM_free(a->bytes);
171   sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
172   if (a->canon_enc) {
173     OPENSSL_free(a->canon_enc);
174   }
175   OPENSSL_free(a);
176   *pval = NULL;
177 }
178 
local_sk_X509_NAME_ENTRY_free(STACK_OF (X509_NAME_ENTRY)* ne)179 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) {
180   sk_X509_NAME_ENTRY_free(ne);
181 }
182 
local_sk_X509_NAME_ENTRY_pop_free(STACK_OF (X509_NAME_ENTRY)* ne)183 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) {
184   sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
185 }
186 
x509_name_ex_d2i(ASN1_VALUE ** val,const unsigned char ** in,long len,const ASN1_ITEM * it,int opt,ASN1_TLC * ctx)187 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
188                             long len, const ASN1_ITEM *it, int opt,
189                             ASN1_TLC *ctx) {
190   const unsigned char *p = *in, *q;
191   STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
192   X509_NAME *nm = NULL;
193   size_t i, j;
194   int ret;
195   STACK_OF(X509_NAME_ENTRY) *entries;
196   X509_NAME_ENTRY *entry;
197   // Bound the size of an X509_NAME we are willing to parse.
198   if (len > X509_NAME_MAX) {
199     len = X509_NAME_MAX;
200   }
201   q = p;
202 
203   // Get internal representation of Name
204   ASN1_VALUE *intname_val = NULL;
205   ret = ASN1_item_ex_d2i(&intname_val, &p, len,
206                          ASN1_ITEM_rptr(X509_NAME_INTERNAL), /*tag=*/-1,
207                          /*aclass=*/0, opt, /*buf=*/NULL);
208   if (ret <= 0) {
209     return ret;
210   }
211   intname = (STACK_OF(STACK_OF_X509_NAME_ENTRY) *)intname_val;
212 
213   if (*val) {
214     x509_name_ex_free(val, NULL);
215   }
216   ASN1_VALUE *nm_val = NULL;
217   if (!x509_name_ex_new(&nm_val, NULL)) {
218     goto err;
219   }
220   nm = (X509_NAME *)nm_val;
221   // We've decoded it: now cache encoding
222   if (!BUF_MEM_grow(nm->bytes, p - q)) {
223     goto err;
224   }
225   OPENSSL_memcpy(nm->bytes->data, q, p - q);
226 
227   // Convert internal representation to X509_NAME structure
228   for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname); i++) {
229     entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname, i);
230     for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
231       entry = sk_X509_NAME_ENTRY_value(entries, j);
232       entry->set = (int)i;
233       if (!sk_X509_NAME_ENTRY_push(nm->entries, entry)) {
234         goto err;
235       }
236       (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
237     }
238   }
239   ret = x509_name_canon(nm);
240   if (!ret) {
241     goto err;
242   }
243   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
244   nm->modified = 0;
245   *val = (ASN1_VALUE *)nm;
246   *in = p;
247   return ret;
248 err:
249   X509_NAME_free(nm);
250   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
251                                        local_sk_X509_NAME_ENTRY_pop_free);
252   OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
253   return 0;
254 }
255 
x509_name_ex_i2d(ASN1_VALUE ** val,unsigned char ** out,const ASN1_ITEM * it)256 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
257                             const ASN1_ITEM *it) {
258   X509_NAME *a = (X509_NAME *)*val;
259   if (a->modified && (!x509_name_encode(a) || !x509_name_canon(a))) {
260     return -1;
261   }
262   int ret = a->bytes->length;
263   if (out != NULL) {
264     OPENSSL_memcpy(*out, a->bytes->data, ret);
265     *out += ret;
266   }
267   return ret;
268 }
269 
x509_name_encode(X509_NAME * a)270 static int x509_name_encode(X509_NAME *a) {
271   int len;
272   unsigned char *p;
273   STACK_OF(X509_NAME_ENTRY) *entries = NULL;
274   X509_NAME_ENTRY *entry;
275   int set = -1;
276   size_t i;
277   STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname =
278       sk_STACK_OF_X509_NAME_ENTRY_new_null();
279   if (!intname) {
280     goto err;
281   }
282   for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
283     entry = sk_X509_NAME_ENTRY_value(a->entries, i);
284     if (entry->set != set) {
285       entries = sk_X509_NAME_ENTRY_new_null();
286       if (!entries) {
287         goto err;
288       }
289       if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
290         sk_X509_NAME_ENTRY_free(entries);
291         goto err;
292       }
293       set = entry->set;
294     }
295     if (!sk_X509_NAME_ENTRY_push(entries, entry)) {
296       goto err;
297     }
298   }
299   ASN1_VALUE *intname_val = (ASN1_VALUE *)intname;
300   len = ASN1_item_ex_i2d(&intname_val, NULL, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
301                          /*tag=*/-1, /*aclass=*/0);
302   if (len <= 0) {
303     goto err;
304   }
305   if (!BUF_MEM_grow(a->bytes, len)) {
306     goto err;
307   }
308   p = (unsigned char *)a->bytes->data;
309   if (ASN1_item_ex_i2d(&intname_val, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
310                        /*tag=*/-1, /*aclass=*/0) <= 0) {
311     goto err;
312   }
313   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
314   a->modified = 0;
315   return 1;
316 err:
317   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
318   return 0;
319 }
320 
321 // This function generates the canonical encoding of the Name structure. In
322 // it all strings are converted to UTF8, leading, trailing and multiple
323 // spaces collapsed, converted to lower case and the leading SEQUENCE header
324 // removed. In future we could also normalize the UTF8 too. By doing this
325 // comparison of Name structures can be rapidly perfomed by just using
326 // OPENSSL_memcmp() of the canonical encoding. By omitting the leading SEQUENCE
327 // name constraints of type dirName can also be checked with a simple
328 // OPENSSL_memcmp().
329 
x509_name_canon(X509_NAME * a)330 static int x509_name_canon(X509_NAME *a) {
331   unsigned char *p;
332   STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
333   STACK_OF(X509_NAME_ENTRY) *entries = NULL;
334   X509_NAME_ENTRY *entry, *tmpentry = NULL;
335   int set = -1, ret = 0, len;
336   size_t i;
337 
338   if (a->canon_enc) {
339     OPENSSL_free(a->canon_enc);
340     a->canon_enc = NULL;
341   }
342   // Special case: empty X509_NAME => null encoding
343   if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
344     a->canon_enclen = 0;
345     return 1;
346   }
347   intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
348   if (!intname) {
349     goto err;
350   }
351   for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
352     entry = sk_X509_NAME_ENTRY_value(a->entries, i);
353     if (entry->set != set) {
354       entries = sk_X509_NAME_ENTRY_new_null();
355       if (!entries) {
356         goto err;
357       }
358       if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
359         sk_X509_NAME_ENTRY_free(entries);
360         goto err;
361       }
362       set = entry->set;
363     }
364     tmpentry = X509_NAME_ENTRY_new();
365     if (tmpentry == NULL) {
366       goto err;
367     }
368     tmpentry->object = OBJ_dup(entry->object);
369     if (!asn1_string_canon(tmpentry->value, entry->value)) {
370       goto err;
371     }
372     if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
373       goto err;
374     }
375     tmpentry = NULL;
376   }
377 
378   // Finally generate encoding
379 
380   len = i2d_name_canon(intname, NULL);
381   if (len < 0) {
382     goto err;
383   }
384   a->canon_enclen = len;
385 
386   p = OPENSSL_malloc(a->canon_enclen);
387 
388   if (!p) {
389     goto err;
390   }
391 
392   a->canon_enc = p;
393 
394   i2d_name_canon(intname, &p);
395 
396   ret = 1;
397 
398 err:
399 
400   if (tmpentry) {
401     X509_NAME_ENTRY_free(tmpentry);
402   }
403   if (intname) {
404     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
405                                          local_sk_X509_NAME_ENTRY_pop_free);
406   }
407   return ret;
408 }
409 
410 // Bitmap of all the types of string that will be canonicalized.
411 
412 #define ASN1_MASK_CANON                                            \
413   (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING | \
414    B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING |  \
415    B_ASN1_VISIBLESTRING)
416 
asn1_string_canon(ASN1_STRING * out,ASN1_STRING * in)417 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) {
418   unsigned char *to, *from;
419   int len, i;
420 
421   // If type not in bitmask just copy string across
422   if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
423     if (!ASN1_STRING_copy(out, in)) {
424       return 0;
425     }
426     return 1;
427   }
428 
429   out->type = V_ASN1_UTF8STRING;
430   out->length = ASN1_STRING_to_UTF8(&out->data, in);
431   if (out->length == -1) {
432     return 0;
433   }
434 
435   to = out->data;
436   from = to;
437 
438   len = out->length;
439 
440   // Convert string in place to canonical form.
441 
442   // Ignore leading spaces
443   while ((len > 0) && OPENSSL_isspace(*from)) {
444     from++;
445     len--;
446   }
447 
448   to = from + len;
449 
450   // Ignore trailing spaces
451   while ((len > 0) && OPENSSL_isspace(to[-1])) {
452     to--;
453     len--;
454   }
455 
456   to = out->data;
457 
458   i = 0;
459   while (i < len) {
460     // Collapse multiple spaces
461     if (OPENSSL_isspace(*from)) {
462       // Copy one space across
463       *to++ = ' ';
464       // Ignore subsequent spaces. Note: don't need to check len here
465       // because we know the last character is a non-space so we can't
466       // overflow.
467       do {
468         from++;
469         i++;
470       } while (OPENSSL_isspace(*from));
471     } else {
472       *to++ = OPENSSL_tolower(*from);
473       from++;
474       i++;
475     }
476   }
477 
478   out->length = to - out->data;
479 
480   return 1;
481 }
482 
i2d_name_canon(STACK_OF (STACK_OF_X509_NAME_ENTRY)* _intname,unsigned char ** in)483 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname,
484                           unsigned char **in) {
485   int len, ltmp;
486   size_t i;
487   ASN1_VALUE *v;
488   STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
489 
490   len = 0;
491   for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
492     v = sk_ASN1_VALUE_value(intname, i);
493     ltmp = ASN1_item_ex_i2d(&v, in, ASN1_ITEM_rptr(X509_NAME_ENTRIES),
494                             /*tag=*/-1, /*aclass=*/0);
495     if (ltmp < 0) {
496       return ltmp;
497     }
498     len += ltmp;
499   }
500   return len;
501 }
502 
X509_NAME_set(X509_NAME ** xn,X509_NAME * name)503 int X509_NAME_set(X509_NAME **xn, X509_NAME *name) {
504   if ((name = X509_NAME_dup(name)) == NULL) {
505     return 0;
506   }
507   X509_NAME_free(*xn);
508   *xn = name;
509   return 1;
510 }
511 
X509_NAME_ENTRY_set(const X509_NAME_ENTRY * ne)512 int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) { return ne->set; }
513 
X509_NAME_get0_der(X509_NAME * nm,const unsigned char ** out_der,size_t * out_der_len)514 int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **out_der,
515                        size_t *out_der_len) {
516   // Make sure encoding is valid
517   if (i2d_X509_NAME(nm, NULL) <= 0) {
518     return 0;
519   }
520   if (out_der != NULL) {
521     *out_der = (unsigned char *)nm->bytes->data;
522   }
523   if (out_der_len != NULL) {
524     *out_der_len = nm->bytes->length;
525   }
526   return 1;
527 }
528