3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
65 #include <openssl/asn1.h>
66 #include <openssl/ocsp.h>
67 #include <openssl/err.h>
68 #include <openssl/buffer.h>
70 /* Stateful OCSP request code, supporting non-blocking I/O */
72 /* Opaque OCSP request status structure */
74 struct ocsp_req_ctx_st
{
75 int state
; /* Current I/O state */
76 unsigned char *iobuf
; /* Line buffer */
77 int iobuflen
; /* Line buffer length */
78 BIO
*io
; /* BIO to perform I/O with */
79 BIO
*mem
; /* Memory BIO response is built into */
80 unsigned long asn1_len
; /* ASN1 length of response */
81 unsigned long max_resp_len
; /* Maximum length of response */
84 #define OCSP_MAX_RESP_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 /* First call: ready to start I/O */
102 #define OHS_ASN1_WRITE_INIT (5 | OHS_NOREAD)
103 /* Request being sent */
104 #define OHS_ASN1_WRITE (6 | OHS_NOREAD)
105 /* Request being flushed */
106 #define OHS_ASN1_FLUSH (7 | OHS_NOREAD)
108 #define OHS_DONE (8 | OHS_NOREAD)
109 /* Headers set, no final \r\n included */
110 #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
)
116 OCSP_REQ_CTX
*rctx
= OPENSSL_malloc(sizeof(*rctx
));
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
) {
131 OCSP_REQ_CTX_free(rctx
);
137 void OCSP_REQ_CTX_free(OCSP_REQ_CTX
*rctx
)
142 OPENSSL_free(rctx
->iobuf
);
146 BIO
*OCSP_REQ_CTX_get0_mem_bio(OCSP_REQ_CTX
*rctx
)
151 void OCSP_set_max_response_length(OCSP_REQ_CTX
*rctx
, unsigned long len
)
154 rctx
->max_resp_len
= OCSP_MAX_RESP_LENGTH
;
156 rctx
->max_resp_len
= len
;
159 int OCSP_REQ_CTX_i2d(OCSP_REQ_CTX
*rctx
, const ASN1_ITEM
*it
, ASN1_VALUE
*val
)
161 static const char req_hdr
[] =
162 "Content-Type: application/ocsp-request\r\n"
163 "Content-Length: %d\r\n\r\n";
164 int reqlen
= ASN1_item_i2d(val
, NULL
, it
);
165 if (BIO_printf(rctx
->mem
, req_hdr
, reqlen
) <= 0)
167 if (ASN1_item_i2d_bio(it
, rctx
->mem
, val
) <= 0)
169 rctx
->state
= OHS_ASN1_WRITE_INIT
;
173 int OCSP_REQ_CTX_nbio_d2i(OCSP_REQ_CTX
*rctx
,
174 ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
177 const unsigned char *p
;
179 rv
= OCSP_REQ_CTX_nbio(rctx
);
183 len
= BIO_get_mem_data(rctx
->mem
, &p
);
184 *pval
= ASN1_item_d2i(NULL
, &p
, len
, it
);
186 rctx
->state
= OHS_ERROR
;
192 int OCSP_REQ_CTX_http(OCSP_REQ_CTX
*rctx
, const char *op
, const char *path
)
194 static const char http_hdr
[] = "%s %s HTTP/1.0\r\n";
199 if (BIO_printf(rctx
->mem
, http_hdr
, op
, path
) <= 0)
201 rctx
->state
= OHS_HTTP_HEADER
;
205 int OCSP_REQ_CTX_set1_req(OCSP_REQ_CTX
*rctx
, OCSP_REQUEST
*req
)
207 return OCSP_REQ_CTX_i2d(rctx
, ASN1_ITEM_rptr(OCSP_REQUEST
),
211 int OCSP_REQ_CTX_add1_header(OCSP_REQ_CTX
*rctx
,
212 const char *name
, const char *value
)
216 if (BIO_puts(rctx
->mem
, name
) <= 0)
219 if (BIO_write(rctx
->mem
, ": ", 2) != 2)
221 if (BIO_puts(rctx
->mem
, value
) <= 0)
224 if (BIO_write(rctx
->mem
, "\r\n", 2) != 2)
226 rctx
->state
= OHS_HTTP_HEADER
;
230 OCSP_REQ_CTX
*OCSP_sendreq_new(BIO
*io
, const char *path
, OCSP_REQUEST
*req
,
234 OCSP_REQ_CTX
*rctx
= NULL
;
235 rctx
= OCSP_REQ_CTX_new(io
, maxline
);
239 if (!OCSP_REQ_CTX_http(rctx
, "POST", path
))
242 if (req
&& !OCSP_REQ_CTX_set1_req(rctx
, req
))
248 OCSP_REQ_CTX_free(rctx
);
253 * Parse the HTTP response. This will look like this: "HTTP/1.0 200 OK". We
254 * need to obtain the numeric code and (optional) informational message.
257 static int parse_http_line1(char *line
)
261 /* Skip to first white space (passed protocol info) */
263 for (p
= line
; *p
&& !isspace((unsigned char)*p
); p
++)
266 OCSPerr(OCSP_F_PARSE_HTTP_LINE1
, OCSP_R_SERVER_RESPONSE_PARSE_ERROR
);
270 /* Skip past white space to start of response code */
271 while (*p
&& isspace((unsigned char)*p
))
275 OCSPerr(OCSP_F_PARSE_HTTP_LINE1
, OCSP_R_SERVER_RESPONSE_PARSE_ERROR
);
279 /* Find end of response code: first whitespace after start of code */
280 for (q
= p
; *q
&& !isspace((unsigned char)*q
); q
++)
284 OCSPerr(OCSP_F_PARSE_HTTP_LINE1
, OCSP_R_SERVER_RESPONSE_PARSE_ERROR
);
288 /* Set end of response code and start of message */
291 /* Attempt to parse numeric code */
292 retcode
= strtoul(p
, &r
, 10);
297 /* Skip over any leading white space in message */
298 while (*q
&& isspace((unsigned char)*q
))
303 * Finally zap any trailing white space in message (include CRLF)
306 /* We know q has a non white space character so this is OK */
307 for (r
= q
+ strlen(q
) - 1; isspace((unsigned char)*r
); r
--)
310 if (retcode
!= 200) {
311 OCSPerr(OCSP_F_PARSE_HTTP_LINE1
, OCSP_R_SERVER_RESPONSE_ERROR
);
313 ERR_add_error_data(2, "Code=", p
);
315 ERR_add_error_data(4, "Code=", p
, ",Reason=", q
);
323 int OCSP_REQ_CTX_nbio(OCSP_REQ_CTX
*rctx
)
326 const unsigned char *p
;
328 if (!(rctx
->state
& OHS_NOREAD
)) {
329 n
= BIO_read(rctx
->io
, rctx
->iobuf
, rctx
->iobuflen
);
332 if (BIO_should_retry(rctx
->io
))
337 /* Write data to memory BIO */
339 if (BIO_write(rctx
->mem
, rctx
->iobuf
, n
) != n
)
343 switch (rctx
->state
) {
344 case OHS_HTTP_HEADER
:
345 /* Last operation was adding headers: need a final \r\n */
346 if (BIO_write(rctx
->mem
, "\r\n", 2) != 2) {
347 rctx
->state
= OHS_ERROR
;
350 rctx
->state
= OHS_ASN1_WRITE_INIT
;
352 case OHS_ASN1_WRITE_INIT
:
353 rctx
->asn1_len
= BIO_get_mem_data(rctx
->mem
, NULL
);
354 rctx
->state
= OHS_ASN1_WRITE
;
357 n
= BIO_get_mem_data(rctx
->mem
, &p
);
359 i
= BIO_write(rctx
->io
, p
+ (n
- rctx
->asn1_len
), rctx
->asn1_len
);
362 if (BIO_should_retry(rctx
->io
))
364 rctx
->state
= OHS_ERROR
;
370 if (rctx
->asn1_len
> 0)
373 rctx
->state
= OHS_ASN1_FLUSH
;
375 (void)BIO_reset(rctx
->mem
);
379 i
= BIO_flush(rctx
->io
);
382 rctx
->state
= OHS_FIRSTLINE
;
386 if (BIO_should_retry(rctx
->io
))
389 rctx
->state
= OHS_ERROR
;
398 /* Attempt to read a line in */
402 * Due to &%^*$" memory BIO behaviour with BIO_gets we have to check
403 * there's a complete line in there before calling BIO_gets or we'll
404 * just get a partial read.
406 n
= BIO_get_mem_data(rctx
->mem
, &p
);
407 if ((n
<= 0) || !memchr(p
, '\n', n
)) {
408 if (n
>= rctx
->iobuflen
) {
409 rctx
->state
= OHS_ERROR
;
414 n
= BIO_gets(rctx
->mem
, (char *)rctx
->iobuf
, rctx
->iobuflen
);
417 if (BIO_should_retry(rctx
->mem
))
419 rctx
->state
= OHS_ERROR
;
423 /* Don't allow excessive lines */
424 if (n
== rctx
->iobuflen
) {
425 rctx
->state
= OHS_ERROR
;
430 if (rctx
->state
== OHS_FIRSTLINE
) {
431 if (parse_http_line1((char *)rctx
->iobuf
)) {
432 rctx
->state
= OHS_HEADERS
;
435 rctx
->state
= OHS_ERROR
;
439 /* Look for blank line: end of headers */
440 for (p
= rctx
->iobuf
; *p
; p
++) {
441 if ((*p
!= '\r') && (*p
!= '\n'))
447 rctx
->state
= OHS_ASN1_HEADER
;
453 case OHS_ASN1_HEADER
:
455 * Now reading ASN1 header: can read at least 2 bytes which is enough
456 * for ASN1 SEQUENCE header and either length field or at least the
457 * length of the length field.
459 n
= BIO_get_mem_data(rctx
->mem
, &p
);
463 /* Check it is an ASN1 SEQUENCE */
464 if (*p
++ != (V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
)) {
465 rctx
->state
= OHS_ERROR
;
469 /* Check out length field */
472 * If MSB set on initial length octet we can now always read 6
473 * octets: make sure we have them.
478 /* Not NDEF or excessive length */
480 rctx
->state
= OHS_ERROR
;
485 for (i
= 0; i
< n
; i
++) {
486 rctx
->asn1_len
<<= 8;
487 rctx
->asn1_len
|= *p
++;
490 if (rctx
->asn1_len
> rctx
->max_resp_len
) {
491 rctx
->state
= OHS_ERROR
;
495 rctx
->asn1_len
+= n
+ 2;
497 rctx
->asn1_len
= *p
+ 2;
499 rctx
->state
= OHS_ASN1_CONTENT
;
503 case OHS_ASN1_CONTENT
:
504 n
= BIO_get_mem_data(rctx
->mem
, NULL
);
505 if (n
< (int)rctx
->asn1_len
)
508 rctx
->state
= OHS_DONE
;
520 int OCSP_sendreq_nbio(OCSP_RESPONSE
**presp
, OCSP_REQ_CTX
*rctx
)
522 return OCSP_REQ_CTX_nbio_d2i(rctx
,
523 (ASN1_VALUE
**)presp
,
524 ASN1_ITEM_rptr(OCSP_RESPONSE
));
527 /* Blocking OCSP request handler: now a special case of non-blocking I/O */
529 OCSP_RESPONSE
*OCSP_sendreq_bio(BIO
*b
, const char *path
, OCSP_REQUEST
*req
)
531 OCSP_RESPONSE
*resp
= NULL
;
535 ctx
= OCSP_sendreq_new(b
, path
, req
, -1);
541 rv
= OCSP_sendreq_nbio(&resp
, ctx
);
542 } while ((rv
== -1) && BIO_should_retry(b
));
544 OCSP_REQ_CTX_free(ctx
);