2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 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).
59 #include <openssl/asn1.h>
64 #include <openssl/ocsp.h>
65 #include <openssl/err.h>
66 #include <openssl/buffer.h>
67 #ifdef OPENSSL_SYS_SUNOS
68 #define strtoul (unsigned long)strtol
69 #endif /* OPENSSL_SYS_SUNOS */
71 /* Stateful OCSP request code, supporting non-blocking I/O */
73 /* Opaque OCSP request status structure */
75 struct ocsp_req_ctx_st {
76 int state; /* Current I/O state */
77 unsigned char *iobuf; /* Line buffer */
78 int iobuflen; /* Line buffer length */
79 BIO *io; /* BIO to perform I/O with */
80 BIO *mem; /* Memory BIO response is built into */
81 unsigned long asn1_len; /* ASN1 length of response */
84 #define OCSP_MAX_REQUEST_LENGTH (100 * 1024)
85 #define OCSP_MAX_LINE_LEN 4096;
89 /* If set no reading should be performed */
90 #define OHS_NOREAD 0x1000
92 #define OHS_ERROR (0 | OHS_NOREAD)
93 /* First line being read */
94 #define OHS_FIRSTLINE 1
95 /* MIME headers being read */
97 /* OCSP initial header (tag + length) being read */
98 #define OHS_ASN1_HEADER 3
99 /* OCSP content octets being read */
100 #define OHS_ASN1_CONTENT 4
101 /* Request being sent */
102 #define OHS_ASN1_WRITE (6 | OHS_NOREAD)
103 /* Request being flushed */
104 #define OHS_ASN1_FLUSH (7 | OHS_NOREAD)
106 #define OHS_DONE (8 | OHS_NOREAD)
109 static int parse_http_line1(char *line);
111 void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx)
116 OPENSSL_free(rctx->iobuf);
120 OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, char *path, OCSP_REQUEST *req,
123 static char post_hdr[] = "POST %s HTTP/1.0\r\n"
124 "Content-Type: application/ocsp-request\r\n"
125 "Content-Length: %d\r\n\r\n";
128 rctx = OPENSSL_malloc(sizeof(OCSP_REQ_CTX));
129 rctx->state = OHS_FIRSTLINE;
130 rctx->mem = BIO_new(BIO_s_mem());
133 rctx->iobuflen = maxline;
135 rctx->iobuflen = OCSP_MAX_LINE_LEN;
136 rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
140 if (BIO_printf(rctx->mem, post_hdr, path,
141 i2d_OCSP_REQUEST(req, NULL)) <= 0)
143 rctx->state = OHS_ERROR;
146 if (i2d_OCSP_REQUEST_bio(rctx->mem, req) <= 0)
148 rctx->state = OHS_ERROR;
151 rctx->state = OHS_ASN1_WRITE;
152 rctx->asn1_len = BIO_get_mem_data(rctx->mem, NULL);
157 /* Parse the HTTP response. This will look like this:
158 * "HTTP/1.0 200 OK". We need to obtain the numeric code and
159 * (optional) informational message.
162 static int parse_http_line1(char *line)
166 /* Skip to first white space (passed protocol info) */
168 for(p = line; *p && !isspace((unsigned char)*p); p++)
172 OCSPerr(OCSP_F_OCSP_SENDREQ_NBIO,
173 OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
177 /* Skip past white space to start of response code */
178 while(*p && isspace((unsigned char)*p))
183 OCSPerr(OCSP_F_OCSP_SENDREQ_NBIO,
184 OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
188 /* Find end of response code: first whitespace after start of code */
189 for(q = p; *q && !isspace((unsigned char)*q); q++)
194 OCSPerr(OCSP_F_OCSP_SENDREQ_NBIO,
195 OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
199 /* Set end of response code and start of message */
202 /* Attempt to parse numeric code */
203 retcode = strtoul(p, &r, 10);
208 /* Skip over any leading white space in message */
209 while(*q && isspace((unsigned char)*q))
214 /* Finally zap any trailing white space in message (include
217 /* We know q has a non white space character so this is OK */
218 for(r = q + strlen(q) - 1; isspace((unsigned char)*r); r--)
223 OCSPerr(OCSP_F_OCSP_SENDREQ_NBIO, OCSP_R_SERVER_RESPONSE_ERROR);
225 ERR_add_error_data(2, "Code=", p);
227 ERR_add_error_data(4, "Code=", p, ",Reason=", q);
236 int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx)
239 const unsigned char *p;
242 if (!(rctx->state & OHS_NOREAD))
244 n = BIO_read(rctx->io, rctx->iobuf, rctx->iobuflen);
248 if (BIO_should_retry(rctx->io))
253 /* Write data to memory BIO */
255 if (BIO_write(rctx->mem, rctx->iobuf, n) != n)
263 n = BIO_get_mem_data(rctx->mem, &p);
265 i = BIO_write(rctx->io,
266 p + (n - rctx->asn1_len), rctx->asn1_len);
270 if (BIO_should_retry(rctx->io))
272 rctx->state = OHS_ERROR;
278 if (rctx->asn1_len > 0)
281 rctx->state = OHS_ASN1_FLUSH;
283 BIO_reset(rctx->mem);
287 i = BIO_flush(rctx->io);
291 rctx->state = OHS_FIRSTLINE;
295 if (BIO_should_retry(rctx->io))
298 rctx->state = OHS_ERROR;
307 /* Attempt to read a line in */
310 /* Due to &%^*$" memory BIO behaviour with BIO_gets we
311 * have to check there's a complete line in there before
312 * calling BIO_gets or we'll just get a partial read.
314 n = BIO_get_mem_data(rctx->mem, &p);
315 if ((n <= 0) || !memchr(p, '\n', n))
317 if (n >= rctx->iobuflen)
319 rctx->state = OHS_ERROR;
324 n = BIO_gets(rctx->mem, (char *)rctx->iobuf, rctx->iobuflen);
328 if (BIO_should_retry(rctx->mem))
330 rctx->state = OHS_ERROR;
334 /* Don't allow excessive lines */
335 if (n == rctx->iobuflen)
337 rctx->state = OHS_ERROR;
342 if (rctx->state == OHS_FIRSTLINE)
344 if (parse_http_line1((char *)rctx->iobuf))
346 rctx->state = OHS_HEADERS;
351 rctx->state = OHS_ERROR;
357 /* Look for blank line: end of headers */
358 for (p = rctx->iobuf; *p; p++)
360 if ((*p != '\r') && (*p != '\n'))
366 rctx->state = OHS_ASN1_HEADER;
373 case OHS_ASN1_HEADER:
374 /* Now reading ASN1 header: can read at least 6 bytes which
375 * is more than enough for any valid ASN1 SEQUENCE header
377 n = BIO_get_mem_data(rctx->mem, &p);
381 /* Check it is an ASN1 SEQUENCE */
382 if (*p++ != (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED))
384 rctx->state = OHS_ERROR;
388 /* Check out length field */
392 /* Not NDEF or excessive length */
395 rctx->state = OHS_ERROR;
400 for (i = 0; i < n; i++)
402 rctx->asn1_len <<= 8;
403 rctx->asn1_len |= *p++;
406 if (rctx->asn1_len > OCSP_MAX_REQUEST_LENGTH)
408 rctx->state = OHS_ERROR;
412 rctx->asn1_len += n + 2;
415 rctx->asn1_len = *p + 2;
417 rctx->state = OHS_ASN1_CONTENT;
421 case OHS_ASN1_CONTENT:
422 n = BIO_get_mem_data(rctx->mem, &p);
423 if (n < rctx->asn1_len)
427 *presp = d2i_OCSP_RESPONSE(NULL, &p, rctx->asn1_len);
430 rctx->state = OHS_DONE;
434 rctx->state = OHS_ERROR;
451 /* Blocking OCSP request handler: now a special case of non-blocking I/O */
453 OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, char *path, OCSP_REQUEST *req)
455 OCSP_RESPONSE *resp = NULL;
459 ctx = OCSP_sendreq_new(b, path, req, -1);
463 rv = OCSP_sendreq_nbio(&resp, ctx);
464 } while ((rv == -1) && BIO_should_retry(b));
466 OCSP_REQ_CTX_free(ctx);