update libressl to 2.8.2
[unleashed.git] / lib / libcrypto / ts / ts_conf.c
blob41d185ee5a0a28b044debd682e51b6a6e01440f6
1 /* $OpenBSD: ts_conf.c,v 1.11 2018/04/14 07:18:37 tb Exp $ */
2 /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #include <string.h>
61 #include <openssl/opensslconf.h>
63 #include <openssl/crypto.h>
64 #include <openssl/err.h>
65 #include <openssl/pem.h>
66 #include <openssl/ts.h>
68 #ifndef OPENSSL_NO_ENGINE
69 #include <openssl/engine.h>
70 #endif
72 /* Macro definitions for the configuration file. */
74 #define BASE_SECTION "tsa"
75 #define ENV_DEFAULT_TSA "default_tsa"
76 #define ENV_SERIAL "serial"
77 #define ENV_CRYPTO_DEVICE "crypto_device"
78 #define ENV_SIGNER_CERT "signer_cert"
79 #define ENV_CERTS "certs"
80 #define ENV_SIGNER_KEY "signer_key"
81 #define ENV_DEFAULT_POLICY "default_policy"
82 #define ENV_OTHER_POLICIES "other_policies"
83 #define ENV_DIGESTS "digests"
84 #define ENV_ACCURACY "accuracy"
85 #define ENV_ORDERING "ordering"
86 #define ENV_TSA_NAME "tsa_name"
87 #define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
88 #define ENV_VALUE_SECS "secs"
89 #define ENV_VALUE_MILLISECS "millisecs"
90 #define ENV_VALUE_MICROSECS "microsecs"
91 #define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
92 #define ENV_VALUE_YES "yes"
93 #define ENV_VALUE_NO "no"
95 /* Function definitions for certificate and key loading. */
97 X509 *
98 TS_CONF_load_cert(const char *file)
100 BIO *cert = NULL;
101 X509 *x = NULL;
103 if ((cert = BIO_new_file(file, "r")) == NULL)
104 goto end;
105 x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
107 end:
108 if (x == NULL)
109 fprintf(stderr, "unable to load certificate: %s\n", file);
110 BIO_free(cert);
111 return x;
114 STACK_OF(X509) *
115 TS_CONF_load_certs(const char *file)
117 BIO *certs = NULL;
118 STACK_OF(X509) *othercerts = NULL;
119 STACK_OF(X509_INFO) *allcerts = NULL;
120 int i;
122 if (!(certs = BIO_new_file(file, "r")))
123 goto end;
125 if (!(othercerts = sk_X509_new_null()))
126 goto end;
127 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
128 for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
129 X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
130 if (xi->x509) {
131 if (sk_X509_push(othercerts, xi->x509) == 0) {
132 sk_X509_pop_free(othercerts, X509_free);
133 othercerts = NULL;
134 goto end;
136 xi->x509 = NULL;
140 end:
141 if (othercerts == NULL)
142 fprintf(stderr, "unable to load certificates: %s\n", file);
143 sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
144 BIO_free(certs);
145 return othercerts;
148 EVP_PKEY *
149 TS_CONF_load_key(const char *file, const char *pass)
151 BIO *key = NULL;
152 EVP_PKEY *pkey = NULL;
154 if (!(key = BIO_new_file(file, "r")))
155 goto end;
156 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *) pass);
158 end:
159 if (pkey == NULL)
160 fprintf(stderr, "unable to load private key: %s\n", file);
161 BIO_free(key);
162 return pkey;
165 /* Function definitions for handling configuration options. */
167 static void
168 TS_CONF_lookup_fail(const char *name, const char *tag)
170 fprintf(stderr, "variable lookup failed for %s::%s\n", name, tag);
173 static void
174 TS_CONF_invalid(const char *name, const char *tag)
176 fprintf(stderr, "invalid variable value for %s::%s\n", name, tag);
179 const char *
180 TS_CONF_get_tsa_section(CONF *conf, const char *section)
182 if (!section) {
183 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
184 if (!section)
185 TS_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
187 return section;
191 TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
192 TS_RESP_CTX *ctx)
194 int ret = 0;
195 char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
197 if (!serial) {
198 TS_CONF_lookup_fail(section, ENV_SERIAL);
199 goto err;
201 TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
203 ret = 1;
205 err:
206 return ret;
209 #ifndef OPENSSL_NO_ENGINE
212 TS_CONF_set_crypto_device(CONF *conf, const char *section, const char *device)
214 int ret = 0;
216 if (!device)
217 device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
219 if (device && !TS_CONF_set_default_engine(device)) {
220 TS_CONF_invalid(section, ENV_CRYPTO_DEVICE);
221 goto err;
223 ret = 1;
225 err:
226 return ret;
230 TS_CONF_set_default_engine(const char *name)
232 ENGINE *e = NULL;
233 int ret = 0;
235 /* Leave the default if builtin specified. */
236 if (strcmp(name, "builtin") == 0)
237 return 1;
239 if (!(e = ENGINE_by_id(name)))
240 goto err;
241 /* All the operations are going to be carried out by the engine. */
242 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
243 goto err;
244 ret = 1;
246 err:
247 if (!ret) {
248 TSerror(TS_R_COULD_NOT_SET_ENGINE);
249 ERR_asprintf_error_data("engine:%s", name);
251 ENGINE_free(e);
252 return ret;
255 #endif
258 TS_CONF_set_signer_cert(CONF *conf, const char *section, const char *cert,
259 TS_RESP_CTX *ctx)
261 int ret = 0;
262 X509 *cert_obj = NULL;
264 if (!cert)
265 cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
266 if (!cert) {
267 TS_CONF_lookup_fail(section, ENV_SIGNER_CERT);
268 goto err;
270 if (!(cert_obj = TS_CONF_load_cert(cert)))
271 goto err;
272 if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
273 goto err;
275 ret = 1;
277 err:
278 X509_free(cert_obj);
279 return ret;
283 TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
284 TS_RESP_CTX *ctx)
286 int ret = 0;
287 STACK_OF(X509) *certs_obj = NULL;
289 if (!certs)
290 certs = NCONF_get_string(conf, section, ENV_CERTS);
291 /* Certificate chain is optional. */
292 if (!certs)
293 goto end;
294 if (!(certs_obj = TS_CONF_load_certs(certs)))
295 goto err;
296 if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
297 goto err;
299 end:
300 ret = 1;
301 err:
302 sk_X509_pop_free(certs_obj, X509_free);
303 return ret;
307 TS_CONF_set_signer_key(CONF *conf, const char *section, const char *key,
308 const char *pass, TS_RESP_CTX *ctx)
310 int ret = 0;
311 EVP_PKEY *key_obj = NULL;
313 if (!key)
314 key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
315 if (!key) {
316 TS_CONF_lookup_fail(section, ENV_SIGNER_KEY);
317 goto err;
319 if (!(key_obj = TS_CONF_load_key(key, pass)))
320 goto err;
321 if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
322 goto err;
324 ret = 1;
326 err:
327 EVP_PKEY_free(key_obj);
328 return ret;
332 TS_CONF_set_def_policy(CONF *conf, const char *section, const char *policy,
333 TS_RESP_CTX *ctx)
335 int ret = 0;
336 ASN1_OBJECT *policy_obj = NULL;
338 if (!policy)
339 policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
340 if (!policy) {
341 TS_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
342 goto err;
344 if (!(policy_obj = OBJ_txt2obj(policy, 0))) {
345 TS_CONF_invalid(section, ENV_DEFAULT_POLICY);
346 goto err;
348 if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
349 goto err;
351 ret = 1;
353 err:
354 ASN1_OBJECT_free(policy_obj);
355 return ret;
359 TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
361 int ret = 0;
362 int i;
363 STACK_OF(CONF_VALUE) *list = NULL;
364 char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
366 /* If no other policy is specified, that's fine. */
367 if (policies && !(list = X509V3_parse_list(policies))) {
368 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
369 goto err;
371 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
372 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
373 const char *extval = val->value ? val->value : val->name;
374 ASN1_OBJECT *objtmp;
375 if (!(objtmp = OBJ_txt2obj(extval, 0))) {
376 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
377 goto err;
379 if (!TS_RESP_CTX_add_policy(ctx, objtmp))
380 goto err;
381 ASN1_OBJECT_free(objtmp);
384 ret = 1;
386 err:
387 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
388 return ret;
392 TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
394 int ret = 0;
395 int i;
396 STACK_OF(CONF_VALUE) *list = NULL;
397 char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
399 if (!digests) {
400 TS_CONF_lookup_fail(section, ENV_DIGESTS);
401 goto err;
403 if (!(list = X509V3_parse_list(digests))) {
404 TS_CONF_invalid(section, ENV_DIGESTS);
405 goto err;
407 if (sk_CONF_VALUE_num(list) == 0) {
408 TS_CONF_invalid(section, ENV_DIGESTS);
409 goto err;
411 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
412 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
413 const char *extval = val->value ? val->value : val->name;
414 const EVP_MD *md;
415 if (!(md = EVP_get_digestbyname(extval))) {
416 TS_CONF_invalid(section, ENV_DIGESTS);
417 goto err;
419 if (!TS_RESP_CTX_add_md(ctx, md))
420 goto err;
423 ret = 1;
425 err:
426 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
427 return ret;
431 TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
433 int ret = 0;
434 int i;
435 int secs = 0, millis = 0, micros = 0;
436 STACK_OF(CONF_VALUE) *list = NULL;
437 char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
439 if (accuracy && !(list = X509V3_parse_list(accuracy))) {
440 TS_CONF_invalid(section, ENV_ACCURACY);
441 goto err;
443 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
444 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
445 if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
446 if (val->value)
447 secs = atoi(val->value);
448 } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
449 if (val->value)
450 millis = atoi(val->value);
451 } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
452 if (val->value)
453 micros = atoi(val->value);
454 } else {
455 TS_CONF_invalid(section, ENV_ACCURACY);
456 goto err;
459 if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
460 goto err;
462 ret = 1;
464 err:
465 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
466 return ret;
470 TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
471 TS_RESP_CTX *ctx)
473 int ret = 0;
474 long digits = 0;
476 /* If not specified, set the default value to 0, i.e. sec precision */
477 if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
478 &digits))
479 digits = 0;
480 if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
481 TS_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
482 goto err;
485 if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
486 goto err;
488 return 1;
490 err:
491 return ret;
494 static int
495 TS_CONF_add_flag(CONF *conf, const char *section, const char *field, int flag,
496 TS_RESP_CTX *ctx)
498 /* Default is false. */
499 const char *value = NCONF_get_string(conf, section, field);
501 if (value) {
502 if (strcmp(value, ENV_VALUE_YES) == 0)
503 TS_RESP_CTX_add_flags(ctx, flag);
504 else if (strcmp(value, ENV_VALUE_NO) != 0) {
505 TS_CONF_invalid(section, field);
506 return 0;
510 return 1;
514 TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
516 return TS_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
520 TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
522 return TS_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
526 TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section, TS_RESP_CTX *ctx)
528 return TS_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
529 TS_ESS_CERT_ID_CHAIN, ctx);