1 /* $OpenBSD: ts.c,v 1.12 2015/10/17 07:51:10 semarie Exp $ */
2 /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
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).
65 #include <openssl/bio.h>
66 #include <openssl/bn.h>
67 #include <openssl/err.h>
68 #include <openssl/pem.h>
69 #include <openssl/ts.h>
71 /* Length of the nonce of the request in bits (must be a multiple of 8). */
72 #define NONCE_LENGTH 64
74 /* Macro definitions for the configuration file. */
75 #define ENV_OID_FILE "oid_file"
77 /* Local function declarations. */
79 static ASN1_OBJECT *txt2obj(const char *oid);
80 static CONF *load_config_file(const char *configfile);
82 /* Query related functions. */
83 static int query_command(const char *data, char *digest,
84 const EVP_MD * md, const char *policy, int no_nonce,
85 int cert, const char *in, const char *out, int text);
86 static BIO *BIO_open_with_default(const char *file, const char *mode,
88 static TS_REQ *create_query(BIO * data_bio, char *digest, const EVP_MD * md,
89 const char *policy, int no_nonce, int cert);
90 static int create_digest(BIO * input, char *digest,
91 const EVP_MD * md, unsigned char **md_value);
92 static ASN1_INTEGER *create_nonce(int bits);
94 /* Reply related functions. */
95 static int reply_command(CONF * conf, char *section,
96 char *queryfile, char *passin, char *inkey,
97 char *signer, char *chain, const char *policy,
98 char *in, int token_in, char *out, int token_out,
100 static TS_RESP *read_PKCS7(BIO * in_bio);
101 static TS_RESP *create_response(CONF * conf, const char *section,
102 char *queryfile, char *passin, char *inkey,
103 char *signer, char *chain, const char *policy);
104 static ASN1_INTEGER *serial_cb(TS_RESP_CTX * ctx, void *data);
105 static ASN1_INTEGER *next_serial(const char *serialfile);
106 static int save_ts_serial(const char *serialfile, ASN1_INTEGER * serial);
108 /* Verify related functions. */
109 static int verify_command(char *data, char *digest, char *queryfile,
110 char *in, int token_in,
111 char *ca_path, char *ca_file, char *untrusted);
112 static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
114 char *ca_path, char *ca_file,
116 static X509_STORE *create_cert_store(char *ca_path, char *ca_file);
117 static int verify_cb(int ok, X509_STORE_CTX * ctx);
120 ts_main(int argc, char **argv)
123 char *configfile = NULL;
124 char *section = NULL;
127 CMD_NONE, CMD_QUERY, CMD_REPLY, CMD_VERIFY
131 const EVP_MD *md = NULL;
138 char *queryfile = NULL;
139 char *passin = NULL; /* Password source. */
140 char *password = NULL; /* Password itself. */
144 char *ca_path = NULL;
145 char *ca_file = NULL;
146 char *untrusted = NULL;
147 /* Input is ContentInfo instead of TimeStampResp. */
149 /* Output is ContentInfo instead of TimeStampResp. */
152 if (single_execution) {
153 if (pledge("stdio rpath wpath cpath tty", NULL) == -1) {
159 for (argc--, argv++; argc > 0; argc--, argv++) {
160 if (strcmp(*argv, "-config") == 0) {
163 configfile = *++argv;
164 } else if (strcmp(*argv, "-section") == 0) {
168 } else if (strcmp(*argv, "-query") == 0) {
169 if (mode != CMD_NONE)
172 } else if (strcmp(*argv, "-data") == 0) {
176 } else if (strcmp(*argv, "-digest") == 0) {
180 } else if (strcmp(*argv, "-policy") == 0) {
184 } else if (strcmp(*argv, "-no_nonce") == 0) {
186 } else if (strcmp(*argv, "-cert") == 0) {
188 } else if (strcmp(*argv, "-in") == 0) {
192 } else if (strcmp(*argv, "-token_in") == 0) {
194 } else if (strcmp(*argv, "-out") == 0) {
198 } else if (strcmp(*argv, "-token_out") == 0) {
200 } else if (strcmp(*argv, "-text") == 0) {
202 } else if (strcmp(*argv, "-reply") == 0) {
203 if (mode != CMD_NONE)
206 } else if (strcmp(*argv, "-queryfile") == 0) {
210 } else if (strcmp(*argv, "-passin") == 0) {
214 } else if (strcmp(*argv, "-inkey") == 0) {
218 } else if (strcmp(*argv, "-signer") == 0) {
222 } else if (strcmp(*argv, "-chain") == 0) {
226 } else if (strcmp(*argv, "-verify") == 0) {
227 if (mode != CMD_NONE)
230 } else if (strcmp(*argv, "-CApath") == 0) {
234 } else if (strcmp(*argv, "-CAfile") == 0) {
238 } else if (strcmp(*argv, "-untrusted") == 0) {
242 } else if ((md = EVP_get_digestbyname(*argv + 1)) != NULL) {
248 /* Get the password if required. */
249 if (mode == CMD_REPLY && passin &&
250 !app_passwd(bio_err, passin, NULL, &password, NULL)) {
251 BIO_printf(bio_err, "Error getting password.\n");
255 * Check consistency of parameters and execute the appropriate
263 * Data file and message imprint cannot be specified at the
266 ret = data != NULL && digest != NULL;
269 /* Load the config file for possible policy OIDs. */
270 conf = load_config_file(configfile);
271 ret = !query_command(data, digest, md, policy, no_nonce, cert,
275 conf = load_config_file(configfile);
277 ret = !(queryfile != NULL && conf != NULL && !token_in);
281 /* 'in' and 'queryfile' are exclusive. */
282 ret = !(queryfile == NULL);
287 ret = !reply_command(conf, section, queryfile,
288 password, inkey, signer, chain, policy,
289 in, token_in, out, token_out, text);
292 ret = !(((queryfile && !data && !digest) ||
293 (!queryfile && data && !digest) ||
294 (!queryfile && !data && digest)) && in != NULL);
298 ret = !verify_command(data, digest, queryfile, in, token_in,
299 ca_path, ca_file, untrusted);
305 BIO_printf(bio_err, "usage:\n"
306 "ts -query [-config configfile] "
307 "[-data file_to_hash] [-digest digest_bytes]"
308 "[-md4|-md5|-sha1|-ripemd160] "
309 "[-policy object_id] [-no_nonce] [-cert] "
310 "[-in request.tsq] [-out request.tsq] [-text]\n");
311 BIO_printf(bio_err, "or\n"
312 "ts -reply [-config configfile] [-section tsa_section] "
313 "[-queryfile request.tsq] [-passin password] "
314 "[-signer tsa_cert.pem] [-inkey private_key.pem] "
315 "[-chain certs_file.pem] [-policy object_id] "
316 "[-in response.tsr] [-token_in] "
317 "[-out response.tsr] [-token_out] [-text]\n");
318 BIO_printf(bio_err, "or\n"
319 "ts -verify [-data file_to_hash] [-digest digest_bytes] "
320 "[-queryfile request.tsq] "
321 "-in response.tsr [-token_in] "
322 "-CApath ca_path -CAfile ca_file.pem "
323 "-untrusted cert_file.pem\n");
335 * Configuration file-related function definitions.
339 txt2obj(const char *oid)
341 ASN1_OBJECT *oid_obj = NULL;
343 if (!(oid_obj = OBJ_txt2obj(oid, 0)))
344 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
350 load_config_file(const char *configfile)
356 configfile = getenv("OPENSSL_CONF");
359 (!(conf = NCONF_new(NULL)) ||
360 NCONF_load(conf, configfile, &errorline) <= 0)) {
362 BIO_printf(bio_err, "error loading the config file "
363 "'%s'\n", configfile);
365 BIO_printf(bio_err, "error on line %ld of config file "
366 "'%s'\n", errorline, configfile);
371 BIO_printf(bio_err, "Using configuration from %s\n",
373 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
375 BIO *oid_bio = BIO_new_file(p, "r");
377 ERR_print_errors(bio_err);
379 OBJ_create_objects(oid_bio);
380 BIO_free_all(oid_bio);
384 if (!add_oid_section(bio_err, conf))
385 ERR_print_errors(bio_err);
391 * Query-related method definitions.
395 query_command(const char *data, char *digest, const EVP_MD * md,
396 const char *policy, int no_nonce, int cert, const char *in,
397 const char *out, int text)
400 TS_REQ *query = NULL;
402 BIO *data_bio = NULL;
405 /* Build query object either from file or from scratch. */
407 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
409 query = d2i_TS_REQ_bio(in_bio, NULL);
411 /* Open the file if no explicit digest bytes were specified. */
413 !(data_bio = BIO_open_with_default(data, "rb", stdin)))
415 /* Creating the query object. */
416 query = create_query(data_bio, digest, md,
417 policy, no_nonce, cert);
418 /* Saving the random number generator state. */
423 /* Write query either in ASN.1 or in text format. */
424 if ((out_bio = BIO_open_with_default(out, "wb", stdout)) == NULL)
428 if (!TS_REQ_print_bio(out_bio, query))
432 if (!i2d_TS_REQ_bio(out_bio, query))
439 ERR_print_errors(bio_err);
442 BIO_free_all(in_bio);
443 BIO_free_all(data_bio);
444 BIO_free_all(out_bio);
451 BIO_open_with_default(const char *file, const char *mode, FILE * default_fp)
453 return file == NULL ? BIO_new_fp(default_fp, BIO_NOCLOSE) :
454 BIO_new_file(file, mode);
458 create_query(BIO * data_bio, char *digest, const EVP_MD * md,
459 const char *policy, int no_nonce, int cert)
462 TS_REQ *ts_req = NULL;
464 TS_MSG_IMPRINT *msg_imprint = NULL;
465 X509_ALGOR *algo = NULL;
466 unsigned char *data = NULL;
467 ASN1_OBJECT *policy_obj = NULL;
468 ASN1_INTEGER *nonce_asn1 = NULL;
470 /* Setting default message digest. */
471 if (!md && !(md = EVP_get_digestbyname("sha1")))
474 /* Creating request object. */
475 if (!(ts_req = TS_REQ_new()))
478 /* Setting version. */
479 if (!TS_REQ_set_version(ts_req, 1))
482 /* Creating and adding MSG_IMPRINT object. */
483 if (!(msg_imprint = TS_MSG_IMPRINT_new()))
486 /* Adding algorithm. */
487 if (!(algo = X509_ALGOR_new()))
489 if (!(algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))))
491 if (!(algo->parameter = ASN1_TYPE_new()))
493 algo->parameter->type = V_ASN1_NULL;
494 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
497 /* Adding message digest. */
498 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
500 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
503 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
506 /* Setting policy if requested. */
507 if (policy && !(policy_obj = txt2obj(policy)))
509 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
512 /* Setting nonce if requested. */
513 if (!no_nonce && !(nonce_asn1 = create_nonce(NONCE_LENGTH)))
515 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
518 /* Setting certificate request flag if requested. */
519 if (!TS_REQ_set_cert_req(ts_req, cert))
528 BIO_printf(bio_err, "could not create query\n");
530 TS_MSG_IMPRINT_free(msg_imprint);
531 X509_ALGOR_free(algo);
533 ASN1_OBJECT_free(policy_obj);
534 ASN1_INTEGER_free(nonce_asn1);
540 create_digest(BIO * input, char *digest, const EVP_MD * md,
541 unsigned char **md_value)
545 md_value_len = EVP_MD_size(md);
546 if (md_value_len < 0)
549 /* Digest must be computed from an input file. */
551 unsigned char buffer[4096];
554 *md_value = malloc(md_value_len);
558 EVP_DigestInit(&md_ctx, md);
559 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
560 EVP_DigestUpdate(&md_ctx, buffer, length);
562 EVP_DigestFinal(&md_ctx, *md_value, NULL);
564 /* Digest bytes are specified with digest. */
566 *md_value = string_to_hex(digest, &digest_len);
567 if (!*md_value || md_value_len != digest_len) {
570 BIO_printf(bio_err, "bad digest, %d bytes "
571 "must be specified\n", md_value_len);
581 static ASN1_INTEGER *
582 create_nonce(int bits)
584 unsigned char buf[20];
585 ASN1_INTEGER *nonce = NULL;
586 int len = (bits - 1) / 8 + 1;
589 /* Generating random byte sequence. */
590 if (len > (int) sizeof(buf))
592 arc4random_buf(buf, len);
594 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
595 for (i = 0; i < len && !buf[i]; ++i)
597 if (!(nonce = ASN1_INTEGER_new()))
600 /* Allocate at least one byte. */
601 nonce->length = len - i;
602 if (!(nonce->data = malloc(nonce->length + 1)))
604 memcpy(nonce->data, buf + i, nonce->length);
609 BIO_printf(bio_err, "could not create nonce\n");
610 ASN1_INTEGER_free(nonce);
614 * Reply-related method definitions.
618 reply_command(CONF * conf, char *section, char *queryfile,
619 char *passin, char *inkey, char *signer, char *chain, const char *policy,
620 char *in, int token_in, char *out, int token_out, int text)
623 TS_RESP *response = NULL;
625 BIO *query_bio = NULL;
626 BIO *inkey_bio = NULL;
627 BIO *signer_bio = NULL;
630 /* Build response object either from response or query. */
632 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
636 * We have a ContentInfo (PKCS7) object, add
637 * 'granted' status info around it.
639 response = read_PKCS7(in_bio);
641 /* We have a ready-made TS_RESP object. */
642 response = d2i_TS_RESP_bio(in_bio, NULL);
645 response = create_response(conf, section, queryfile,
646 passin, inkey, signer, chain,
649 BIO_printf(bio_err, "Response has been generated.\n");
651 BIO_printf(bio_err, "Response is not generated.\n");
653 if (response == NULL)
656 /* Write response either in ASN.1 or text format. */
657 if ((out_bio = BIO_open_with_default(out, "wb", stdout)) == NULL)
662 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
663 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
666 if (!TS_RESP_print_bio(out_bio, response))
670 /* ASN.1 DER output. */
672 PKCS7 *token = TS_RESP_get_token(response);
673 if (!i2d_PKCS7_bio(out_bio, token))
676 if (!i2d_TS_RESP_bio(out_bio, response))
684 ERR_print_errors(bio_err);
687 BIO_free_all(in_bio);
688 BIO_free_all(query_bio);
689 BIO_free_all(inkey_bio);
690 BIO_free_all(signer_bio);
691 BIO_free_all(out_bio);
692 TS_RESP_free(response);
697 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
699 read_PKCS7(BIO * in_bio)
703 TS_TST_INFO *tst_info = NULL;
704 TS_RESP *resp = NULL;
705 TS_STATUS_INFO *si = NULL;
707 /* Read PKCS7 object and extract the signed time stamp info. */
708 if (!(token = d2i_PKCS7_bio(in_bio, NULL)))
710 if (!(tst_info = PKCS7_to_TS_TST_INFO(token)))
713 /* Creating response object. */
714 if (!(resp = TS_RESP_new()))
717 /* Create granted status info. */
718 if (!(si = TS_STATUS_INFO_new()))
720 if (!(ASN1_INTEGER_set(si->status, TS_STATUS_GRANTED)))
722 if (!TS_RESP_set_status_info(resp, si))
725 /* Setting encapsulated token. */
726 TS_RESP_set_tst_info(resp, token, tst_info);
727 token = NULL; /* Ownership is lost. */
728 tst_info = NULL; /* Ownership is lost. */
733 TS_TST_INFO_free(tst_info);
738 TS_STATUS_INFO_free(si);
743 create_response(CONF * conf, const char *section,
744 char *queryfile, char *passin, char *inkey,
745 char *signer, char *chain, const char *policy)
748 TS_RESP *response = NULL;
749 BIO *query_bio = NULL;
750 TS_RESP_CTX *resp_ctx = NULL;
752 if (!(query_bio = BIO_new_file(queryfile, "rb")))
755 /* Getting TSA configuration section. */
756 if (!(section = TS_CONF_get_tsa_section(conf, section)))
759 /* Setting up response generation context. */
760 if (!(resp_ctx = TS_RESP_CTX_new()))
763 /* Setting serial number provider callback. */
764 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
767 /* Setting TSA signer certificate. */
768 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
771 /* Setting TSA signer certificate chain. */
772 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
775 /* Setting TSA signer private key. */
776 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
779 /* Setting default policy OID. */
780 if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
783 /* Setting acceptable policy OIDs. */
784 if (!TS_CONF_set_policies(conf, section, resp_ctx))
787 /* Setting the acceptable one-way hash algorithms. */
788 if (!TS_CONF_set_digests(conf, section, resp_ctx))
791 /* Setting guaranteed time stamp accuracy. */
792 if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
795 /* Setting the precision of the time. */
796 if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
799 /* Setting the ordering flaf if requested. */
800 if (!TS_CONF_set_ordering(conf, section, resp_ctx))
803 /* Setting the TSA name required flag if requested. */
804 if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
807 /* Setting the ESS cert id chain flag if requested. */
808 if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
811 /* Creating the response. */
812 if (!(response = TS_RESP_create_response(resp_ctx, query_bio)))
818 TS_RESP_free(response);
821 TS_RESP_CTX_free(resp_ctx);
822 BIO_free_all(query_bio);
827 static ASN1_INTEGER *
828 serial_cb(TS_RESP_CTX * ctx, void *data)
830 const char *serial_file = (const char *) data;
831 ASN1_INTEGER *serial = next_serial(serial_file);
834 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
835 "Error during serial number "
837 TS_RESP_CTX_add_failure_info(ctx,
838 TS_INFO_ADD_INFO_NOT_AVAILABLE);
840 save_ts_serial(serial_file, serial);
845 static ASN1_INTEGER *
846 next_serial(const char *serialfile)
850 ASN1_INTEGER *serial = NULL;
853 if (!(serial = ASN1_INTEGER_new()))
856 if (!(in = BIO_new_file(serialfile, "r"))) {
858 BIO_printf(bio_err, "Warning: could not open file %s for "
859 "reading, using serial number: 1\n", serialfile);
860 if (!ASN1_INTEGER_set(serial, 1))
864 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
865 BIO_printf(bio_err, "unable to load number from %s\n",
869 if (!(bn = ASN1_INTEGER_to_BN(serial, NULL)))
871 ASN1_INTEGER_free(serial);
873 if (!BN_add_word(bn, 1))
875 if (!(serial = BN_to_ASN1_INTEGER(bn, NULL)))
881 ASN1_INTEGER_free(serial);
890 save_ts_serial(const char *serialfile, ASN1_INTEGER * serial)
895 if (!(out = BIO_new_file(serialfile, "w")))
897 if (i2a_ASN1_INTEGER(out, serial) <= 0)
899 if (BIO_puts(out, "\n") <= 0)
904 BIO_printf(bio_err, "could not save serial number to %s\n",
911 * Verify-related method definitions.
915 verify_command(char *data, char *digest, char *queryfile, char *in,
916 int token_in, char *ca_path, char *ca_file, char *untrusted)
920 TS_RESP *response = NULL;
921 TS_VERIFY_CTX *verify_ctx = NULL;
924 /* Decode the token (PKCS7) or response (TS_RESP) files. */
925 if (!(in_bio = BIO_new_file(in, "rb")))
928 if (!(token = d2i_PKCS7_bio(in_bio, NULL)))
931 if (!(response = d2i_TS_RESP_bio(in_bio, NULL)))
935 if (!(verify_ctx = create_verify_ctx(data, digest, queryfile,
936 ca_path, ca_file, untrusted)))
939 /* Checking the token or response against the request. */
941 TS_RESP_verify_token(verify_ctx, token) :
942 TS_RESP_verify_response(verify_ctx, response);
945 printf("Verification: ");
950 /* Print errors, if there are any. */
951 ERR_print_errors(bio_err);
955 BIO_free_all(in_bio);
957 TS_RESP_free(response);
958 TS_VERIFY_CTX_free(verify_ctx);
962 static TS_VERIFY_CTX *
963 create_verify_ctx(char *data, char *digest, char *queryfile, char *ca_path,
964 char *ca_file, char *untrusted)
966 TS_VERIFY_CTX *ctx = NULL;
968 TS_REQ *request = NULL;
971 if (data != NULL || digest != NULL) {
972 if (!(ctx = TS_VERIFY_CTX_new()))
974 ctx->flags = TS_VFY_VERSION | TS_VFY_SIGNER;
976 ctx->flags |= TS_VFY_DATA;
977 if (!(ctx->data = BIO_new_file(data, "rb")))
979 } else if (digest != NULL) {
981 ctx->flags |= TS_VFY_IMPRINT;
982 if (!(ctx->imprint = string_to_hex(digest,
984 BIO_printf(bio_err, "invalid digest string\n");
987 ctx->imprint_len = imprint_len;
989 } else if (queryfile != NULL) {
991 * The request has just to be read, decoded and converted to
992 * a verify context object.
994 if (!(input = BIO_new_file(queryfile, "rb")))
996 if (!(request = d2i_TS_REQ_bio(input, NULL)))
998 if (!(ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)))
1003 /* Add the signature verification flag and arguments. */
1004 ctx->flags |= TS_VFY_SIGNATURE;
1006 /* Initialising the X509_STORE object. */
1007 if (!(ctx->store = create_cert_store(ca_path, ca_file)))
1010 /* Loading untrusted certificates. */
1011 if (untrusted && !(ctx->certs = TS_CONF_load_certs(untrusted)))
1017 TS_VERIFY_CTX_free(ctx);
1020 BIO_free_all(input);
1021 TS_REQ_free(request);
1026 create_cert_store(char *ca_path, char *ca_file)
1028 X509_STORE *cert_ctx = NULL;
1029 X509_LOOKUP *lookup = NULL;
1032 /* Creating the X509_STORE object. */
1033 cert_ctx = X509_STORE_new();
1035 /* Setting the callback for certificate chain verification. */
1036 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
1038 /* Adding a trusted certificate directory source. */
1040 lookup = X509_STORE_add_lookup(cert_ctx,
1041 X509_LOOKUP_hash_dir());
1042 if (lookup == NULL) {
1043 BIO_printf(bio_err, "memory allocation failure\n");
1046 i = X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM);
1048 BIO_printf(bio_err, "Error loading directory %s\n",
1053 /* Adding a trusted certificate file source. */
1055 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
1056 if (lookup == NULL) {
1057 BIO_printf(bio_err, "memory allocation failure\n");
1060 i = X509_LOOKUP_load_file(lookup, ca_file, X509_FILETYPE_PEM);
1062 BIO_printf(bio_err, "Error loading file %s\n", ca_file);
1068 X509_STORE_free(cert_ctx);
1073 verify_cb(int ok, X509_STORE_CTX * ctx)
1080 X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
1082 printf("%s\n", buf);
1083 printf("error %d at %d depth lookup: %s\n",
1084 ctx->error, ctx->error_depth,
1085 X509_verify_cert_error_string(ctx->error));