Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / crypto / ts / ts_conf.c
blobf807c0a0acd0ff6b35839b7ea1fd4d4336f19bfc
1 /* $OpenBSD: ts_conf.c,v 1.8 2014/10/28 05:46:56 miod 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 TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE,
249 TS_R_COULD_NOT_SET_ENGINE);
250 ERR_asprintf_error_data("engine:%s", name);
252 if (e)
253 ENGINE_free(e);
254 return ret;
257 #endif
260 TS_CONF_set_signer_cert(CONF *conf, const char *section, const char *cert,
261 TS_RESP_CTX *ctx)
263 int ret = 0;
264 X509 *cert_obj = NULL;
266 if (!cert)
267 cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
268 if (!cert) {
269 TS_CONF_lookup_fail(section, ENV_SIGNER_CERT);
270 goto err;
272 if (!(cert_obj = TS_CONF_load_cert(cert)))
273 goto err;
274 if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
275 goto err;
277 ret = 1;
279 err:
280 X509_free(cert_obj);
281 return ret;
285 TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
286 TS_RESP_CTX *ctx)
288 int ret = 0;
289 STACK_OF(X509) *certs_obj = NULL;
291 if (!certs)
292 certs = NCONF_get_string(conf, section, ENV_CERTS);
293 /* Certificate chain is optional. */
294 if (!certs)
295 goto end;
296 if (!(certs_obj = TS_CONF_load_certs(certs)))
297 goto err;
298 if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
299 goto err;
301 end:
302 ret = 1;
303 err:
304 sk_X509_pop_free(certs_obj, X509_free);
305 return ret;
309 TS_CONF_set_signer_key(CONF *conf, const char *section, const char *key,
310 const char *pass, TS_RESP_CTX *ctx)
312 int ret = 0;
313 EVP_PKEY *key_obj = NULL;
315 if (!key)
316 key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
317 if (!key) {
318 TS_CONF_lookup_fail(section, ENV_SIGNER_KEY);
319 goto err;
321 if (!(key_obj = TS_CONF_load_key(key, pass)))
322 goto err;
323 if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
324 goto err;
326 ret = 1;
328 err:
329 EVP_PKEY_free(key_obj);
330 return ret;
334 TS_CONF_set_def_policy(CONF *conf, const char *section, const char *policy,
335 TS_RESP_CTX *ctx)
337 int ret = 0;
338 ASN1_OBJECT *policy_obj = NULL;
340 if (!policy)
341 policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
342 if (!policy) {
343 TS_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
344 goto err;
346 if (!(policy_obj = OBJ_txt2obj(policy, 0))) {
347 TS_CONF_invalid(section, ENV_DEFAULT_POLICY);
348 goto err;
350 if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
351 goto err;
353 ret = 1;
355 err:
356 ASN1_OBJECT_free(policy_obj);
357 return ret;
361 TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
363 int ret = 0;
364 int i;
365 STACK_OF(CONF_VALUE) *list = NULL;
366 char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
368 /* If no other policy is specified, that's fine. */
369 if (policies && !(list = X509V3_parse_list(policies))) {
370 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
371 goto err;
373 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
374 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
375 const char *extval = val->value ? val->value : val->name;
376 ASN1_OBJECT *objtmp;
377 if (!(objtmp = OBJ_txt2obj(extval, 0))) {
378 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
379 goto err;
381 if (!TS_RESP_CTX_add_policy(ctx, objtmp))
382 goto err;
383 ASN1_OBJECT_free(objtmp);
386 ret = 1;
388 err:
389 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
390 return ret;
394 TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
396 int ret = 0;
397 int i;
398 STACK_OF(CONF_VALUE) *list = NULL;
399 char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
401 if (!digests) {
402 TS_CONF_lookup_fail(section, ENV_DIGESTS);
403 goto err;
405 if (!(list = X509V3_parse_list(digests))) {
406 TS_CONF_invalid(section, ENV_DIGESTS);
407 goto err;
409 if (sk_CONF_VALUE_num(list) == 0) {
410 TS_CONF_invalid(section, ENV_DIGESTS);
411 goto err;
413 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
414 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
415 const char *extval = val->value ? val->value : val->name;
416 const EVP_MD *md;
417 if (!(md = EVP_get_digestbyname(extval))) {
418 TS_CONF_invalid(section, ENV_DIGESTS);
419 goto err;
421 if (!TS_RESP_CTX_add_md(ctx, md))
422 goto err;
425 ret = 1;
427 err:
428 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
429 return ret;
433 TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
435 int ret = 0;
436 int i;
437 int secs = 0, millis = 0, micros = 0;
438 STACK_OF(CONF_VALUE) *list = NULL;
439 char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
441 if (accuracy && !(list = X509V3_parse_list(accuracy))) {
442 TS_CONF_invalid(section, ENV_ACCURACY);
443 goto err;
445 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
446 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
447 if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
448 if (val->value)
449 secs = atoi(val->value);
450 } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
451 if (val->value)
452 millis = atoi(val->value);
453 } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
454 if (val->value)
455 micros = atoi(val->value);
456 } else {
457 TS_CONF_invalid(section, ENV_ACCURACY);
458 goto err;
461 if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
462 goto err;
464 ret = 1;
466 err:
467 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
468 return ret;
472 TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
473 TS_RESP_CTX *ctx)
475 int ret = 0;
476 long digits = 0;
478 /* If not specified, set the default value to 0, i.e. sec precision */
479 if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
480 &digits))
481 digits = 0;
482 if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
483 TS_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
484 goto err;
487 if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
488 goto err;
490 return 1;
492 err:
493 return ret;
496 static int
497 TS_CONF_add_flag(CONF *conf, const char *section, const char *field, int flag,
498 TS_RESP_CTX *ctx)
500 /* Default is false. */
501 const char *value = NCONF_get_string(conf, section, field);
503 if (value) {
504 if (strcmp(value, ENV_VALUE_YES) == 0)
505 TS_RESP_CTX_add_flags(ctx, flag);
506 else if (strcmp(value, ENV_VALUE_NO) != 0) {
507 TS_CONF_invalid(section, field);
508 return 0;
512 return 1;
516 TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
518 return TS_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
522 TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
524 return TS_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
528 TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section, TS_RESP_CTX *ctx)
530 return TS_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
531 TS_ESS_CERT_ID_CHAIN, ctx);