Annotation of src/usr.bin/openssl/ocsp.c, Revision 1.5
1.5 ! deraadt 1: /* $OpenBSD: ocsp.c,v 1.4 2015/09/11 14:30:23 bcook Exp $ */
1.1 jsing 2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3: * project 2000.
4: */
5: /* ====================================================================
6: * Copyright (c) 1999 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: #ifndef OPENSSL_NO_OCSP
59:
1.2 deraadt 60: #include <sys/types.h>
1.1 jsing 61:
62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <limits.h>
65: #include <string.h>
1.2 deraadt 66: #include <poll.h>
1.1 jsing 67: #include <time.h>
68:
69: /* Needs to be included before the openssl headers! */
70: #include "apps.h"
71:
72: #include <openssl/bn.h>
73: #include <openssl/crypto.h>
74: #include <openssl/err.h>
75: #include <openssl/evp.h>
76: #include <openssl/ssl.h>
77: #include <openssl/x509v3.h>
78:
79: /* Maximum leeway in validity period: default 5 minutes */
80: #define MAX_VALIDITY_PERIOD (5 * 60)
81:
82: static int
83: add_ocsp_cert(OCSP_REQUEST ** req, X509 * cert, const EVP_MD * cert_id_md, X509 * issuer,
84: STACK_OF(OCSP_CERTID) * ids);
85: static int add_ocsp_serial(OCSP_REQUEST ** req, char *serial, const EVP_MD * cert_id_md, X509 * issuer,
86: STACK_OF(OCSP_CERTID) * ids);
87: static int print_ocsp_summary(BIO * out, OCSP_BASICRESP * bs, OCSP_REQUEST * req,
88: STACK_OF(OPENSSL_STRING) * names,
89: STACK_OF(OCSP_CERTID) * ids, long nsec,
90: long maxage);
91:
92: static int make_ocsp_response(OCSP_RESPONSE ** resp, OCSP_REQUEST * req, CA_DB * db,
93: X509 * ca, X509 * rcert, EVP_PKEY * rkey,
94: STACK_OF(X509) * rother, unsigned long flags,
95: int nmin, int ndays);
96:
97: static char **lookup_serial(CA_DB * db, ASN1_INTEGER * ser);
98: static BIO *init_responder(char *port);
99: static int do_responder(OCSP_REQUEST ** preq, BIO ** pcbio, BIO * acbio, char *port);
100: static int send_ocsp_response(BIO * cbio, OCSP_RESPONSE * resp);
101: static OCSP_RESPONSE *query_responder(BIO * err, BIO * cbio, char *path,
102: STACK_OF(CONF_VALUE) * headers,
103: OCSP_REQUEST * req, int req_timeout);
104:
105: int
106: ocsp_main(int argc, char **argv)
107: {
108: char **args;
109: char *host = NULL, *port = NULL, *path = "/";
110: char *reqin = NULL, *respin = NULL;
111: char *reqout = NULL, *respout = NULL;
112: char *signfile = NULL, *keyfile = NULL;
113: char *rsignfile = NULL, *rkeyfile = NULL;
114: char *outfile = NULL;
115: int add_nonce = 1, noverify = 0, use_ssl = -1;
116: STACK_OF(CONF_VALUE) * headers = NULL;
117: OCSP_REQUEST *req = NULL;
118: OCSP_RESPONSE *resp = NULL;
119: OCSP_BASICRESP *bs = NULL;
120: X509 *issuer = NULL, *cert = NULL;
121: X509 *signer = NULL, *rsigner = NULL;
122: EVP_PKEY *key = NULL, *rkey = NULL;
123: BIO *acbio = NULL, *cbio = NULL;
124: BIO *derbio = NULL;
125: BIO *out = NULL;
126: int req_timeout = -1;
127: int req_text = 0, resp_text = 0;
128: long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
129: char *CAfile = NULL, *CApath = NULL;
130: X509_STORE *store = NULL;
131: STACK_OF(X509) * sign_other = NULL, *verify_other = NULL, *rother = NULL;
132: char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
133: unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
134: int ret = 1;
135: int accept_count = -1;
136: int badarg = 0;
137: int i;
138: int ignore_err = 0;
139: STACK_OF(OPENSSL_STRING) * reqnames = NULL;
140: STACK_OF(OCSP_CERTID) * ids = NULL;
141: X509 *rca_cert = NULL;
142: char *ridx_filename = NULL;
143: char *rca_filename = NULL;
144: CA_DB *rdb = NULL;
145: int nmin = 0, ndays = -1;
146: const EVP_MD *cert_id_md = NULL;
147: const char *errstr = NULL;
148:
149: args = argv + 1;
150: reqnames = sk_OPENSSL_STRING_new_null();
151: ids = sk_OCSP_CERTID_new_null();
152: while (!badarg && *args && *args[0] == '-') {
153: if (!strcmp(*args, "-out")) {
154: if (args[1]) {
155: args++;
156: outfile = *args;
157: } else
158: badarg = 1;
159: } else if (!strcmp(*args, "-timeout")) {
160: if (args[1]) {
161: args++;
162: req_timeout = strtonum(*args, 0,
163: INT_MAX, &errstr);
164: if (errstr) {
165: BIO_printf(bio_err,
166: "Illegal timeout value %s: %s\n",
167: *args, errstr);
168: badarg = 1;
169: }
170: } else
171: badarg = 1;
172: } else if (!strcmp(*args, "-url")) {
173: if (args[1]) {
174: args++;
175: if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl)) {
176: BIO_printf(bio_err, "Error parsing URL\n");
177: badarg = 1;
178: }
179: } else
180: badarg = 1;
181: } else if (!strcmp(*args, "-host")) {
182: if (args[1]) {
183: args++;
184: host = *args;
185: } else
186: badarg = 1;
187: } else if (!strcmp(*args, "-port")) {
188: if (args[1]) {
189: args++;
190: port = *args;
191: } else
192: badarg = 1;
193: } else if (!strcmp(*args, "-header")) {
194: if (args[1] && args[2]) {
195: if (!X509V3_add_value(args[1], args[2], &headers))
196: goto end;
197: args += 2;
198: } else
199: badarg = 1;
200: } else if (!strcmp(*args, "-ignore_err"))
201: ignore_err = 1;
202: else if (!strcmp(*args, "-noverify"))
203: noverify = 1;
204: else if (!strcmp(*args, "-nonce"))
205: add_nonce = 2;
206: else if (!strcmp(*args, "-no_nonce"))
207: add_nonce = 0;
208: else if (!strcmp(*args, "-resp_no_certs"))
209: rflags |= OCSP_NOCERTS;
210: else if (!strcmp(*args, "-resp_key_id"))
211: rflags |= OCSP_RESPID_KEY;
212: else if (!strcmp(*args, "-no_certs"))
213: sign_flags |= OCSP_NOCERTS;
214: else if (!strcmp(*args, "-no_signature_verify"))
215: verify_flags |= OCSP_NOSIGS;
216: else if (!strcmp(*args, "-no_cert_verify"))
217: verify_flags |= OCSP_NOVERIFY;
218: else if (!strcmp(*args, "-no_chain"))
219: verify_flags |= OCSP_NOCHAIN;
220: else if (!strcmp(*args, "-no_cert_checks"))
221: verify_flags |= OCSP_NOCHECKS;
222: else if (!strcmp(*args, "-no_explicit"))
223: verify_flags |= OCSP_NOEXPLICIT;
224: else if (!strcmp(*args, "-trust_other"))
225: verify_flags |= OCSP_TRUSTOTHER;
226: else if (!strcmp(*args, "-no_intern"))
227: verify_flags |= OCSP_NOINTERN;
228: else if (!strcmp(*args, "-text")) {
229: req_text = 1;
230: resp_text = 1;
231: } else if (!strcmp(*args, "-req_text"))
232: req_text = 1;
233: else if (!strcmp(*args, "-resp_text"))
234: resp_text = 1;
235: else if (!strcmp(*args, "-reqin")) {
236: if (args[1]) {
237: args++;
238: reqin = *args;
239: } else
240: badarg = 1;
241: } else if (!strcmp(*args, "-respin")) {
242: if (args[1]) {
243: args++;
244: respin = *args;
245: } else
246: badarg = 1;
247: } else if (!strcmp(*args, "-signer")) {
248: if (args[1]) {
249: args++;
250: signfile = *args;
251: } else
252: badarg = 1;
253: } else if (!strcmp(*args, "-VAfile")) {
254: if (args[1]) {
255: args++;
256: verify_certfile = *args;
257: verify_flags |= OCSP_TRUSTOTHER;
258: } else
259: badarg = 1;
260: } else if (!strcmp(*args, "-sign_other")) {
261: if (args[1]) {
262: args++;
263: sign_certfile = *args;
264: } else
265: badarg = 1;
266: } else if (!strcmp(*args, "-verify_other")) {
267: if (args[1]) {
268: args++;
269: verify_certfile = *args;
270: } else
271: badarg = 1;
272: } else if (!strcmp(*args, "-CAfile")) {
273: if (args[1]) {
274: args++;
275: CAfile = *args;
276: } else
277: badarg = 1;
278: } else if (!strcmp(*args, "-CApath")) {
279: if (args[1]) {
280: args++;
281: CApath = *args;
282: } else
283: badarg = 1;
284: } else if (!strcmp(*args, "-validity_period")) {
285: if (args[1]) {
286: args++;
287: nsec = strtonum(*args, 0, LONG_MAX, &errstr);
288: if (errstr) {
289: BIO_printf(bio_err,
290: "Illegal validity period %s: %s\n",
291: *args, errstr);
292: badarg = 1;
293: }
294: } else
295: badarg = 1;
296: } else if (!strcmp(*args, "-status_age")) {
297: if (args[1]) {
298: args++;
299: maxage = strtonum(*args, 0, LONG_MAX, &errstr);
300: if (errstr) {
301: BIO_printf(bio_err,
302: "Illegal validity age %s: %s\n",
303: *args, errstr);
304: badarg = 1;
305: }
306: } else
307: badarg = 1;
308: } else if (!strcmp(*args, "-signkey")) {
309: if (args[1]) {
310: args++;
311: keyfile = *args;
312: } else
313: badarg = 1;
314: } else if (!strcmp(*args, "-reqout")) {
315: if (args[1]) {
316: args++;
317: reqout = *args;
318: } else
319: badarg = 1;
320: } else if (!strcmp(*args, "-respout")) {
321: if (args[1]) {
322: args++;
323: respout = *args;
324: } else
325: badarg = 1;
326: } else if (!strcmp(*args, "-path")) {
327: if (args[1]) {
328: args++;
329: path = *args;
330: } else
331: badarg = 1;
332: } else if (!strcmp(*args, "-issuer")) {
333: if (args[1]) {
334: args++;
335: X509_free(issuer);
336: issuer = load_cert(bio_err, *args, FORMAT_PEM,
1.4 bcook 337: NULL, "issuer certificate");
1.1 jsing 338: if (!issuer)
339: goto end;
340: } else
341: badarg = 1;
342: } else if (!strcmp(*args, "-cert")) {
343: if (args[1]) {
344: args++;
345: X509_free(cert);
346: cert = load_cert(bio_err, *args, FORMAT_PEM,
1.4 bcook 347: NULL, "certificate");
1.1 jsing 348: if (!cert)
349: goto end;
350: if (!cert_id_md)
351: cert_id_md = EVP_sha1();
352: if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
353: goto end;
354: if (!sk_OPENSSL_STRING_push(reqnames, *args))
355: goto end;
356: } else
357: badarg = 1;
358: } else if (!strcmp(*args, "-serial")) {
359: if (args[1]) {
360: args++;
361: if (!cert_id_md)
362: cert_id_md = EVP_sha1();
363: if (!add_ocsp_serial(&req, *args, cert_id_md, issuer, ids))
364: goto end;
365: if (!sk_OPENSSL_STRING_push(reqnames, *args))
366: goto end;
367: } else
368: badarg = 1;
369: } else if (!strcmp(*args, "-index")) {
370: if (args[1]) {
371: args++;
372: ridx_filename = *args;
373: } else
374: badarg = 1;
375: } else if (!strcmp(*args, "-CA")) {
376: if (args[1]) {
377: args++;
378: rca_filename = *args;
379: } else
380: badarg = 1;
381: } else if (!strcmp(*args, "-nmin")) {
382: if (args[1]) {
383: args++;
384: nmin = strtonum(*args, 0, INT_MAX, &errstr);
385: if (errstr) {
386: BIO_printf(bio_err,
387: "Illegal update period %s: %s\n",
388: *args, errstr);
389: badarg = 1;
390: }
391: }
392: if (ndays == -1)
393: ndays = 0;
394: else
395: badarg = 1;
396: } else if (!strcmp(*args, "-nrequest")) {
397: if (args[1]) {
398: args++;
399: accept_count = strtonum(*args, 0, INT_MAX, &errstr);
400: if (errstr) {
401: BIO_printf(bio_err,
402: "Illegal accept count %s: %s\n",
403: *args, errstr);
404: badarg = 1;
405: }
406: } else
407: badarg = 1;
408: } else if (!strcmp(*args, "-ndays")) {
409: if (args[1]) {
410: args++;
411: ndays = strtonum(*args, 0, INT_MAX, &errstr);
412: if (errstr) {
413: BIO_printf(bio_err,
414: "Illegal update period %s: %s\n",
415: *args, errstr);
416: badarg = 1;
417: }
418: } else
419: badarg = 1;
420: } else if (!strcmp(*args, "-rsigner")) {
421: if (args[1]) {
422: args++;
423: rsignfile = *args;
424: } else
425: badarg = 1;
426: } else if (!strcmp(*args, "-rkey")) {
427: if (args[1]) {
428: args++;
429: rkeyfile = *args;
430: } else
431: badarg = 1;
432: } else if (!strcmp(*args, "-rother")) {
433: if (args[1]) {
434: args++;
435: rcertfile = *args;
436: } else
437: badarg = 1;
438: } else if ((cert_id_md = EVP_get_digestbyname((*args) + 1)) == NULL) {
439: badarg = 1;
440: }
441: args++;
442: }
443:
444: /* Have we anything to do? */
445: if (!req && !reqin && !respin && !(port && ridx_filename))
446: badarg = 1;
447:
448: if (badarg) {
449: BIO_printf(bio_err, "OCSP utility\n");
450: BIO_printf(bio_err, "Usage ocsp [options]\n");
451: BIO_printf(bio_err, "where options are\n");
452: BIO_printf(bio_err, "-out file output filename\n");
453: BIO_printf(bio_err, "-issuer file issuer certificate\n");
454: BIO_printf(bio_err, "-cert file certificate to check\n");
455: BIO_printf(bio_err, "-serial n serial number to check\n");
456: BIO_printf(bio_err, "-signer file certificate to sign OCSP request with\n");
457: BIO_printf(bio_err, "-signkey file private key to sign OCSP request with\n");
458: BIO_printf(bio_err, "-sign_other file additional certificates to include in signed request\n");
459: BIO_printf(bio_err, "-no_certs don't include any certificates in signed request\n");
460: BIO_printf(bio_err, "-req_text print text form of request\n");
461: BIO_printf(bio_err, "-resp_text print text form of response\n");
462: BIO_printf(bio_err, "-text print text form of request and response\n");
463: BIO_printf(bio_err, "-reqout file write DER encoded OCSP request to \"file\"\n");
464: BIO_printf(bio_err, "-respout file write DER encoded OCSP reponse to \"file\"\n");
465: BIO_printf(bio_err, "-reqin file read DER encoded OCSP request from \"file\"\n");
466: BIO_printf(bio_err, "-respin file read DER encoded OCSP reponse from \"file\"\n");
467: BIO_printf(bio_err, "-nonce add OCSP nonce to request\n");
468: BIO_printf(bio_err, "-no_nonce don't add OCSP nonce to request\n");
469: BIO_printf(bio_err, "-url URL OCSP responder URL\n");
470: BIO_printf(bio_err, "-host host:n send OCSP request to host on port n\n");
471: BIO_printf(bio_err, "-path path to use in OCSP request\n");
472: BIO_printf(bio_err, "-CApath dir trusted certificates directory\n");
473: BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
474: BIO_printf(bio_err, "-VAfile file validator certificates file\n");
475: BIO_printf(bio_err, "-validity_period n maximum validity discrepancy in seconds\n");
476: BIO_printf(bio_err, "-status_age n maximum status age in seconds\n");
477: BIO_printf(bio_err, "-noverify don't verify response at all\n");
478: BIO_printf(bio_err, "-verify_other file additional certificates to search for signer\n");
479: BIO_printf(bio_err, "-trust_other don't verify additional certificates\n");
480: BIO_printf(bio_err, "-no_intern don't search certificates contained in response for signer\n");
481: BIO_printf(bio_err, "-no_signature_verify don't check signature on response\n");
482: BIO_printf(bio_err, "-no_cert_verify don't check signing certificate\n");
483: BIO_printf(bio_err, "-no_chain don't chain verify response\n");
484: BIO_printf(bio_err, "-no_cert_checks don't do additional checks on signing certificate\n");
485: BIO_printf(bio_err, "-port num port to run responder on\n");
486: BIO_printf(bio_err, "-index file certificate status index file\n");
487: BIO_printf(bio_err, "-CA file CA certificate\n");
488: BIO_printf(bio_err, "-rsigner file responder certificate to sign responses with\n");
489: BIO_printf(bio_err, "-rkey file responder key to sign responses with\n");
490: BIO_printf(bio_err, "-rother file other certificates to include in response\n");
491: BIO_printf(bio_err, "-resp_no_certs don't include any certificates in response\n");
492: BIO_printf(bio_err, "-nmin n number of minutes before next update\n");
493: BIO_printf(bio_err, "-ndays n number of days before next update\n");
494: BIO_printf(bio_err, "-resp_key_id identify reponse by signing certificate key ID\n");
495: BIO_printf(bio_err, "-nrequest n number of requests to accept (default unlimited)\n");
496: BIO_printf(bio_err, "-<dgst alg> use specified digest in the request\n");
497: goto end;
498: }
499: if (outfile)
500: out = BIO_new_file(outfile, "w");
501: else
502: out = BIO_new_fp(stdout, BIO_NOCLOSE);
503:
504: if (!out) {
505: BIO_printf(bio_err, "Error opening output file\n");
506: goto end;
507: }
508: if (!req && (add_nonce != 2))
509: add_nonce = 0;
510:
511: if (!req && reqin) {
512: derbio = BIO_new_file(reqin, "rb");
513: if (!derbio) {
514: BIO_printf(bio_err, "Error Opening OCSP request file\n");
515: goto end;
516: }
517: req = d2i_OCSP_REQUEST_bio(derbio, NULL);
518: BIO_free(derbio);
519: if (!req) {
520: BIO_printf(bio_err, "Error reading OCSP request\n");
521: goto end;
522: }
523: }
524: if (!req && port) {
525: acbio = init_responder(port);
526: if (!acbio)
527: goto end;
528: }
529: if (rsignfile && !rdb) {
530: if (!rkeyfile)
531: rkeyfile = rsignfile;
532: rsigner = load_cert(bio_err, rsignfile, FORMAT_PEM,
1.4 bcook 533: NULL, "responder certificate");
1.1 jsing 534: if (!rsigner) {
535: BIO_printf(bio_err, "Error loading responder certificate\n");
536: goto end;
537: }
538: rca_cert = load_cert(bio_err, rca_filename, FORMAT_PEM,
1.4 bcook 539: NULL, "CA certificate");
1.1 jsing 540: if (rcertfile) {
541: rother = load_certs(bio_err, rcertfile, FORMAT_PEM,
1.4 bcook 542: NULL, "responder other certificates");
1.1 jsing 543: if (!rother)
544: goto end;
545: }
1.4 bcook 546: rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, 0, NULL,
1.1 jsing 547: "responder private key");
548: if (!rkey)
549: goto end;
550: }
551: if (acbio)
552: BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
553:
554: redo_accept:
555:
556: if (acbio) {
557: if (!do_responder(&req, &cbio, acbio, port))
558: goto end;
559: if (!req) {
560: resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
561: send_ocsp_response(cbio, resp);
562: goto done_resp;
563: }
564: }
565: if (!req && (signfile || reqout || host || add_nonce || ridx_filename)) {
566: BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
567: goto end;
568: }
569: if (req && add_nonce)
570: OCSP_request_add1_nonce(req, NULL, -1);
571:
572: if (signfile) {
573: if (!keyfile)
574: keyfile = signfile;
575: signer = load_cert(bio_err, signfile, FORMAT_PEM,
1.4 bcook 576: NULL, "signer certificate");
1.1 jsing 577: if (!signer) {
578: BIO_printf(bio_err, "Error loading signer certificate\n");
579: goto end;
580: }
581: if (sign_certfile) {
582: sign_other = load_certs(bio_err, sign_certfile, FORMAT_PEM,
1.4 bcook 583: NULL, "signer certificates");
1.1 jsing 584: if (!sign_other)
585: goto end;
586: }
1.4 bcook 587: key = load_key(bio_err, keyfile, FORMAT_PEM, 0, NULL,
1.1 jsing 588: "signer private key");
589: if (!key)
590: goto end;
591:
592: if (!OCSP_request_sign(req, signer, key, NULL, sign_other, sign_flags)) {
593: BIO_printf(bio_err, "Error signing OCSP request\n");
594: goto end;
595: }
596: }
597: if (req_text && req)
598: OCSP_REQUEST_print(out, req, 0);
599:
600: if (reqout) {
601: derbio = BIO_new_file(reqout, "wb");
602: if (!derbio) {
603: BIO_printf(bio_err, "Error opening file %s\n", reqout);
604: goto end;
605: }
606: i2d_OCSP_REQUEST_bio(derbio, req);
607: BIO_free(derbio);
608: }
609: if (ridx_filename && (!rkey || !rsigner || !rca_cert)) {
610: BIO_printf(bio_err, "Need a responder certificate, key and CA for this operation!\n");
611: goto end;
612: }
613: if (ridx_filename && !rdb) {
614: rdb = load_index(ridx_filename, NULL);
615: if (!rdb)
616: goto end;
617: if (!index_index(rdb))
618: goto end;
619: }
620: if (rdb) {
621: i = make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey, rother, rflags, nmin, ndays);
622: if (cbio)
623: send_ocsp_response(cbio, resp);
624: } else if (host) {
625: resp = process_responder(bio_err, req, host, path,
626: port, use_ssl, headers, req_timeout);
627: if (!resp)
628: goto end;
629: } else if (respin) {
630: derbio = BIO_new_file(respin, "rb");
631: if (!derbio) {
632: BIO_printf(bio_err, "Error Opening OCSP response file\n");
633: goto end;
634: }
635: resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
636: BIO_free(derbio);
637: if (!resp) {
638: BIO_printf(bio_err, "Error reading OCSP response\n");
639: goto end;
640: }
641: } else {
642: ret = 0;
643: goto end;
644: }
645:
646: done_resp:
647:
648: if (respout) {
649: derbio = BIO_new_file(respout, "wb");
650: if (!derbio) {
651: BIO_printf(bio_err, "Error opening file %s\n", respout);
652: goto end;
653: }
654: i2d_OCSP_RESPONSE_bio(derbio, resp);
655: BIO_free(derbio);
656: }
657: i = OCSP_response_status(resp);
658:
659: if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
660: BIO_printf(out, "Responder Error: %s (%d)\n",
661: OCSP_response_status_str(i), i);
662: if (ignore_err)
663: goto redo_accept;
664: ret = 0;
665: goto end;
666: }
667: if (resp_text)
668: OCSP_RESPONSE_print(out, resp, 0);
669:
670: /* If running as responder don't verify our own response */
671: if (cbio) {
672: if (accept_count > 0)
673: accept_count--;
674: /* Redo if more connections needed */
675: if (accept_count) {
676: BIO_free_all(cbio);
677: cbio = NULL;
678: OCSP_REQUEST_free(req);
679: req = NULL;
680: OCSP_RESPONSE_free(resp);
681: resp = NULL;
682: goto redo_accept;
683: }
684: goto end;
685: }
686: if (!store)
687: store = setup_verify(bio_err, CAfile, CApath);
688: if (!store)
689: goto end;
690: if (verify_certfile) {
691: verify_other = load_certs(bio_err, verify_certfile, FORMAT_PEM,
1.4 bcook 692: NULL, "validator certificate");
1.1 jsing 693: if (!verify_other)
694: goto end;
695: }
696: bs = OCSP_response_get1_basic(resp);
697:
698: if (!bs) {
699: BIO_printf(bio_err, "Error parsing response\n");
700: goto end;
701: }
702: if (!noverify) {
703: if (req && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
704: if (i == -1)
705: BIO_printf(bio_err, "WARNING: no nonce in response\n");
706: else {
707: BIO_printf(bio_err, "Nonce Verify error\n");
708: goto end;
709: }
710: }
711: i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
712: if (i < 0)
713: i = OCSP_basic_verify(bs, NULL, store, 0);
714:
715: if (i <= 0) {
716: BIO_printf(bio_err, "Response Verify Failure\n");
717: ERR_print_errors(bio_err);
718: } else
719: BIO_printf(bio_err, "Response verify OK\n");
720:
721: }
722: if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
723: goto end;
724:
725: ret = 0;
726:
727: end:
728: ERR_print_errors(bio_err);
729: X509_free(signer);
730: X509_STORE_free(store);
731: EVP_PKEY_free(key);
732: EVP_PKEY_free(rkey);
733: X509_free(issuer);
734: X509_free(cert);
735: X509_free(rsigner);
736: X509_free(rca_cert);
737: free_index(rdb);
738: BIO_free_all(cbio);
739: BIO_free_all(acbio);
740: BIO_free(out);
741: OCSP_REQUEST_free(req);
742: OCSP_RESPONSE_free(resp);
743: OCSP_BASICRESP_free(bs);
744: sk_OPENSSL_STRING_free(reqnames);
745: sk_OCSP_CERTID_free(ids);
746: sk_X509_pop_free(sign_other, X509_free);
747: sk_X509_pop_free(verify_other, X509_free);
748: sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
749:
750: if (use_ssl != -1) {
751: free(host);
752: free(port);
753: free(path);
754: }
755: return (ret);
756: }
757:
758: static int
759: add_ocsp_cert(OCSP_REQUEST ** req, X509 * cert, const EVP_MD * cert_id_md, X509 * issuer,
760: STACK_OF(OCSP_CERTID) * ids)
761: {
762: OCSP_CERTID *id;
763: if (!issuer) {
764: BIO_printf(bio_err, "No issuer certificate specified\n");
765: return 0;
766: }
767: if (!*req)
768: *req = OCSP_REQUEST_new();
769: if (!*req)
770: goto err;
771: id = OCSP_cert_to_id(cert_id_md, cert, issuer);
772: if (!id || !sk_OCSP_CERTID_push(ids, id))
773: goto err;
774: if (!OCSP_request_add0_id(*req, id))
775: goto err;
776: return 1;
777:
778: err:
779: BIO_printf(bio_err, "Error Creating OCSP request\n");
780: return 0;
781: }
782:
783: static int
784: add_ocsp_serial(OCSP_REQUEST ** req, char *serial, const EVP_MD * cert_id_md, X509 * issuer,
785: STACK_OF(OCSP_CERTID) * ids)
786: {
787: OCSP_CERTID *id;
788: X509_NAME *iname;
789: ASN1_BIT_STRING *ikey;
790: ASN1_INTEGER *sno;
791: if (!issuer) {
792: BIO_printf(bio_err, "No issuer certificate specified\n");
793: return 0;
794: }
795: if (!*req)
796: *req = OCSP_REQUEST_new();
797: if (!*req)
798: goto err;
799: iname = X509_get_subject_name(issuer);
800: ikey = X509_get0_pubkey_bitstr(issuer);
801: sno = s2i_ASN1_INTEGER(NULL, serial);
802: if (!sno) {
803: BIO_printf(bio_err, "Error converting serial number %s\n", serial);
804: return 0;
805: }
806: id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
807: ASN1_INTEGER_free(sno);
808: if (!id || !sk_OCSP_CERTID_push(ids, id))
809: goto err;
810: if (!OCSP_request_add0_id(*req, id))
811: goto err;
812: return 1;
813:
814: err:
815: BIO_printf(bio_err, "Error Creating OCSP request\n");
816: return 0;
817: }
818:
819: static int
820: print_ocsp_summary(BIO * out, OCSP_BASICRESP * bs, OCSP_REQUEST * req,
821: STACK_OF(OPENSSL_STRING) * names,
822: STACK_OF(OCSP_CERTID) * ids, long nsec,
823: long maxage)
824: {
825: OCSP_CERTID *id;
826: char *name;
827: int i;
828:
829: int status, reason;
830:
831: ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
832:
833: if (!bs || !req || !sk_OPENSSL_STRING_num(names) || !sk_OCSP_CERTID_num(ids))
834: return 1;
835:
836: for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
837: id = sk_OCSP_CERTID_value(ids, i);
838: name = sk_OPENSSL_STRING_value(names, i);
839: BIO_printf(out, "%s: ", name);
840:
841: if (!OCSP_resp_find_status(bs, id, &status, &reason,
842: &rev, &thisupd, &nextupd)) {
843: BIO_puts(out, "ERROR: No Status found.\n");
844: continue;
845: }
846: /*
847: * Check validity: if invalid write to output BIO so we know
848: * which response this refers to.
849: */
850: if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
851: BIO_puts(out, "WARNING: Status times invalid.\n");
852: ERR_print_errors(out);
853: }
854: BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
855:
856: BIO_puts(out, "\tThis Update: ");
857: ASN1_GENERALIZEDTIME_print(out, thisupd);
858: BIO_puts(out, "\n");
859:
860: if (nextupd) {
861: BIO_puts(out, "\tNext Update: ");
862: ASN1_GENERALIZEDTIME_print(out, nextupd);
863: BIO_puts(out, "\n");
864: }
865: if (status != V_OCSP_CERTSTATUS_REVOKED)
866: continue;
867:
868: if (reason != -1)
869: BIO_printf(out, "\tReason: %s\n",
870: OCSP_crl_reason_str(reason));
871:
872: BIO_puts(out, "\tRevocation Time: ");
873: ASN1_GENERALIZEDTIME_print(out, rev);
874: BIO_puts(out, "\n");
875: }
876:
877: return 1;
878: }
879:
880:
881: static int
882: make_ocsp_response(OCSP_RESPONSE ** resp, OCSP_REQUEST * req, CA_DB * db,
883: X509 * ca, X509 * rcert, EVP_PKEY * rkey,
884: STACK_OF(X509) * rother, unsigned long flags,
885: int nmin, int ndays)
886: {
887: ASN1_TIME *thisupd = NULL, *nextupd = NULL;
888: OCSP_CERTID *cid, *ca_id = NULL;
889: OCSP_BASICRESP *bs = NULL;
890: int i, id_count, ret = 1;
891:
892: id_count = OCSP_request_onereq_count(req);
893:
894: if (id_count <= 0) {
895: *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
896: goto end;
897: }
898: bs = OCSP_BASICRESP_new();
899: thisupd = X509_gmtime_adj(NULL, 0);
900: if (ndays != -1)
901: nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24);
902:
903: /* Examine each certificate id in the request */
904: for (i = 0; i < id_count; i++) {
905: OCSP_ONEREQ *one;
906: ASN1_INTEGER *serial;
907: char **inf;
908: ASN1_OBJECT *cert_id_md_oid;
909: const EVP_MD *cert_id_md;
910: one = OCSP_request_onereq_get0(req, i);
911: cid = OCSP_onereq_get0_id(one);
912:
913: OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
914:
915: cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
916: if (!cert_id_md) {
917: *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
918: NULL);
919: goto end;
920: }
921: if (ca_id)
922: OCSP_CERTID_free(ca_id);
923: ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
924:
925: /* Is this request about our CA? */
926: if (OCSP_id_issuer_cmp(ca_id, cid)) {
927: OCSP_basic_add1_status(bs, cid,
928: V_OCSP_CERTSTATUS_UNKNOWN,
929: 0, NULL,
930: thisupd, nextupd);
931: continue;
932: }
933: OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
934: inf = lookup_serial(db, serial);
935: if (!inf)
936: OCSP_basic_add1_status(bs, cid,
937: V_OCSP_CERTSTATUS_UNKNOWN,
938: 0, NULL,
939: thisupd, nextupd);
940: else if (inf[DB_type][0] == DB_TYPE_VAL)
941: OCSP_basic_add1_status(bs, cid,
942: V_OCSP_CERTSTATUS_GOOD,
943: 0, NULL,
944: thisupd, nextupd);
945: else if (inf[DB_type][0] == DB_TYPE_REV) {
946: ASN1_OBJECT *inst = NULL;
947: ASN1_TIME *revtm = NULL;
948: ASN1_GENERALIZEDTIME *invtm = NULL;
949: OCSP_SINGLERESP *single;
950: int reason = -1;
951: unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
952: single = OCSP_basic_add1_status(bs, cid,
953: V_OCSP_CERTSTATUS_REVOKED,
954: reason, revtm,
955: thisupd, nextupd);
956: if (invtm)
957: OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, invtm, 0, 0);
958: else if (inst)
959: OCSP_SINGLERESP_add1_ext_i2d(single, NID_hold_instruction_code, inst, 0, 0);
960: ASN1_OBJECT_free(inst);
961: ASN1_TIME_free(revtm);
962: ASN1_GENERALIZEDTIME_free(invtm);
963: }
964: }
965:
966: OCSP_copy_nonce(bs, req);
967:
968: OCSP_basic_sign(bs, rcert, rkey, NULL, rother, flags);
969:
970: *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
971:
972: end:
973: ASN1_TIME_free(thisupd);
974: ASN1_TIME_free(nextupd);
975: OCSP_CERTID_free(ca_id);
976: OCSP_BASICRESP_free(bs);
977: return ret;
978:
979: }
980:
981: static char **
982: lookup_serial(CA_DB * db, ASN1_INTEGER * ser)
983: {
984: int i;
985: BIGNUM *bn = NULL;
986: char *itmp, *row[DB_NUMBER], **rrow;
987: for (i = 0; i < DB_NUMBER; i++)
988: row[i] = NULL;
989: bn = ASN1_INTEGER_to_BN(ser, NULL);
990: OPENSSL_assert(bn); /* FIXME: should report an error at this
991: * point and abort */
992: if (BN_is_zero(bn))
993: itmp = strdup("00");
994: else
995: itmp = BN_bn2hex(bn);
996: row[DB_serial] = itmp;
997: BN_free(bn);
998: rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
999: free(itmp);
1000: return rrow;
1001: }
1002:
1003: /* Quick and dirty OCSP server: read in and parse input request */
1004:
1005: static BIO *
1006: init_responder(char *port)
1007: {
1008: BIO *acbio = NULL, *bufbio = NULL;
1009: bufbio = BIO_new(BIO_f_buffer());
1010: if (!bufbio)
1011: goto err;
1012: acbio = BIO_new_accept(port);
1013: if (!acbio)
1014: goto err;
1015: BIO_set_accept_bios(acbio, bufbio);
1016: bufbio = NULL;
1017:
1018: if (BIO_do_accept(acbio) <= 0) {
1019: BIO_printf(bio_err, "Error setting up accept BIO\n");
1020: ERR_print_errors(bio_err);
1021: goto err;
1022: }
1023: return acbio;
1024:
1025: err:
1026: BIO_free_all(acbio);
1027: BIO_free(bufbio);
1028: return NULL;
1029: }
1030:
1031: static int
1032: do_responder(OCSP_REQUEST ** preq, BIO ** pcbio, BIO * acbio, char *port)
1033: {
1034: int have_post = 0, len;
1035: OCSP_REQUEST *req = NULL;
1036: char inbuf[1024];
1037: BIO *cbio = NULL;
1038:
1039: if (BIO_do_accept(acbio) <= 0) {
1040: BIO_printf(bio_err, "Error accepting connection\n");
1041: ERR_print_errors(bio_err);
1042: return 0;
1043: }
1044: cbio = BIO_pop(acbio);
1045: *pcbio = cbio;
1046:
1047: for (;;) {
1048: len = BIO_gets(cbio, inbuf, sizeof inbuf);
1049: if (len <= 0)
1050: return 1;
1051: /* Look for "POST" signalling start of query */
1052: if (!have_post) {
1053: if (strncmp(inbuf, "POST", 4)) {
1054: BIO_printf(bio_err, "Invalid request\n");
1055: return 1;
1056: }
1057: have_post = 1;
1058: }
1059: /* Look for end of headers */
1060: if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
1061: break;
1062: }
1063:
1064: /* Try to read OCSP request */
1065:
1066: req = d2i_OCSP_REQUEST_bio(cbio, NULL);
1067:
1068: if (!req) {
1069: BIO_printf(bio_err, "Error parsing OCSP request\n");
1070: ERR_print_errors(bio_err);
1071: }
1072: *preq = req;
1073:
1074: return 1;
1075:
1076: }
1077:
1078: static int
1079: send_ocsp_response(BIO * cbio, OCSP_RESPONSE * resp)
1080: {
1081: static const char http_resp[] =
1082: "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
1083: "Content-Length: %d\r\n\r\n";
1084: if (!cbio)
1085: return 0;
1086: BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
1087: i2d_OCSP_RESPONSE_bio(cbio, resp);
1088: (void) BIO_flush(cbio);
1089: return 1;
1090: }
1091:
1092: static OCSP_RESPONSE *
1093: query_responder(BIO * err, BIO * cbio, char *path,
1094: STACK_OF(CONF_VALUE) * headers,
1095: OCSP_REQUEST * req, int req_timeout)
1096: {
1097: int fd;
1098: int rv;
1099: int i;
1100: OCSP_REQ_CTX *ctx = NULL;
1101: OCSP_RESPONSE *rsp = NULL;
1.2 deraadt 1102: struct pollfd pfd[1];
1.1 jsing 1103:
1104: if (req_timeout != -1)
1105: BIO_set_nbio(cbio, 1);
1106:
1107: rv = BIO_do_connect(cbio);
1108:
1109: if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
1110: BIO_puts(err, "Error connecting BIO\n");
1111: return NULL;
1112: }
1.5 ! deraadt 1113: if (BIO_get_fd(cbio, &fd) < 0) {
1.1 jsing 1114: BIO_puts(err, "Can't get connection fd\n");
1115: goto err;
1116: }
1117: if (req_timeout != -1 && rv <= 0) {
1.2 deraadt 1118: pfd[0].fd = fd;
1119: pfd[0].events = POLLOUT;
1120: rv = poll(pfd, 1, req_timeout * 1000);
1.1 jsing 1121: if (rv == 0) {
1122: BIO_puts(err, "Timeout on connect\n");
1123: return NULL;
1124: }
1.2 deraadt 1125: if (rv == -1) {
1126: BIO_puts(err, "Poll error\n");
1127: return NULL;
1128: }
1.1 jsing 1129: }
1130: ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
1131: if (!ctx)
1132: return NULL;
1133:
1134: for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
1135: CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
1136: if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
1137: goto err;
1138: }
1139:
1140: if (!OCSP_REQ_CTX_set1_req(ctx, req))
1141: goto err;
1142:
1143: for (;;) {
1144: rv = OCSP_sendreq_nbio(&rsp, ctx);
1145: if (rv != -1)
1146: break;
1147: if (req_timeout == -1)
1148: continue;
1.2 deraadt 1149: pfd[0].fd = fd;
1.1 jsing 1150: if (BIO_should_read(cbio))
1.2 deraadt 1151: pfd[0].events = POLLIN;
1.1 jsing 1152: else if (BIO_should_write(cbio))
1.2 deraadt 1153: pfd[0].events = POLLOUT;
1.1 jsing 1154: else {
1155: BIO_puts(err, "Unexpected retry condition\n");
1156: goto err;
1157: }
1.2 deraadt 1158: rv = poll(pfd, 1, req_timeout * 1000);
1.1 jsing 1159: if (rv == 0) {
1160: BIO_puts(err, "Timeout on request\n");
1161: break;
1162: }
1.2 deraadt 1163: if (rv == -1 || (pfd[0].revents & (POLLERR|POLLNVAL))) {
1164: BIO_puts(err, "Poll error\n");
1.1 jsing 1165: break;
1166: }
1167: }
1168: err:
1169: if (ctx)
1170: OCSP_REQ_CTX_free(ctx);
1171:
1172: return rsp;
1173: }
1174:
1175: OCSP_RESPONSE *
1176: process_responder(BIO * err, OCSP_REQUEST * req,
1177: char *host, char *path, char *port, int use_ssl,
1178: STACK_OF(CONF_VALUE) * headers,
1179: int req_timeout)
1180: {
1181: BIO *cbio = NULL;
1182: SSL_CTX *ctx = NULL;
1183: OCSP_RESPONSE *resp = NULL;
1184: cbio = BIO_new_connect(host);
1185: if (!cbio) {
1186: BIO_printf(err, "Error creating connect BIO\n");
1187: goto end;
1188: }
1189: if (port)
1190: BIO_set_conn_port(cbio, port);
1191: if (use_ssl == 1) {
1192: BIO *sbio;
1193: ctx = SSL_CTX_new(SSLv23_client_method());
1194: if (ctx == NULL) {
1195: BIO_printf(err, "Error creating SSL context.\n");
1196: goto end;
1197: }
1198: SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
1199: sbio = BIO_new_ssl(ctx, 1);
1200: cbio = BIO_push(sbio, cbio);
1201: }
1202: resp = query_responder(err, cbio, path, headers, req, req_timeout);
1203: if (!resp)
1204: BIO_printf(bio_err, "Error querying OCSP responder\n");
1205: end:
1206: if (cbio)
1207: BIO_free_all(cbio);
1208: if (ctx)
1209: SSL_CTX_free(ctx);
1210: return resp;
1211: }
1212:
1213: #endif