ocsp_ht.c 13 KB

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