68
69
#define strtoul (unsigned long)strtol
69
70
#endif /* OPENSSL_SYS_SUNOS */
71
/* Quick and dirty HTTP OCSP request handler.
72
* Could make this a bit cleverer by adding
73
* support for non blocking BIOs and a few
72
/* Stateful OCSP request code, supporting non-blocking I/O */
74
/* Opaque OCSP request status structure */
76
struct ocsp_req_ctx_st {
77
int state; /* Current I/O state */
78
unsigned char *iobuf; /* Line buffer */
79
int iobuflen; /* Line buffer length */
80
BIO *io; /* BIO to perform I/O with */
81
BIO *mem; /* Memory BIO response is built into */
82
unsigned long asn1_len; /* ASN1 length of response */
85
#define OCSP_MAX_REQUEST_LENGTH (100 * 1024)
86
#define OCSP_MAX_LINE_LEN 4096;
90
/* If set no reading should be performed */
91
#define OHS_NOREAD 0x1000
93
#define OHS_ERROR (0 | OHS_NOREAD)
94
/* First line being read */
95
#define OHS_FIRSTLINE 1
96
/* MIME headers being read */
98
/* OCSP initial header (tag + length) being read */
99
#define OHS_ASN1_HEADER 3
100
/* OCSP content octets being read */
101
#define OHS_ASN1_CONTENT 4
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)
110
static int parse_http_line1(char *line);
112
void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx)
117
OPENSSL_free(rctx->iobuf);
121
OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, char *path, OCSP_REQUEST *req,
124
static char post_hdr[] = "POST %s HTTP/1.0\r\n"
125
"Content-Type: application/ocsp-request\r\n"
126
"Content-Length: %d\r\n\r\n";
129
rctx = OPENSSL_malloc(sizeof(OCSP_REQ_CTX));
130
rctx->state = OHS_FIRSTLINE;
131
rctx->mem = BIO_new(BIO_s_mem());
134
rctx->iobuflen = maxline;
136
rctx->iobuflen = OCSP_MAX_LINE_LEN;
137
rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
141
if (BIO_printf(rctx->mem, post_hdr, path,
142
i2d_OCSP_REQUEST(req, NULL)) <= 0)
144
rctx->state = OHS_ERROR;
147
if (i2d_OCSP_REQUEST_bio(rctx->mem, req) <= 0)
149
rctx->state = OHS_ERROR;
152
rctx->state = OHS_ASN1_WRITE;
153
rctx->asn1_len = BIO_get_mem_data(rctx->mem, NULL);
158
/* Parse the HTTP response. This will look like this:
159
* "HTTP/1.0 200 OK". We need to obtain the numeric code and
160
* (optional) informational message.
77
OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, char *path, OCSP_REQUEST *req)
81
OCSP_RESPONSE *resp = NULL;
163
static int parse_http_line1(char *line)
84
static char req_txt[] =
85
"POST %s HTTP/1.0\r\n\
86
Content-Type: application/ocsp-request\r\n\
87
Content-Length: %d\r\n\r\n";
89
len = i2d_OCSP_REQUEST(req, NULL);
90
if(BIO_printf(b, req_txt, path, len) < 0) {
91
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_WRITE_ERROR);
94
if(i2d_OCSP_REQUEST_bio(b, req) <= 0) {
95
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_WRITE_ERROR);
98
if(!(mem = BIO_new(BIO_s_mem()))) goto err;
99
/* Copy response to a memory BIO: socket bios can't do gets! */
100
while ((len = BIO_read(b, tmpbuf, sizeof tmpbuf))) {
102
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_READ_ERROR);
105
BIO_write(mem, tmpbuf, len);
107
if(BIO_gets(mem, tmpbuf, 512) <= 0) {
108
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
111
/* Parse the HTTP response. This will look like this:
112
* "HTTP/1.0 200 OK". We need to obtain the numeric code and
113
* (optional) informational message.
116
167
/* Skip to first white space (passed protocol info) */
117
for(p = tmpbuf; *p && !isspace((unsigned char)*p); p++) continue;
119
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
169
for(p = line; *p && !isspace((unsigned char)*p); p++)
173
OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
174
OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
122
178
/* Skip past white space to start of response code */
123
while(*p && isspace((unsigned char)*p)) p++;
125
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
179
while(*p && isspace((unsigned char)*p))
184
OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
185
OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
128
189
/* Find end of response code: first whitespace after start of code */
129
for(q = p; *q && !isspace((unsigned char)*q); q++) continue;
131
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
190
for(q = p; *q && !isspace((unsigned char)*q); q++)
195
OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
196
OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
134
200
/* Set end of response code and start of message */
136
203
/* Attempt to parse numeric code */
137
204
retcode = strtoul(p, &r, 10);
139
209
/* Skip over any leading white space in message */
140
while(*q && isspace((unsigned char)*q)) q++;
142
/* Finally zap any trailing white space in message (include CRLF) */
143
/* We know q has a non white space character so this is OK */
144
for(r = q + strlen(q) - 1; isspace((unsigned char)*r); r--) *r = 0;
147
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_ERROR);
210
while(*q && isspace((unsigned char)*q))
215
/* Finally zap any trailing white space in message (include
218
/* We know q has a non white space character so this is OK */
219
for(r = q + strlen(q) - 1; isspace((unsigned char)*r); r--)
224
OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_ERROR);
149
226
ERR_add_error_data(2, "Code=", p);
152
228
ERR_add_error_data(4, "Code=", p, ",Reason=", q);
156
/* Find blank line marking beginning of content */
157
while(BIO_gets(mem, tmpbuf, 512) > 0)
159
for(p = tmpbuf; *p && isspace((unsigned char)*p); p++) continue;
163
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_NO_CONTENT);
166
if(!(resp = d2i_OCSP_RESPONSE_bio(mem, NULL))) {
167
OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,ERR_R_NESTED_ASN1_ERROR);
237
int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx)
240
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
(void)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 < (int)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);