2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
16 #include <openssl/bio.h>
17 #include <openssl/err.h>
18 #include <openssl/pem.h>
19 #include <openssl/rand.h>
20 #include <openssl/ts.h>
21 #include <openssl/bn.h>
23 /* Request nonce length, in bits (must be a multiple of 8). */
24 #define NONCE_LENGTH 64
26 /* Name of config entry that defines the OID file. */
27 #define ENV_OID_FILE "oid_file"
29 /* Is |EXACTLY_ONE| of three pointers set? */
30 #define EXACTLY_ONE(a, b, c) \
31 (( a && !b && !c) || \
35 static ASN1_OBJECT
*txt2obj(const char *oid
);
36 static CONF
*load_config_file(const char *configfile
);
38 /* Query related functions. */
39 static int query_command(const char *data
, const char *digest
,
40 const EVP_MD
*md
, const char *policy
, int no_nonce
,
41 int cert
, const char *in
, const char *out
, int text
);
42 static TS_REQ
*create_query(BIO
*data_bio
, const char *digest
, const EVP_MD
*md
,
43 const char *policy
, int no_nonce
, int cert
);
44 static int create_digest(BIO
*input
, const char *digest
,
45 const EVP_MD
*md
, unsigned char **md_value
);
46 static ASN1_INTEGER
*create_nonce(int bits
);
48 /* Reply related functions. */
49 static int reply_command(CONF
*conf
, const char *section
, const char *engine
,
50 const char *queryfile
, const char *passin
, const char *inkey
,
51 const EVP_MD
*md
, const char *signer
, const char *chain
,
52 const char *policy
, const char *in
, int token_in
,
53 const char *out
, int token_out
, int text
);
54 static TS_RESP
*read_PKCS7(BIO
*in_bio
);
55 static TS_RESP
*create_response(CONF
*conf
, const char *section
, const char *engine
,
56 const char *queryfile
, const char *passin
,
57 const char *inkey
, const EVP_MD
*md
, const char *signer
,
58 const char *chain
, const char *policy
);
59 static ASN1_INTEGER
*serial_cb(TS_RESP_CTX
*ctx
, void *data
);
60 static ASN1_INTEGER
*next_serial(const char *serialfile
);
61 static int save_ts_serial(const char *serialfile
, ASN1_INTEGER
*serial
);
63 /* Verify related functions. */
64 static int verify_command(const char *data
, const char *digest
, const char *queryfile
,
65 const char *in
, int token_in
,
66 const char *CApath
, const char *CAfile
,
68 char *untrusted
, X509_VERIFY_PARAM
*vpm
);
69 static TS_VERIFY_CTX
*create_verify_ctx(const char *data
, const char *digest
,
70 const char *queryfile
,
71 const char *CApath
, const char *CAfile
,
74 X509_VERIFY_PARAM
*vpm
);
75 static X509_STORE
*create_cert_store(const char *CApath
, const char *CAfile
,
76 const char *CAstore
, X509_VERIFY_PARAM
*vpm
);
77 static int verify_cb(int ok
, X509_STORE_CTX
*ctx
);
79 typedef enum OPTION_choice
{
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_CASTORE
, OPT_UNTRUSTED
,
86 OPT_MD
, OPT_V_ENUM
, OPT_R_ENUM
, OPT_PROV_ENUM
89 const OPTIONS ts_options
[] = {
90 OPT_SECTION("General"),
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 #ifndef OPENSSL_NO_ENGINE
95 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
97 {"inkey", OPT_INKEY
, 's', "File with private key for reply"},
98 {"signer", OPT_SIGNER
, 's', "Signer certificate file"},
99 {"chain", OPT_CHAIN
, '<', "File with signer CA chain"},
100 {"CAfile", OPT_CAFILE
, '<', "File with trusted CA certs"},
101 {"CApath", OPT_CAPATH
, '/', "Path to trusted CA files"},
102 {"CAstore", OPT_CASTORE
, ':', "URI to trusted CA store"},
103 {"untrusted", OPT_UNTRUSTED
, '<', "Extra untrusted certs"},
104 {"token_in", OPT_TOKEN_IN
, '-', "Input is a PKCS#7 file"},
105 {"token_out", OPT_TOKEN_OUT
, '-', "Output is a PKCS#7 file"},
106 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
107 {"", OPT_MD
, '-', "Any supported digest"},
109 OPT_SECTION("Query"),
110 {"query", OPT_QUERY
, '-', "Generate a TS query"},
111 {"data", OPT_DATA
, '<', "File to hash"},
112 {"digest", OPT_DIGEST
, 's', "Digest (as a hex string)"},
113 {"queryfile", OPT_QUERYFILE
, '<', "File containing a TS query"},
114 {"cert", OPT_CERT
, '-', "Put cert request into query"},
115 {"in", OPT_IN
, '<', "Input file"},
117 OPT_SECTION("Verify"),
118 {"verify", OPT_VERIFY
, '-', "Verify a TS response"},
119 {"reply", OPT_REPLY
, '-', "Generate a TS reply"},
120 {"tspolicy", OPT_TSPOLICY
, 's', "Policy OID to use"},
121 {"no_nonce", OPT_NO_NONCE
, '-', "Do not include a nonce"},
122 {"out", OPT_OUT
, '>', "Output file"},
123 {"text", OPT_TEXT
, '-', "Output text (not DER)"},
132 * This command is so complex, special help is needed.
134 static char* opt_helplist
[] = {
137 " openssl ts -query [-rand file...] [-config file] [-data file]",
138 " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]",
139 " [-in file] [-out file] [-text]",
141 " openssl ts -reply [-config file] [-section tsa_section]",
142 " [-queryfile file] [-passin password]",
143 " [-signer tsa_cert.pem] [-inkey private_key.pem]",
144 " [-chain certs_file.pem] [-tspolicy oid]",
145 " [-in file] [-token_in] [-out file] [-token_out]",
146 #ifndef OPENSSL_NO_ENGINE
147 " [-text] [-engine id]",
152 " openssl ts -verify -CApath dir -CAfile root-cert.pem -CAstore uri",
153 " -untrusted extra-certs.pem [-data file] [-digest hexstring]",
154 " [-queryfile request.tsq] -in response.tsr [-token_in] ...",
158 int ts_main(int argc
, char **argv
)
161 const char *CAfile
= NULL
, *prog
;
162 char *untrusted
= NULL
;
163 const char *configfile
= default_config_file
, *engine
= NULL
;
164 const char *section
= NULL
, *digestname
= NULL
;
166 char *password
= NULL
;
167 char *data
= NULL
, *digest
= NULL
, *policy
= NULL
;
168 char *in
= NULL
, *out
= NULL
, *queryfile
= NULL
, *passin
= NULL
;
169 char *inkey
= NULL
, *signer
= NULL
, *chain
= NULL
, *CApath
= NULL
;
170 char *CAstore
= NULL
;
172 OPTION_CHOICE o
, mode
= OPT_ERR
;
173 int ret
= 1, no_nonce
= 0, cert
= 0, text
= 0;
175 X509_VERIFY_PARAM
*vpm
= NULL
;
176 /* Input is ContentInfo instead of TimeStampResp. */
178 /* Output is ContentInfo instead of TimeStampResp. */
181 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
184 opt_set_unknown_name("digest");
185 prog
= opt_init(argc
, argv
, ts_options
);
186 while ((o
= opt_next()) != OPT_EOF
) {
191 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
194 opt_help(ts_options
);
195 for (helpp
= opt_helplist
; *helpp
; ++helpp
)
196 BIO_printf(bio_err
, "%s\n", *helpp
);
200 configfile
= opt_arg();
208 if (mode
!= OPT_ERR
) {
209 BIO_printf(bio_err
, "%s: Must give only one of -query, -reply, or -verify\n", prog
);
225 if (!opt_provider(o
))
253 queryfile
= opt_arg();
277 untrusted
= opt_arg();
283 digestname
= opt_unknown();
286 if (!opt_verify(o
, vpm
))
293 /* No extra arguments. */
294 if (!opt_check_rest_arg(NULL
))
296 if (mode
== OPT_ERR
) {
297 BIO_printf(bio_err
, "%s: Must give one of -query, -reply, or -verify\n", prog
);
301 if (!app_RAND_load())
304 if (!opt_md(digestname
, &md
))
306 if (mode
== OPT_REPLY
&& passin
&&
307 !app_passwd(passin
, NULL
, &password
, NULL
)) {
308 BIO_printf(bio_err
, "Error getting password.\n");
312 if ((conf
= load_config_file(configfile
)) == NULL
)
314 if (configfile
!= default_config_file
&& !app_load_modules(conf
))
317 /* Check parameter consistency and execute the appropriate function. */
318 if (mode
== OPT_QUERY
) {
321 if ((data
!= NULL
) && (digest
!= NULL
))
323 ret
= !query_command(data
, digest
, md
, policy
, no_nonce
, cert
,
325 } else if (mode
== OPT_REPLY
) {
328 if ((in
!= NULL
) && (queryfile
!= NULL
))
331 if ((conf
== NULL
) || (token_in
!= 0))
334 ret
= !reply_command(conf
, section
, engine
, queryfile
,
335 password
, inkey
, md
, signer
, chain
, policy
,
336 in
, token_in
, out
, token_out
, text
);
338 } else if (mode
== OPT_VERIFY
) {
339 if ((in
== NULL
) || !EXACTLY_ONE(queryfile
, data
, digest
))
341 ret
= !verify_command(data
, digest
, queryfile
, in
, token_in
,
342 CApath
, CAfile
, CAstore
, untrusted
,
343 vpmtouched
? vpm
: NULL
);
349 X509_VERIFY_PARAM_free(vpm
);
352 OPENSSL_free(password
);
357 * Configuration file-related function definitions.
360 static ASN1_OBJECT
*txt2obj(const char *oid
)
362 ASN1_OBJECT
*oid_obj
= NULL
;
364 if ((oid_obj
= OBJ_txt2obj(oid
, 0)) == NULL
)
365 BIO_printf(bio_err
, "cannot convert %s to OID\n", oid
);
370 static CONF
*load_config_file(const char *configfile
)
372 CONF
*conf
= app_load_config(configfile
);
377 BIO_printf(bio_err
, "Using configuration from %s\n", configfile
);
378 p
= NCONF_get_string(conf
, NULL
, ENV_OID_FILE
);
380 BIO
*oid_bio
= BIO_new_file(p
, "r");
382 ERR_print_errors(bio_err
);
384 OBJ_create_objects(oid_bio
);
385 BIO_free_all(oid_bio
);
389 if (!add_oid_section(conf
))
390 ERR_print_errors(bio_err
);
396 * Query-related method definitions.
398 static int query_command(const char *data
, const char *digest
, const EVP_MD
*md
,
399 const char *policy
, int no_nonce
,
400 int cert
, const char *in
, const char *out
, int text
)
403 TS_REQ
*query
= NULL
;
405 BIO
*data_bio
= NULL
;
408 /* Build query object. */
410 if ((in_bio
= bio_open_default(in
, 'r', FORMAT_ASN1
)) == NULL
)
412 query
= d2i_TS_REQ_bio(in_bio
, NULL
);
415 && (data_bio
= bio_open_default(data
, 'r', FORMAT_ASN1
)) == NULL
)
417 query
= create_query(data_bio
, digest
, md
, policy
, no_nonce
, cert
);
423 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_TEXT
)) == NULL
)
425 if (!TS_REQ_print_bio(out_bio
, query
))
428 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_ASN1
)) == NULL
)
430 if (!i2d_TS_REQ_bio(out_bio
, query
))
437 ERR_print_errors(bio_err
);
438 BIO_free_all(in_bio
);
439 BIO_free_all(data_bio
);
440 BIO_free_all(out_bio
);
445 static TS_REQ
*create_query(BIO
*data_bio
, const char *digest
, const EVP_MD
*md
,
446 const char *policy
, int no_nonce
, int cert
)
449 TS_REQ
*ts_req
= NULL
;
451 TS_MSG_IMPRINT
*msg_imprint
= NULL
;
452 X509_ALGOR
*algo
= NULL
;
453 unsigned char *data
= NULL
;
454 ASN1_OBJECT
*policy_obj
= NULL
;
455 ASN1_INTEGER
*nonce_asn1
= NULL
;
457 if (md
== NULL
&& (md
= EVP_get_digestbyname("sha256")) == NULL
)
459 if ((ts_req
= TS_REQ_new()) == NULL
)
461 if (!TS_REQ_set_version(ts_req
, 1))
463 if ((msg_imprint
= TS_MSG_IMPRINT_new()) == NULL
)
465 if ((algo
= X509_ALGOR_new()) == NULL
)
467 if ((algo
->algorithm
= OBJ_nid2obj(EVP_MD_get_type(md
))) == NULL
)
469 if ((algo
->parameter
= ASN1_TYPE_new()) == NULL
)
471 algo
->parameter
->type
= V_ASN1_NULL
;
472 if (!TS_MSG_IMPRINT_set_algo(msg_imprint
, algo
))
474 if ((len
= create_digest(data_bio
, digest
, md
, &data
)) == 0)
476 if (!TS_MSG_IMPRINT_set_msg(msg_imprint
, data
, len
))
478 if (!TS_REQ_set_msg_imprint(ts_req
, msg_imprint
))
480 if (policy
&& (policy_obj
= txt2obj(policy
)) == NULL
)
482 if (policy_obj
&& !TS_REQ_set_policy_id(ts_req
, policy_obj
))
485 /* Setting nonce if requested. */
486 if (!no_nonce
&& (nonce_asn1
= create_nonce(NONCE_LENGTH
)) == NULL
)
488 if (nonce_asn1
&& !TS_REQ_set_nonce(ts_req
, nonce_asn1
))
490 if (!TS_REQ_set_cert_req(ts_req
, cert
))
498 BIO_printf(bio_err
, "could not create query\n");
499 ERR_print_errors(bio_err
);
501 TS_MSG_IMPRINT_free(msg_imprint
);
502 X509_ALGOR_free(algo
);
504 ASN1_OBJECT_free(policy_obj
);
505 ASN1_INTEGER_free(nonce_asn1
);
509 static int create_digest(BIO
*input
, const char *digest
, const EVP_MD
*md
,
510 unsigned char **md_value
)
514 EVP_MD_CTX
*md_ctx
= NULL
;
516 md_value_len
= EVP_MD_get_size(md
);
517 if (md_value_len
< 0)
521 unsigned char buffer
[4096];
524 md_ctx
= EVP_MD_CTX_new();
527 *md_value
= app_malloc(md_value_len
, "digest buffer");
528 if (!EVP_DigestInit(md_ctx
, md
))
530 while ((length
= BIO_read(input
, buffer
, sizeof(buffer
))) > 0) {
531 if (!EVP_DigestUpdate(md_ctx
, buffer
, length
))
534 if (!EVP_DigestFinal(md_ctx
, *md_value
, NULL
))
536 md_value_len
= EVP_MD_get_size(md
);
540 *md_value
= OPENSSL_hexstr2buf(digest
, &digest_len
);
541 if (*md_value
== NULL
|| md_value_len
!= digest_len
) {
542 OPENSSL_free(*md_value
);
544 BIO_printf(bio_err
, "bad digest, %d bytes "
545 "must be specified\n", md_value_len
);
551 EVP_MD_CTX_free(md_ctx
);
555 static ASN1_INTEGER
*create_nonce(int bits
)
557 unsigned char buf
[20];
558 ASN1_INTEGER
*nonce
= NULL
;
559 int len
= (bits
- 1) / 8 + 1;
562 if (len
> (int)sizeof(buf
))
564 if (RAND_bytes(buf
, len
) <= 0)
567 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
568 for (i
= 0; i
< len
&& !buf
[i
]; ++i
)
570 if ((nonce
= ASN1_INTEGER_new()) == NULL
)
572 OPENSSL_free(nonce
->data
);
573 nonce
->length
= len
- i
;
574 nonce
->data
= app_malloc(nonce
->length
+ 1, "nonce buffer");
575 memcpy(nonce
->data
, buf
+ i
, nonce
->length
);
579 BIO_printf(bio_err
, "could not create nonce\n");
580 ASN1_INTEGER_free(nonce
);
585 * Reply-related method definitions.
588 static int reply_command(CONF
*conf
, const char *section
, const char *engine
,
589 const char *queryfile
, const char *passin
, const char *inkey
,
590 const EVP_MD
*md
, const char *signer
, const char *chain
,
591 const char *policy
, const char *in
, int token_in
,
592 const char *out
, int token_out
, int text
)
595 TS_RESP
*response
= NULL
;
597 BIO
*query_bio
= NULL
;
598 BIO
*inkey_bio
= NULL
;
599 BIO
*signer_bio
= NULL
;
603 if ((in_bio
= BIO_new_file(in
, "rb")) == NULL
)
606 response
= read_PKCS7(in_bio
);
608 response
= d2i_TS_RESP_bio(in_bio
, NULL
);
611 response
= create_response(conf
, section
, engine
, queryfile
,
612 passin
, inkey
, md
, signer
, chain
, policy
);
613 if (response
!= NULL
)
614 BIO_printf(bio_err
, "Response has been generated.\n");
616 BIO_printf(bio_err
, "Response is not generated.\n");
618 if (response
== NULL
)
621 /* Write response. */
623 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_TEXT
)) == NULL
)
626 TS_TST_INFO
*tst_info
= TS_RESP_get_tst_info(response
);
627 if (!TS_TST_INFO_print_bio(out_bio
, tst_info
))
630 if (!TS_RESP_print_bio(out_bio
, response
))
634 if ((out_bio
= bio_open_default(out
, 'w', FORMAT_ASN1
)) == NULL
)
637 PKCS7
*token
= TS_RESP_get_token(response
);
638 if (!i2d_PKCS7_bio(out_bio
, token
))
641 if (!i2d_TS_RESP_bio(out_bio
, response
))
649 ERR_print_errors(bio_err
);
650 BIO_free_all(in_bio
);
651 BIO_free_all(query_bio
);
652 BIO_free_all(inkey_bio
);
653 BIO_free_all(signer_bio
);
654 BIO_free_all(out_bio
);
655 TS_RESP_free(response
);
659 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
660 static TS_RESP
*read_PKCS7(BIO
*in_bio
)
664 TS_TST_INFO
*tst_info
= NULL
;
665 TS_RESP
*resp
= NULL
;
666 TS_STATUS_INFO
*si
= NULL
;
668 if ((token
= d2i_PKCS7_bio(in_bio
, NULL
)) == NULL
)
670 if ((tst_info
= PKCS7_to_TS_TST_INFO(token
)) == NULL
)
672 if ((resp
= TS_RESP_new()) == NULL
)
674 if ((si
= TS_STATUS_INFO_new()) == NULL
)
676 if (!TS_STATUS_INFO_set_status(si
, TS_STATUS_GRANTED
))
678 if (!TS_RESP_set_status_info(resp
, si
))
680 TS_RESP_set_tst_info(resp
, token
, tst_info
);
681 token
= NULL
; /* Ownership is lost. */
682 tst_info
= NULL
; /* Ownership is lost. */
687 TS_TST_INFO_free(tst_info
);
692 TS_STATUS_INFO_free(si
);
696 static TS_RESP
*create_response(CONF
*conf
, const char *section
, const char *engine
,
697 const char *queryfile
, const char *passin
,
698 const char *inkey
, const EVP_MD
*md
, const char *signer
,
699 const char *chain
, const char *policy
)
702 TS_RESP
*response
= NULL
;
703 BIO
*query_bio
= NULL
;
704 TS_RESP_CTX
*resp_ctx
= NULL
;
706 if ((query_bio
= BIO_new_file(queryfile
, "rb")) == NULL
)
708 if ((section
= TS_CONF_get_tsa_section(conf
, section
)) == NULL
)
710 if ((resp_ctx
= TS_RESP_CTX_new()) == NULL
)
712 if (!TS_CONF_set_serial(conf
, section
, serial_cb
, resp_ctx
))
714 #ifndef OPENSSL_NO_ENGINE
715 if (!TS_CONF_set_crypto_device(conf
, section
, engine
))
718 if (!TS_CONF_set_signer_cert(conf
, section
, signer
, resp_ctx
))
720 if (!TS_CONF_set_certs(conf
, section
, chain
, resp_ctx
))
722 if (!TS_CONF_set_signer_key(conf
, section
, inkey
, passin
, resp_ctx
))
726 if (!TS_RESP_CTX_set_signer_digest(resp_ctx
, md
))
728 } else if (!TS_CONF_set_signer_digest(conf
, section
, NULL
, resp_ctx
)) {
732 if (!TS_CONF_set_ess_cert_id_digest(conf
, section
, resp_ctx
))
734 if (!TS_CONF_set_def_policy(conf
, section
, policy
, resp_ctx
))
736 if (!TS_CONF_set_policies(conf
, section
, resp_ctx
))
738 if (!TS_CONF_set_digests(conf
, section
, resp_ctx
))
740 if (!TS_CONF_set_accuracy(conf
, section
, resp_ctx
))
742 if (!TS_CONF_set_clock_precision_digits(conf
, section
, resp_ctx
))
744 if (!TS_CONF_set_ordering(conf
, section
, resp_ctx
))
746 if (!TS_CONF_set_tsa_name(conf
, section
, resp_ctx
))
748 if (!TS_CONF_set_ess_cert_id_chain(conf
, section
, resp_ctx
))
750 if ((response
= TS_RESP_create_response(resp_ctx
, query_bio
)) == NULL
)
756 TS_RESP_free(response
);
759 TS_RESP_CTX_free(resp_ctx
);
760 BIO_free_all(query_bio
);
764 static ASN1_INTEGER
*serial_cb(TS_RESP_CTX
*ctx
, void *data
)
766 const char *serial_file
= (const char *)data
;
767 ASN1_INTEGER
*serial
= next_serial(serial_file
);
769 if (serial
== NULL
) {
770 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
771 "Error during serial number "
773 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_ADD_INFO_NOT_AVAILABLE
);
775 save_ts_serial(serial_file
, serial
);
781 static ASN1_INTEGER
*next_serial(const char *serialfile
)
785 ASN1_INTEGER
*serial
= NULL
;
788 if ((serial
= ASN1_INTEGER_new()) == NULL
)
791 if ((in
= BIO_new_file(serialfile
, "r")) == NULL
) {
793 BIO_printf(bio_err
, "Warning: could not open file %s for "
794 "reading, using serial number: 1\n", serialfile
);
795 if (!ASN1_INTEGER_set(serial
, 1))
799 if (!a2i_ASN1_INTEGER(in
, serial
, buf
, sizeof(buf
))) {
800 BIO_printf(bio_err
, "unable to load number from %s\n",
804 if ((bn
= ASN1_INTEGER_to_BN(serial
, NULL
)) == NULL
)
806 ASN1_INTEGER_free(serial
);
808 if (!BN_add_word(bn
, 1))
810 if ((serial
= BN_to_ASN1_INTEGER(bn
, NULL
)) == NULL
)
817 ASN1_INTEGER_free(serial
);
825 static int save_ts_serial(const char *serialfile
, ASN1_INTEGER
*serial
)
830 if ((out
= BIO_new_file(serialfile
, "w")) == NULL
)
832 if (i2a_ASN1_INTEGER(out
, serial
) <= 0)
834 if (BIO_puts(out
, "\n") <= 0)
839 BIO_printf(bio_err
, "could not save serial number to %s\n",
847 * Verify-related method definitions.
850 static int verify_command(const char *data
, const char *digest
, const char *queryfile
,
851 const char *in
, int token_in
,
852 const char *CApath
, const char *CAfile
,
853 const char *CAstore
, char *untrusted
,
854 X509_VERIFY_PARAM
*vpm
)
858 TS_RESP
*response
= NULL
;
859 TS_VERIFY_CTX
*verify_ctx
= NULL
;
862 if ((in_bio
= BIO_new_file(in
, "rb")) == NULL
)
865 if ((token
= d2i_PKCS7_bio(in_bio
, NULL
)) == NULL
)
868 if ((response
= d2i_TS_RESP_bio(in_bio
, NULL
)) == NULL
)
872 if ((verify_ctx
= create_verify_ctx(data
, digest
, queryfile
,
873 CApath
, CAfile
, CAstore
, untrusted
,
878 ? TS_RESP_verify_token(verify_ctx
, token
)
879 : TS_RESP_verify_response(verify_ctx
, response
);
882 printf("Verification: ");
887 ERR_print_errors(bio_err
);
890 BIO_free_all(in_bio
);
892 TS_RESP_free(response
);
893 TS_VERIFY_CTX_free(verify_ctx
);
897 static TS_VERIFY_CTX
*create_verify_ctx(const char *data
, const char *digest
,
898 const char *queryfile
,
899 const char *CApath
, const char *CAfile
,
902 X509_VERIFY_PARAM
*vpm
)
904 TS_VERIFY_CTX
*ctx
= NULL
;
905 STACK_OF(X509
) *certs
;
907 TS_REQ
*request
= NULL
;
911 if (data
!= NULL
|| digest
!= NULL
) {
912 if ((ctx
= TS_VERIFY_CTX_new()) == NULL
)
914 f
= TS_VFY_VERSION
| TS_VFY_SIGNER
;
919 if ((out
= BIO_new_file(data
, "rb")) == NULL
)
921 if (TS_VERIFY_CTX_set_data(ctx
, out
) == NULL
) {
925 } else if (digest
!= NULL
) {
927 unsigned char *hexstr
= OPENSSL_hexstr2buf(digest
, &imprint_len
);
929 if (TS_VERIFY_CTX_set_imprint(ctx
, hexstr
, imprint_len
) == NULL
) {
930 BIO_printf(bio_err
, "invalid digest string\n");
935 } else if (queryfile
!= NULL
) {
936 if ((input
= BIO_new_file(queryfile
, "rb")) == NULL
)
938 if ((request
= d2i_TS_REQ_bio(input
, NULL
)) == NULL
)
940 if ((ctx
= TS_REQ_to_TS_VERIFY_CTX(request
, NULL
)) == NULL
)
946 /* Add the signature verification flag and arguments. */
947 TS_VERIFY_CTX_add_flags(ctx
, f
| TS_VFY_SIGNATURE
);
949 /* Initialising the X509_STORE object. */
950 if (TS_VERIFY_CTX_set_store(ctx
,
951 create_cert_store(CApath
, CAfile
, CAstore
, vpm
))
955 /* Loading any extra untrusted certificates. */
956 if (untrusted
!= NULL
) {
957 certs
= load_certs_multifile(untrusted
, NULL
, "extra untrusted certs",
959 if (certs
== NULL
|| TS_VERIFY_CTX_set_certs(ctx
, certs
) == NULL
)
966 TS_VERIFY_CTX_free(ctx
);
970 TS_REQ_free(request
);
974 static X509_STORE
*create_cert_store(const char *CApath
, const char *CAfile
,
975 const char *CAstore
, X509_VERIFY_PARAM
*vpm
)
977 X509_STORE
*cert_ctx
= NULL
;
978 X509_LOOKUP
*lookup
= NULL
;
979 OSSL_LIB_CTX
*libctx
= app_get0_libctx();
980 const char *propq
= app_get0_propq();
982 cert_ctx
= X509_STORE_new();
983 if (cert_ctx
== NULL
) {
984 BIO_printf(bio_err
, "memory allocation failure\n");
987 X509_STORE_set_verify_cb(cert_ctx
, verify_cb
);
988 if (CApath
!= NULL
) {
989 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
990 if (lookup
== NULL
) {
991 BIO_printf(bio_err
, "memory allocation failure\n");
994 if (!X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
)) {
995 BIO_printf(bio_err
, "Error loading directory %s\n", CApath
);
1000 if (CAfile
!= NULL
) {
1001 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
1002 if (lookup
== NULL
) {
1003 BIO_printf(bio_err
, "memory allocation failure\n");
1006 if (!X509_LOOKUP_load_file_ex(lookup
, CAfile
, X509_FILETYPE_PEM
, libctx
,
1008 BIO_printf(bio_err
, "Error loading file %s\n", CAfile
);
1013 if (CAstore
!= NULL
) {
1014 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_store());
1015 if (lookup
== NULL
) {
1016 BIO_printf(bio_err
, "memory allocation failure\n");
1019 if (!X509_LOOKUP_load_store_ex(lookup
, CAstore
, libctx
, propq
)) {
1020 BIO_printf(bio_err
, "Error loading store URI %s\n", CAstore
);
1026 X509_STORE_set1_param(cert_ctx
, vpm
);
1031 X509_STORE_free(cert_ctx
);
1035 static int verify_cb(int ok
, X509_STORE_CTX
*ctx
)