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>
66 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer,
67 STACK_OF(OCSP_CERTID) *ids);
68 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
69 STACK_OF(OCSP_CERTID) *ids);
70 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
71 STACK *names, STACK_OF(OCSP_CERTID) *ids);
74 #define PROG ocsp_main
76 int MAIN(int, char **);
78 int MAIN(int argc, char **argv)
81 char *host = NULL, *path = "/";
82 char *reqin = NULL, *respin = NULL;
83 char *reqout = NULL, *respout = NULL;
84 char *signfile = NULL, *keyfile = NULL;
86 int add_nonce = 1, noverify = 0;
87 OCSP_REQUEST *req = NULL;
88 OCSP_RESPONSE *resp = NULL;
89 OCSP_BASICRESP *bs = NULL;
90 X509 *issuer = NULL, *cert = NULL;
93 BIO *cbio = NULL, *derbio = NULL;
95 int req_text = 0, resp_text = 0;
96 char *CAfile = NULL, *CApath = NULL;
97 X509_STORE *store = NULL;
101 STACK *reqnames = NULL;
102 STACK_OF(OCSP_CERTID) *ids = NULL;
103 if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
104 ERR_load_crypto_strings();
106 reqnames = sk_new_null();
107 ids = sk_OCSP_CERTID_new_null();
108 while (!badarg && *args && *args[0] == '-')
110 if (!strcmp(*args, "-out"))
119 else if (!strcmp(*args, "-host"))
128 else if (!strcmp(*args, "-noverify"))
130 else if (!strcmp(*args, "-nonce"))
132 else if (!strcmp(*args, "-no_nonce"))
134 else if (!strcmp(*args, "-text"))
139 else if (!strcmp(*args, "-req_text"))
141 else if (!strcmp(*args, "-resp_text"))
143 else if (!strcmp(*args, "-reqin"))
152 else if (!strcmp(*args, "-respin"))
161 else if (!strcmp(*args, "-signer"))
170 else if (!strcmp (*args, "-CAfile"))
179 else if (!strcmp (*args, "-CApath"))
188 else if (!strcmp(*args, "-signkey"))
197 else if (!strcmp(*args, "-reqout"))
206 else if (!strcmp(*args, "-respout"))
215 else if (!strcmp(*args, "-path"))
224 else if (!strcmp(*args, "-issuer"))
230 issuer = load_cert(bio_err, *args, FORMAT_PEM);
231 if(!issuer) goto end;
235 else if (!strcmp (*args, "-cert"))
241 cert = load_cert(bio_err, *args, FORMAT_PEM);
243 if(!add_ocsp_cert(&req, cert, issuer, ids))
245 if(!sk_push(reqnames, *args))
250 else if (!strcmp(*args, "-serial"))
255 if(!add_ocsp_serial(&req, *args, issuer, ids))
257 if(!sk_push(reqnames, *args))
266 /* Have we anything to do? */
267 if (!req && !reqin && !respin) badarg = 1;
271 BIO_printf (bio_err, "OCSP utility\n");
272 BIO_printf (bio_err, "Usage ocsp [options]\n");
273 BIO_printf (bio_err, "where options are\n");
274 BIO_printf (bio_err, "-out file output filename\n");
275 BIO_printf (bio_err, "-issuer file issuer certificate\n");
276 BIO_printf (bio_err, "-cert file certificate to check\n");
277 BIO_printf (bio_err, "-serial n serial number to check\n");
278 BIO_printf (bio_err, "-signer file certificate to sign OCSP request with\n");
279 BIO_printf (bio_err, "-signkey file private key to sign OCSP request with\n");
280 BIO_printf (bio_err, "-req_text print text form of request\n");
281 BIO_printf (bio_err, "-resp_text print text form of response\n");
282 BIO_printf (bio_err, "-text print text form of request and response\n");
283 BIO_printf (bio_err, "-reqout file write DER encoded OCSP request to \"file\"\n");
284 BIO_printf (bio_err, "-respout file write DER encoded OCSP reponse to \"file\"\n");
285 BIO_printf (bio_err, "-reqin file read DER encoded OCSP request from \"file\"\n");
286 BIO_printf (bio_err, "-respin file read DER encoded OCSP reponse from \"file\"\n");
287 BIO_printf (bio_err, "-nonce add OCSP nonce to request\n");
288 BIO_printf (bio_err, "-no_nonce don't add OCSP nonce to request\n");
289 BIO_printf (bio_err, "-host host:n send OCSP request to host on port n\n");
290 BIO_printf (bio_err, "-path path to use in OCSP request\n");
291 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
292 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
293 BIO_printf (bio_err, "-noverify don't verify response\n");
297 if(outfile) out = BIO_new_file(outfile, "w");
298 else out = BIO_new_fp(stdout, BIO_NOCLOSE);
302 BIO_printf(bio_err, "Error opening output file\n");
306 if (!req && (add_nonce != 2)) add_nonce = 0;
310 derbio = BIO_new_file(reqin, "rb");
313 BIO_printf(bio_err, "Error Opening OCSP request file\n");
316 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
320 BIO_printf(bio_err, "Error reading OCSP request\n");
325 if (!req && (signfile || reqout || host || add_nonce))
327 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
331 if (req && add_nonce) OCSP_request_add1_nonce(req, NULL, -1);
335 if (!keyfile) keyfile = signfile;
336 signer = load_cert(bio_err, signfile, FORMAT_PEM);
339 BIO_printf(bio_err, "Error loading signer certificate\n");
342 key = load_key(bio_err, keyfile, FORMAT_PEM, NULL, NULL);
345 BIO_printf(bio_err, "Error loading signer private key\n");
348 if (!OCSP_request_sign(req, signer, key, EVP_sha1(), NULL, 0))
350 BIO_printf(bio_err, "Error signing OCSP request\n");
357 derbio = BIO_new_file(reqout, "wb");
360 BIO_printf(bio_err, "Error opening file %s\n", reqout);
363 i2d_OCSP_REQUEST_bio(derbio, req);
367 if (req_text && req) OCSP_REQUEST_print(out, req, 0);
371 cbio = BIO_new_connect(host);
374 BIO_printf(bio_err, "Error creating connect BIO\n");
377 if (BIO_do_connect(cbio) <= 0)
379 BIO_printf(bio_err, "Error connecting BIO\n");
382 resp = OCSP_sendreq_bio(cbio, path, req);
387 BIO_printf(bio_err, "Error querying OCSP responsder\n");
393 derbio = BIO_new_file(respin, "rb");
396 BIO_printf(bio_err, "Error Opening OCSP response file\n");
399 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
403 BIO_printf(bio_err, "Error reading OCSP response\n");
416 derbio = BIO_new_file(respout, "wb");
419 BIO_printf(bio_err, "Error opening file %s\n", respout);
422 i2d_OCSP_RESPONSE_bio(derbio, resp);
426 i = OCSP_response_status(resp);
428 if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL)
430 BIO_printf(out, "Responder Error: %s (%ld)\n",
431 OCSP_response_status_str(i), i);
436 if (resp_text) OCSP_RESPONSE_print(out, resp, 0);
438 store = setup_verify(bio_err, CAfile, CApath);
441 bs = OCSP_response_get1_basic(resp);
445 BIO_printf(bio_err, "Error parsing response\n");
451 if (req && (OCSP_check_nonce(req, bs) <= 0))
453 BIO_printf(bio_err, "Nonce Verify error\n");
457 i = OCSP_basic_verify(bs, NULL, store, 0);
461 BIO_printf(bio_err, "Response Verify Failure\n", i);
462 ERR_print_errors(bio_err);
465 BIO_printf(bio_err, "Response verify OK\n");
469 if (!print_ocsp_summary(out, bs, req, reqnames, ids))
475 ERR_print_errors(bio_err);
477 X509_STORE_free(store);
483 OCSP_REQUEST_free(req);
484 OCSP_RESPONSE_free(resp);
485 OCSP_BASICRESP_free(bs);
487 sk_OCSP_CERTID_free(ids);
492 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer,
493 STACK_OF(OCSP_CERTID) *ids)
498 BIO_printf(bio_err, "No issuer certificate specified\n");
501 if(!*req) *req = OCSP_REQUEST_new();
503 id = OCSP_cert_to_id(NULL, cert, issuer);
504 if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
505 if(!OCSP_request_add0_id(*req, id)) goto err;
509 BIO_printf(bio_err, "Error Creating OCSP request\n");
513 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
514 STACK_OF(OCSP_CERTID) *ids)
518 ASN1_BIT_STRING *ikey;
522 BIO_printf(bio_err, "No issuer certificate specified\n");
525 if(!*req) *req = OCSP_REQUEST_new();
527 iname = X509_get_subject_name(issuer);
528 ikey = X509_get0_pubkey_bitstr(issuer);
529 sno = s2i_ASN1_INTEGER(NULL, serial);
532 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
535 id = OCSP_cert_id_new(EVP_sha1(), iname, ikey, sno);
536 ASN1_INTEGER_free(sno);
537 if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
538 if(!OCSP_request_add0_id(*req, id)) goto err;
542 BIO_printf(bio_err, "Error Creating OCSP request\n");
546 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
547 STACK *names, STACK_OF(OCSP_CERTID) *ids)
555 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
557 if (!bs || !req || !sk_num(names) || !sk_OCSP_CERTID_num(ids))
560 for (i = 0; i < sk_OCSP_CERTID_num(ids); i++)
562 id = sk_OCSP_CERTID_value(ids, i);
563 name = sk_value(names, i);
564 BIO_printf(out, "%s: ", name);
566 if(!OCSP_resp_find_status(bs, id, &status, &reason,
567 &rev, &thisupd, &nextupd))
569 BIO_puts(out, "ERROR: No Status found.\n");
572 BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
574 BIO_puts(out, "\tThis Update: ");
575 ASN1_GENERALIZEDTIME_print(out, thisupd);
580 BIO_puts(out, "\tNext Update: ");
581 ASN1_GENERALIZEDTIME_print(out, thisupd);
585 if (status != V_OCSP_CERTSTATUS_REVOKED)
589 BIO_printf(out, "\tReason: %s\n",
590 OCSP_crl_reason_str(reason));
592 BIO_puts(out, "\tRevocation Time: ");
593 ASN1_GENERALIZEDTIME_print(out, rev);