xref: /minix3/crypto/external/bsd/openssl/dist/crypto/ts/ts_conf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/ts/ts_conf.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc  * 2002.
5ebfedea0SLionel Sambuc  */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc  *    distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
23ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc  *
26ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc  *
35ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc  *    acknowledgment:
37ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc  * ====================================================================
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
56ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc  *
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc #include <string.h>
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc #include <openssl/crypto.h>
63ebfedea0SLionel Sambuc #include "cryptlib.h"
64ebfedea0SLionel Sambuc #include <openssl/pem.h>
65ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
66ebfedea0SLionel Sambuc # include <openssl/engine.h>
67ebfedea0SLionel Sambuc #endif
68ebfedea0SLionel Sambuc #include <openssl/ts.h>
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc /* Macro definitions for the configuration file. */
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc #define BASE_SECTION                    "tsa"
73ebfedea0SLionel Sambuc #define ENV_DEFAULT_TSA                 "default_tsa"
74ebfedea0SLionel Sambuc #define ENV_SERIAL                      "serial"
75ebfedea0SLionel Sambuc #define ENV_CRYPTO_DEVICE               "crypto_device"
76ebfedea0SLionel Sambuc #define ENV_SIGNER_CERT                 "signer_cert"
77ebfedea0SLionel Sambuc #define ENV_CERTS                       "certs"
78ebfedea0SLionel Sambuc #define ENV_SIGNER_KEY                  "signer_key"
79ebfedea0SLionel Sambuc #define ENV_DEFAULT_POLICY              "default_policy"
80ebfedea0SLionel Sambuc #define ENV_OTHER_POLICIES              "other_policies"
81ebfedea0SLionel Sambuc #define ENV_DIGESTS                     "digests"
82ebfedea0SLionel Sambuc #define ENV_ACCURACY                    "accuracy"
83ebfedea0SLionel Sambuc #define ENV_ORDERING                    "ordering"
84ebfedea0SLionel Sambuc #define ENV_TSA_NAME                    "tsa_name"
85ebfedea0SLionel Sambuc #define ENV_ESS_CERT_ID_CHAIN           "ess_cert_id_chain"
86ebfedea0SLionel Sambuc #define ENV_VALUE_SECS                  "secs"
87ebfedea0SLionel Sambuc #define ENV_VALUE_MILLISECS             "millisecs"
88ebfedea0SLionel Sambuc #define ENV_VALUE_MICROSECS             "microsecs"
89ebfedea0SLionel Sambuc #define ENV_CLOCK_PRECISION_DIGITS      "clock_precision_digits"
90ebfedea0SLionel Sambuc #define ENV_VALUE_YES                   "yes"
91ebfedea0SLionel Sambuc #define ENV_VALUE_NO                    "no"
92ebfedea0SLionel Sambuc 
93ebfedea0SLionel Sambuc /* Function definitions for certificate and key loading. */
94ebfedea0SLionel Sambuc 
TS_CONF_load_cert(const char * file)95ebfedea0SLionel Sambuc X509 *TS_CONF_load_cert(const char *file)
96ebfedea0SLionel Sambuc {
97ebfedea0SLionel Sambuc     BIO *cert = NULL;
98ebfedea0SLionel Sambuc     X509 *x = NULL;
99ebfedea0SLionel Sambuc 
100*0a6a1f1dSLionel Sambuc     if ((cert = BIO_new_file(file, "r")) == NULL)
101*0a6a1f1dSLionel Sambuc         goto end;
102ebfedea0SLionel Sambuc     x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
103ebfedea0SLionel Sambuc  end:
104ebfedea0SLionel Sambuc     if (x == NULL)
105ebfedea0SLionel Sambuc         fprintf(stderr, "unable to load certificate: %s\n", file);
106ebfedea0SLionel Sambuc     BIO_free(cert);
107ebfedea0SLionel Sambuc     return x;
108ebfedea0SLionel Sambuc }
109ebfedea0SLionel Sambuc 
STACK_OF(X509)110ebfedea0SLionel Sambuc STACK_OF(X509) *TS_CONF_load_certs(const char *file)
111ebfedea0SLionel Sambuc {
112ebfedea0SLionel Sambuc     BIO *certs = NULL;
113ebfedea0SLionel Sambuc     STACK_OF(X509) *othercerts = NULL;
114ebfedea0SLionel Sambuc     STACK_OF(X509_INFO) *allcerts = NULL;
115ebfedea0SLionel Sambuc     int i;
116ebfedea0SLionel Sambuc 
117*0a6a1f1dSLionel Sambuc     if (!(certs = BIO_new_file(file, "r")))
118*0a6a1f1dSLionel Sambuc         goto end;
119ebfedea0SLionel Sambuc 
120*0a6a1f1dSLionel Sambuc     if (!(othercerts = sk_X509_new_null()))
121*0a6a1f1dSLionel Sambuc         goto end;
122ebfedea0SLionel Sambuc     allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
123*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
124ebfedea0SLionel Sambuc         X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
125*0a6a1f1dSLionel Sambuc         if (xi->x509) {
126ebfedea0SLionel Sambuc             sk_X509_push(othercerts, xi->x509);
127ebfedea0SLionel Sambuc             xi->x509 = NULL;
128ebfedea0SLionel Sambuc         }
129ebfedea0SLionel Sambuc     }
130ebfedea0SLionel Sambuc  end:
131ebfedea0SLionel Sambuc     if (othercerts == NULL)
132ebfedea0SLionel Sambuc         fprintf(stderr, "unable to load certificates: %s\n", file);
133ebfedea0SLionel Sambuc     sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
134ebfedea0SLionel Sambuc     BIO_free(certs);
135ebfedea0SLionel Sambuc     return othercerts;
136ebfedea0SLionel Sambuc }
137ebfedea0SLionel Sambuc 
TS_CONF_load_key(const char * file,const char * pass)138ebfedea0SLionel Sambuc EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
139ebfedea0SLionel Sambuc {
140ebfedea0SLionel Sambuc     BIO *key = NULL;
141ebfedea0SLionel Sambuc     EVP_PKEY *pkey = NULL;
142ebfedea0SLionel Sambuc 
143*0a6a1f1dSLionel Sambuc     if (!(key = BIO_new_file(file, "r")))
144*0a6a1f1dSLionel Sambuc         goto end;
145ebfedea0SLionel Sambuc     pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
146ebfedea0SLionel Sambuc  end:
147ebfedea0SLionel Sambuc     if (pkey == NULL)
148ebfedea0SLionel Sambuc         fprintf(stderr, "unable to load private key: %s\n", file);
149ebfedea0SLionel Sambuc     BIO_free(key);
150ebfedea0SLionel Sambuc     return pkey;
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc /* Function definitions for handling configuration options. */
154ebfedea0SLionel Sambuc 
TS_CONF_lookup_fail(const char * name,const char * tag)155ebfedea0SLionel Sambuc static void TS_CONF_lookup_fail(const char *name, const char *tag)
156ebfedea0SLionel Sambuc {
157ebfedea0SLionel Sambuc     fprintf(stderr, "variable lookup failed for %s::%s\n", name, tag);
158ebfedea0SLionel Sambuc }
159ebfedea0SLionel Sambuc 
TS_CONF_invalid(const char * name,const char * tag)160ebfedea0SLionel Sambuc static void TS_CONF_invalid(const char *name, const char *tag)
161ebfedea0SLionel Sambuc {
162ebfedea0SLionel Sambuc     fprintf(stderr, "invalid variable value for %s::%s\n", name, tag);
163ebfedea0SLionel Sambuc }
164ebfedea0SLionel Sambuc 
TS_CONF_get_tsa_section(CONF * conf,const char * section)165ebfedea0SLionel Sambuc const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
166ebfedea0SLionel Sambuc {
167*0a6a1f1dSLionel Sambuc     if (!section) {
168ebfedea0SLionel Sambuc         section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
169ebfedea0SLionel Sambuc         if (!section)
170ebfedea0SLionel Sambuc             TS_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
171ebfedea0SLionel Sambuc     }
172ebfedea0SLionel Sambuc     return section;
173ebfedea0SLionel Sambuc }
174ebfedea0SLionel Sambuc 
TS_CONF_set_serial(CONF * conf,const char * section,TS_serial_cb cb,TS_RESP_CTX * ctx)175ebfedea0SLionel Sambuc int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
176ebfedea0SLionel Sambuc                        TS_RESP_CTX *ctx)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc     int ret = 0;
179ebfedea0SLionel Sambuc     char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
180*0a6a1f1dSLionel Sambuc     if (!serial) {
181ebfedea0SLionel Sambuc         TS_CONF_lookup_fail(section, ENV_SERIAL);
182ebfedea0SLionel Sambuc         goto err;
183ebfedea0SLionel Sambuc     }
184ebfedea0SLionel Sambuc     TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
185ebfedea0SLionel Sambuc 
186ebfedea0SLionel Sambuc     ret = 1;
187ebfedea0SLionel Sambuc  err:
188ebfedea0SLionel Sambuc     return ret;
189ebfedea0SLionel Sambuc }
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
192ebfedea0SLionel Sambuc 
TS_CONF_set_crypto_device(CONF * conf,const char * section,const char * device)193ebfedea0SLionel Sambuc int TS_CONF_set_crypto_device(CONF *conf, const char *section,
194ebfedea0SLionel Sambuc                               const char *device)
195ebfedea0SLionel Sambuc {
196ebfedea0SLionel Sambuc     int ret = 0;
197ebfedea0SLionel Sambuc 
198ebfedea0SLionel Sambuc     if (!device)
199*0a6a1f1dSLionel Sambuc         device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
200ebfedea0SLionel Sambuc 
201*0a6a1f1dSLionel Sambuc     if (device && !TS_CONF_set_default_engine(device)) {
202ebfedea0SLionel Sambuc         TS_CONF_invalid(section, ENV_CRYPTO_DEVICE);
203ebfedea0SLionel Sambuc         goto err;
204ebfedea0SLionel Sambuc     }
205ebfedea0SLionel Sambuc     ret = 1;
206ebfedea0SLionel Sambuc  err:
207ebfedea0SLionel Sambuc     return ret;
208ebfedea0SLionel Sambuc }
209ebfedea0SLionel Sambuc 
TS_CONF_set_default_engine(const char * name)210ebfedea0SLionel Sambuc int TS_CONF_set_default_engine(const char *name)
211ebfedea0SLionel Sambuc {
212ebfedea0SLionel Sambuc     ENGINE *e = NULL;
213ebfedea0SLionel Sambuc     int ret = 0;
214ebfedea0SLionel Sambuc 
215ebfedea0SLionel Sambuc     /* Leave the default if builtin specified. */
216*0a6a1f1dSLionel Sambuc     if (strcmp(name, "builtin") == 0)
217*0a6a1f1dSLionel Sambuc         return 1;
218ebfedea0SLionel Sambuc 
219*0a6a1f1dSLionel Sambuc     if (!(e = ENGINE_by_id(name)))
220*0a6a1f1dSLionel Sambuc         goto err;
221ebfedea0SLionel Sambuc     /* Enable the use of the NCipher HSM for forked children. */
222ebfedea0SLionel Sambuc     if (strcmp(name, "chil") == 0)
223ebfedea0SLionel Sambuc         ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
224ebfedea0SLionel Sambuc     /* All the operations are going to be carried out by the engine. */
225*0a6a1f1dSLionel Sambuc     if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
226*0a6a1f1dSLionel Sambuc         goto err;
227ebfedea0SLionel Sambuc     ret = 1;
228ebfedea0SLionel Sambuc  err:
229*0a6a1f1dSLionel Sambuc     if (!ret) {
230*0a6a1f1dSLionel Sambuc         TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
231ebfedea0SLionel Sambuc         ERR_add_error_data(2, "engine:", name);
232ebfedea0SLionel Sambuc     }
233*0a6a1f1dSLionel Sambuc     if (e)
234*0a6a1f1dSLionel Sambuc         ENGINE_free(e);
235ebfedea0SLionel Sambuc     return ret;
236ebfedea0SLionel Sambuc }
237ebfedea0SLionel Sambuc 
238ebfedea0SLionel Sambuc #endif
239ebfedea0SLionel Sambuc 
TS_CONF_set_signer_cert(CONF * conf,const char * section,const char * cert,TS_RESP_CTX * ctx)240ebfedea0SLionel Sambuc int TS_CONF_set_signer_cert(CONF *conf, const char *section,
241ebfedea0SLionel Sambuc                             const char *cert, TS_RESP_CTX *ctx)
242ebfedea0SLionel Sambuc {
243ebfedea0SLionel Sambuc     int ret = 0;
244ebfedea0SLionel Sambuc     X509 *cert_obj = NULL;
245ebfedea0SLionel Sambuc     if (!cert)
246ebfedea0SLionel Sambuc         cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
247*0a6a1f1dSLionel Sambuc     if (!cert) {
248ebfedea0SLionel Sambuc         TS_CONF_lookup_fail(section, ENV_SIGNER_CERT);
249ebfedea0SLionel Sambuc         goto err;
250ebfedea0SLionel Sambuc     }
251ebfedea0SLionel Sambuc     if (!(cert_obj = TS_CONF_load_cert(cert)))
252ebfedea0SLionel Sambuc         goto err;
253ebfedea0SLionel Sambuc     if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
254ebfedea0SLionel Sambuc         goto err;
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc     ret = 1;
257ebfedea0SLionel Sambuc  err:
258ebfedea0SLionel Sambuc     X509_free(cert_obj);
259ebfedea0SLionel Sambuc     return ret;
260ebfedea0SLionel Sambuc }
261ebfedea0SLionel Sambuc 
TS_CONF_set_certs(CONF * conf,const char * section,const char * certs,TS_RESP_CTX * ctx)262ebfedea0SLionel Sambuc int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
263ebfedea0SLionel Sambuc                       TS_RESP_CTX *ctx)
264ebfedea0SLionel Sambuc {
265ebfedea0SLionel Sambuc     int ret = 0;
266ebfedea0SLionel Sambuc     STACK_OF(X509) *certs_obj = NULL;
267ebfedea0SLionel Sambuc     if (!certs)
268ebfedea0SLionel Sambuc         certs = NCONF_get_string(conf, section, ENV_CERTS);
269ebfedea0SLionel Sambuc     /* Certificate chain is optional. */
270*0a6a1f1dSLionel Sambuc     if (!certs)
271*0a6a1f1dSLionel Sambuc         goto end;
272*0a6a1f1dSLionel Sambuc     if (!(certs_obj = TS_CONF_load_certs(certs)))
273*0a6a1f1dSLionel Sambuc         goto err;
274*0a6a1f1dSLionel Sambuc     if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
275*0a6a1f1dSLionel Sambuc         goto err;
276ebfedea0SLionel Sambuc  end:
277ebfedea0SLionel Sambuc     ret = 1;
278ebfedea0SLionel Sambuc  err:
279ebfedea0SLionel Sambuc     sk_X509_pop_free(certs_obj, X509_free);
280ebfedea0SLionel Sambuc     return ret;
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc 
TS_CONF_set_signer_key(CONF * conf,const char * section,const char * key,const char * pass,TS_RESP_CTX * ctx)283ebfedea0SLionel Sambuc int TS_CONF_set_signer_key(CONF *conf, const char *section,
284ebfedea0SLionel Sambuc                            const char *key, const char *pass,
285ebfedea0SLionel Sambuc                            TS_RESP_CTX *ctx)
286ebfedea0SLionel Sambuc {
287ebfedea0SLionel Sambuc     int ret = 0;
288ebfedea0SLionel Sambuc     EVP_PKEY *key_obj = NULL;
289ebfedea0SLionel Sambuc     if (!key)
290ebfedea0SLionel Sambuc         key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
291*0a6a1f1dSLionel Sambuc     if (!key) {
292ebfedea0SLionel Sambuc         TS_CONF_lookup_fail(section, ENV_SIGNER_KEY);
293ebfedea0SLionel Sambuc         goto err;
294ebfedea0SLionel Sambuc     }
295*0a6a1f1dSLionel Sambuc     if (!(key_obj = TS_CONF_load_key(key, pass)))
296*0a6a1f1dSLionel Sambuc         goto err;
297*0a6a1f1dSLionel Sambuc     if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
298*0a6a1f1dSLionel Sambuc         goto err;
299ebfedea0SLionel Sambuc 
300ebfedea0SLionel Sambuc     ret = 1;
301ebfedea0SLionel Sambuc  err:
302ebfedea0SLionel Sambuc     EVP_PKEY_free(key_obj);
303ebfedea0SLionel Sambuc     return ret;
304ebfedea0SLionel Sambuc }
305ebfedea0SLionel Sambuc 
TS_CONF_set_def_policy(CONF * conf,const char * section,const char * policy,TS_RESP_CTX * ctx)306ebfedea0SLionel Sambuc int TS_CONF_set_def_policy(CONF *conf, const char *section,
307ebfedea0SLionel Sambuc                            const char *policy, TS_RESP_CTX *ctx)
308ebfedea0SLionel Sambuc {
309ebfedea0SLionel Sambuc     int ret = 0;
310ebfedea0SLionel Sambuc     ASN1_OBJECT *policy_obj = NULL;
311ebfedea0SLionel Sambuc     if (!policy)
312*0a6a1f1dSLionel Sambuc         policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
313*0a6a1f1dSLionel Sambuc     if (!policy) {
314ebfedea0SLionel Sambuc         TS_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
315ebfedea0SLionel Sambuc         goto err;
316ebfedea0SLionel Sambuc     }
317*0a6a1f1dSLionel Sambuc     if (!(policy_obj = OBJ_txt2obj(policy, 0))) {
318ebfedea0SLionel Sambuc         TS_CONF_invalid(section, ENV_DEFAULT_POLICY);
319ebfedea0SLionel Sambuc         goto err;
320ebfedea0SLionel Sambuc     }
321ebfedea0SLionel Sambuc     if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
322ebfedea0SLionel Sambuc         goto err;
323ebfedea0SLionel Sambuc 
324ebfedea0SLionel Sambuc     ret = 1;
325ebfedea0SLionel Sambuc  err:
326ebfedea0SLionel Sambuc     ASN1_OBJECT_free(policy_obj);
327ebfedea0SLionel Sambuc     return ret;
328ebfedea0SLionel Sambuc }
329ebfedea0SLionel Sambuc 
TS_CONF_set_policies(CONF * conf,const char * section,TS_RESP_CTX * ctx)330*0a6a1f1dSLionel Sambuc int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
331ebfedea0SLionel Sambuc {
332ebfedea0SLionel Sambuc     int ret = 0;
333ebfedea0SLionel Sambuc     int i;
334ebfedea0SLionel Sambuc     STACK_OF(CONF_VALUE) *list = NULL;
335ebfedea0SLionel Sambuc     char *policies = NCONF_get_string(conf, section,
336ebfedea0SLionel Sambuc                                       ENV_OTHER_POLICIES);
337ebfedea0SLionel Sambuc     /* If no other policy is specified, that's fine. */
338*0a6a1f1dSLionel Sambuc     if (policies && !(list = X509V3_parse_list(policies))) {
339ebfedea0SLionel Sambuc         TS_CONF_invalid(section, ENV_OTHER_POLICIES);
340ebfedea0SLionel Sambuc         goto err;
341ebfedea0SLionel Sambuc     }
342*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
343ebfedea0SLionel Sambuc         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
344ebfedea0SLionel Sambuc         const char *extval = val->value ? val->value : val->name;
345ebfedea0SLionel Sambuc         ASN1_OBJECT *objtmp;
346*0a6a1f1dSLionel Sambuc         if (!(objtmp = OBJ_txt2obj(extval, 0))) {
347ebfedea0SLionel Sambuc             TS_CONF_invalid(section, ENV_OTHER_POLICIES);
348ebfedea0SLionel Sambuc             goto err;
349ebfedea0SLionel Sambuc         }
350ebfedea0SLionel Sambuc         if (!TS_RESP_CTX_add_policy(ctx, objtmp))
351ebfedea0SLionel Sambuc             goto err;
352ebfedea0SLionel Sambuc         ASN1_OBJECT_free(objtmp);
353ebfedea0SLionel Sambuc     }
354ebfedea0SLionel Sambuc 
355ebfedea0SLionel Sambuc     ret = 1;
356ebfedea0SLionel Sambuc  err:
357ebfedea0SLionel Sambuc     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
358ebfedea0SLionel Sambuc     return ret;
359ebfedea0SLionel Sambuc }
360ebfedea0SLionel Sambuc 
TS_CONF_set_digests(CONF * conf,const char * section,TS_RESP_CTX * ctx)361*0a6a1f1dSLionel Sambuc int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
362ebfedea0SLionel Sambuc {
363ebfedea0SLionel Sambuc     int ret = 0;
364ebfedea0SLionel Sambuc     int i;
365ebfedea0SLionel Sambuc     STACK_OF(CONF_VALUE) *list = NULL;
366ebfedea0SLionel Sambuc     char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
367*0a6a1f1dSLionel Sambuc     if (!digests) {
368ebfedea0SLionel Sambuc         TS_CONF_lookup_fail(section, ENV_DIGESTS);
369ebfedea0SLionel Sambuc         goto err;
370ebfedea0SLionel Sambuc     }
371*0a6a1f1dSLionel Sambuc     if (!(list = X509V3_parse_list(digests))) {
372ebfedea0SLionel Sambuc         TS_CONF_invalid(section, ENV_DIGESTS);
373ebfedea0SLionel Sambuc         goto err;
374ebfedea0SLionel Sambuc     }
375*0a6a1f1dSLionel Sambuc     if (sk_CONF_VALUE_num(list) == 0) {
376ebfedea0SLionel Sambuc         TS_CONF_invalid(section, ENV_DIGESTS);
377ebfedea0SLionel Sambuc         goto err;
378ebfedea0SLionel Sambuc     }
379*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
380ebfedea0SLionel Sambuc         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
381ebfedea0SLionel Sambuc         const char *extval = val->value ? val->value : val->name;
382ebfedea0SLionel Sambuc         const EVP_MD *md;
383*0a6a1f1dSLionel Sambuc         if (!(md = EVP_get_digestbyname(extval))) {
384ebfedea0SLionel Sambuc             TS_CONF_invalid(section, ENV_DIGESTS);
385ebfedea0SLionel Sambuc             goto err;
386ebfedea0SLionel Sambuc         }
387ebfedea0SLionel Sambuc         if (!TS_RESP_CTX_add_md(ctx, md))
388ebfedea0SLionel Sambuc             goto err;
389ebfedea0SLionel Sambuc     }
390ebfedea0SLionel Sambuc 
391ebfedea0SLionel Sambuc     ret = 1;
392ebfedea0SLionel Sambuc  err:
393ebfedea0SLionel Sambuc     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
394ebfedea0SLionel Sambuc     return ret;
395ebfedea0SLionel Sambuc }
396ebfedea0SLionel Sambuc 
TS_CONF_set_accuracy(CONF * conf,const char * section,TS_RESP_CTX * ctx)397ebfedea0SLionel Sambuc int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
398ebfedea0SLionel Sambuc {
399ebfedea0SLionel Sambuc     int ret = 0;
400ebfedea0SLionel Sambuc     int i;
401ebfedea0SLionel Sambuc     int secs = 0, millis = 0, micros = 0;
402ebfedea0SLionel Sambuc     STACK_OF(CONF_VALUE) *list = NULL;
403ebfedea0SLionel Sambuc     char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
404ebfedea0SLionel Sambuc 
405*0a6a1f1dSLionel Sambuc     if (accuracy && !(list = X509V3_parse_list(accuracy))) {
406ebfedea0SLionel Sambuc         TS_CONF_invalid(section, ENV_ACCURACY);
407ebfedea0SLionel Sambuc         goto err;
408ebfedea0SLionel Sambuc     }
409*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
410ebfedea0SLionel Sambuc         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
411*0a6a1f1dSLionel Sambuc         if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
412*0a6a1f1dSLionel Sambuc             if (val->value)
413*0a6a1f1dSLionel Sambuc                 secs = atoi(val->value);
414*0a6a1f1dSLionel Sambuc         } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
415*0a6a1f1dSLionel Sambuc             if (val->value)
416*0a6a1f1dSLionel Sambuc                 millis = atoi(val->value);
417*0a6a1f1dSLionel Sambuc         } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
418*0a6a1f1dSLionel Sambuc             if (val->value)
419*0a6a1f1dSLionel Sambuc                 micros = atoi(val->value);
420*0a6a1f1dSLionel Sambuc         } else {
421ebfedea0SLionel Sambuc             TS_CONF_invalid(section, ENV_ACCURACY);
422ebfedea0SLionel Sambuc             goto err;
423ebfedea0SLionel Sambuc         }
424ebfedea0SLionel Sambuc     }
425ebfedea0SLionel Sambuc     if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
426ebfedea0SLionel Sambuc         goto err;
427ebfedea0SLionel Sambuc 
428ebfedea0SLionel Sambuc     ret = 1;
429ebfedea0SLionel Sambuc  err:
430ebfedea0SLionel Sambuc     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
431ebfedea0SLionel Sambuc     return ret;
432ebfedea0SLionel Sambuc }
433ebfedea0SLionel Sambuc 
TS_CONF_set_clock_precision_digits(CONF * conf,const char * section,TS_RESP_CTX * ctx)434ebfedea0SLionel Sambuc int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
435ebfedea0SLionel Sambuc                                        TS_RESP_CTX *ctx)
436ebfedea0SLionel Sambuc {
437ebfedea0SLionel Sambuc     int ret = 0;
438ebfedea0SLionel Sambuc     long digits = 0;
439ebfedea0SLionel Sambuc 
440*0a6a1f1dSLionel Sambuc     /*
441*0a6a1f1dSLionel Sambuc      * If not specified, set the default value to 0, i.e. sec precision
442*0a6a1f1dSLionel Sambuc      */
443ebfedea0SLionel Sambuc     if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
444ebfedea0SLionel Sambuc                             &digits))
445ebfedea0SLionel Sambuc         digits = 0;
446*0a6a1f1dSLionel Sambuc     if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
447ebfedea0SLionel Sambuc         TS_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
448ebfedea0SLionel Sambuc         goto err;
449ebfedea0SLionel Sambuc     }
450ebfedea0SLionel Sambuc 
451ebfedea0SLionel Sambuc     if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
452ebfedea0SLionel Sambuc         goto err;
453ebfedea0SLionel Sambuc 
454ebfedea0SLionel Sambuc     return 1;
455ebfedea0SLionel Sambuc  err:
456ebfedea0SLionel Sambuc     return ret;
457ebfedea0SLionel Sambuc }
458ebfedea0SLionel Sambuc 
TS_CONF_add_flag(CONF * conf,const char * section,const char * field,int flag,TS_RESP_CTX * ctx)459*0a6a1f1dSLionel Sambuc static int TS_CONF_add_flag(CONF *conf, const char *section,
460*0a6a1f1dSLionel Sambuc                             const char *field, int flag, TS_RESP_CTX *ctx)
461ebfedea0SLionel Sambuc {
462ebfedea0SLionel Sambuc     /* Default is false. */
463ebfedea0SLionel Sambuc     const char *value = NCONF_get_string(conf, section, field);
464*0a6a1f1dSLionel Sambuc     if (value) {
465ebfedea0SLionel Sambuc         if (strcmp(value, ENV_VALUE_YES) == 0)
466ebfedea0SLionel Sambuc             TS_RESP_CTX_add_flags(ctx, flag);
467*0a6a1f1dSLionel Sambuc         else if (strcmp(value, ENV_VALUE_NO) != 0) {
468ebfedea0SLionel Sambuc             TS_CONF_invalid(section, field);
469ebfedea0SLionel Sambuc             return 0;
470ebfedea0SLionel Sambuc         }
471ebfedea0SLionel Sambuc     }
472ebfedea0SLionel Sambuc 
473ebfedea0SLionel Sambuc     return 1;
474ebfedea0SLionel Sambuc }
475ebfedea0SLionel Sambuc 
TS_CONF_set_ordering(CONF * conf,const char * section,TS_RESP_CTX * ctx)476ebfedea0SLionel Sambuc int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
477ebfedea0SLionel Sambuc {
478ebfedea0SLionel Sambuc     return TS_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
479ebfedea0SLionel Sambuc }
480ebfedea0SLionel Sambuc 
TS_CONF_set_tsa_name(CONF * conf,const char * section,TS_RESP_CTX * ctx)481ebfedea0SLionel Sambuc int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
482ebfedea0SLionel Sambuc {
483ebfedea0SLionel Sambuc     return TS_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
484ebfedea0SLionel Sambuc }
485ebfedea0SLionel Sambuc 
TS_CONF_set_ess_cert_id_chain(CONF * conf,const char * section,TS_RESP_CTX * ctx)486ebfedea0SLionel Sambuc int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
487ebfedea0SLionel Sambuc                                   TS_RESP_CTX *ctx)
488ebfedea0SLionel Sambuc {
489ebfedea0SLionel Sambuc     return TS_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
490ebfedea0SLionel Sambuc                             TS_ESS_CERT_ID_CHAIN, ctx);
491ebfedea0SLionel Sambuc }
492