ocsp_ht.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* ocsp_ht.c */
  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. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <ctype.h>
  61. #include <string.h>
  62. #include "e_os.h"
  63. #include <openssl/asn1.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. /* Stateful OCSP request code, supporting non-blocking I/O */
  71. /* Opaque OCSP request status structure */
  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. #define OCSP_MAX_REQUEST_LENGTH (100 * 1024)
  81. #define OCSP_MAX_LINE_LEN 4096;
  82. /* OCSP states */
  83. /* If set no reading should be performed */
  84. #define OHS_NOREAD 0x1000
  85. /* Error condition */
  86. #define OHS_ERROR (0 | OHS_NOREAD)
  87. /* First line being read */
  88. #define OHS_FIRSTLINE 1
  89. /* MIME headers being read */
  90. #define OHS_HEADERS 2
  91. /* OCSP initial header (tag + length) being read */
  92. #define OHS_ASN1_HEADER 3
  93. /* OCSP content octets being read */
  94. #define OHS_ASN1_CONTENT 4
  95. /* Request being sent */
  96. #define OHS_ASN1_WRITE (6 | OHS_NOREAD)
  97. /* Request being flushed */
  98. #define OHS_ASN1_FLUSH (7 | OHS_NOREAD)
  99. /* Completed */
  100. #define OHS_DONE (8 | OHS_NOREAD)
  101. static int parse_http_line1(char *line);
  102. void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx)
  103. {
  104. if (rctx->mem)
  105. BIO_free(rctx->mem);
  106. if (rctx->iobuf)
  107. OPENSSL_free(rctx->iobuf);
  108. OPENSSL_free(rctx);
  109. }
  110. int OCSP_REQ_CTX_set1_req(OCSP_REQ_CTX *rctx, OCSP_REQUEST *req)
  111. {
  112. static const char req_hdr[] =
  113. "Content-Type: application/ocsp-request\r\n"
  114. "Content-Length: %d\r\n\r\n";
  115. if (BIO_printf(rctx->mem, req_hdr, i2d_OCSP_REQUEST(req, NULL)) <= 0)
  116. return 0;
  117. if (i2d_OCSP_REQUEST_bio(rctx->mem, req) <= 0)
  118. return 0;
  119. rctx->state = OHS_ASN1_WRITE;
  120. rctx->asn1_len = BIO_get_mem_data(rctx->mem, NULL);
  121. return 1;
  122. }
  123. int OCSP_REQ_CTX_add1_header(OCSP_REQ_CTX *rctx,
  124. const char *name, const char *value)
  125. {
  126. if (!name)
  127. return 0;
  128. if (BIO_puts(rctx->mem, name) <= 0)
  129. return 0;
  130. if (value)
  131. {
  132. if (BIO_write(rctx->mem, ": ", 2) != 2)
  133. return 0;
  134. if (BIO_puts(rctx->mem, value) <= 0)
  135. return 0;
  136. }
  137. if (BIO_write(rctx->mem, "\r\n", 2) != 2)
  138. return 0;
  139. return 1;
  140. }
  141. OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, char *path, OCSP_REQUEST *req,
  142. int maxline)
  143. {
  144. static const char post_hdr[] = "POST %s HTTP/1.0\r\n";
  145. OCSP_REQ_CTX *rctx;
  146. rctx = OPENSSL_malloc(sizeof(OCSP_REQ_CTX));
  147. rctx->state = OHS_ERROR;
  148. rctx->mem = BIO_new(BIO_s_mem());
  149. rctx->io = io;
  150. rctx->asn1_len = 0;
  151. if (maxline > 0)
  152. rctx->iobuflen = maxline;
  153. else
  154. rctx->iobuflen = OCSP_MAX_LINE_LEN;
  155. rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
  156. if (!rctx->iobuf)
  157. return 0;
  158. if (!path)
  159. path = "/";
  160. if (BIO_printf(rctx->mem, post_hdr, path) <= 0)
  161. return 0;
  162. if (req && !OCSP_REQ_CTX_set1_req(rctx, req))
  163. return 0;
  164. return rctx;
  165. }
  166. /* Parse the HTTP response. This will look like this:
  167. * "HTTP/1.0 200 OK". We need to obtain the numeric code and
  168. * (optional) informational message.
  169. */
  170. static int parse_http_line1(char *line)
  171. {
  172. int retcode;
  173. char *p, *q, *r;
  174. /* Skip to first white space (passed protocol info) */
  175. for(p = line; *p && !isspace((unsigned char)*p); p++)
  176. continue;
  177. if(!*p)
  178. {
  179. OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
  180. OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
  181. return 0;
  182. }
  183. /* Skip past white space to start of response code */
  184. while(*p && isspace((unsigned char)*p))
  185. p++;
  186. if(!*p)
  187. {
  188. OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
  189. OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
  190. return 0;
  191. }
  192. /* Find end of response code: first whitespace after start of code */
  193. for(q = p; *q && !isspace((unsigned char)*q); q++)
  194. continue;
  195. if(!*q)
  196. {
  197. OCSPerr(OCSP_F_PARSE_HTTP_LINE1,
  198. OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
  199. return 0;
  200. }
  201. /* Set end of response code and start of message */
  202. *q++ = 0;
  203. /* Attempt to parse numeric code */
  204. retcode = strtoul(p, &r, 10);
  205. if(*r)
  206. return 0;
  207. /* Skip over any leading white space in message */
  208. while(*q && isspace((unsigned char)*q))
  209. q++;
  210. if(*q)
  211. {
  212. /* Finally zap any trailing white space in message (include
  213. * CRLF) */
  214. /* We know q has a non white space character so this is OK */
  215. for(r = q + strlen(q) - 1; isspace((unsigned char)*r); r--)
  216. *r = 0;
  217. }
  218. if(retcode != 200)
  219. {
  220. OCSPerr(OCSP_F_PARSE_HTTP_LINE1, OCSP_R_SERVER_RESPONSE_ERROR);
  221. if(!*q)
  222. ERR_add_error_data(2, "Code=", p);
  223. else
  224. ERR_add_error_data(4, "Code=", p, ",Reason=", q);
  225. return 0;
  226. }
  227. return 1;
  228. }
  229. int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx)
  230. {
  231. int i, n;
  232. const unsigned char *p;
  233. next_io:
  234. if (!(rctx->state & OHS_NOREAD))
  235. {
  236. n = BIO_read(rctx->io, rctx->iobuf, rctx->iobuflen);
  237. if (n <= 0)
  238. {
  239. if (BIO_should_retry(rctx->io))
  240. return -1;
  241. return 0;
  242. }
  243. /* Write data to memory BIO */
  244. if (BIO_write(rctx->mem, rctx->iobuf, n) != n)
  245. return 0;
  246. }
  247. switch(rctx->state)
  248. {
  249. case OHS_ASN1_WRITE:
  250. n = BIO_get_mem_data(rctx->mem, &p);
  251. i = BIO_write(rctx->io,
  252. p + (n - rctx->asn1_len), rctx->asn1_len);
  253. if (i <= 0)
  254. {
  255. if (BIO_should_retry(rctx->io))
  256. return -1;
  257. rctx->state = OHS_ERROR;
  258. return 0;
  259. }
  260. rctx->asn1_len -= i;
  261. if (rctx->asn1_len > 0)
  262. goto next_io;
  263. rctx->state = OHS_ASN1_FLUSH;
  264. (void)BIO_reset(rctx->mem);
  265. case OHS_ASN1_FLUSH:
  266. i = BIO_flush(rctx->io);
  267. if (i > 0)
  268. {
  269. rctx->state = OHS_FIRSTLINE;
  270. goto next_io;
  271. }
  272. if (BIO_should_retry(rctx->io))
  273. return -1;
  274. rctx->state = OHS_ERROR;
  275. return 0;
  276. case OHS_ERROR:
  277. return 0;
  278. case OHS_FIRSTLINE:
  279. case OHS_HEADERS:
  280. /* Attempt to read a line in */
  281. next_line:
  282. /* Due to &%^*$" memory BIO behaviour with BIO_gets we
  283. * have to check there's a complete line in there before
  284. * calling BIO_gets or we'll just get a partial read.
  285. */
  286. n = BIO_get_mem_data(rctx->mem, &p);
  287. if ((n <= 0) || !memchr(p, '\n', n))
  288. {
  289. if (n >= rctx->iobuflen)
  290. {
  291. rctx->state = OHS_ERROR;
  292. return 0;
  293. }
  294. goto next_io;
  295. }
  296. n = BIO_gets(rctx->mem, (char *)rctx->iobuf, rctx->iobuflen);
  297. if (n <= 0)
  298. {
  299. if (BIO_should_retry(rctx->mem))
  300. goto next_io;
  301. rctx->state = OHS_ERROR;
  302. return 0;
  303. }
  304. /* Don't allow excessive lines */
  305. if (n == rctx->iobuflen)
  306. {
  307. rctx->state = OHS_ERROR;
  308. return 0;
  309. }
  310. /* First line */
  311. if (rctx->state == OHS_FIRSTLINE)
  312. {
  313. if (parse_http_line1((char *)rctx->iobuf))
  314. {
  315. rctx->state = OHS_HEADERS;
  316. goto next_line;
  317. }
  318. else
  319. {
  320. rctx->state = OHS_ERROR;
  321. return 0;
  322. }
  323. }
  324. else
  325. {
  326. /* Look for blank line: end of headers */
  327. for (p = rctx->iobuf; *p; p++)
  328. {
  329. if ((*p != '\r') && (*p != '\n'))
  330. break;
  331. }
  332. if (*p)
  333. goto next_line;
  334. rctx->state = OHS_ASN1_HEADER;
  335. }
  336. /* Fall thru */
  337. case OHS_ASN1_HEADER:
  338. /* Now reading ASN1 header: can read at least 6 bytes which
  339. * is more than enough for any valid ASN1 SEQUENCE header
  340. */
  341. n = BIO_get_mem_data(rctx->mem, &p);
  342. if (n < 6)
  343. goto next_io;
  344. /* Check it is an ASN1 SEQUENCE */
  345. if (*p++ != (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED))
  346. {
  347. rctx->state = OHS_ERROR;
  348. return 0;
  349. }
  350. /* Check out length field */
  351. if (*p & 0x80)
  352. {
  353. n = *p & 0x7F;
  354. /* Not NDEF or excessive length */
  355. if (!n || (n > 4))
  356. {
  357. rctx->state = OHS_ERROR;
  358. return 0;
  359. }
  360. p++;
  361. rctx->asn1_len = 0;
  362. for (i = 0; i < n; i++)
  363. {
  364. rctx->asn1_len <<= 8;
  365. rctx->asn1_len |= *p++;
  366. }
  367. if (rctx->asn1_len > OCSP_MAX_REQUEST_LENGTH)
  368. {
  369. rctx->state = OHS_ERROR;
  370. return 0;
  371. }
  372. rctx->asn1_len += n + 2;
  373. }
  374. else
  375. rctx->asn1_len = *p + 2;
  376. rctx->state = OHS_ASN1_CONTENT;
  377. /* Fall thru */
  378. case OHS_ASN1_CONTENT:
  379. n = BIO_get_mem_data(rctx->mem, &p);
  380. if (n < (int)rctx->asn1_len)
  381. goto next_io;
  382. *presp = d2i_OCSP_RESPONSE(NULL, &p, rctx->asn1_len);
  383. if (*presp)
  384. {
  385. rctx->state = OHS_DONE;
  386. return 1;
  387. }
  388. rctx->state = OHS_ERROR;
  389. return 0;
  390. break;
  391. case OHS_DONE:
  392. return 1;
  393. }
  394. return 0;
  395. }
  396. /* Blocking OCSP request handler: now a special case of non-blocking I/O */
  397. OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, char *path, OCSP_REQUEST *req)
  398. {
  399. OCSP_RESPONSE *resp = NULL;
  400. OCSP_REQ_CTX *ctx;
  401. int rv;
  402. ctx = OCSP_sendreq_new(b, path, req, -1);
  403. do
  404. {
  405. rv = OCSP_sendreq_nbio(&resp, ctx);
  406. } while ((rv == -1) && BIO_should_retry(b));
  407. OCSP_REQ_CTX_free(ctx);
  408. if (rv)
  409. return resp;
  410. return NULL;
  411. }