• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int ret;
265     X509_NAME *a = (X509_NAME *)*val;
266     if (a->modified) {
267         ret = x509_name_encode(a);
268         if (ret < 0)
269             return ret;
270         ret = x509_name_canon(a);
271         if (ret < 0)
272             return ret;
273     }
274     ret = a->bytes->length;
275     if (out != NULL) {
276         OPENSSL_memcpy(*out, a->bytes->data, ret);
277         *out += ret;
278     }
279     return ret;
280 }
281 
x509_name_encode(X509_NAME * a)282 static int x509_name_encode(X509_NAME *a)
283 {
284     int len;
285     unsigned char *p;
286     STACK_OF(X509_NAME_ENTRY) *entries = NULL;
287     X509_NAME_ENTRY *entry;
288     int set = -1;
289     size_t i;
290     STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname =
291         sk_STACK_OF_X509_NAME_ENTRY_new_null();
292     if (!intname)
293         goto memerr;
294     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
295         entry = sk_X509_NAME_ENTRY_value(a->entries, i);
296         if (entry->set != set) {
297             entries = sk_X509_NAME_ENTRY_new_null();
298             if (!entries)
299                 goto memerr;
300             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
301                 sk_X509_NAME_ENTRY_free(entries);
302                 goto memerr;
303             }
304             set = entry->set;
305         }
306         if (!sk_X509_NAME_ENTRY_push(entries, entry))
307             goto memerr;
308     }
309     ASN1_VALUE *intname_val = (ASN1_VALUE *)intname;
310     len = ASN1_item_ex_i2d(&intname_val, NULL,
311                            ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
312     if (!BUF_MEM_grow(a->bytes, len))
313         goto memerr;
314     p = (unsigned char *)a->bytes->data;
315     ASN1_item_ex_i2d(&intname_val,
316                      &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
317     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
318                                          local_sk_X509_NAME_ENTRY_free);
319     a->modified = 0;
320     return len;
321  memerr:
322     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
323                                          local_sk_X509_NAME_ENTRY_free);
324     OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
325     return -1;
326 }
327 
328 /*
329  * This function generates the canonical encoding of the Name structure. In
330  * it all strings are converted to UTF8, leading, trailing and multiple
331  * spaces collapsed, converted to lower case and the leading SEQUENCE header
332  * removed. In future we could also normalize the UTF8 too. By doing this
333  * comparison of Name structures can be rapidly perfomed by just using
334  * OPENSSL_memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
335  * constraints of type dirName can also be checked with a simple OPENSSL_memcmp().
336  */
337 
x509_name_canon(X509_NAME * a)338 static int x509_name_canon(X509_NAME *a)
339 {
340     unsigned char *p;
341     STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
342     STACK_OF(X509_NAME_ENTRY) *entries = NULL;
343     X509_NAME_ENTRY *entry, *tmpentry = NULL;
344     int set = -1, ret = 0, len;
345     size_t i;
346 
347     if (a->canon_enc) {
348         OPENSSL_free(a->canon_enc);
349         a->canon_enc = NULL;
350     }
351     /* Special case: empty X509_NAME => null encoding */
352     if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
353         a->canon_enclen = 0;
354         return 1;
355     }
356     intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
357     if (!intname)
358         goto err;
359     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
360         entry = sk_X509_NAME_ENTRY_value(a->entries, i);
361         if (entry->set != set) {
362             entries = sk_X509_NAME_ENTRY_new_null();
363             if (!entries)
364                 goto err;
365             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
366                 sk_X509_NAME_ENTRY_free(entries);
367                 goto err;
368             }
369             set = entry->set;
370         }
371         tmpentry = X509_NAME_ENTRY_new();
372         if (tmpentry == NULL)
373             goto err;
374         tmpentry->object = OBJ_dup(entry->object);
375         if (!asn1_string_canon(tmpentry->value, entry->value))
376             goto err;
377         if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
378             goto err;
379         tmpentry = NULL;
380     }
381 
382     /* Finally generate encoding */
383 
384     len = i2d_name_canon(intname, NULL);
385     if (len < 0) {
386         goto err;
387     }
388     a->canon_enclen = len;
389 
390     p = OPENSSL_malloc(a->canon_enclen);
391 
392     if (!p)
393         goto err;
394 
395     a->canon_enc = p;
396 
397     i2d_name_canon(intname, &p);
398 
399     ret = 1;
400 
401  err:
402 
403     if (tmpentry)
404         X509_NAME_ENTRY_free(tmpentry);
405     if (intname)
406         sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
407                                              local_sk_X509_NAME_ENTRY_pop_free);
408     return ret;
409 }
410 
411 /* Bitmap of all the types of string that will be canonicalized. */
412 
413 #define ASN1_MASK_CANON \
414         (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
415         | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
416         | B_ASN1_VISIBLESTRING)
417 
asn1_string_canon(ASN1_STRING * out,ASN1_STRING * in)418 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
419 {
420     unsigned char *to, *from;
421     int len, i;
422 
423     /* If type not in bitmask just copy string across */
424     if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
425         if (!ASN1_STRING_copy(out, in))
426             return 0;
427         return 1;
428     }
429 
430     out->type = V_ASN1_UTF8STRING;
431     out->length = ASN1_STRING_to_UTF8(&out->data, in);
432     if (out->length == -1)
433         return 0;
434 
435     to = out->data;
436     from = to;
437 
438     len = out->length;
439 
440     /*
441      * Convert string in place to canonical form. Ultimately we may need to
442      * handle a wider range of characters but for now ignore anything with
443      * MSB set and rely on the isspace() and tolower() functions.
444      */
445 
446     /* Ignore leading spaces */
447     while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
448         from++;
449         len--;
450     }
451 
452     to = from + len;
453 
454     /* Ignore trailing spaces */
455     while ((len > 0) && !(to[-1] & 0x80) && isspace(to[-1])) {
456         to--;
457         len--;
458     }
459 
460     to = out->data;
461 
462     i = 0;
463     while (i < len) {
464         /* If MSB set just copy across */
465         if (*from & 0x80) {
466             *to++ = *from++;
467             i++;
468         }
469         /* Collapse multiple spaces */
470         else if (isspace(*from)) {
471             /* Copy one space across */
472             *to++ = ' ';
473             /*
474              * Ignore subsequent spaces. Note: don't need to check len here
475              * because we know the last character is a non-space so we can't
476              * overflow.
477              */
478             do {
479                 from++;
480                 i++;
481             }
482             while (!(*from & 0x80) && isspace(*from));
483         } else {
484             *to++ = OPENSSL_tolower(*from);
485             from++;
486             i++;
487         }
488     }
489 
490     out->length = to - out->data;
491 
492     return 1;
493 
494 }
495 
i2d_name_canon(STACK_OF (STACK_OF_X509_NAME_ENTRY)* _intname,unsigned char ** in)496 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
497                           unsigned char **in)
498 {
499     int len, ltmp;
500     size_t i;
501     ASN1_VALUE *v;
502     STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
503 
504     len = 0;
505     for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
506         v = sk_ASN1_VALUE_value(intname, i);
507         ltmp = ASN1_item_ex_i2d(&v, in,
508                                 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
509         if (ltmp < 0)
510             return ltmp;
511         len += ltmp;
512     }
513     return len;
514 }
515 
X509_NAME_set(X509_NAME ** xn,X509_NAME * name)516 int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
517 {
518     if ((name = X509_NAME_dup(name)) == NULL)
519         return 0;
520     X509_NAME_free(*xn);
521     *xn = name;
522     return 1;
523 }
524 
X509_NAME_ENTRY_set(const X509_NAME_ENTRY * ne)525 int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
526 {
527     return ne->set;
528 }
529 
X509_NAME_get0_der(X509_NAME * nm,const unsigned char ** pder,size_t * pderlen)530 int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **pder,
531                        size_t *pderlen)
532 {
533     /* Make sure encoding is valid */
534     if (i2d_X509_NAME(nm, NULL) <= 0)
535         return 0;
536     if (pder != NULL)
537         *pder = (unsigned char *)nm->bytes->data;
538     if (pderlen != NULL)
539         *pderlen = nm->bytes->length;
540     return 1;
541 }
542