CTR, HASH and HMAC DRBGs in provider
[openssl.git] / crypto / x509 / x_name.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <stdio.h>
11 #include "crypto/ctype.h"
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include "crypto/x509.h"
16 #include "crypto/asn1.h"
17 #include "x509_local.h"
18
19 DEFINE_STACK_OF(X509_NAME_ENTRY)
20 DEFINE_STACK_OF(ASN1_VALUE)
21
22 /*
23  * Maximum length of X509_NAME: much larger than anything we should
24  * ever see in practice.
25  */
26
27 #define X509_NAME_MAX (1024 * 1024)
28
29 static int x509_name_ex_d2i(ASN1_VALUE **val,
30                             const unsigned char **in, long len,
31                             const ASN1_ITEM *it,
32                             int tag, int aclass, char opt, ASN1_TLC *ctx);
33
34 static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out,
35                             const ASN1_ITEM *it, int tag, int aclass);
36 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
37 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
38
39 static int x509_name_encode(X509_NAME *a);
40 static int x509_name_canon(X509_NAME *a);
41 static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in);
42 static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
43                           unsigned char **in);
44
45 static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval,
46                               int indent,
47                               const char *fname, const ASN1_PCTX *pctx);
48
49 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
50         ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
51         ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
52 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
53
54 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
55 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
56
57 /*
58  * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
59  * declare two template wrappers for this
60  */
61
62 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
63         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
64 static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
65
66 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
67         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
68 static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
69
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
77 static const ASN1_EXTERN_FUNCS x509_name_ff = {
78     NULL,
79     x509_name_ex_new,
80     x509_name_ex_free,
81     0,                          /* Default clear behaviour is OK */
82     x509_name_ex_d2i,
83     x509_name_ex_i2d,
84     x509_name_ex_print
85 };
86
87 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
88
89 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
90
91 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
92
93 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
94 {
95     X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret));
96
97     if (ret == NULL)
98         goto memerr;
99     if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
100         goto memerr;
101     if ((ret->bytes = BUF_MEM_new()) == NULL)
102         goto memerr;
103     ret->modified = 1;
104     *val = (ASN1_VALUE *)ret;
105     return 1;
106
107  memerr:
108     ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
109     if (ret) {
110         sk_X509_NAME_ENTRY_free(ret->entries);
111         OPENSSL_free(ret);
112     }
113     return 0;
114 }
115
116 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
117 {
118     X509_NAME *a;
119
120     if (pval == NULL || *pval == NULL)
121         return;
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     OPENSSL_free(a->canon_enc);
127     OPENSSL_free(a);
128     *pval = NULL;
129 }
130
131 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
132 {
133     sk_X509_NAME_ENTRY_free(ne);
134 }
135
136 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
137 {
138     sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
139 }
140
141 static int x509_name_ex_d2i(ASN1_VALUE **val,
142                             const unsigned char **in, long len,
143                             const ASN1_ITEM *it, int tag, int aclass,
144                             char opt, ASN1_TLC *ctx)
145 {
146     const unsigned char *p = *in, *q;
147     union {
148         STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
149         ASN1_VALUE *a;
150     } intname = {
151         NULL
152     };
153     union {
154         X509_NAME *x;
155         ASN1_VALUE *a;
156     } nm = {
157         NULL
158     };
159     int i, j, ret;
160     STACK_OF(X509_NAME_ENTRY) *entries;
161     X509_NAME_ENTRY *entry;
162
163     if (len > X509_NAME_MAX)
164         len = X509_NAME_MAX;
165     q = p;
166
167     /* Get internal representation of Name */
168     ret = ASN1_item_ex_d2i(&intname.a,
169                            &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
170                            tag, aclass, opt, ctx);
171
172     if (ret <= 0)
173         return ret;
174
175     if (*val)
176         x509_name_ex_free(val, NULL);
177     if (!x509_name_ex_new(&nm.a, NULL))
178         goto err;
179     /* We've decoded it: now cache encoding */
180     if (!BUF_MEM_grow(nm.x->bytes, p - q))
181         goto err;
182     memcpy(nm.x->bytes->data, q, p - q);
183
184     /* Convert internal representation to X509_NAME structure */
185     for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
186         entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
187         for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
188             entry = sk_X509_NAME_ENTRY_value(entries, j);
189             entry->set = i;
190             if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
191                 goto err;
192             sk_X509_NAME_ENTRY_set(entries, j, NULL);
193         }
194     }
195     ret = x509_name_canon(nm.x);
196     if (!ret)
197         goto err;
198     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
199                                          local_sk_X509_NAME_ENTRY_free);
200     nm.x->modified = 0;
201     *val = nm.a;
202     *in = p;
203     return ret;
204
205  err:
206     if (nm.x != NULL)
207         X509_NAME_free(nm.x);
208     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
209                                          local_sk_X509_NAME_ENTRY_pop_free);
210     ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
211     return 0;
212 }
213
214 static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out,
215                             const ASN1_ITEM *it, int tag, int aclass)
216 {
217     int ret;
218     X509_NAME *a = (X509_NAME *)*val;
219
220     if (a->modified) {
221         ret = x509_name_encode(a);
222         if (ret < 0)
223             return ret;
224         ret = x509_name_canon(a);
225         if (ret < 0)
226             return ret;
227     }
228     ret = a->bytes->length;
229     if (out != NULL) {
230         memcpy(*out, a->bytes->data, ret);
231         *out += ret;
232     }
233     return ret;
234 }
235
236 static int x509_name_encode(X509_NAME *a)
237 {
238     union {
239         STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
240         const ASN1_VALUE *a;
241     } intname = {
242         NULL
243     };
244     int len;
245     unsigned char *p;
246     STACK_OF(X509_NAME_ENTRY) *entries = NULL;
247     X509_NAME_ENTRY *entry;
248     int i, set = -1;
249
250     intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
251     if (!intname.s)
252         goto memerr;
253     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
254         entry = sk_X509_NAME_ENTRY_value(a->entries, i);
255         if (entry->set != set) {
256             entries = sk_X509_NAME_ENTRY_new_null();
257             if (!entries)
258                 goto memerr;
259             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) {
260                 sk_X509_NAME_ENTRY_free(entries);
261                 goto memerr;
262             }
263             set = entry->set;
264         }
265         if (!sk_X509_NAME_ENTRY_push(entries, entry))
266             goto memerr;
267     }
268     len = ASN1_item_ex_i2d(&intname.a, NULL,
269                            ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
270     if (!BUF_MEM_grow(a->bytes, len))
271         goto memerr;
272     p = (unsigned char *)a->bytes->data;
273     ASN1_item_ex_i2d(&intname.a,
274                      &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
275     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
276                                          local_sk_X509_NAME_ENTRY_free);
277     a->modified = 0;
278     return len;
279  memerr:
280     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
281                                          local_sk_X509_NAME_ENTRY_free);
282     ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
283     return -1;
284 }
285
286 static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval,
287                               int indent,
288                               const char *fname, const ASN1_PCTX *pctx)
289 {
290     if (X509_NAME_print_ex(out, (const X509_NAME *)*pval,
291                            indent, pctx->nm_flags) <= 0)
292         return 0;
293     return 2;
294 }
295
296 /*
297  * This function generates the canonical encoding of the Name structure. In
298  * it all strings are converted to UTF8, leading, trailing and multiple
299  * spaces collapsed, converted to lower case and the leading SEQUENCE header
300  * removed. In future we could also normalize the UTF8 too. By doing this
301  * comparison of Name structures can be rapidly performed by just using
302  * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
303  * constraints of type dirName can also be checked with a simple memcmp().
304  */
305
306 static int x509_name_canon(X509_NAME *a)
307 {
308     unsigned char *p;
309     STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname;
310     STACK_OF(X509_NAME_ENTRY) *entries = NULL;
311     X509_NAME_ENTRY *entry, *tmpentry = NULL;
312     int i, set = -1, ret = 0, len;
313
314     OPENSSL_free(a->canon_enc);
315     a->canon_enc = NULL;
316     /* Special case: empty X509_NAME => null encoding */
317     if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
318         a->canon_enclen = 0;
319         return 1;
320     }
321     intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
322     if (intname == NULL) {
323         X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
324         goto err;
325     }
326     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
327         entry = sk_X509_NAME_ENTRY_value(a->entries, i);
328         if (entry->set != set) {
329             entries = sk_X509_NAME_ENTRY_new_null();
330             if (entries == NULL)
331                 goto err;
332             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
333                 sk_X509_NAME_ENTRY_free(entries);
334                 X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
335                 goto err;
336             }
337             set = entry->set;
338         }
339         tmpentry = X509_NAME_ENTRY_new();
340         if (tmpentry == NULL) {
341             X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
342             goto err;
343         }
344         tmpentry->object = OBJ_dup(entry->object);
345         if (tmpentry->object == NULL) {
346             X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
347             goto err;
348         }
349         if (!asn1_string_canon(tmpentry->value, entry->value))
350             goto err;
351         if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
352             X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
353             goto err;
354         }
355         tmpentry = NULL;
356     }
357
358     /* Finally generate encoding */
359     len = i2d_name_canon(intname, NULL);
360     if (len < 0)
361         goto err;
362     a->canon_enclen = len;
363
364     p = OPENSSL_malloc(a->canon_enclen);
365     if (p == NULL) {
366         X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
367         goto err;
368     }
369
370     a->canon_enc = p;
371
372     i2d_name_canon(intname, &p);
373
374     ret = 1;
375
376  err:
377     X509_NAME_ENTRY_free(tmpentry);
378     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
379                                          local_sk_X509_NAME_ENTRY_pop_free);
380     return ret;
381 }
382
383 /* Bitmap of all the types of string that will be canonicalized. */
384
385 #define ASN1_MASK_CANON \
386         (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
387         | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
388         | B_ASN1_VISIBLESTRING)
389
390 static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in)
391 {
392     unsigned char *to, *from;
393     int len, i;
394
395     /* If type not in bitmask just copy string across */
396     if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
397         if (!ASN1_STRING_copy(out, in))
398             return 0;
399         return 1;
400     }
401
402     out->type = V_ASN1_UTF8STRING;
403     out->length = ASN1_STRING_to_UTF8(&out->data, in);
404     if (out->length == -1)
405         return 0;
406
407     to = out->data;
408     from = to;
409
410     len = out->length;
411
412     /*
413      * Convert string in place to canonical form. Ultimately we may need to
414      * handle a wider range of characters but for now ignore anything with
415      * MSB set and rely on the ossl_isspace() to fail on bad characters without
416      * needing isascii or range checks as well.
417      */
418
419     /* Ignore leading spaces */
420     while (len > 0 && ossl_isspace(*from)) {
421         from++;
422         len--;
423     }
424
425     to = from + len;
426
427     /* Ignore trailing spaces */
428     while (len > 0 && ossl_isspace(to[-1])) {
429         to--;
430         len--;
431     }
432
433     to = out->data;
434
435     i = 0;
436     while (i < len) {
437         /* If not ASCII set just copy across */
438         if (!ossl_isascii(*from)) {
439             *to++ = *from++;
440             i++;
441         }
442         /* Collapse multiple spaces */
443         else if (ossl_isspace(*from)) {
444             /* Copy one space across */
445             *to++ = ' ';
446             /*
447              * Ignore subsequent spaces. Note: don't need to check len here
448              * because we know the last character is a non-space so we can't
449              * overflow.
450              */
451             do {
452                 from++;
453                 i++;
454             }
455             while (ossl_isspace(*from));
456         } else {
457             *to++ = ossl_tolower(*from);
458             from++;
459             i++;
460         }
461     }
462
463     out->length = to - out->data;
464
465     return 1;
466
467 }
468
469 static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
470                           unsigned char **in)
471 {
472     int i, len, ltmp;
473     const ASN1_VALUE *v;
474     STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
475
476     len = 0;
477     for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
478         v = sk_ASN1_VALUE_value(intname, i);
479         ltmp = ASN1_item_ex_i2d(&v, in,
480                                 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
481         if (ltmp < 0)
482             return ltmp;
483         len += ltmp;
484     }
485     return len;
486 }
487
488 int X509_NAME_set(X509_NAME **xn, const X509_NAME *name)
489 {
490     X509_NAME *name_copy;
491
492     if (*xn == name)
493         return *xn != NULL;
494     if ((name_copy = X509_NAME_dup(name)) == NULL)
495         return 0;
496     X509_NAME_free(*xn);
497     *xn = name_copy;
498     return 1;
499 }
500
501 int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
502 {
503     char *s, *c, *b;
504     int l, i;
505
506     l = 80 - 2 - obase;
507
508     b = X509_NAME_oneline(name, NULL, 0);
509     if (b == NULL)
510         return 0;
511     if (*b == '\0') {
512         OPENSSL_free(b);
513         return 1;
514     }
515     s = b + 1;                  /* skip the first slash */
516
517     c = s;
518     for (;;) {
519         if (((*s == '/') &&
520              (ossl_isupper(s[1]) && ((s[2] == '=') ||
521                                 (ossl_isupper(s[2]) && (s[3] == '='))
522               ))) || (*s == '\0'))
523         {
524             i = s - c;
525             if (BIO_write(bp, c, i) != i)
526                 goto err;
527             c = s + 1;          /* skip following slash */
528             if (*s != '\0') {
529                 if (BIO_write(bp, ", ", 2) != 2)
530                     goto err;
531             }
532             l--;
533         }
534         if (*s == '\0')
535             break;
536         s++;
537         l--;
538     }
539
540     OPENSSL_free(b);
541     return 1;
542  err:
543     X509err(X509_F_X509_NAME_PRINT, ERR_R_BUF_LIB);
544     OPENSSL_free(b);
545     return 0;
546 }
547
548 int X509_NAME_get0_der(const X509_NAME *nm, const unsigned char **pder,
549                        size_t *pderlen)
550 {
551     /* Make sure encoding is valid */
552     if (i2d_X509_NAME(nm, NULL) <= 0)
553         return 0;
554     if (pder != NULL)
555         *pder = (unsigned char *)nm->bytes->data;
556     if (pderlen != NULL)
557         *pderlen = nm->bytes->length;
558     return 1;
559 }