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