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