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