1 /* ocsp_ht.c */
2 /*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 2006.
5 */
6 /* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
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
19 * distribution.
20 *
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/)"
25 *
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.
30 *
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.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
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 * ====================================================================
53 *
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).
57 *
58 */
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <ctype.h>
63 #include <string.h>
64 #include "e_os.h"
65 #include <openssl/asn1.h>
66 #include <openssl/ocsp.h>
67 #include <openssl/err.h>
68 #include <openssl/buffer.h>
69 #ifdef OPENSSL_SYS_SUNOS
70 # define strtoul (unsigned long)strtol
71 #endif /* OPENSSL_SYS_SUNOS */
72
73 /* Stateful OCSP request code, supporting non-blocking I/O */
74
75 /* Opaque OCSP request status structure */
76
77 struct ocsp_req_ctx_st {
78 int state; /* Current I/O state */
79 unsigned char *iobuf; /* Line buffer */
80 int iobuflen; /* Line buffer length */
81 BIO *io; /* BIO to perform I/O with */
82 BIO *mem; /* Memory BIO response is built into */
83 unsigned long asn1_len; /* ASN1 length of response */
84 };
85
86 #define OCSP_MAX_REQUEST_LENGTH (100 * 1024)
87 #define OCSP_MAX_LINE_LEN 4096;
88
89 /* OCSP states */
90
91 /* If set no reading should be performed */
92 #define OHS_NOREAD 0x1000
93 /* Error condition */
94 #define OHS_ERROR (0 | OHS_NOREAD)
95 /* First line being read */
96 #define OHS_FIRSTLINE 1
97 /* MIME headers being read */
98 #define OHS_HEADERS 2
99 /* OCSP initial header (tag + length) being read */
100 #define OHS_ASN1_HEADER 3
101 /* OCSP content octets being read */
102 #define OHS_ASN1_CONTENT 4
103 /* Request being sent */
104 #define OHS_ASN1_WRITE (6 | OHS_NOREAD)
105 /* Request being flushed */
106 #define OHS_ASN1_FLUSH (7 | OHS_NOREAD)
107 /* Completed */
108 #define OHS_DONE (8 | OHS_NOREAD)
109
110 static int parse_http_line1(char *line);
111
OCSP_REQ_CTX_free(OCSP_REQ_CTX * rctx)112 void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx)
113 {
114 if (rctx->mem)
115 BIO_free(rctx->mem);
116 if (rctx->iobuf)
117 OPENSSL_free(rctx->iobuf);
118 OPENSSL_free(rctx);
119 }
120
OCSP_sendreq_new(BIO * io,char * path,OCSP_REQUEST * req,int maxline)121 OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, char *path, OCSP_REQUEST *req,
122 int maxline)
123 {
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";
127
128 OCSP_REQ_CTX *rctx;
129 rctx = OPENSSL_malloc(sizeof(OCSP_REQ_CTX));
130 rctx->state = OHS_FIRSTLINE;
131 rctx->mem = BIO_new(BIO_s_mem());
132 rctx->io = io;
133 if (maxline > 0)
134 rctx->iobuflen = maxline;
135 else
136 rctx->iobuflen = OCSP_MAX_LINE_LEN;
137 rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
138 if (!path)
139 path = "/";
140
141 if (BIO_printf(rctx->mem, post_hdr, path,
142 i2d_OCSP_REQUEST(req, NULL)) <= 0) {
143 rctx->state = OHS_ERROR;
144 return 0;
145 }
146 if (i2d_OCSP_REQUEST_bio(rctx->mem, req) <= 0) {
147 rctx->state = OHS_ERROR;
148 return 0;
149 }
150 rctx->state = OHS_ASN1_WRITE;
151 rctx->asn1_len = BIO_get_mem_data(rctx->mem, NULL);
152
153 return rctx;
154 }
155
156 /*
157 * Parse the HTTP response. This will look like this: "HTTP/1.0 200 OK". We
158 * need to obtain the numeric code and (optional) informational message.
159 */
160
parse_http_line1(char * line)161 static int parse_http_line1(char *line)
162 {
163 int retcode;
164 char *p, *q, *r;
165 /* Skip to first white space (passed protocol info) */
166
167 for (p = line; *p && !isspace((unsigned char)*p); p++)
168 continue;
169 if (!*p) {
170 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
171 return 0;
172 }
173
174 /* Skip past white space to start of response code */
175 while (*p && isspace((unsigned char)*p))
176 p++;
177
178 if (!*p) {
179 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
180 return 0;
181 }
182
183 /* Find end of response code: first whitespace after start of code */
184 for (q = p; *q && !isspace((unsigned char)*q); q++)
185 continue;
186
187 if (!*q) {
188 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
189 return 0;
190 }
191
192 /* Set end of response code and start of message */
193 *q++ = 0;
194
195 /* Attempt to parse numeric code */
196 retcode = strtoul(p, &r, 10);
197
198 if (*r)
199 return 0;
200
201 /* Skip over any leading white space in message */
202 while (*q && isspace((unsigned char)*q))
203 q++;
204
205 if (*q) {
206 /*
207 * Finally zap any trailing white space in message (include CRLF)
208 */
209
210 /* We know q has a non white space character so this is OK */
211 for (r = q + strlen(q) - 1; isspace((unsigned char)*r); r--)
212 *r = 0;
213 }
214 if (retcode != 200) {
215 OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_ERROR);
216 if (!*q)
217 ERR_add_error_data(2, "Code=", p);
218 else
219 ERR_add_error_data(4, "Code=", p, ",Reason=", q);
220 return 0;
221 }
222
223 return 1;
224
225 }
226
OCSP_sendreq_nbio(OCSP_RESPONSE ** presp,OCSP_REQ_CTX * rctx)227 int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx)
228 {
229 int i, n;
230 const unsigned char *p;
231 next_io:
232 if (!(rctx->state & OHS_NOREAD)) {
233 n = BIO_read(rctx->io, rctx->iobuf, rctx->iobuflen);
234
235 if (n <= 0) {
236 if (BIO_should_retry(rctx->io))
237 return -1;
238 return 0;
239 }
240
241 /* Write data to memory BIO */
242
243 if (BIO_write(rctx->mem, rctx->iobuf, n) != n)
244 return 0;
245 }
246
247 switch (rctx->state) {
248
249 case OHS_ASN1_WRITE:
250 n = BIO_get_mem_data(rctx->mem, &p);
251
252 i = BIO_write(rctx->io, p + (n - rctx->asn1_len), rctx->asn1_len);
253
254 if (i <= 0) {
255 if (BIO_should_retry(rctx->io))
256 return -1;
257 rctx->state = OHS_ERROR;
258 return 0;
259 }
260
261 rctx->asn1_len -= i;
262
263 if (rctx->asn1_len > 0)
264 goto next_io;
265
266 rctx->state = OHS_ASN1_FLUSH;
267
268 (void)BIO_reset(rctx->mem);
269
270 case OHS_ASN1_FLUSH:
271
272 i = BIO_flush(rctx->io);
273
274 if (i > 0) {
275 rctx->state = OHS_FIRSTLINE;
276 goto next_io;
277 }
278
279 if (BIO_should_retry(rctx->io))
280 return -1;
281
282 rctx->state = OHS_ERROR;
283 return 0;
284
285 case OHS_ERROR:
286 return 0;
287
288 case OHS_FIRSTLINE:
289 case OHS_HEADERS:
290
291 /* Attempt to read a line in */
292
293 next_line:
294 /*
295 * Due to &%^*$" memory BIO behaviour with BIO_gets we have to check
296 * there's a complete line in there before calling BIO_gets or we'll
297 * just get a partial read.
298 */
299 n = BIO_get_mem_data(rctx->mem, &p);
300 if ((n <= 0) || !memchr(p, '\n', n)) {
301 if (n >= rctx->iobuflen) {
302 rctx->state = OHS_ERROR;
303 return 0;
304 }
305 goto next_io;
306 }
307 n = BIO_gets(rctx->mem, (char *)rctx->iobuf, rctx->iobuflen);
308
309 if (n <= 0) {
310 if (BIO_should_retry(rctx->mem))
311 goto next_io;
312 rctx->state = OHS_ERROR;
313 return 0;
314 }
315
316 /* Don't allow excessive lines */
317 if (n == rctx->iobuflen) {
318 rctx->state = OHS_ERROR;
319 return 0;
320 }
321
322 /* First line */
323 if (rctx->state == OHS_FIRSTLINE) {
324 if (parse_http_line1((char *)rctx->iobuf)) {
325 rctx->state = OHS_HEADERS;
326 goto next_line;
327 } else {
328 rctx->state = OHS_ERROR;
329 return 0;
330 }
331 } else {
332 /* Look for blank line: end of headers */
333 for (p = rctx->iobuf; *p; p++) {
334 if ((*p != '\r') && (*p != '\n'))
335 break;
336 }
337 if (*p)
338 goto next_line;
339
340 rctx->state = OHS_ASN1_HEADER;
341
342 }
343
344 /* Fall thru */
345
346 case OHS_ASN1_HEADER:
347 /*
348 * Now reading ASN1 header: can read at least 2 bytes which is enough
349 * for ASN1 SEQUENCE header and either length field or at least the
350 * length of the length field.
351 */
352 n = BIO_get_mem_data(rctx->mem, &p);
353 if (n < 2)
354 goto next_io;
355
356 /* Check it is an ASN1 SEQUENCE */
357 if (*p++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
358 rctx->state = OHS_ERROR;
359 return 0;
360 }
361
362 /* Check out length field */
363 if (*p & 0x80) {
364 /*
365 * If MSB set on initial length octet we can now always read 6
366 * octets: make sure we have them.
367 */
368 if (n < 6)
369 goto next_io;
370 n = *p & 0x7F;
371 /* Not NDEF or excessive length */
372 if (!n || (n > 4)) {
373 rctx->state = OHS_ERROR;
374 return 0;
375 }
376 p++;
377 rctx->asn1_len = 0;
378 for (i = 0; i < n; i++) {
379 rctx->asn1_len <<= 8;
380 rctx->asn1_len |= *p++;
381 }
382
383 if (rctx->asn1_len > OCSP_MAX_REQUEST_LENGTH) {
384 rctx->state = OHS_ERROR;
385 return 0;
386 }
387
388 rctx->asn1_len += n + 2;
389 } else
390 rctx->asn1_len = *p + 2;
391
392 rctx->state = OHS_ASN1_CONTENT;
393
394 /* Fall thru */
395
396 case OHS_ASN1_CONTENT:
397 n = BIO_get_mem_data(rctx->mem, &p);
398 if (n < (int)rctx->asn1_len)
399 goto next_io;
400
401 *presp = d2i_OCSP_RESPONSE(NULL, &p, rctx->asn1_len);
402 if (*presp) {
403 rctx->state = OHS_DONE;
404 return 1;
405 }
406
407 rctx->state = OHS_ERROR;
408 return 0;
409
410 break;
411
412 case OHS_DONE:
413 return 1;
414
415 }
416
417 return 0;
418
419 }
420
421 /* Blocking OCSP request handler: now a special case of non-blocking I/O */
422
OCSP_sendreq_bio(BIO * b,char * path,OCSP_REQUEST * req)423 OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, char *path, OCSP_REQUEST *req)
424 {
425 OCSP_RESPONSE *resp = NULL;
426 OCSP_REQ_CTX *ctx;
427 int rv;
428
429 ctx = OCSP_sendreq_new(b, path, req, -1);
430
431 if (!ctx)
432 return NULL;
433
434 do {
435 rv = OCSP_sendreq_nbio(&resp, ctx);
436 } while ((rv == -1) && BIO_should_retry(b));
437
438 OCSP_REQ_CTX_free(ctx);
439
440 if (rv)
441 return resp;
442
443 return NULL;
444 }
445