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