ocsp.c 40 KB

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