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