2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include <openssl/asn1.h>
16 #include <openssl/ocsp.h>
17 #include <openssl/err.h>
18 #include <openssl/buffer.h>
20 /* Stateful OCSP request code, supporting non-blocking I/O */
22 /* Opaque OCSP request status structure */
24 struct ocsp_req_ctx_st {
25 int state; /* Current I/O state */
26 unsigned char *iobuf; /* Line buffer */
27 int iobuflen; /* Line buffer length */
28 BIO *io; /* BIO to perform I/O with */
29 BIO *mem; /* Memory BIO response is built into */
30 unsigned long asn1_len; /* ASN1 length of response */
31 unsigned long max_resp_len; /* Maximum length of response */
34 #define OCSP_MAX_RESP_LENGTH (100 * 1024)
35 #define OCSP_MAX_LINE_LEN 4096;
39 /* If set no reading should be performed */
40 #define OHS_NOREAD 0x1000
42 #define OHS_ERROR (0 | OHS_NOREAD)
43 /* First line being read */
44 #define OHS_FIRSTLINE 1
45 /* MIME headers being read */
47 /* OCSP initial header (tag + length) being read */
48 #define OHS_ASN1_HEADER 3
49 /* OCSP content octets being read */
50 #define OHS_ASN1_CONTENT 4
51 /* First call: ready to start I/O */
52 #define OHS_ASN1_WRITE_INIT (5 | OHS_NOREAD)
53 /* Request being sent */
54 #define OHS_ASN1_WRITE (6 | OHS_NOREAD)
55 /* Request being flushed */
56 #define OHS_ASN1_FLUSH (7 | OHS_NOREAD)
58 #define OHS_DONE (8 | OHS_NOREAD)
59 /* Headers set, no final \r\n included */
60 #define OHS_HTTP_HEADER (9 | OHS_NOREAD)
62 static int parse_http_line1(char *line);
64 OCSP_REQ_CTX *OCSP_REQ_CTX_new(BIO *io, int maxline)
66 OCSP_REQ_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
70 rctx->state = OHS_ERROR;
71 rctx->max_resp_len = OCSP_MAX_RESP_LENGTH;
72 rctx->mem = BIO_new(BIO_s_mem());
75 rctx->iobuflen = maxline;
77 rctx->iobuflen = OCSP_MAX_LINE_LEN;
78 rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
79 if (rctx->iobuf == NULL || rctx->mem == NULL) {
80 OCSP_REQ_CTX_free(rctx);
86 void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx)
91 OPENSSL_free(rctx->iobuf);
95 BIO *OCSP_REQ_CTX_get0_mem_bio(OCSP_REQ_CTX *rctx)
100 void OCSP_set_max_response_length(OCSP_REQ_CTX *rctx, unsigned long len)
103 rctx->max_resp_len = OCSP_MAX_RESP_LENGTH;
105 rctx->max_resp_len = len;
108 int OCSP_REQ_CTX_i2d(OCSP_REQ_CTX *rctx, const ASN1_ITEM *it, ASN1_VALUE *val)
110 static const char req_hdr[] =
111 "Content-Type: application/ocsp-request\r\n"
112 "Content-Length: %d\r\n\r\n";
113 int reqlen = ASN1_item_i2d(val, NULL, it);
114 if (BIO_printf(rctx->mem, req_hdr, reqlen) <= 0)
116 if (ASN1_item_i2d_bio(it, rctx->mem, val) <= 0)
118 rctx->state = OHS_ASN1_WRITE_INIT;
122 int OCSP_REQ_CTX_nbio_d2i(OCSP_REQ_CTX *rctx,
123 ASN1_VALUE **pval, const ASN1_ITEM *it)
126 const unsigned char *p;
128 rv = OCSP_REQ_CTX_nbio(rctx);
132 len = BIO_get_mem_data(rctx->mem, &p);
133 *pval = ASN1_item_d2i(NULL, &p, len, it);
135 rctx->state = OHS_ERROR;
141 int OCSP_REQ_CTX_http(OCSP_REQ_CTX *rctx, const char *op, const char *path)
143 static const char http_hdr[] = "%s %s HTTP/1.0\r\n";
148 if (BIO_printf(rctx->mem, http_hdr, op, path) <= 0)
150 rctx->state = OHS_HTTP_HEADER;
154 int OCSP_REQ_CTX_set1_req(OCSP_REQ_CTX *rctx, OCSP_REQUEST *req)
156 return OCSP_REQ_CTX_i2d(rctx, ASN1_ITEM_rptr(OCSP_REQUEST),
160 int OCSP_REQ_CTX_add1_header(OCSP_REQ_CTX *rctx,
161 const char *name, const char *value)
165 if (BIO_puts(rctx->mem, name) <= 0)
168 if (BIO_write(rctx->mem, ": ", 2) != 2)
170 if (BIO_puts(rctx->mem, value) <= 0)
173 if (BIO_write(rctx->mem, "\r\n", 2) != 2)
175 rctx->state = OHS_HTTP_HEADER;
179 OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path, OCSP_REQUEST *req,
183 OCSP_REQ_CTX *rctx = NULL;
184 rctx = OCSP_REQ_CTX_new(io, maxline);
188 if (!OCSP_REQ_CTX_http(rctx, "POST", path))
191 if (req && !OCSP_REQ_CTX_set1_req(rctx, req))
197 OCSP_REQ_CTX_free(rctx);
202 * Parse the HTTP response. This will look like this: "HTTP/1.0 200 OK". We
203 * need to obtain the numeric code and (optional) informational message.
206 static int parse_http_line1(char *line)
210 /* Skip to first white space (passed protocol info) */
212 for (p = line; *p && !isspace((unsigned char)*p); p++)
215 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
219 /* Skip past white space to start of response code */
220 while (*p && isspace((unsigned char)*p))
224 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
228 /* Find end of response code: first whitespace after start of code */
229 for (q = p; *q && !isspace((unsigned char)*q); q++)
233 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
237 /* Set end of response code and start of message */
240 /* Attempt to parse numeric code */
241 retcode = strtoul(p, &r, 10);
246 /* Skip over any leading white space in message */
247 while (*q && isspace((unsigned char)*q))
252 * Finally zap any trailing white space in message (include CRLF)
255 /* We know q has a non white space character so this is OK */
256 for (r = q + strlen(q) - 1; isspace((unsigned char)*r); r--)
259 if (retcode != 200) {
260 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_ERROR);
262 ERR_add_error_data(2, "Code=", p);
264 ERR_add_error_data(4, "Code=", p, ",Reason=", q);
272 int OCSP_REQ_CTX_nbio(OCSP_REQ_CTX *rctx)
275 const unsigned char *p;
277 if (!(rctx->state & OHS_NOREAD)) {
278 n = BIO_read(rctx->io, rctx->iobuf, rctx->iobuflen);
281 if (BIO_should_retry(rctx->io))
286 /* Write data to memory BIO */
288 if (BIO_write(rctx->mem, rctx->iobuf, n) != n)
292 switch (rctx->state) {
293 case OHS_HTTP_HEADER:
294 /* Last operation was adding headers: need a final \r\n */
295 if (BIO_write(rctx->mem, "\r\n", 2) != 2) {
296 rctx->state = OHS_ERROR;
299 rctx->state = OHS_ASN1_WRITE_INIT;
301 case OHS_ASN1_WRITE_INIT:
302 rctx->asn1_len = BIO_get_mem_data(rctx->mem, NULL);
303 rctx->state = OHS_ASN1_WRITE;
306 n = BIO_get_mem_data(rctx->mem, &p);
308 i = BIO_write(rctx->io, p + (n - rctx->asn1_len), rctx->asn1_len);
311 if (BIO_should_retry(rctx->io))
313 rctx->state = OHS_ERROR;
319 if (rctx->asn1_len > 0)
322 rctx->state = OHS_ASN1_FLUSH;
324 (void)BIO_reset(rctx->mem);
328 i = BIO_flush(rctx->io);
331 rctx->state = OHS_FIRSTLINE;
335 if (BIO_should_retry(rctx->io))
338 rctx->state = OHS_ERROR;
347 /* Attempt to read a line in */
351 * Due to &%^*$" memory BIO behaviour with BIO_gets we have to check
352 * there's a complete line in there before calling BIO_gets or we'll
353 * just get a partial read.
355 n = BIO_get_mem_data(rctx->mem, &p);
356 if ((n <= 0) || !memchr(p, '\n', n)) {
357 if (n >= rctx->iobuflen) {
358 rctx->state = OHS_ERROR;
363 n = BIO_gets(rctx->mem, (char *)rctx->iobuf, rctx->iobuflen);
366 if (BIO_should_retry(rctx->mem))
368 rctx->state = OHS_ERROR;
372 /* Don't allow excessive lines */
373 if (n == rctx->iobuflen) {
374 rctx->state = OHS_ERROR;
379 if (rctx->state == OHS_FIRSTLINE) {
380 if (parse_http_line1((char *)rctx->iobuf)) {
381 rctx->state = OHS_HEADERS;
384 rctx->state = OHS_ERROR;
388 /* Look for blank line: end of headers */
389 for (p = rctx->iobuf; *p; p++) {
390 if ((*p != '\r') && (*p != '\n'))
396 rctx->state = OHS_ASN1_HEADER;
402 case OHS_ASN1_HEADER:
404 * Now reading ASN1 header: can read at least 2 bytes which is enough
405 * for ASN1 SEQUENCE header and either length field or at least the
406 * length of the length field.
408 n = BIO_get_mem_data(rctx->mem, &p);
412 /* Check it is an ASN1 SEQUENCE */
413 if (*p++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
414 rctx->state = OHS_ERROR;
418 /* Check out length field */
421 * If MSB set on initial length octet we can now always read 6
422 * octets: make sure we have them.
427 /* Not NDEF or excessive length */
429 rctx->state = OHS_ERROR;
434 for (i = 0; i < n; i++) {
435 rctx->asn1_len <<= 8;
436 rctx->asn1_len |= *p++;
439 if (rctx->asn1_len > rctx->max_resp_len) {
440 rctx->state = OHS_ERROR;
444 rctx->asn1_len += n + 2;
446 rctx->asn1_len = *p + 2;
448 rctx->state = OHS_ASN1_CONTENT;
452 case OHS_ASN1_CONTENT:
453 n = BIO_get_mem_data(rctx->mem, NULL);
454 if (n < (int)rctx->asn1_len)
457 rctx->state = OHS_DONE;
469 int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx)
471 return OCSP_REQ_CTX_nbio_d2i(rctx,
472 (ASN1_VALUE **)presp,
473 ASN1_ITEM_rptr(OCSP_RESPONSE));
476 /* Blocking OCSP request handler: now a special case of non-blocking I/O */
478 OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req)
480 OCSP_RESPONSE *resp = NULL;
484 ctx = OCSP_sendreq_new(b, path, req, -1);
490 rv = OCSP_sendreq_nbio(&resp, ctx);
491 } while ((rv == -1) && BIO_should_retry(b));
493 OCSP_REQ_CTX_free(ctx);