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