1 /* $OpenBSD: ts_conf.c,v 1.14 2024/03/26 00:39:22 beck 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.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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.
19 *
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/)"
24 *
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.
29 *
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.
33 *
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/)"
38 *
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 * ====================================================================
52 *
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).
56 *
57 */
58
59 #include <string.h>
60
61 #include <openssl/opensslconf.h>
62
63 #include <openssl/crypto.h>
64 #include <openssl/err.h>
65 #include <openssl/pem.h>
66 #include <openssl/ts.h>
67
68 /* Macro definitions for the configuration file. */
69
70 #define BASE_SECTION "tsa"
71 #define ENV_DEFAULT_TSA "default_tsa"
72 #define ENV_SERIAL "serial"
73 #define ENV_CRYPTO_DEVICE "crypto_device"
74 #define ENV_SIGNER_CERT "signer_cert"
75 #define ENV_CERTS "certs"
76 #define ENV_SIGNER_KEY "signer_key"
77 #define ENV_DEFAULT_POLICY "default_policy"
78 #define ENV_OTHER_POLICIES "other_policies"
79 #define ENV_DIGESTS "digests"
80 #define ENV_ACCURACY "accuracy"
81 #define ENV_ORDERING "ordering"
82 #define ENV_TSA_NAME "tsa_name"
83 #define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
84 #define ENV_VALUE_SECS "secs"
85 #define ENV_VALUE_MILLISECS "millisecs"
86 #define ENV_VALUE_MICROSECS "microsecs"
87 #define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
88 #define ENV_VALUE_YES "yes"
89 #define ENV_VALUE_NO "no"
90
91 /* Function definitions for certificate and key loading. */
92
93 X509 *
TS_CONF_load_cert(const char * file)94 TS_CONF_load_cert(const char *file)
95 {
96 BIO *cert = NULL;
97 X509 *x = NULL;
98
99 if ((cert = BIO_new_file(file, "r")) == NULL)
100 goto end;
101 x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
102
103 end:
104 if (x == NULL)
105 fprintf(stderr, "unable to load certificate: %s\n", file);
106 BIO_free(cert);
107 return x;
108 }
109 LCRYPTO_ALIAS(TS_CONF_load_cert);
110
STACK_OF(X509)111 STACK_OF(X509) *
112 TS_CONF_load_certs(const char *file)
113 {
114 BIO *certs = NULL;
115 STACK_OF(X509) *othercerts = NULL;
116 STACK_OF(X509_INFO) *allcerts = NULL;
117 int i;
118
119 if (!(certs = BIO_new_file(file, "r")))
120 goto end;
121
122 if (!(othercerts = sk_X509_new_null()))
123 goto end;
124 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
125 for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
126 X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
127 if (xi->x509) {
128 if (sk_X509_push(othercerts, xi->x509) == 0) {
129 sk_X509_pop_free(othercerts, X509_free);
130 othercerts = NULL;
131 goto end;
132 }
133 xi->x509 = NULL;
134 }
135 }
136
137 end:
138 if (othercerts == NULL)
139 fprintf(stderr, "unable to load certificates: %s\n", file);
140 sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
141 BIO_free(certs);
142 return othercerts;
143 }
144 LCRYPTO_ALIAS(TS_CONF_load_certs);
145
146 EVP_PKEY *
TS_CONF_load_key(const char * file,const char * pass)147 TS_CONF_load_key(const char *file, const char *pass)
148 {
149 BIO *key = NULL;
150 EVP_PKEY *pkey = NULL;
151
152 if (!(key = BIO_new_file(file, "r")))
153 goto end;
154 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *) pass);
155
156 end:
157 if (pkey == NULL)
158 fprintf(stderr, "unable to load private key: %s\n", file);
159 BIO_free(key);
160 return pkey;
161 }
162 LCRYPTO_ALIAS(TS_CONF_load_key);
163
164 /* Function definitions for handling configuration options. */
165
166 static void
TS_CONF_lookup_fail(const char * name,const char * tag)167 TS_CONF_lookup_fail(const char *name, const char *tag)
168 {
169 fprintf(stderr, "variable lookup failed for %s::%s\n", name, tag);
170 }
171
172 static void
TS_CONF_invalid(const char * name,const char * tag)173 TS_CONF_invalid(const char *name, const char *tag)
174 {
175 fprintf(stderr, "invalid variable value for %s::%s\n", name, tag);
176 }
177
178 const char *
TS_CONF_get_tsa_section(CONF * conf,const char * section)179 TS_CONF_get_tsa_section(CONF *conf, const char *section)
180 {
181 if (!section) {
182 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
183 if (!section)
184 TS_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
185 }
186 return section;
187 }
188 LCRYPTO_ALIAS(TS_CONF_get_tsa_section);
189
190 int
TS_CONF_set_serial(CONF * conf,const char * section,TS_serial_cb cb,TS_RESP_CTX * ctx)191 TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
192 TS_RESP_CTX *ctx)
193 {
194 int ret = 0;
195 char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
196
197 if (!serial) {
198 TS_CONF_lookup_fail(section, ENV_SERIAL);
199 goto err;
200 }
201 TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
202
203 ret = 1;
204
205 err:
206 return ret;
207 }
208 LCRYPTO_ALIAS(TS_CONF_set_serial);
209
210 int
TS_CONF_set_signer_cert(CONF * conf,const char * section,const char * cert,TS_RESP_CTX * ctx)211 TS_CONF_set_signer_cert(CONF *conf, const char *section, const char *cert,
212 TS_RESP_CTX *ctx)
213 {
214 int ret = 0;
215 X509 *cert_obj = NULL;
216
217 if (!cert)
218 cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
219 if (!cert) {
220 TS_CONF_lookup_fail(section, ENV_SIGNER_CERT);
221 goto err;
222 }
223 if (!(cert_obj = TS_CONF_load_cert(cert)))
224 goto err;
225 if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
226 goto err;
227
228 ret = 1;
229
230 err:
231 X509_free(cert_obj);
232 return ret;
233 }
234 LCRYPTO_ALIAS(TS_CONF_set_signer_cert);
235
236 int
TS_CONF_set_certs(CONF * conf,const char * section,const char * certs,TS_RESP_CTX * ctx)237 TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
238 TS_RESP_CTX *ctx)
239 {
240 int ret = 0;
241 STACK_OF(X509) *certs_obj = NULL;
242
243 if (!certs)
244 certs = NCONF_get_string(conf, section, ENV_CERTS);
245 /* Certificate chain is optional. */
246 if (!certs)
247 goto end;
248 if (!(certs_obj = TS_CONF_load_certs(certs)))
249 goto err;
250 if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
251 goto err;
252
253 end:
254 ret = 1;
255 err:
256 sk_X509_pop_free(certs_obj, X509_free);
257 return ret;
258 }
259 LCRYPTO_ALIAS(TS_CONF_set_certs);
260
261 int
TS_CONF_set_signer_key(CONF * conf,const char * section,const char * key,const char * pass,TS_RESP_CTX * ctx)262 TS_CONF_set_signer_key(CONF *conf, const char *section, const char *key,
263 const char *pass, TS_RESP_CTX *ctx)
264 {
265 int ret = 0;
266 EVP_PKEY *key_obj = NULL;
267
268 if (!key)
269 key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
270 if (!key) {
271 TS_CONF_lookup_fail(section, ENV_SIGNER_KEY);
272 goto err;
273 }
274 if (!(key_obj = TS_CONF_load_key(key, pass)))
275 goto err;
276 if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
277 goto err;
278
279 ret = 1;
280
281 err:
282 EVP_PKEY_free(key_obj);
283 return ret;
284 }
285 LCRYPTO_ALIAS(TS_CONF_set_signer_key);
286
287 int
TS_CONF_set_def_policy(CONF * conf,const char * section,const char * policy,TS_RESP_CTX * ctx)288 TS_CONF_set_def_policy(CONF *conf, const char *section, const char *policy,
289 TS_RESP_CTX *ctx)
290 {
291 int ret = 0;
292 ASN1_OBJECT *policy_obj = NULL;
293
294 if (!policy)
295 policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
296 if (!policy) {
297 TS_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
298 goto err;
299 }
300 if (!(policy_obj = OBJ_txt2obj(policy, 0))) {
301 TS_CONF_invalid(section, ENV_DEFAULT_POLICY);
302 goto err;
303 }
304 if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
305 goto err;
306
307 ret = 1;
308
309 err:
310 ASN1_OBJECT_free(policy_obj);
311 return ret;
312 }
313 LCRYPTO_ALIAS(TS_CONF_set_def_policy);
314
315 int
TS_CONF_set_policies(CONF * conf,const char * section,TS_RESP_CTX * ctx)316 TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
317 {
318 int ret = 0;
319 int i;
320 STACK_OF(CONF_VALUE) *list = NULL;
321 char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
322
323 /* If no other policy is specified, that's fine. */
324 if (policies && !(list = X509V3_parse_list(policies))) {
325 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
326 goto err;
327 }
328 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
329 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
330 const char *extval = val->value ? val->value : val->name;
331 ASN1_OBJECT *objtmp;
332 if (!(objtmp = OBJ_txt2obj(extval, 0))) {
333 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
334 goto err;
335 }
336 if (!TS_RESP_CTX_add_policy(ctx, objtmp))
337 goto err;
338 ASN1_OBJECT_free(objtmp);
339 }
340
341 ret = 1;
342
343 err:
344 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
345 return ret;
346 }
347 LCRYPTO_ALIAS(TS_CONF_set_policies);
348
349 int
TS_CONF_set_digests(CONF * conf,const char * section,TS_RESP_CTX * ctx)350 TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
351 {
352 int ret = 0;
353 int i;
354 STACK_OF(CONF_VALUE) *list = NULL;
355 char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
356
357 if (!digests) {
358 TS_CONF_lookup_fail(section, ENV_DIGESTS);
359 goto err;
360 }
361 if (!(list = X509V3_parse_list(digests))) {
362 TS_CONF_invalid(section, ENV_DIGESTS);
363 goto err;
364 }
365 if (sk_CONF_VALUE_num(list) == 0) {
366 TS_CONF_invalid(section, ENV_DIGESTS);
367 goto err;
368 }
369 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
370 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
371 const char *extval = val->value ? val->value : val->name;
372 const EVP_MD *md;
373 if (!(md = EVP_get_digestbyname(extval))) {
374 TS_CONF_invalid(section, ENV_DIGESTS);
375 goto err;
376 }
377 if (!TS_RESP_CTX_add_md(ctx, md))
378 goto err;
379 }
380
381 ret = 1;
382
383 err:
384 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
385 return ret;
386 }
387 LCRYPTO_ALIAS(TS_CONF_set_digests);
388
389 int
TS_CONF_set_accuracy(CONF * conf,const char * section,TS_RESP_CTX * ctx)390 TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
391 {
392 int ret = 0;
393 int i;
394 int secs = 0, millis = 0, micros = 0;
395 STACK_OF(CONF_VALUE) *list = NULL;
396 char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
397
398 if (accuracy && !(list = X509V3_parse_list(accuracy))) {
399 TS_CONF_invalid(section, ENV_ACCURACY);
400 goto err;
401 }
402 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
403 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
404 if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
405 if (val->value)
406 secs = atoi(val->value);
407 } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
408 if (val->value)
409 millis = atoi(val->value);
410 } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
411 if (val->value)
412 micros = atoi(val->value);
413 } else {
414 TS_CONF_invalid(section, ENV_ACCURACY);
415 goto err;
416 }
417 }
418 if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
419 goto err;
420
421 ret = 1;
422
423 err:
424 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
425 return ret;
426 }
427 LCRYPTO_ALIAS(TS_CONF_set_accuracy);
428
429 int
TS_CONF_set_clock_precision_digits(CONF * conf,const char * section,TS_RESP_CTX * ctx)430 TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
431 TS_RESP_CTX *ctx)
432 {
433 int ret = 0;
434 long digits = 0;
435
436 /* If not specified, set the default value to 0, i.e. sec precision */
437 if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
438 &digits))
439 digits = 0;
440 /* We only support second precision, so reject everything else */
441 if (digits != 0) {
442 TS_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
443 goto err;
444 }
445
446 if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
447 goto err;
448
449 return 1;
450
451 err:
452 return ret;
453 }
454 LCRYPTO_ALIAS(TS_CONF_set_clock_precision_digits);
455
456 static int
TS_CONF_add_flag(CONF * conf,const char * section,const char * field,int flag,TS_RESP_CTX * ctx)457 TS_CONF_add_flag(CONF *conf, const char *section, const char *field, int flag,
458 TS_RESP_CTX *ctx)
459 {
460 /* Default is false. */
461 const char *value = NCONF_get_string(conf, section, field);
462
463 if (value) {
464 if (strcmp(value, ENV_VALUE_YES) == 0)
465 TS_RESP_CTX_add_flags(ctx, flag);
466 else if (strcmp(value, ENV_VALUE_NO) != 0) {
467 TS_CONF_invalid(section, field);
468 return 0;
469 }
470 }
471
472 return 1;
473 }
474
475 int
TS_CONF_set_ordering(CONF * conf,const char * section,TS_RESP_CTX * ctx)476 TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
477 {
478 return TS_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
479 }
480 LCRYPTO_ALIAS(TS_CONF_set_ordering);
481
482 int
TS_CONF_set_tsa_name(CONF * conf,const char * section,TS_RESP_CTX * ctx)483 TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
484 {
485 return TS_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
486 }
487 LCRYPTO_ALIAS(TS_CONF_set_tsa_name);
488
489 int
TS_CONF_set_ess_cert_id_chain(CONF * conf,const char * section,TS_RESP_CTX * ctx)490 TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section, TS_RESP_CTX *ctx)
491 {
492 return TS_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
493 TS_ESS_CERT_ID_CHAIN, ctx);
494 }
495 LCRYPTO_ALIAS(TS_CONF_set_ess_cert_id_chain);
496