1 /* crypto/ts/ts_conf.c */
3 * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/crypto.h>
63 #include "internal/cryptlib.h"
64 #include <openssl/pem.h>
65 #ifndef OPENSSL_NO_ENGINE
66 # include <openssl/engine.h>
68 #include <openssl/ts.h>
70 /* Macro definitions for the configuration file. */
71 #define BASE_SECTION "tsa"
72 #define ENV_DEFAULT_TSA "default_tsa"
73 #define ENV_SERIAL "serial"
74 #define ENV_CRYPTO_DEVICE "crypto_device"
75 #define ENV_SIGNER_CERT "signer_cert"
76 #define ENV_CERTS "certs"
77 #define ENV_SIGNER_KEY "signer_key"
78 #define ENV_SIGNER_DIGEST "signer_digest"
79 #define ENV_DEFAULT_POLICY "default_policy"
80 #define ENV_OTHER_POLICIES "other_policies"
81 #define ENV_DIGESTS "digests"
82 #define ENV_ACCURACY "accuracy"
83 #define ENV_ORDERING "ordering"
84 #define ENV_TSA_NAME "tsa_name"
85 #define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
86 #define ENV_VALUE_SECS "secs"
87 #define ENV_VALUE_MILLISECS "millisecs"
88 #define ENV_VALUE_MICROSECS "microsecs"
89 #define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
90 #define ENV_VALUE_YES "yes"
91 #define ENV_VALUE_NO "no"
93 /* Function definitions for certificate and key loading. */
95 X509 *TS_CONF_load_cert(const char *file)
100 if ((cert = BIO_new_file(file, "r")) == NULL)
102 x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
105 TSerr(TS_F_TS_CONF_LOAD_CERT, TS_R_CANNOT_LOAD_CERT);
110 STACK_OF(X509) *TS_CONF_load_certs(const char *file)
113 STACK_OF(X509) *othercerts = NULL;
114 STACK_OF(X509_INFO) *allcerts = NULL;
117 if ((certs = BIO_new_file(file, "r")) == NULL)
119 if ((othercerts = sk_X509_new_null()) == NULL)
122 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
123 for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
124 X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
126 sk_X509_push(othercerts, xi->x509);
131 if (othercerts == NULL)
132 TSerr(TS_F_TS_CONF_LOAD_CERTS, TS_R_CANNOT_LOAD_CERT);
133 sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
138 EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
141 EVP_PKEY *pkey = NULL;
143 if ((key = BIO_new_file(file, "r")) == NULL)
145 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
148 TSerr(TS_F_TS_CONF_LOAD_KEY, TS_R_CANNOT_LOAD_KEY);
153 /* Function definitions for handling configuration options. */
155 static void ts_CONF_lookup_fail(const char *name, const char *tag)
157 TSerr(TS_F_TS_CONF_LOOKUP_FAIL, TS_R_VAR_LOOKUP_FAILURE);
158 ERR_add_error_data(3, name, "::", tag);
161 static void ts_CONF_invalid(const char *name, const char *tag)
163 TSerr(TS_F_TS_CONF_INVALID, TS_R_VAR_BAD_VALUE);
164 ERR_add_error_data(3, name, "::", tag);
167 const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
170 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
172 ts_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
177 int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
181 char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
183 ts_CONF_lookup_fail(section, ENV_SERIAL);
186 TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
193 #ifndef OPENSSL_NO_ENGINE
195 int TS_CONF_set_crypto_device(CONF *conf, const char *section,
201 device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
203 if (device && !TS_CONF_set_default_engine(device)) {
204 ts_CONF_invalid(section, ENV_CRYPTO_DEVICE);
212 int TS_CONF_set_default_engine(const char *name)
217 if (strcmp(name, "builtin") == 0)
220 if ((e = ENGINE_by_id(name)) == NULL)
222 if (strcmp(name, "chil") == 0)
223 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
224 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
230 TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
231 ERR_add_error_data(2, "engine:", name);
239 int TS_CONF_set_signer_cert(CONF *conf, const char *section,
240 const char *cert, TS_RESP_CTX *ctx)
243 X509 *cert_obj = NULL;
246 cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
248 ts_CONF_lookup_fail(section, ENV_SIGNER_CERT);
252 if ((cert_obj = TS_CONF_load_cert(cert)) == NULL)
254 if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
263 int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
267 STACK_OF(X509) *certs_obj = NULL;
270 /* Certificate chain is optional. */
271 if ((certs = NCONF_get_string(conf, section, ENV_CERTS)) == NULL)
274 if ((certs_obj = TS_CONF_load_certs(certs)) == NULL)
276 if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
281 sk_X509_pop_free(certs_obj, X509_free);
285 int TS_CONF_set_signer_key(CONF *conf, const char *section,
286 const char *key, const char *pass,
290 EVP_PKEY *key_obj = NULL;
292 key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
294 ts_CONF_lookup_fail(section, ENV_SIGNER_KEY);
297 if ((key_obj = TS_CONF_load_key(key, pass)) == NULL)
299 if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
304 EVP_PKEY_free(key_obj);
308 int TS_CONF_set_signer_digest(CONF *conf, const char *section,
309 const char *md, TS_RESP_CTX *ctx)
312 const EVP_MD *sign_md = NULL;
314 md = NCONF_get_string(conf, section, ENV_SIGNER_DIGEST);
316 ts_CONF_lookup_fail(section, ENV_SIGNER_DIGEST);
319 sign_md = EVP_get_digestbyname(md);
320 if (sign_md == NULL) {
321 ts_CONF_invalid(section, ENV_SIGNER_DIGEST);
324 if (!TS_RESP_CTX_set_signer_digest(ctx, sign_md))
332 int TS_CONF_set_def_policy(CONF *conf, const char *section,
333 const char *policy, TS_RESP_CTX *ctx)
336 ASN1_OBJECT *policy_obj = NULL;
338 policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
340 ts_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
343 if ((policy_obj = OBJ_txt2obj(policy, 0)) == NULL) {
344 ts_CONF_invalid(section, ENV_DEFAULT_POLICY);
347 if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
352 ASN1_OBJECT_free(policy_obj);
356 int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
360 STACK_OF(CONF_VALUE) *list = NULL;
361 char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
363 /* If no other policy is specified, that's fine. */
364 if (policies && (list = X509V3_parse_list(policies)) == NULL) {
365 ts_CONF_invalid(section, ENV_OTHER_POLICIES);
368 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
369 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
370 const char *extval = val->value ? val->value : val->name;
373 if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
374 ts_CONF_invalid(section, ENV_OTHER_POLICIES);
377 if (!TS_RESP_CTX_add_policy(ctx, objtmp))
379 ASN1_OBJECT_free(objtmp);
384 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
388 int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
392 STACK_OF(CONF_VALUE) *list = NULL;
393 char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
395 if (digests == NULL) {
396 ts_CONF_lookup_fail(section, ENV_DIGESTS);
399 if ((list = X509V3_parse_list(digests)) == NULL) {
400 ts_CONF_invalid(section, ENV_DIGESTS);
403 if (sk_CONF_VALUE_num(list) == 0) {
404 ts_CONF_invalid(section, ENV_DIGESTS);
407 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
408 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
409 const char *extval = val->value ? val->value : val->name;
412 if ((md = EVP_get_digestbyname(extval)) == NULL) {
413 ts_CONF_invalid(section, ENV_DIGESTS);
416 if (!TS_RESP_CTX_add_md(ctx, md))
422 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
426 int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
430 int secs = 0, millis = 0, micros = 0;
431 STACK_OF(CONF_VALUE) *list = NULL;
432 char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
434 if (accuracy && (list = X509V3_parse_list(accuracy)) == NULL) {
435 ts_CONF_invalid(section, ENV_ACCURACY);
438 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
439 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
440 if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
442 secs = atoi(val->value);
443 } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
445 millis = atoi(val->value);
446 } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
448 micros = atoi(val->value);
450 ts_CONF_invalid(section, ENV_ACCURACY);
454 if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
459 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
463 int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
470 * If not specified, set the default value to 0, i.e. sec precision
472 if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
475 if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
476 ts_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
480 if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
488 static int ts_CONF_add_flag(CONF *conf, const char *section,
489 const char *field, int flag, TS_RESP_CTX *ctx)
491 const char *value = NCONF_get_string(conf, section, field);
494 if (strcmp(value, ENV_VALUE_YES) == 0)
495 TS_RESP_CTX_add_flags(ctx, flag);
496 else if (strcmp(value, ENV_VALUE_NO) != 0) {
497 ts_CONF_invalid(section, field);
505 int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
507 return ts_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
510 int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
512 return ts_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
515 int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
518 return ts_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
519 TS_ESS_CERT_ID_CHAIN, ctx);