1/* ocsp_ht.c */ 2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 3 * project 2000. 4 */ 5/* ==================================================================== 6 * Copyright (c) 2000 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 <openssl/asn1.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <ctype.h> 63#include <string.h> 64#include <openssl/ocsp.h> 65#include <openssl/err.h> 66#include <openssl/buffer.h> 67#ifdef OPENSSL_SYS_SUNOS 68#define strtoul (unsigned long)strtol 69#endif /* OPENSSL_SYS_SUNOS */ 70 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 74 * other refinements. 75 */ 76 77OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, char *path, OCSP_REQUEST *req) 78{ 79 BIO *mem = NULL; 80 char tmpbuf[1024]; 81 OCSP_RESPONSE *resp = NULL; 82 char *p, *q, *r; 83 int len, retcode; 84 static char req_txt[] = 85"POST %s HTTP/1.0\r\n\ 86Content-Type: application/ocsp-request\r\n\ 87Content-Length: %d\r\n\r\n"; 88 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); 92 goto err; 93 } 94 if(i2d_OCSP_REQUEST_bio(b, req) <= 0) { 95 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_WRITE_ERROR); 96 goto err; 97 } 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))) { 101 if(len < 0) { 102 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_READ_ERROR); 103 goto err; 104 } 105 BIO_write(mem, tmpbuf, len); 106 } 107 if(BIO_gets(mem, tmpbuf, 512) <= 0) { 108 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR); 109 goto err; 110 } 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. 114 */ 115 116 /* Skip to first white space (passed protocol info) */ 117 for(p = tmpbuf; *p && !isspace((unsigned char)*p); p++) continue; 118 if(!*p) { 119 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR); 120 goto err; 121 } 122 /* Skip past white space to start of response code */ 123 while(*p && isspace((unsigned char)*p)) p++; 124 if(!*p) { 125 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR); 126 goto err; 127 } 128 /* Find end of response code: first whitespace after start of code */ 129 for(q = p; *q && !isspace((unsigned char)*q); q++) continue; 130 if(!*q) { 131 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR); 132 goto err; 133 } 134 /* Set end of response code and start of message */ 135 *q++ = 0; 136 /* Attempt to parse numeric code */ 137 retcode = strtoul(p, &r, 10); 138 if(*r) goto err; 139 /* Skip over any leading white space in message */ 140 while(*q && isspace((unsigned char)*q)) q++; 141 if(*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; 145 } 146 if(retcode != 200) { 147 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_ERROR); 148 if(!*q) { 149 ERR_add_error_data(2, "Code=", p); 150 } 151 else { 152 ERR_add_error_data(4, "Code=", p, ",Reason=", q); 153 } 154 goto err; 155 } 156 /* Find blank line marking beginning of content */ 157 while(BIO_gets(mem, tmpbuf, 512) > 0) 158 { 159 for(p = tmpbuf; *p && isspace((unsigned char)*p); p++) continue; 160 if(!*p) break; 161 } 162 if(*p) { 163 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_NO_CONTENT); 164 goto err; 165 } 166 if(!(resp = d2i_OCSP_RESPONSE_bio(mem, NULL))) { 167 OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,ERR_R_NESTED_ASN1_ERROR); 168 goto err; 169 } 170 err: 171 BIO_free(mem); 172 return resp; 173} 174