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