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
stbl_module_init(CONF_IMODULE * md,const CONF * cnf)20 static int stbl_module_init(CONF_IMODULE *md, const CONF *cnf)
21 {
22 int i;
23 const char *stbl_section;
24 STACK_OF(CONF_VALUE) *sktmp;
25 CONF_VALUE *mval;
26
27 stbl_section = CONF_imodule_get_value(md);
28 if ((sktmp = NCONF_get_section(cnf, stbl_section)) == NULL) {
29 ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_LOADING_SECTION);
30 return 0;
31 }
32 for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
33 mval = sk_CONF_VALUE_value(sktmp, i);
34 if (!do_tcreate(mval->value, mval->name)) {
35 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_VALUE);
36 return 0;
37 }
38 }
39 return 1;
40 }
41
stbl_module_finish(CONF_IMODULE * md)42 static void stbl_module_finish(CONF_IMODULE *md)
43 {
44 ASN1_STRING_TABLE_cleanup();
45 }
46
ASN1_add_stable_module(void)47 void ASN1_add_stable_module(void)
48 {
49 CONF_module_add("stbl_section", stbl_module_init, stbl_module_finish);
50 }
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
do_tcreate(const char * value,const char * name)57 static int do_tcreate(const char *value, const char *name)
58 {
59 char *eptr;
60 int nid, i, rv = 0;
61 long tbl_min = -1, tbl_max = -1;
62 unsigned long tbl_mask = 0, tbl_flags = 0;
63 STACK_OF(CONF_VALUE) *lst = NULL;
64 CONF_VALUE *cnf = NULL;
65 nid = OBJ_sn2nid(name);
66 if (nid == NID_undef)
67 nid = OBJ_ln2nid(name);
68 if (nid == NID_undef)
69 goto err;
70 lst = X509V3_parse_list(value);
71 if (!lst)
72 goto err;
73 for (i = 0; i < sk_CONF_VALUE_num(lst); i++) {
74 cnf = sk_CONF_VALUE_value(lst, i);
75 if (cnf->value == NULL)
76 goto err;
77 if (strcmp(cnf->name, "min") == 0) {
78 tbl_min = strtoul(cnf->value, &eptr, 0);
79 if (*eptr)
80 goto err;
81 } else if (strcmp(cnf->name, "max") == 0) {
82 tbl_max = strtoul(cnf->value, &eptr, 0);
83 if (*eptr)
84 goto err;
85 } else if (strcmp(cnf->name, "mask") == 0) {
86 if (!ASN1_str2mask(cnf->value, &tbl_mask) || !tbl_mask)
87 goto err;
88 } else if (strcmp(cnf->name, "flags") == 0) {
89 if (strcmp(cnf->value, "nomask") == 0)
90 tbl_flags = STABLE_NO_MASK;
91 else if (strcmp(cnf->value, "none") == 0)
92 tbl_flags = STABLE_FLAGS_CLEAR;
93 else
94 goto err;
95 } else
96 goto err;
97 }
98 rv = 1;
99 err:
100 if (rv == 0) {
101 if (cnf)
102 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_STRING_TABLE_VALUE,
103 "field=%s, value=%s", cnf->name,
104 cnf->value != NULL ? cnf->value
105 : value);
106 else
107 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_STRING_TABLE_VALUE,
108 "name=%s, value=%s", name, value);
109 } else {
110 rv = ASN1_STRING_TABLE_add(nid, tbl_min, tbl_max,
111 tbl_mask, tbl_flags);
112 if (!rv)
113 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
114 }
115 sk_CONF_VALUE_pop_free(lst, X509V3_conf_free);
116 return rv;
117 }
118