1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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 /*
78  * Maximum length of X509_NAME: much larger than anything we should
79  * ever see in practice.
80  */
81 
82 #define X509_NAME_MAX (1024 * 1024)
83 
84 static int x509_name_ex_d2i(ASN1_VALUE **val,
85                             const unsigned char **in, long len,
86                             const ASN1_ITEM *it,
87                             int tag, int aclass, char opt, ASN1_TLC *ctx);
88 
89 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
90                             const ASN1_ITEM *it, int tag, int aclass);
91 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
92 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
93 
94 static int x509_name_encode(X509_NAME *a);
95 static int x509_name_canon(X509_NAME *a);
96 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
97 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
98                           unsigned char **in);
99 
100 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
101         ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
102         ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
103 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
104 
105 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
106 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
107 
108 /*
109  * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
110  * declare two template wrappers for this
111  */
112 
113 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
114         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
115 ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
116 
117 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
118         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
119 ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
120 
121 /*
122  * Normally that's where it would end: we'd have two nested STACK structures
123  * representing the ASN1. Unfortunately X509_NAME uses a completely different
124  * form and caches encodings so we have to process the internal form and
125  * convert to the external form.
126  */
127 
128 static const ASN1_EXTERN_FUNCS x509_name_ff = {
129     NULL,
130     x509_name_ex_new,
131     x509_name_ex_free,
132     0,                          /* Default clear behaviour is OK */
133     x509_name_ex_d2i,
134     x509_name_ex_i2d,
135     NULL,
136 };
137 
IMPLEMENT_EXTERN_ASN1(X509_NAME,V_ASN1_SEQUENCE,x509_name_ff)138 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
139 
140 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
141 
142 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
143 
144 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
145 {
146     X509_NAME *ret = NULL;
147     ret = OPENSSL_malloc(sizeof(X509_NAME));
148     if (!ret)
149         goto memerr;
150     if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
151         goto memerr;
152     if ((ret->bytes = BUF_MEM_new()) == NULL)
153         goto memerr;
154     ret->canon_enc = NULL;
155     ret->canon_enclen = 0;
156     ret->modified = 1;
157     *val = (ASN1_VALUE *)ret;
158     return 1;
159 
160  memerr:
161     OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
162     if (ret) {
163         if (ret->entries)
164             sk_X509_NAME_ENTRY_free(ret->entries);
165         OPENSSL_free(ret);
166     }
167     return 0;
168 }
169 
x509_name_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)170 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
171 {
172     X509_NAME *a;
173     if (!pval || !*pval)
174         return;
175     a = (X509_NAME *)*pval;
176 
177     BUF_MEM_free(a->bytes);
178     sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
179     if (a->canon_enc)
180         OPENSSL_free(a->canon_enc);
181     OPENSSL_free(a);
182     *pval = NULL;
183 }
184 
local_sk_X509_NAME_ENTRY_free(STACK_OF (X509_NAME_ENTRY)* ne)185 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
186 {
187     sk_X509_NAME_ENTRY_free(ne);
188 }
189 
local_sk_X509_NAME_ENTRY_pop_free(STACK_OF (X509_NAME_ENTRY)* ne)190 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
191 {
192     sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
193 }
194 
x509_name_ex_d2i(ASN1_VALUE ** val,const unsigned char ** in,long len,const ASN1_ITEM * it,int tag,int aclass,char opt,ASN1_TLC * ctx)195 static int x509_name_ex_d2i(ASN1_VALUE **val,
196                             const unsigned char **in, long len,
197                             const ASN1_ITEM *it, int tag, int aclass,
198                             char opt, ASN1_TLC *ctx)
199 {
200     const unsigned char *p = *in, *q;
201     STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
202     X509_NAME *nm = NULL;
203     size_t i, j;
204     int ret;
205     STACK_OF(X509_NAME_ENTRY) *entries;
206     X509_NAME_ENTRY *entry;
207     /* Bound the size of an X509_NAME we are willing to parse. */
208     if (len > X509_NAME_MAX) {
209         len = X509_NAME_MAX;
210     }
211     q = p;
212 
213     /* Get internal representation of Name */
214     ASN1_VALUE *intname_val = NULL;
215     ret = ASN1_item_ex_d2i(&intname_val,
216                            &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
217                            tag, aclass, opt, ctx);
218     if (ret <= 0)
219         return ret;
220     intname = (STACK_OF(STACK_OF_X509_NAME_ENTRY) *)intname_val;
221 
222     if (*val)
223         x509_name_ex_free(val, NULL);
224     ASN1_VALUE *nm_val = NULL;
225     if (!x509_name_ex_new(&nm_val, NULL))
226         goto err;
227     nm = (X509_NAME *)nm_val;
228     /* We've decoded it: now cache encoding */
229     if (!BUF_MEM_grow(nm->bytes, p - q))
230         goto err;
231     OPENSSL_memcpy(nm->bytes->data, q, p - q);
232 
233     /* Convert internal representation to X509_NAME structure */
234     for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname); i++) {
235         entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname, i);
236         for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
237             entry = sk_X509_NAME_ENTRY_value(entries, j);
238             entry->set = i;
239             if (!sk_X509_NAME_ENTRY_push(nm->entries, entry))
240                 goto err;
241             (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
242         }
243     }
244     ret = x509_name_canon(nm);
245     if (!ret)
246         goto err;
247     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
248                                          local_sk_X509_NAME_ENTRY_free);
249     nm->modified = 0;
250     *val = (ASN1_VALUE *)nm;
251     *in = p;
252     return ret;
253  err:
254     X509_NAME_free(nm);
255     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
256                                          local_sk_X509_NAME_ENTRY_pop_free);
257     OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
258     return 0;
259 }
260 
x509_name_ex_i2d(ASN1_VALUE ** val,unsigned char ** out,const ASN1_ITEM * it,int tag,int aclass)261 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
262                             const ASN1_ITEM *it, int tag, int aclass)
263 {
264     X509_NAME *a = (X509_NAME *)*val;
265     if (a->modified &&
266         (!x509_name_encode(a) ||
267          !x509_name_canon(a))) {
268         return -1;
269     }
270     int ret = a->bytes->length;
271     if (out != NULL) {
272         OPENSSL_memcpy(*out, a->bytes->data, ret);
273         *out += ret;
274     }
275     return ret;
276 }
277 
x509_name_encode(X509_NAME * a)278 static int x509_name_encode(X509_NAME *a)
279 {
280     int len;
281     unsigned char *p;
282     STACK_OF(X509_NAME_ENTRY) *entries = NULL;
283     X509_NAME_ENTRY *entry;
284     int set = -1;
285     size_t i;
286     STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname =
287         sk_STACK_OF_X509_NAME_ENTRY_new_null();
288     if (!intname)
289         goto memerr;
290     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
291         entry = sk_X509_NAME_ENTRY_value(a->entries, i);
292         if (entry->set != set) {
293             entries = sk_X509_NAME_ENTRY_new_null();
294             if (!entries)
295                 goto memerr;
296             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
297                 sk_X509_NAME_ENTRY_free(entries);
298                 goto memerr;
299             }
300             set = entry->set;
301         }
302         if (!sk_X509_NAME_ENTRY_push(entries, entry))
303             goto memerr;
304     }
305     ASN1_VALUE *intname_val = (ASN1_VALUE *)intname;
306     len =
307         ASN1_item_ex_i2d(&intname_val, NULL, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
308                          /*tag=*/-1, /*aclass=*/0);
309     if (len <= 0) {
310       goto err;
311     }
312     if (!BUF_MEM_grow(a->bytes, len))
313         goto memerr;
314     p = (unsigned char *)a->bytes->data;
315     if (ASN1_item_ex_i2d(&intname_val, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
316                          /*tag=*/-1, /*aclass=*/0) <= 0) {
317         goto err;
318     }
319     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
320                                          local_sk_X509_NAME_ENTRY_free);
321     a->modified = 0;
322     return 1;
323  memerr:
324     OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
325 err:
326     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
327                                          local_sk_X509_NAME_ENTRY_free);
328     return 0;
329 }
330 
331 /*
332  * This function generates the canonical encoding of the Name structure. In
333  * it all strings are converted to UTF8, leading, trailing and multiple
334  * spaces collapsed, converted to lower case and the leading SEQUENCE header
335  * removed. In future we could also normalize the UTF8 too. By doing this
336  * comparison of Name structures can be rapidly perfomed by just using
337  * OPENSSL_memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
338  * constraints of type dirName can also be checked with a simple OPENSSL_memcmp().
339  */
340 
x509_name_canon(X509_NAME * a)341 static int x509_name_canon(X509_NAME *a)
342 {
343     unsigned char *p;
344     STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
345     STACK_OF(X509_NAME_ENTRY) *entries = NULL;
346     X509_NAME_ENTRY *entry, *tmpentry = NULL;
347     int set = -1, ret = 0, len;
348     size_t i;
349 
350     if (a->canon_enc) {
351         OPENSSL_free(a->canon_enc);
352         a->canon_enc = NULL;
353     }
354     /* Special case: empty X509_NAME => null encoding */
355     if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
356         a->canon_enclen = 0;
357         return 1;
358     }
359     intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
360     if (!intname)
361         goto err;
362     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
363         entry = sk_X509_NAME_ENTRY_value(a->entries, i);
364         if (entry->set != set) {
365             entries = sk_X509_NAME_ENTRY_new_null();
366             if (!entries)
367                 goto err;
368             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
369                 sk_X509_NAME_ENTRY_free(entries);
370                 goto err;
371             }
372             set = entry->set;
373         }
374         tmpentry = X509_NAME_ENTRY_new();
375         if (tmpentry == NULL)
376             goto err;
377         tmpentry->object = OBJ_dup(entry->object);
378         if (!asn1_string_canon(tmpentry->value, entry->value))
379             goto err;
380         if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
381             goto err;
382         tmpentry = NULL;
383     }
384 
385     /* Finally generate encoding */
386 
387     len = i2d_name_canon(intname, NULL);
388     if (len < 0) {
389         goto err;
390     }
391     a->canon_enclen = len;
392 
393     p = OPENSSL_malloc(a->canon_enclen);
394 
395     if (!p)
396         goto err;
397 
398     a->canon_enc = p;
399 
400     i2d_name_canon(intname, &p);
401 
402     ret = 1;
403 
404  err:
405 
406     if (tmpentry)
407         X509_NAME_ENTRY_free(tmpentry);
408     if (intname)
409         sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
410                                              local_sk_X509_NAME_ENTRY_pop_free);
411     return ret;
412 }
413 
414 /* Bitmap of all the types of string that will be canonicalized. */
415 
416 #define ASN1_MASK_CANON \
417         (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
418         | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
419         | B_ASN1_VISIBLESTRING)
420 
asn1_string_canon(ASN1_STRING * out,ASN1_STRING * in)421 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
422 {
423     unsigned char *to, *from;
424     int len, i;
425 
426     /* If type not in bitmask just copy string across */
427     if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
428         if (!ASN1_STRING_copy(out, in))
429             return 0;
430         return 1;
431     }
432 
433     out->type = V_ASN1_UTF8STRING;
434     out->length = ASN1_STRING_to_UTF8(&out->data, in);
435     if (out->length == -1)
436         return 0;
437 
438     to = out->data;
439     from = to;
440 
441     len = out->length;
442 
443     /*
444      * Convert string in place to canonical form. Ultimately we may need to
445      * handle a wider range of characters but for now ignore anything with
446      * MSB set and rely on the isspace() and tolower() functions.
447      */
448 
449     /* Ignore leading spaces */
450     while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
451         from++;
452         len--;
453     }
454 
455     to = from + len;
456 
457     /* Ignore trailing spaces */
458     while ((len > 0) && !(to[-1] & 0x80) && isspace(to[-1])) {
459         to--;
460         len--;
461     }
462 
463     to = out->data;
464 
465     i = 0;
466     while (i < len) {
467         /* If MSB set just copy across */
468         if (*from & 0x80) {
469             *to++ = *from++;
470             i++;
471         }
472         /* Collapse multiple spaces */
473         else if (isspace(*from)) {
474             /* Copy one space across */
475             *to++ = ' ';
476             /*
477              * Ignore subsequent spaces. Note: don't need to check len here
478              * because we know the last character is a non-space so we can't
479              * overflow.
480              */
481             do {
482                 from++;
483                 i++;
484             }
485             while (!(*from & 0x80) && isspace(*from));
486         } else {
487             *to++ = OPENSSL_tolower(*from);
488             from++;
489             i++;
490         }
491     }
492 
493     out->length = to - out->data;
494 
495     return 1;
496 
497 }
498 
i2d_name_canon(STACK_OF (STACK_OF_X509_NAME_ENTRY)* _intname,unsigned char ** in)499 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
500                           unsigned char **in)
501 {
502     int len, ltmp;
503     size_t i;
504     ASN1_VALUE *v;
505     STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
506 
507     len = 0;
508     for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
509         v = sk_ASN1_VALUE_value(intname, i);
510         ltmp = ASN1_item_ex_i2d(&v, in, ASN1_ITEM_rptr(X509_NAME_ENTRIES),
511                                 /*tag=*/-1, /*aclass=*/0);
512         if (ltmp < 0)
513             return ltmp;
514         len += ltmp;
515     }
516     return len;
517 }
518 
X509_NAME_set(X509_NAME ** xn,X509_NAME * name)519 int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
520 {
521     if ((name = X509_NAME_dup(name)) == NULL)
522         return 0;
523     X509_NAME_free(*xn);
524     *xn = name;
525     return 1;
526 }
527 
X509_NAME_ENTRY_set(const X509_NAME_ENTRY * ne)528 int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
529 {
530     return ne->set;
531 }
532 
X509_NAME_get0_der(X509_NAME * nm,const unsigned char ** pder,size_t * pderlen)533 int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **pder,
534                        size_t *pderlen)
535 {
536     /* Make sure encoding is valid */
537     if (i2d_X509_NAME(nm, NULL) <= 0)
538         return 0;
539     if (pder != NULL)
540         *pder = (unsigned char *)nm->bytes->data;
541     if (pderlen != NULL)
542         *pderlen = nm->bytes->length;
543     return 1;
544 }
545