ocsp.c 43 KB

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