e610691416343740cde6e2c6fdc168f60c2c098f
[openssl.git] / crypto / ts / ts_conf.c
1 /*
2  * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
3  * 2002.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <string.h>
60
61 #include <openssl/crypto.h>
62 #include "internal/cryptlib.h"
63 #include <openssl/pem.h>
64 #include <openssl/engine.h>
65 #include <openssl/ts.h>
66
67 /* Macro definitions for the configuration file. */
68 #define BASE_SECTION                    "tsa"
69 #define ENV_DEFAULT_TSA                 "default_tsa"
70 #define ENV_SERIAL                      "serial"
71 #define ENV_CRYPTO_DEVICE               "crypto_device"
72 #define ENV_SIGNER_CERT                 "signer_cert"
73 #define ENV_CERTS                       "certs"
74 #define ENV_SIGNER_KEY                  "signer_key"
75 #define ENV_SIGNER_DIGEST               "signer_digest"
76 #define ENV_DEFAULT_POLICY              "default_policy"
77 #define ENV_OTHER_POLICIES              "other_policies"
78 #define ENV_DIGESTS                     "digests"
79 #define ENV_ACCURACY                    "accuracy"
80 #define ENV_ORDERING                    "ordering"
81 #define ENV_TSA_NAME                    "tsa_name"
82 #define ENV_ESS_CERT_ID_CHAIN           "ess_cert_id_chain"
83 #define ENV_VALUE_SECS                  "secs"
84 #define ENV_VALUE_MILLISECS             "millisecs"
85 #define ENV_VALUE_MICROSECS             "microsecs"
86 #define ENV_CLOCK_PRECISION_DIGITS      "clock_precision_digits"
87 #define ENV_VALUE_YES                   "yes"
88 #define ENV_VALUE_NO                    "no"
89
90 /* Function definitions for certificate and key loading. */
91
92 X509 *TS_CONF_load_cert(const char *file)
93 {
94     BIO *cert = NULL;
95     X509 *x = NULL;
96
97     if ((cert = BIO_new_file(file, "r")) == NULL)
98         goto end;
99     x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
100  end:
101     if (x == NULL)
102         TSerr(TS_F_TS_CONF_LOAD_CERT, TS_R_CANNOT_LOAD_CERT);
103     BIO_free(cert);
104     return x;
105 }
106
107 STACK_OF(X509) *TS_CONF_load_certs(const char *file)
108 {
109     BIO *certs = NULL;
110     STACK_OF(X509) *othercerts = NULL;
111     STACK_OF(X509_INFO) *allcerts = NULL;
112     int i;
113
114     if ((certs = BIO_new_file(file, "r")) == NULL)
115         goto end;
116     if ((othercerts = sk_X509_new_null()) == NULL)
117         goto end;
118
119     allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
120     for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
121         X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
122         if (xi->x509) {
123             sk_X509_push(othercerts, xi->x509);
124             xi->x509 = NULL;
125         }
126     }
127  end:
128     if (othercerts == NULL)
129         TSerr(TS_F_TS_CONF_LOAD_CERTS, TS_R_CANNOT_LOAD_CERT);
130     sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
131     BIO_free(certs);
132     return othercerts;
133 }
134
135 EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
136 {
137     BIO *key = NULL;
138     EVP_PKEY *pkey = NULL;
139
140     if ((key = BIO_new_file(file, "r")) == NULL)
141         goto end;
142     pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
143  end:
144     if (pkey == NULL)
145         TSerr(TS_F_TS_CONF_LOAD_KEY, TS_R_CANNOT_LOAD_KEY);
146     BIO_free(key);
147     return pkey;
148 }
149
150 /* Function definitions for handling configuration options. */
151
152 static void ts_CONF_lookup_fail(const char *name, const char *tag)
153 {
154     TSerr(TS_F_TS_CONF_LOOKUP_FAIL, TS_R_VAR_LOOKUP_FAILURE);
155     ERR_add_error_data(3, name, "::", tag);
156 }
157
158 static void ts_CONF_invalid(const char *name, const char *tag)
159 {
160     TSerr(TS_F_TS_CONF_INVALID, TS_R_VAR_BAD_VALUE);
161     ERR_add_error_data(3, name, "::", tag);
162 }
163
164 const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
165 {
166     if (!section) {
167         section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
168         if (!section)
169             ts_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
170     }
171     return section;
172 }
173
174 int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
175                        TS_RESP_CTX *ctx)
176 {
177     int ret = 0;
178     char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
179     if (!serial) {
180         ts_CONF_lookup_fail(section, ENV_SERIAL);
181         goto err;
182     }
183     TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
184
185     ret = 1;
186  err:
187     return ret;
188 }
189
190 #ifndef OPENSSL_NO_ENGINE
191
192 int TS_CONF_set_crypto_device(CONF *conf, const char *section,
193                               const char *device)
194 {
195     int ret = 0;
196
197     if (device == NULL)
198         device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
199
200     if (device && !TS_CONF_set_default_engine(device)) {
201         ts_CONF_invalid(section, ENV_CRYPTO_DEVICE);
202         goto err;
203     }
204     ret = 1;
205  err:
206     return ret;
207 }
208
209 int TS_CONF_set_default_engine(const char *name)
210 {
211     ENGINE *e = NULL;
212     int ret = 0;
213
214     if (strcmp(name, "builtin") == 0)
215         return 1;
216
217     if ((e = ENGINE_by_id(name)) == NULL)
218         goto err;
219     if (strcmp(name, "chil") == 0)
220         ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
221     if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
222         goto err;
223     ret = 1;
224
225  err:
226     if (!ret) {
227         TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
228         ERR_add_error_data(2, "engine:", name);
229     }
230     ENGINE_free(e);
231     return ret;
232 }
233
234 #endif
235
236 int TS_CONF_set_signer_cert(CONF *conf, const char *section,
237                             const char *cert, TS_RESP_CTX *ctx)
238 {
239     int ret = 0;
240     X509 *cert_obj = NULL;
241
242     if (cert == NULL) {
243         cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
244         if (cert == NULL) {
245             ts_CONF_lookup_fail(section, ENV_SIGNER_CERT);
246             goto err;
247         }
248     }
249     if ((cert_obj = TS_CONF_load_cert(cert)) == NULL)
250         goto err;
251     if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
252         goto err;
253
254     ret = 1;
255  err:
256     X509_free(cert_obj);
257     return ret;
258 }
259
260 int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
261                       TS_RESP_CTX *ctx)
262 {
263     int ret = 0;
264     STACK_OF(X509) *certs_obj = NULL;
265
266     if (certs == NULL) {
267         /* Certificate chain is optional. */
268         if ((certs = NCONF_get_string(conf, section, ENV_CERTS)) == NULL)
269             goto end;
270     }
271     if ((certs_obj = TS_CONF_load_certs(certs)) == NULL)
272         goto err;
273     if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
274         goto err;
275  end:
276     ret = 1;
277  err:
278     sk_X509_pop_free(certs_obj, X509_free);
279     return ret;
280 }
281
282 int TS_CONF_set_signer_key(CONF *conf, const char *section,
283                            const char *key, const char *pass,
284                            TS_RESP_CTX *ctx)
285 {
286     int ret = 0;
287     EVP_PKEY *key_obj = NULL;
288     if (!key)
289         key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
290     if (!key) {
291         ts_CONF_lookup_fail(section, ENV_SIGNER_KEY);
292         goto err;
293     }
294     if ((key_obj = TS_CONF_load_key(key, pass)) == NULL)
295         goto err;
296     if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
297         goto err;
298
299     ret = 1;
300  err:
301     EVP_PKEY_free(key_obj);
302     return ret;
303 }
304
305 int TS_CONF_set_signer_digest(CONF *conf, const char *section,
306                               const char *md, TS_RESP_CTX *ctx)
307 {
308     int ret = 0;
309     const EVP_MD *sign_md = NULL;
310     if (md == NULL)
311         md = NCONF_get_string(conf, section, ENV_SIGNER_DIGEST);
312     if (md == NULL) {
313         ts_CONF_lookup_fail(section, ENV_SIGNER_DIGEST);
314         goto err;
315     }
316     sign_md = EVP_get_digestbyname(md);
317     if (sign_md == NULL) {
318         ts_CONF_invalid(section, ENV_SIGNER_DIGEST);
319         goto err;
320     }
321     if (!TS_RESP_CTX_set_signer_digest(ctx, sign_md))
322         goto err;
323
324     ret = 1;
325  err:
326     return ret;
327 }
328
329 int TS_CONF_set_def_policy(CONF *conf, const char *section,
330                            const char *policy, TS_RESP_CTX *ctx)
331 {
332     int ret = 0;
333     ASN1_OBJECT *policy_obj = NULL;
334     if (!policy)
335         policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
336     if (!policy) {
337         ts_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
338         goto err;
339     }
340     if ((policy_obj = OBJ_txt2obj(policy, 0)) == NULL) {
341         ts_CONF_invalid(section, ENV_DEFAULT_POLICY);
342         goto err;
343     }
344     if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
345         goto err;
346
347     ret = 1;
348  err:
349     ASN1_OBJECT_free(policy_obj);
350     return ret;
351 }
352
353 int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
354 {
355     int ret = 0;
356     int i;
357     STACK_OF(CONF_VALUE) *list = NULL;
358     char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
359
360     /* If no other policy is specified, that's fine. */
361     if (policies && (list = X509V3_parse_list(policies)) == NULL) {
362         ts_CONF_invalid(section, ENV_OTHER_POLICIES);
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         ASN1_OBJECT *objtmp;
369
370         if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
371             ts_CONF_invalid(section, ENV_OTHER_POLICIES);
372             goto err;
373         }
374         if (!TS_RESP_CTX_add_policy(ctx, objtmp))
375             goto err;
376         ASN1_OBJECT_free(objtmp);
377     }
378
379     ret = 1;
380  err:
381     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
382     return ret;
383 }
384
385 int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
386 {
387     int ret = 0;
388     int i;
389     STACK_OF(CONF_VALUE) *list = NULL;
390     char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
391
392     if (digests == NULL) {
393         ts_CONF_lookup_fail(section, ENV_DIGESTS);
394         goto err;
395     }
396     if ((list = X509V3_parse_list(digests)) == NULL) {
397         ts_CONF_invalid(section, ENV_DIGESTS);
398         goto err;
399     }
400     if (sk_CONF_VALUE_num(list) == 0) {
401         ts_CONF_invalid(section, ENV_DIGESTS);
402         goto err;
403     }
404     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
405         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
406         const char *extval = val->value ? val->value : val->name;
407         const EVP_MD *md;
408
409         if ((md = EVP_get_digestbyname(extval)) == NULL) {
410             ts_CONF_invalid(section, ENV_DIGESTS);
411             goto err;
412         }
413         if (!TS_RESP_CTX_add_md(ctx, md))
414             goto err;
415     }
416
417     ret = 1;
418  err:
419     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
420     return ret;
421 }
422
423 int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
424 {
425     int ret = 0;
426     int i;
427     int secs = 0, millis = 0, micros = 0;
428     STACK_OF(CONF_VALUE) *list = NULL;
429     char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
430
431     if (accuracy && (list = X509V3_parse_list(accuracy)) == NULL) {
432         ts_CONF_invalid(section, ENV_ACCURACY);
433         goto err;
434     }
435     for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
436         CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
437         if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
438             if (val->value)
439                 secs = atoi(val->value);
440         } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
441             if (val->value)
442                 millis = atoi(val->value);
443         } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
444             if (val->value)
445                 micros = atoi(val->value);
446         } else {
447             ts_CONF_invalid(section, ENV_ACCURACY);
448             goto err;
449         }
450     }
451     if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
452         goto err;
453
454     ret = 1;
455  err:
456     sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
457     return ret;
458 }
459
460 int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
461                                        TS_RESP_CTX *ctx)
462 {
463     int ret = 0;
464     long digits = 0;
465
466     /*
467      * If not specified, set the default value to 0, i.e. sec precision
468      */
469     if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
470                             &digits))
471         digits = 0;
472     if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
473         ts_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
474         goto err;
475     }
476
477     if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
478         goto err;
479
480     return 1;
481  err:
482     return ret;
483 }
484
485 static int ts_CONF_add_flag(CONF *conf, const char *section,
486                             const char *field, int flag, TS_RESP_CTX *ctx)
487 {
488     const char *value = NCONF_get_string(conf, section, field);
489
490     if (value) {
491         if (strcmp(value, ENV_VALUE_YES) == 0)
492             TS_RESP_CTX_add_flags(ctx, flag);
493         else if (strcmp(value, ENV_VALUE_NO) != 0) {
494             ts_CONF_invalid(section, field);
495             return 0;
496         }
497     }
498
499     return 1;
500 }
501
502 int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
503 {
504     return ts_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
505 }
506
507 int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
508 {
509     return ts_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
510 }
511
512 int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
513                                   TS_RESP_CTX *ctx)
514 {
515     return ts_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
516                             TS_ESS_CERT_ID_CHAIN, ctx);
517 }