]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ts.c
Deprecate OBJ_cleanup() and make it a no-op
[thirdparty/openssl.git] / apps / ts.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
3 * 2002.
c7235be6
UM
4 */
5/* ====================================================================
6 * Copyright (c) 2001 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
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
c7235be6
UM
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
f3852635 59#include <openssl/opensslconf.h>
7188f1f6
MC
60#ifdef OPENSSL_NO_TS
61NON_EMPTY_TRANSLATION_UNIT
62#else
f3852635
RL
63# include <stdio.h>
64# include <stdlib.h>
65# include <string.h>
66# include "apps.h"
67# include <openssl/bio.h>
68# include <openssl/err.h>
69# include <openssl/pem.h>
70# include <openssl/rand.h>
71# include <openssl/ts.h>
72# include <openssl/bn.h>
c7235be6 73
18cd23df 74/* Request nonce length, in bits (must be a multiple of 8). */
f3852635 75# define NONCE_LENGTH 64
c7235be6 76
18cd23df 77/* Name of config entry that defines the OID file. */
f3852635 78# define ENV_OID_FILE "oid_file"
c7235be6 79
18cd23df 80/* Is |EXACTLY_ONE| of three pointers set? */
f3852635 81# define EXACTLY_ONE(a, b, c) \
18cd23df
RS
82 (( a && !b && !c) || \
83 ( b && !a && !c) || \
84 ( c && !a && !b))
c7235be6
UM
85
86static ASN1_OBJECT *txt2obj(const char *oid);
87static CONF *load_config_file(const char *configfile);
88
89/* Query related functions. */
90static int query_command(const char *data, char *digest,
0f113f3e
MC
91 const EVP_MD *md, const char *policy, int no_nonce,
92 int cert, const char *in, const char *out, int text);
c7235be6 93static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
0f113f3e 94 const char *policy, int no_nonce, int cert);
c7235be6 95static int create_digest(BIO *input, char *digest,
0f113f3e 96 const EVP_MD *md, unsigned char **md_value);
c7235be6
UM
97static ASN1_INTEGER *create_nonce(int bits);
98
99/* Reply related functions. */
0f113f3e
MC
100static int reply_command(CONF *conf, char *section, char *engine,
101 char *queryfile, char *passin, char *inkey,
e20b4727
DSH
102 const EVP_MD *md, char *signer, char *chain,
103 const char *policy, char *in, int token_in,
104 char *out, int token_out, int text);
c7235be6
UM
105static TS_RESP *read_PKCS7(BIO *in_bio);
106static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
7e1b7485 107 char *queryfile, char *passin,
e20b4727
DSH
108 char *inkey, const EVP_MD *md, char *signer,
109 char *chain, const char *policy);
0f113f3e 110static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data);
c7235be6
UM
111static ASN1_INTEGER *next_serial(const char *serialfile);
112static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
113
114/* Verify related functions. */
115static int verify_command(char *data, char *digest, char *queryfile,
0f113f3e 116 char *in, int token_in,
08538fc0 117 char *CApath, char *CAfile, char *untrusted,
118 X509_VERIFY_PARAM *vpm);
0f113f3e
MC
119static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
120 char *queryfile,
7e1b7485 121 char *CApath, char *CAfile,
08538fc0 122 char *untrusted,
123 X509_VERIFY_PARAM *vpm);
124static X509_STORE *create_cert_store(char *CApath, char *CAfile,
125 X509_VERIFY_PARAM *vpm);
6d23cf97 126static int verify_cb(int ok, X509_STORE_CTX *ctx);
c7235be6 127
7e1b7485
RS
128typedef enum OPTION_choice {
129 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
130 OPT_ENGINE, OPT_CONFIG, OPT_SECTION, OPT_QUERY, OPT_DATA,
08538fc0 131 OPT_DIGEST, OPT_RAND, OPT_TSPOLICY, OPT_NO_NONCE, OPT_CERT,
7e1b7485
RS
132 OPT_IN, OPT_TOKEN_IN, OPT_OUT, OPT_TOKEN_OUT, OPT_TEXT,
133 OPT_REPLY, OPT_QUERYFILE, OPT_PASSIN, OPT_INKEY, OPT_SIGNER,
134 OPT_CHAIN, OPT_VERIFY, OPT_CAPATH, OPT_CAFILE, OPT_UNTRUSTED,
08538fc0 135 OPT_MD, OPT_V_ENUM
7e1b7485
RS
136} OPTION_CHOICE;
137
138OPTIONS ts_options[] = {
139 {"help", OPT_HELP, '-', "Display this summary"},
140 {"config", OPT_CONFIG, '<', "Configuration file"},
141 {"section", OPT_SECTION, 's', "Section to use within config file"},
142 {"query", OPT_QUERY, '-', "Generate a TS query"},
143 {"data", OPT_DATA, '<', "File to hash"},
144 {"digest", OPT_DIGEST, 's', "Digest (as a hex string)"},
145 {"rand", OPT_RAND, 's',
146 "Load the file(s) into the random number generator"},
08538fc0 147 {"tspolicy", OPT_TSPOLICY, 's', "Policy OID to use"},
7e1b7485
RS
148 {"no_nonce", OPT_NO_NONCE, '-', "Do not include a nonce"},
149 {"cert", OPT_CERT, '-', "Put cert request into query"},
150 {"in", OPT_IN, '<', "Input file"},
151 {"token_in", OPT_TOKEN_IN, '-', "Input is a PKCS#7 file"},
152 {"out", OPT_OUT, '>', "Output file"},
153 {"token_out", OPT_TOKEN_OUT, '-', "Output is a PKCS#7 file"},
154 {"text", OPT_TEXT, '-', "Output text (not DER)"},
155 {"reply", OPT_REPLY, '-', "Generate a TS reply"},
156 {"queryfile", OPT_QUERYFILE, '<', "File containing a TS query"},
157 {"passin", OPT_PASSIN, 's'},
158 {"inkey", OPT_INKEY, '<', "File with private key for reply"},
159 {"signer", OPT_SIGNER, 's'},
160 {"chain", OPT_CHAIN, '<', "File with signer CA chain"},
161 {"verify", OPT_VERIFY, '-', "Verify a TS response"},
162 {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"},
163 {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"},
164 {"untrusted", OPT_UNTRUSTED, '<', "File with untrusted certs"},
9c3bcfa0 165 {"", OPT_MD, '-', "Any supported digest"},
f3852635 166# ifndef OPENSSL_NO_ENGINE
7e1b7485 167 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
f3852635 168# endif
08538fc0 169 {OPT_HELP_STR, 1, '-', "\nOptions specific to 'ts -verify': \n"},
170 OPT_V_OPTIONS,
171 {OPT_HELP_STR, 1, '-', "\n"},
7e1b7485
RS
172 {NULL}
173};
c7235be6 174
7e1b7485
RS
175/*
176 * This comand is so complex, special help is needed.
177 */
178static char* opt_helplist[] = {
179 "Typical uses:",
180 "ts -query [-rand file...] [-config file] [-data file]",
08538fc0 181 " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]",
7e1b7485
RS
182 " [-in file] [-out file] [-text]",
183 " or",
184 "ts -reply [-config file] [-section tsa_section]",
185 " [-queryfile file] [-passin password]",
186 " [-signer tsa_cert.pem] [-inkey private_key.pem]",
08538fc0 187 " [-chain certs_file.pem] [-tspolicy oid]",
7e1b7485 188 " [-in file] [-token_in] [-out file] [-token_out]",
f3852635 189# ifndef OPENSSL_NO_ENGINE
7e1b7485 190 " [-text]",
f3852635 191# else
7e1b7485 192 " [-text] [-engine id]",
f3852635 193# endif
7e1b7485
RS
194 " or",
195 "ts -verify -CApath dir -CAfile file.pem -untrusted file.pem",
196 " [-data file] [-digest hexstring]",
197 " [-queryfile file] -in file [-token_in]",
08538fc0 198 " [[options specific to 'ts -verify']]",
7e1b7485
RS
199 NULL,
200};
201
202int ts_main(int argc, char **argv)
0f113f3e 203{
0f113f3e 204 CONF *conf = NULL;
7e1b7485 205 char *CAfile = NULL, *untrusted = NULL, *engine = NULL, *prog, **helpp;
cc01d217
RS
206 char *configfile = default_config_file;
207 char *section = NULL, *password = NULL;
7e1b7485
RS
208 char *data = NULL, *digest = NULL, *rnd = NULL, *policy = NULL;
209 char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL;
210 char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL;
0f113f3e 211 const EVP_MD *md = NULL;
7e1b7485
RS
212 OPTION_CHOICE o, mode = OPT_ERR;
213 int ret = 1, no_nonce = 0, cert = 0, text = 0;
08538fc0 214 int vpmtouched = 0;
215 X509_VERIFY_PARAM *vpm = NULL;
0f113f3e
MC
216 /* Input is ContentInfo instead of TimeStampResp. */
217 int token_in = 0;
218 /* Output is ContentInfo instead of TimeStampResp. */
219 int token_out = 0;
0f113f3e 220
08538fc0 221 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
222 goto end;
223
7e1b7485
RS
224 prog = opt_init(argc, argv, ts_options);
225 while ((o = opt_next()) != OPT_EOF) {
226 switch (o) {
227 case OPT_EOF:
228 case OPT_ERR:
229 opthelp:
230 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
231 goto end;
232 case OPT_HELP:
233 opt_help(ts_options);
234 for (helpp = opt_helplist; *helpp; ++helpp)
235 BIO_printf(bio_err, "%s\n", *helpp);
236 ret = 0;
237 goto end;
238 case OPT_CONFIG:
239 configfile = opt_arg();
240 break;
241 case OPT_SECTION:
242 section = opt_arg();
243 break;
244 case OPT_QUERY:
245 case OPT_REPLY:
246 case OPT_VERIFY:
247 if (mode != OPT_ERR)
248 goto opthelp;
249 mode = o;
250 break;
251 case OPT_DATA:
252 data = opt_arg();
253 break;
254 case OPT_DIGEST:
255 digest = opt_arg();
256 break;
257 case OPT_RAND:
258 rnd = opt_arg();
259 break;
08538fc0 260 case OPT_TSPOLICY:
7e1b7485
RS
261 policy = opt_arg();
262 break;
263 case OPT_NO_NONCE:
0f113f3e 264 no_nonce = 1;
7e1b7485
RS
265 break;
266 case OPT_CERT:
0f113f3e 267 cert = 1;
7e1b7485
RS
268 break;
269 case OPT_IN:
270 in = opt_arg();
271 break;
272 case OPT_TOKEN_IN:
0f113f3e 273 token_in = 1;
7e1b7485
RS
274 break;
275 case OPT_OUT:
276 out = opt_arg();
277 break;
278 case OPT_TOKEN_OUT:
0f113f3e 279 token_out = 1;
7e1b7485
RS
280 break;
281 case OPT_TEXT:
0f113f3e 282 text = 1;
7e1b7485
RS
283 break;
284 case OPT_QUERYFILE:
285 queryfile = opt_arg();
286 break;
287 case OPT_PASSIN:
288 passin = opt_arg();
289 break;
290 case OPT_INKEY:
291 inkey = opt_arg();
292 break;
293 case OPT_SIGNER:
294 signer = opt_arg();
295 break;
296 case OPT_CHAIN:
297 chain = opt_arg();
298 break;
299 case OPT_CAPATH:
300 CApath = opt_arg();
301 break;
302 case OPT_CAFILE:
303 CAfile = opt_arg();
304 break;
305 case OPT_UNTRUSTED:
306 untrusted = opt_arg();
307 break;
308 case OPT_ENGINE:
309 engine = opt_arg();
310 break;
311 case OPT_MD:
312 if (!opt_md(opt_unknown(), &md))
313 goto opthelp;
314 break;
08538fc0 315 case OPT_V_CASES:
316 if (!opt_verify(o, vpm))
317 goto end;
318 vpmtouched++;
319 break;
7e1b7485 320 }
0f113f3e 321 }
7e1b7485 322 argc = opt_num_rest();
08538fc0 323 argv = opt_rest();
7e1b7485
RS
324 if (mode == OPT_ERR || argc != 0)
325 goto opthelp;
0f113f3e
MC
326
327 /* Seed the random number generator if it is going to be used. */
7e1b7485
RS
328 if (mode == OPT_QUERY && !no_nonce) {
329 if (!app_RAND_load_file(NULL, 1) && rnd == NULL)
0f113f3e
MC
330 BIO_printf(bio_err, "warning, not much extra random "
331 "data, consider using the -rand option\n");
332 if (rnd != NULL)
333 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
334 app_RAND_load_files(rnd));
335 }
336
7e1b7485
RS
337 if (mode == OPT_REPLY && passin &&
338 !app_passwd(passin, NULL, &password, NULL)) {
0f113f3e 339 BIO_printf(bio_err, "Error getting password.\n");
7e1b7485 340 goto end;
0f113f3e
MC
341 }
342
296f54ee
RL
343 conf = load_config_file(configfile);
344 if (!app_load_modules(conf))
345 goto end;
346
18cd23df 347 /* Check parameter consistency and execute the appropriate function. */
0f113f3e 348 switch (mode) {
7e1b7485
RS
349 default:
350 case OPT_ERR:
351 goto opthelp;
352 case OPT_QUERY:
08538fc0 353 if (vpmtouched)
354 goto opthelp;
18cd23df 355 if ((data != NULL) && (digest != NULL))
7e1b7485 356 goto opthelp;
0f113f3e
MC
357 ret = !query_command(data, digest, md, policy, no_nonce, cert,
358 in, out, text);
359 break;
7e1b7485 360 case OPT_REPLY:
08538fc0 361 if (vpmtouched)
362 goto opthelp;
18cd23df
RS
363 if ((in != NULL) && (queryfile != NULL))
364 goto opthelp;
0f113f3e 365 if (in == NULL) {
18cd23df 366 if ((conf == NULL) || (token_in != 0))
7e1b7485 367 goto opthelp;
0f113f3e 368 }
0f113f3e 369 ret = !reply_command(conf, section, engine, queryfile,
e20b4727 370 password, inkey, md, signer, chain, policy,
0f113f3e
MC
371 in, token_in, out, token_out, text);
372 break;
7e1b7485 373 case OPT_VERIFY:
18cd23df 374 if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
7e1b7485 375 goto opthelp;
0f113f3e 376 ret = !verify_command(data, digest, queryfile, in, token_in,
08538fc0 377 CApath, CAfile, untrusted,
378 vpmtouched ? vpm : NULL);
0f113f3e
MC
379 }
380
7e1b7485 381 end:
08538fc0 382 X509_VERIFY_PARAM_free(vpm);
7e1b7485 383 app_RAND_write_file(NULL);
0f113f3e
MC
384 NCONF_free(conf);
385 OPENSSL_free(password);
7e1b7485 386 return (ret);
0f113f3e 387}
c7235be6
UM
388
389/*
390 * Configuration file-related function definitions.
391 */
392
393static ASN1_OBJECT *txt2obj(const char *oid)
0f113f3e
MC
394{
395 ASN1_OBJECT *oid_obj = NULL;
c7235be6 396
75ebbd9a 397 if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
0f113f3e 398 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
c7235be6 399
0f113f3e
MC
400 return oid_obj;
401}
c7235be6
UM
402
403static CONF *load_config_file(const char *configfile)
0f113f3e 404{
cc01d217 405 CONF *conf = app_load_config(configfile);
0f113f3e
MC
406
407 if (conf != NULL) {
408 const char *p;
409
410 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
411 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
412 if (p != NULL) {
413 BIO *oid_bio = BIO_new_file(p, "r");
414 if (!oid_bio)
415 ERR_print_errors(bio_err);
416 else {
417 OBJ_create_objects(oid_bio);
418 BIO_free_all(oid_bio);
419 }
420 } else
421 ERR_clear_error();
7e1b7485 422 if (!add_oid_section(conf))
0f113f3e
MC
423 ERR_print_errors(bio_err);
424 }
425 return conf;
426}
c7235be6
UM
427
428/*
429 * Query-related method definitions.
430 */
c7235be6 431static int query_command(const char *data, char *digest, const EVP_MD *md,
0f113f3e
MC
432 const char *policy, int no_nonce,
433 int cert, const char *in, const char *out, int text)
434{
435 int ret = 0;
436 TS_REQ *query = NULL;
437 BIO *in_bio = NULL;
438 BIO *data_bio = NULL;
439 BIO *out_bio = NULL;
440
18cd23df 441 /* Build query object. */
0f113f3e 442 if (in != NULL) {
bdd58d98 443 if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
0f113f3e
MC
444 goto end;
445 query = d2i_TS_REQ_bio(in_bio, NULL);
446 } else {
75ebbd9a 447 if (digest == NULL
bdd58d98 448 && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
0f113f3e 449 goto end;
0f113f3e 450 query = create_query(data_bio, digest, md, policy, no_nonce, cert);
0f113f3e
MC
451 }
452 if (query == NULL)
453 goto end;
454
0f113f3e 455 if (text) {
bdd58d98
RL
456 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
457 goto end;
0f113f3e
MC
458 if (!TS_REQ_print_bio(out_bio, query))
459 goto end;
460 } else {
bdd58d98
RL
461 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
462 goto end;
0f113f3e
MC
463 if (!i2d_TS_REQ_bio(out_bio, query))
464 goto end;
465 }
466
467 ret = 1;
c7235be6
UM
468
469 end:
0f113f3e 470 ERR_print_errors(bio_err);
0f113f3e
MC
471 BIO_free_all(in_bio);
472 BIO_free_all(data_bio);
473 BIO_free_all(out_bio);
474 TS_REQ_free(query);
0f113f3e
MC
475 return ret;
476}
c7235be6 477
c7235be6 478static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
0f113f3e
MC
479 const char *policy, int no_nonce, int cert)
480{
481 int ret = 0;
482 TS_REQ *ts_req = NULL;
483 int len;
484 TS_MSG_IMPRINT *msg_imprint = NULL;
485 X509_ALGOR *algo = NULL;
486 unsigned char *data = NULL;
487 ASN1_OBJECT *policy_obj = NULL;
488 ASN1_INTEGER *nonce_asn1 = NULL;
489
75ebbd9a 490 if (md == NULL && (md = EVP_get_digestbyname("sha1")) == NULL)
0f113f3e 491 goto err;
75ebbd9a 492 if ((ts_req = TS_REQ_new()) == NULL)
0f113f3e 493 goto err;
0f113f3e
MC
494 if (!TS_REQ_set_version(ts_req, 1))
495 goto err;
75ebbd9a 496 if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
0f113f3e 497 goto err;
75ebbd9a 498 if ((algo = X509_ALGOR_new()) == NULL)
0f113f3e 499 goto err;
75ebbd9a 500 if ((algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))) == NULL)
0f113f3e 501 goto err;
75ebbd9a 502 if ((algo->parameter = ASN1_TYPE_new()) == NULL)
0f113f3e
MC
503 goto err;
504 algo->parameter->type = V_ASN1_NULL;
505 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
506 goto err;
0f113f3e
MC
507 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
508 goto err;
509 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
510 goto err;
0f113f3e
MC
511 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
512 goto err;
75ebbd9a 513 if (policy && (policy_obj = txt2obj(policy)) == NULL)
0f113f3e
MC
514 goto err;
515 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
516 goto err;
517
518 /* Setting nonce if requested. */
75ebbd9a 519 if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
0f113f3e
MC
520 goto err;
521 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
522 goto err;
0f113f3e
MC
523 if (!TS_REQ_set_cert_req(ts_req, cert))
524 goto err;
525
526 ret = 1;
c7235be6 527 err:
0f113f3e
MC
528 if (!ret) {
529 TS_REQ_free(ts_req);
530 ts_req = NULL;
531 BIO_printf(bio_err, "could not create query\n");
18cd23df 532 ERR_print_errors(bio_err);
0f113f3e
MC
533 }
534 TS_MSG_IMPRINT_free(msg_imprint);
535 X509_ALGOR_free(algo);
536 OPENSSL_free(data);
537 ASN1_OBJECT_free(policy_obj);
538 ASN1_INTEGER_free(nonce_asn1);
539 return ts_req;
540}
c7235be6
UM
541
542static int create_digest(BIO *input, char *digest, const EVP_MD *md,
0f113f3e
MC
543 unsigned char **md_value)
544{
545 int md_value_len;
546
547 md_value_len = EVP_MD_size(md);
548 if (md_value_len < 0)
18cd23df
RS
549 return 0;
550
0f113f3e 551 if (input) {
bfb0641f 552 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
0f113f3e
MC
553 unsigned char buffer[4096];
554 int length;
555
6e59a892
RL
556 if (md_ctx == NULL)
557 return 0;
68dc6824 558 *md_value = app_malloc(md_value_len, "digest buffer");
6e59a892 559 EVP_DigestInit(md_ctx, md);
0f113f3e 560 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
6e59a892 561 EVP_DigestUpdate(md_ctx, buffer, length);
0f113f3e 562 }
6e59a892 563 if (!EVP_DigestFinal(md_ctx, *md_value, NULL)) {
bfb0641f 564 EVP_MD_CTX_free(md_ctx);
0f113f3e 565 return 0;
6e59a892 566 }
bfb0641f 567 EVP_MD_CTX_free(md_ctx);
0f113f3e 568 } else {
0f113f3e
MC
569 long digest_len;
570 *md_value = string_to_hex(digest, &digest_len);
571 if (!*md_value || md_value_len != digest_len) {
572 OPENSSL_free(*md_value);
573 *md_value = NULL;
574 BIO_printf(bio_err, "bad digest, %d bytes "
575 "must be specified\n", md_value_len);
18cd23df 576 return 0;
0f113f3e
MC
577 }
578 }
0f113f3e 579 return md_value_len;
0f113f3e 580}
c7235be6
UM
581
582static ASN1_INTEGER *create_nonce(int bits)
0f113f3e
MC
583{
584 unsigned char buf[20];
585 ASN1_INTEGER *nonce = NULL;
586 int len = (bits - 1) / 8 + 1;
587 int i;
588
0f113f3e
MC
589 if (len > (int)sizeof(buf))
590 goto err;
591 if (RAND_bytes(buf, len) <= 0)
592 goto err;
593
594 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
75ebbd9a
RS
595 for (i = 0; i < len && !buf[i]; ++i)
596 continue;
597 if ((nonce = ASN1_INTEGER_new()) == NULL)
0f113f3e
MC
598 goto err;
599 OPENSSL_free(nonce->data);
0f113f3e 600 nonce->length = len - i;
68dc6824 601 nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
0f113f3e 602 memcpy(nonce->data, buf + i, nonce->length);
0f113f3e 603 return nonce;
18cd23df 604
c7235be6 605 err:
0f113f3e
MC
606 BIO_printf(bio_err, "could not create nonce\n");
607 ASN1_INTEGER_free(nonce);
608 return NULL;
609}
610
c7235be6
UM
611/*
612 * Reply-related method definitions.
613 */
614
0f113f3e
MC
615static int reply_command(CONF *conf, char *section, char *engine,
616 char *queryfile, char *passin, char *inkey,
e20b4727
DSH
617 const EVP_MD *md, char *signer, char *chain,
618 const char *policy, char *in, int token_in,
0f113f3e
MC
619 char *out, int token_out, int text)
620{
621 int ret = 0;
622 TS_RESP *response = NULL;
623 BIO *in_bio = NULL;
624 BIO *query_bio = NULL;
625 BIO *inkey_bio = NULL;
626 BIO *signer_bio = NULL;
627 BIO *out_bio = NULL;
628
0f113f3e
MC
629 if (in != NULL) {
630 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
631 goto end;
632 if (token_in) {
0f113f3e
MC
633 response = read_PKCS7(in_bio);
634 } else {
0f113f3e
MC
635 response = d2i_TS_RESP_bio(in_bio, NULL);
636 }
637 } else {
638 response = create_response(conf, section, engine, queryfile,
e20b4727 639 passin, inkey, md, signer, chain, policy);
0f113f3e
MC
640 if (response)
641 BIO_printf(bio_err, "Response has been generated.\n");
642 else
643 BIO_printf(bio_err, "Response is not generated.\n");
644 }
645 if (response == NULL)
646 goto end;
647
18cd23df 648 /* Write response. */
0f113f3e 649 if (text) {
bdd58d98
RL
650 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
651 goto end;
0f113f3e
MC
652 if (token_out) {
653 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
654 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
655 goto end;
656 } else {
657 if (!TS_RESP_print_bio(out_bio, response))
658 goto end;
659 }
660 } else {
bdd58d98
RL
661 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
662 goto end;
0f113f3e
MC
663 if (token_out) {
664 PKCS7 *token = TS_RESP_get_token(response);
665 if (!i2d_PKCS7_bio(out_bio, token))
666 goto end;
667 } else {
668 if (!i2d_TS_RESP_bio(out_bio, response))
669 goto end;
670 }
671 }
672
673 ret = 1;
c7235be6
UM
674
675 end:
0f113f3e 676 ERR_print_errors(bio_err);
0f113f3e
MC
677 BIO_free_all(in_bio);
678 BIO_free_all(query_bio);
679 BIO_free_all(inkey_bio);
680 BIO_free_all(signer_bio);
681 BIO_free_all(out_bio);
682 TS_RESP_free(response);
0f113f3e
MC
683 return ret;
684}
c7235be6
UM
685
686/* Reads a PKCS7 token and adds default 'granted' status info to it. */
687static TS_RESP *read_PKCS7(BIO *in_bio)
0f113f3e
MC
688{
689 int ret = 0;
690 PKCS7 *token = NULL;
691 TS_TST_INFO *tst_info = NULL;
692 TS_RESP *resp = NULL;
693 TS_STATUS_INFO *si = NULL;
694
75ebbd9a 695 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
0f113f3e 696 goto end;
75ebbd9a 697 if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
0f113f3e 698 goto end;
75ebbd9a 699 if ((resp = TS_RESP_new()) == NULL)
0f113f3e 700 goto end;
75ebbd9a 701 if ((si = TS_STATUS_INFO_new()) == NULL)
0f113f3e 702 goto end;
ca4a494c 703 if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
0f113f3e
MC
704 goto end;
705 if (!TS_RESP_set_status_info(resp, si))
706 goto end;
0f113f3e
MC
707 TS_RESP_set_tst_info(resp, token, tst_info);
708 token = NULL; /* Ownership is lost. */
709 tst_info = NULL; /* Ownership is lost. */
0f113f3e 710 ret = 1;
18cd23df 711
c7235be6 712 end:
0f113f3e
MC
713 PKCS7_free(token);
714 TS_TST_INFO_free(tst_info);
715 if (!ret) {
716 TS_RESP_free(resp);
717 resp = NULL;
718 }
719 TS_STATUS_INFO_free(si);
720 return resp;
721}
722
723static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
7e1b7485 724 char *queryfile, char *passin,
e20b4727
DSH
725 char *inkey, const EVP_MD *md, char *signer,
726 char *chain, const char *policy)
0f113f3e
MC
727{
728 int ret = 0;
729 TS_RESP *response = NULL;
730 BIO *query_bio = NULL;
731 TS_RESP_CTX *resp_ctx = NULL;
732
75ebbd9a 733 if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
0f113f3e 734 goto end;
75ebbd9a 735 if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
0f113f3e 736 goto end;
75ebbd9a 737 if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
0f113f3e 738 goto end;
0f113f3e
MC
739 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
740 goto end;
f3852635 741# ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
742 if (!TS_CONF_set_crypto_device(conf, section, engine))
743 goto end;
f3852635 744# endif
0f113f3e
MC
745 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
746 goto end;
0f113f3e
MC
747 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
748 goto end;
0f113f3e
MC
749 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
750 goto end;
e20b4727
DSH
751
752 if (md) {
753 if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
754 goto end;
755 } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
756 goto end;
757 }
758
0f113f3e
MC
759 if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
760 goto end;
0f113f3e
MC
761 if (!TS_CONF_set_policies(conf, section, resp_ctx))
762 goto end;
0f113f3e
MC
763 if (!TS_CONF_set_digests(conf, section, resp_ctx))
764 goto end;
0f113f3e
MC
765 if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
766 goto end;
0f113f3e
MC
767 if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
768 goto end;
0f113f3e
MC
769 if (!TS_CONF_set_ordering(conf, section, resp_ctx))
770 goto end;
0f113f3e
MC
771 if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
772 goto end;
0f113f3e
MC
773 if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
774 goto end;
75ebbd9a 775 if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
0f113f3e 776 goto end;
0f113f3e 777 ret = 1;
18cd23df 778
c7235be6 779 end:
0f113f3e
MC
780 if (!ret) {
781 TS_RESP_free(response);
782 response = NULL;
783 }
784 TS_RESP_CTX_free(resp_ctx);
785 BIO_free_all(query_bio);
0f113f3e
MC
786 return response;
787}
788
789static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
790{
791 const char *serial_file = (const char *)data;
792 ASN1_INTEGER *serial = next_serial(serial_file);
793
794 if (!serial) {
795 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
796 "Error during serial number "
797 "generation.");
798 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
799 } else
800 save_ts_serial(serial_file, serial);
801
802 return serial;
803}
c7235be6
UM
804
805static ASN1_INTEGER *next_serial(const char *serialfile)
0f113f3e
MC
806{
807 int ret = 0;
808 BIO *in = NULL;
809 ASN1_INTEGER *serial = NULL;
810 BIGNUM *bn = NULL;
811
75ebbd9a 812 if ((serial = ASN1_INTEGER_new()) == NULL)
0f113f3e
MC
813 goto err;
814
75ebbd9a 815 if ((in = BIO_new_file(serialfile, "r")) == NULL) {
0f113f3e
MC
816 ERR_clear_error();
817 BIO_printf(bio_err, "Warning: could not open file %s for "
818 "reading, using serial number: 1\n", serialfile);
819 if (!ASN1_INTEGER_set(serial, 1))
820 goto err;
821 } else {
822 char buf[1024];
823 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
824 BIO_printf(bio_err, "unable to load number from %s\n",
825 serialfile);
826 goto err;
827 }
75ebbd9a 828 if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
0f113f3e
MC
829 goto err;
830 ASN1_INTEGER_free(serial);
831 serial = NULL;
832 if (!BN_add_word(bn, 1))
833 goto err;
75ebbd9a 834 if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
0f113f3e
MC
835 goto err;
836 }
837 ret = 1;
18cd23df 838
c7235be6 839 err:
0f113f3e
MC
840 if (!ret) {
841 ASN1_INTEGER_free(serial);
842 serial = NULL;
843 }
844 BIO_free_all(in);
845 BN_free(bn);
846 return serial;
847}
c7235be6
UM
848
849static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
0f113f3e
MC
850{
851 int ret = 0;
852 BIO *out = NULL;
853
75ebbd9a 854 if ((out = BIO_new_file(serialfile, "w")) == NULL)
0f113f3e
MC
855 goto err;
856 if (i2a_ASN1_INTEGER(out, serial) <= 0)
857 goto err;
858 if (BIO_puts(out, "\n") <= 0)
859 goto err;
860 ret = 1;
c7235be6 861 err:
0f113f3e
MC
862 if (!ret)
863 BIO_printf(bio_err, "could not save serial number to %s\n",
864 serialfile);
865 BIO_free_all(out);
866 return ret;
867}
c7235be6 868
18cd23df 869
c7235be6
UM
870/*
871 * Verify-related method definitions.
872 */
873
874static int verify_command(char *data, char *digest, char *queryfile,
0f113f3e 875 char *in, int token_in,
08538fc0 876 char *CApath, char *CAfile, char *untrusted,
877 X509_VERIFY_PARAM *vpm)
0f113f3e
MC
878{
879 BIO *in_bio = NULL;
880 PKCS7 *token = NULL;
881 TS_RESP *response = NULL;
882 TS_VERIFY_CTX *verify_ctx = NULL;
883 int ret = 0;
884
75ebbd9a 885 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
0f113f3e
MC
886 goto end;
887 if (token_in) {
75ebbd9a 888 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
0f113f3e
MC
889 goto end;
890 } else {
75ebbd9a 891 if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
0f113f3e
MC
892 goto end;
893 }
894
75ebbd9a 895 if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
08538fc0 896 CApath, CAfile, untrusted,
897 vpm)) == NULL)
0f113f3e
MC
898 goto end;
899
18cd23df
RS
900 ret = token_in
901 ? TS_RESP_verify_token(verify_ctx, token)
902 : TS_RESP_verify_response(verify_ctx, response);
c7235be6
UM
903
904 end:
0f113f3e
MC
905 printf("Verification: ");
906 if (ret)
907 printf("OK\n");
908 else {
909 printf("FAILED\n");
0f113f3e
MC
910 ERR_print_errors(bio_err);
911 }
912
0f113f3e
MC
913 BIO_free_all(in_bio);
914 PKCS7_free(token);
915 TS_RESP_free(response);
916 TS_VERIFY_CTX_free(verify_ctx);
917 return ret;
918}
919
920static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
921 char *queryfile,
7e1b7485 922 char *CApath, char *CAfile,
08538fc0 923 char *untrusted,
924 X509_VERIFY_PARAM *vpm)
0f113f3e
MC
925{
926 TS_VERIFY_CTX *ctx = NULL;
927 BIO *input = NULL;
928 TS_REQ *request = NULL;
929 int ret = 0;
ca4a494c 930 int f = 0;
0f113f3e
MC
931
932 if (data != NULL || digest != NULL) {
75ebbd9a 933 if ((ctx = TS_VERIFY_CTX_new()) == NULL)
0f113f3e 934 goto err;
ca4a494c 935 f = TS_VFY_VERSION | TS_VFY_SIGNER;
0f113f3e 936 if (data != NULL) {
ca4a494c
RS
937 f |= TS_VFY_DATA;
938 if (TS_VERIFY_CTX_set_data(ctx, BIO_new_file(data, "rb")) == NULL)
0f113f3e
MC
939 goto err;
940 } else if (digest != NULL) {
941 long imprint_len;
ca4a494c
RS
942 unsigned char *hexstr = string_to_hex(digest, &imprint_len);
943 f |= TS_VFY_IMPRINT;
944 if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
0f113f3e
MC
945 BIO_printf(bio_err, "invalid digest string\n");
946 goto err;
947 }
0f113f3e
MC
948 }
949
950 } else if (queryfile != NULL) {
75ebbd9a 951 if ((input = BIO_new_file(queryfile, "rb")) == NULL)
0f113f3e 952 goto err;
75ebbd9a 953 if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
0f113f3e 954 goto err;
75ebbd9a 955 if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
0f113f3e
MC
956 goto err;
957 } else
958 return NULL;
959
960 /* Add the signature verification flag and arguments. */
ca4a494c 961 TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
0f113f3e
MC
962
963 /* Initialising the X509_STORE object. */
08538fc0 964 if (TS_VERIFY_CTX_set_store(ctx, create_cert_store(CApath, CAfile, vpm))
ca4a494c 965 == NULL)
0f113f3e
MC
966 goto err;
967
968 /* Loading untrusted certificates. */
ca4a494c
RS
969 if (untrusted
970 && TS_VERIFY_CTS_set_certs(ctx, TS_CONF_load_certs(untrusted)) == NULL)
0f113f3e 971 goto err;
0f113f3e 972 ret = 1;
18cd23df 973
c7235be6 974 err:
0f113f3e
MC
975 if (!ret) {
976 TS_VERIFY_CTX_free(ctx);
977 ctx = NULL;
978 }
979 BIO_free_all(input);
980 TS_REQ_free(request);
981 return ctx;
982}
c7235be6 983
08538fc0 984static X509_STORE *create_cert_store(char *CApath, char *CAfile, X509_VERIFY_PARAM *vpm)
0f113f3e
MC
985{
986 X509_STORE *cert_ctx = NULL;
987 X509_LOOKUP *lookup = NULL;
988 int i;
989
0f113f3e 990 cert_ctx = X509_STORE_new();
0f113f3e 991 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
96487cdd 992 if (CApath != NULL) {
0f113f3e
MC
993 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
994 if (lookup == NULL) {
995 BIO_printf(bio_err, "memory allocation failure\n");
996 goto err;
997 }
7e1b7485 998 i = X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM);
0f113f3e 999 if (!i) {
7e1b7485 1000 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
0f113f3e
MC
1001 goto err;
1002 }
1003 }
1004
96487cdd 1005 if (CAfile != NULL) {
0f113f3e
MC
1006 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
1007 if (lookup == NULL) {
1008 BIO_printf(bio_err, "memory allocation failure\n");
1009 goto err;
1010 }
7e1b7485 1011 i = X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM);
0f113f3e 1012 if (!i) {
7e1b7485 1013 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
0f113f3e
MC
1014 goto err;
1015 }
1016 }
08538fc0 1017
1018 if (vpm != NULL)
1019 X509_STORE_set1_param(cert_ctx, vpm);
1020
0f113f3e 1021 return cert_ctx;
18cd23df 1022
c7235be6 1023 err:
0f113f3e
MC
1024 X509_STORE_free(cert_ctx);
1025 return NULL;
1026}
c7235be6 1027
6d23cf97 1028static int verify_cb(int ok, X509_STORE_CTX *ctx)
0f113f3e 1029{
0f113f3e
MC
1030 return ok;
1031}
f3852635 1032#endif