Annotation of src/usr.bin/openssl/ts.c, Revision 1.9
1.9 ! doug 1: /* $OpenBSD: ts.c,v 1.8 2015/09/13 23:36:21 doug Exp $ */
1.1 jsing 2: /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3: * project 2002.
4: */
5: /* ====================================================================
6: * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: *
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
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
18: * distribution.
19: *
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/)"
24: *
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.
29: *
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.
33: *
34: * 6. Redistributions of any form whatsoever must retain the following
35: * acknowledgment:
36: * "This product includes software developed by the OpenSSL Project
37: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38: *
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: * ====================================================================
52: *
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).
56: *
57: */
58:
59: #include <stdio.h>
60: #include <stdlib.h>
61: #include <string.h>
62:
63: #include "apps.h"
64:
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>
70:
71: /* Length of the nonce of the request in bits (must be a multiple of 8). */
72: #define NONCE_LENGTH 64
73:
74: /* Macro definitions for the configuration file. */
75: #define ENV_OID_FILE "oid_file"
76:
77: /* Local function declarations. */
78:
79: static ASN1_OBJECT *txt2obj(const char *oid);
80: static CONF *load_config_file(const char *configfile);
81:
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,
87: FILE * default_fp);
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);
93:
94: /* Reply related functions. */
1.6 bcook 95: static int reply_command(CONF * conf, char *section,
1.1 jsing 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,
99: int text);
100: static TS_RESP *read_PKCS7(BIO * in_bio);
1.6 bcook 101: static TS_RESP *create_response(CONF * conf, const char *section,
1.1 jsing 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);
107:
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,
113: char *queryfile,
114: char *ca_path, char *ca_file,
115: char *untrusted);
116: static X509_STORE *create_cert_store(char *ca_path, char *ca_file);
117: static int verify_cb(int ok, X509_STORE_CTX * ctx);
118:
119: int
120: ts_main(int argc, char **argv)
121: {
122: int ret = 1;
123: char *configfile = NULL;
124: char *section = NULL;
125: CONF *conf = NULL;
126: enum mode {
127: CMD_NONE, CMD_QUERY, CMD_REPLY, CMD_VERIFY
128: } mode = CMD_NONE;
129: char *data = NULL;
130: char *digest = NULL;
131: const EVP_MD *md = NULL;
132: char *policy = NULL;
133: int no_nonce = 0;
134: int cert = 0;
135: char *in = NULL;
136: char *out = NULL;
137: int text = 0;
138: char *queryfile = NULL;
139: char *passin = NULL; /* Password source. */
140: char *password = NULL; /* Password itself. */
141: char *inkey = NULL;
142: char *signer = NULL;
143: char *chain = NULL;
144: char *ca_path = NULL;
145: char *ca_file = NULL;
146: char *untrusted = NULL;
147: /* Input is ContentInfo instead of TimeStampResp. */
148: int token_in = 0;
149: /* Output is ContentInfo instead of TimeStampResp. */
150: int token_out = 0;
151:
152: for (argc--, argv++; argc > 0; argc--, argv++) {
153: if (strcmp(*argv, "-config") == 0) {
154: if (argc-- < 1)
155: goto usage;
156: configfile = *++argv;
157: } else if (strcmp(*argv, "-section") == 0) {
158: if (argc-- < 1)
159: goto usage;
160: section = *++argv;
161: } else if (strcmp(*argv, "-query") == 0) {
162: if (mode != CMD_NONE)
163: goto usage;
164: mode = CMD_QUERY;
165: } else if (strcmp(*argv, "-data") == 0) {
166: if (argc-- < 1)
167: goto usage;
168: data = *++argv;
169: } else if (strcmp(*argv, "-digest") == 0) {
170: if (argc-- < 1)
171: goto usage;
172: digest = *++argv;
173: } else if (strcmp(*argv, "-policy") == 0) {
174: if (argc-- < 1)
175: goto usage;
176: policy = *++argv;
177: } else if (strcmp(*argv, "-no_nonce") == 0) {
178: no_nonce = 1;
179: } else if (strcmp(*argv, "-cert") == 0) {
180: cert = 1;
181: } else if (strcmp(*argv, "-in") == 0) {
182: if (argc-- < 1)
183: goto usage;
184: in = *++argv;
185: } else if (strcmp(*argv, "-token_in") == 0) {
186: token_in = 1;
187: } else if (strcmp(*argv, "-out") == 0) {
188: if (argc-- < 1)
189: goto usage;
190: out = *++argv;
191: } else if (strcmp(*argv, "-token_out") == 0) {
192: token_out = 1;
193: } else if (strcmp(*argv, "-text") == 0) {
194: text = 1;
195: } else if (strcmp(*argv, "-reply") == 0) {
196: if (mode != CMD_NONE)
197: goto usage;
198: mode = CMD_REPLY;
199: } else if (strcmp(*argv, "-queryfile") == 0) {
200: if (argc-- < 1)
201: goto usage;
202: queryfile = *++argv;
203: } else if (strcmp(*argv, "-passin") == 0) {
204: if (argc-- < 1)
205: goto usage;
206: passin = *++argv;
207: } else if (strcmp(*argv, "-inkey") == 0) {
208: if (argc-- < 1)
209: goto usage;
210: inkey = *++argv;
211: } else if (strcmp(*argv, "-signer") == 0) {
212: if (argc-- < 1)
213: goto usage;
214: signer = *++argv;
215: } else if (strcmp(*argv, "-chain") == 0) {
216: if (argc-- < 1)
217: goto usage;
218: chain = *++argv;
219: } else if (strcmp(*argv, "-verify") == 0) {
220: if (mode != CMD_NONE)
221: goto usage;
222: mode = CMD_VERIFY;
223: } else if (strcmp(*argv, "-CApath") == 0) {
224: if (argc-- < 1)
225: goto usage;
226: ca_path = *++argv;
227: } else if (strcmp(*argv, "-CAfile") == 0) {
228: if (argc-- < 1)
229: goto usage;
230: ca_file = *++argv;
231: } else if (strcmp(*argv, "-untrusted") == 0) {
232: if (argc-- < 1)
233: goto usage;
234: untrusted = *++argv;
235: } else if ((md = EVP_get_digestbyname(*argv + 1)) != NULL) {
236: /* empty. */
237: } else
238: goto usage;
239: }
240:
241: /* Get the password if required. */
242: if (mode == CMD_REPLY && passin &&
243: !app_passwd(bio_err, passin, NULL, &password, NULL)) {
244: BIO_printf(bio_err, "Error getting password.\n");
245: goto cleanup;
246: }
247: /*
248: * Check consistency of parameters and execute the appropriate
249: * function.
250: */
251: switch (mode) {
252: case CMD_NONE:
253: goto usage;
254: case CMD_QUERY:
255: /*
256: * Data file and message imprint cannot be specified at the
257: * same time.
258: */
259: ret = data != NULL && digest != NULL;
260: if (ret)
261: goto usage;
262: /* Load the config file for possible policy OIDs. */
263: conf = load_config_file(configfile);
264: ret = !query_command(data, digest, md, policy, no_nonce, cert,
265: in, out, text);
266: break;
267: case CMD_REPLY:
268: conf = load_config_file(configfile);
269: if (in == NULL) {
270: ret = !(queryfile != NULL && conf != NULL && !token_in);
271: if (ret)
272: goto usage;
273: } else {
274: /* 'in' and 'queryfile' are exclusive. */
275: ret = !(queryfile == NULL);
276: if (ret)
277: goto usage;
278: }
279:
1.6 bcook 280: ret = !reply_command(conf, section, queryfile,
1.1 jsing 281: password, inkey, signer, chain, policy,
282: in, token_in, out, token_out, text);
283: break;
284: case CMD_VERIFY:
285: ret = !(((queryfile && !data && !digest) ||
286: (!queryfile && data && !digest) ||
287: (!queryfile && !data && digest)) && in != NULL);
288: if (ret)
289: goto usage;
290:
291: ret = !verify_command(data, digest, queryfile, in, token_in,
292: ca_path, ca_file, untrusted);
293: }
294:
295: goto cleanup;
296:
297: usage:
298: BIO_printf(bio_err, "usage:\n"
299: "ts -query [-config configfile] "
300: "[-data file_to_hash] [-digest digest_bytes]"
1.9 ! doug 301: "[-md2|-md4|-md5|-sha|-sha1|-ripemd160] "
1.1 jsing 302: "[-policy object_id] [-no_nonce] [-cert] "
303: "[-in request.tsq] [-out request.tsq] [-text]\n");
304: BIO_printf(bio_err, "or\n"
305: "ts -reply [-config configfile] [-section tsa_section] "
306: "[-queryfile request.tsq] [-passin password] "
307: "[-signer tsa_cert.pem] [-inkey private_key.pem] "
308: "[-chain certs_file.pem] [-policy object_id] "
309: "[-in response.tsr] [-token_in] "
1.6 bcook 310: "[-out response.tsr] [-token_out] [-text]\n");
1.1 jsing 311: BIO_printf(bio_err, "or\n"
312: "ts -verify [-data file_to_hash] [-digest digest_bytes] "
313: "[-queryfile request.tsq] "
314: "-in response.tsr [-token_in] "
315: "-CApath ca_path -CAfile ca_file.pem "
316: "-untrusted cert_file.pem\n");
317:
318: cleanup:
319: /* Clean up. */
320: NCONF_free(conf);
321: free(password);
322: OBJ_cleanup();
323:
324: return (ret);
325: }
326:
327: /*
328: * Configuration file-related function definitions.
329: */
330:
331: static ASN1_OBJECT *
332: txt2obj(const char *oid)
333: {
334: ASN1_OBJECT *oid_obj = NULL;
335:
336: if (!(oid_obj = OBJ_txt2obj(oid, 0)))
337: BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
338:
339: return oid_obj;
340: }
341:
342: static CONF *
343: load_config_file(const char *configfile)
344: {
345: CONF *conf = NULL;
346: long errorline = -1;
347:
348: if (!configfile)
349: configfile = getenv("OPENSSL_CONF");
350:
351: if (configfile &&
352: (!(conf = NCONF_new(NULL)) ||
353: NCONF_load(conf, configfile, &errorline) <= 0)) {
354: if (errorline <= 0)
355: BIO_printf(bio_err, "error loading the config file "
356: "'%s'\n", configfile);
357: else
358: BIO_printf(bio_err, "error on line %ld of config file "
359: "'%s'\n", errorline, configfile);
360: }
361: if (conf != NULL) {
362: const char *p;
363:
364: BIO_printf(bio_err, "Using configuration from %s\n",
365: configfile);
366: p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
367: if (p != NULL) {
368: BIO *oid_bio = BIO_new_file(p, "r");
369: if (!oid_bio)
370: ERR_print_errors(bio_err);
371: else {
372: OBJ_create_objects(oid_bio);
373: BIO_free_all(oid_bio);
374: }
375: } else
376: ERR_clear_error();
377: if (!add_oid_section(bio_err, conf))
378: ERR_print_errors(bio_err);
379: }
380: return conf;
381: }
382:
383: /*
384: * Query-related method definitions.
385: */
386:
387: static int
388: query_command(const char *data, char *digest, const EVP_MD * md,
389: const char *policy, int no_nonce, int cert, const char *in,
390: const char *out, int text)
391: {
392: int ret = 0;
393: TS_REQ *query = NULL;
394: BIO *in_bio = NULL;
395: BIO *data_bio = NULL;
396: BIO *out_bio = NULL;
397:
398: /* Build query object either from file or from scratch. */
399: if (in != NULL) {
400: if ((in_bio = BIO_new_file(in, "rb")) == NULL)
401: goto end;
402: query = d2i_TS_REQ_bio(in_bio, NULL);
403: } else {
404: /* Open the file if no explicit digest bytes were specified. */
405: if (!digest &&
406: !(data_bio = BIO_open_with_default(data, "rb", stdin)))
407: goto end;
408: /* Creating the query object. */
409: query = create_query(data_bio, digest, md,
410: policy, no_nonce, cert);
411: /* Saving the random number generator state. */
412: }
413: if (query == NULL)
414: goto end;
415:
416: /* Write query either in ASN.1 or in text format. */
417: if ((out_bio = BIO_open_with_default(out, "wb", stdout)) == NULL)
418: goto end;
419: if (text) {
420: /* Text output. */
421: if (!TS_REQ_print_bio(out_bio, query))
422: goto end;
423: } else {
424: /* ASN.1 output. */
425: if (!i2d_TS_REQ_bio(out_bio, query))
426: goto end;
427: }
428:
429: ret = 1;
430:
431: end:
432: ERR_print_errors(bio_err);
433:
434: /* Clean up. */
435: BIO_free_all(in_bio);
436: BIO_free_all(data_bio);
437: BIO_free_all(out_bio);
438: TS_REQ_free(query);
439:
440: return ret;
441: }
442:
443: static BIO *
444: BIO_open_with_default(const char *file, const char *mode, FILE * default_fp)
445: {
446: return file == NULL ? BIO_new_fp(default_fp, BIO_NOCLOSE) :
447: BIO_new_file(file, mode);
448: }
449:
450: static TS_REQ *
451: create_query(BIO * data_bio, char *digest, const EVP_MD * md,
452: const char *policy, int no_nonce, int cert)
453: {
454: int ret = 0;
455: TS_REQ *ts_req = NULL;
456: int len;
457: TS_MSG_IMPRINT *msg_imprint = NULL;
458: X509_ALGOR *algo = NULL;
459: unsigned char *data = NULL;
460: ASN1_OBJECT *policy_obj = NULL;
461: ASN1_INTEGER *nonce_asn1 = NULL;
462:
463: /* Setting default message digest. */
464: if (!md && !(md = EVP_get_digestbyname("sha1")))
465: goto err;
466:
467: /* Creating request object. */
468: if (!(ts_req = TS_REQ_new()))
469: goto err;
470:
471: /* Setting version. */
472: if (!TS_REQ_set_version(ts_req, 1))
473: goto err;
474:
475: /* Creating and adding MSG_IMPRINT object. */
476: if (!(msg_imprint = TS_MSG_IMPRINT_new()))
477: goto err;
478:
479: /* Adding algorithm. */
480: if (!(algo = X509_ALGOR_new()))
481: goto err;
482: if (!(algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))))
483: goto err;
484: if (!(algo->parameter = ASN1_TYPE_new()))
485: goto err;
486: algo->parameter->type = V_ASN1_NULL;
487: if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
488: goto err;
489:
490: /* Adding message digest. */
491: if ((len = create_digest(data_bio, digest, md, &data)) == 0)
492: goto err;
493: if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
494: goto err;
495:
496: if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
497: goto err;
498:
499: /* Setting policy if requested. */
500: if (policy && !(policy_obj = txt2obj(policy)))
501: goto err;
502: if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
503: goto err;
504:
505: /* Setting nonce if requested. */
506: if (!no_nonce && !(nonce_asn1 = create_nonce(NONCE_LENGTH)))
507: goto err;
508: if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
509: goto err;
510:
511: /* Setting certificate request flag if requested. */
512: if (!TS_REQ_set_cert_req(ts_req, cert))
513: goto err;
514:
515: ret = 1;
516:
517: err:
518: if (!ret) {
519: TS_REQ_free(ts_req);
520: ts_req = NULL;
521: BIO_printf(bio_err, "could not create query\n");
522: }
523: TS_MSG_IMPRINT_free(msg_imprint);
524: X509_ALGOR_free(algo);
525: free(data);
526: ASN1_OBJECT_free(policy_obj);
527: ASN1_INTEGER_free(nonce_asn1);
528:
529: return ts_req;
530: }
531:
532: static int
533: create_digest(BIO * input, char *digest, const EVP_MD * md,
534: unsigned char **md_value)
535: {
536: int md_value_len;
537:
538: md_value_len = EVP_MD_size(md);
539: if (md_value_len < 0)
540: goto err;
541: if (input) {
542: /* Digest must be computed from an input file. */
543: EVP_MD_CTX md_ctx;
544: unsigned char buffer[4096];
545: int length;
546:
547: *md_value = malloc(md_value_len);
548: if (*md_value == 0)
549: goto err;
550:
551: EVP_DigestInit(&md_ctx, md);
552: while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
553: EVP_DigestUpdate(&md_ctx, buffer, length);
554: }
555: EVP_DigestFinal(&md_ctx, *md_value, NULL);
556: } else {
557: /* Digest bytes are specified with digest. */
558: long digest_len;
559: *md_value = string_to_hex(digest, &digest_len);
560: if (!*md_value || md_value_len != digest_len) {
561: free(*md_value);
562: *md_value = NULL;
563: BIO_printf(bio_err, "bad digest, %d bytes "
564: "must be specified\n", md_value_len);
565: goto err;
566: }
567: }
568:
569: return md_value_len;
570: err:
571: return 0;
572: }
573:
574: static ASN1_INTEGER *
575: create_nonce(int bits)
576: {
577: unsigned char buf[20];
578: ASN1_INTEGER *nonce = NULL;
579: int len = (bits - 1) / 8 + 1;
580: int i;
581:
582: /* Generating random byte sequence. */
583: if (len > (int) sizeof(buf))
584: goto err;
1.3 jsing 585: arc4random_buf(buf, len);
1.1 jsing 586:
587: /* Find the first non-zero byte and creating ASN1_INTEGER object. */
588: for (i = 0; i < len && !buf[i]; ++i)
589: ;
590: if (!(nonce = ASN1_INTEGER_new()))
591: goto err;
592: free(nonce->data);
593: /* Allocate at least one byte. */
594: nonce->length = len - i;
595: if (!(nonce->data = malloc(nonce->length + 1)))
596: goto err;
597: memcpy(nonce->data, buf + i, nonce->length);
598:
599: return nonce;
600:
601: err:
602: BIO_printf(bio_err, "could not create nonce\n");
603: ASN1_INTEGER_free(nonce);
604: return NULL;
605: }
606: /*
607: * Reply-related method definitions.
608: */
609:
610: static int
1.6 bcook 611: reply_command(CONF * conf, char *section, char *queryfile,
1.1 jsing 612: char *passin, char *inkey, char *signer, char *chain, const char *policy,
613: char *in, int token_in, char *out, int token_out, int text)
614: {
615: int ret = 0;
616: TS_RESP *response = NULL;
617: BIO *in_bio = NULL;
618: BIO *query_bio = NULL;
619: BIO *inkey_bio = NULL;
620: BIO *signer_bio = NULL;
621: BIO *out_bio = NULL;
622:
623: /* Build response object either from response or query. */
624: if (in != NULL) {
625: if ((in_bio = BIO_new_file(in, "rb")) == NULL)
626: goto end;
627: if (token_in) {
628: /*
629: * We have a ContentInfo (PKCS7) object, add
630: * 'granted' status info around it.
631: */
632: response = read_PKCS7(in_bio);
633: } else {
634: /* We have a ready-made TS_RESP object. */
635: response = d2i_TS_RESP_bio(in_bio, NULL);
636: }
637: } else {
1.6 bcook 638: response = create_response(conf, section, queryfile,
1.1 jsing 639: passin, inkey, signer, chain,
640: policy);
641: if (response)
642: BIO_printf(bio_err, "Response has been generated.\n");
643: else
644: BIO_printf(bio_err, "Response is not generated.\n");
645: }
646: if (response == NULL)
647: goto end;
648:
649: /* Write response either in ASN.1 or text format. */
650: if ((out_bio = BIO_open_with_default(out, "wb", stdout)) == NULL)
651: goto end;
652: if (text) {
653: /* Text output. */
654: if (token_out) {
655: TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
656: if (!TS_TST_INFO_print_bio(out_bio, tst_info))
657: goto end;
658: } else {
659: if (!TS_RESP_print_bio(out_bio, response))
660: goto end;
661: }
662: } else {
663: /* ASN.1 DER output. */
664: if (token_out) {
665: PKCS7 *token = TS_RESP_get_token(response);
666: if (!i2d_PKCS7_bio(out_bio, token))
667: goto end;
668: } else {
669: if (!i2d_TS_RESP_bio(out_bio, response))
670: goto end;
671: }
672: }
673:
674: ret = 1;
675:
676: end:
677: ERR_print_errors(bio_err);
678:
679: /* Clean up. */
680: BIO_free_all(in_bio);
681: BIO_free_all(query_bio);
682: BIO_free_all(inkey_bio);
683: BIO_free_all(signer_bio);
684: BIO_free_all(out_bio);
685: TS_RESP_free(response);
686:
687: return ret;
688: }
689:
690: /* Reads a PKCS7 token and adds default 'granted' status info to it. */
691: static TS_RESP *
692: read_PKCS7(BIO * in_bio)
693: {
694: int ret = 0;
695: PKCS7 *token = NULL;
696: TS_TST_INFO *tst_info = NULL;
697: TS_RESP *resp = NULL;
698: TS_STATUS_INFO *si = NULL;
699:
700: /* Read PKCS7 object and extract the signed time stamp info. */
701: if (!(token = d2i_PKCS7_bio(in_bio, NULL)))
702: goto end;
703: if (!(tst_info = PKCS7_to_TS_TST_INFO(token)))
704: goto end;
705:
706: /* Creating response object. */
707: if (!(resp = TS_RESP_new()))
708: goto end;
709:
710: /* Create granted status info. */
711: if (!(si = TS_STATUS_INFO_new()))
712: goto end;
713: if (!(ASN1_INTEGER_set(si->status, TS_STATUS_GRANTED)))
714: goto end;
715: if (!TS_RESP_set_status_info(resp, si))
716: goto end;
717:
718: /* Setting encapsulated token. */
719: TS_RESP_set_tst_info(resp, token, tst_info);
720: token = NULL; /* Ownership is lost. */
721: tst_info = NULL; /* Ownership is lost. */
722:
723: ret = 1;
724: end:
725: PKCS7_free(token);
726: TS_TST_INFO_free(tst_info);
727: if (!ret) {
728: TS_RESP_free(resp);
729: resp = NULL;
730: }
731: TS_STATUS_INFO_free(si);
732: return resp;
733: }
734:
735: static TS_RESP *
1.6 bcook 736: create_response(CONF * conf, const char *section,
1.1 jsing 737: char *queryfile, char *passin, char *inkey,
738: char *signer, char *chain, const char *policy)
739: {
740: int ret = 0;
741: TS_RESP *response = NULL;
742: BIO *query_bio = NULL;
743: TS_RESP_CTX *resp_ctx = NULL;
744:
745: if (!(query_bio = BIO_new_file(queryfile, "rb")))
746: goto end;
747:
748: /* Getting TSA configuration section. */
749: if (!(section = TS_CONF_get_tsa_section(conf, section)))
750: goto end;
751:
752: /* Setting up response generation context. */
753: if (!(resp_ctx = TS_RESP_CTX_new()))
754: goto end;
755:
756: /* Setting serial number provider callback. */
757: if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
758: goto end;
759:
760: /* Setting TSA signer certificate. */
761: if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
762: goto end;
763:
764: /* Setting TSA signer certificate chain. */
765: if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
766: goto end;
767:
768: /* Setting TSA signer private key. */
769: if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
770: goto end;
771:
772: /* Setting default policy OID. */
773: if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
774: goto end;
775:
776: /* Setting acceptable policy OIDs. */
777: if (!TS_CONF_set_policies(conf, section, resp_ctx))
778: goto end;
779:
780: /* Setting the acceptable one-way hash algorithms. */
781: if (!TS_CONF_set_digests(conf, section, resp_ctx))
782: goto end;
783:
784: /* Setting guaranteed time stamp accuracy. */
785: if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
786: goto end;
787:
788: /* Setting the precision of the time. */
789: if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
790: goto end;
791:
792: /* Setting the ordering flaf if requested. */
793: if (!TS_CONF_set_ordering(conf, section, resp_ctx))
794: goto end;
795:
796: /* Setting the TSA name required flag if requested. */
797: if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
798: goto end;
799:
800: /* Setting the ESS cert id chain flag if requested. */
801: if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
802: goto end;
803:
804: /* Creating the response. */
805: if (!(response = TS_RESP_create_response(resp_ctx, query_bio)))
806: goto end;
807:
808: ret = 1;
809: end:
810: if (!ret) {
811: TS_RESP_free(response);
812: response = NULL;
813: }
814: TS_RESP_CTX_free(resp_ctx);
815: BIO_free_all(query_bio);
816:
817: return response;
818: }
819:
820: static ASN1_INTEGER *
821: serial_cb(TS_RESP_CTX * ctx, void *data)
822: {
823: const char *serial_file = (const char *) data;
824: ASN1_INTEGER *serial = next_serial(serial_file);
825:
826: if (!serial) {
827: TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
828: "Error during serial number "
829: "generation.");
830: TS_RESP_CTX_add_failure_info(ctx,
831: TS_INFO_ADD_INFO_NOT_AVAILABLE);
832: } else
833: save_ts_serial(serial_file, serial);
834:
835: return serial;
836: }
837:
838: static ASN1_INTEGER *
839: next_serial(const char *serialfile)
840: {
841: int ret = 0;
842: BIO *in = NULL;
843: ASN1_INTEGER *serial = NULL;
844: BIGNUM *bn = NULL;
845:
846: if (!(serial = ASN1_INTEGER_new()))
847: goto err;
848:
849: if (!(in = BIO_new_file(serialfile, "r"))) {
850: ERR_clear_error();
851: BIO_printf(bio_err, "Warning: could not open file %s for "
852: "reading, using serial number: 1\n", serialfile);
853: if (!ASN1_INTEGER_set(serial, 1))
854: goto err;
855: } else {
856: char buf[1024];
857: if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
858: BIO_printf(bio_err, "unable to load number from %s\n",
859: serialfile);
860: goto err;
861: }
862: if (!(bn = ASN1_INTEGER_to_BN(serial, NULL)))
863: goto err;
864: ASN1_INTEGER_free(serial);
865: serial = NULL;
866: if (!BN_add_word(bn, 1))
867: goto err;
868: if (!(serial = BN_to_ASN1_INTEGER(bn, NULL)))
869: goto err;
870: }
871: ret = 1;
872: err:
873: if (!ret) {
874: ASN1_INTEGER_free(serial);
875: serial = NULL;
876: }
877: BIO_free_all(in);
878: BN_free(bn);
879: return serial;
880: }
881:
882: static int
883: save_ts_serial(const char *serialfile, ASN1_INTEGER * serial)
884: {
885: int ret = 0;
886: BIO *out = NULL;
887:
888: if (!(out = BIO_new_file(serialfile, "w")))
889: goto err;
890: if (i2a_ASN1_INTEGER(out, serial) <= 0)
891: goto err;
892: if (BIO_puts(out, "\n") <= 0)
893: goto err;
894: ret = 1;
895: err:
896: if (!ret)
897: BIO_printf(bio_err, "could not save serial number to %s\n",
898: serialfile);
899: BIO_free_all(out);
900: return ret;
901: }
902:
903: /*
904: * Verify-related method definitions.
905: */
906:
907: static int
908: verify_command(char *data, char *digest, char *queryfile, char *in,
909: int token_in, char *ca_path, char *ca_file, char *untrusted)
910: {
911: BIO *in_bio = NULL;
912: PKCS7 *token = NULL;
913: TS_RESP *response = NULL;
914: TS_VERIFY_CTX *verify_ctx = NULL;
915: int ret = 0;
916:
917: /* Decode the token (PKCS7) or response (TS_RESP) files. */
918: if (!(in_bio = BIO_new_file(in, "rb")))
919: goto end;
920: if (token_in) {
921: if (!(token = d2i_PKCS7_bio(in_bio, NULL)))
922: goto end;
923: } else {
924: if (!(response = d2i_TS_RESP_bio(in_bio, NULL)))
925: goto end;
926: }
927:
928: if (!(verify_ctx = create_verify_ctx(data, digest, queryfile,
929: ca_path, ca_file, untrusted)))
930: goto end;
931:
932: /* Checking the token or response against the request. */
933: ret = token_in ?
934: TS_RESP_verify_token(verify_ctx, token) :
935: TS_RESP_verify_response(verify_ctx, response);
936:
937: end:
938: printf("Verification: ");
939: if (ret)
940: printf("OK\n");
941: else {
942: printf("FAILED\n");
943: /* Print errors, if there are any. */
944: ERR_print_errors(bio_err);
945: }
946:
947: /* Clean up. */
948: BIO_free_all(in_bio);
949: PKCS7_free(token);
950: TS_RESP_free(response);
951: TS_VERIFY_CTX_free(verify_ctx);
952: return ret;
953: }
954:
955: static TS_VERIFY_CTX *
956: create_verify_ctx(char *data, char *digest, char *queryfile, char *ca_path,
957: char *ca_file, char *untrusted)
958: {
959: TS_VERIFY_CTX *ctx = NULL;
960: BIO *input = NULL;
961: TS_REQ *request = NULL;
962: int ret = 0;
963:
964: if (data != NULL || digest != NULL) {
965: if (!(ctx = TS_VERIFY_CTX_new()))
966: goto err;
967: ctx->flags = TS_VFY_VERSION | TS_VFY_SIGNER;
968: if (data != NULL) {
969: ctx->flags |= TS_VFY_DATA;
970: if (!(ctx->data = BIO_new_file(data, "rb")))
971: goto err;
972: } else if (digest != NULL) {
973: long imprint_len;
974: ctx->flags |= TS_VFY_IMPRINT;
975: if (!(ctx->imprint = string_to_hex(digest,
976: &imprint_len))) {
977: BIO_printf(bio_err, "invalid digest string\n");
978: goto err;
979: }
980: ctx->imprint_len = imprint_len;
981: }
982: } else if (queryfile != NULL) {
983: /*
984: * The request has just to be read, decoded and converted to
985: * a verify context object.
986: */
987: if (!(input = BIO_new_file(queryfile, "rb")))
988: goto err;
989: if (!(request = d2i_TS_REQ_bio(input, NULL)))
990: goto err;
991: if (!(ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)))
992: goto err;
993: } else
994: return NULL;
995:
996: /* Add the signature verification flag and arguments. */
997: ctx->flags |= TS_VFY_SIGNATURE;
998:
999: /* Initialising the X509_STORE object. */
1000: if (!(ctx->store = create_cert_store(ca_path, ca_file)))
1001: goto err;
1002:
1003: /* Loading untrusted certificates. */
1004: if (untrusted && !(ctx->certs = TS_CONF_load_certs(untrusted)))
1005: goto err;
1006:
1007: ret = 1;
1008: err:
1009: if (!ret) {
1010: TS_VERIFY_CTX_free(ctx);
1011: ctx = NULL;
1012: }
1013: BIO_free_all(input);
1014: TS_REQ_free(request);
1015: return ctx;
1016: }
1017:
1018: static X509_STORE *
1019: create_cert_store(char *ca_path, char *ca_file)
1020: {
1021: X509_STORE *cert_ctx = NULL;
1022: X509_LOOKUP *lookup = NULL;
1023: int i;
1024:
1025: /* Creating the X509_STORE object. */
1026: cert_ctx = X509_STORE_new();
1027:
1028: /* Setting the callback for certificate chain verification. */
1029: X509_STORE_set_verify_cb(cert_ctx, verify_cb);
1030:
1031: /* Adding a trusted certificate directory source. */
1032: if (ca_path) {
1033: lookup = X509_STORE_add_lookup(cert_ctx,
1034: X509_LOOKUP_hash_dir());
1035: if (lookup == NULL) {
1036: BIO_printf(bio_err, "memory allocation failure\n");
1037: goto err;
1038: }
1039: i = X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM);
1040: if (!i) {
1041: BIO_printf(bio_err, "Error loading directory %s\n",
1042: ca_path);
1043: goto err;
1044: }
1045: }
1046: /* Adding a trusted certificate file source. */
1047: if (ca_file) {
1048: lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
1049: if (lookup == NULL) {
1050: BIO_printf(bio_err, "memory allocation failure\n");
1051: goto err;
1052: }
1053: i = X509_LOOKUP_load_file(lookup, ca_file, X509_FILETYPE_PEM);
1054: if (!i) {
1055: BIO_printf(bio_err, "Error loading file %s\n", ca_file);
1056: goto err;
1057: }
1058: }
1059: return cert_ctx;
1060: err:
1061: X509_STORE_free(cert_ctx);
1062: return NULL;
1063: }
1064:
1065: static int
1066: verify_cb(int ok, X509_STORE_CTX * ctx)
1067: {
1068: /*
1069: char buf[256];
1070:
1071: if (!ok)
1072: {
1073: X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
1074: buf, sizeof(buf));
1075: printf("%s\n", buf);
1076: printf("error %d at %d depth lookup: %s\n",
1077: ctx->error, ctx->error_depth,
1078: X509_verify_cert_error_string(ctx->error));
1079: }
1080: */
1081:
1082: return ok;
1083: }