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