Check for overflows in ASN1_object_size().
[openssl.git] / crypto / asn1 / asn_mstbl.c
1 /*
2  * Copyright 2012-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 <stdio.h>
11 #include <ctype.h>
12 #include <openssl/crypto.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509v3.h>
16
17 /* Multi string module: add table entries from a given section */
18
19 static int do_tcreate(const char *value, const char *name);
20
21 static int stbl_module_init(CONF_IMODULE *md, const CONF *cnf)
22 {
23     int i;
24     const char *stbl_section;
25     STACK_OF(CONF_VALUE) *sktmp;
26     CONF_VALUE *mval;
27
28     stbl_section = CONF_imodule_get_value(md);
29     if ((sktmp = NCONF_get_section(cnf, stbl_section)) == NULL) {
30         ASN1err(ASN1_F_STBL_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
31         return 0;
32     }
33     for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
34         mval = sk_CONF_VALUE_value(sktmp, i);
35         if (!do_tcreate(mval->value, mval->name)) {
36             ASN1err(ASN1_F_STBL_MODULE_INIT, ASN1_R_INVALID_VALUE);
37             return 0;
38         }
39     }
40     return 1;
41 }
42
43 static void stbl_module_finish(CONF_IMODULE *md)
44 {
45     ASN1_STRING_TABLE_cleanup();
46 }
47
48 void ASN1_add_stable_module(void)
49 {
50     CONF_module_add("stbl_section", stbl_module_init, stbl_module_finish);
51 }
52
53 /*
54  * Create an table entry based on a name value pair. format is oid_name =
55  * n1:v1, n2:v2,... where name is "min", "max", "mask" or "flags".
56  */
57
58 static int do_tcreate(const char *value, const char *name)
59 {
60     char *eptr;
61     int nid, i, rv = 0;
62     long tbl_min = -1, tbl_max = -1;
63     unsigned long tbl_mask = 0, tbl_flags = 0;
64     STACK_OF(CONF_VALUE) *lst = NULL;
65     CONF_VALUE *cnf = NULL;
66     nid = OBJ_sn2nid(name);
67     if (nid == NID_undef)
68         nid = OBJ_ln2nid(name);
69     if (nid == NID_undef)
70         goto err;
71     lst = X509V3_parse_list(value);
72     if (!lst)
73         goto err;
74     for (i = 0; i < sk_CONF_VALUE_num(lst); i++) {
75         cnf = sk_CONF_VALUE_value(lst, i);
76         if (strcmp(cnf->name, "min") == 0) {
77             tbl_min = strtoul(cnf->value, &eptr, 0);
78             if (*eptr)
79                 goto err;
80         } else if (strcmp(cnf->name, "max") == 0) {
81             tbl_max = strtoul(cnf->value, &eptr, 0);
82             if (*eptr)
83                 goto err;
84         } else if (strcmp(cnf->name, "mask") == 0) {
85             if (!ASN1_str2mask(cnf->value, &tbl_mask) || !tbl_mask)
86                 goto err;
87         } else if (strcmp(cnf->name, "flags") == 0) {
88             if (strcmp(cnf->value, "nomask") == 0)
89                 tbl_flags = STABLE_NO_MASK;
90             else if (strcmp(cnf->value, "none") == 0)
91                 tbl_flags = STABLE_FLAGS_CLEAR;
92             else
93                 goto err;
94         } else
95             goto err;
96     }
97     rv = 1;
98  err:
99     if (rv == 0) {
100         ASN1err(ASN1_F_DO_TCREATE, ASN1_R_INVALID_STRING_TABLE_VALUE);
101         if (cnf)
102             ERR_add_error_data(4, "field=", cnf->name,
103                                ", value=", cnf->value);
104         else
105             ERR_add_error_data(4, "name=", name, ", value=", value);
106     } else {
107         rv = ASN1_STRING_TABLE_add(nid, tbl_min, tbl_max,
108                                    tbl_mask, tbl_flags);
109         if (!rv)
110             ASN1err(ASN1_F_DO_TCREATE, ERR_R_MALLOC_FAILURE);
111     }
112     sk_CONF_VALUE_pop_free(lst, X509V3_conf_free);
113     return rv;
114 }