2 * Copyright 2006-2018 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
19 # include <openssl/bio.h>
20 # include <openssl/err.h>
21 # include <openssl/pem.h>
22 # include <openssl/rand.h>
23 # include <openssl/ts.h>
24 # include <openssl/bn.h>
26 /* Request nonce length, in bits (must be a multiple of 8). */
27 # define NONCE_LENGTH 64
29 /* Name of config entry that defines the OID file. */
30 # define ENV_OID_FILE "oid_file"
32 /* Is |EXACTLY_ONE| of three pointers set? */
33 # define EXACTLY_ONE(a, b, c) \
34 (( a && !b && !c) || \
38 static ASN1_OBJECT
*txt2obj(const char *oid
);
39 static CONF
*load_config_file(const char *configfile
);
41 /* Query related functions. */
42 static int query_command(const char *data
, const char *digest
,
43 const EVP_MD
*md
, const char *policy
, int no_nonce
,
44 int cert
, const char *in
, const char *out
, int text
);
45 static TS_REQ
*create_query(BIO
*data_bio
, const char *digest
, const EVP_MD
*md
,
46 const char *policy
, int no_nonce
, int cert
);
47 static int create_digest(BIO
*input
, const char *digest
,
48 const EVP_MD
*md
, unsigned char **md_value
);
49 static ASN1_INTEGER
*create_nonce(int bits
);
51 /* Reply related functions. */
52 static int reply_command(CONF
*conf
, const char *section
, const char *engine
,
53 const char *queryfile
, const char *passin
, const char *inkey
,
54 const EVP_MD
*md
, const char *signer
, const char *chain
,
55 const char *policy
, const char *in
, int token_in
,
56 const char *out
, int token_out
, int text
);
57 static TS_RESP
*read_PKCS7(BIO
*in_bio
);
58 static TS_RESP
*create_response(CONF
*conf
, const char *section
, const char *engine
,
59 const char *queryfile
, const char *passin
,
60 const char *inkey
, const EVP_MD
*md
, const char *signer
,
61 const char *chain
, const char *policy
);
62 static ASN1_INTEGER
*serial_cb(TS_RESP_CTX
*ctx
, void *data
);
63 static ASN1_INTEGER
*next_serial(const char *serialfile
);
64 static int save_ts_serial(const char *serialfile
, ASN1_INTEGER
*serial
);
66 /* Verify related functions. */
67 static int verify_command(const char *data
, const char *digest
, const char *queryfile
,
68 const char *in
, int token_in
,
69 const char *CApath
, const char *CAfile
, const char *untrusted
,
70 X509_VERIFY_PARAM
*vpm
);
71 static TS_VERIFY_CTX
*create_verify_ctx(const char *data
, const char *digest
,
72 const char *queryfile
,
73 const char *CApath
, const char *CAfile
,
74 const char *untrusted
,
75 X509_VERIFY_PARAM
*vpm
);
76 static X509_STORE
*create_cert_store(const char *CApath
, const char *CAfile
,
77 X509_VERIFY_PARAM
*vpm
);
78 static int verify_cb(int ok
, X509_STORE_CTX
*ctx
);
80 typedef enum OPTION_choice
{
81 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
82 OPT_ENGINE
, OPT_CONFIG
, OPT_SECTION
, OPT_QUERY
, OPT_DATA
,
83 OPT_DIGEST
, OPT_TSPOLICY
, OPT_NO_NONCE
, OPT_CERT
,
84 OPT_IN
, OPT_TOKEN_IN
, OPT_OUT
, OPT_TOKEN_OUT
, OPT_TEXT
,
85 OPT_REPLY
, OPT_QUERYFILE
, OPT_PASSIN
, OPT_INKEY
, OPT_SIGNER
,
86 OPT_CHAIN
, OPT_VERIFY
, OPT_CAPATH
, OPT_CAFILE
, OPT_UNTRUSTED
,
87 OPT_MD
, OPT_V_ENUM
, OPT_R_ENUM
90 const OPTIONS ts_options
[] = {
91 {"help", OPT_HELP
, '-', "Display this summary"},
92 {"config", OPT_CONFIG
, '<', "Configuration file"},
93 {"section", OPT_SECTION
, 's', "Section to use within config file"},
94 {"query", OPT_QUERY
, '-', "Generate a TS query"},
95 {"data", OPT_DATA
, '<', "File to hash"},
96 {"digest", OPT_DIGEST
, 's', "Digest (as a hex string)"},
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
, 's', "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
, *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();
239 queryfile
= opt_arg();
260 untrusted
= opt_arg();
266 if (!opt_md(opt_unknown(), &md
))
270 if (!opt_verify(o
, vpm
))
276 if (mode
== OPT_ERR
|| opt_num_rest() != 0)
279 if (mode
== OPT_REPLY
&& passin
&&
280 !app_passwd(passin
, NULL
, &password
, NULL
)) {
281 BIO_printf(bio_err
, "Error getting password.\n");
285 conf
= load_config_file(configfile
);
286 if (configfile
!= default_config_file
&& !app_load_modules(conf
))
289 /* Check parameter consistency and execute the appropriate function. */
290 if (mode
== OPT_QUERY
) {
293 if ((data
!= NULL
) && (digest
!= NULL
))
295 ret
= !query_command(data
, digest
, md
, policy
, no_nonce
, cert
,
297 } else if (mode
== OPT_REPLY
) {
300 if ((in
!= NULL
) && (queryfile
!= NULL
))
303 if ((conf
== NULL
) || (token_in
!= 0))
306 ret
= !reply_command(conf
, section
, engine
, queryfile
,
307 password
, inkey
, md
, signer
, chain
, policy
,
308 in
, token_in
, out
, token_out
, text
);
310 } else if (mode
== OPT_VERIFY
) {
311 if ((in
== NULL
) || !EXACTLY_ONE(queryfile
, data
, digest
))
313 ret
= !verify_command(data
, digest
, queryfile
, in
, token_in
,
314 CApath
, CAfile
, untrusted
,
315 vpmtouched
? vpm
: NULL
);
321 X509_VERIFY_PARAM_free(vpm
);
323 OPENSSL_free(password
);
328 * Configuration file-related function definitions.
331 static ASN1_OBJECT
*txt2obj(const char *oid
)
333 ASN1_OBJECT
*oid_obj
= NULL
;
335 if ((oid_obj
= OBJ_txt2obj(oid
, 0)) == NULL
)
336 BIO_printf(bio_err
, "cannot convert %s to OID\n", oid
);
341 static CONF
*load_config_file(const char *configfile
)
343 CONF
*conf
= app_load_config(configfile
);
348 BIO_printf(bio_err
, "Using configuration from %s\n", configfile
);
349 p
= NCONF_get_string(conf
, NULL
, ENV_OID_FILE
);
351 BIO
*oid_bio
= BIO_new_file(p
, "r");
353 ERR_print_errors(bio_err
);
355 OBJ_create_objects(oid_bio
);
356 BIO_free_all(oid_bio
);
360 if (!add_oid_section(conf
))
361 ERR_print_errors(bio_err
);
367 * Query-related method definitions.
369 static int query_command(const char *data
, const char *digest
, const EVP_MD
*md
,
370 const char *policy
, int no_nonce
,
371 int cert
, const char *in
, const char *out
, int text
)
374 TS_REQ
*query
= NULL
;
376 BIO
*data_bio
= NULL
;
379 /* Build query object. */
381 if ((in_bio
= bio_open_default(in
, 'r', FORMAT_ASN1
)) == NULL
)
383 query
= d2i_TS_REQ_bio(in_bio
, NULL
);
386 && (data_bio
= bio_open_default(data
, 'r', FORMAT_ASN1
)) == NULL
)
388 query
= create_query(data_bio
, digest
, md
, policy
, no_nonce
, cert
);
394 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_TEXT
)) == NULL
)
396 if (!TS_REQ_print_bio(out_bio
, query
))
399 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_ASN1
)) == NULL
)
401 if (!i2d_TS_REQ_bio(out_bio
, query
))
408 ERR_print_errors(bio_err
);
409 BIO_free_all(in_bio
);
410 BIO_free_all(data_bio
);
411 BIO_free_all(out_bio
);
416 static TS_REQ
*create_query(BIO
*data_bio
, const char *digest
, const EVP_MD
*md
,
417 const char *policy
, int no_nonce
, int cert
)
420 TS_REQ
*ts_req
= NULL
;
422 TS_MSG_IMPRINT
*msg_imprint
= NULL
;
423 X509_ALGOR
*algo
= NULL
;
424 unsigned char *data
= NULL
;
425 ASN1_OBJECT
*policy_obj
= NULL
;
426 ASN1_INTEGER
*nonce_asn1
= NULL
;
428 if (md
== NULL
&& (md
= EVP_get_digestbyname("sha1")) == NULL
)
430 if ((ts_req
= TS_REQ_new()) == NULL
)
432 if (!TS_REQ_set_version(ts_req
, 1))
434 if ((msg_imprint
= TS_MSG_IMPRINT_new()) == NULL
)
436 if ((algo
= X509_ALGOR_new()) == NULL
)
438 if ((algo
->algorithm
= OBJ_nid2obj(EVP_MD_type(md
))) == NULL
)
440 if ((algo
->parameter
= ASN1_TYPE_new()) == NULL
)
442 algo
->parameter
->type
= V_ASN1_NULL
;
443 if (!TS_MSG_IMPRINT_set_algo(msg_imprint
, algo
))
445 if ((len
= create_digest(data_bio
, digest
, md
, &data
)) == 0)
447 if (!TS_MSG_IMPRINT_set_msg(msg_imprint
, data
, len
))
449 if (!TS_REQ_set_msg_imprint(ts_req
, msg_imprint
))
451 if (policy
&& (policy_obj
= txt2obj(policy
)) == NULL
)
453 if (policy_obj
&& !TS_REQ_set_policy_id(ts_req
, policy_obj
))
456 /* Setting nonce if requested. */
457 if (!no_nonce
&& (nonce_asn1
= create_nonce(NONCE_LENGTH
)) == NULL
)
459 if (nonce_asn1
&& !TS_REQ_set_nonce(ts_req
, nonce_asn1
))
461 if (!TS_REQ_set_cert_req(ts_req
, cert
))
469 BIO_printf(bio_err
, "could not create query\n");
470 ERR_print_errors(bio_err
);
472 TS_MSG_IMPRINT_free(msg_imprint
);
473 X509_ALGOR_free(algo
);
475 ASN1_OBJECT_free(policy_obj
);
476 ASN1_INTEGER_free(nonce_asn1
);
480 static int create_digest(BIO
*input
, const char *digest
, const EVP_MD
*md
,
481 unsigned char **md_value
)
485 EVP_MD_CTX
*md_ctx
= NULL
;
487 md_value_len
= EVP_MD_size(md
);
488 if (md_value_len
< 0)
492 unsigned char buffer
[4096];
495 md_ctx
= EVP_MD_CTX_new();
498 *md_value
= app_malloc(md_value_len
, "digest buffer");
499 if (!EVP_DigestInit(md_ctx
, md
))
501 while ((length
= BIO_read(input
, buffer
, sizeof(buffer
))) > 0) {
502 if (!EVP_DigestUpdate(md_ctx
, buffer
, length
))
505 if (!EVP_DigestFinal(md_ctx
, *md_value
, NULL
))
507 md_value_len
= EVP_MD_size(md
);
510 *md_value
= OPENSSL_hexstr2buf(digest
, &digest_len
);
511 if (!*md_value
|| md_value_len
!= digest_len
) {
512 OPENSSL_free(*md_value
);
514 BIO_printf(bio_err
, "bad digest, %d bytes "
515 "must be specified\n", md_value_len
);
521 EVP_MD_CTX_free(md_ctx
);
525 static ASN1_INTEGER
*create_nonce(int bits
)
527 unsigned char buf
[20];
528 ASN1_INTEGER
*nonce
= NULL
;
529 int len
= (bits
- 1) / 8 + 1;
532 if (len
> (int)sizeof(buf
))
534 if (RAND_bytes(buf
, len
) <= 0)
537 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
538 for (i
= 0; i
< len
&& !buf
[i
]; ++i
)
540 if ((nonce
= ASN1_INTEGER_new()) == NULL
)
542 OPENSSL_free(nonce
->data
);
543 nonce
->length
= len
- i
;
544 nonce
->data
= app_malloc(nonce
->length
+ 1, "nonce buffer");
545 memcpy(nonce
->data
, buf
+ i
, nonce
->length
);
549 BIO_printf(bio_err
, "could not create nonce\n");
550 ASN1_INTEGER_free(nonce
);
555 * Reply-related method definitions.
558 static int reply_command(CONF
*conf
, const char *section
, const char *engine
,
559 const char *queryfile
, const char *passin
, const char *inkey
,
560 const EVP_MD
*md
, const char *signer
, const char *chain
,
561 const char *policy
, const char *in
, int token_in
,
562 const char *out
, int token_out
, int text
)
565 TS_RESP
*response
= NULL
;
567 BIO
*query_bio
= NULL
;
568 BIO
*inkey_bio
= NULL
;
569 BIO
*signer_bio
= NULL
;
573 if ((in_bio
= BIO_new_file(in
, "rb")) == NULL
)
576 response
= read_PKCS7(in_bio
);
578 response
= d2i_TS_RESP_bio(in_bio
, NULL
);
581 response
= create_response(conf
, section
, engine
, queryfile
,
582 passin
, inkey
, md
, signer
, chain
, policy
);
583 if (response
!= NULL
)
584 BIO_printf(bio_err
, "Response has been generated.\n");
586 BIO_printf(bio_err
, "Response is not generated.\n");
588 if (response
== NULL
)
591 /* Write response. */
593 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_TEXT
)) == NULL
)
596 TS_TST_INFO
*tst_info
= TS_RESP_get_tst_info(response
);
597 if (!TS_TST_INFO_print_bio(out_bio
, tst_info
))
600 if (!TS_RESP_print_bio(out_bio
, response
))
604 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_ASN1
)) == NULL
)
607 PKCS7
*token
= TS_RESP_get_token(response
);
608 if (!i2d_PKCS7_bio(out_bio
, token
))
611 if (!i2d_TS_RESP_bio(out_bio
, response
))
619 ERR_print_errors(bio_err
);
620 BIO_free_all(in_bio
);
621 BIO_free_all(query_bio
);
622 BIO_free_all(inkey_bio
);
623 BIO_free_all(signer_bio
);
624 BIO_free_all(out_bio
);
625 TS_RESP_free(response
);
629 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
630 static TS_RESP
*read_PKCS7(BIO
*in_bio
)
634 TS_TST_INFO
*tst_info
= NULL
;
635 TS_RESP
*resp
= NULL
;
636 TS_STATUS_INFO
*si
= NULL
;
638 if ((token
= d2i_PKCS7_bio(in_bio
, NULL
)) == NULL
)
640 if ((tst_info
= PKCS7_to_TS_TST_INFO(token
)) == NULL
)
642 if ((resp
= TS_RESP_new()) == NULL
)
644 if ((si
= TS_STATUS_INFO_new()) == NULL
)
646 if (!TS_STATUS_INFO_set_status(si
, TS_STATUS_GRANTED
))
648 if (!TS_RESP_set_status_info(resp
, si
))
650 TS_RESP_set_tst_info(resp
, token
, tst_info
);
651 token
= NULL
; /* Ownership is lost. */
652 tst_info
= NULL
; /* Ownership is lost. */
657 TS_TST_INFO_free(tst_info
);
662 TS_STATUS_INFO_free(si
);
666 static TS_RESP
*create_response(CONF
*conf
, const char *section
, const char *engine
,
667 const char *queryfile
, const char *passin
,
668 const char *inkey
, const EVP_MD
*md
, const char *signer
,
669 const char *chain
, const char *policy
)
672 TS_RESP
*response
= NULL
;
673 BIO
*query_bio
= NULL
;
674 TS_RESP_CTX
*resp_ctx
= NULL
;
676 if ((query_bio
= BIO_new_file(queryfile
, "rb")) == NULL
)
678 if ((section
= TS_CONF_get_tsa_section(conf
, section
)) == NULL
)
680 if ((resp_ctx
= TS_RESP_CTX_new()) == NULL
)
682 if (!TS_CONF_set_serial(conf
, section
, serial_cb
, resp_ctx
))
684 # ifndef OPENSSL_NO_ENGINE
685 if (!TS_CONF_set_crypto_device(conf
, section
, engine
))
688 if (!TS_CONF_set_signer_cert(conf
, section
, signer
, resp_ctx
))
690 if (!TS_CONF_set_certs(conf
, section
, chain
, resp_ctx
))
692 if (!TS_CONF_set_signer_key(conf
, section
, inkey
, passin
, resp_ctx
))
696 if (!TS_RESP_CTX_set_signer_digest(resp_ctx
, md
))
698 } else if (!TS_CONF_set_signer_digest(conf
, section
, NULL
, resp_ctx
)) {
702 if (!TS_CONF_set_ess_cert_id_digest(conf
, section
, resp_ctx
))
704 if (!TS_CONF_set_def_policy(conf
, section
, policy
, resp_ctx
))
706 if (!TS_CONF_set_policies(conf
, section
, resp_ctx
))
708 if (!TS_CONF_set_digests(conf
, section
, resp_ctx
))
710 if (!TS_CONF_set_accuracy(conf
, section
, resp_ctx
))
712 if (!TS_CONF_set_clock_precision_digits(conf
, section
, resp_ctx
))
714 if (!TS_CONF_set_ordering(conf
, section
, resp_ctx
))
716 if (!TS_CONF_set_tsa_name(conf
, section
, resp_ctx
))
718 if (!TS_CONF_set_ess_cert_id_chain(conf
, section
, resp_ctx
))
720 if ((response
= TS_RESP_create_response(resp_ctx
, query_bio
)) == NULL
)
726 TS_RESP_free(response
);
729 TS_RESP_CTX_free(resp_ctx
);
730 BIO_free_all(query_bio
);
734 static ASN1_INTEGER
*serial_cb(TS_RESP_CTX
*ctx
, void *data
)
736 const char *serial_file
= (const char *)data
;
737 ASN1_INTEGER
*serial
= next_serial(serial_file
);
739 if (serial
== NULL
) {
740 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
741 "Error during serial number "
743 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_ADD_INFO_NOT_AVAILABLE
);
745 save_ts_serial(serial_file
, serial
);
751 static ASN1_INTEGER
*next_serial(const char *serialfile
)
755 ASN1_INTEGER
*serial
= NULL
;
758 if ((serial
= ASN1_INTEGER_new()) == NULL
)
761 if ((in
= BIO_new_file(serialfile
, "r")) == NULL
) {
763 BIO_printf(bio_err
, "Warning: could not open file %s for "
764 "reading, using serial number: 1\n", serialfile
);
765 if (!ASN1_INTEGER_set(serial
, 1))
769 if (!a2i_ASN1_INTEGER(in
, serial
, buf
, sizeof(buf
))) {
770 BIO_printf(bio_err
, "unable to load number from %s\n",
774 if ((bn
= ASN1_INTEGER_to_BN(serial
, NULL
)) == NULL
)
776 ASN1_INTEGER_free(serial
);
778 if (!BN_add_word(bn
, 1))
780 if ((serial
= BN_to_ASN1_INTEGER(bn
, NULL
)) == NULL
)
787 ASN1_INTEGER_free(serial
);
795 static int save_ts_serial(const char *serialfile
, ASN1_INTEGER
*serial
)
800 if ((out
= BIO_new_file(serialfile
, "w")) == NULL
)
802 if (i2a_ASN1_INTEGER(out
, serial
) <= 0)
804 if (BIO_puts(out
, "\n") <= 0)
809 BIO_printf(bio_err
, "could not save serial number to %s\n",
817 * Verify-related method definitions.
820 static int verify_command(const char *data
, const char *digest
, const char *queryfile
,
821 const char *in
, int token_in
,
822 const char *CApath
, const char *CAfile
, const char *untrusted
,
823 X509_VERIFY_PARAM
*vpm
)
827 TS_RESP
*response
= NULL
;
828 TS_VERIFY_CTX
*verify_ctx
= NULL
;
831 if ((in_bio
= BIO_new_file(in
, "rb")) == NULL
)
834 if ((token
= d2i_PKCS7_bio(in_bio
, NULL
)) == NULL
)
837 if ((response
= d2i_TS_RESP_bio(in_bio
, NULL
)) == NULL
)
841 if ((verify_ctx
= create_verify_ctx(data
, digest
, queryfile
,
842 CApath
, CAfile
, untrusted
,
847 ? TS_RESP_verify_token(verify_ctx
, token
)
848 : TS_RESP_verify_response(verify_ctx
, response
);
851 printf("Verification: ");
856 ERR_print_errors(bio_err
);
859 BIO_free_all(in_bio
);
861 TS_RESP_free(response
);
862 TS_VERIFY_CTX_free(verify_ctx
);
866 static TS_VERIFY_CTX
*create_verify_ctx(const char *data
, const char *digest
,
867 const char *queryfile
,
868 const char *CApath
, const char *CAfile
,
869 const char *untrusted
,
870 X509_VERIFY_PARAM
*vpm
)
872 TS_VERIFY_CTX
*ctx
= NULL
;
874 TS_REQ
*request
= NULL
;
878 if (data
!= NULL
|| digest
!= NULL
) {
879 if ((ctx
= TS_VERIFY_CTX_new()) == NULL
)
881 f
= TS_VFY_VERSION
| TS_VFY_SIGNER
;
886 if ((out
= BIO_new_file(data
, "rb")) == NULL
)
888 if (TS_VERIFY_CTX_set_data(ctx
, out
) == NULL
) {
892 } else if (digest
!= NULL
) {
894 unsigned char *hexstr
= OPENSSL_hexstr2buf(digest
, &imprint_len
);
896 if (TS_VERIFY_CTX_set_imprint(ctx
, hexstr
, imprint_len
) == NULL
) {
897 BIO_printf(bio_err
, "invalid digest string\n");
902 } else if (queryfile
!= NULL
) {
903 if ((input
= BIO_new_file(queryfile
, "rb")) == NULL
)
905 if ((request
= d2i_TS_REQ_bio(input
, NULL
)) == NULL
)
907 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 */