2 * Copyright 2006-2017 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_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,
86 OPT_MD, OPT_V_ENUM, OPT_R_ENUM
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)"},
97 {"tspolicy", OPT_TSPOLICY, 's', "Policy OID to use"},
98 {"no_nonce", OPT_NO_NONCE, '-', "Do not include a nonce"},
99 {"cert", OPT_CERT, '-', "Put cert request into query"},
100 {"in", OPT_IN, '<', "Input file"},
101 {"token_in", OPT_TOKEN_IN, '-', "Input is a PKCS#7 file"},
102 {"out", OPT_OUT, '>', "Output file"},
103 {"token_out", OPT_TOKEN_OUT, '-', "Output is a PKCS#7 file"},
104 {"text", OPT_TEXT, '-', "Output text (not DER)"},
105 {"reply", OPT_REPLY, '-', "Generate a TS reply"},
106 {"queryfile", OPT_QUERYFILE, '<', "File containing a TS query"},
107 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
108 {"inkey", OPT_INKEY, 's', "File with private key for reply"},
109 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
110 {"chain", OPT_CHAIN, '<', "File with signer CA chain"},
111 {"verify", OPT_VERIFY, '-', "Verify a TS response"},
112 {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"},
113 {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"},
114 {"untrusted", OPT_UNTRUSTED, '<', "File with untrusted certs"},
115 {"", OPT_MD, '-', "Any supported digest"},
116 # ifndef OPENSSL_NO_ENGINE
117 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
119 {OPT_HELP_STR, 1, '-', "\nOptions specific to 'ts -verify': \n"},
121 {OPT_HELP_STR, 1, '-', "\n"},
126 * This command is so complex, special help is needed.
128 static char* opt_helplist[] = {
130 "ts -query [-rand file...] [-config file] [-data file]",
131 " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]",
132 " [-in file] [-out file] [-text]",
134 "ts -reply [-config file] [-section tsa_section]",
135 " [-queryfile file] [-passin password]",
136 " [-signer tsa_cert.pem] [-inkey private_key.pem]",
137 " [-chain certs_file.pem] [-tspolicy oid]",
138 " [-in file] [-token_in] [-out file] [-token_out]",
139 # ifndef OPENSSL_NO_ENGINE
140 " [-text] [-engine id]",
145 "ts -verify -CApath dir -CAfile file.pem -untrusted file.pem",
146 " [-data file] [-digest hexstring]",
147 " [-queryfile file] -in file [-token_in]",
148 " [[options specific to 'ts -verify']]",
152 int ts_main(int argc, char **argv)
155 const char *CAfile = NULL, *untrusted = NULL, *prog;
156 const char *configfile = default_config_file, *engine = NULL;
157 const char *section = NULL;
159 char *password = NULL;
160 char *data = NULL, *digest = NULL, *policy = NULL;
161 char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL;
162 char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL;
163 const EVP_MD *md = NULL;
164 OPTION_CHOICE o, mode = OPT_ERR;
165 int ret = 1, no_nonce = 0, cert = 0, text = 0;
167 X509_VERIFY_PARAM *vpm = NULL;
168 /* Input is ContentInfo instead of TimeStampResp. */
170 /* Output is ContentInfo instead of TimeStampResp. */
173 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
176 prog = opt_init(argc, argv, ts_options);
177 while ((o = opt_next()) != OPT_EOF) {
182 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
185 opt_help(ts_options);
186 for (helpp = opt_helplist; *helpp; ++helpp)
187 BIO_printf(bio_err, "%s\n", *helpp);
191 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 if (mode == OPT_REPLY && passin &&
279 !app_passwd(passin, NULL, &password, NULL)) {
280 BIO_printf(bio_err, "Error getting password.\n");
284 conf = load_config_file(configfile);
285 if (configfile != default_config_file && !app_load_modules(conf))
288 /* Check parameter consistency and execute the appropriate function. */
289 if (mode == OPT_QUERY) {
292 if ((data != NULL) && (digest != NULL))
294 ret = !query_command(data, digest, md, policy, no_nonce, cert,
296 } else if (mode == OPT_REPLY) {
299 if ((in != NULL) && (queryfile != NULL))
302 if ((conf == NULL) || (token_in != 0))
305 ret = !reply_command(conf, section, engine, queryfile,
306 password, inkey, md, signer, chain, policy,
307 in, token_in, out, token_out, text);
309 } else if (mode == OPT_VERIFY) {
310 if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
312 ret = !verify_command(data, digest, queryfile, in, token_in,
313 CApath, CAfile, untrusted,
314 vpmtouched ? vpm : NULL);
320 X509_VERIFY_PARAM_free(vpm);
322 OPENSSL_free(password);
327 * Configuration file-related function definitions.
330 static ASN1_OBJECT *txt2obj(const char *oid)
332 ASN1_OBJECT *oid_obj = NULL;
334 if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
335 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
340 static CONF *load_config_file(const char *configfile)
342 CONF *conf = app_load_config(configfile);
347 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
348 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
350 BIO *oid_bio = BIO_new_file(p, "r");
352 ERR_print_errors(bio_err);
354 OBJ_create_objects(oid_bio);
355 BIO_free_all(oid_bio);
359 if (!add_oid_section(conf))
360 ERR_print_errors(bio_err);
366 * Query-related method definitions.
368 static int query_command(const char *data, const char *digest, const EVP_MD *md,
369 const char *policy, int no_nonce,
370 int cert, const char *in, const char *out, int text)
373 TS_REQ *query = NULL;
375 BIO *data_bio = NULL;
378 /* Build query object. */
380 if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
382 query = d2i_TS_REQ_bio(in_bio, NULL);
385 && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
387 query = create_query(data_bio, digest, md, policy, no_nonce, cert);
393 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
395 if (!TS_REQ_print_bio(out_bio, query))
398 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
400 if (!i2d_TS_REQ_bio(out_bio, query))
407 ERR_print_errors(bio_err);
408 BIO_free_all(in_bio);
409 BIO_free_all(data_bio);
410 BIO_free_all(out_bio);
415 static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
416 const char *policy, int no_nonce, int cert)
419 TS_REQ *ts_req = NULL;
421 TS_MSG_IMPRINT *msg_imprint = NULL;
422 X509_ALGOR *algo = NULL;
423 unsigned char *data = NULL;
424 ASN1_OBJECT *policy_obj = NULL;
425 ASN1_INTEGER *nonce_asn1 = NULL;
427 if (md == NULL && (md = EVP_get_digestbyname("sha1")) == NULL)
429 if ((ts_req = TS_REQ_new()) == NULL)
431 if (!TS_REQ_set_version(ts_req, 1))
433 if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
435 if ((algo = X509_ALGOR_new()) == NULL)
437 if ((algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))) == NULL)
439 if ((algo->parameter = ASN1_TYPE_new()) == NULL)
441 algo->parameter->type = V_ASN1_NULL;
442 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
444 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
446 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
448 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
450 if (policy && (policy_obj = txt2obj(policy)) == NULL)
452 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
455 /* Setting nonce if requested. */
456 if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
458 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
460 if (!TS_REQ_set_cert_req(ts_req, cert))
468 BIO_printf(bio_err, "could not create query\n");
469 ERR_print_errors(bio_err);
471 TS_MSG_IMPRINT_free(msg_imprint);
472 X509_ALGOR_free(algo);
474 ASN1_OBJECT_free(policy_obj);
475 ASN1_INTEGER_free(nonce_asn1);
479 static int create_digest(BIO *input, const char *digest, const EVP_MD *md,
480 unsigned char **md_value)
484 EVP_MD_CTX *md_ctx = NULL;
486 md_value_len = EVP_MD_size(md);
487 if (md_value_len < 0)
491 unsigned char buffer[4096];
494 md_ctx = EVP_MD_CTX_new();
497 *md_value = app_malloc(md_value_len, "digest buffer");
498 if (!EVP_DigestInit(md_ctx, md))
500 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
501 if (!EVP_DigestUpdate(md_ctx, buffer, length))
504 if (!EVP_DigestFinal(md_ctx, *md_value, NULL))
506 md_value_len = EVP_MD_size(md);
509 *md_value = OPENSSL_hexstr2buf(digest, &digest_len);
510 if (!*md_value || md_value_len != digest_len) {
511 OPENSSL_free(*md_value);
513 BIO_printf(bio_err, "bad digest, %d bytes "
514 "must be specified\n", md_value_len);
520 EVP_MD_CTX_free(md_ctx);
524 static ASN1_INTEGER *create_nonce(int bits)
526 unsigned char buf[20];
527 ASN1_INTEGER *nonce = NULL;
528 int len = (bits - 1) / 8 + 1;
531 if (len > (int)sizeof(buf))
533 if (RAND_bytes(buf, len) <= 0)
536 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
537 for (i = 0; i < len && !buf[i]; ++i)
539 if ((nonce = ASN1_INTEGER_new()) == NULL)
541 OPENSSL_free(nonce->data);
542 nonce->length = len - i;
543 nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
544 memcpy(nonce->data, buf + i, nonce->length);
548 BIO_printf(bio_err, "could not create nonce\n");
549 ASN1_INTEGER_free(nonce);
554 * Reply-related method definitions.
557 static int reply_command(CONF *conf, const char *section, const char *engine,
558 const char *queryfile, const char *passin, const char *inkey,
559 const EVP_MD *md, const char *signer, const char *chain,
560 const char *policy, const char *in, int token_in,
561 const char *out, int token_out, int text)
564 TS_RESP *response = NULL;
566 BIO *query_bio = NULL;
567 BIO *inkey_bio = NULL;
568 BIO *signer_bio = NULL;
572 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
575 response = read_PKCS7(in_bio);
577 response = d2i_TS_RESP_bio(in_bio, NULL);
580 response = create_response(conf, section, engine, queryfile,
581 passin, inkey, md, signer, chain, policy);
582 if (response != NULL)
583 BIO_printf(bio_err, "Response has been generated.\n");
585 BIO_printf(bio_err, "Response is not generated.\n");
587 if (response == NULL)
590 /* Write response. */
592 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
595 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
596 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
599 if (!TS_RESP_print_bio(out_bio, response))
603 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
606 PKCS7 *token = TS_RESP_get_token(response);
607 if (!i2d_PKCS7_bio(out_bio, token))
610 if (!i2d_TS_RESP_bio(out_bio, response))
618 ERR_print_errors(bio_err);
619 BIO_free_all(in_bio);
620 BIO_free_all(query_bio);
621 BIO_free_all(inkey_bio);
622 BIO_free_all(signer_bio);
623 BIO_free_all(out_bio);
624 TS_RESP_free(response);
628 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
629 static TS_RESP *read_PKCS7(BIO *in_bio)
633 TS_TST_INFO *tst_info = NULL;
634 TS_RESP *resp = NULL;
635 TS_STATUS_INFO *si = NULL;
637 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
639 if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
641 if ((resp = TS_RESP_new()) == NULL)
643 if ((si = TS_STATUS_INFO_new()) == NULL)
645 if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
647 if (!TS_RESP_set_status_info(resp, si))
649 TS_RESP_set_tst_info(resp, token, tst_info);
650 token = NULL; /* Ownership is lost. */
651 tst_info = NULL; /* Ownership is lost. */
656 TS_TST_INFO_free(tst_info);
661 TS_STATUS_INFO_free(si);
665 static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
666 const char *queryfile, const char *passin,
667 const char *inkey, const EVP_MD *md, const char *signer,
668 const char *chain, const char *policy)
671 TS_RESP *response = NULL;
672 BIO *query_bio = NULL;
673 TS_RESP_CTX *resp_ctx = NULL;
675 if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
677 if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
679 if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
681 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
683 # ifndef OPENSSL_NO_ENGINE
684 if (!TS_CONF_set_crypto_device(conf, section, engine))
687 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
689 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
691 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
695 if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
697 } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
701 if (!TS_CONF_set_ess_cert_id_digest(conf, section, resp_ctx))
703 if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
705 if (!TS_CONF_set_policies(conf, section, resp_ctx))
707 if (!TS_CONF_set_digests(conf, section, resp_ctx))
709 if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
711 if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
713 if (!TS_CONF_set_ordering(conf, section, resp_ctx))
715 if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
717 if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
719 if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
725 TS_RESP_free(response);
728 TS_RESP_CTX_free(resp_ctx);
729 BIO_free_all(query_bio);
733 static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
735 const char *serial_file = (const char *)data;
736 ASN1_INTEGER *serial = next_serial(serial_file);
738 if (serial == NULL) {
739 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
740 "Error during serial number "
742 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
744 save_ts_serial(serial_file, serial);
750 static ASN1_INTEGER *next_serial(const char *serialfile)
754 ASN1_INTEGER *serial = NULL;
757 if ((serial = ASN1_INTEGER_new()) == NULL)
760 if ((in = BIO_new_file(serialfile, "r")) == NULL) {
762 BIO_printf(bio_err, "Warning: could not open file %s for "
763 "reading, using serial number: 1\n", serialfile);
764 if (!ASN1_INTEGER_set(serial, 1))
768 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
769 BIO_printf(bio_err, "unable to load number from %s\n",
773 if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
775 ASN1_INTEGER_free(serial);
777 if (!BN_add_word(bn, 1))
779 if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
786 ASN1_INTEGER_free(serial);
794 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
799 if ((out = BIO_new_file(serialfile, "w")) == NULL)
801 if (i2a_ASN1_INTEGER(out, serial) <= 0)
803 if (BIO_puts(out, "\n") <= 0)
808 BIO_printf(bio_err, "could not save serial number to %s\n",
816 * Verify-related method definitions.
819 static int verify_command(const char *data, const char *digest, const char *queryfile,
820 const char *in, int token_in,
821 const char *CApath, const char *CAfile, const char *untrusted,
822 X509_VERIFY_PARAM *vpm)
826 TS_RESP *response = NULL;
827 TS_VERIFY_CTX *verify_ctx = NULL;
830 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
833 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
836 if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
840 if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
841 CApath, CAfile, untrusted,
846 ? TS_RESP_verify_token(verify_ctx, token)
847 : TS_RESP_verify_response(verify_ctx, response);
850 printf("Verification: ");
855 ERR_print_errors(bio_err);
858 BIO_free_all(in_bio);
860 TS_RESP_free(response);
861 TS_VERIFY_CTX_free(verify_ctx);
865 static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
866 const char *queryfile,
867 const char *CApath, const char *CAfile,
868 const char *untrusted,
869 X509_VERIFY_PARAM *vpm)
871 TS_VERIFY_CTX *ctx = NULL;
873 TS_REQ *request = NULL;
877 if (data != NULL || digest != NULL) {
878 if ((ctx = TS_VERIFY_CTX_new()) == NULL)
880 f = TS_VFY_VERSION | TS_VFY_SIGNER;
885 if ((out = BIO_new_file(data, "rb")) == NULL)
887 if (TS_VERIFY_CTX_set_data(ctx, out) == NULL) {
891 } else if (digest != NULL) {
893 unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
895 if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
896 BIO_printf(bio_err, "invalid digest string\n");
901 } else if (queryfile != NULL) {
902 if ((input = BIO_new_file(queryfile, "rb")) == NULL)
904 if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
906 if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
912 /* Add the signature verification flag and arguments. */
913 TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
915 /* Initialising the X509_STORE object. */
916 if (TS_VERIFY_CTX_set_store(ctx, create_cert_store(CApath, CAfile, vpm))
920 /* Loading untrusted certificates. */
922 && TS_VERIFY_CTS_set_certs(ctx, TS_CONF_load_certs(untrusted)) == NULL)
928 TS_VERIFY_CTX_free(ctx);
932 TS_REQ_free(request);
936 static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
937 X509_VERIFY_PARAM *vpm)
939 X509_STORE *cert_ctx = NULL;
940 X509_LOOKUP *lookup = NULL;
943 cert_ctx = X509_STORE_new();
944 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
945 if (CApath != NULL) {
946 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
947 if (lookup == NULL) {
948 BIO_printf(bio_err, "memory allocation failure\n");
951 i = X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM);
953 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
958 if (CAfile != NULL) {
959 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
960 if (lookup == NULL) {
961 BIO_printf(bio_err, "memory allocation failure\n");
964 i = X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM);
966 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
972 X509_STORE_set1_param(cert_ctx, vpm);
977 X509_STORE_free(cert_ctx);
981 static int verify_cb(int ok, X509_STORE_CTX *ctx)
985 #endif /* ndef OPENSSL_NO_TS */