2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include <openssl/crypto.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/pem.h>
15 #include <openssl/engine.h>
16 #include <openssl/ts.h>
18 /* Macro definitions for the configuration file. */
19 #define BASE_SECTION "tsa"
20 #define ENV_DEFAULT_TSA "default_tsa"
21 #define ENV_SERIAL "serial"
22 #define ENV_CRYPTO_DEVICE "crypto_device"
23 #define ENV_SIGNER_CERT "signer_cert"
24 #define ENV_CERTS "certs"
25 #define ENV_SIGNER_KEY "signer_key"
26 #define ENV_SIGNER_DIGEST "signer_digest"
27 #define ENV_DEFAULT_POLICY "default_policy"
28 #define ENV_OTHER_POLICIES "other_policies"
29 #define ENV_DIGESTS "digests"
30 #define ENV_ACCURACY "accuracy"
31 #define ENV_ORDERING "ordering"
32 #define ENV_TSA_NAME "tsa_name"
33 #define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
34 #define ENV_VALUE_SECS "secs"
35 #define ENV_VALUE_MILLISECS "millisecs"
36 #define ENV_VALUE_MICROSECS "microsecs"
37 #define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
38 #define ENV_VALUE_YES "yes"
39 #define ENV_VALUE_NO "no"
41 /* Function definitions for certificate and key loading. */
43 X509 *TS_CONF_load_cert(const char *file)
48 if ((cert = BIO_new_file(file, "r")) == NULL)
50 x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
53 TSerr(TS_F_TS_CONF_LOAD_CERT, TS_R_CANNOT_LOAD_CERT);
58 STACK_OF(X509) *TS_CONF_load_certs(const char *file)
61 STACK_OF(X509) *othercerts = NULL;
62 STACK_OF(X509_INFO) *allcerts = NULL;
65 if ((certs = BIO_new_file(file, "r")) == NULL)
67 if ((othercerts = sk_X509_new_null()) == NULL)
70 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
71 for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
72 X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
74 sk_X509_push(othercerts, xi->x509);
79 if (othercerts == NULL)
80 TSerr(TS_F_TS_CONF_LOAD_CERTS, TS_R_CANNOT_LOAD_CERT);
81 sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
86 EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
89 EVP_PKEY *pkey = NULL;
91 if ((key = BIO_new_file(file, "r")) == NULL)
93 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
96 TSerr(TS_F_TS_CONF_LOAD_KEY, TS_R_CANNOT_LOAD_KEY);
101 /* Function definitions for handling configuration options. */
103 static void ts_CONF_lookup_fail(const char *name, const char *tag)
105 TSerr(TS_F_TS_CONF_LOOKUP_FAIL, TS_R_VAR_LOOKUP_FAILURE);
106 ERR_add_error_data(3, name, "::", tag);
109 static void ts_CONF_invalid(const char *name, const char *tag)
111 TSerr(TS_F_TS_CONF_INVALID, TS_R_VAR_BAD_VALUE);
112 ERR_add_error_data(3, name, "::", tag);
115 const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
118 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
120 ts_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
125 int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
129 char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
131 ts_CONF_lookup_fail(section, ENV_SERIAL);
134 TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
141 #ifndef OPENSSL_NO_ENGINE
143 int TS_CONF_set_crypto_device(CONF *conf, const char *section,
149 device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
151 if (device && !TS_CONF_set_default_engine(device)) {
152 ts_CONF_invalid(section, ENV_CRYPTO_DEVICE);
160 int TS_CONF_set_default_engine(const char *name)
165 if (strcmp(name, "builtin") == 0)
168 if ((e = ENGINE_by_id(name)) == NULL)
170 if (strcmp(name, "chil") == 0)
171 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
172 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
178 TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
179 ERR_add_error_data(2, "engine:", name);
187 int TS_CONF_set_signer_cert(CONF *conf, const char *section,
188 const char *cert, TS_RESP_CTX *ctx)
191 X509 *cert_obj = NULL;
194 cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
196 ts_CONF_lookup_fail(section, ENV_SIGNER_CERT);
200 if ((cert_obj = TS_CONF_load_cert(cert)) == NULL)
202 if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
211 int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
215 STACK_OF(X509) *certs_obj = NULL;
218 /* Certificate chain is optional. */
219 if ((certs = NCONF_get_string(conf, section, ENV_CERTS)) == NULL)
222 if ((certs_obj = TS_CONF_load_certs(certs)) == NULL)
224 if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
229 sk_X509_pop_free(certs_obj, X509_free);
233 int TS_CONF_set_signer_key(CONF *conf, const char *section,
234 const char *key, const char *pass,
238 EVP_PKEY *key_obj = NULL;
240 key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
242 ts_CONF_lookup_fail(section, ENV_SIGNER_KEY);
245 if ((key_obj = TS_CONF_load_key(key, pass)) == NULL)
247 if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
252 EVP_PKEY_free(key_obj);
256 int TS_CONF_set_signer_digest(CONF *conf, const char *section,
257 const char *md, TS_RESP_CTX *ctx)
260 const EVP_MD *sign_md = NULL;
262 md = NCONF_get_string(conf, section, ENV_SIGNER_DIGEST);
264 ts_CONF_lookup_fail(section, ENV_SIGNER_DIGEST);
267 sign_md = EVP_get_digestbyname(md);
268 if (sign_md == NULL) {
269 ts_CONF_invalid(section, ENV_SIGNER_DIGEST);
272 if (!TS_RESP_CTX_set_signer_digest(ctx, sign_md))
280 int TS_CONF_set_def_policy(CONF *conf, const char *section,
281 const char *policy, TS_RESP_CTX *ctx)
284 ASN1_OBJECT *policy_obj = NULL;
286 policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
288 ts_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
291 if ((policy_obj = OBJ_txt2obj(policy, 0)) == NULL) {
292 ts_CONF_invalid(section, ENV_DEFAULT_POLICY);
295 if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
300 ASN1_OBJECT_free(policy_obj);
304 int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
308 STACK_OF(CONF_VALUE) *list = NULL;
309 char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
311 /* If no other policy is specified, that's fine. */
312 if (policies && (list = X509V3_parse_list(policies)) == NULL) {
313 ts_CONF_invalid(section, ENV_OTHER_POLICIES);
316 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
317 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
318 const char *extval = val->value ? val->value : val->name;
321 if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
322 ts_CONF_invalid(section, ENV_OTHER_POLICIES);
325 if (!TS_RESP_CTX_add_policy(ctx, objtmp))
327 ASN1_OBJECT_free(objtmp);
332 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
336 int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
340 STACK_OF(CONF_VALUE) *list = NULL;
341 char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
343 if (digests == NULL) {
344 ts_CONF_lookup_fail(section, ENV_DIGESTS);
347 if ((list = X509V3_parse_list(digests)) == NULL) {
348 ts_CONF_invalid(section, ENV_DIGESTS);
351 if (sk_CONF_VALUE_num(list) == 0) {
352 ts_CONF_invalid(section, ENV_DIGESTS);
355 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
356 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
357 const char *extval = val->value ? val->value : val->name;
360 if ((md = EVP_get_digestbyname(extval)) == NULL) {
361 ts_CONF_invalid(section, ENV_DIGESTS);
364 if (!TS_RESP_CTX_add_md(ctx, md))
370 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
374 int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
378 int secs = 0, millis = 0, micros = 0;
379 STACK_OF(CONF_VALUE) *list = NULL;
380 char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
382 if (accuracy && (list = X509V3_parse_list(accuracy)) == NULL) {
383 ts_CONF_invalid(section, ENV_ACCURACY);
386 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
387 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
388 if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
390 secs = atoi(val->value);
391 } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
393 millis = atoi(val->value);
394 } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
396 micros = atoi(val->value);
398 ts_CONF_invalid(section, ENV_ACCURACY);
402 if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
407 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
411 int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
418 * If not specified, set the default value to 0, i.e. sec precision
420 if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
423 if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
424 ts_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
428 if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
436 static int ts_CONF_add_flag(CONF *conf, const char *section,
437 const char *field, int flag, TS_RESP_CTX *ctx)
439 const char *value = NCONF_get_string(conf, section, field);
442 if (strcmp(value, ENV_VALUE_YES) == 0)
443 TS_RESP_CTX_add_flags(ctx, flag);
444 else if (strcmp(value, ENV_VALUE_NO) != 0) {
445 ts_CONF_invalid(section, field);
453 int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
455 return ts_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
458 int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
460 return ts_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
463 int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
466 return ts_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
467 TS_ESS_CERT_ID_CHAIN, ctx);