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