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