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