ocsp.c 34 KB

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