ocsp_ht.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <openssl/asn1.h>
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <ctype.h>
  62. #include <string.h>
  63. #include <openssl/ocsp.h>
  64. #include <openssl/err.h>
  65. #include <openssl/buffer.h>
  66. #ifdef OPENSSL_SYS_SUNOS
  67. #define strtoul (unsigned long)strtol
  68. #endif /* OPENSSL_SYS_SUNOS */
  69. /* Quick and dirty HTTP OCSP request handler.
  70. * Could make this a bit cleverer by adding
  71. * support for non blocking BIOs and a few
  72. * other refinements.
  73. */
  74. OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, char *path, OCSP_REQUEST *req)
  75. {
  76. BIO *mem = NULL;
  77. char tmpbuf[1024];
  78. OCSP_RESPONSE *resp = NULL;
  79. char *p, *q, *r;
  80. int len, retcode;
  81. static char req_txt[] =
  82. "POST %s HTTP/1.0\r\n\
  83. Content-Type: application/ocsp-request\r\n\
  84. Content-Length: %d\r\n\r\n";
  85. len = i2d_OCSP_REQUEST(req, NULL);
  86. if(BIO_printf(b, req_txt, path, len) < 0) {
  87. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_WRITE_ERROR);
  88. goto err;
  89. }
  90. if(i2d_OCSP_REQUEST_bio(b, req) <= 0) {
  91. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_WRITE_ERROR);
  92. goto err;
  93. }
  94. if(!(mem = BIO_new(BIO_s_mem()))) goto err;
  95. /* Copy response to a memory BIO: socket bios can't do gets! */
  96. while ((len = BIO_read(b, tmpbuf, sizeof tmpbuf))) {
  97. if(len < 0) {
  98. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_READ_ERROR);
  99. goto err;
  100. }
  101. BIO_write(mem, tmpbuf, len);
  102. }
  103. if(BIO_gets(mem, tmpbuf, 512) <= 0) {
  104. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
  105. goto err;
  106. }
  107. /* Parse the HTTP response. This will look like this:
  108. * "HTTP/1.0 200 OK". We need to obtain the numeric code and
  109. * (optional) informational message.
  110. */
  111. /* Skip to first white space (passed protocol info) */
  112. for(p = tmpbuf; *p && !isspace((unsigned char)*p); p++) continue;
  113. if(!*p) {
  114. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
  115. goto err;
  116. }
  117. /* Skip past white space to start of response code */
  118. while(*p && isspace((unsigned char)*p)) p++;
  119. if(!*p) {
  120. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
  121. goto err;
  122. }
  123. /* Find end of response code: first whitespace after start of code */
  124. for(q = p; *q && !isspace((unsigned char)*q); q++) continue;
  125. if(!*q) {
  126. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
  127. goto err;
  128. }
  129. /* Set end of response code and start of message */
  130. *q++ = 0;
  131. /* Attempt to parse numeric code */
  132. retcode = strtoul(p, &r, 10);
  133. if(*r) goto err;
  134. /* Skip over any leading white space in message */
  135. while(*q && isspace((unsigned char)*q)) q++;
  136. if(*q) {
  137. /* Finally zap any trailing white space in message (include CRLF) */
  138. /* We know q has a non white space character so this is OK */
  139. for(r = q + strlen(q) - 1; isspace((unsigned char)*r); r--) *r = 0;
  140. }
  141. if(retcode != 200) {
  142. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_SERVER_RESPONSE_ERROR);
  143. if(!*q) {
  144. ERR_add_error_data(2, "Code=", p);
  145. }
  146. else {
  147. ERR_add_error_data(4, "Code=", p, ",Reason=", q);
  148. }
  149. goto err;
  150. }
  151. /* Find blank line marking beginning of content */
  152. while(BIO_gets(mem, tmpbuf, 512) > 0)
  153. {
  154. for(p = tmpbuf; *p && isspace((unsigned char)*p); p++) continue;
  155. if(!*p) break;
  156. }
  157. if(*p) {
  158. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,OCSP_R_NO_CONTENT);
  159. goto err;
  160. }
  161. if(!(resp = d2i_OCSP_RESPONSE_bio(mem, NULL))) {
  162. OCSPerr(OCSP_F_OCSP_SENDREQ_BIO,ERR_R_NESTED_ASN1_ERROR);
  163. goto err;
  164. }
  165. err:
  166. BIO_free(mem);
  167. return resp;
  168. }