In OpenSSL builds, declare STACK for datatypes ...
[openssl.git] / crypto / ts / ts_conf.c
1 /*
2  * Copyright 2006-2016 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 #include <string.h>
11
12 #include <openssl/crypto.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/pem.h>
15 #include <openssl/engine.h>
16 #include <openssl/ts.h>
17
18 DEFINE_STACK_OF(X509)
19 DEFINE_STACK_OF(X509_INFO)
20 DEFINE_STACK_OF(CONF_VALUE)
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
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         TSerr(TS_F_TS_CONF_LOAD_CERT, TS_R_CANNOT_LOAD_CERT);
59     BIO_free(cert);
60     return x;
61 }
62
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         if (xi->x509) {
79             sk_X509_push(othercerts, xi->x509);
80             xi->x509 = NULL;
81         }
82     }
83  end:
84     if (othercerts == NULL)
85         TSerr(TS_F_TS_CONF_LOAD_CERTS, TS_R_CANNOT_LOAD_CERT);
86     sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
87     BIO_free(certs);
88     return othercerts;
89 }
90
91 EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
92 {
93     BIO *key = NULL;
94     EVP_PKEY *pkey = NULL;
95
96     if ((key = BIO_new_file(file, "r")) == NULL)
97         goto end;
98     pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
99  end:
100     if (pkey == NULL)
101         TSerr(TS_F_TS_CONF_LOAD_KEY, TS_R_CANNOT_LOAD_KEY);
102     BIO_free(key);
103     return pkey;
104 }
105
106 /* Function definitions for handling configuration options. */
107
108 static void ts_CONF_lookup_fail(const char *name, const char *tag)
109 {
110     TSerr(TS_F_TS_CONF_LOOKUP_FAIL, TS_R_VAR_LOOKUP_FAILURE);
111     ERR_add_error_data(3, name, "::", tag);
112 }
113
114 static void ts_CONF_invalid(const char *name, const char *tag)
115 {
116     TSerr(TS_F_TS_CONF_INVALID, TS_R_VAR_BAD_VALUE);
117     ERR_add_error_data(3, name, "::", tag);
118 }
119
120 const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
121 {
122     if (!section) {
123         section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
124         if (!section)
125             ts_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
126     }
127     return section;
128 }
129
130 int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
131                        TS_RESP_CTX *ctx)
132 {
133     int ret = 0;
134     char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
135     if (!serial) {
136         ts_CONF_lookup_fail(section, ENV_SERIAL);
137         goto err;
138     }
139     TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
140
141     ret = 1;
142  err:
143     return ret;
144 }
145
146 #ifndef OPENSSL_NO_ENGINE
147
148 int TS_CONF_set_crypto_device(CONF *conf, const char *section,
149                               const char *device)
150 {
151     int ret = 0;
152
153     if (device == NULL)
154         device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
155
156     if (device && !TS_CONF_set_default_engine(device)) {
157         ts_CONF_invalid(section, ENV_CRYPTO_DEVICE);
158         goto err;
159     }
160     ret = 1;
161  err:
162     return ret;
163 }
164
165 int TS_CONF_set_default_engine(const char *name)
166 {
167     ENGINE *e = NULL;
168     int ret = 0;
169
170     if (strcmp(name, "builtin") == 0)
171         return 1;
172
173     if ((e = ENGINE_by_id(name)) == NULL)
174         goto err;
175     if (strcmp(name, "chil") == 0)
176         ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
177     if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
178         goto err;
179     ret = 1;
180
181  err:
182     if (!ret) {
183         TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
184         ERR_add_error_data(2, "engine:", name);
185     }
186     ENGINE_free(e);
187     return ret;
188 }
189
190 #endif
191
192 int TS_CONF_set_signer_cert(CONF *conf, const char *section,
193                             const char *cert, TS_RESP_CTX *ctx)
194 {
195     int ret = 0;
196     X509 *cert_obj = NULL;
197
198     if (cert == NULL) {
199         cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
200         if (cert == NULL) {
201             ts_CONF_lookup_fail(section, ENV_SIGNER_CERT);
202             goto err;
203         }
204     }
205     if ((cert_obj = TS_CONF_load_cert(cert)) == NULL)
206         goto err;
207     if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
208         goto err;
209
210     ret = 1;
211  err:
212     X509_free(cert_obj);
213     return ret;
214 }
215
216 int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
217                       TS_RESP_CTX *ctx)
218 {
219     int ret = 0;
220     STACK_OF(X509) *certs_obj = NULL;
221
222     if (certs == NULL) {
223         /* Certificate chain is optional. */
224         if ((certs = NCONF_get_string(conf, section, ENV_CERTS)) == NULL)
225             goto end;
226     }
227     if ((certs_obj = TS_CONF_load_certs(certs)) == NULL)
228         goto err;
229     if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
230         goto err;
231  end:
232     ret = 1;
233  err:
234     sk_X509_pop_free(certs_obj, X509_free);
235     return ret;
236 }
237
238 int TS_CONF_set_signer_key(CONF *conf, const char *section,
239                            const char *key, const char *pass,
240                            TS_RESP_CTX *ctx)
241 {
242     int ret = 0;
243     EVP_PKEY *key_obj = NULL;
244     if (!key)
245         key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
246     if (!key) {
247         ts_CONF_lookup_fail(section, ENV_SIGNER_KEY);
248         goto err;
249     }
250     if ((key_obj = TS_CONF_load_key(key, pass)) == NULL)
251         goto err;
252     if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
253         goto err;
254
255     ret = 1;
256  err:
257     EVP_PKEY_free(key_obj);
258     return ret;
259 }
260
261 int TS_CONF_set_signer_digest(CONF *conf, const char *section,
262                               const char *md, TS_RESP_CTX *ctx)
263 {
264     int ret = 0;
265     const EVP_MD *sign_md = NULL;
266     if (md == NULL)
267         md = NCONF_get_string(conf, section, ENV_SIGNER_DIGEST);
268     if (md == NULL) {
269         ts_CONF_lookup_fail(section, ENV_SIGNER_DIGEST);
270         goto err;
271     }
272     sign_md = EVP_get_digestbyname(md);
273     if (sign_md == NULL) {
274         ts_CONF_invalid(section, ENV_SIGNER_DIGEST);
275         goto err;
276     }
277     if (!TS_RESP_CTX_set_signer_digest(ctx, sign_md))
278         goto err;
279
280     ret = 1;
281  err:
282     return ret;
283 }
284
285 int TS_CONF_set_def_policy(CONF *conf, const char *section,
286                            const char *policy, TS_RESP_CTX *ctx)
287 {
288     int ret = 0;
289     ASN1_OBJECT *policy_obj = NULL;
290
291     if (policy == NULL)
292         policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
293     if (policy == NULL) {
294         ts_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
295         goto err;
296     }
297     if ((policy_obj = OBJ_txt2obj(policy, 0)) == NULL) {
298         ts_CONF_invalid(section, ENV_DEFAULT_POLICY);
299         goto err;
300     }
301     if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
302         goto err;
303
304     ret = 1;
305  err:
306     ASN1_OBJECT_free(policy_obj);
307     return ret;
308 }
309
310 int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
311 {
312     int ret = 0;
313     int i;
314     STACK_OF(CONF_VALUE) *list = NULL;
315     char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
316
317     /* If no other policy is specified, that's fine. */
318     if (policies && (list = X509V3_parse_list(policies)) == NULL) {
319         ts_CONF_invalid(section, ENV_OTHER_POLICIES);
320         goto err;
321     }
322     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
323         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
324         const char *extval = val->value ? val->value : val->name;
325         ASN1_OBJECT *objtmp;
326
327         if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
328             ts_CONF_invalid(section, ENV_OTHER_POLICIES);
329             goto err;
330         }
331         if (!TS_RESP_CTX_add_policy(ctx, objtmp))
332             goto err;
333         ASN1_OBJECT_free(objtmp);
334     }
335
336     ret = 1;
337  err:
338     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
339     return ret;
340 }
341
342 int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
343 {
344     int ret = 0;
345     int i;
346     STACK_OF(CONF_VALUE) *list = NULL;
347     char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
348
349     if (digests == NULL) {
350         ts_CONF_lookup_fail(section, ENV_DIGESTS);
351         goto err;
352     }
353     if ((list = X509V3_parse_list(digests)) == NULL) {
354         ts_CONF_invalid(section, ENV_DIGESTS);
355         goto err;
356     }
357     if (sk_CONF_VALUE_num(list) == 0) {
358         ts_CONF_invalid(section, ENV_DIGESTS);
359         goto err;
360     }
361     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
362         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
363         const char *extval = val->value ? val->value : val->name;
364         const EVP_MD *md;
365
366         if ((md = EVP_get_digestbyname(extval)) == NULL) {
367             ts_CONF_invalid(section, ENV_DIGESTS);
368             goto err;
369         }
370         if (!TS_RESP_CTX_add_md(ctx, md))
371             goto err;
372     }
373
374     ret = 1;
375  err:
376     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
377     return ret;
378 }
379
380 int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
381 {
382     int ret = 0;
383     int i;
384     int secs = 0, millis = 0, micros = 0;
385     STACK_OF(CONF_VALUE) *list = NULL;
386     char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
387
388     if (accuracy && (list = X509V3_parse_list(accuracy)) == NULL) {
389         ts_CONF_invalid(section, ENV_ACCURACY);
390         goto err;
391     }
392     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
393         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
394         if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
395             if (val->value)
396                 secs = atoi(val->value);
397         } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
398             if (val->value)
399                 millis = atoi(val->value);
400         } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
401             if (val->value)
402                 micros = atoi(val->value);
403         } else {
404             ts_CONF_invalid(section, ENV_ACCURACY);
405             goto err;
406         }
407     }
408     if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
409         goto err;
410
411     ret = 1;
412  err:
413     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
414     return ret;
415 }
416
417 int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
418                                        TS_RESP_CTX *ctx)
419 {
420     int ret = 0;
421     long digits = 0;
422
423     /*
424      * If not specified, set the default value to 0, i.e. sec precision
425      */
426     if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
427                             &digits))
428         digits = 0;
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
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
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
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
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
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 = "sha1";
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 }