ocsp.c 30 KB

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