2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/pem.h>
62 #include <openssl/ocsp.h>
63 #include <openssl/err.h>
64 #include <openssl/ssl.h>
67 /* Maximum leeway in validity period: default 5 minutes */
68 #define MAX_VALIDITY_PERIOD (5 * 60)
70 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer,
71 STACK_OF(OCSP_CERTID) *ids);
72 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
73 STACK_OF(OCSP_CERTID) *ids);
74 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
75 STACK *names, STACK_OF(OCSP_CERTID) *ids,
76 long nsec, long maxage);
79 #define PROG ocsp_main
81 int MAIN(int, char **);
83 int MAIN(int argc, char **argv)
87 char *host = NULL, *port = NULL, *path = "/";
88 char *reqin = NULL, *respin = NULL;
89 char *reqout = NULL, *respout = NULL;
90 char *signfile = NULL, *keyfile = NULL;
92 int add_nonce = 1, noverify = 0, use_ssl = -1;
93 OCSP_REQUEST *req = NULL;
94 OCSP_RESPONSE *resp = NULL;
95 OCSP_BASICRESP *bs = NULL;
96 X509 *issuer = NULL, *cert = NULL;
99 BIO *cbio = NULL, *derbio = NULL;
101 int req_text = 0, resp_text = 0;
102 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
103 char *CAfile = NULL, *CApath = NULL;
104 X509_STORE *store = NULL;
106 STACK_OF(X509) *sign_other = NULL, *verify_other = NULL;
107 char *sign_certfile = NULL, *verify_certfile = NULL;
108 unsigned long sign_flags = 0, verify_flags = 0;
112 STACK *reqnames = NULL;
113 STACK_OF(OCSP_CERTID) *ids = NULL;
114 if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
115 SSL_load_error_strings();
117 reqnames = sk_new_null();
118 ids = sk_OCSP_CERTID_new_null();
119 while (!badarg && *args && *args[0] == '-')
121 if (!strcmp(*args, "-out"))
130 else if (!strcmp(*args, "-url"))
135 if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl))
137 BIO_printf(bio_err, "Error parsing URL\n");
143 else if (!strcmp(*args, "-host"))
152 else if (!strcmp(*args, "-noverify"))
154 else if (!strcmp(*args, "-nonce"))
156 else if (!strcmp(*args, "-no_nonce"))
158 else if (!strcmp(*args, "-no_certs"))
159 sign_flags |= OCSP_NOCERTS;
160 else if (!strcmp(*args, "-no_signature_verify"))
161 verify_flags |= OCSP_NOSIGS;
162 else if (!strcmp(*args, "-no_cert_verify"))
163 verify_flags |= OCSP_NOVERIFY;
164 else if (!strcmp(*args, "-no_chain"))
165 verify_flags |= OCSP_NOCHAIN;
166 else if (!strcmp(*args, "-no_cert_checks"))
167 verify_flags |= OCSP_NOCHECKS;
168 else if (!strcmp(*args, "-no_explicit"))
169 verify_flags |= OCSP_NOEXPLICIT;
170 else if (!strcmp(*args, "-trust_other"))
171 verify_flags |= OCSP_TRUSTOTHER;
172 else if (!strcmp(*args, "-no_intern"))
173 verify_flags |= OCSP_NOINTERN;
174 else if (!strcmp(*args, "-text"))
179 else if (!strcmp(*args, "-req_text"))
181 else if (!strcmp(*args, "-resp_text"))
183 else if (!strcmp(*args, "-reqin"))
192 else if (!strcmp(*args, "-respin"))
201 else if (!strcmp(*args, "-signer"))
210 else if (!strcmp (*args, "-VAfile"))
215 verify_certfile = *args;
216 verify_flags |= OCSP_TRUSTOTHER;
220 else if (!strcmp(*args, "-sign_other"))
225 sign_certfile = *args;
229 else if (!strcmp(*args, "-verify_other"))
234 verify_certfile = *args;
238 else if (!strcmp (*args, "-CAfile"))
247 else if (!strcmp (*args, "-CApath"))
256 else if (!strcmp (*args, "-validity_period"))
265 "Illegal validity period %s\n",
272 else if (!strcmp (*args, "-status_age"))
277 maxage = atol(*args);
281 "Illegal validity age %s\n",
288 else if (!strcmp(*args, "-signkey"))
297 else if (!strcmp(*args, "-reqout"))
306 else if (!strcmp(*args, "-respout"))
315 else if (!strcmp(*args, "-path"))
324 else if (!strcmp(*args, "-issuer"))
330 issuer = load_cert(bio_err, *args, FORMAT_PEM,
331 NULL, e, "issuer certificate");
332 if(!issuer) goto end;
336 else if (!strcmp (*args, "-cert"))
342 cert = load_cert(bio_err, *args, FORMAT_PEM,
343 NULL, e, "certificate");
345 if(!add_ocsp_cert(&req, cert, issuer, ids))
347 if(!sk_push(reqnames, *args))
352 else if (!strcmp(*args, "-serial"))
357 if(!add_ocsp_serial(&req, *args, issuer, ids))
359 if(!sk_push(reqnames, *args))
368 /* Have we anything to do? */
369 if (!req && !reqin && !respin) badarg = 1;
373 BIO_printf (bio_err, "OCSP utility\n");
374 BIO_printf (bio_err, "Usage ocsp [options]\n");
375 BIO_printf (bio_err, "where options are\n");
376 BIO_printf (bio_err, "-out file output filename\n");
377 BIO_printf (bio_err, "-issuer file issuer certificate\n");
378 BIO_printf (bio_err, "-cert file certificate to check\n");
379 BIO_printf (bio_err, "-serial n serial number to check\n");
380 BIO_printf (bio_err, "-signer file certificate to sign OCSP request with\n");
381 BIO_printf (bio_err, "-signkey file private key to sign OCSP request with\n");
382 BIO_printf (bio_err, "-sign_certs file additional certificates to include in signed request\n");
383 BIO_printf (bio_err, "-no_certs don't include any certificates in signed request\n");
384 BIO_printf (bio_err, "-req_text print text form of request\n");
385 BIO_printf (bio_err, "-resp_text print text form of response\n");
386 BIO_printf (bio_err, "-text print text form of request and response\n");
387 BIO_printf (bio_err, "-reqout file write DER encoded OCSP request to \"file\"\n");
388 BIO_printf (bio_err, "-respout file write DER encoded OCSP reponse to \"file\"\n");
389 BIO_printf (bio_err, "-reqin file read DER encoded OCSP request from \"file\"\n");
390 BIO_printf (bio_err, "-respin file read DER encoded OCSP reponse from \"file\"\n");
391 BIO_printf (bio_err, "-nonce add OCSP nonce to request\n");
392 BIO_printf (bio_err, "-no_nonce don't add OCSP nonce to request\n");
393 BIO_printf (bio_err, "-url URL OCSP responder URL\n");
394 BIO_printf (bio_err, "-host host:n send OCSP request to host on port n\n");
395 BIO_printf (bio_err, "-path path to use in OCSP request\n");
396 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
397 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
398 BIO_printf (bio_err, "-VAfile file validator certificates file\n");
399 BIO_printf (bio_err, "-validity_period n maximum validity discrepancy in seconds\n");
400 BIO_printf (bio_err, "-status_age n maximum status age in seconds\n");
401 BIO_printf (bio_err, "-noverify don't verify response at all\n");
402 BIO_printf (bio_err, "-verify_certs file additional certificates to search for signer\n");
403 BIO_printf (bio_err, "-trust_other don't verify additional certificates\n");
404 BIO_printf (bio_err, "-no_intern don't search certificates contained in response for signer\n");
405 BIO_printf (bio_err, "-no_sig_verify don't check signature on response\n");
406 BIO_printf (bio_err, "-no_cert_verify don't check signing certificate\n");
407 BIO_printf (bio_err, "-no_chain don't chain verify response\n");
408 BIO_printf (bio_err, "-no_cert_checks don't do additional checks on signing certificate\n");
412 if(outfile) out = BIO_new_file(outfile, "w");
413 else out = BIO_new_fp(stdout, BIO_NOCLOSE);
417 BIO_printf(bio_err, "Error opening output file\n");
421 if (!req && (add_nonce != 2)) add_nonce = 0;
425 derbio = BIO_new_file(reqin, "rb");
428 BIO_printf(bio_err, "Error Opening OCSP request file\n");
431 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
435 BIO_printf(bio_err, "Error reading OCSP request\n");
440 if (!req && (signfile || reqout || host || add_nonce))
442 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
446 if (req && add_nonce) OCSP_request_add1_nonce(req, NULL, -1);
450 if (!keyfile) keyfile = signfile;
451 signer = load_cert(bio_err, signfile, FORMAT_PEM,
452 NULL, e, "signer certificate");
455 BIO_printf(bio_err, "Error loading signer certificate\n");
460 sign_other = load_certs(bio_err, sign_certfile, FORMAT_PEM,
461 NULL, e, "signer certificates");
462 if (!sign_other) goto end;
464 key = load_key(bio_err, keyfile, FORMAT_PEM, NULL, NULL,
465 "signer private key");
468 #if 0 /* An appropriate message has already been printed */
469 BIO_printf(bio_err, "Error loading signer private key\n");
473 if (!OCSP_request_sign(req, signer, key, EVP_sha1(), sign_other, sign_flags))
475 BIO_printf(bio_err, "Error signing OCSP request\n");
482 derbio = BIO_new_file(reqout, "wb");
485 BIO_printf(bio_err, "Error opening file %s\n", reqout);
488 i2d_OCSP_REQUEST_bio(derbio, req);
492 if (req_text && req) OCSP_REQUEST_print(out, req, 0);
496 cbio = BIO_new_connect(host);
499 BIO_printf(bio_err, "Error creating connect BIO\n");
502 if (port) BIO_set_conn_port(cbio, port);
506 ctx = SSL_CTX_new(SSLv23_client_method());
507 SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
508 sbio = BIO_new_ssl(ctx, 1);
509 cbio = BIO_push(sbio, cbio);
511 if (BIO_do_connect(cbio) <= 0)
513 BIO_printf(bio_err, "Error connecting BIO\n");
516 resp = OCSP_sendreq_bio(cbio, path, req);
521 BIO_printf(bio_err, "Error querying OCSP responsder\n");
527 derbio = BIO_new_file(respin, "rb");
530 BIO_printf(bio_err, "Error Opening OCSP response file\n");
533 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
537 BIO_printf(bio_err, "Error reading OCSP response\n");
550 derbio = BIO_new_file(respout, "wb");
553 BIO_printf(bio_err, "Error opening file %s\n", respout);
556 i2d_OCSP_RESPONSE_bio(derbio, resp);
560 i = OCSP_response_status(resp);
562 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL)
564 BIO_printf(out, "Responder Error: %s (%ld)\n",
565 OCSP_response_status_str(i), i);
570 if (resp_text) OCSP_RESPONSE_print(out, resp, 0);
572 store = setup_verify(bio_err, CAfile, CApath);
576 verify_other = load_certs(bio_err, verify_certfile, FORMAT_PEM,
577 NULL, e, "validator certificate");
578 if (!verify_other) goto end;
581 bs = OCSP_response_get1_basic(resp);
585 BIO_printf(bio_err, "Error parsing response\n");
591 if (req && ((i = OCSP_check_nonce(req, bs)) <= 0))
594 BIO_printf(bio_err, "WARNING: no nonce in response\n");
597 BIO_printf(bio_err, "Nonce Verify error\n");
602 i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
603 if (i < 0) i = OCSP_basic_verify(bs, NULL, store, 0);
607 BIO_printf(bio_err, "Response Verify Failure\n", i);
608 ERR_print_errors(bio_err);
611 BIO_printf(bio_err, "Response verify OK\n");
615 if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
621 ERR_print_errors(bio_err);
623 X509_STORE_free(store);
629 OCSP_REQUEST_free(req);
630 OCSP_RESPONSE_free(resp);
631 OCSP_BASICRESP_free(bs);
633 sk_OCSP_CERTID_free(ids);
634 sk_X509_pop_free(sign_other, X509_free);
635 sk_X509_pop_free(verify_other, X509_free);
648 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer,
649 STACK_OF(OCSP_CERTID) *ids)
654 BIO_printf(bio_err, "No issuer certificate specified\n");
657 if(!*req) *req = OCSP_REQUEST_new();
659 id = OCSP_cert_to_id(NULL, cert, issuer);
660 if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
661 if(!OCSP_request_add0_id(*req, id)) goto err;
665 BIO_printf(bio_err, "Error Creating OCSP request\n");
669 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
670 STACK_OF(OCSP_CERTID) *ids)
674 ASN1_BIT_STRING *ikey;
678 BIO_printf(bio_err, "No issuer certificate specified\n");
681 if(!*req) *req = OCSP_REQUEST_new();
683 iname = X509_get_subject_name(issuer);
684 ikey = X509_get0_pubkey_bitstr(issuer);
685 sno = s2i_ASN1_INTEGER(NULL, serial);
688 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
691 id = OCSP_cert_id_new(EVP_sha1(), iname, ikey, sno);
692 ASN1_INTEGER_free(sno);
693 if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
694 if(!OCSP_request_add0_id(*req, id)) goto err;
698 BIO_printf(bio_err, "Error Creating OCSP request\n");
702 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
703 STACK *names, STACK_OF(OCSP_CERTID) *ids,
704 long nsec, long maxage)
712 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
714 if (!bs || !req || !sk_num(names) || !sk_OCSP_CERTID_num(ids))
717 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++)
719 id = sk_OCSP_CERTID_value(ids, i);
720 name = sk_value(names, i);
721 BIO_printf(out, "%s: ", name);
723 if(!OCSP_resp_find_status(bs, id, &status, &reason,
724 &rev, &thisupd, &nextupd))
726 BIO_puts(out, "ERROR: No Status found.\n");
730 /* Check validity: if invalid write to output BIO so we
731 * know which response this refers to.
733 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage))
735 BIO_puts(out, "WARNING: Status times invalid.\n");
736 ERR_print_errors(out);
738 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
740 BIO_puts(out, "\tThis Update: ");
741 ASN1_GENERALIZEDTIME_print(out, thisupd);
746 BIO_puts(out, "\tNext Update: ");
747 ASN1_GENERALIZEDTIME_print(out, nextupd);
751 if (status != V_OCSP_CERTSTATUS_REVOKED)
755 BIO_printf(out, "\tReason: %s\n",
756 OCSP_crl_reason_str(reason));
758 BIO_puts(out, "\tRevocation Time: ");
759 ASN1_GENERALIZEDTIME_print(out, rev);