xref: /freebsd/crypto/openssl/crypto/ts/ts_conf.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2006-2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12 
13 #include <string.h>
14 
15 #include <openssl/crypto.h>
16 #include "internal/cryptlib.h"
17 #include <openssl/pem.h>
18 #include <openssl/engine.h>
19 #include <openssl/ts.h>
20 #include <openssl/conf_api.h>
21 
22 /* Macro definitions for the configuration file. */
23 #define BASE_SECTION "tsa"
24 #define ENV_DEFAULT_TSA "default_tsa"
25 #define ENV_SERIAL "serial"
26 #define ENV_CRYPTO_DEVICE "crypto_device"
27 #define ENV_SIGNER_CERT "signer_cert"
28 #define ENV_CERTS "certs"
29 #define ENV_SIGNER_KEY "signer_key"
30 #define ENV_SIGNER_DIGEST "signer_digest"
31 #define ENV_DEFAULT_POLICY "default_policy"
32 #define ENV_OTHER_POLICIES "other_policies"
33 #define ENV_DIGESTS "digests"
34 #define ENV_ACCURACY "accuracy"
35 #define ENV_ORDERING "ordering"
36 #define ENV_TSA_NAME "tsa_name"
37 #define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
38 #define ENV_VALUE_SECS "secs"
39 #define ENV_VALUE_MILLISECS "millisecs"
40 #define ENV_VALUE_MICROSECS "microsecs"
41 #define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
42 #define ENV_VALUE_YES "yes"
43 #define ENV_VALUE_NO "no"
44 #define ENV_ESS_CERT_ID_ALG "ess_cert_id_alg"
45 
46 /* Function definitions for certificate and key loading. */
47 
TS_CONF_load_cert(const char * file)48 X509 *TS_CONF_load_cert(const char *file)
49 {
50     BIO *cert = NULL;
51     X509 *x = NULL;
52 
53 #if defined(OPENSSL_SYS_WINDOWS)
54     if ((cert = BIO_new_file(file, "rb")) == NULL)
55 #else
56     if ((cert = BIO_new_file(file, "r")) == NULL)
57 #endif
58         goto end;
59     x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
60 end:
61     if (x == NULL)
62         ERR_raise(ERR_LIB_TS, TS_R_CANNOT_LOAD_CERT);
63     BIO_free(cert);
64     return x;
65 }
66 
STACK_OF(X509)67 STACK_OF(X509) *TS_CONF_load_certs(const char *file)
68 {
69     BIO *certs = NULL;
70     STACK_OF(X509) *othercerts = NULL;
71     STACK_OF(X509_INFO) *allcerts = NULL;
72     int i;
73 
74 #if defined(OPENSSL_SYS_WINDOWS)
75     if ((certs = BIO_new_file(file, "rb")) == NULL)
76 #else
77     if ((certs = BIO_new_file(file, "r")) == NULL)
78 #endif
79         goto end;
80     if ((othercerts = sk_X509_new_null()) == NULL)
81         goto end;
82 
83     allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
84     for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
85         X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
86 
87         if (xi->x509 != NULL) {
88             if (!X509_add_cert(othercerts, xi->x509, X509_ADD_FLAG_DEFAULT)) {
89                 OSSL_STACK_OF_X509_free(othercerts);
90                 othercerts = NULL;
91                 goto end;
92             }
93             xi->x509 = NULL;
94         }
95     }
96 end:
97     if (othercerts == NULL)
98         ERR_raise(ERR_LIB_TS, TS_R_CANNOT_LOAD_CERT);
99     sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
100     BIO_free(certs);
101     return othercerts;
102 }
103 
TS_CONF_load_key(const char * file,const char * pass)104 EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
105 {
106     BIO *key = NULL;
107     EVP_PKEY *pkey = NULL;
108 
109 #if defined(OPENSSL_SYS_WINDOWS)
110     if ((key = BIO_new_file(file, "rb")) == NULL)
111 #else
112     if ((key = BIO_new_file(file, "r")) == NULL)
113 #endif
114         goto end;
115     pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
116 end:
117     if (pkey == NULL)
118         ERR_raise(ERR_LIB_TS, TS_R_CANNOT_LOAD_KEY);
119     BIO_free(key);
120     return pkey;
121 }
122 
123 /* Function definitions for handling configuration options. */
124 
ts_CONF_lookup_fail(const char * name,const char * tag)125 static void ts_CONF_lookup_fail(const char *name, const char *tag)
126 {
127     ERR_raise_data(ERR_LIB_TS, TS_R_VAR_LOOKUP_FAILURE, "%s::%s", name, tag);
128 }
129 
ts_CONF_invalid(const char * name,const char * tag)130 static void ts_CONF_invalid(const char *name, const char *tag)
131 {
132     ERR_raise_data(ERR_LIB_TS, TS_R_VAR_BAD_VALUE, "%s::%s", name, tag);
133 }
134 
TS_CONF_get_tsa_section(CONF * conf,const char * section)135 const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
136 {
137     if (!section) {
138         section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
139         if (!section)
140             ts_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
141     }
142     return section;
143 }
144 
TS_CONF_set_serial(CONF * conf,const char * section,TS_serial_cb cb,TS_RESP_CTX * ctx)145 int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
146     TS_RESP_CTX *ctx)
147 {
148     int ret = 0;
149     char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
150     if (!serial) {
151         ts_CONF_lookup_fail(section, ENV_SERIAL);
152         goto err;
153     }
154     TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
155 
156     ret = 1;
157 err:
158     return ret;
159 }
160 
161 #ifndef OPENSSL_NO_ENGINE
162 
TS_CONF_set_crypto_device(CONF * conf,const char * section,const char * device)163 int TS_CONF_set_crypto_device(CONF *conf, const char *section,
164     const char *device)
165 {
166     int ret = 0;
167 
168     if (device == NULL)
169         device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
170 
171     if (device && !TS_CONF_set_default_engine(device)) {
172         ts_CONF_invalid(section, ENV_CRYPTO_DEVICE);
173         goto err;
174     }
175     ret = 1;
176 err:
177     return ret;
178 }
179 
TS_CONF_set_default_engine(const char * name)180 int TS_CONF_set_default_engine(const char *name)
181 {
182     ENGINE *e = NULL;
183     int ret = 0;
184 
185     if (strcmp(name, "builtin") == 0)
186         return 1;
187 
188     if ((e = ENGINE_by_id(name)) == NULL)
189         goto err;
190     if (strcmp(name, "chil") == 0)
191         ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
192     if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
193         goto err;
194     ret = 1;
195 
196 err:
197     if (!ret)
198         ERR_raise_data(ERR_LIB_TS, TS_R_COULD_NOT_SET_ENGINE,
199             "engine:%s", name);
200     ENGINE_free(e);
201     return ret;
202 }
203 
204 #endif
205 
TS_CONF_set_signer_cert(CONF * conf,const char * section,const char * cert,TS_RESP_CTX * ctx)206 int TS_CONF_set_signer_cert(CONF *conf, const char *section,
207     const char *cert, TS_RESP_CTX *ctx)
208 {
209     int ret = 0;
210     X509 *cert_obj = NULL;
211 
212     if (cert == NULL) {
213         cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
214         if (cert == NULL) {
215             ts_CONF_lookup_fail(section, ENV_SIGNER_CERT);
216             goto err;
217         }
218     }
219     if ((cert_obj = TS_CONF_load_cert(cert)) == NULL)
220         goto err;
221     if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
222         goto err;
223 
224     ret = 1;
225 err:
226     X509_free(cert_obj);
227     return ret;
228 }
229 
TS_CONF_set_certs(CONF * conf,const char * section,const char * certs,TS_RESP_CTX * ctx)230 int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
231     TS_RESP_CTX *ctx)
232 {
233     int ret = 0;
234     STACK_OF(X509) *certs_obj = NULL;
235 
236     if (certs == NULL) {
237         /* Certificate chain is optional. */
238         if ((certs = NCONF_get_string(conf, section, ENV_CERTS)) == NULL)
239             goto end;
240     }
241     if ((certs_obj = TS_CONF_load_certs(certs)) == NULL)
242         goto err;
243     if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
244         goto err;
245 end:
246     ret = 1;
247 err:
248     OSSL_STACK_OF_X509_free(certs_obj);
249     return ret;
250 }
251 
TS_CONF_set_signer_key(CONF * conf,const char * section,const char * key,const char * pass,TS_RESP_CTX * ctx)252 int TS_CONF_set_signer_key(CONF *conf, const char *section,
253     const char *key, const char *pass,
254     TS_RESP_CTX *ctx)
255 {
256     int ret = 0;
257     EVP_PKEY *key_obj = NULL;
258     if (!key)
259         key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
260     if (!key) {
261         ts_CONF_lookup_fail(section, ENV_SIGNER_KEY);
262         goto err;
263     }
264     if ((key_obj = TS_CONF_load_key(key, pass)) == NULL)
265         goto err;
266     if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
267         goto err;
268 
269     ret = 1;
270 err:
271     EVP_PKEY_free(key_obj);
272     return ret;
273 }
274 
TS_CONF_set_signer_digest(CONF * conf,const char * section,const char * md,TS_RESP_CTX * ctx)275 int TS_CONF_set_signer_digest(CONF *conf, const char *section,
276     const char *md, TS_RESP_CTX *ctx)
277 {
278     int ret = 0;
279     const EVP_MD *sign_md = NULL;
280     if (md == NULL)
281         md = NCONF_get_string(conf, section, ENV_SIGNER_DIGEST);
282     if (md == NULL) {
283         ts_CONF_lookup_fail(section, ENV_SIGNER_DIGEST);
284         goto err;
285     }
286     sign_md = EVP_get_digestbyname(md);
287     if (sign_md == NULL) {
288         ts_CONF_invalid(section, ENV_SIGNER_DIGEST);
289         goto err;
290     }
291     if (!TS_RESP_CTX_set_signer_digest(ctx, sign_md))
292         goto err;
293 
294     ret = 1;
295 err:
296     return ret;
297 }
298 
TS_CONF_set_def_policy(CONF * conf,const char * section,const char * policy,TS_RESP_CTX * ctx)299 int TS_CONF_set_def_policy(CONF *conf, const char *section,
300     const char *policy, TS_RESP_CTX *ctx)
301 {
302     int ret = 0;
303     ASN1_OBJECT *policy_obj = NULL;
304 
305     if (policy == NULL)
306         policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
307     if (policy == NULL) {
308         ts_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
309         goto err;
310     }
311     if ((policy_obj = OBJ_txt2obj(policy, 0)) == NULL) {
312         ts_CONF_invalid(section, ENV_DEFAULT_POLICY);
313         goto err;
314     }
315     if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
316         goto err;
317 
318     ret = 1;
319 err:
320     ASN1_OBJECT_free(policy_obj);
321     return ret;
322 }
323 
TS_CONF_set_policies(CONF * conf,const char * section,TS_RESP_CTX * ctx)324 int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
325 {
326     int ret = 0;
327     int i;
328     STACK_OF(CONF_VALUE) *list = NULL;
329     char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
330 
331     /* If no other policy is specified, that's fine. */
332     if (policies && (list = X509V3_parse_list(policies)) == NULL) {
333         ts_CONF_invalid(section, ENV_OTHER_POLICIES);
334         goto err;
335     }
336     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
337         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
338         const char *extval = val->value ? val->value : val->name;
339         ASN1_OBJECT *objtmp;
340 
341         if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
342             ts_CONF_invalid(section, ENV_OTHER_POLICIES);
343             goto err;
344         }
345         if (!TS_RESP_CTX_add_policy(ctx, objtmp)) {
346             ASN1_OBJECT_free(objtmp);
347             goto err;
348         }
349         ASN1_OBJECT_free(objtmp);
350     }
351 
352     ret = 1;
353 err:
354     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
355     return ret;
356 }
357 
TS_CONF_set_digests(CONF * conf,const char * section,TS_RESP_CTX * ctx)358 int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
359 {
360     int ret = 0;
361     int i;
362     STACK_OF(CONF_VALUE) *list = NULL;
363     char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
364 
365     if (digests == NULL) {
366         ts_CONF_lookup_fail(section, ENV_DIGESTS);
367         goto err;
368     }
369     if ((list = X509V3_parse_list(digests)) == NULL) {
370         ts_CONF_invalid(section, ENV_DIGESTS);
371         goto err;
372     }
373     if (sk_CONF_VALUE_num(list) == 0) {
374         ts_CONF_invalid(section, ENV_DIGESTS);
375         goto err;
376     }
377     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
378         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
379         const char *extval = val->value ? val->value : val->name;
380         const EVP_MD *md;
381 
382         if ((md = EVP_get_digestbyname(extval)) == NULL) {
383             ts_CONF_invalid(section, ENV_DIGESTS);
384             goto err;
385         }
386         if (!TS_RESP_CTX_add_md(ctx, md))
387             goto err;
388     }
389 
390     ret = 1;
391 err:
392     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
393     return ret;
394 }
395 
TS_CONF_set_accuracy(CONF * conf,const char * section,TS_RESP_CTX * ctx)396 int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
397 {
398     int ret = 0;
399     int i;
400     int secs = 0, millis = 0, micros = 0;
401     STACK_OF(CONF_VALUE) *list = NULL;
402     char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
403 
404     if (accuracy && (list = X509V3_parse_list(accuracy)) == NULL) {
405         ts_CONF_invalid(section, ENV_ACCURACY);
406         goto err;
407     }
408     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
409         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
410         if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
411             if (val->value)
412                 secs = atoi(val->value);
413         } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
414             if (val->value)
415                 millis = atoi(val->value);
416         } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
417             if (val->value)
418                 micros = atoi(val->value);
419         } else {
420             ts_CONF_invalid(section, ENV_ACCURACY);
421             goto err;
422         }
423     }
424     if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
425         goto err;
426 
427     ret = 1;
428 err:
429     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
430     return ret;
431 }
432 
TS_CONF_set_clock_precision_digits(const CONF * conf,const char * section,TS_RESP_CTX * ctx)433 int TS_CONF_set_clock_precision_digits(const CONF *conf, const char *section,
434     TS_RESP_CTX *ctx)
435 {
436     int ret = 0;
437     long digits = 0;
438 
439     /*
440      * If not specified, set the default value to 0, i.e. sec precision
441      */
442     digits = _CONF_get_number(conf, section, ENV_CLOCK_PRECISION_DIGITS);
443     if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
444         ts_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
445         goto err;
446     }
447 
448     if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
449         goto err;
450 
451     return 1;
452 err:
453     return ret;
454 }
455 
ts_CONF_add_flag(CONF * conf,const char * section,const char * field,int flag,TS_RESP_CTX * ctx)456 static int ts_CONF_add_flag(CONF *conf, const char *section,
457     const char *field, int flag, TS_RESP_CTX *ctx)
458 {
459     const char *value = NCONF_get_string(conf, section, field);
460 
461     if (value) {
462         if (strcmp(value, ENV_VALUE_YES) == 0)
463             TS_RESP_CTX_add_flags(ctx, flag);
464         else if (strcmp(value, ENV_VALUE_NO) != 0) {
465             ts_CONF_invalid(section, field);
466             return 0;
467         }
468     }
469 
470     return 1;
471 }
472 
TS_CONF_set_ordering(CONF * conf,const char * section,TS_RESP_CTX * ctx)473 int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
474 {
475     return ts_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
476 }
477 
TS_CONF_set_tsa_name(CONF * conf,const char * section,TS_RESP_CTX * ctx)478 int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
479 {
480     return ts_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
481 }
482 
TS_CONF_set_ess_cert_id_chain(CONF * conf,const char * section,TS_RESP_CTX * ctx)483 int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
484     TS_RESP_CTX *ctx)
485 {
486     return ts_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
487         TS_ESS_CERT_ID_CHAIN, ctx);
488 }
489 
TS_CONF_set_ess_cert_id_digest(CONF * conf,const char * section,TS_RESP_CTX * ctx)490 int TS_CONF_set_ess_cert_id_digest(CONF *conf, const char *section,
491     TS_RESP_CTX *ctx)
492 {
493     int ret = 0;
494     const EVP_MD *cert_md = NULL;
495     const char *md = NCONF_get_string(conf, section, ENV_ESS_CERT_ID_ALG);
496 
497     if (md == NULL)
498         md = "sha256";
499 
500     cert_md = EVP_get_digestbyname(md);
501     if (cert_md == NULL) {
502         ts_CONF_invalid(section, ENV_ESS_CERT_ID_ALG);
503         goto err;
504     }
505 
506     if (!TS_RESP_CTX_set_ess_cert_id_digest(ctx, cert_md))
507         goto err;
508 
509     ret = 1;
510 err:
511     return ret;
512 }
513