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 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer);
70 #define PROG ocsp_main
72 int MAIN(int, char **);
74 int MAIN(int argc, char **argv)
77 char *host = NULL, *path = "/";
78 char *reqin = NULL, *respin = NULL;
79 char *reqout = NULL, *respout = NULL;
80 char *signfile = NULL, *keyfile = NULL;
83 OCSP_REQUEST *req = NULL;
84 OCSP_RESPONSE *resp = NULL;
85 X509 *issuer = NULL, *cert = NULL;
88 BIO *cbio = NULL, *derbio = NULL;
90 int req_text = 0, resp_text = 0;
93 if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
94 ERR_load_crypto_strings();
96 while (!badarg && *args && *args[0] == '-')
98 if (!strcmp(*args, "-out"))
107 else if (!strcmp(*args, "-host"))
116 else if (!strcmp(*args, "-nonce"))
118 else if (!strcmp(*args, "-no_nonce"))
120 else if (!strcmp(*args, "-text"))
125 else if (!strcmp(*args, "-req_text"))
127 else if (!strcmp(*args, "-resp_text"))
129 else if (!strcmp(*args, "-reqin"))
138 else if (!strcmp(*args, "-respin"))
147 else if (!strcmp(*args, "-signer"))
156 else if (!strcmp(*args, "-signkey"))
165 else if (!strcmp(*args, "-reqout"))
174 else if (!strcmp(*args, "-respout"))
183 else if (!strcmp(*args, "-path"))
192 else if (!strcmp(*args, "-issuer"))
198 issuer = load_cert(bio_err, *args, FORMAT_PEM);
199 if(!issuer) goto end;
203 else if (!strcmp (*args, "-cert"))
209 cert = load_cert(bio_err, *args, FORMAT_PEM);
211 if(!add_ocsp_cert(&req, cert, issuer))
216 else if (!strcmp(*args, "-serial"))
221 if(!add_ocsp_serial(&req, *args, issuer))
230 /* Have we anything to do? */
231 if (!req && !reqin && !respin) badarg = 1;
235 BIO_printf (bio_err, "OCSP utility\n");
236 BIO_printf (bio_err, "Usage ocsp [options]\n");
237 BIO_printf (bio_err, "where options are\n");
238 BIO_printf (bio_err, "-out file output filename\n");
239 BIO_printf (bio_err, "-issuer file issuer certificate\n");
240 BIO_printf (bio_err, "-cert file certificate to check\n");
241 BIO_printf (bio_err, "-serial n serial number to check\n");
242 BIO_printf (bio_err, "-signer file certificate to sign OCSP request with\n");
243 BIO_printf (bio_err, "-signkey file private key to sign OCSP request with\n");
244 BIO_printf (bio_err, "-req_text print text form of request\n");
245 BIO_printf (bio_err, "-resp_text print text form of response\n");
246 BIO_printf (bio_err, "-text print text form of request and response\n");
247 BIO_printf (bio_err, "-reqout file write DER encoded OCSP request to \"file\"\n");
248 BIO_printf (bio_err, "-respout file write DER encoded OCSP reponse to \"file\"\n");
249 BIO_printf (bio_err, "-reqin file read DER encoded OCSP request from \"file\"\n");
250 BIO_printf (bio_err, "-respin file read DER encoded OCSP reponse from \"file\"\n");
251 BIO_printf (bio_err, "-nonce add OCSP nonce to request\n");
252 BIO_printf (bio_err, "-no_nonce don't add OCSP nonce to request\n");
253 BIO_printf (bio_err, "-host host:n send OCSP request to host on port n\n");
254 BIO_printf (bio_err, "-path path to use in OCSP request\n");
258 if(outfile) out = BIO_new_file(outfile, "w");
259 else out = BIO_new_fp(stdout, BIO_NOCLOSE);
263 BIO_printf(bio_err, "Error opening output file\n");
267 if (!req && (add_nonce != 2)) add_nonce = 0;
271 derbio = BIO_new_file(reqin, "rb");
274 BIO_printf(bio_err, "Error Opening OCSP request file\n");
277 req = d2i_OCSP_REQUEST_bio(derbio, NULL);
281 BIO_printf(bio_err, "Error reading OCSP request\n");
286 if (!req && (signfile || reqout || host || add_nonce))
288 BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
292 if (req) OCSP_request_add1_nonce(req, NULL, -1);
296 if (!keyfile) keyfile = signfile;
297 signer = load_cert(bio_err, signfile, FORMAT_PEM);
300 BIO_printf(bio_err, "Error loading signer certificate\n");
303 key = load_key(bio_err, keyfile, FORMAT_PEM, NULL, NULL);
306 BIO_printf(bio_err, "Error loading signer private key\n");
309 if (!OCSP_request_sign(req, signer, key, EVP_sha1(), NULL, 0))
311 BIO_printf(bio_err, "Error signing OCSP request\n");
318 derbio = BIO_new_file(reqout, "wb");
321 BIO_printf(bio_err, "Error opening file %s\n", reqout);
324 i2d_OCSP_REQUEST_bio(derbio, req);
328 if (req_text && req) OCSP_REQUEST_print(out, req, 0);
332 cbio = BIO_new_connect(host);
335 BIO_printf(bio_err, "Error creating connect BIO\n");
338 if (BIO_do_connect(cbio) <= 0)
340 BIO_printf(bio_err, "Error connecting BIO\n");
343 resp = OCSP_sendreq_bio(cbio, path, req);
348 BIO_printf(bio_err, "Error querying OCSP responsder\n");
354 derbio = BIO_new_file(respin, "rb");
357 BIO_printf(bio_err, "Error Opening OCSP response file\n");
360 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
364 BIO_printf(bio_err, "Error reading OCSP response\n");
377 derbio = BIO_new_file(respout, "wb");
380 BIO_printf(bio_err, "Error opening file %s\n", respout);
383 i2d_OCSP_RESPONSE_bio(derbio, resp);
387 if (resp_text) OCSP_RESPONSE_print(out, resp, 0);
392 ERR_print_errors(bio_err);
399 OCSP_REQUEST_free(req);
400 OCSP_RESPONSE_free(resp);
405 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer)
410 BIO_printf(bio_err, "No issuer certificate specified\n");
413 if(!*req) *req = OCSP_REQUEST_new();
415 id = OCSP_cert_to_id(NULL, cert, issuer);
417 if(!OCSP_request_add0_id(*req, id)) goto err;
421 BIO_printf(bio_err, "Error Creating OCSP request\n");
425 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer)
429 ASN1_BIT_STRING *ikey;
433 BIO_printf(bio_err, "No issuer certificate specified\n");
436 if(!*req) *req = OCSP_REQUEST_new();
438 iname = X509_get_subject_name(issuer);
439 ikey = issuer->cert_info->key->public_key;
440 sno = s2i_ASN1_INTEGER(NULL, serial);
443 BIO_printf(bio_err, "Error converting serial number %s\n", serial);
446 id = OCSP_cert_id_new(EVP_sha1(), iname, ikey, sno);
447 ASN1_INTEGER_free(sno);
449 if(!OCSP_request_add0_id(*req, id)) goto err;
453 BIO_printf(bio_err, "Error Creating OCSP request\n");