2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
18 # include <openssl/bio.h>
19 # include <openssl/err.h>
20 # include <openssl/pem.h>
21 # include <openssl/rand.h>
22 # include <openssl/ts.h>
23 # include <openssl/bn.h>
25 /* Request nonce length, in bits (must be a multiple of 8). */
26 # define NONCE_LENGTH 64
28 /* Name of config entry that defines the OID file. */
29 # define ENV_OID_FILE "oid_file"
31 /* Is |EXACTLY_ONE| of three pointers set? */
32 # define EXACTLY_ONE(a, b, c) \
33 (( a && !b && !c) || \
37 static ASN1_OBJECT *txt2obj(const char *oid);
38 static CONF *load_config_file(const char *configfile);
40 /* Query related functions. */
41 static int query_command(const char *data, const char *digest,
42 const EVP_MD *md, const char *policy, int no_nonce,
43 int cert, const char *in, const char *out, int text);
44 static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
45 const char *policy, int no_nonce, int cert);
46 static int create_digest(BIO *input, const char *digest,
47 const EVP_MD *md, unsigned char **md_value);
48 static ASN1_INTEGER *create_nonce(int bits);
50 /* Reply related functions. */
51 static int reply_command(CONF *conf, const char *section, const char *engine,
52 const char *queryfile, const char *passin, const char *inkey,
53 const EVP_MD *md, const char *signer, const char *chain,
54 const char *policy, const char *in, int token_in,
55 const char *out, int token_out, int text);
56 static TS_RESP *read_PKCS7(BIO *in_bio);
57 static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
58 const char *queryfile, const char *passin,
59 const char *inkey, const EVP_MD *md, const char *signer,
60 const char *chain, const char *policy);
61 static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data);
62 static ASN1_INTEGER *next_serial(const char *serialfile);
63 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
65 /* Verify related functions. */
66 static int verify_command(const char *data, const char *digest, const char *queryfile,
67 const char *in, int token_in,
68 const char *CApath, const char *CAfile, const char *untrusted,
69 X509_VERIFY_PARAM *vpm);
70 static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
71 const char *queryfile,
72 const char *CApath, const char *CAfile,
73 const char *untrusted,
74 X509_VERIFY_PARAM *vpm);
75 static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
76 X509_VERIFY_PARAM *vpm);
77 static int verify_cb(int ok, X509_STORE_CTX *ctx);
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_RAND, 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_UNTRUSTED,
89 const OPTIONS ts_options[] = {
90 {"help", OPT_HELP, '-', "Display this summary"},
91 {"config", OPT_CONFIG, '<', "Configuration file"},
92 {"section", OPT_SECTION, 's', "Section to use within config file"},
93 {"query", OPT_QUERY, '-', "Generate a TS query"},
94 {"data", OPT_DATA, '<', "File to hash"},
95 {"digest", OPT_DIGEST, 's', "Digest (as a hex string)"},
96 {"rand", OPT_RAND, 's',
97 "Load the file(s) into the random number generator"},
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, '<', "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"},
120 {OPT_HELP_STR, 1, '-', "\nOptions specific to 'ts -verify': \n"},
122 {OPT_HELP_STR, 1, '-', "\n"},
127 * This command is so complex, special help is needed.
129 static char* opt_helplist[] = {
131 "ts -query [-rand file...] [-config file] [-data file]",
132 " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]",
133 " [-in file] [-out file] [-text]",
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]",
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']]",
153 int ts_main(int argc, char **argv)
156 const char *CAfile = NULL, *untrusted = NULL, *prog;
157 const char *configfile = default_config_file, *engine = NULL;
158 const char *section = NULL;
160 char *password = NULL;
161 char *data = NULL, *digest = NULL, *rnd = 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;
168 X509_VERIFY_PARAM *vpm = NULL;
169 /* Input is ContentInfo instead of TimeStampResp. */
171 /* Output is ContentInfo instead of TimeStampResp. */
174 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
177 prog = opt_init(argc, argv, ts_options);
178 while ((o = opt_next()) != OPT_EOF) {
183 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
186 opt_help(ts_options);
187 for (helpp = opt_helplist; *helpp; ++helpp)
188 BIO_printf(bio_err, "%s\n", *helpp);
192 configfile = opt_arg();
238 queryfile = opt_arg();
259 untrusted = opt_arg();
265 if (!opt_md(opt_unknown(), &md))
269 if (!opt_verify(o, vpm))
275 if (mode == OPT_ERR || opt_num_rest() != 0)
278 /* Seed the random number generator if it is going to be used. */
279 if (mode == OPT_QUERY && !no_nonce) {
280 if (!app_RAND_load_file(NULL, 1) && rnd == NULL)
281 BIO_printf(bio_err, "warning, not much extra random "
282 "data, consider using the -rand option\n");
284 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
285 app_RAND_load_files(rnd));
288 if (mode == OPT_REPLY && passin &&
289 !app_passwd(passin, NULL, &password, NULL)) {
290 BIO_printf(bio_err, "Error getting password.\n");
294 conf = load_config_file(configfile);
295 if (configfile != default_config_file && !app_load_modules(conf))
298 /* Check parameter consistency and execute the appropriate function. */
299 if (mode == OPT_QUERY) {
302 if ((data != NULL) && (digest != NULL))
304 ret = !query_command(data, digest, md, policy, no_nonce, cert,
306 } else if (mode == OPT_REPLY) {
309 if ((in != NULL) && (queryfile != NULL))
312 if ((conf == NULL) || (token_in != 0))
315 ret = !reply_command(conf, section, engine, queryfile,
316 password, inkey, md, signer, chain, policy,
317 in, token_in, out, token_out, text);
319 } else if (mode == OPT_VERIFY) {
320 if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
322 ret = !verify_command(data, digest, queryfile, in, token_in,
323 CApath, CAfile, untrusted,
324 vpmtouched ? vpm : NULL);
330 X509_VERIFY_PARAM_free(vpm);
331 app_RAND_write_file(NULL);
333 OPENSSL_free(password);
338 * Configuration file-related function definitions.
341 static ASN1_OBJECT *txt2obj(const char *oid)
343 ASN1_OBJECT *oid_obj = NULL;
345 if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
346 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
351 static CONF *load_config_file(const char *configfile)
353 CONF *conf = app_load_config(configfile);
358 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
359 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
361 BIO *oid_bio = BIO_new_file(p, "r");
363 ERR_print_errors(bio_err);
365 OBJ_create_objects(oid_bio);
366 BIO_free_all(oid_bio);
370 if (!add_oid_section(conf))
371 ERR_print_errors(bio_err);
377 * Query-related method definitions.
379 static int query_command(const char *data, const char *digest, const EVP_MD *md,
380 const char *policy, int no_nonce,
381 int cert, const char *in, const char *out, int text)
384 TS_REQ *query = NULL;
386 BIO *data_bio = NULL;
389 /* Build query object. */
391 if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
393 query = d2i_TS_REQ_bio(in_bio, NULL);
396 && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
398 query = create_query(data_bio, digest, md, policy, no_nonce, cert);
404 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
406 if (!TS_REQ_print_bio(out_bio, query))
409 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
411 if (!i2d_TS_REQ_bio(out_bio, query))
418 ERR_print_errors(bio_err);
419 BIO_free_all(in_bio);
420 BIO_free_all(data_bio);
421 BIO_free_all(out_bio);
426 static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
427 const char *policy, int no_nonce, int cert)
430 TS_REQ *ts_req = NULL;
432 TS_MSG_IMPRINT *msg_imprint = NULL;
433 X509_ALGOR *algo = NULL;
434 unsigned char *data = NULL;
435 ASN1_OBJECT *policy_obj = NULL;
436 ASN1_INTEGER *nonce_asn1 = NULL;
438 if (md == NULL && (md = EVP_get_digestbyname("sha1")) == NULL)
440 if ((ts_req = TS_REQ_new()) == NULL)
442 if (!TS_REQ_set_version(ts_req, 1))
444 if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
446 if ((algo = X509_ALGOR_new()) == NULL)
448 if ((algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))) == NULL)
450 if ((algo->parameter = ASN1_TYPE_new()) == NULL)
452 algo->parameter->type = V_ASN1_NULL;
453 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
455 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
457 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
459 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
461 if (policy && (policy_obj = txt2obj(policy)) == NULL)
463 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
466 /* Setting nonce if requested. */
467 if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
469 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
471 if (!TS_REQ_set_cert_req(ts_req, cert))
479 BIO_printf(bio_err, "could not create query\n");
480 ERR_print_errors(bio_err);
482 TS_MSG_IMPRINT_free(msg_imprint);
483 X509_ALGOR_free(algo);
485 ASN1_OBJECT_free(policy_obj);
486 ASN1_INTEGER_free(nonce_asn1);
490 static int create_digest(BIO *input, const char *digest, const EVP_MD *md,
491 unsigned char **md_value)
495 EVP_MD_CTX *md_ctx = NULL;
497 md_value_len = EVP_MD_size(md);
498 if (md_value_len < 0)
502 unsigned char buffer[4096];
505 md_ctx = EVP_MD_CTX_new();
508 *md_value = app_malloc(md_value_len, "digest buffer");
509 if (!EVP_DigestInit(md_ctx, md))
511 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
512 if (!EVP_DigestUpdate(md_ctx, buffer, length))
515 if (!EVP_DigestFinal(md_ctx, *md_value, NULL))
517 md_value_len = EVP_MD_size(md);
520 *md_value = OPENSSL_hexstr2buf(digest, &digest_len);
521 if (!*md_value || md_value_len != digest_len) {
522 OPENSSL_free(*md_value);
524 BIO_printf(bio_err, "bad digest, %d bytes "
525 "must be specified\n", md_value_len);
531 EVP_MD_CTX_free(md_ctx);
535 static ASN1_INTEGER *create_nonce(int bits)
537 unsigned char buf[20];
538 ASN1_INTEGER *nonce = NULL;
539 int len = (bits - 1) / 8 + 1;
542 if (len > (int)sizeof(buf))
544 if (RAND_bytes(buf, len) <= 0)
547 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
548 for (i = 0; i < len && !buf[i]; ++i)
550 if ((nonce = ASN1_INTEGER_new()) == NULL)
552 OPENSSL_free(nonce->data);
553 nonce->length = len - i;
554 nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
555 memcpy(nonce->data, buf + i, nonce->length);
559 BIO_printf(bio_err, "could not create nonce\n");
560 ASN1_INTEGER_free(nonce);
565 * Reply-related method definitions.
568 static int reply_command(CONF *conf, const char *section, const char *engine,
569 const char *queryfile, const char *passin, const char *inkey,
570 const EVP_MD *md, const char *signer, const char *chain,
571 const char *policy, const char *in, int token_in,
572 const char *out, int token_out, int text)
575 TS_RESP *response = NULL;
577 BIO *query_bio = NULL;
578 BIO *inkey_bio = NULL;
579 BIO *signer_bio = NULL;
583 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
586 response = read_PKCS7(in_bio);
588 response = d2i_TS_RESP_bio(in_bio, NULL);
591 response = create_response(conf, section, engine, queryfile,
592 passin, inkey, md, signer, chain, policy);
594 BIO_printf(bio_err, "Response has been generated.\n");
596 BIO_printf(bio_err, "Response is not generated.\n");
598 if (response == NULL)
601 /* Write response. */
603 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
606 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
607 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
610 if (!TS_RESP_print_bio(out_bio, response))
614 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
617 PKCS7 *token = TS_RESP_get_token(response);
618 if (!i2d_PKCS7_bio(out_bio, token))
621 if (!i2d_TS_RESP_bio(out_bio, response))
629 ERR_print_errors(bio_err);
630 BIO_free_all(in_bio);
631 BIO_free_all(query_bio);
632 BIO_free_all(inkey_bio);
633 BIO_free_all(signer_bio);
634 BIO_free_all(out_bio);
635 TS_RESP_free(response);
639 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
640 static TS_RESP *read_PKCS7(BIO *in_bio)
644 TS_TST_INFO *tst_info = NULL;
645 TS_RESP *resp = NULL;
646 TS_STATUS_INFO *si = NULL;
648 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
650 if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
652 if ((resp = TS_RESP_new()) == NULL)
654 if ((si = TS_STATUS_INFO_new()) == NULL)
656 if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
658 if (!TS_RESP_set_status_info(resp, si))
660 TS_RESP_set_tst_info(resp, token, tst_info);
661 token = NULL; /* Ownership is lost. */
662 tst_info = NULL; /* Ownership is lost. */
667 TS_TST_INFO_free(tst_info);
672 TS_STATUS_INFO_free(si);
676 static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
677 const char *queryfile, const char *passin,
678 const char *inkey, const EVP_MD *md, const char *signer,
679 const char *chain, const char *policy)
682 TS_RESP *response = NULL;
683 BIO *query_bio = NULL;
684 TS_RESP_CTX *resp_ctx = NULL;
686 if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
688 if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
690 if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
692 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
694 # ifndef OPENSSL_NO_ENGINE
695 if (!TS_CONF_set_crypto_device(conf, section, engine))
698 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
700 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
702 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
706 if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
708 } else if (!TS_CONF_set_signer_digest(conf, section, NULL, 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);
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);
758 static ASN1_INTEGER *next_serial(const char *serialfile)
762 ASN1_INTEGER *serial = NULL;
765 if ((serial = ASN1_INTEGER_new()) == NULL)
768 if ((in = BIO_new_file(serialfile, "r")) == NULL) {
770 BIO_printf(bio_err, "Warning: could not open file %s for "
771 "reading, using serial number: 1\n", serialfile);
772 if (!ASN1_INTEGER_set(serial, 1))
776 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
777 BIO_printf(bio_err, "unable to load number from %s\n",
781 if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
783 ASN1_INTEGER_free(serial);
785 if (!BN_add_word(bn, 1))
787 if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
794 ASN1_INTEGER_free(serial);
802 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
807 if ((out = BIO_new_file(serialfile, "w")) == NULL)
809 if (i2a_ASN1_INTEGER(out, serial) <= 0)
811 if (BIO_puts(out, "\n") <= 0)
816 BIO_printf(bio_err, "could not save serial number to %s\n",
824 * Verify-related method definitions.
827 static int verify_command(const char *data, const char *digest, const char *queryfile,
828 const char *in, int token_in,
829 const char *CApath, const char *CAfile, const char *untrusted,
830 X509_VERIFY_PARAM *vpm)
834 TS_RESP *response = NULL;
835 TS_VERIFY_CTX *verify_ctx = NULL;
838 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
841 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
844 if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
848 if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
849 CApath, CAfile, untrusted,
854 ? TS_RESP_verify_token(verify_ctx, token)
855 : TS_RESP_verify_response(verify_ctx, response);
858 printf("Verification: ");
863 ERR_print_errors(bio_err);
866 BIO_free_all(in_bio);
868 TS_RESP_free(response);
869 TS_VERIFY_CTX_free(verify_ctx);
873 static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
874 const char *queryfile,
875 const char *CApath, const char *CAfile,
876 const char *untrusted,
877 X509_VERIFY_PARAM *vpm)
879 TS_VERIFY_CTX *ctx = NULL;
881 TS_REQ *request = NULL;
885 if (data != NULL || digest != NULL) {
886 if ((ctx = TS_VERIFY_CTX_new()) == NULL)
888 f = TS_VFY_VERSION | TS_VFY_SIGNER;
891 if (TS_VERIFY_CTX_set_data(ctx, BIO_new_file(data, "rb")) == NULL)
893 } else if (digest != NULL) {
895 unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
897 if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
898 BIO_printf(bio_err, "invalid digest string\n");
903 } else if (queryfile != NULL) {
904 if ((input = BIO_new_file(queryfile, "rb")) == NULL)
906 if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
908 if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
913 /* Add the signature verification flag and arguments. */
914 TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
916 /* Initialising the X509_STORE object. */
917 if (TS_VERIFY_CTX_set_store(ctx, create_cert_store(CApath, CAfile, vpm))
921 /* Loading untrusted certificates. */
923 && TS_VERIFY_CTS_set_certs(ctx, TS_CONF_load_certs(untrusted)) == NULL)
929 TS_VERIFY_CTX_free(ctx);
933 TS_REQ_free(request);
937 static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
938 X509_VERIFY_PARAM *vpm)
940 X509_STORE *cert_ctx = NULL;
941 X509_LOOKUP *lookup = NULL;
944 cert_ctx = X509_STORE_new();
945 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
946 if (CApath != NULL) {
947 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
948 if (lookup == NULL) {
949 BIO_printf(bio_err, "memory allocation failure\n");
952 i = X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM);
954 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
959 if (CAfile != NULL) {
960 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
961 if (lookup == NULL) {
962 BIO_printf(bio_err, "memory allocation failure\n");
965 i = X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM);
967 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
973 X509_STORE_set1_param(cert_ctx, vpm);
978 X509_STORE_free(cert_ctx);
982 static int verify_cb(int ok, X509_STORE_CTX *ctx)
986 #endif /* ndef OPENSSL_NO_TS */