2
0

ocsp.c 44 KB

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