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