3 * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
64 #include <openssl/bio.h>
65 #include <openssl/err.h>
66 #include <openssl/pem.h>
67 #include <openssl/rand.h>
68 #include <openssl/ts.h>
69 #include <openssl/bn.h>
74 /* Length of the nonce of the request in bits (must be a multiple of 8). */
75 #define NONCE_LENGTH 64
77 /* Macro definitions for the configuration file. */
78 #define ENV_OID_FILE "oid_file"
80 /* Local function declarations. */
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 BIO
*BIO_open_with_default(const char *file
, const char *mode
,
91 static TS_REQ
*create_query(BIO
*data_bio
, char *digest
, const EVP_MD
*md
,
92 const char *policy
, int no_nonce
, int cert
);
93 static int create_digest(BIO
*input
, char *digest
,
94 const EVP_MD
*md
, unsigned char **md_value
);
95 static ASN1_INTEGER
*create_nonce(int bits
);
97 /* Reply related functions. */
98 static int reply_command(CONF
*conf
, char *section
, char *engine
,
99 char *queryfile
, char *passin
, char *inkey
,
100 char *signer
, char *chain
, const char *policy
,
101 char *in
, int token_in
, char *out
, int token_out
,
103 static TS_RESP
*read_PKCS7(BIO
*in_bio
);
104 static TS_RESP
*create_response(CONF
*conf
, const char *section
, char *engine
,
105 char *queryfile
, char *passin
, char *inkey
,
106 char *signer
, char *chain
,
108 static ASN1_INTEGER
*MS_CALLBACK
serial_cb(TS_RESP_CTX
*ctx
, void *data
);
109 static ASN1_INTEGER
*next_serial(const char *serialfile
);
110 static int save_ts_serial(const char *serialfile
, ASN1_INTEGER
*serial
);
112 /* Verify related functions. */
113 static int verify_command(char *data
, char *digest
, char *queryfile
,
114 char *in
, int token_in
,
115 char *ca_path
, char *ca_file
, char *untrusted
);
116 static TS_VERIFY_CTX
*create_verify_ctx(char *data
, char *digest
,
118 char *ca_path
, char *ca_file
,
120 static X509_STORE
*create_cert_store(char *ca_path
, char *ca_file
);
121 static int MS_CALLBACK
verify_cb(int ok
, X509_STORE_CTX
*ctx
);
123 /* Main function definition. */
124 int MAIN(int, char **);
126 int MAIN(int argc
, char **argv
)
129 char *configfile
= NULL
;
130 char *section
= NULL
;
133 CMD_NONE
, CMD_QUERY
, CMD_REPLY
, CMD_VERIFY
137 const EVP_MD
*md
= NULL
;
145 char *queryfile
= NULL
;
146 char *passin
= NULL
; /* Password source. */
147 char *password
= NULL
; /* Password itself. */
151 char *ca_path
= NULL
;
152 char *ca_file
= NULL
;
153 char *untrusted
= NULL
;
155 /* Input is ContentInfo instead of TimeStampResp. */
157 /* Output is ContentInfo instead of TimeStampResp. */
159 int free_bio_err
= 0;
161 ERR_load_crypto_strings();
164 if (bio_err
== NULL
&& (bio_err
= BIO_new(BIO_s_file())) != NULL
) {
166 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
169 if (!load_config(bio_err
, NULL
))
172 for (argc
--, argv
++; argc
> 0; argc
--, argv
++) {
173 if (strcmp(*argv
, "-config") == 0) {
176 configfile
= *++argv
;
177 } else if (strcmp(*argv
, "-section") == 0) {
181 } else if (strcmp(*argv
, "-query") == 0) {
182 if (mode
!= CMD_NONE
)
185 } else if (strcmp(*argv
, "-data") == 0) {
189 } else if (strcmp(*argv
, "-digest") == 0) {
193 } else if (strcmp(*argv
, "-rand") == 0) {
197 } else if (strcmp(*argv
, "-policy") == 0) {
201 } else if (strcmp(*argv
, "-no_nonce") == 0) {
203 } else if (strcmp(*argv
, "-cert") == 0) {
205 } else if (strcmp(*argv
, "-in") == 0) {
209 } else if (strcmp(*argv
, "-token_in") == 0) {
211 } else if (strcmp(*argv
, "-out") == 0) {
215 } else if (strcmp(*argv
, "-token_out") == 0) {
217 } else if (strcmp(*argv
, "-text") == 0) {
219 } else if (strcmp(*argv
, "-reply") == 0) {
220 if (mode
!= CMD_NONE
)
223 } else if (strcmp(*argv
, "-queryfile") == 0) {
227 } else if (strcmp(*argv
, "-passin") == 0) {
231 } else if (strcmp(*argv
, "-inkey") == 0) {
235 } else if (strcmp(*argv
, "-signer") == 0) {
239 } else if (strcmp(*argv
, "-chain") == 0) {
243 } else if (strcmp(*argv
, "-verify") == 0) {
244 if (mode
!= CMD_NONE
)
247 } else if (strcmp(*argv
, "-CApath") == 0) {
251 } else if (strcmp(*argv
, "-CAfile") == 0) {
255 } else if (strcmp(*argv
, "-untrusted") == 0) {
259 } else if (strcmp(*argv
, "-engine") == 0) {
263 } else if ((md
= EVP_get_digestbyname(*argv
+ 1)) != NULL
) {
269 /* Seed the random number generator if it is going to be used. */
270 if (mode
== CMD_QUERY
&& !no_nonce
) {
271 if (!app_RAND_load_file(NULL
, bio_err
, 1) && rnd
== NULL
)
272 BIO_printf(bio_err
, "warning, not much extra random "
273 "data, consider using the -rand option\n");
275 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
276 app_RAND_load_files(rnd
));
279 /* Get the password if required. */
280 if (mode
== CMD_REPLY
&& passin
&&
281 !app_passwd(bio_err
, passin
, NULL
, &password
, NULL
)) {
282 BIO_printf(bio_err
, "Error getting password.\n");
287 * Check consistency of parameters and execute the appropriate function.
294 * Data file and message imprint cannot be specified at the same
297 ret
= data
!= NULL
&& digest
!= NULL
;
300 /* Load the config file for possible policy OIDs. */
301 conf
= load_config_file(configfile
);
302 ret
= !query_command(data
, digest
, md
, policy
, no_nonce
, cert
,
306 conf
= load_config_file(configfile
);
308 ret
= !(queryfile
!= NULL
&& conf
!= NULL
&& !token_in
);
312 /* 'in' and 'queryfile' are exclusive. */
313 ret
= !(queryfile
== NULL
);
318 ret
= !reply_command(conf
, section
, engine
, queryfile
,
319 password
, inkey
, signer
, chain
, policy
,
320 in
, token_in
, out
, token_out
, text
);
323 ret
= !(((queryfile
&& !data
&& !digest
)
324 || (!queryfile
&& data
&& !digest
)
325 || (!queryfile
&& !data
&& digest
))
330 ret
= !verify_command(data
, digest
, queryfile
, in
, token_in
,
331 ca_path
, ca_file
, untrusted
);
337 BIO_printf(bio_err
, "usage:\n"
338 "ts -query [-rand file%cfile%c...] [-config configfile] "
339 "[-data file_to_hash] [-digest digest_bytes]"
340 "[-md2|-md4|-md5|-sha|-sha1|-mdc2|-ripemd160] "
341 "[-policy object_id] [-no_nonce] [-cert] "
342 "[-in request.tsq] [-out request.tsq] [-text]\n",
343 LIST_SEPARATOR_CHAR
, LIST_SEPARATOR_CHAR
);
344 BIO_printf(bio_err
, "or\n"
345 "ts -reply [-config configfile] [-section tsa_section] "
346 "[-queryfile request.tsq] [-passin password] "
347 "[-signer tsa_cert.pem] [-inkey private_key.pem] "
348 "[-chain certs_file.pem] [-policy object_id] "
349 "[-in response.tsr] [-token_in] "
350 "[-out response.tsr] [-token_out] [-text] [-engine id]\n");
351 BIO_printf(bio_err
, "or\n"
352 "ts -verify [-data file_to_hash] [-digest digest_bytes] "
353 "[-queryfile request.tsq] "
354 "-in response.tsr [-token_in] "
355 "-CApath ca_path -CAfile ca_file.pem "
356 "-untrusted cert_file.pem\n");
359 app_RAND_write_file(NULL
, bio_err
);
361 OPENSSL_free(password
);
364 BIO_free_all(bio_err
);
372 * Configuration file-related function definitions.
375 static ASN1_OBJECT
*txt2obj(const char *oid
)
377 ASN1_OBJECT
*oid_obj
= NULL
;
379 if (!(oid_obj
= OBJ_txt2obj(oid
, 0)))
380 BIO_printf(bio_err
, "cannot convert %s to OID\n", oid
);
385 static CONF
*load_config_file(const char *configfile
)
391 configfile
= getenv("OPENSSL_CONF");
393 configfile
= getenv("SSLEAY_CONF");
396 (!(conf
= NCONF_new(NULL
)) ||
397 NCONF_load(conf
, configfile
, &errorline
) <= 0)) {
399 BIO_printf(bio_err
, "error loading the config file "
400 "'%s'\n", configfile
);
402 BIO_printf(bio_err
, "error on line %ld of config file "
403 "'%s'\n", errorline
, 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(bio_err
, conf
))
422 ERR_print_errors(bio_err
);
428 * Query-related method definitions.
431 static int query_command(const char *data
, char *digest
, const EVP_MD
*md
,
432 const char *policy
, int no_nonce
,
433 int cert
, const char *in
, const char *out
, int text
)
436 TS_REQ
*query
= NULL
;
438 BIO
*data_bio
= NULL
;
441 /* Build query object either from file or from scratch. */
443 if ((in_bio
= BIO_new_file(in
, "rb")) == NULL
)
445 query
= d2i_TS_REQ_bio(in_bio
, NULL
);
448 * Open the file if no explicit digest bytes were specified.
450 if (!digest
&& !(data_bio
= BIO_open_with_default(data
, "rb", stdin
)))
452 /* Creating the query object. */
453 query
= create_query(data_bio
, digest
, md
, policy
, no_nonce
, cert
);
454 /* Saving the random number generator state. */
459 /* Write query either in ASN.1 or in text format. */
460 if ((out_bio
= BIO_open_with_default(out
, "wb", stdout
)) == NULL
)
464 if (!TS_REQ_print_bio(out_bio
, query
))
468 if (!i2d_TS_REQ_bio(out_bio
, query
))
475 ERR_print_errors(bio_err
);
478 BIO_free_all(in_bio
);
479 BIO_free_all(data_bio
);
480 BIO_free_all(out_bio
);
486 static BIO
*BIO_open_with_default(const char *file
, const char *mode
,
489 return file
== NULL
? BIO_new_fp(default_fp
, BIO_NOCLOSE
)
490 : BIO_new_file(file
, mode
);
493 static TS_REQ
*create_query(BIO
*data_bio
, char *digest
, const EVP_MD
*md
,
494 const char *policy
, int no_nonce
, int cert
)
497 TS_REQ
*ts_req
= NULL
;
499 TS_MSG_IMPRINT
*msg_imprint
= NULL
;
500 X509_ALGOR
*algo
= NULL
;
501 unsigned char *data
= NULL
;
502 ASN1_OBJECT
*policy_obj
= NULL
;
503 ASN1_INTEGER
*nonce_asn1
= NULL
;
505 /* Setting default message digest. */
506 if (!md
&& !(md
= EVP_get_digestbyname("sha1")))
509 /* Creating request object. */
510 if (!(ts_req
= TS_REQ_new()))
513 /* Setting version. */
514 if (!TS_REQ_set_version(ts_req
, 1))
517 /* Creating and adding MSG_IMPRINT object. */
518 if (!(msg_imprint
= TS_MSG_IMPRINT_new()))
521 /* Adding algorithm. */
522 if (!(algo
= X509_ALGOR_new()))
524 if (!(algo
->algorithm
= OBJ_nid2obj(EVP_MD_type(md
))))
526 if (!(algo
->parameter
= ASN1_TYPE_new()))
528 algo
->parameter
->type
= V_ASN1_NULL
;
529 if (!TS_MSG_IMPRINT_set_algo(msg_imprint
, algo
))
532 /* Adding message digest. */
533 if ((len
= create_digest(data_bio
, digest
, md
, &data
)) == 0)
535 if (!TS_MSG_IMPRINT_set_msg(msg_imprint
, data
, len
))
538 if (!TS_REQ_set_msg_imprint(ts_req
, msg_imprint
))
541 /* Setting policy if requested. */
542 if (policy
&& !(policy_obj
= txt2obj(policy
)))
544 if (policy_obj
&& !TS_REQ_set_policy_id(ts_req
, policy_obj
))
547 /* Setting nonce if requested. */
548 if (!no_nonce
&& !(nonce_asn1
= create_nonce(NONCE_LENGTH
)))
550 if (nonce_asn1
&& !TS_REQ_set_nonce(ts_req
, nonce_asn1
))
553 /* Setting certificate request flag if requested. */
554 if (!TS_REQ_set_cert_req(ts_req
, cert
))
562 BIO_printf(bio_err
, "could not create query\n");
564 TS_MSG_IMPRINT_free(msg_imprint
);
565 X509_ALGOR_free(algo
);
567 ASN1_OBJECT_free(policy_obj
);
568 ASN1_INTEGER_free(nonce_asn1
);
572 static int create_digest(BIO
*input
, char *digest
, const EVP_MD
*md
,
573 unsigned char **md_value
)
577 md_value_len
= EVP_MD_size(md
);
578 if (md_value_len
< 0)
581 /* Digest must be computed from an input file. */
583 unsigned char buffer
[4096];
586 *md_value
= OPENSSL_malloc(md_value_len
);
590 EVP_DigestInit(&md_ctx
, md
);
591 while ((length
= BIO_read(input
, buffer
, sizeof(buffer
))) > 0) {
592 EVP_DigestUpdate(&md_ctx
, buffer
, length
);
594 EVP_DigestFinal(&md_ctx
, *md_value
, NULL
);
596 /* Digest bytes are specified with digest. */
598 *md_value
= string_to_hex(digest
, &digest_len
);
599 if (!*md_value
|| md_value_len
!= digest_len
) {
600 OPENSSL_free(*md_value
);
602 BIO_printf(bio_err
, "bad digest, %d bytes "
603 "must be specified\n", md_value_len
);
613 static ASN1_INTEGER
*create_nonce(int bits
)
615 unsigned char buf
[20];
616 ASN1_INTEGER
*nonce
= NULL
;
617 int len
= (bits
- 1) / 8 + 1;
620 /* Generating random byte sequence. */
621 if (len
> (int)sizeof(buf
))
623 if (RAND_bytes(buf
, len
) <= 0)
626 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
627 for (i
= 0; i
< len
&& !buf
[i
]; ++i
) ;
628 if (!(nonce
= ASN1_INTEGER_new()))
630 OPENSSL_free(nonce
->data
);
631 /* Allocate at least one byte. */
632 nonce
->length
= len
- i
;
633 if (!(nonce
->data
= OPENSSL_malloc(nonce
->length
+ 1)))
635 memcpy(nonce
->data
, buf
+ i
, nonce
->length
);
639 BIO_printf(bio_err
, "could not create nonce\n");
640 ASN1_INTEGER_free(nonce
);
645 * Reply-related method definitions.
648 static int reply_command(CONF
*conf
, char *section
, char *engine
,
649 char *queryfile
, char *passin
, char *inkey
,
650 char *signer
, char *chain
, const char *policy
,
651 char *in
, int token_in
,
652 char *out
, int token_out
, int text
)
655 TS_RESP
*response
= NULL
;
657 BIO
*query_bio
= NULL
;
658 BIO
*inkey_bio
= NULL
;
659 BIO
*signer_bio
= NULL
;
662 /* Build response object either from response or query. */
664 if ((in_bio
= BIO_new_file(in
, "rb")) == NULL
)
668 * We have a ContentInfo (PKCS7) object, add 'granted' status
671 response
= read_PKCS7(in_bio
);
673 /* We have a ready-made TS_RESP object. */
674 response
= d2i_TS_RESP_bio(in_bio
, NULL
);
677 response
= create_response(conf
, section
, engine
, queryfile
,
678 passin
, inkey
, signer
, chain
, policy
);
680 BIO_printf(bio_err
, "Response has been generated.\n");
682 BIO_printf(bio_err
, "Response is not generated.\n");
684 if (response
== NULL
)
687 /* Write response either in ASN.1 or text format. */
688 if ((out_bio
= BIO_open_with_default(out
, "wb", stdout
)) == NULL
)
693 TS_TST_INFO
*tst_info
= TS_RESP_get_tst_info(response
);
694 if (!TS_TST_INFO_print_bio(out_bio
, tst_info
))
697 if (!TS_RESP_print_bio(out_bio
, response
))
701 /* ASN.1 DER output. */
703 PKCS7
*token
= TS_RESP_get_token(response
);
704 if (!i2d_PKCS7_bio(out_bio
, token
))
707 if (!i2d_TS_RESP_bio(out_bio
, response
))
715 ERR_print_errors(bio_err
);
718 BIO_free_all(in_bio
);
719 BIO_free_all(query_bio
);
720 BIO_free_all(inkey_bio
);
721 BIO_free_all(signer_bio
);
722 BIO_free_all(out_bio
);
723 TS_RESP_free(response
);
728 /* Reads a PKCS7 token and adds default 'granted' status info to it. */
729 static TS_RESP
*read_PKCS7(BIO
*in_bio
)
733 TS_TST_INFO
*tst_info
= NULL
;
734 TS_RESP
*resp
= NULL
;
735 TS_STATUS_INFO
*si
= NULL
;
737 /* Read PKCS7 object and extract the signed time stamp info. */
738 if (!(token
= d2i_PKCS7_bio(in_bio
, NULL
)))
740 if (!(tst_info
= PKCS7_to_TS_TST_INFO(token
)))
743 /* Creating response object. */
744 if (!(resp
= TS_RESP_new()))
747 /* Create granted status info. */
748 if (!(si
= TS_STATUS_INFO_new()))
750 if (!(ASN1_INTEGER_set(si
->status
, TS_STATUS_GRANTED
)))
752 if (!TS_RESP_set_status_info(resp
, si
))
755 /* Setting encapsulated token. */
756 TS_RESP_set_tst_info(resp
, token
, tst_info
);
757 token
= NULL
; /* Ownership is lost. */
758 tst_info
= NULL
; /* Ownership is lost. */
763 TS_TST_INFO_free(tst_info
);
768 TS_STATUS_INFO_free(si
);
772 static TS_RESP
*create_response(CONF
*conf
, const char *section
, char *engine
,
773 char *queryfile
, char *passin
, char *inkey
,
774 char *signer
, char *chain
, const char *policy
)
777 TS_RESP
*response
= NULL
;
778 BIO
*query_bio
= NULL
;
779 TS_RESP_CTX
*resp_ctx
= NULL
;
781 if (!(query_bio
= BIO_new_file(queryfile
, "rb")))
784 /* Getting TSA configuration section. */
785 if (!(section
= TS_CONF_get_tsa_section(conf
, section
)))
788 /* Setting up response generation context. */
789 if (!(resp_ctx
= TS_RESP_CTX_new()))
792 /* Setting serial number provider callback. */
793 if (!TS_CONF_set_serial(conf
, section
, serial_cb
, resp_ctx
))
795 #ifndef OPENSSL_NO_ENGINE
796 /* Setting default OpenSSL engine. */
797 if (!TS_CONF_set_crypto_device(conf
, section
, engine
))
801 /* Setting TSA signer certificate. */
802 if (!TS_CONF_set_signer_cert(conf
, section
, signer
, resp_ctx
))
805 /* Setting TSA signer certificate chain. */
806 if (!TS_CONF_set_certs(conf
, section
, chain
, resp_ctx
))
809 /* Setting TSA signer private key. */
810 if (!TS_CONF_set_signer_key(conf
, section
, inkey
, passin
, resp_ctx
))
813 /* Setting default policy OID. */
814 if (!TS_CONF_set_def_policy(conf
, section
, policy
, resp_ctx
))
817 /* Setting acceptable policy OIDs. */
818 if (!TS_CONF_set_policies(conf
, section
, resp_ctx
))
821 /* Setting the acceptable one-way hash algorithms. */
822 if (!TS_CONF_set_digests(conf
, section
, resp_ctx
))
825 /* Setting guaranteed time stamp accuracy. */
826 if (!TS_CONF_set_accuracy(conf
, section
, resp_ctx
))
829 /* Setting the precision of the time. */
830 if (!TS_CONF_set_clock_precision_digits(conf
, section
, resp_ctx
))
833 /* Setting the ordering flaf if requested. */
834 if (!TS_CONF_set_ordering(conf
, section
, resp_ctx
))
837 /* Setting the TSA name required flag if requested. */
838 if (!TS_CONF_set_tsa_name(conf
, section
, resp_ctx
))
841 /* Setting the ESS cert id chain flag if requested. */
842 if (!TS_CONF_set_ess_cert_id_chain(conf
, section
, resp_ctx
))
845 /* Creating the response. */
846 if (!(response
= TS_RESP_create_response(resp_ctx
, query_bio
)))
852 TS_RESP_free(response
);
855 TS_RESP_CTX_free(resp_ctx
);
856 BIO_free_all(query_bio
);
861 static ASN1_INTEGER
*MS_CALLBACK
serial_cb(TS_RESP_CTX
*ctx
, void *data
)
863 const char *serial_file
= (const char *)data
;
864 ASN1_INTEGER
*serial
= next_serial(serial_file
);
867 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
868 "Error during serial number "
870 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_ADD_INFO_NOT_AVAILABLE
);
872 save_ts_serial(serial_file
, serial
);
877 static ASN1_INTEGER
*next_serial(const char *serialfile
)
881 ASN1_INTEGER
*serial
= NULL
;
884 if (!(serial
= ASN1_INTEGER_new()))
887 if (!(in
= BIO_new_file(serialfile
, "r"))) {
889 BIO_printf(bio_err
, "Warning: could not open file %s for "
890 "reading, using serial number: 1\n", serialfile
);
891 if (!ASN1_INTEGER_set(serial
, 1))
895 if (!a2i_ASN1_INTEGER(in
, serial
, buf
, sizeof(buf
))) {
896 BIO_printf(bio_err
, "unable to load number from %s\n",
900 if (!(bn
= ASN1_INTEGER_to_BN(serial
, NULL
)))
902 ASN1_INTEGER_free(serial
);
904 if (!BN_add_word(bn
, 1))
906 if (!(serial
= BN_to_ASN1_INTEGER(bn
, NULL
)))
912 ASN1_INTEGER_free(serial
);
920 static int save_ts_serial(const char *serialfile
, ASN1_INTEGER
*serial
)
925 if (!(out
= BIO_new_file(serialfile
, "w")))
927 if (i2a_ASN1_INTEGER(out
, serial
) <= 0)
929 if (BIO_puts(out
, "\n") <= 0)
934 BIO_printf(bio_err
, "could not save serial number to %s\n",
941 * Verify-related method definitions.
944 static int verify_command(char *data
, char *digest
, char *queryfile
,
945 char *in
, int token_in
,
946 char *ca_path
, char *ca_file
, char *untrusted
)
950 TS_RESP
*response
= NULL
;
951 TS_VERIFY_CTX
*verify_ctx
= NULL
;
954 /* Decode the token (PKCS7) or response (TS_RESP) files. */
955 if (!(in_bio
= BIO_new_file(in
, "rb")))
958 if (!(token
= d2i_PKCS7_bio(in_bio
, NULL
)))
961 if (!(response
= d2i_TS_RESP_bio(in_bio
, NULL
)))
965 if (!(verify_ctx
= create_verify_ctx(data
, digest
, queryfile
,
966 ca_path
, ca_file
, untrusted
)))
969 /* Checking the token or response against the request. */
971 TS_RESP_verify_token(verify_ctx
, token
) :
972 TS_RESP_verify_response(verify_ctx
, response
);
975 printf("Verification: ");
980 /* Print errors, if there are any. */
981 ERR_print_errors(bio_err
);
985 BIO_free_all(in_bio
);
987 TS_RESP_free(response
);
988 TS_VERIFY_CTX_free(verify_ctx
);
992 static TS_VERIFY_CTX
*create_verify_ctx(char *data
, char *digest
,
994 char *ca_path
, char *ca_file
,
997 TS_VERIFY_CTX
*ctx
= NULL
;
999 TS_REQ
*request
= NULL
;
1002 if (data
!= NULL
|| digest
!= NULL
) {
1003 if (!(ctx
= TS_VERIFY_CTX_new()))
1005 ctx
->flags
= TS_VFY_VERSION
| TS_VFY_SIGNER
;
1007 ctx
->flags
|= TS_VFY_DATA
;
1008 if (!(ctx
->data
= BIO_new_file(data
, "rb")))
1010 } else if (digest
!= NULL
) {
1012 ctx
->flags
|= TS_VFY_IMPRINT
;
1013 if (!(ctx
->imprint
= string_to_hex(digest
, &imprint_len
))) {
1014 BIO_printf(bio_err
, "invalid digest string\n");
1017 ctx
->imprint_len
= imprint_len
;
1020 } else if (queryfile
!= NULL
) {
1022 * The request has just to be read, decoded and converted to a verify
1025 if (!(input
= BIO_new_file(queryfile
, "rb")))
1027 if (!(request
= d2i_TS_REQ_bio(input
, NULL
)))
1029 if (!(ctx
= TS_REQ_to_TS_VERIFY_CTX(request
, NULL
)))
1034 /* Add the signature verification flag and arguments. */
1035 ctx
->flags
|= TS_VFY_SIGNATURE
;
1037 /* Initialising the X509_STORE object. */
1038 if (!(ctx
->store
= create_cert_store(ca_path
, ca_file
)))
1041 /* Loading untrusted certificates. */
1042 if (untrusted
&& !(ctx
->certs
= TS_CONF_load_certs(untrusted
)))
1048 TS_VERIFY_CTX_free(ctx
);
1051 BIO_free_all(input
);
1052 TS_REQ_free(request
);
1056 static X509_STORE
*create_cert_store(char *ca_path
, char *ca_file
)
1058 X509_STORE
*cert_ctx
= NULL
;
1059 X509_LOOKUP
*lookup
= NULL
;
1062 /* Creating the X509_STORE object. */
1063 cert_ctx
= X509_STORE_new();
1065 /* Setting the callback for certificate chain verification. */
1066 X509_STORE_set_verify_cb(cert_ctx
, verify_cb
);
1068 /* Adding a trusted certificate directory source. */
1070 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
1071 if (lookup
== NULL
) {
1072 BIO_printf(bio_err
, "memory allocation failure\n");
1075 i
= X509_LOOKUP_add_dir(lookup
, ca_path
, X509_FILETYPE_PEM
);
1077 BIO_printf(bio_err
, "Error loading directory %s\n", ca_path
);
1082 /* Adding a trusted certificate file source. */
1084 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
1085 if (lookup
== NULL
) {
1086 BIO_printf(bio_err
, "memory allocation failure\n");
1089 i
= X509_LOOKUP_load_file(lookup
, ca_file
, X509_FILETYPE_PEM
);
1091 BIO_printf(bio_err
, "Error loading file %s\n", ca_file
);
1098 X509_STORE_free(cert_ctx
);
1102 static int MS_CALLBACK
verify_cb(int ok
, X509_STORE_CTX
*ctx
)
1109 X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
1111 printf("%s\n", buf);
1112 printf("error %d at %d depth lookup: %s\n",
1113 ctx->error, ctx->error_depth,
1114 X509_verify_cert_error_string(ctx->error));