2 * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/bio.h>
64 #include <openssl/err.h>
65 #include <openssl/pem.h>
66 #include <openssl/rand.h>
67 #include <openssl/ts.h>
68 #include <openssl/bn.h>
70 /* Request nonce length, in bits (must be a multiple of 8). */
71 #define NONCE_LENGTH 64
73 /* Name of config entry that defines the OID file. */
74 #define ENV_OID_FILE "oid_file"
76 /* Is |EXACTLY_ONE| of three pointers set? */
77 #define EXACTLY_ONE(a, b, c) \
78 (( a && !b && !c) || \
82 static ASN1_OBJECT *txt2obj(const char *oid);
83 static CONF *load_config_file(const char *configfile);
85 /* Query related functions. */
86 static int query_command(const char *data, char *digest,
87 const EVP_MD *md, const char *policy, int no_nonce,
88 int cert, const char *in, const char *out, int text);
89 static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
90 const char *policy, int no_nonce, int cert);
91 static int create_digest(BIO *input, char *digest,
92 const EVP_MD *md, unsigned char **md_value);
93 static ASN1_INTEGER *create_nonce(int bits);
95 /* Reply related functions. */
96 static int reply_command(CONF *conf, char *section, char *engine,
97 char *queryfile, char *passin, char *inkey,
98 char *signer, char *chain, const char *policy,
99 char *in, int token_in, char *out, int token_out,
101 static TS_RESP *read_PKCS7(BIO *in_bio);
102 static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
103 char *queryfile, char *passin,
104 char *inkey, char *signer, char *chain,
106 static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data);
107 static ASN1_INTEGER *next_serial(const char *serialfile);
108 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
110 /* Verify related functions. */
111 static int verify_command(char *data, char *digest, char *queryfile,
112 char *in, int token_in,
113 char *CApath, char *CAfile, char *untrusted);
114 static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
116 char *CApath, char *CAfile,
118 static X509_STORE *create_cert_store(char *CApath, char *CAfile);
119 static int verify_cb(int ok, X509_STORE_CTX *ctx);
121 typedef enum OPTION_choice {
122 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
123 OPT_ENGINE, OPT_CONFIG, OPT_SECTION, OPT_QUERY, OPT_DATA,
124 OPT_DIGEST, OPT_RAND, OPT_POLICY, OPT_NO_NONCE, OPT_CERT,
125 OPT_IN, OPT_TOKEN_IN, OPT_OUT, OPT_TOKEN_OUT, OPT_TEXT,
126 OPT_REPLY, OPT_QUERYFILE, OPT_PASSIN, OPT_INKEY, OPT_SIGNER,
127 OPT_CHAIN, OPT_VERIFY, OPT_CAPATH, OPT_CAFILE, OPT_UNTRUSTED,
131 OPTIONS ts_options[] = {
132 {"help", OPT_HELP, '-', "Display this summary"},
133 {"config", OPT_CONFIG, '<', "Configuration file"},
134 {"section", OPT_SECTION, 's', "Section to use within config file"},
135 {"query", OPT_QUERY, '-', "Generate a TS query"},
136 {"data", OPT_DATA, '<', "File to hash"},
137 {"digest", OPT_DIGEST, 's', "Digest (as a hex string)"},
138 {"rand", OPT_RAND, 's',
139 "Load the file(s) into the random number generator"},
140 {"policy", OPT_POLICY, 's', "Policy OID to use"},
141 {"no_nonce", OPT_NO_NONCE, '-', "Do not include a nonce"},
142 {"cert", OPT_CERT, '-', "Put cert request into query"},
143 {"in", OPT_IN, '<', "Input file"},
144 {"token_in", OPT_TOKEN_IN, '-', "Input is a PKCS#7 file"},
145 {"out", OPT_OUT, '>', "Output file"},
146 {"token_out", OPT_TOKEN_OUT, '-', "Output is a PKCS#7 file"},
147 {"text", OPT_TEXT, '-', "Output text (not DER)"},
148 {"reply", OPT_REPLY, '-', "Generate a TS reply"},
149 {"queryfile", OPT_QUERYFILE, '<', "File containing a TS query"},
150 {"passin", OPT_PASSIN, 's'},
151 {"inkey", OPT_INKEY, '<', "File with private key for reply"},
152 {"signer", OPT_SIGNER, 's'},
153 {"chain", OPT_CHAIN, '<', "File with signer CA chain"},
154 {"verify", OPT_VERIFY, '-', "Verify a TS response"},
155 {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"},
156 {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"},
157 {"untrusted", OPT_UNTRUSTED, '<', "File with untrusted certs"},
158 {"", OPT_MD, '-', "Any supported digest"},
159 #ifndef OPENSSL_NO_ENGINE
160 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
166 * This comand is so complex, special help is needed.
168 static char* opt_helplist[] = {
170 "ts -query [-rand file...] [-config file] [-data file]",
171 " [-digest hexstring] [-policy oid] [-no_nonce] [-cert]",
172 " [-in file] [-out file] [-text]",
174 "ts -reply [-config file] [-section tsa_section]",
175 " [-queryfile file] [-passin password]",
176 " [-signer tsa_cert.pem] [-inkey private_key.pem]",
177 " [-chain certs_file.pem] [-policy oid]",
178 " [-in file] [-token_in] [-out file] [-token_out]",
179 #ifndef OPENSSL_NO_ENGINE
182 " [-text] [-engine id]",
185 "ts -verify -CApath dir -CAfile file.pem -untrusted file.pem",
186 " [-data file] [-digest hexstring]",
187 " [-queryfile file] -in file [-token_in]",
191 int ts_main(int argc, char **argv)
194 char *CAfile = NULL, *untrusted = NULL, *engine = NULL, *prog, **helpp;
195 char *configfile = default_config_file;
196 char *section = NULL, *password = NULL;
197 char *data = NULL, *digest = NULL, *rnd = NULL, *policy = NULL;
198 char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL;
199 char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL;
200 const EVP_MD *md = NULL;
201 OPTION_CHOICE o, mode = OPT_ERR;
202 int ret = 1, no_nonce = 0, cert = 0, text = 0;
203 /* Input is ContentInfo instead of TimeStampResp. */
205 /* Output is ContentInfo instead of TimeStampResp. */
208 prog = opt_init(argc, argv, ts_options);
209 while ((o = opt_next()) != OPT_EOF) {
214 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
217 opt_help(ts_options);
218 for (helpp = opt_helplist; *helpp; ++helpp)
219 BIO_printf(bio_err, "%s\n", *helpp);
223 configfile = opt_arg();
269 queryfile = opt_arg();
290 untrusted = opt_arg();
296 if (!opt_md(opt_unknown(), &md))
301 argc = opt_num_rest();
303 if (mode == OPT_ERR || argc != 0)
306 /* Seed the random number generator if it is going to be used. */
307 if (mode == OPT_QUERY && !no_nonce) {
308 if (!app_RAND_load_file(NULL, 1) && rnd == NULL)
309 BIO_printf(bio_err, "warning, not much extra random "
310 "data, consider using the -rand option\n");
312 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
313 app_RAND_load_files(rnd));
316 if (mode == OPT_REPLY && passin &&
317 !app_passwd(passin, NULL, &password, NULL)) {
318 BIO_printf(bio_err, "Error getting password.\n");
322 conf = load_config_file(configfile);
323 if (!app_load_modules(conf))
326 /* Check parameter consistency and execute the appropriate function. */
332 if ((data != NULL) && (digest != NULL))
334 ret = !query_command(data, digest, md, policy, no_nonce, cert,
338 if ((in != NULL) && (queryfile != NULL))
341 if ((conf == NULL) || (token_in != 0))
344 ret = !reply_command(conf, section, engine, queryfile,
345 password, inkey, signer, chain, policy,
346 in, token_in, out, token_out, text);
349 if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
351 ret = !verify_command(data, digest, queryfile, in, token_in,
352 CApath, CAfile, untrusted);
356 app_RAND_write_file(NULL);
358 OPENSSL_free(password);
364 * Configuration file-related function definitions.
367 static ASN1_OBJECT *txt2obj(const char *oid)
369 ASN1_OBJECT *oid_obj = NULL;
371 if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
372 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
377 static CONF *load_config_file(const char *configfile)
379 CONF *conf = app_load_config(configfile);
384 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
385 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
387 BIO *oid_bio = BIO_new_file(p, "r");
389 ERR_print_errors(bio_err);
391 OBJ_create_objects(oid_bio);
392 BIO_free_all(oid_bio);
396 if (!add_oid_section(conf))
397 ERR_print_errors(bio_err);
403 * Query-related method definitions.
405 static int query_command(const char *data, char *digest, const EVP_MD *md,
406 const char *policy, int no_nonce,
407 int cert, const char *in, const char *out, int text)
410 TS_REQ *query = NULL;
412 BIO *data_bio = NULL;
415 /* Build query object. */
417 if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
419 query = d2i_TS_REQ_bio(in_bio, NULL);
422 && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
424 query = create_query(data_bio, digest, md, policy, no_nonce, cert);
430 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
432 if (!TS_REQ_print_bio(out_bio, query))
435 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
437 if (!i2d_TS_REQ_bio(out_bio, query))
444 ERR_print_errors(bio_err);
445 BIO_free_all(in_bio);
446 BIO_free_all(data_bio);
447 BIO_free_all(out_bio);
452 static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
453 const char *policy, int no_nonce, int cert)
456 TS_REQ *ts_req = NULL;
458 TS_MSG_IMPRINT *msg_imprint = NULL;
459 X509_ALGOR *algo = NULL;
460 unsigned char *data = NULL;
461 ASN1_OBJECT *policy_obj = NULL;
462 ASN1_INTEGER *nonce_asn1 = NULL;
464 if (md == NULL && (md = EVP_get_digestbyname("sha1")) == NULL)
466 if ((ts_req = TS_REQ_new()) == NULL)
468 if (!TS_REQ_set_version(ts_req, 1))
470 if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
472 if ((algo = X509_ALGOR_new()) == NULL)
474 if ((algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))) == NULL)
476 if ((algo->parameter = ASN1_TYPE_new()) == NULL)
478 algo->parameter->type = V_ASN1_NULL;
479 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
481 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
483 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
485 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
487 if (policy && (policy_obj = txt2obj(policy)) == NULL)
489 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
492 /* Setting nonce if requested. */
493 if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
495 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
497 if (!TS_REQ_set_cert_req(ts_req, cert))
505 BIO_printf(bio_err, "could not create query\n");
506 ERR_print_errors(bio_err);
508 TS_MSG_IMPRINT_free(msg_imprint);
509 X509_ALGOR_free(algo);
511 ASN1_OBJECT_free(policy_obj);
512 ASN1_INTEGER_free(nonce_asn1);
516 static int create_digest(BIO *input, char *digest, const EVP_MD *md,
517 unsigned char **md_value)
521 md_value_len = EVP_MD_size(md);
522 if (md_value_len < 0)
527 unsigned char buffer[4096];
530 *md_value = app_malloc(md_value_len, "digest buffer");
531 EVP_DigestInit(&md_ctx, md);
532 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
533 EVP_DigestUpdate(&md_ctx, buffer, length);
535 if (!EVP_DigestFinal(&md_ctx, *md_value, NULL))
539 *md_value = string_to_hex(digest, &digest_len);
540 if (!*md_value || md_value_len != digest_len) {
541 OPENSSL_free(*md_value);
543 BIO_printf(bio_err, "bad digest, %d bytes "
544 "must be specified\n", md_value_len);
551 static ASN1_INTEGER *create_nonce(int bits)
553 unsigned char buf[20];
554 ASN1_INTEGER *nonce = NULL;
555 int len = (bits - 1) / 8 + 1;
558 if (len > (int)sizeof(buf))
560 if (RAND_bytes(buf, len) <= 0)
563 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
564 for (i = 0; i < len && !buf[i]; ++i)
566 if ((nonce = ASN1_INTEGER_new()) == NULL)
568 OPENSSL_free(nonce->data);
569 nonce->length = len - i;
570 nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
571 memcpy(nonce->data, buf + i, nonce->length);
575 BIO_printf(bio_err, "could not create nonce\n");
576 ASN1_INTEGER_free(nonce);
581 * Reply-related method definitions.
584 static int reply_command(CONF *conf, char *section, char *engine,
585 char *queryfile, char *passin, char *inkey,
586 char *signer, char *chain, const char *policy,
587 char *in, int token_in,
588 char *out, int token_out, int text)
591 TS_RESP *response = NULL;
593 BIO *query_bio = NULL;
594 BIO *inkey_bio = NULL;
595 BIO *signer_bio = NULL;
599 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
602 response = read_PKCS7(in_bio);
604 response = d2i_TS_RESP_bio(in_bio, NULL);
607 response = create_response(conf, section, engine, queryfile,
608 passin, inkey, signer, chain, policy);
610 BIO_printf(bio_err, "Response has been generated.\n");
612 BIO_printf(bio_err, "Response is not generated.\n");
614 if (response == NULL)
617 /* Write response. */
619 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
622 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
623 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
626 if (!TS_RESP_print_bio(out_bio, response))
630 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
633 PKCS7 *token = TS_RESP_get_token(response);
634 if (!i2d_PKCS7_bio(out_bio, token))
637 if (!i2d_TS_RESP_bio(out_bio, response))
645 ERR_print_errors(bio_err);
646 BIO_free_all(in_bio);
647 BIO_free_all(query_bio);
648 BIO_free_all(inkey_bio);
649 BIO_free_all(signer_bio);
650 BIO_free_all(out_bio);
651 TS_RESP_free(response);
655 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
656 static TS_RESP *read_PKCS7(BIO *in_bio)
660 TS_TST_INFO *tst_info = NULL;
661 TS_RESP *resp = NULL;
662 TS_STATUS_INFO *si = NULL;
664 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
666 if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
668 if ((resp = TS_RESP_new()) == NULL)
670 if ((si = TS_STATUS_INFO_new()) == NULL)
672 if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
674 if (!TS_RESP_set_status_info(resp, si))
676 TS_RESP_set_tst_info(resp, token, tst_info);
677 token = NULL; /* Ownership is lost. */
678 tst_info = NULL; /* Ownership is lost. */
683 TS_TST_INFO_free(tst_info);
688 TS_STATUS_INFO_free(si);
692 static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
693 char *queryfile, char *passin,
694 char *inkey, char *signer, char *chain,
698 TS_RESP *response = NULL;
699 BIO *query_bio = NULL;
700 TS_RESP_CTX *resp_ctx = NULL;
702 if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
704 if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
706 if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
708 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
710 #ifndef OPENSSL_NO_ENGINE
711 if (!TS_CONF_set_crypto_device(conf, section, engine))
714 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
716 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
718 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
720 if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
722 if (!TS_CONF_set_policies(conf, section, resp_ctx))
724 if (!TS_CONF_set_digests(conf, section, resp_ctx))
726 if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
728 if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
730 if (!TS_CONF_set_ordering(conf, section, resp_ctx))
732 if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
734 if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
736 if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
742 TS_RESP_free(response);
745 TS_RESP_CTX_free(resp_ctx);
746 BIO_free_all(query_bio);
750 static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
752 const char *serial_file = (const char *)data;
753 ASN1_INTEGER *serial = next_serial(serial_file);
756 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
757 "Error during serial number "
759 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
761 save_ts_serial(serial_file, serial);
766 static ASN1_INTEGER *next_serial(const char *serialfile)
770 ASN1_INTEGER *serial = NULL;
773 if ((serial = ASN1_INTEGER_new()) == NULL)
776 if ((in = BIO_new_file(serialfile, "r")) == NULL) {
778 BIO_printf(bio_err, "Warning: could not open file %s for "
779 "reading, using serial number: 1\n", serialfile);
780 if (!ASN1_INTEGER_set(serial, 1))
784 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
785 BIO_printf(bio_err, "unable to load number from %s\n",
789 if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
791 ASN1_INTEGER_free(serial);
793 if (!BN_add_word(bn, 1))
795 if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
802 ASN1_INTEGER_free(serial);
810 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
815 if ((out = BIO_new_file(serialfile, "w")) == NULL)
817 if (i2a_ASN1_INTEGER(out, serial) <= 0)
819 if (BIO_puts(out, "\n") <= 0)
824 BIO_printf(bio_err, "could not save serial number to %s\n",
832 * Verify-related method definitions.
835 static int verify_command(char *data, char *digest, char *queryfile,
836 char *in, int token_in,
837 char *CApath, char *CAfile, char *untrusted)
841 TS_RESP *response = NULL;
842 TS_VERIFY_CTX *verify_ctx = NULL;
845 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
848 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
851 if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
855 if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
856 CApath, CAfile, untrusted)) == NULL)
860 ? TS_RESP_verify_token(verify_ctx, token)
861 : TS_RESP_verify_response(verify_ctx, response);
864 printf("Verification: ");
869 ERR_print_errors(bio_err);
872 BIO_free_all(in_bio);
874 TS_RESP_free(response);
875 TS_VERIFY_CTX_free(verify_ctx);
879 static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
881 char *CApath, char *CAfile,
884 TS_VERIFY_CTX *ctx = NULL;
886 TS_REQ *request = NULL;
890 if (data != NULL || digest != NULL) {
891 if ((ctx = TS_VERIFY_CTX_new()) == NULL)
893 f = TS_VFY_VERSION | TS_VFY_SIGNER;
896 if (TS_VERIFY_CTX_set_data(ctx, BIO_new_file(data, "rb")) == NULL)
898 } else if (digest != NULL) {
900 unsigned char *hexstr = string_to_hex(digest, &imprint_len);
902 if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
903 BIO_printf(bio_err, "invalid digest string\n");
908 } else if (queryfile != NULL) {
909 if ((input = BIO_new_file(queryfile, "rb")) == NULL)
911 if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
913 if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
918 /* Add the signature verification flag and arguments. */
919 TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
921 /* Initialising the X509_STORE object. */
922 if (TS_VERIFY_CTX_set_store(ctx, create_cert_store(CApath, CAfile))
926 /* Loading untrusted certificates. */
928 && TS_VERIFY_CTS_set_certs(ctx, TS_CONF_load_certs(untrusted)) == NULL)
934 TS_VERIFY_CTX_free(ctx);
938 TS_REQ_free(request);
942 static X509_STORE *create_cert_store(char *CApath, char *CAfile)
944 X509_STORE *cert_ctx = NULL;
945 X509_LOOKUP *lookup = NULL;
948 cert_ctx = X509_STORE_new();
949 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
951 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
952 if (lookup == NULL) {
953 BIO_printf(bio_err, "memory allocation failure\n");
956 i = X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM);
958 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
964 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
965 if (lookup == NULL) {
966 BIO_printf(bio_err, "memory allocation failure\n");
969 i = X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM);
971 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
978 X509_STORE_free(cert_ctx);
982 static int verify_cb(int ok, X509_STORE_CTX *ctx)