• 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 // 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_FUNCTIONS_const(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     0,  // Default clear behaviour is OK
126     x509_name_ex_d2i,
127     x509_name_ex_i2d,
128 };
129 
IMPLEMENT_EXTERN_ASN1(X509_NAME,V_ASN1_SEQUENCE,x509_name_ff)130 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
131 
132 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
133 
134 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
135 
136 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) {
137   X509_NAME *ret = NULL;
138   ret = OPENSSL_malloc(sizeof(X509_NAME));
139   if (!ret) {
140     goto memerr;
141   }
142   if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) {
143     goto memerr;
144   }
145   if ((ret->bytes = BUF_MEM_new()) == NULL) {
146     goto memerr;
147   }
148   ret->canon_enc = NULL;
149   ret->canon_enclen = 0;
150   ret->modified = 1;
151   *val = (ASN1_VALUE *)ret;
152   return 1;
153 
154 memerr:
155   if (ret) {
156     if (ret->entries) {
157       sk_X509_NAME_ENTRY_free(ret->entries);
158     }
159     OPENSSL_free(ret);
160   }
161   return 0;
162 }
163 
x509_name_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)164 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
165   X509_NAME *a;
166   if (!pval || !*pval) {
167     return;
168   }
169   a = (X509_NAME *)*pval;
170 
171   BUF_MEM_free(a->bytes);
172   sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
173   if (a->canon_enc) {
174     OPENSSL_free(a->canon_enc);
175   }
176   OPENSSL_free(a);
177   *pval = NULL;
178 }
179 
local_sk_X509_NAME_ENTRY_free(STACK_OF (X509_NAME_ENTRY)* ne)180 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) {
181   sk_X509_NAME_ENTRY_free(ne);
182 }
183 
local_sk_X509_NAME_ENTRY_pop_free(STACK_OF (X509_NAME_ENTRY)* ne)184 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) {
185   sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
186 }
187 
x509_name_ex_d2i(ASN1_VALUE ** val,const unsigned char ** in,long len,const ASN1_ITEM * it,int opt,ASN1_TLC * ctx)188 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
189                             long len, const ASN1_ITEM *it, int opt,
190                             ASN1_TLC *ctx) {
191   const unsigned char *p = *in, *q;
192   STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
193   X509_NAME *nm = NULL;
194   size_t i, j;
195   int ret;
196   STACK_OF(X509_NAME_ENTRY) *entries;
197   X509_NAME_ENTRY *entry;
198   // Bound the size of an X509_NAME we are willing to parse.
199   if (len > X509_NAME_MAX) {
200     len = X509_NAME_MAX;
201   }
202   q = p;
203 
204   // Get internal representation of Name
205   ASN1_VALUE *intname_val = NULL;
206   ret = ASN1_item_ex_d2i(&intname_val, &p, len,
207                          ASN1_ITEM_rptr(X509_NAME_INTERNAL), /*tag=*/-1,
208                          /*aclass=*/0, opt, /*buf=*/NULL);
209   if (ret <= 0) {
210     return ret;
211   }
212   intname = (STACK_OF(STACK_OF_X509_NAME_ENTRY) *)intname_val;
213 
214   if (*val) {
215     x509_name_ex_free(val, NULL);
216   }
217   ASN1_VALUE *nm_val = NULL;
218   if (!x509_name_ex_new(&nm_val, NULL)) {
219     goto err;
220   }
221   nm = (X509_NAME *)nm_val;
222   // We've decoded it: now cache encoding
223   if (!BUF_MEM_grow(nm->bytes, p - q)) {
224     goto err;
225   }
226   OPENSSL_memcpy(nm->bytes->data, q, p - q);
227 
228   // Convert internal representation to X509_NAME structure
229   for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname); i++) {
230     entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname, i);
231     for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
232       entry = sk_X509_NAME_ENTRY_value(entries, j);
233       entry->set = i;
234       if (!sk_X509_NAME_ENTRY_push(nm->entries, entry)) {
235         goto err;
236       }
237       (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
238     }
239   }
240   ret = x509_name_canon(nm);
241   if (!ret) {
242     goto err;
243   }
244   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
245   nm->modified = 0;
246   *val = (ASN1_VALUE *)nm;
247   *in = p;
248   return ret;
249 err:
250   X509_NAME_free(nm);
251   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
252                                        local_sk_X509_NAME_ENTRY_pop_free);
253   OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
254   return 0;
255 }
256 
x509_name_ex_i2d(ASN1_VALUE ** val,unsigned char ** out,const ASN1_ITEM * it)257 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
258                             const ASN1_ITEM *it) {
259   X509_NAME *a = (X509_NAME *)*val;
260   if (a->modified && (!x509_name_encode(a) || !x509_name_canon(a))) {
261     return -1;
262   }
263   int ret = a->bytes->length;
264   if (out != NULL) {
265     OPENSSL_memcpy(*out, a->bytes->data, ret);
266     *out += ret;
267   }
268   return ret;
269 }
270 
x509_name_encode(X509_NAME * a)271 static int x509_name_encode(X509_NAME *a) {
272   int len;
273   unsigned char *p;
274   STACK_OF(X509_NAME_ENTRY) *entries = NULL;
275   X509_NAME_ENTRY *entry;
276   int set = -1;
277   size_t i;
278   STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname =
279       sk_STACK_OF_X509_NAME_ENTRY_new_null();
280   if (!intname) {
281     goto err;
282   }
283   for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
284     entry = sk_X509_NAME_ENTRY_value(a->entries, i);
285     if (entry->set != set) {
286       entries = sk_X509_NAME_ENTRY_new_null();
287       if (!entries) {
288         goto err;
289       }
290       if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
291         sk_X509_NAME_ENTRY_free(entries);
292         goto err;
293       }
294       set = entry->set;
295     }
296     if (!sk_X509_NAME_ENTRY_push(entries, entry)) {
297       goto err;
298     }
299   }
300   ASN1_VALUE *intname_val = (ASN1_VALUE *)intname;
301   len = ASN1_item_ex_i2d(&intname_val, NULL, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
302                          /*tag=*/-1, /*aclass=*/0);
303   if (len <= 0) {
304     goto err;
305   }
306   if (!BUF_MEM_grow(a->bytes, len)) {
307     goto err;
308   }
309   p = (unsigned char *)a->bytes->data;
310   if (ASN1_item_ex_i2d(&intname_val, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
311                        /*tag=*/-1, /*aclass=*/0) <= 0) {
312     goto err;
313   }
314   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
315   a->modified = 0;
316   return 1;
317 err:
318   sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
319   return 0;
320 }
321 
322 // This function generates the canonical encoding of the Name structure. In
323 // it all strings are converted to UTF8, leading, trailing and multiple
324 // spaces collapsed, converted to lower case and the leading SEQUENCE header
325 // removed. In future we could also normalize the UTF8 too. By doing this
326 // comparison of Name structures can be rapidly perfomed by just using
327 // OPENSSL_memcmp() of the canonical encoding. By omitting the leading SEQUENCE
328 // name constraints of type dirName can also be checked with a simple
329 // OPENSSL_memcmp().
330 
x509_name_canon(X509_NAME * a)331 static int x509_name_canon(X509_NAME *a) {
332   unsigned char *p;
333   STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
334   STACK_OF(X509_NAME_ENTRY) *entries = NULL;
335   X509_NAME_ENTRY *entry, *tmpentry = NULL;
336   int set = -1, ret = 0, len;
337   size_t i;
338 
339   if (a->canon_enc) {
340     OPENSSL_free(a->canon_enc);
341     a->canon_enc = NULL;
342   }
343   // Special case: empty X509_NAME => null encoding
344   if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
345     a->canon_enclen = 0;
346     return 1;
347   }
348   intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
349   if (!intname) {
350     goto err;
351   }
352   for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
353     entry = sk_X509_NAME_ENTRY_value(a->entries, i);
354     if (entry->set != set) {
355       entries = sk_X509_NAME_ENTRY_new_null();
356       if (!entries) {
357         goto err;
358       }
359       if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
360         sk_X509_NAME_ENTRY_free(entries);
361         goto err;
362       }
363       set = entry->set;
364     }
365     tmpentry = X509_NAME_ENTRY_new();
366     if (tmpentry == NULL) {
367       goto err;
368     }
369     tmpentry->object = OBJ_dup(entry->object);
370     if (!asn1_string_canon(tmpentry->value, entry->value)) {
371       goto err;
372     }
373     if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
374       goto err;
375     }
376     tmpentry = NULL;
377   }
378 
379   // Finally generate encoding
380 
381   len = i2d_name_canon(intname, NULL);
382   if (len < 0) {
383     goto err;
384   }
385   a->canon_enclen = len;
386 
387   p = OPENSSL_malloc(a->canon_enclen);
388 
389   if (!p) {
390     goto err;
391   }
392 
393   a->canon_enc = p;
394 
395   i2d_name_canon(intname, &p);
396 
397   ret = 1;
398 
399 err:
400 
401   if (tmpentry) {
402     X509_NAME_ENTRY_free(tmpentry);
403   }
404   if (intname) {
405     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
406                                          local_sk_X509_NAME_ENTRY_pop_free);
407   }
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   unsigned char *to, *from;
420   int len, i;
421 
422   // If type not in bitmask just copy string across
423   if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
424     if (!ASN1_STRING_copy(out, in)) {
425       return 0;
426     }
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 
436   to = out->data;
437   from = to;
438 
439   len = out->length;
440 
441   // Convert string in place to canonical form.
442 
443   // Ignore leading spaces
444   while ((len > 0) && OPENSSL_isspace(*from)) {
445     from++;
446     len--;
447   }
448 
449   to = from + len;
450 
451   // Ignore trailing spaces
452   while ((len > 0) && OPENSSL_isspace(to[-1])) {
453     to--;
454     len--;
455   }
456 
457   to = out->data;
458 
459   i = 0;
460   while (i < len) {
461     // Collapse multiple spaces
462     if (OPENSSL_isspace(*from)) {
463       // Copy one space across
464       *to++ = ' ';
465       // Ignore subsequent spaces. Note: don't need to check len here
466       // because we know the last character is a non-space so we can't
467       // overflow.
468       do {
469         from++;
470         i++;
471       } while (OPENSSL_isspace(*from));
472     } else {
473       *to++ = OPENSSL_tolower(*from);
474       from++;
475       i++;
476     }
477   }
478 
479   out->length = to - out->data;
480 
481   return 1;
482 }
483 
i2d_name_canon(STACK_OF (STACK_OF_X509_NAME_ENTRY)* _intname,unsigned char ** in)484 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname,
485                           unsigned char **in) {
486   int len, ltmp;
487   size_t i;
488   ASN1_VALUE *v;
489   STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
490 
491   len = 0;
492   for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
493     v = sk_ASN1_VALUE_value(intname, i);
494     ltmp = ASN1_item_ex_i2d(&v, in, ASN1_ITEM_rptr(X509_NAME_ENTRIES),
495                             /*tag=*/-1, /*aclass=*/0);
496     if (ltmp < 0) {
497       return ltmp;
498     }
499     len += ltmp;
500   }
501   return len;
502 }
503 
X509_NAME_set(X509_NAME ** xn,X509_NAME * name)504 int X509_NAME_set(X509_NAME **xn, X509_NAME *name) {
505   if ((name = X509_NAME_dup(name)) == NULL) {
506     return 0;
507   }
508   X509_NAME_free(*xn);
509   *xn = name;
510   return 1;
511 }
512 
X509_NAME_ENTRY_set(const X509_NAME_ENTRY * ne)513 int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) { return ne->set; }
514 
X509_NAME_get0_der(X509_NAME * nm,const unsigned char ** pder,size_t * pderlen)515 int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **pder,
516                        size_t *pderlen) {
517   // Make sure encoding is valid
518   if (i2d_X509_NAME(nm, NULL) <= 0) {
519     return 0;
520   }
521   if (pder != NULL) {
522     *pder = (unsigned char *)nm->bytes->data;
523   }
524   if (pderlen != NULL) {
525     *pderlen = nm->bytes->length;
526   }
527   return 1;
528 }
529