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