ocsp_ht.c 15 KB

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