2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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"
40 #define ENV_ESS_CERT_ID_ALG "ess_cert_id_alg"
42 /* Function definitions for certificate and key loading. */
44 X509 *TS_CONF_load_cert(const char *file)
49 if ((cert = BIO_new_file(file, "r")) == NULL)
51 x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
54 TSerr(TS_F_TS_CONF_LOAD_CERT, TS_R_CANNOT_LOAD_CERT);
59 STACK_OF(X509) *TS_CONF_load_certs(const char *file)
62 STACK_OF(X509) *othercerts = NULL;
63 STACK_OF(X509_INFO) *allcerts = NULL;
66 if ((certs = BIO_new_file(file, "r")) == NULL)
68 if ((othercerts = sk_X509_new_null()) == NULL)
71 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
72 for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
73 X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
75 sk_X509_push(othercerts, xi->x509);
80 if (othercerts == NULL)
81 TSerr(TS_F_TS_CONF_LOAD_CERTS, TS_R_CANNOT_LOAD_CERT);
82 sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
87 EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
90 EVP_PKEY *pkey = NULL;
92 if ((key = BIO_new_file(file, "r")) == NULL)
94 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
97 TSerr(TS_F_TS_CONF_LOAD_KEY, TS_R_CANNOT_LOAD_KEY);
102 /* Function definitions for handling configuration options. */
104 static void ts_CONF_lookup_fail(const char *name, const char *tag)
106 TSerr(TS_F_TS_CONF_LOOKUP_FAIL, TS_R_VAR_LOOKUP_FAILURE);
107 ERR_add_error_data(3, name, "::", tag);
110 static void ts_CONF_invalid(const char *name, const char *tag)
112 TSerr(TS_F_TS_CONF_INVALID, TS_R_VAR_BAD_VALUE);
113 ERR_add_error_data(3, name, "::", tag);
116 const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
119 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
121 ts_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
126 int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
130 char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
132 ts_CONF_lookup_fail(section, ENV_SERIAL);
135 TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
142 #ifndef OPENSSL_NO_ENGINE
144 int TS_CONF_set_crypto_device(CONF *conf, const char *section,
150 device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
152 if (device && !TS_CONF_set_default_engine(device)) {
153 ts_CONF_invalid(section, ENV_CRYPTO_DEVICE);
161 int TS_CONF_set_default_engine(const char *name)
166 if (strcmp(name, "builtin") == 0)
169 if ((e = ENGINE_by_id(name)) == NULL)
171 if (strcmp(name, "chil") == 0)
172 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
173 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
179 TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
180 ERR_add_error_data(2, "engine:", name);
188 int TS_CONF_set_signer_cert(CONF *conf, const char *section,
189 const char *cert, TS_RESP_CTX *ctx)
192 X509 *cert_obj = NULL;
195 cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
197 ts_CONF_lookup_fail(section, ENV_SIGNER_CERT);
201 if ((cert_obj = TS_CONF_load_cert(cert)) == NULL)
203 if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
212 int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
216 STACK_OF(X509) *certs_obj = NULL;
219 /* Certificate chain is optional. */
220 if ((certs = NCONF_get_string(conf, section, ENV_CERTS)) == NULL)
223 if ((certs_obj = TS_CONF_load_certs(certs)) == NULL)
225 if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
230 sk_X509_pop_free(certs_obj, X509_free);
234 int TS_CONF_set_signer_key(CONF *conf, const char *section,
235 const char *key, const char *pass,
239 EVP_PKEY *key_obj = NULL;
241 key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
243 ts_CONF_lookup_fail(section, ENV_SIGNER_KEY);
246 if ((key_obj = TS_CONF_load_key(key, pass)) == NULL)
248 if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
253 EVP_PKEY_free(key_obj);
257 int TS_CONF_set_signer_digest(CONF *conf, const char *section,
258 const char *md, TS_RESP_CTX *ctx)
261 const EVP_MD *sign_md = NULL;
263 md = NCONF_get_string(conf, section, ENV_SIGNER_DIGEST);
265 ts_CONF_lookup_fail(section, ENV_SIGNER_DIGEST);
268 sign_md = EVP_get_digestbyname(md);
269 if (sign_md == NULL) {
270 ts_CONF_invalid(section, ENV_SIGNER_DIGEST);
273 if (!TS_RESP_CTX_set_signer_digest(ctx, sign_md))
281 int TS_CONF_set_def_policy(CONF *conf, const char *section,
282 const char *policy, TS_RESP_CTX *ctx)
285 ASN1_OBJECT *policy_obj = NULL;
287 policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
289 ts_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
292 if ((policy_obj = OBJ_txt2obj(policy, 0)) == NULL) {
293 ts_CONF_invalid(section, ENV_DEFAULT_POLICY);
296 if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
301 ASN1_OBJECT_free(policy_obj);
305 int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
309 STACK_OF(CONF_VALUE) *list = NULL;
310 char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
312 /* If no other policy is specified, that's fine. */
313 if (policies && (list = X509V3_parse_list(policies)) == NULL) {
314 ts_CONF_invalid(section, ENV_OTHER_POLICIES);
317 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
318 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
319 const char *extval = val->value ? val->value : val->name;
322 if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
323 ts_CONF_invalid(section, ENV_OTHER_POLICIES);
326 if (!TS_RESP_CTX_add_policy(ctx, objtmp))
328 ASN1_OBJECT_free(objtmp);
333 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
337 int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
341 STACK_OF(CONF_VALUE) *list = NULL;
342 char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
344 if (digests == NULL) {
345 ts_CONF_lookup_fail(section, ENV_DIGESTS);
348 if ((list = X509V3_parse_list(digests)) == NULL) {
349 ts_CONF_invalid(section, ENV_DIGESTS);
352 if (sk_CONF_VALUE_num(list) == 0) {
353 ts_CONF_invalid(section, ENV_DIGESTS);
356 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
357 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
358 const char *extval = val->value ? val->value : val->name;
361 if ((md = EVP_get_digestbyname(extval)) == NULL) {
362 ts_CONF_invalid(section, ENV_DIGESTS);
365 if (!TS_RESP_CTX_add_md(ctx, md))
371 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
375 int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
379 int secs = 0, millis = 0, micros = 0;
380 STACK_OF(CONF_VALUE) *list = NULL;
381 char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
383 if (accuracy && (list = X509V3_parse_list(accuracy)) == NULL) {
384 ts_CONF_invalid(section, ENV_ACCURACY);
387 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
388 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
389 if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
391 secs = atoi(val->value);
392 } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
394 millis = atoi(val->value);
395 } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
397 micros = atoi(val->value);
399 ts_CONF_invalid(section, ENV_ACCURACY);
403 if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
408 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
412 int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
419 * If not specified, set the default value to 0, i.e. sec precision
421 if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
424 if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
425 ts_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
429 if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
437 static int ts_CONF_add_flag(CONF *conf, const char *section,
438 const char *field, int flag, TS_RESP_CTX *ctx)
440 const char *value = NCONF_get_string(conf, section, field);
443 if (strcmp(value, ENV_VALUE_YES) == 0)
444 TS_RESP_CTX_add_flags(ctx, flag);
445 else if (strcmp(value, ENV_VALUE_NO) != 0) {
446 ts_CONF_invalid(section, field);
454 int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
456 return ts_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
459 int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
461 return ts_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
464 int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
467 return ts_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
468 TS_ESS_CERT_ID_CHAIN, ctx);
471 int TS_CONF_set_ess_cert_id_digest(CONF *conf, const char *section,
475 const EVP_MD *cert_md = NULL;
476 const char *md = NCONF_get_string(conf, section, ENV_ESS_CERT_ID_ALG);
481 cert_md = EVP_get_digestbyname(md);
482 if (cert_md == NULL) {
483 ts_CONF_invalid(section, ENV_ESS_CERT_ID_ALG);
487 if (!TS_RESP_CTX_set_ess_cert_id_digest(ctx, cert_md))