1 | /* |
2 | * Copyright 2012-2017 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 | { |
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 | ASN1err(ASN1_F_STBL_MODULE_INIT, 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 | ASN1err(ASN1_F_STBL_MODULE_INIT, ASN1_R_INVALID_VALUE); |
36 | return 0; |
37 | } |
38 | } |
39 | return 1; |
40 | } |
41 | |
42 | static void stbl_module_finish(CONF_IMODULE *md) |
43 | { |
44 | ASN1_STRING_TABLE_cleanup(); |
45 | } |
46 | |
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 | |
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 (strcmp(cnf->name, "min" ) == 0) { |
76 | tbl_min = strtoul(cnf->value, &eptr, 0); |
77 | if (*eptr) |
78 | goto err; |
79 | } else if (strcmp(cnf->name, "max" ) == 0) { |
80 | tbl_max = strtoul(cnf->value, &eptr, 0); |
81 | if (*eptr) |
82 | goto err; |
83 | } else if (strcmp(cnf->name, "mask" ) == 0) { |
84 | if (!ASN1_str2mask(cnf->value, &tbl_mask) || !tbl_mask) |
85 | goto err; |
86 | } else if (strcmp(cnf->name, "flags" ) == 0) { |
87 | if (strcmp(cnf->value, "nomask" ) == 0) |
88 | tbl_flags = STABLE_NO_MASK; |
89 | else if (strcmp(cnf->value, "none" ) == 0) |
90 | tbl_flags = STABLE_FLAGS_CLEAR; |
91 | else |
92 | goto err; |
93 | } else |
94 | goto err; |
95 | } |
96 | rv = 1; |
97 | err: |
98 | if (rv == 0) { |
99 | ASN1err(ASN1_F_DO_TCREATE, ASN1_R_INVALID_STRING_TABLE_VALUE); |
100 | if (cnf) |
101 | ERR_add_error_data(4, "field=" , cnf->name, |
102 | ", value=" , cnf->value); |
103 | else |
104 | ERR_add_error_data(4, "name=" , name, ", value=" , value); |
105 | } else { |
106 | rv = ASN1_STRING_TABLE_add(nid, tbl_min, tbl_max, |
107 | tbl_mask, tbl_flags); |
108 | if (!rv) |
109 | ASN1err(ASN1_F_DO_TCREATE, ERR_R_MALLOC_FAILURE); |
110 | } |
111 | sk_CONF_VALUE_pop_free(lst, X509V3_conf_free); |
112 | return rv; |
113 | } |
114 | |