2
0

ocsp.c 44 KB

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