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).
59 #include <openssl/opensslconf.h>
61 NON_EMPTY_TRANSLATION_UNIT
67 # include <openssl/bio.h>
68 # include <openssl/err.h>
69 # include <openssl/pem.h>
70 # include <openssl/rand.h>
71 # include <openssl/ts.h>
72 # include <openssl/bn.h>
74 /* Request nonce length, in bits (must be a multiple of 8). */
75 # define NONCE_LENGTH 64
77 /* Name of config entry that defines the OID file. */
78 # define ENV_OID_FILE "oid_file"
80 /* Is |EXACTLY_ONE| of three pointers set? */
81 # define EXACTLY_ONE(a, b, c) \
82 (( a && !b && !c) || \
86 static ASN1_OBJECT *txt2obj(const char *oid);
87 static CONF *load_config_file(const char *configfile);
89 /* Query related functions. */
90 static int query_command(const char *data, char *digest,
91 const EVP_MD *md, const char *policy, int no_nonce,
92 int cert, const char *in, const char *out, int text);
93 static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
94 const char *policy, int no_nonce, int cert);
95 static int create_digest(BIO *input, char *digest,
96 const EVP_MD *md, unsigned char **md_value);
97 static ASN1_INTEGER *create_nonce(int bits);
99 /* Reply related functions. */
100 static int reply_command(CONF *conf, char *section, char *engine,
101 char *queryfile, char *passin, char *inkey,
102 const EVP_MD *md, char *signer, char *chain,
103 const char *policy, char *in, int token_in,
104 char *out, int token_out, int text);
105 static TS_RESP *read_PKCS7(BIO *in_bio);
106 static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
107 char *queryfile, char *passin,
108 char *inkey, const EVP_MD *md, char *signer,
109 char *chain, const char *policy);
110 static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data);
111 static ASN1_INTEGER *next_serial(const char *serialfile);
112 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
114 /* Verify related functions. */
115 static int verify_command(char *data, char *digest, char *queryfile,
116 char *in, int token_in,
117 char *CApath, char *CAfile, char *untrusted,
118 X509_VERIFY_PARAM *vpm);
119 static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
121 char *CApath, char *CAfile,
123 X509_VERIFY_PARAM *vpm);
124 static X509_STORE *create_cert_store(char *CApath, char *CAfile,
125 X509_VERIFY_PARAM *vpm);
126 static int verify_cb(int ok, X509_STORE_CTX *ctx);
128 typedef enum OPTION_choice {
129 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
130 OPT_ENGINE, OPT_CONFIG, OPT_SECTION, OPT_QUERY, OPT_DATA,
131 OPT_DIGEST, OPT_RAND, OPT_TSPOLICY, OPT_NO_NONCE, OPT_CERT,
132 OPT_IN, OPT_TOKEN_IN, OPT_OUT, OPT_TOKEN_OUT, OPT_TEXT,
133 OPT_REPLY, OPT_QUERYFILE, OPT_PASSIN, OPT_INKEY, OPT_SIGNER,
134 OPT_CHAIN, OPT_VERIFY, OPT_CAPATH, OPT_CAFILE, OPT_UNTRUSTED,
138 OPTIONS ts_options[] = {
139 {"help", OPT_HELP, '-', "Display this summary"},
140 {"config", OPT_CONFIG, '<', "Configuration file"},
141 {"section", OPT_SECTION, 's', "Section to use within config file"},
142 {"query", OPT_QUERY, '-', "Generate a TS query"},
143 {"data", OPT_DATA, '<', "File to hash"},
144 {"digest", OPT_DIGEST, 's', "Digest (as a hex string)"},
145 {"rand", OPT_RAND, 's',
146 "Load the file(s) into the random number generator"},
147 {"tspolicy", OPT_TSPOLICY, 's', "Policy OID to use"},
148 {"no_nonce", OPT_NO_NONCE, '-', "Do not include a nonce"},
149 {"cert", OPT_CERT, '-', "Put cert request into query"},
150 {"in", OPT_IN, '<', "Input file"},
151 {"token_in", OPT_TOKEN_IN, '-', "Input is a PKCS#7 file"},
152 {"out", OPT_OUT, '>', "Output file"},
153 {"token_out", OPT_TOKEN_OUT, '-', "Output is a PKCS#7 file"},
154 {"text", OPT_TEXT, '-', "Output text (not DER)"},
155 {"reply", OPT_REPLY, '-', "Generate a TS reply"},
156 {"queryfile", OPT_QUERYFILE, '<', "File containing a TS query"},
157 {"passin", OPT_PASSIN, 's'},
158 {"inkey", OPT_INKEY, '<', "File with private key for reply"},
159 {"signer", OPT_SIGNER, 's'},
160 {"chain", OPT_CHAIN, '<', "File with signer CA chain"},
161 {"verify", OPT_VERIFY, '-', "Verify a TS response"},
162 {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"},
163 {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"},
164 {"untrusted", OPT_UNTRUSTED, '<', "File with untrusted certs"},
165 {"", OPT_MD, '-', "Any supported digest"},
166 # ifndef OPENSSL_NO_ENGINE
167 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
169 {OPT_HELP_STR, 1, '-', "\nOptions specific to 'ts -verify': \n"},
171 {OPT_HELP_STR, 1, '-', "\n"},
176 * This comand is so complex, special help is needed.
178 static char* opt_helplist[] = {
180 "ts -query [-rand file...] [-config file] [-data file]",
181 " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]",
182 " [-in file] [-out file] [-text]",
184 "ts -reply [-config file] [-section tsa_section]",
185 " [-queryfile file] [-passin password]",
186 " [-signer tsa_cert.pem] [-inkey private_key.pem]",
187 " [-chain certs_file.pem] [-tspolicy oid]",
188 " [-in file] [-token_in] [-out file] [-token_out]",
189 # ifndef OPENSSL_NO_ENGINE
192 " [-text] [-engine id]",
195 "ts -verify -CApath dir -CAfile file.pem -untrusted file.pem",
196 " [-data file] [-digest hexstring]",
197 " [-queryfile file] -in file [-token_in]",
198 " [[options specific to 'ts -verify']]",
202 int ts_main(int argc, char **argv)
205 char *CAfile = NULL, *untrusted = NULL, *engine = NULL, *prog, **helpp;
206 char *configfile = default_config_file;
207 char *section = NULL, *password = NULL;
208 char *data = NULL, *digest = NULL, *rnd = NULL, *policy = NULL;
209 char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL;
210 char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL;
211 const EVP_MD *md = NULL;
212 OPTION_CHOICE o, mode = OPT_ERR;
213 int ret = 1, no_nonce = 0, cert = 0, text = 0;
215 X509_VERIFY_PARAM *vpm = NULL;
216 /* Input is ContentInfo instead of TimeStampResp. */
218 /* Output is ContentInfo instead of TimeStampResp. */
221 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
224 prog = opt_init(argc, argv, ts_options);
225 while ((o = opt_next()) != OPT_EOF) {
230 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
233 opt_help(ts_options);
234 for (helpp = opt_helplist; *helpp; ++helpp)
235 BIO_printf(bio_err, "%s\n", *helpp);
239 configfile = opt_arg();
285 queryfile = opt_arg();
306 untrusted = opt_arg();
312 if (!opt_md(opt_unknown(), &md))
316 if (!opt_verify(o, vpm))
322 argc = opt_num_rest();
323 if (mode == OPT_ERR || argc != 0)
326 /* Seed the random number generator if it is going to be used. */
327 if (mode == OPT_QUERY && !no_nonce) {
328 if (!app_RAND_load_file(NULL, 1) && rnd == NULL)
329 BIO_printf(bio_err, "warning, not much extra random "
330 "data, consider using the -rand option\n");
332 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
333 app_RAND_load_files(rnd));
336 if (mode == OPT_REPLY && passin &&
337 !app_passwd(passin, NULL, &password, NULL)) {
338 BIO_printf(bio_err, "Error getting password.\n");
342 conf = load_config_file(configfile);
343 if (!app_load_modules(conf))
346 /* Check parameter consistency and execute the appropriate function. */
354 if ((data != NULL) && (digest != NULL))
356 ret = !query_command(data, digest, md, policy, no_nonce, cert,
362 if ((in != NULL) && (queryfile != NULL))
365 if ((conf == NULL) || (token_in != 0))
368 ret = !reply_command(conf, section, engine, queryfile,
369 password, inkey, md, signer, chain, policy,
370 in, token_in, out, token_out, text);
373 if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
375 ret = !verify_command(data, digest, queryfile, in, token_in,
376 CApath, CAfile, untrusted,
377 vpmtouched ? vpm : NULL);
381 X509_VERIFY_PARAM_free(vpm);
382 app_RAND_write_file(NULL);
384 OPENSSL_free(password);
389 * Configuration file-related function definitions.
392 static ASN1_OBJECT *txt2obj(const char *oid)
394 ASN1_OBJECT *oid_obj = NULL;
396 if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
397 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
402 static CONF *load_config_file(const char *configfile)
404 CONF *conf = app_load_config(configfile);
409 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
410 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
412 BIO *oid_bio = BIO_new_file(p, "r");
414 ERR_print_errors(bio_err);
416 OBJ_create_objects(oid_bio);
417 BIO_free_all(oid_bio);
421 if (!add_oid_section(conf))
422 ERR_print_errors(bio_err);
428 * Query-related method definitions.
430 static int query_command(const char *data, char *digest, const EVP_MD *md,
431 const char *policy, int no_nonce,
432 int cert, const char *in, const char *out, int text)
435 TS_REQ *query = NULL;
437 BIO *data_bio = NULL;
440 /* Build query object. */
442 if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
444 query = d2i_TS_REQ_bio(in_bio, NULL);
447 && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
449 query = create_query(data_bio, digest, md, policy, no_nonce, cert);
455 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
457 if (!TS_REQ_print_bio(out_bio, query))
460 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
462 if (!i2d_TS_REQ_bio(out_bio, query))
469 ERR_print_errors(bio_err);
470 BIO_free_all(in_bio);
471 BIO_free_all(data_bio);
472 BIO_free_all(out_bio);
477 static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
478 const char *policy, int no_nonce, int cert)
481 TS_REQ *ts_req = NULL;
483 TS_MSG_IMPRINT *msg_imprint = NULL;
484 X509_ALGOR *algo = NULL;
485 unsigned char *data = NULL;
486 ASN1_OBJECT *policy_obj = NULL;
487 ASN1_INTEGER *nonce_asn1 = NULL;
489 if (md == NULL && (md = EVP_get_digestbyname("sha1")) == NULL)
491 if ((ts_req = TS_REQ_new()) == NULL)
493 if (!TS_REQ_set_version(ts_req, 1))
495 if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
497 if ((algo = X509_ALGOR_new()) == NULL)
499 if ((algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))) == NULL)
501 if ((algo->parameter = ASN1_TYPE_new()) == NULL)
503 algo->parameter->type = V_ASN1_NULL;
504 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
506 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
508 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
510 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
512 if (policy && (policy_obj = txt2obj(policy)) == NULL)
514 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
517 /* Setting nonce if requested. */
518 if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
520 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
522 if (!TS_REQ_set_cert_req(ts_req, cert))
530 BIO_printf(bio_err, "could not create query\n");
531 ERR_print_errors(bio_err);
533 TS_MSG_IMPRINT_free(msg_imprint);
534 X509_ALGOR_free(algo);
536 ASN1_OBJECT_free(policy_obj);
537 ASN1_INTEGER_free(nonce_asn1);
541 static int create_digest(BIO *input, char *digest, const EVP_MD *md,
542 unsigned char **md_value)
546 md_value_len = EVP_MD_size(md);
547 if (md_value_len < 0)
551 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
552 unsigned char buffer[4096];
557 *md_value = app_malloc(md_value_len, "digest buffer");
558 EVP_DigestInit(md_ctx, md);
559 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
560 EVP_DigestUpdate(md_ctx, buffer, length);
562 if (!EVP_DigestFinal(md_ctx, *md_value, NULL)) {
563 EVP_MD_CTX_free(md_ctx);
566 EVP_MD_CTX_free(md_ctx);
569 *md_value = OPENSSL_hexstr2buf(digest, &digest_len);
570 if (!*md_value || md_value_len != digest_len) {
571 OPENSSL_free(*md_value);
573 BIO_printf(bio_err, "bad digest, %d bytes "
574 "must be specified\n", md_value_len);
581 static ASN1_INTEGER *create_nonce(int bits)
583 unsigned char buf[20];
584 ASN1_INTEGER *nonce = NULL;
585 int len = (bits - 1) / 8 + 1;
588 if (len > (int)sizeof(buf))
590 if (RAND_bytes(buf, len) <= 0)
593 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
594 for (i = 0; i < len && !buf[i]; ++i)
596 if ((nonce = ASN1_INTEGER_new()) == NULL)
598 OPENSSL_free(nonce->data);
599 nonce->length = len - i;
600 nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
601 memcpy(nonce->data, buf + i, nonce->length);
605 BIO_printf(bio_err, "could not create nonce\n");
606 ASN1_INTEGER_free(nonce);
611 * Reply-related method definitions.
614 static int reply_command(CONF *conf, char *section, char *engine,
615 char *queryfile, char *passin, char *inkey,
616 const EVP_MD *md, char *signer, char *chain,
617 const char *policy, char *in, int token_in,
618 char *out, int token_out, int text)
621 TS_RESP *response = NULL;
623 BIO *query_bio = NULL;
624 BIO *inkey_bio = NULL;
625 BIO *signer_bio = NULL;
629 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
632 response = read_PKCS7(in_bio);
634 response = d2i_TS_RESP_bio(in_bio, NULL);
637 response = create_response(conf, section, engine, queryfile,
638 passin, inkey, md, signer, chain, policy);
640 BIO_printf(bio_err, "Response has been generated.\n");
642 BIO_printf(bio_err, "Response is not generated.\n");
644 if (response == NULL)
647 /* Write response. */
649 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
652 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
653 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
656 if (!TS_RESP_print_bio(out_bio, response))
660 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
663 PKCS7 *token = TS_RESP_get_token(response);
664 if (!i2d_PKCS7_bio(out_bio, token))
667 if (!i2d_TS_RESP_bio(out_bio, response))
675 ERR_print_errors(bio_err);
676 BIO_free_all(in_bio);
677 BIO_free_all(query_bio);
678 BIO_free_all(inkey_bio);
679 BIO_free_all(signer_bio);
680 BIO_free_all(out_bio);
681 TS_RESP_free(response);
685 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
686 static TS_RESP *read_PKCS7(BIO *in_bio)
690 TS_TST_INFO *tst_info = NULL;
691 TS_RESP *resp = NULL;
692 TS_STATUS_INFO *si = NULL;
694 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
696 if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
698 if ((resp = TS_RESP_new()) == NULL)
700 if ((si = TS_STATUS_INFO_new()) == NULL)
702 if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
704 if (!TS_RESP_set_status_info(resp, si))
706 TS_RESP_set_tst_info(resp, token, tst_info);
707 token = NULL; /* Ownership is lost. */
708 tst_info = NULL; /* Ownership is lost. */
713 TS_TST_INFO_free(tst_info);
718 TS_STATUS_INFO_free(si);
722 static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
723 char *queryfile, char *passin,
724 char *inkey, const EVP_MD *md, char *signer,
725 char *chain, const char *policy)
728 TS_RESP *response = NULL;
729 BIO *query_bio = NULL;
730 TS_RESP_CTX *resp_ctx = NULL;
732 if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
734 if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
736 if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
738 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
740 # ifndef OPENSSL_NO_ENGINE
741 if (!TS_CONF_set_crypto_device(conf, section, engine))
744 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
746 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
748 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
752 if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
754 } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
758 if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
760 if (!TS_CONF_set_policies(conf, section, resp_ctx))
762 if (!TS_CONF_set_digests(conf, section, resp_ctx))
764 if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
766 if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
768 if (!TS_CONF_set_ordering(conf, section, resp_ctx))
770 if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
772 if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
774 if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
780 TS_RESP_free(response);
783 TS_RESP_CTX_free(resp_ctx);
784 BIO_free_all(query_bio);
788 static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
790 const char *serial_file = (const char *)data;
791 ASN1_INTEGER *serial = next_serial(serial_file);
794 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
795 "Error during serial number "
797 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
799 save_ts_serial(serial_file, serial);
804 static ASN1_INTEGER *next_serial(const char *serialfile)
808 ASN1_INTEGER *serial = NULL;
811 if ((serial = ASN1_INTEGER_new()) == NULL)
814 if ((in = BIO_new_file(serialfile, "r")) == NULL) {
816 BIO_printf(bio_err, "Warning: could not open file %s for "
817 "reading, using serial number: 1\n", serialfile);
818 if (!ASN1_INTEGER_set(serial, 1))
822 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
823 BIO_printf(bio_err, "unable to load number from %s\n",
827 if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
829 ASN1_INTEGER_free(serial);
831 if (!BN_add_word(bn, 1))
833 if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
840 ASN1_INTEGER_free(serial);
848 static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
853 if ((out = BIO_new_file(serialfile, "w")) == NULL)
855 if (i2a_ASN1_INTEGER(out, serial) <= 0)
857 if (BIO_puts(out, "\n") <= 0)
862 BIO_printf(bio_err, "could not save serial number to %s\n",
870 * Verify-related method definitions.
873 static int verify_command(char *data, char *digest, char *queryfile,
874 char *in, int token_in,
875 char *CApath, char *CAfile, char *untrusted,
876 X509_VERIFY_PARAM *vpm)
880 TS_RESP *response = NULL;
881 TS_VERIFY_CTX *verify_ctx = NULL;
884 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
887 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
890 if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
894 if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
895 CApath, CAfile, untrusted,
900 ? TS_RESP_verify_token(verify_ctx, token)
901 : TS_RESP_verify_response(verify_ctx, response);
904 printf("Verification: ");
909 ERR_print_errors(bio_err);
912 BIO_free_all(in_bio);
914 TS_RESP_free(response);
915 TS_VERIFY_CTX_free(verify_ctx);
919 static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
921 char *CApath, char *CAfile,
923 X509_VERIFY_PARAM *vpm)
925 TS_VERIFY_CTX *ctx = NULL;
927 TS_REQ *request = NULL;
931 if (data != NULL || digest != NULL) {
932 if ((ctx = TS_VERIFY_CTX_new()) == NULL)
934 f = TS_VFY_VERSION | TS_VFY_SIGNER;
937 if (TS_VERIFY_CTX_set_data(ctx, BIO_new_file(data, "rb")) == NULL)
939 } else if (digest != NULL) {
941 unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
943 if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
944 BIO_printf(bio_err, "invalid digest string\n");
949 } else if (queryfile != NULL) {
950 if ((input = BIO_new_file(queryfile, "rb")) == NULL)
952 if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
954 if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
959 /* Add the signature verification flag and arguments. */
960 TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
962 /* Initialising the X509_STORE object. */
963 if (TS_VERIFY_CTX_set_store(ctx, create_cert_store(CApath, CAfile, vpm))
967 /* Loading untrusted certificates. */
969 && TS_VERIFY_CTS_set_certs(ctx, TS_CONF_load_certs(untrusted)) == NULL)
975 TS_VERIFY_CTX_free(ctx);
979 TS_REQ_free(request);
983 static X509_STORE *create_cert_store(char *CApath, char *CAfile, X509_VERIFY_PARAM *vpm)
985 X509_STORE *cert_ctx = NULL;
986 X509_LOOKUP *lookup = NULL;
989 cert_ctx = X509_STORE_new();
990 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
991 if (CApath != NULL) {
992 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
993 if (lookup == NULL) {
994 BIO_printf(bio_err, "memory allocation failure\n");
997 i = X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM);
999 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
1004 if (CAfile != NULL) {
1005 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
1006 if (lookup == NULL) {
1007 BIO_printf(bio_err, "memory allocation failure\n");
1010 i = X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM);
1012 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
1018 X509_STORE_set1_param(cert_ctx, vpm);
1023 X509_STORE_free(cert_ctx);
1027 static int verify_cb(int ok, X509_STORE_CTX *ctx)