Move e_os.h to be the very first include.
[openssl.git] / crypto / x509v3 / v3_ncons.c
1 /*
2  * Copyright 2003-2017 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 "e_os.h"               /* for strncasecmp */
11 #include "internal/cryptlib.h"
12 #include <stdio.h>
13 #include "internal/asn1_int.h"
14 #include <openssl/asn1t.h>
15 #include <openssl/conf.h>
16 #include <openssl/x509v3.h>
17
18 #include "internal/x509_int.h"
19 #include "ext_dat.h"
20
21 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
22                                   X509V3_CTX *ctx,
23                                   STACK_OF(CONF_VALUE) *nval);
24 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
25                                 BIO *bp, int ind);
26 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
27                                    STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
28                                    int ind, const char *name);
29 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
30
31 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
32 static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
33 static int nc_dn(X509_NAME *sub, X509_NAME *nm);
34 static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
35 static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
36 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
37 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
38
39 const X509V3_EXT_METHOD v3_name_constraints = {
40     NID_name_constraints, 0,
41     ASN1_ITEM_ref(NAME_CONSTRAINTS),
42     0, 0, 0, 0,
43     0, 0,
44     0, v2i_NAME_CONSTRAINTS,
45     i2r_NAME_CONSTRAINTS, 0,
46     NULL
47 };
48
49 ASN1_SEQUENCE(GENERAL_SUBTREE) = {
50         ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
51         ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
52         ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
53 } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
54
55 ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
56         ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
57                                                         GENERAL_SUBTREE, 0),
58         ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
59                                                         GENERAL_SUBTREE, 1),
60 } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
61
62
63 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
64 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
65
66 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
67                                   X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
68 {
69     int i;
70     CONF_VALUE tval, *val;
71     STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
72     NAME_CONSTRAINTS *ncons = NULL;
73     GENERAL_SUBTREE *sub = NULL;
74
75     ncons = NAME_CONSTRAINTS_new();
76     if (ncons == NULL)
77         goto memerr;
78     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
79         val = sk_CONF_VALUE_value(nval, i);
80         if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) {
81             ptree = &ncons->permittedSubtrees;
82             tval.name = val->name + 10;
83         } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) {
84             ptree = &ncons->excludedSubtrees;
85             tval.name = val->name + 9;
86         } else {
87             X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
88             goto err;
89         }
90         tval.value = val->value;
91         sub = GENERAL_SUBTREE_new();
92         if (sub == NULL)
93             goto memerr;
94         if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
95             goto err;
96         if (*ptree == NULL)
97             *ptree = sk_GENERAL_SUBTREE_new_null();
98         if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
99             goto memerr;
100         sub = NULL;
101     }
102
103     return ncons;
104
105  memerr:
106     X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
107  err:
108     NAME_CONSTRAINTS_free(ncons);
109     GENERAL_SUBTREE_free(sub);
110
111     return NULL;
112 }
113
114 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
115                                 BIO *bp, int ind)
116 {
117     NAME_CONSTRAINTS *ncons = a;
118     do_i2r_name_constraints(method, ncons->permittedSubtrees,
119                             bp, ind, "Permitted");
120     do_i2r_name_constraints(method, ncons->excludedSubtrees,
121                             bp, ind, "Excluded");
122     return 1;
123 }
124
125 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
126                                    STACK_OF(GENERAL_SUBTREE) *trees,
127                                    BIO *bp, int ind, const char *name)
128 {
129     GENERAL_SUBTREE *tree;
130     int i;
131     if (sk_GENERAL_SUBTREE_num(trees) > 0)
132         BIO_printf(bp, "%*s%s:\n", ind, "", name);
133     for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
134         tree = sk_GENERAL_SUBTREE_value(trees, i);
135         BIO_printf(bp, "%*s", ind + 2, "");
136         if (tree->base->type == GEN_IPADD)
137             print_nc_ipadd(bp, tree->base->d.ip);
138         else
139             GENERAL_NAME_print(bp, tree->base);
140         BIO_puts(bp, "\n");
141     }
142     return 1;
143 }
144
145 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
146 {
147     int i, len;
148     unsigned char *p;
149     p = ip->data;
150     len = ip->length;
151     BIO_puts(bp, "IP:");
152     if (len == 8) {
153         BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
154                    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
155     } else if (len == 32) {
156         for (i = 0; i < 16; i++) {
157             BIO_printf(bp, "%X", p[0] << 8 | p[1]);
158             p += 2;
159             if (i == 7)
160                 BIO_puts(bp, "/");
161             else if (i != 15)
162                 BIO_puts(bp, ":");
163         }
164     } else
165         BIO_printf(bp, "IP Address:<invalid>");
166     return 1;
167 }
168
169 /*-
170  * Check a certificate conforms to a specified set of constraints.
171  * Return values:
172  *  X509_V_OK: All constraints obeyed.
173  *  X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
174  *  X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
175  *  X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
176  *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE:  Unsupported constraint type.
177  *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
178  *  X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
179  */
180
181 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
182 {
183     int r, i;
184     X509_NAME *nm;
185
186     nm = X509_get_subject_name(x);
187
188     if (X509_NAME_entry_count(nm) > 0) {
189         GENERAL_NAME gntmp;
190         gntmp.type = GEN_DIRNAME;
191         gntmp.d.directoryName = nm;
192
193         r = nc_match(&gntmp, nc);
194
195         if (r != X509_V_OK)
196             return r;
197
198         gntmp.type = GEN_EMAIL;
199
200         /* Process any email address attributes in subject name */
201
202         for (i = -1;;) {
203             const X509_NAME_ENTRY *ne;
204
205             i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
206             if (i == -1)
207                 break;
208             ne = X509_NAME_get_entry(nm, i);
209             gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
210             if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
211                 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
212
213             r = nc_match(&gntmp, nc);
214
215             if (r != X509_V_OK)
216                 return r;
217         }
218
219     }
220
221     for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
222         GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
223         r = nc_match(gen, nc);
224         if (r != X509_V_OK)
225             return r;
226     }
227
228     return X509_V_OK;
229
230 }
231
232 int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
233 {
234     int r, i;
235     X509_NAME *nm;
236
237     ASN1_STRING stmp;
238     GENERAL_NAME gntmp;
239     stmp.flags = 0;
240     stmp.type = V_ASN1_IA5STRING;
241     gntmp.type = GEN_DNS;
242     gntmp.d.dNSName = &stmp;
243
244     nm = X509_get_subject_name(x);
245
246     /* Process any commonName attributes in subject name */
247
248     for (i = -1;;) {
249         X509_NAME_ENTRY *ne;
250         ASN1_STRING *hn;
251
252         i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
253         if (i == -1)
254             break;
255         ne = X509_NAME_get_entry(nm, i);
256         hn = X509_NAME_ENTRY_get_data(ne);
257         /* Only process attributes that look like host names */
258         if (asn1_valid_host(hn)) {
259             unsigned char *h;
260             int hlen = ASN1_STRING_to_UTF8(&h, hn);
261             if (hlen <= 0)
262                 return X509_V_ERR_OUT_OF_MEM;
263
264             stmp.length = hlen;
265             stmp.data = h;
266
267             r = nc_match(&gntmp, nc);
268
269             OPENSSL_free(h);
270
271             if (r != X509_V_OK)
272                     return r;
273         }
274     }
275     return X509_V_OK;
276 }
277
278 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
279 {
280     GENERAL_SUBTREE *sub;
281     int i, r, match = 0;
282
283     /*
284      * Permitted subtrees: if any subtrees exist of matching the type at
285      * least one subtree must match.
286      */
287
288     for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
289         sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
290         if (gen->type != sub->base->type)
291             continue;
292         if (sub->minimum || sub->maximum)
293             return X509_V_ERR_SUBTREE_MINMAX;
294         /* If we already have a match don't bother trying any more */
295         if (match == 2)
296             continue;
297         if (match == 0)
298             match = 1;
299         r = nc_match_single(gen, sub->base);
300         if (r == X509_V_OK)
301             match = 2;
302         else if (r != X509_V_ERR_PERMITTED_VIOLATION)
303             return r;
304     }
305
306     if (match == 1)
307         return X509_V_ERR_PERMITTED_VIOLATION;
308
309     /* Excluded subtrees: must not match any of these */
310
311     for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
312         sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
313         if (gen->type != sub->base->type)
314             continue;
315         if (sub->minimum || sub->maximum)
316             return X509_V_ERR_SUBTREE_MINMAX;
317
318         r = nc_match_single(gen, sub->base);
319         if (r == X509_V_OK)
320             return X509_V_ERR_EXCLUDED_VIOLATION;
321         else if (r != X509_V_ERR_PERMITTED_VIOLATION)
322             return r;
323
324     }
325
326     return X509_V_OK;
327
328 }
329
330 static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
331 {
332     switch (base->type) {
333     case GEN_DIRNAME:
334         return nc_dn(gen->d.directoryName, base->d.directoryName);
335
336     case GEN_DNS:
337         return nc_dns(gen->d.dNSName, base->d.dNSName);
338
339     case GEN_EMAIL:
340         return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
341
342     case GEN_URI:
343         return nc_uri(gen->d.uniformResourceIdentifier,
344                       base->d.uniformResourceIdentifier);
345
346     case GEN_IPADD:
347         return nc_ip(gen->d.iPAddress, base->d.iPAddress);
348
349     default:
350         return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
351     }
352
353 }
354
355 /*
356  * directoryName name constraint matching. The canonical encoding of
357  * X509_NAME makes this comparison easy. It is matched if the subtree is a
358  * subset of the name.
359  */
360
361 static int nc_dn(X509_NAME *nm, X509_NAME *base)
362 {
363     /* Ensure canonical encodings are up to date.  */
364     if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
365         return X509_V_ERR_OUT_OF_MEM;
366     if (base->modified && i2d_X509_NAME(base, NULL) < 0)
367         return X509_V_ERR_OUT_OF_MEM;
368     if (base->canon_enclen > nm->canon_enclen)
369         return X509_V_ERR_PERMITTED_VIOLATION;
370     if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
371         return X509_V_ERR_PERMITTED_VIOLATION;
372     return X509_V_OK;
373 }
374
375 static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
376 {
377     char *baseptr = (char *)base->data;
378     char *dnsptr = (char *)dns->data;
379     /* Empty matches everything */
380     if (!*baseptr)
381         return X509_V_OK;
382     /*
383      * Otherwise can add zero or more components on the left so compare RHS
384      * and if dns is longer and expect '.' as preceding character.
385      */
386     if (dns->length > base->length) {
387         dnsptr += dns->length - base->length;
388         if (*baseptr != '.' && dnsptr[-1] != '.')
389             return X509_V_ERR_PERMITTED_VIOLATION;
390     }
391
392     if (strcasecmp(baseptr, dnsptr))
393         return X509_V_ERR_PERMITTED_VIOLATION;
394
395     return X509_V_OK;
396
397 }
398
399 static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
400 {
401     const char *baseptr = (char *)base->data;
402     const char *emlptr = (char *)eml->data;
403
404     const char *baseat = strchr(baseptr, '@');
405     const char *emlat = strchr(emlptr, '@');
406     if (!emlat)
407         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
408     /* Special case: initial '.' is RHS match */
409     if (!baseat && (*baseptr == '.')) {
410         if (eml->length > base->length) {
411             emlptr += eml->length - base->length;
412             if (strcasecmp(baseptr, emlptr) == 0)
413                 return X509_V_OK;
414         }
415         return X509_V_ERR_PERMITTED_VIOLATION;
416     }
417
418     /* If we have anything before '@' match local part */
419
420     if (baseat) {
421         if (baseat != baseptr) {
422             if ((baseat - baseptr) != (emlat - emlptr))
423                 return X509_V_ERR_PERMITTED_VIOLATION;
424             /* Case sensitive match of local part */
425             if (strncmp(baseptr, emlptr, emlat - emlptr))
426                 return X509_V_ERR_PERMITTED_VIOLATION;
427         }
428         /* Position base after '@' */
429         baseptr = baseat + 1;
430     }
431     emlptr = emlat + 1;
432     /* Just have hostname left to match: case insensitive */
433     if (strcasecmp(baseptr, emlptr))
434         return X509_V_ERR_PERMITTED_VIOLATION;
435
436     return X509_V_OK;
437
438 }
439
440 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
441 {
442     const char *baseptr = (char *)base->data;
443     const char *hostptr = (char *)uri->data;
444     const char *p = strchr(hostptr, ':');
445     int hostlen;
446     /* Check for foo:// and skip past it */
447     if (!p || (p[1] != '/') || (p[2] != '/'))
448         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
449     hostptr = p + 3;
450
451     /* Determine length of hostname part of URI */
452
453     /* Look for a port indicator as end of hostname first */
454
455     p = strchr(hostptr, ':');
456     /* Otherwise look for trailing slash */
457     if (!p)
458         p = strchr(hostptr, '/');
459
460     if (!p)
461         hostlen = strlen(hostptr);
462     else
463         hostlen = p - hostptr;
464
465     if (hostlen == 0)
466         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
467
468     /* Special case: initial '.' is RHS match */
469     if (*baseptr == '.') {
470         if (hostlen > base->length) {
471             p = hostptr + hostlen - base->length;
472             if (strncasecmp(p, baseptr, base->length) == 0)
473                 return X509_V_OK;
474         }
475         return X509_V_ERR_PERMITTED_VIOLATION;
476     }
477
478     if ((base->length != (int)hostlen)
479         || strncasecmp(hostptr, baseptr, hostlen))
480         return X509_V_ERR_PERMITTED_VIOLATION;
481
482     return X509_V_OK;
483
484 }
485
486 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
487 {
488     int hostlen, baselen, i;
489     unsigned char *hostptr, *baseptr, *maskptr;
490     hostptr = ip->data;
491     hostlen = ip->length;
492     baseptr = base->data;
493     baselen = base->length;
494
495     /* Invalid if not IPv4 or IPv6 */
496     if (!((hostlen == 4) || (hostlen == 16)))
497         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
498     if (!((baselen == 8) || (baselen == 32)))
499         return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
500
501     /* Do not match IPv4 with IPv6 */
502     if (hostlen * 2 != baselen)
503         return X509_V_ERR_PERMITTED_VIOLATION;
504
505     maskptr = base->data + hostlen;
506
507     /* Considering possible not aligned base ipAddress */
508     /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
509     for (i = 0; i < hostlen; i++)
510         if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
511             return X509_V_ERR_PERMITTED_VIOLATION;
512
513     return X509_V_OK;
514
515 }