ocsp_ht.c 15 KB

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