ocsp.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /* ocsp.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999 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. #ifndef OPENSSL_NO_OCSP
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <time.h>
  63. #include "apps.h" /* needs to be included before the openssl headers! */
  64. #include <openssl/e_os2.h>
  65. #include <openssl/crypto.h>
  66. #include <openssl/err.h>
  67. #include <openssl/ssl.h>
  68. #include <openssl/evp.h>
  69. #include <openssl/bn.h>
  70. #if defined(NETWARE_CLIB)
  71. # ifdef NETWARE_BSDSOCK
  72. # include <sys/socket.h>
  73. # include <sys/bsdskt.h>
  74. # else
  75. # include <novsock2.h>
  76. # endif
  77. #elif defined(NETWARE_LIBC)
  78. # ifdef NETWARE_BSDSOCK
  79. # include <sys/select.h>
  80. # else
  81. # include <novsock2.h>
  82. # endif
  83. #endif
  84. /* Maximum leeway in validity period: default 5 minutes */
  85. #define MAX_VALIDITY_PERIOD (5 * 60)
  86. static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, const EVP_MD *cert_id_md, X509 *issuer,
  87. STACK_OF(OCSP_CERTID) *ids);
  88. static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, const EVP_MD * cert_id_md, X509 *issuer,
  89. STACK_OF(OCSP_CERTID) *ids);
  90. static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
  91. STACK *names, STACK_OF(OCSP_CERTID) *ids,
  92. long nsec, long maxage);
  93. static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, CA_DB *db,
  94. X509 *ca, X509 *rcert, EVP_PKEY *rkey,
  95. STACK_OF(X509) *rother, unsigned long flags,
  96. int nmin, int ndays);
  97. static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
  98. static BIO *init_responder(char *port);
  99. static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, char *port);
  100. static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
  101. static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
  102. OCSP_REQUEST *req, int req_timeout);
  103. #undef PROG
  104. #define PROG ocsp_main
  105. int MAIN(int, char **);
  106. int MAIN(int argc, char **argv)
  107. {
  108. ENGINE *e = NULL;
  109. char **args;
  110. char *host = NULL, *port = NULL, *path = "/";
  111. char *reqin = NULL, *respin = NULL;
  112. char *reqout = NULL, *respout = NULL;
  113. char *signfile = NULL, *keyfile = NULL;
  114. char *rsignfile = NULL, *rkeyfile = NULL;
  115. char *outfile = NULL;
  116. int add_nonce = 1, noverify = 0, use_ssl = -1;
  117. OCSP_REQUEST *req = NULL;
  118. OCSP_RESPONSE *resp = NULL;
  119. OCSP_BASICRESP *bs = NULL;
  120. X509 *issuer = NULL, *cert = NULL;
  121. X509 *signer = NULL, *rsigner = NULL;
  122. EVP_PKEY *key = NULL, *rkey = NULL;
  123. BIO *acbio = NULL, *cbio = NULL;
  124. BIO *derbio = NULL;
  125. BIO *out = NULL;
  126. int req_timeout = -1;
  127. int req_text = 0, resp_text = 0;
  128. long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
  129. char *CAfile = NULL, *CApath = NULL;
  130. X509_STORE *store = NULL;
  131. STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
  132. char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
  133. unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
  134. int ret = 1;
  135. int accept_count = -1;
  136. int badarg = 0;
  137. int i;
  138. int ignore_err = 0;
  139. STACK *reqnames = NULL;
  140. STACK_OF(OCSP_CERTID) *ids = NULL;
  141. X509 *rca_cert = NULL;
  142. char *ridx_filename = NULL;
  143. char *rca_filename = NULL;
  144. CA_DB *rdb = NULL;
  145. int nmin = 0, ndays = -1;
  146. const EVP_MD *cert_id_md = NULL;
  147. if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  148. if (!load_config(bio_err, NULL))
  149. goto end;
  150. SSL_load_error_strings();
  151. OpenSSL_add_ssl_algorithms();
  152. args = argv + 1;
  153. reqnames = sk_new_null();
  154. ids = sk_OCSP_CERTID_new_null();
  155. while (!badarg && *args && *args[0] == '-')
  156. {
  157. if (!strcmp(*args, "-out"))
  158. {
  159. if (args[1])
  160. {
  161. args++;
  162. outfile = *args;
  163. }
  164. else badarg = 1;
  165. }
  166. else if (!strcmp(*args, "-timeout"))
  167. {
  168. if (args[1])
  169. {
  170. args++;
  171. req_timeout = atol(*args);
  172. if (req_timeout < 0)
  173. {
  174. BIO_printf(bio_err,
  175. "Illegal timeout value %s\n",
  176. *args);
  177. badarg = 1;
  178. }
  179. }
  180. else badarg = 1;
  181. }
  182. else if (!strcmp(*args, "-url"))
  183. {
  184. if (args[1])
  185. {
  186. args++;
  187. if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl))
  188. {
  189. BIO_printf(bio_err, "Error parsing URL\n");
  190. badarg = 1;
  191. }
  192. }
  193. else badarg = 1;
  194. }
  195. else if (!strcmp(*args, "-host"))
  196. {
  197. if (args[1])
  198. {
  199. args++;
  200. host = *args;
  201. }
  202. else badarg = 1;
  203. }
  204. else if (!strcmp(*args, "-port"))
  205. {
  206. if (args[1])
  207. {
  208. args++;
  209. port = *args;
  210. }
  211. else badarg = 1;
  212. }
  213. else if (!strcmp(*args, "-ignore_err"))
  214. ignore_err = 1;
  215. else if (!strcmp(*args, "-noverify"))
  216. noverify = 1;
  217. else if (!strcmp(*args, "-nonce"))
  218. add_nonce = 2;
  219. else if (!strcmp(*args, "-no_nonce"))
  220. add_nonce = 0;
  221. else if (!strcmp(*args, "-resp_no_certs"))
  222. rflags |= OCSP_NOCERTS;
  223. else if (!strcmp(*args, "-resp_key_id"))
  224. rflags |= OCSP_RESPID_KEY;
  225. else if (!strcmp(*args, "-no_certs"))
  226. sign_flags |= OCSP_NOCERTS;
  227. else if (!strcmp(*args, "-no_signature_verify"))
  228. verify_flags |= OCSP_NOSIGS;
  229. else if (!strcmp(*args, "-no_cert_verify"))
  230. verify_flags |= OCSP_NOVERIFY;
  231. else if (!strcmp(*args, "-no_chain"))
  232. verify_flags |= OCSP_NOCHAIN;
  233. else if (!strcmp(*args, "-no_cert_checks"))
  234. verify_flags |= OCSP_NOCHECKS;
  235. else if (!strcmp(*args, "-no_explicit"))
  236. verify_flags |= OCSP_NOEXPLICIT;
  237. else if (!strcmp(*args, "-trust_other"))
  238. verify_flags |= OCSP_TRUSTOTHER;
  239. else if (!strcmp(*args, "-no_intern"))
  240. verify_flags |= OCSP_NOINTERN;
  241. else if (!strcmp(*args, "-text"))
  242. {
  243. req_text = 1;
  244. resp_text = 1;
  245. }
  246. else if (!strcmp(*args, "-req_text"))
  247. req_text = 1;
  248. else if (!strcmp(*args, "-resp_text"))
  249. resp_text = 1;
  250. else if (!strcmp(*args, "-reqin"))
  251. {
  252. if (args[1])
  253. {
  254. args++;
  255. reqin = *args;
  256. }
  257. else badarg = 1;
  258. }
  259. else if (!strcmp(*args, "-respin"))
  260. {
  261. if (args[1])
  262. {
  263. args++;
  264. respin = *args;
  265. }
  266. else badarg = 1;
  267. }
  268. else if (!strcmp(*args, "-signer"))
  269. {
  270. if (args[1])
  271. {
  272. args++;
  273. signfile = *args;
  274. }
  275. else badarg = 1;
  276. }
  277. else if (!strcmp (*args, "-VAfile"))
  278. {
  279. if (args[1])
  280. {
  281. args++;
  282. verify_certfile = *args;
  283. verify_flags |= OCSP_TRUSTOTHER;
  284. }
  285. else badarg = 1;
  286. }
  287. else if (!strcmp(*args, "-sign_other"))
  288. {
  289. if (args[1])
  290. {
  291. args++;
  292. sign_certfile = *args;
  293. }
  294. else badarg = 1;
  295. }
  296. else if (!strcmp(*args, "-verify_other"))
  297. {
  298. if (args[1])
  299. {
  300. args++;
  301. verify_certfile = *args;
  302. }
  303. else badarg = 1;
  304. }
  305. else if (!strcmp (*args, "-CAfile"))
  306. {
  307. if (args[1])
  308. {
  309. args++;
  310. CAfile = *args;
  311. }
  312. else badarg = 1;
  313. }
  314. else if (!strcmp (*args, "-CApath"))
  315. {
  316. if (args[1])
  317. {
  318. args++;
  319. CApath = *args;
  320. }
  321. else badarg = 1;
  322. }
  323. else if (!strcmp (*args, "-validity_period"))
  324. {
  325. if (args[1])
  326. {
  327. args++;
  328. nsec = atol(*args);
  329. if (nsec < 0)
  330. {
  331. BIO_printf(bio_err,
  332. "Illegal validity period %s\n",
  333. *args);
  334. badarg = 1;
  335. }
  336. }
  337. else badarg = 1;
  338. }
  339. else if (!strcmp (*args, "-status_age"))
  340. {
  341. if (args[1])
  342. {
  343. args++;
  344. maxage = atol(*args);
  345. if (maxage < 0)
  346. {
  347. BIO_printf(bio_err,
  348. "Illegal validity age %s\n",
  349. *args);
  350. badarg = 1;
  351. }
  352. }
  353. else badarg = 1;
  354. }
  355. else if (!strcmp(*args, "-signkey"))
  356. {
  357. if (args[1])
  358. {
  359. args++;
  360. keyfile = *args;
  361. }
  362. else badarg = 1;
  363. }
  364. else if (!strcmp(*args, "-reqout"))
  365. {
  366. if (args[1])
  367. {
  368. args++;
  369. reqout = *args;
  370. }
  371. else badarg = 1;
  372. }
  373. else if (!strcmp(*args, "-respout"))
  374. {
  375. if (args[1])
  376. {
  377. args++;
  378. respout = *args;
  379. }
  380. else badarg = 1;
  381. }
  382. else if (!strcmp(*args, "-path"))
  383. {
  384. if (args[1])
  385. {
  386. args++;
  387. path = *args;
  388. }
  389. else badarg = 1;
  390. }
  391. else if (!strcmp(*args, "-issuer"))
  392. {
  393. if (args[1])
  394. {
  395. args++;
  396. X509_free(issuer);
  397. issuer = load_cert(bio_err, *args, FORMAT_PEM,
  398. NULL, e, "issuer certificate");
  399. if(!issuer) goto end;
  400. }
  401. else badarg = 1;
  402. }
  403. else if (!strcmp (*args, "-cert"))
  404. {
  405. if (args[1])
  406. {
  407. args++;
  408. X509_free(cert);
  409. cert = load_cert(bio_err, *args, FORMAT_PEM,
  410. NULL, e, "certificate");
  411. if(!cert) goto end;
  412. if (!cert_id_md) cert_id_md = EVP_sha1();
  413. if(!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
  414. goto end;
  415. if(!sk_push(reqnames, *args))
  416. goto end;
  417. }
  418. else badarg = 1;
  419. }
  420. else if (!strcmp(*args, "-serial"))
  421. {
  422. if (args[1])
  423. {
  424. args++;
  425. if (!cert_id_md) cert_id_md = EVP_sha1();
  426. if(!add_ocsp_serial(&req, *args, cert_id_md, issuer, ids))
  427. goto end;
  428. if(!sk_push(reqnames, *args))
  429. goto end;
  430. }
  431. else badarg = 1;
  432. }
  433. else if (!strcmp(*args, "-index"))
  434. {
  435. if (args[1])
  436. {
  437. args++;
  438. ridx_filename = *args;
  439. }
  440. else badarg = 1;
  441. }
  442. else if (!strcmp(*args, "-CA"))
  443. {
  444. if (args[1])
  445. {
  446. args++;
  447. rca_filename = *args;
  448. }
  449. else badarg = 1;
  450. }
  451. else if (!strcmp (*args, "-nmin"))
  452. {
  453. if (args[1])
  454. {
  455. args++;
  456. nmin = atol(*args);
  457. if (nmin < 0)
  458. {
  459. BIO_printf(bio_err,
  460. "Illegal update period %s\n",
  461. *args);
  462. badarg = 1;
  463. }
  464. }
  465. if (ndays == -1)
  466. ndays = 0;
  467. else badarg = 1;
  468. }
  469. else if (!strcmp (*args, "-nrequest"))
  470. {
  471. if (args[1])
  472. {
  473. args++;
  474. accept_count = atol(*args);
  475. if (accept_count < 0)
  476. {
  477. BIO_printf(bio_err,
  478. "Illegal accept count %s\n",
  479. *args);
  480. badarg = 1;
  481. }
  482. }
  483. else badarg = 1;
  484. }
  485. else if (!strcmp (*args, "-ndays"))
  486. {
  487. if (args[1])
  488. {
  489. args++;
  490. ndays = atol(*args);
  491. if (ndays < 0)
  492. {
  493. BIO_printf(bio_err,
  494. "Illegal update period %s\n",
  495. *args);
  496. badarg = 1;
  497. }
  498. }
  499. else badarg = 1;
  500. }
  501. else if (!strcmp(*args, "-rsigner"))
  502. {
  503. if (args[1])
  504. {
  505. args++;
  506. rsignfile = *args;
  507. }
  508. else badarg = 1;
  509. }
  510. else if (!strcmp(*args, "-rkey"))
  511. {
  512. if (args[1])
  513. {
  514. args++;
  515. rkeyfile = *args;
  516. }
  517. else badarg = 1;
  518. }
  519. else if (!strcmp(*args, "-rother"))
  520. {
  521. if (args[1])
  522. {
  523. args++;
  524. rcertfile = *args;
  525. }
  526. else badarg = 1;
  527. }
  528. else if ((cert_id_md = EVP_get_digestbyname((*args)+1))==NULL)
  529. {
  530. badarg = 1;
  531. }
  532. args++;
  533. }
  534. /* Have we anything to do? */
  535. if (!req && !reqin && !respin && !(port && ridx_filename)) badarg = 1;
  536. if (badarg)
  537. {
  538. BIO_printf (bio_err, "OCSP utility\n");
  539. BIO_printf (bio_err, "Usage ocsp [options]\n");
  540. BIO_printf (bio_err, "where options are\n");
  541. BIO_printf (bio_err, "-out file output filename\n");
  542. BIO_printf (bio_err, "-issuer file issuer certificate\n");
  543. BIO_printf (bio_err, "-cert file certificate to check\n");
  544. BIO_printf (bio_err, "-serial n serial number to check\n");
  545. BIO_printf (bio_err, "-signer file certificate to sign OCSP request with\n");
  546. BIO_printf (bio_err, "-signkey file private key to sign OCSP request with\n");
  547. BIO_printf (bio_err, "-sign_other file additional certificates to include in signed request\n");
  548. BIO_printf (bio_err, "-no_certs don't include any certificates in signed request\n");
  549. BIO_printf (bio_err, "-req_text print text form of request\n");
  550. BIO_printf (bio_err, "-resp_text print text form of response\n");
  551. BIO_printf (bio_err, "-text print text form of request and response\n");
  552. BIO_printf (bio_err, "-reqout file write DER encoded OCSP request to \"file\"\n");
  553. BIO_printf (bio_err, "-respout file write DER encoded OCSP reponse to \"file\"\n");
  554. BIO_printf (bio_err, "-reqin file read DER encoded OCSP request from \"file\"\n");
  555. BIO_printf (bio_err, "-respin file read DER encoded OCSP reponse from \"file\"\n");
  556. BIO_printf (bio_err, "-nonce add OCSP nonce to request\n");
  557. BIO_printf (bio_err, "-no_nonce don't add OCSP nonce to request\n");
  558. BIO_printf (bio_err, "-url URL OCSP responder URL\n");
  559. BIO_printf (bio_err, "-host host:n send OCSP request to host on port n\n");
  560. BIO_printf (bio_err, "-path path to use in OCSP request\n");
  561. BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
  562. BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
  563. BIO_printf (bio_err, "-VAfile file validator certificates file\n");
  564. BIO_printf (bio_err, "-validity_period n maximum validity discrepancy in seconds\n");
  565. BIO_printf (bio_err, "-status_age n maximum status age in seconds\n");
  566. BIO_printf (bio_err, "-noverify don't verify response at all\n");
  567. BIO_printf (bio_err, "-verify_other file additional certificates to search for signer\n");
  568. BIO_printf (bio_err, "-trust_other don't verify additional certificates\n");
  569. BIO_printf (bio_err, "-no_intern don't search certificates contained in response for signer\n");
  570. BIO_printf (bio_err, "-no_signature_verify don't check signature on response\n");
  571. BIO_printf (bio_err, "-no_cert_verify don't check signing certificate\n");
  572. BIO_printf (bio_err, "-no_chain don't chain verify response\n");
  573. BIO_printf (bio_err, "-no_cert_checks don't do additional checks on signing certificate\n");
  574. BIO_printf (bio_err, "-port num port to run responder on\n");
  575. BIO_printf (bio_err, "-index file certificate status index file\n");
  576. BIO_printf (bio_err, "-CA file CA certificate\n");
  577. BIO_printf (bio_err, "-rsigner file responder certificate to sign responses with\n");
  578. BIO_printf (bio_err, "-rkey file responder key to sign responses with\n");
  579. BIO_printf (bio_err, "-rother file other certificates to include in response\n");
  580. BIO_printf (bio_err, "-resp_no_certs don't include any certificates in response\n");
  581. BIO_printf (bio_err, "-nmin n number of minutes before next update\n");
  582. BIO_printf (bio_err, "-ndays n number of days before next update\n");
  583. BIO_printf (bio_err, "-resp_key_id identify reponse by signing certificate key ID\n");
  584. BIO_printf (bio_err, "-nrequest n number of requests to accept (default unlimited)\n");
  585. BIO_printf (bio_err, "-<dgst alg> use specified digest in the request");
  586. goto end;
  587. }
  588. if(outfile) out = BIO_new_file(outfile, "w");
  589. else out = BIO_new_fp(stdout, BIO_NOCLOSE);
  590. if(!out)
  591. {
  592. BIO_printf(bio_err, "Error opening output file\n");
  593. goto end;
  594. }
  595. if (!req && (add_nonce != 2)) add_nonce = 0;
  596. if (!req && reqin)
  597. {
  598. derbio = BIO_new_file(reqin, "rb");
  599. if (!derbio)
  600. {
  601. BIO_printf(bio_err, "Error Opening OCSP request file\n");
  602. goto end;
  603. }
  604. req = d2i_OCSP_REQUEST_bio(derbio, NULL);
  605. BIO_free(derbio);
  606. if(!req)
  607. {
  608. BIO_printf(bio_err, "Error reading OCSP request\n");
  609. goto end;
  610. }
  611. }
  612. if (!req && port)
  613. {
  614. acbio = init_responder(port);
  615. if (!acbio)
  616. goto end;
  617. }
  618. if (rsignfile && !rdb)
  619. {
  620. if (!rkeyfile) rkeyfile = rsignfile;
  621. rsigner = load_cert(bio_err, rsignfile, FORMAT_PEM,
  622. NULL, e, "responder certificate");
  623. if (!rsigner)
  624. {
  625. BIO_printf(bio_err, "Error loading responder certificate\n");
  626. goto end;
  627. }
  628. rca_cert = load_cert(bio_err, rca_filename, FORMAT_PEM,
  629. NULL, e, "CA certificate");
  630. if (rcertfile)
  631. {
  632. rother = load_certs(bio_err, rcertfile, FORMAT_PEM,
  633. NULL, e, "responder other certificates");
  634. if (!rother) goto end;
  635. }
  636. rkey = load_key(bio_err, rkeyfile, FORMAT_PEM, 0, NULL, NULL,
  637. "responder private key");
  638. if (!rkey)
  639. goto end;
  640. }
  641. if(acbio)
  642. BIO_printf(bio_err, "Waiting for OCSP client connections...\n");
  643. redo_accept:
  644. if (acbio)
  645. {
  646. if (!do_responder(&req, &cbio, acbio, port))
  647. goto end;
  648. if (!req)
  649. {
  650. resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
  651. send_ocsp_response(cbio, resp);
  652. goto done_resp;
  653. }
  654. }
  655. if (!req && (signfile || reqout || host || add_nonce || ridx_filename))
  656. {
  657. BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
  658. goto end;
  659. }
  660. if (req && add_nonce) OCSP_request_add1_nonce(req, NULL, -1);
  661. if (signfile)
  662. {
  663. if (!keyfile) keyfile = signfile;
  664. signer = load_cert(bio_err, signfile, FORMAT_PEM,
  665. NULL, e, "signer certificate");
  666. if (!signer)
  667. {
  668. BIO_printf(bio_err, "Error loading signer certificate\n");
  669. goto end;
  670. }
  671. if (sign_certfile)
  672. {
  673. sign_other = load_certs(bio_err, sign_certfile, FORMAT_PEM,
  674. NULL, e, "signer certificates");
  675. if (!sign_other) goto end;
  676. }
  677. key = load_key(bio_err, keyfile, FORMAT_PEM, 0, NULL, NULL,
  678. "signer private key");
  679. if (!key)
  680. goto end;
  681. if (!OCSP_request_sign(req, signer, key, NULL, sign_other, sign_flags))
  682. {
  683. BIO_printf(bio_err, "Error signing OCSP request\n");
  684. goto end;
  685. }
  686. }
  687. if (req_text && req) OCSP_REQUEST_print(out, req, 0);
  688. if (reqout)
  689. {
  690. derbio = BIO_new_file(reqout, "wb");
  691. if(!derbio)
  692. {
  693. BIO_printf(bio_err, "Error opening file %s\n", reqout);
  694. goto end;
  695. }
  696. i2d_OCSP_REQUEST_bio(derbio, req);
  697. BIO_free(derbio);
  698. }
  699. if (ridx_filename && (!rkey || !rsigner || !rca_cert))
  700. {
  701. BIO_printf(bio_err, "Need a responder certificate, key and CA for this operation!\n");
  702. goto end;
  703. }
  704. if (ridx_filename && !rdb)
  705. {
  706. rdb = load_index(ridx_filename, NULL);
  707. if (!rdb) goto end;
  708. if (!index_index(rdb)) goto end;
  709. }
  710. if (rdb)
  711. {
  712. i = make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey, rother, rflags, nmin, ndays);
  713. if (cbio)
  714. send_ocsp_response(cbio, resp);
  715. }
  716. else if (host)
  717. {
  718. #ifndef OPENSSL_NO_SOCK
  719. resp = process_responder(bio_err, req, host, path,
  720. port, use_ssl, req_timeout);
  721. if (!resp)
  722. goto end;
  723. #else
  724. BIO_printf(bio_err, "Error creating connect BIO - sockets not supported.\n");
  725. goto end;
  726. #endif
  727. }
  728. else if (respin)
  729. {
  730. derbio = BIO_new_file(respin, "rb");
  731. if (!derbio)
  732. {
  733. BIO_printf(bio_err, "Error Opening OCSP response file\n");
  734. goto end;
  735. }
  736. resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
  737. BIO_free(derbio);
  738. if(!resp)
  739. {
  740. BIO_printf(bio_err, "Error reading OCSP response\n");
  741. goto end;
  742. }
  743. }
  744. else
  745. {
  746. ret = 0;
  747. goto end;
  748. }
  749. done_resp:
  750. if (respout)
  751. {
  752. derbio = BIO_new_file(respout, "wb");
  753. if(!derbio)
  754. {
  755. BIO_printf(bio_err, "Error opening file %s\n", respout);
  756. goto end;
  757. }
  758. i2d_OCSP_RESPONSE_bio(derbio, resp);
  759. BIO_free(derbio);
  760. }
  761. i = OCSP_response_status(resp);
  762. if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL)
  763. {
  764. BIO_printf(out, "Responder Error: %s (%d)\n",
  765. OCSP_response_status_str(i), i);
  766. if (ignore_err)
  767. goto redo_accept;
  768. ret = 0;
  769. goto end;
  770. }
  771. if (resp_text) OCSP_RESPONSE_print(out, resp, 0);
  772. /* If running as responder don't verify our own response */
  773. if (cbio)
  774. {
  775. if (accept_count > 0)
  776. accept_count--;
  777. /* Redo if more connections needed */
  778. if (accept_count)
  779. {
  780. BIO_free_all(cbio);
  781. cbio = NULL;
  782. OCSP_REQUEST_free(req);
  783. req = NULL;
  784. OCSP_RESPONSE_free(resp);
  785. resp = NULL;
  786. goto redo_accept;
  787. }
  788. goto end;
  789. }
  790. if (!store)
  791. store = setup_verify(bio_err, CAfile, CApath);
  792. if (!store)
  793. goto end;
  794. if (verify_certfile)
  795. {
  796. verify_other = load_certs(bio_err, verify_certfile, FORMAT_PEM,
  797. NULL, e, "validator certificate");
  798. if (!verify_other) goto end;
  799. }
  800. bs = OCSP_response_get1_basic(resp);
  801. if (!bs)
  802. {
  803. BIO_printf(bio_err, "Error parsing response\n");
  804. goto end;
  805. }
  806. if (!noverify)
  807. {
  808. if (req && ((i = OCSP_check_nonce(req, bs)) <= 0))
  809. {
  810. if (i == -1)
  811. BIO_printf(bio_err, "WARNING: no nonce in response\n");
  812. else
  813. {
  814. BIO_printf(bio_err, "Nonce Verify error\n");
  815. goto end;
  816. }
  817. }
  818. i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
  819. if (i < 0) i = OCSP_basic_verify(bs, NULL, store, 0);
  820. if(i <= 0)
  821. {
  822. BIO_printf(bio_err, "Response Verify Failure\n");
  823. ERR_print_errors(bio_err);
  824. }
  825. else
  826. BIO_printf(bio_err, "Response verify OK\n");
  827. }
  828. if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
  829. goto end;
  830. ret = 0;
  831. end:
  832. ERR_print_errors(bio_err);
  833. X509_free(signer);
  834. X509_STORE_free(store);
  835. EVP_PKEY_free(key);
  836. EVP_PKEY_free(rkey);
  837. X509_free(issuer);
  838. X509_free(cert);
  839. X509_free(rsigner);
  840. X509_free(rca_cert);
  841. free_index(rdb);
  842. BIO_free_all(cbio);
  843. BIO_free_all(acbio);
  844. BIO_free(out);
  845. OCSP_REQUEST_free(req);
  846. OCSP_RESPONSE_free(resp);
  847. OCSP_BASICRESP_free(bs);
  848. sk_free(reqnames);
  849. sk_OCSP_CERTID_free(ids);
  850. sk_X509_pop_free(sign_other, X509_free);
  851. sk_X509_pop_free(verify_other, X509_free);
  852. if (use_ssl != -1)
  853. {
  854. OPENSSL_free(host);
  855. OPENSSL_free(port);
  856. OPENSSL_free(path);
  857. }
  858. OPENSSL_EXIT(ret);
  859. }
  860. static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, const EVP_MD *cert_id_md,X509 *issuer,
  861. STACK_OF(OCSP_CERTID) *ids)
  862. {
  863. OCSP_CERTID *id;
  864. if(!issuer)
  865. {
  866. BIO_printf(bio_err, "No issuer certificate specified\n");
  867. return 0;
  868. }
  869. if(!*req) *req = OCSP_REQUEST_new();
  870. if(!*req) goto err;
  871. id = OCSP_cert_to_id(cert_id_md, cert, issuer);
  872. if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
  873. if(!OCSP_request_add0_id(*req, id)) goto err;
  874. return 1;
  875. err:
  876. BIO_printf(bio_err, "Error Creating OCSP request\n");
  877. return 0;
  878. }
  879. static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,const EVP_MD *cert_id_md, X509 *issuer,
  880. STACK_OF(OCSP_CERTID) *ids)
  881. {
  882. OCSP_CERTID *id;
  883. X509_NAME *iname;
  884. ASN1_BIT_STRING *ikey;
  885. ASN1_INTEGER *sno;
  886. if(!issuer)
  887. {
  888. BIO_printf(bio_err, "No issuer certificate specified\n");
  889. return 0;
  890. }
  891. if(!*req) *req = OCSP_REQUEST_new();
  892. if(!*req) goto err;
  893. iname = X509_get_subject_name(issuer);
  894. ikey = X509_get0_pubkey_bitstr(issuer);
  895. sno = s2i_ASN1_INTEGER(NULL, serial);
  896. if(!sno)
  897. {
  898. BIO_printf(bio_err, "Error converting serial number %s\n", serial);
  899. return 0;
  900. }
  901. id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
  902. ASN1_INTEGER_free(sno);
  903. if(!id || !sk_OCSP_CERTID_push(ids, id)) goto err;
  904. if(!OCSP_request_add0_id(*req, id)) goto err;
  905. return 1;
  906. err:
  907. BIO_printf(bio_err, "Error Creating OCSP request\n");
  908. return 0;
  909. }
  910. static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
  911. STACK *names, STACK_OF(OCSP_CERTID) *ids,
  912. long nsec, long maxage)
  913. {
  914. OCSP_CERTID *id;
  915. char *name;
  916. int i;
  917. int status, reason;
  918. ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
  919. if (!bs || !req || !sk_num(names) || !sk_OCSP_CERTID_num(ids))
  920. return 1;
  921. for (i = 0; i < sk_OCSP_CERTID_num(ids); i++)
  922. {
  923. id = sk_OCSP_CERTID_value(ids, i);
  924. name = sk_value(names, i);
  925. BIO_printf(out, "%s: ", name);
  926. if(!OCSP_resp_find_status(bs, id, &status, &reason,
  927. &rev, &thisupd, &nextupd))
  928. {
  929. BIO_puts(out, "ERROR: No Status found.\n");
  930. continue;
  931. }
  932. /* Check validity: if invalid write to output BIO so we
  933. * know which response this refers to.
  934. */
  935. if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage))
  936. {
  937. BIO_puts(out, "WARNING: Status times invalid.\n");
  938. ERR_print_errors(out);
  939. }
  940. BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
  941. BIO_puts(out, "\tThis Update: ");
  942. ASN1_GENERALIZEDTIME_print(out, thisupd);
  943. BIO_puts(out, "\n");
  944. if(nextupd)
  945. {
  946. BIO_puts(out, "\tNext Update: ");
  947. ASN1_GENERALIZEDTIME_print(out, nextupd);
  948. BIO_puts(out, "\n");
  949. }
  950. if (status != V_OCSP_CERTSTATUS_REVOKED)
  951. continue;
  952. if (reason != -1)
  953. BIO_printf(out, "\tReason: %s\n",
  954. OCSP_crl_reason_str(reason));
  955. BIO_puts(out, "\tRevocation Time: ");
  956. ASN1_GENERALIZEDTIME_print(out, rev);
  957. BIO_puts(out, "\n");
  958. }
  959. return 1;
  960. }
  961. static int make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, CA_DB *db,
  962. X509 *ca, X509 *rcert, EVP_PKEY *rkey,
  963. STACK_OF(X509) *rother, unsigned long flags,
  964. int nmin, int ndays)
  965. {
  966. ASN1_TIME *thisupd = NULL, *nextupd = NULL;
  967. OCSP_CERTID *cid, *ca_id = NULL;
  968. OCSP_BASICRESP *bs = NULL;
  969. int i, id_count, ret = 1;
  970. id_count = OCSP_request_onereq_count(req);
  971. if (id_count <= 0)
  972. {
  973. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
  974. goto end;
  975. }
  976. bs = OCSP_BASICRESP_new();
  977. thisupd = X509_gmtime_adj(NULL, 0);
  978. if (ndays != -1)
  979. nextupd = X509_gmtime_adj(NULL, nmin * 60 + ndays * 3600 * 24 );
  980. /* Examine each certificate id in the request */
  981. for (i = 0; i < id_count; i++)
  982. {
  983. OCSP_ONEREQ *one;
  984. ASN1_INTEGER *serial;
  985. char **inf;
  986. ASN1_OBJECT *cert_id_md_oid;
  987. const EVP_MD *cert_id_md;
  988. one = OCSP_request_onereq_get0(req, i);
  989. cid = OCSP_onereq_get0_id(one);
  990. OCSP_id_get0_info(NULL,&cert_id_md_oid, NULL,NULL, cid);
  991. cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
  992. if (! cert_id_md)
  993. {
  994. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  995. NULL);
  996. goto end;
  997. }
  998. if (ca_id) OCSP_CERTID_free(ca_id);
  999. ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca);
  1000. /* Is this request about our CA? */
  1001. if (OCSP_id_issuer_cmp(ca_id, cid))
  1002. {
  1003. OCSP_basic_add1_status(bs, cid,
  1004. V_OCSP_CERTSTATUS_UNKNOWN,
  1005. 0, NULL,
  1006. thisupd, nextupd);
  1007. continue;
  1008. }
  1009. OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
  1010. inf = lookup_serial(db, serial);
  1011. if (!inf)
  1012. OCSP_basic_add1_status(bs, cid,
  1013. V_OCSP_CERTSTATUS_UNKNOWN,
  1014. 0, NULL,
  1015. thisupd, nextupd);
  1016. else if (inf[DB_type][0] == DB_TYPE_VAL)
  1017. OCSP_basic_add1_status(bs, cid,
  1018. V_OCSP_CERTSTATUS_GOOD,
  1019. 0, NULL,
  1020. thisupd, nextupd);
  1021. else if (inf[DB_type][0] == DB_TYPE_REV)
  1022. {
  1023. ASN1_OBJECT *inst = NULL;
  1024. ASN1_TIME *revtm = NULL;
  1025. ASN1_GENERALIZEDTIME *invtm = NULL;
  1026. OCSP_SINGLERESP *single;
  1027. int reason = -1;
  1028. unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
  1029. single = OCSP_basic_add1_status(bs, cid,
  1030. V_OCSP_CERTSTATUS_REVOKED,
  1031. reason, revtm,
  1032. thisupd, nextupd);
  1033. if (invtm)
  1034. OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, invtm, 0, 0);
  1035. else if (inst)
  1036. OCSP_SINGLERESP_add1_ext_i2d(single, NID_hold_instruction_code, inst, 0, 0);
  1037. ASN1_OBJECT_free(inst);
  1038. ASN1_TIME_free(revtm);
  1039. ASN1_GENERALIZEDTIME_free(invtm);
  1040. }
  1041. }
  1042. OCSP_copy_nonce(bs, req);
  1043. OCSP_basic_sign(bs, rcert, rkey, NULL, rother, flags);
  1044. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
  1045. end:
  1046. ASN1_TIME_free(thisupd);
  1047. ASN1_TIME_free(nextupd);
  1048. OCSP_CERTID_free(ca_id);
  1049. OCSP_BASICRESP_free(bs);
  1050. return ret;
  1051. }
  1052. static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
  1053. {
  1054. int i;
  1055. BIGNUM *bn = NULL;
  1056. char *itmp, *row[DB_NUMBER],**rrow;
  1057. for (i = 0; i < DB_NUMBER; i++) row[i] = NULL;
  1058. bn = ASN1_INTEGER_to_BN(ser,NULL);
  1059. OPENSSL_assert(bn); /* FIXME: should report an error at this point and abort */
  1060. if (BN_is_zero(bn))
  1061. itmp = BUF_strdup("00");
  1062. else
  1063. itmp = BN_bn2hex(bn);
  1064. row[DB_serial] = itmp;
  1065. BN_free(bn);
  1066. rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
  1067. OPENSSL_free(itmp);
  1068. return rrow;
  1069. }
  1070. /* Quick and dirty OCSP server: read in and parse input request */
  1071. static BIO *init_responder(char *port)
  1072. {
  1073. BIO *acbio = NULL, *bufbio = NULL;
  1074. bufbio = BIO_new(BIO_f_buffer());
  1075. if (!bufbio)
  1076. goto err;
  1077. #ifndef OPENSSL_NO_SOCK
  1078. acbio = BIO_new_accept(port);
  1079. #else
  1080. BIO_printf(bio_err, "Error setting up accept BIO - sockets not supported.\n");
  1081. #endif
  1082. if (!acbio)
  1083. goto err;
  1084. BIO_set_accept_bios(acbio, bufbio);
  1085. bufbio = NULL;
  1086. if (BIO_do_accept(acbio) <= 0)
  1087. {
  1088. BIO_printf(bio_err, "Error setting up accept BIO\n");
  1089. ERR_print_errors(bio_err);
  1090. goto err;
  1091. }
  1092. return acbio;
  1093. err:
  1094. BIO_free_all(acbio);
  1095. BIO_free(bufbio);
  1096. return NULL;
  1097. }
  1098. static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, char *port)
  1099. {
  1100. int have_post = 0, len;
  1101. OCSP_REQUEST *req = NULL;
  1102. char inbuf[1024];
  1103. BIO *cbio = NULL;
  1104. if (BIO_do_accept(acbio) <= 0)
  1105. {
  1106. BIO_printf(bio_err, "Error accepting connection\n");
  1107. ERR_print_errors(bio_err);
  1108. return 0;
  1109. }
  1110. cbio = BIO_pop(acbio);
  1111. *pcbio = cbio;
  1112. for(;;)
  1113. {
  1114. len = BIO_gets(cbio, inbuf, sizeof inbuf);
  1115. if (len <= 0)
  1116. return 1;
  1117. /* Look for "POST" signalling start of query */
  1118. if (!have_post)
  1119. {
  1120. if(strncmp(inbuf, "POST", 4))
  1121. {
  1122. BIO_printf(bio_err, "Invalid request\n");
  1123. return 1;
  1124. }
  1125. have_post = 1;
  1126. }
  1127. /* Look for end of headers */
  1128. if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
  1129. break;
  1130. }
  1131. /* Try to read OCSP request */
  1132. req = d2i_OCSP_REQUEST_bio(cbio, NULL);
  1133. if (!req)
  1134. {
  1135. BIO_printf(bio_err, "Error parsing OCSP request\n");
  1136. ERR_print_errors(bio_err);
  1137. }
  1138. *preq = req;
  1139. return 1;
  1140. }
  1141. static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
  1142. {
  1143. char http_resp[] =
  1144. "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
  1145. "Content-Length: %d\r\n\r\n";
  1146. if (!cbio)
  1147. return 0;
  1148. BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
  1149. i2d_OCSP_RESPONSE_bio(cbio, resp);
  1150. (void)BIO_flush(cbio);
  1151. return 1;
  1152. }
  1153. static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
  1154. OCSP_REQUEST *req, int req_timeout)
  1155. {
  1156. int fd;
  1157. int rv;
  1158. OCSP_REQ_CTX *ctx = NULL;
  1159. OCSP_RESPONSE *rsp = NULL;
  1160. fd_set confds;
  1161. struct timeval tv;
  1162. if (req_timeout != -1)
  1163. BIO_set_nbio(cbio, 1);
  1164. rv = BIO_do_connect(cbio);
  1165. if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio)))
  1166. {
  1167. BIO_puts(err, "Error connecting BIO\n");
  1168. return NULL;
  1169. }
  1170. if (req_timeout == -1)
  1171. return OCSP_sendreq_bio(cbio, path, req);
  1172. if (BIO_get_fd(cbio, &fd) <= 0)
  1173. {
  1174. BIO_puts(err, "Can't get connection fd\n");
  1175. goto err;
  1176. }
  1177. if (rv <= 0)
  1178. {
  1179. FD_ZERO(&confds);
  1180. openssl_fdset(fd, &confds);
  1181. tv.tv_usec = 0;
  1182. tv.tv_sec = req_timeout;
  1183. rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
  1184. if (rv == 0)
  1185. {
  1186. BIO_puts(err, "Timeout on connect\n");
  1187. return NULL;
  1188. }
  1189. }
  1190. ctx = OCSP_sendreq_new(cbio, path, req, -1);
  1191. if (!ctx)
  1192. return NULL;
  1193. for (;;)
  1194. {
  1195. rv = OCSP_sendreq_nbio(&rsp, ctx);
  1196. if (rv != -1)
  1197. break;
  1198. FD_ZERO(&confds);
  1199. openssl_fdset(fd, &confds);
  1200. tv.tv_usec = 0;
  1201. tv.tv_sec = req_timeout;
  1202. if (BIO_should_read(cbio))
  1203. rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
  1204. else if (BIO_should_write(cbio))
  1205. rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
  1206. else
  1207. {
  1208. BIO_puts(err, "Unexpected retry condition\n");
  1209. goto err;
  1210. }
  1211. if (rv == 0)
  1212. {
  1213. BIO_puts(err, "Timeout on request\n");
  1214. break;
  1215. }
  1216. if (rv == -1)
  1217. {
  1218. BIO_puts(err, "Select error\n");
  1219. break;
  1220. }
  1221. }
  1222. err:
  1223. if (ctx)
  1224. OCSP_REQ_CTX_free(ctx);
  1225. return rsp;
  1226. }
  1227. OCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req,
  1228. char *host, char *path, char *port, int use_ssl,
  1229. int req_timeout)
  1230. {
  1231. BIO *cbio = NULL;
  1232. SSL_CTX *ctx = NULL;
  1233. OCSP_RESPONSE *resp = NULL;
  1234. cbio = BIO_new_connect(host);
  1235. if (!cbio)
  1236. {
  1237. BIO_printf(err, "Error creating connect BIO\n");
  1238. goto end;
  1239. }
  1240. if (port) BIO_set_conn_port(cbio, port);
  1241. if (use_ssl == 1)
  1242. {
  1243. BIO *sbio;
  1244. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  1245. ctx = SSL_CTX_new(SSLv23_client_method());
  1246. #elif !defined(OPENSSL_NO_SSL3)
  1247. ctx = SSL_CTX_new(SSLv3_client_method());
  1248. #elif !defined(OPENSSL_NO_SSL2)
  1249. ctx = SSL_CTX_new(SSLv2_client_method());
  1250. #else
  1251. BIO_printf(err, "SSL is disabled\n");
  1252. goto end;
  1253. #endif
  1254. if (ctx == NULL)
  1255. {
  1256. BIO_printf(err, "Error creating SSL context.\n");
  1257. goto end;
  1258. }
  1259. SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
  1260. sbio = BIO_new_ssl(ctx, 1);
  1261. cbio = BIO_push(sbio, cbio);
  1262. }
  1263. resp = query_responder(err, cbio, path, req, req_timeout);
  1264. if (!resp)
  1265. BIO_printf(bio_err, "Error querying OCSP responsder\n");
  1266. end:
  1267. if (ctx)
  1268. SSL_CTX_free(ctx);
  1269. if (cbio)
  1270. BIO_free_all(cbio);
  1271. return resp;
  1272. }
  1273. #endif