2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
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>
26 /* Request nonce length, in bits (must be a multiple of 8). */
27 # define NONCE_LENGTH 64
29 /* Name of config entry that defines the OID file. */
30 # define ENV_OID_FILE "oid_file"
32 /* Is |EXACTLY_ONE| of three pointers set? */
33 # define EXACTLY_ONE(a, b, c) \
34 (( a && !b && !c) || \
38 static ASN1_OBJECT *txt2obj(const char *oid);
39 static CONF *load_config_file(const char *configfile);
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);
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);
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,
71 const char *untrusted, X509_VERIFY_PARAM *vpm);
72 static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
73 const char *queryfile,
74 const char *CApath, const char *CAfile,
76 const char *untrusted,
77 X509_VERIFY_PARAM *vpm);
78 static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
79 const char *CAstore, X509_VERIFY_PARAM *vpm);
80 static int verify_cb(int ok, X509_STORE_CTX *ctx);
82 typedef enum OPTION_choice {
83 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
84 OPT_ENGINE, OPT_CONFIG, OPT_SECTION, OPT_QUERY, OPT_DATA,
85 OPT_DIGEST, OPT_TSPOLICY, OPT_NO_NONCE, OPT_CERT,
86 OPT_IN, OPT_TOKEN_IN, OPT_OUT, OPT_TOKEN_OUT, OPT_TEXT,
87 OPT_REPLY, OPT_QUERYFILE, OPT_PASSIN, OPT_INKEY, OPT_SIGNER,
88 OPT_CHAIN, OPT_VERIFY, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE, OPT_UNTRUSTED,
89 OPT_MD, OPT_V_ENUM, OPT_R_ENUM
92 const OPTIONS ts_options[] = {
93 {"help", OPT_HELP, '-', "Display this summary"},
94 {"config", OPT_CONFIG, '<', "Configuration file"},
95 {"section", OPT_SECTION, 's', "Section to use within config file"},
96 {"query", OPT_QUERY, '-', "Generate a TS query"},
97 {"data", OPT_DATA, '<', "File to hash"},
98 {"digest", OPT_DIGEST, 's', "Digest (as a hex string)"},
100 {"tspolicy", OPT_TSPOLICY, 's', "Policy OID to use"},
101 {"no_nonce", OPT_NO_NONCE, '-', "Do not include a nonce"},
102 {"cert", OPT_CERT, '-', "Put cert request into query"},
103 {"in", OPT_IN, '<', "Input file"},
104 {"token_in", OPT_TOKEN_IN, '-', "Input is a PKCS#7 file"},
105 {"out", OPT_OUT, '>', "Output file"},
106 {"token_out", OPT_TOKEN_OUT, '-', "Output is a PKCS#7 file"},
107 {"text", OPT_TEXT, '-', "Output text (not DER)"},
108 {"reply", OPT_REPLY, '-', "Generate a TS reply"},
109 {"queryfile", OPT_QUERYFILE, '<', "File containing a TS query"},
110 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
111 {"inkey", OPT_INKEY, 's', "File with private key for reply"},
112 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
113 {"chain", OPT_CHAIN, '<', "File with signer CA chain"},
114 {"verify", OPT_VERIFY, '-', "Verify a TS response"},
115 {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"},
116 {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"},
117 {"CAstore", OPT_CASTORE, ':', "URI to trusted CA store"},
118 {"untrusted", OPT_UNTRUSTED, '<', "File with untrusted certs"},
119 {"", OPT_MD, '-', "Any supported digest"},
120 # ifndef OPENSSL_NO_ENGINE
121 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
123 {OPT_HELP_STR, 1, '-', "\nOptions specific to 'ts -verify': \n"},
125 {OPT_HELP_STR, 1, '-', "\n"},
130 * This command is so complex, special help is needed.
132 static char* opt_helplist[] = {
134 "ts -query [-rand file...] [-config file] [-data file]",
135 " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]",
136 " [-in file] [-out file] [-text]",
138 "ts -reply [-config file] [-section tsa_section]",
139 " [-queryfile file] [-passin password]",
140 " [-signer tsa_cert.pem] [-inkey private_key.pem]",
141 " [-chain certs_file.pem] [-tspolicy oid]",
142 " [-in file] [-token_in] [-out file] [-token_out]",
143 # ifndef OPENSSL_NO_ENGINE
144 " [-text] [-engine id]",
149 "ts -verify -CApath dir -CAfile file.pem -CAstore uri -untrusted file.pem",
150 " [-data file] [-digest hexstring]",
151 " [-queryfile file] -in file [-token_in]",
152 " [[options specific to 'ts -verify']]",
156 int ts_main(int argc, char **argv)
159 const char *CAfile = NULL, *untrusted = NULL, *prog;
160 const char *configfile = default_config_file, *engine = NULL;
161 const char *section = NULL;
163 char *password = NULL;
164 char *data = NULL, *digest = NULL, *policy = NULL;
165 char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL;
166 char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL;
167 char *CAstore = NULL;
168 const EVP_MD *md = NULL;
169 OPTION_CHOICE o, mode = OPT_ERR;
170 int ret = 1, no_nonce = 0, cert = 0, text = 0;
172 X509_VERIFY_PARAM *vpm = NULL;
173 /* Input is ContentInfo instead of TimeStampResp. */
175 /* Output is ContentInfo instead of TimeStampResp. */
178 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
181 prog = opt_init(argc, argv, ts_options);
182 while ((o = opt_next()) != OPT_EOF) {
187 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
190 opt_help(ts_options);
191 for (helpp = opt_helplist; *helpp; ++helpp)
192 BIO_printf(bio_err, "%s\n", *helpp);
196 configfile = opt_arg();
243 queryfile = opt_arg();
267 untrusted = opt_arg();
273 if (!opt_md(opt_unknown(), &md))
277 if (!opt_verify(o, vpm))
283 if (mode == OPT_ERR || opt_num_rest() != 0)
286 if (mode == OPT_REPLY && passin &&
287 !app_passwd(passin, NULL, &password, NULL)) {
288 BIO_printf(bio_err, "Error getting password.\n");
292 conf = load_config_file(configfile);
293 if (configfile != default_config_file && !app_load_modules(conf))
296 /* Check parameter consistency and execute the appropriate function. */
297 if (mode == OPT_QUERY) {
300 if ((data != NULL) && (digest != NULL))
302 ret = !query_command(data, digest, md, policy, no_nonce, cert,
304 } else if (mode == OPT_REPLY) {
307 if ((in != NULL) && (queryfile != NULL))
310 if ((conf == NULL) || (token_in != 0))
313 ret = !reply_command(conf, section, engine, queryfile,
314 password, inkey, md, signer, chain, policy,
315 in, token_in, out, token_out, text);
317 } else if (mode == OPT_VERIFY) {
318 if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
320 ret = !verify_command(data, digest, queryfile, in, token_in,
321 CApath, CAfile, CAstore, untrusted,
322 vpmtouched ? vpm : NULL);
328 X509_VERIFY_PARAM_free(vpm);
330 OPENSSL_free(password);
335 * Configuration file-related function definitions.
338 static ASN1_OBJECT *txt2obj(const char *oid)
340 ASN1_OBJECT *oid_obj = NULL;
342 if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
343 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
348 static CONF *load_config_file(const char *configfile)
350 CONF *conf = app_load_config(configfile);
355 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
356 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
358 BIO *oid_bio = BIO_new_file(p, "r");
360 ERR_print_errors(bio_err);
362 OBJ_create_objects(oid_bio);
363 BIO_free_all(oid_bio);
367 if (!add_oid_section(conf))
368 ERR_print_errors(bio_err);
374 * Query-related method definitions.
376 static int query_command(const char *data, const char *digest, const EVP_MD *md,
377 const char *policy, int no_nonce,
378 int cert, const char *in, const char *out, int text)
381 TS_REQ *query = NULL;
383 BIO *data_bio = NULL;
386 /* Build query object. */
388 if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
390 query = d2i_TS_REQ_bio(in_bio, NULL);
393 && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
395 query = create_query(data_bio, digest, md, policy, no_nonce, cert);
401 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
403 if (!TS_REQ_print_bio(out_bio, query))
406 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
408 if (!i2d_TS_REQ_bio(out_bio, query))
415 ERR_print_errors(bio_err);
416 BIO_free_all(in_bio);
417 BIO_free_all(data_bio);
418 BIO_free_all(out_bio);
423 static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
424 const char *policy, int no_nonce, int cert)
427 TS_REQ *ts_req = NULL;
429 TS_MSG_IMPRINT *msg_imprint = NULL;
430 X509_ALGOR *algo = NULL;
431 unsigned char *data = NULL;
432 ASN1_OBJECT *policy_obj = NULL;
433 ASN1_INTEGER *nonce_asn1 = NULL;
435 if (md == NULL && (md = EVP_get_digestbyname("sha256")) == NULL)
437 if ((ts_req = TS_REQ_new()) == NULL)
439 if (!TS_REQ_set_version(ts_req, 1))
441 if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
443 if ((algo = X509_ALGOR_new()) == NULL)
445 if ((algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))) == NULL)
447 if ((algo->parameter = ASN1_TYPE_new()) == NULL)
449 algo->parameter->type = V_ASN1_NULL;
450 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
452 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
454 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
456 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
458 if (policy && (policy_obj = txt2obj(policy)) == NULL)
460 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
463 /* Setting nonce if requested. */
464 if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
466 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
468 if (!TS_REQ_set_cert_req(ts_req, cert))
476 BIO_printf(bio_err, "could not create query\n");
477 ERR_print_errors(bio_err);
479 TS_MSG_IMPRINT_free(msg_imprint);
480 X509_ALGOR_free(algo);
482 ASN1_OBJECT_free(policy_obj);
483 ASN1_INTEGER_free(nonce_asn1);
487 static int create_digest(BIO *input, const char *digest, const EVP_MD *md,
488 unsigned char **md_value)
492 EVP_MD_CTX *md_ctx = NULL;
494 md_value_len = EVP_MD_size(md);
495 if (md_value_len < 0)
499 unsigned char buffer[4096];
502 md_ctx = EVP_MD_CTX_new();
505 *md_value = app_malloc(md_value_len, "digest buffer");
506 if (!EVP_DigestInit(md_ctx, md))
508 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
509 if (!EVP_DigestUpdate(md_ctx, buffer, length))
512 if (!EVP_DigestFinal(md_ctx, *md_value, NULL))
514 md_value_len = EVP_MD_size(md);
518 *md_value = OPENSSL_hexstr2buf(digest, &digest_len);
519 if (*md_value == NULL || md_value_len != digest_len) {
520 OPENSSL_free(*md_value);
522 BIO_printf(bio_err, "bad digest, %d bytes "
523 "must be specified\n", md_value_len);
529 EVP_MD_CTX_free(md_ctx);
533 static ASN1_INTEGER *create_nonce(int bits)
535 unsigned char buf[20];
536 ASN1_INTEGER *nonce = NULL;
537 int len = (bits - 1) / 8 + 1;
540 if (len > (int)sizeof(buf))
542 if (RAND_bytes(buf, len) <= 0)
545 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
546 for (i = 0; i < len && !buf[i]; ++i)
548 if ((nonce = ASN1_INTEGER_new()) == NULL)
550 OPENSSL_free(nonce->data);
551 nonce->length = len - i;
552 nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
553 memcpy(nonce->data, buf + i, nonce->length);
557 BIO_printf(bio_err, "could not create nonce\n");
558 ASN1_INTEGER_free(nonce);
563 * Reply-related method definitions.
566 static int reply_command(CONF *conf, const char *section, const char *engine,
567 const char *queryfile, const char *passin, const char *inkey,
568 const EVP_MD *md, const char *signer, const char *chain,
569 const char *policy, const char *in, int token_in,
570 const char *out, int token_out, int text)
573 TS_RESP *response = NULL;
575 BIO *query_bio = NULL;
576 BIO *inkey_bio = NULL;
577 BIO *signer_bio = NULL;
581 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
584 response = read_PKCS7(in_bio);
586 response = d2i_TS_RESP_bio(in_bio, NULL);
589 response = create_response(conf, section, engine, queryfile,
590 passin, inkey, md, signer, chain, policy);
591 if (response != NULL)
592 BIO_printf(bio_err, "Response has been generated.\n");
594 BIO_printf(bio_err, "Response is not generated.\n");
596 if (response == NULL)
599 /* Write response. */
601 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
604 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
605 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
608 if (!TS_RESP_print_bio(out_bio, response))
612 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
615 PKCS7 *token = TS_RESP_get_token(response);
616 if (!i2d_PKCS7_bio(out_bio, token))
619 if (!i2d_TS_RESP_bio(out_bio, response))
627 ERR_print_errors(bio_err);
628 BIO_free_all(in_bio);
629 BIO_free_all(query_bio);
630 BIO_free_all(inkey_bio);
631 BIO_free_all(signer_bio);
632 BIO_free_all(out_bio);
633 TS_RESP_free(response);
637 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
638 static TS_RESP *read_PKCS7(BIO *in_bio)
642 TS_TST_INFO *tst_info = NULL;
643 TS_RESP *resp = NULL;
644 TS_STATUS_INFO *si = NULL;
646 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
648 if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
650 if ((resp = TS_RESP_new()) == NULL)
652 if ((si = TS_STATUS_INFO_new()) == NULL)
654 if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
656 if (!TS_RESP_set_status_info(resp, si))
658 TS_RESP_set_tst_info(resp, token, tst_info);
659 token = NULL; /* Ownership is lost. */
660 tst_info = NULL; /* Ownership is lost. */
665 TS_TST_INFO_free(tst_info);
670 TS_STATUS_INFO_free(si);
674 static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
675 const char *queryfile, const char *passin,
676 const char *inkey, const EVP_MD *md, const char *signer,
677 const char *chain, const char *policy)
680 TS_RESP *response = NULL;
681 BIO *query_bio = NULL;
682 TS_RESP_CTX *resp_ctx = NULL;
684 if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
686 if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
688 if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
690 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
692 # ifndef OPENSSL_NO_ENGINE
693 if (!TS_CONF_set_crypto_device(conf, section, engine))
696 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
698 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
700 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
704 if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
706 } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
710 if (!TS_CONF_set_ess_cert_id_digest(conf, section, resp_ctx))
712 if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
714 if (!TS_CONF_set_policies(conf, section, resp_ctx))
716 if (!TS_CONF_set_digests(conf, section, resp_ctx))
718 if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
720 if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
722 if (!TS_CONF_set_ordering(conf, section, resp_ctx))
724 if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
726 if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
728 if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
734 TS_RESP_free(response);
737 TS_RESP_CTX_free(resp_ctx);
738 BIO_free_all(query_bio);
742 static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
744 const char *serial_file = (const char *)data;
745 ASN1_INTEGER *serial = next_serial(serial_file);
747 if (serial == NULL) {
748 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
749 "Error during serial number "
751 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
753 save_ts_serial(serial_file, serial);
759 static ASN1_INTEGER *next_serial(const char *serialfile)
763 ASN1_INTEGER *serial = NULL;
766 if ((serial = ASN1_INTEGER_new()) == NULL)
769 if ((in = BIO_new_file(serialfile, "r")) == NULL) {
771 BIO_printf(bio_err, "Warning: could not open file %s for "
772 "reading, using serial number: 1\n", serialfile);
773 if (!ASN1_INTEGER_set(serial, 1))
777 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
778 BIO_printf(bio_err, "unable to load number from %s\n",
782 if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
784 ASN1_INTEGER_free(serial);
786 if (!BN_add_word(bn, 1))
788 if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
795 ASN1_INTEGER_free(serial);
803 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
808 if ((out = BIO_new_file(serialfile, "w")) == NULL)
810 if (i2a_ASN1_INTEGER(out, serial) <= 0)
812 if (BIO_puts(out, "\n") <= 0)
817 BIO_printf(bio_err, "could not save serial number to %s\n",
825 * Verify-related method definitions.
828 static int verify_command(const char *data, const char *digest, const char *queryfile,
829 const char *in, int token_in,
830 const char *CApath, const char *CAfile,
831 const char *CAstore, const char *untrusted,
832 X509_VERIFY_PARAM *vpm)
836 TS_RESP *response = NULL;
837 TS_VERIFY_CTX *verify_ctx = NULL;
840 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
843 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
846 if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
850 if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
851 CApath, CAfile, CAstore, untrusted,
856 ? TS_RESP_verify_token(verify_ctx, token)
857 : TS_RESP_verify_response(verify_ctx, response);
860 printf("Verification: ");
865 ERR_print_errors(bio_err);
868 BIO_free_all(in_bio);
870 TS_RESP_free(response);
871 TS_VERIFY_CTX_free(verify_ctx);
875 static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
876 const char *queryfile,
877 const char *CApath, const char *CAfile,
879 const char *untrusted,
880 X509_VERIFY_PARAM *vpm)
882 TS_VERIFY_CTX *ctx = NULL;
884 TS_REQ *request = NULL;
888 if (data != NULL || digest != NULL) {
889 if ((ctx = TS_VERIFY_CTX_new()) == NULL)
891 f = TS_VFY_VERSION | TS_VFY_SIGNER;
896 if ((out = BIO_new_file(data, "rb")) == NULL)
898 if (TS_VERIFY_CTX_set_data(ctx, out) == NULL) {
902 } else if (digest != NULL) {
904 unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
906 if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
907 BIO_printf(bio_err, "invalid digest string\n");
912 } else if (queryfile != NULL) {
913 if ((input = BIO_new_file(queryfile, "rb")) == NULL)
915 if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
917 if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
923 /* Add the signature verification flag and arguments. */
924 TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
926 /* Initialising the X509_STORE object. */
927 if (TS_VERIFY_CTX_set_store(ctx,
928 create_cert_store(CApath, CAfile, CAstore, vpm))
932 /* Loading untrusted certificates. */
934 && TS_VERIFY_CTX_set_certs(ctx, TS_CONF_load_certs(untrusted)) == NULL)
940 TS_VERIFY_CTX_free(ctx);
944 TS_REQ_free(request);
948 static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
949 const char *CAstore, X509_VERIFY_PARAM *vpm)
951 X509_STORE *cert_ctx = NULL;
952 X509_LOOKUP *lookup = NULL;
954 cert_ctx = X509_STORE_new();
955 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
956 if (CApath != NULL) {
957 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
958 if (lookup == NULL) {
959 BIO_printf(bio_err, "memory allocation failure\n");
962 if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) {
963 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
968 if (CAfile != NULL) {
969 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
970 if (lookup == NULL) {
971 BIO_printf(bio_err, "memory allocation failure\n");
974 if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) {
975 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
980 if (CAstore != NULL) {
981 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_store());
982 if (lookup == NULL) {
983 BIO_printf(bio_err, "memory allocation failure\n");
986 if (!X509_LOOKUP_load_store(lookup, CAstore)) {
987 BIO_printf(bio_err, "Error loading store URI %s\n", CAstore);
993 X509_STORE_set1_param(cert_ctx, vpm);
998 X509_STORE_free(cert_ctx);
1002 static int verify_cb(int ok, X509_STORE_CTX *ctx)
1006 #endif /* ndef OPENSSL_NO_TS */