2
0

ocsp.c 33 KB

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