2 /* Written by Dr Stephen N Henson (steve@openssl.org) 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).
64 #include <openssl/asn1.h>
65 #include <openssl/ocsp.h>
66 #include <openssl/err.h>
67 #include <openssl/buffer.h>
69 /* Stateful OCSP request code, supporting non-blocking I/O */
71 /* Opaque OCSP request status structure */
73 struct ocsp_req_ctx_st {
74 int state; /* Current I/O state */
75 unsigned char *iobuf; /* Line buffer */
76 int iobuflen; /* Line buffer length */
77 BIO *io; /* BIO to perform I/O with */
78 BIO *mem; /* Memory BIO response is built into */
79 unsigned long asn1_len; /* ASN1 length of response */
80 unsigned long max_resp_len; /* Maximum length of response */
83 #define OCSP_MAX_RESP_LENGTH (100 * 1024)
84 #define OCSP_MAX_LINE_LEN 4096;
88 /* If set no reading should be performed */
89 #define OHS_NOREAD 0x1000
91 #define OHS_ERROR (0 | OHS_NOREAD)
92 /* First line being read */
93 #define OHS_FIRSTLINE 1
94 /* MIME headers being read */
96 /* OCSP initial header (tag + length) being read */
97 #define OHS_ASN1_HEADER 3
98 /* OCSP content octets being read */
99 #define OHS_ASN1_CONTENT 4
100 /* First call: ready to start I/O */
101 #define OHS_ASN1_WRITE_INIT (5 | OHS_NOREAD)
102 /* Request being sent */
103 #define OHS_ASN1_WRITE (6 | OHS_NOREAD)
104 /* Request being flushed */
105 #define OHS_ASN1_FLUSH (7 | OHS_NOREAD)
107 #define OHS_DONE (8 | OHS_NOREAD)
108 /* Headers set, no final \r\n included */
109 #define OHS_HTTP_HEADER (9 | OHS_NOREAD)
112 static int parse_http_line1(char *line);
114 OCSP_REQ_CTX *OCSP_REQ_CTX_new(BIO *io, int maxline)
117 rctx = OPENSSL_malloc(sizeof(OCSP_REQ_CTX));
120 rctx->state = OHS_ERROR;
121 rctx->max_resp_len = OCSP_MAX_RESP_LENGTH;
122 rctx->mem = BIO_new(BIO_s_mem());
126 rctx->iobuflen = maxline;
128 rctx->iobuflen = OCSP_MAX_LINE_LEN;
129 rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
130 if (!rctx->iobuf || !rctx->mem)
132 OCSP_REQ_CTX_free(rctx);
138 void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx)
143 OPENSSL_free(rctx->iobuf);
147 BIO *OCSP_REQ_CTX_get0_mem_bio(OCSP_REQ_CTX *rctx)
152 void OCSP_set_max_response_length(OCSP_REQ_CTX *rctx, unsigned long len)
155 rctx->max_resp_len = OCSP_MAX_RESP_LENGTH;
157 rctx->max_resp_len = len;
160 int OCSP_REQ_CTX_i2d(OCSP_REQ_CTX *rctx, const ASN1_ITEM *it, ASN1_VALUE *val)
162 static const char req_hdr[] =
163 "Content-Type: application/ocsp-request\r\n"
164 "Content-Length: %d\r\n\r\n";
165 int reqlen = ASN1_item_i2d(val, NULL, it);
166 if (BIO_printf(rctx->mem, req_hdr, reqlen) <= 0)
168 if (ASN1_item_i2d_bio(it, rctx->mem, val) <= 0)
170 rctx->state = OHS_ASN1_WRITE_INIT;
174 int OCSP_REQ_CTX_nbio_d2i(OCSP_REQ_CTX *rctx,
175 ASN1_VALUE **pval, const ASN1_ITEM *it)
178 const unsigned char *p;
180 rv = OCSP_REQ_CTX_nbio(rctx);
184 len = BIO_get_mem_data(rctx->mem, &p);
185 *pval = ASN1_item_d2i(NULL, &p, len, it);
188 rctx->state = OHS_ERROR;
194 int OCSP_REQ_CTX_http(OCSP_REQ_CTX *rctx, const char *op, const char *path)
196 static const char http_hdr[] = "%s %s HTTP/1.0\r\n";
201 if (BIO_printf(rctx->mem, http_hdr, op, path) <= 0)
203 rctx->state = OHS_HTTP_HEADER;
207 int OCSP_REQ_CTX_set1_req(OCSP_REQ_CTX *rctx, OCSP_REQUEST *req)
209 return OCSP_REQ_CTX_i2d(rctx, ASN1_ITEM_rptr(OCSP_REQUEST),
213 int OCSP_REQ_CTX_add1_header(OCSP_REQ_CTX *rctx,
214 const char *name, const char *value)
218 if (BIO_puts(rctx->mem, name) <= 0)
222 if (BIO_write(rctx->mem, ": ", 2) != 2)
224 if (BIO_puts(rctx->mem, value) <= 0)
227 if (BIO_write(rctx->mem, "\r\n", 2) != 2)
229 rctx->state = OHS_HTTP_HEADER;
233 OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path, OCSP_REQUEST *req,
237 OCSP_REQ_CTX *rctx = NULL;
238 rctx = OCSP_REQ_CTX_new(io, maxline);
242 if (!OCSP_REQ_CTX_http(rctx, "POST", path))
245 if (req && !OCSP_REQ_CTX_set1_req(rctx, req))
251 OCSP_REQ_CTX_free(rctx);
255 /* Parse the HTTP response. This will look like this:
256 * "HTTP/1.0 200 OK". We need to obtain the numeric code and
257 * (optional) informational message.
260 static int parse_http_line1(char *line)
264 /* Skip to first white space (passed protocol info) */
266 for(p = line; *p && !isspace((unsigned char)*p); p++)
270 OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
271 OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
275 /* Skip past white space to start of response code */
276 while(*p && isspace((unsigned char)*p))
281 OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
282 OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
286 /* Find end of response code: first whitespace after start of code */
287 for(q = p; *q && !isspace((unsigned char)*q); q++)
292 OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
293 OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
297 /* Set end of response code and start of message */
300 /* Attempt to parse numeric code */
301 retcode = strtoul(p, &r, 10);
306 /* Skip over any leading white space in message */
307 while(*q && isspace((unsigned char)*q))
312 /* Finally zap any trailing white space in message (include
315 /* We know q has a non white space character so this is OK */
316 for(r = q + strlen(q) - 1; isspace((unsigned char)*r); r--)
321 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_ERROR);
323 ERR_add_error_data(2, "Code=", p);
325 ERR_add_error_data(4, "Code=", p, ",Reason=", q);
334 int OCSP_REQ_CTX_nbio(OCSP_REQ_CTX *rctx)
337 const unsigned char *p;
339 if (!(rctx->state & OHS_NOREAD))
341 n = BIO_read(rctx->io, rctx->iobuf, rctx->iobuflen);
345 if (BIO_should_retry(rctx->io))
350 /* Write data to memory BIO */
352 if (BIO_write(rctx->mem, rctx->iobuf, n) != n)
358 case OHS_HTTP_HEADER:
359 /* Last operation was adding headers: need a final \r\n */
360 if (BIO_write(rctx->mem, "\r\n", 2) != 2)
362 rctx->state = OHS_ERROR;
365 rctx->state = OHS_ASN1_WRITE_INIT;
367 case OHS_ASN1_WRITE_INIT:
368 rctx->asn1_len = BIO_get_mem_data(rctx->mem, NULL);
369 rctx->state = OHS_ASN1_WRITE;
372 n = BIO_get_mem_data(rctx->mem, &p);
374 i = BIO_write(rctx->io,
375 p + (n - rctx->asn1_len), rctx->asn1_len);
379 if (BIO_should_retry(rctx->io))
381 rctx->state = OHS_ERROR;
387 if (rctx->asn1_len > 0)
390 rctx->state = OHS_ASN1_FLUSH;
392 (void)BIO_reset(rctx->mem);
396 i = BIO_flush(rctx->io);
400 rctx->state = OHS_FIRSTLINE;
404 if (BIO_should_retry(rctx->io))
407 rctx->state = OHS_ERROR;
416 /* Attempt to read a line in */
419 /* Due to &%^*$" memory BIO behaviour with BIO_gets we
420 * have to check there's a complete line in there before
421 * calling BIO_gets or we'll just get a partial read.
423 n = BIO_get_mem_data(rctx->mem, &p);
424 if ((n <= 0) || !memchr(p, '\n', n))
426 if (n >= rctx->iobuflen)
428 rctx->state = OHS_ERROR;
433 n = BIO_gets(rctx->mem, (char *)rctx->iobuf, rctx->iobuflen);
437 if (BIO_should_retry(rctx->mem))
439 rctx->state = OHS_ERROR;
443 /* Don't allow excessive lines */
444 if (n == rctx->iobuflen)
446 rctx->state = OHS_ERROR;
451 if (rctx->state == OHS_FIRSTLINE)
453 if (parse_http_line1((char *)rctx->iobuf))
455 rctx->state = OHS_HEADERS;
460 rctx->state = OHS_ERROR;
466 /* Look for blank line: end of headers */
467 for (p = rctx->iobuf; *p; p++)
469 if ((*p != '\r') && (*p != '\n'))
475 rctx->state = OHS_ASN1_HEADER;
482 case OHS_ASN1_HEADER:
483 /* Now reading ASN1 header: can read at least 2 bytes which
484 * is enough for ASN1 SEQUENCE header and either length field
485 * or at least the length of the length field.
487 n = BIO_get_mem_data(rctx->mem, &p);
491 /* Check it is an ASN1 SEQUENCE */
492 if (*p++ != (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED))
494 rctx->state = OHS_ERROR;
498 /* Check out length field */
501 /* If MSB set on initial length octet we can now
502 * always read 6 octets: make sure we have them.
507 /* Not NDEF or excessive length */
510 rctx->state = OHS_ERROR;
515 for (i = 0; i < n; i++)
517 rctx->asn1_len <<= 8;
518 rctx->asn1_len |= *p++;
521 if (rctx->asn1_len > rctx->max_resp_len)
523 rctx->state = OHS_ERROR;
527 rctx->asn1_len += n + 2;
530 rctx->asn1_len = *p + 2;
532 rctx->state = OHS_ASN1_CONTENT;
536 case OHS_ASN1_CONTENT:
537 n = BIO_get_mem_data(rctx->mem, NULL);
538 if (n < (int)rctx->asn1_len)
541 rctx->state = OHS_DONE;
555 int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx)
557 return OCSP_REQ_CTX_nbio_d2i(rctx,
558 (ASN1_VALUE **)presp, ASN1_ITEM_rptr(OCSP_RESPONSE));
561 /* Blocking OCSP request handler: now a special case of non-blocking I/O */
563 OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req)
565 OCSP_RESPONSE *resp = NULL;
569 ctx = OCSP_sendreq_new(b, path, req, -1);
576 rv = OCSP_sendreq_nbio(&resp, ctx);
577 } while ((rv == -1) && BIO_should_retry(b));
579 OCSP_REQ_CTX_free(ctx);