ocsp.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/opensslconf.h>
  10. #ifdef OPENSSL_SYS_VMS
  11. /* So fd_set and friends get properly defined on OpenVMS */
  12. # define _XOPEN_SOURCE_EXTENDED
  13. #endif
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <ctype.h>
  19. /* Needs to be included before the openssl headers */
  20. #include "apps.h"
  21. #include "http_server.h"
  22. #include "progs.h"
  23. #include "internal/sockets.h"
  24. #include <openssl/e_os2.h>
  25. #include <openssl/crypto.h>
  26. #include <openssl/err.h>
  27. #include <openssl/ssl.h>
  28. #include <openssl/evp.h>
  29. #include <openssl/bn.h>
  30. #include <openssl/x509v3.h>
  31. DEFINE_STACK_OF(OCSP_CERTID)
  32. DEFINE_STACK_OF(CONF_VALUE)
  33. DEFINE_STACK_OF(X509)
  34. DEFINE_STACK_OF_STRING()
  35. #if defined(OPENSSL_SYS_VXWORKS)
  36. /* not supported */
  37. int setpgid(pid_t pid, pid_t pgid)
  38. {
  39. errno = ENOSYS;
  40. return 0;
  41. }
  42. /* not supported */
  43. pid_t fork(void)
  44. {
  45. errno = ENOSYS;
  46. return (pid_t) -1;
  47. }
  48. #endif
  49. /* Maximum leeway in validity period: default 5 minutes */
  50. #define MAX_VALIDITY_PERIOD (5 * 60)
  51. static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
  52. const EVP_MD *cert_id_md, X509 *issuer,
  53. STACK_OF(OCSP_CERTID) *ids);
  54. static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
  55. const EVP_MD *cert_id_md, X509 *issuer,
  56. STACK_OF(OCSP_CERTID) *ids);
  57. static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
  58. STACK_OF(OPENSSL_STRING) *names,
  59. STACK_OF(OCSP_CERTID) *ids, long nsec,
  60. long maxage);
  61. static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
  62. CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
  63. EVP_PKEY *rkey, const EVP_MD *md,
  64. STACK_OF(OPENSSL_STRING) *sigopts,
  65. STACK_OF(X509) *rother, unsigned long flags,
  66. int nmin, int ndays, int badsig,
  67. const EVP_MD *resp_md);
  68. static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
  69. static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
  70. int timeout);
  71. static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp);
  72. static char *prog;
  73. #ifdef HTTP_DAEMON
  74. static int index_changed(CA_DB *);
  75. #endif
  76. typedef enum OPTION_choice {
  77. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  78. OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
  79. OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
  80. OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
  81. OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
  82. OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
  83. OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
  84. OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
  85. OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE,
  86. OPT_NOCAPATH, OPT_NOCASTORE,
  87. OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
  88. OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
  89. OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
  90. OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
  91. OPT_PASSIN,
  92. OPT_RCID,
  93. OPT_V_ENUM,
  94. OPT_MD,
  95. OPT_MULTI, OPT_PROV_ENUM
  96. } OPTION_CHOICE;
  97. const OPTIONS ocsp_options[] = {
  98. OPT_SECTION("General"),
  99. {"help", OPT_HELP, '-', "Display this summary"},
  100. {"ignore_err", OPT_IGNORE_ERR, '-',
  101. "Ignore error on OCSP request or response and continue running"},
  102. {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
  103. {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
  104. {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
  105. {"no-CAfile", OPT_NOCAFILE, '-',
  106. "Do not load the default certificates file"},
  107. {"no-CApath", OPT_NOCAPATH, '-',
  108. "Do not load certificates from the default certificates directory"},
  109. {"no-CAstore", OPT_NOCASTORE, '-',
  110. "Do not load certificates from the default certificates store"},
  111. OPT_SECTION("Responder"),
  112. {"timeout", OPT_TIMEOUT, 'p',
  113. "Connection timeout (in seconds) to the OCSP responder"},
  114. {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
  115. "Don't include any certificates in response"},
  116. #ifdef HTTP_DAEMON
  117. {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
  118. #endif
  119. {"no_certs", OPT_NO_CERTS, '-',
  120. "Don't include any certificates in signed request"},
  121. {"badsig", OPT_BADSIG, '-',
  122. "Corrupt last byte of loaded OSCP response signature (for test)"},
  123. {"CA", OPT_CA, '<', "CA certificate"},
  124. {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
  125. {"nrequest", OPT_REQUEST, 'p',
  126. "Number of requests to accept (default unlimited)"},
  127. {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
  128. {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
  129. {"sign_other", OPT_SIGN_OTHER, '<',
  130. "Additional certificates to include in signed request"},
  131. {"index", OPT_INDEX, '<', "Certificate status index file"},
  132. {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
  133. {"rsigner", OPT_RSIGNER, '<',
  134. "Responder certificate to sign responses with"},
  135. {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
  136. {"passin", OPT_PASSIN, 's', "Responder key pass phrase source"},
  137. {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
  138. {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
  139. {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"},
  140. {"header", OPT_HEADER, 's', "key=value header to add"},
  141. {"rcid", OPT_RCID, 's', "Use specified algorithm for cert id in response"},
  142. {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
  143. OPT_SECTION("Client"),
  144. {"url", OPT_URL, 's', "Responder URL"},
  145. {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
  146. {"port", OPT_PORT, 'p', "Port to run responder on"},
  147. {"out", OPT_OUTFILE, '>', "Output filename"},
  148. {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
  149. {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
  150. {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
  151. {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
  152. "Don't check signature on response"},
  153. {"resp_key_id", OPT_RESP_KEY_ID, '-',
  154. "Identify response by signing certificate key ID"},
  155. {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
  156. "Don't check signing certificate"},
  157. {"text", OPT_TEXT, '-', "Print text form of request and response"},
  158. {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
  159. {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
  160. {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
  161. {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
  162. "Don't do additional checks on signing certificate"},
  163. {"no_explicit", OPT_NO_EXPLICIT, '-',
  164. "Do not explicitly check the chain, just verify the root"},
  165. {"trust_other", OPT_TRUST_OTHER, '-',
  166. "Don't verify additional certificates"},
  167. {"no_intern", OPT_NO_INTERN, '-',
  168. "Don't search certificates contained in response for signer"},
  169. {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
  170. {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
  171. {"verify_other", OPT_VERIFY_OTHER, '<',
  172. "Additional certificates to search for signer"},
  173. {"path", OPT_PATH, 's', "Path to use in OCSP request"},
  174. {"cert", OPT_CERT, '<', "Certificate to check"},
  175. {"serial", OPT_SERIAL, 's', "Serial number to check"},
  176. {"validity_period", OPT_VALIDITY_PERIOD, 'u',
  177. "Maximum validity discrepancy in seconds"},
  178. {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
  179. {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
  180. {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
  181. {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
  182. {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
  183. OPT_V_OPTIONS,
  184. OPT_PROV_OPTIONS,
  185. {NULL}
  186. };
  187. int ocsp_main(int argc, char **argv)
  188. {
  189. BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
  190. const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
  191. STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
  192. int trailing_md = 0;
  193. CA_DB *rdb = NULL;
  194. EVP_PKEY *key = NULL, *rkey = NULL;
  195. OCSP_BASICRESP *bs = NULL;
  196. OCSP_REQUEST *req = NULL;
  197. OCSP_RESPONSE *resp = NULL;
  198. STACK_OF(CONF_VALUE) *headers = NULL;
  199. STACK_OF(OCSP_CERTID) *ids = NULL;
  200. STACK_OF(OPENSSL_STRING) *reqnames = NULL;
  201. STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
  202. STACK_OF(X509) *issuers = NULL;
  203. X509 *issuer = NULL, *cert = NULL;
  204. STACK_OF(X509) *rca_cert = NULL;
  205. const EVP_MD *resp_certid_md = NULL;
  206. X509 *signer = NULL, *rsigner = NULL;
  207. X509_STORE *store = NULL;
  208. X509_VERIFY_PARAM *vpm = NULL;
  209. const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
  210. char *header, *value;
  211. char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
  212. char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
  213. char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
  214. char *rsignfile = NULL, *rkeyfile = NULL;
  215. char *passinarg = NULL, *passin = NULL;
  216. char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
  217. char *signfile = NULL, *keyfile = NULL;
  218. char *thost = NULL, *tport = NULL, *tpath = NULL;
  219. int noCAfile = 0, noCApath = 0, noCAstore = 0;
  220. int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
  221. int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
  222. int req_text = 0, resp_text = 0, res, ret = 1;
  223. int req_timeout = -1;
  224. long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
  225. unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
  226. OPTION_CHOICE o;
  227. reqnames = sk_OPENSSL_STRING_new_null();
  228. if (reqnames == NULL)
  229. goto end;
  230. ids = sk_OCSP_CERTID_new_null();
  231. if (ids == NULL)
  232. goto end;
  233. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  234. return 1;
  235. prog = opt_init(argc, argv, ocsp_options);
  236. while ((o = opt_next()) != OPT_EOF) {
  237. switch (o) {
  238. case OPT_EOF:
  239. case OPT_ERR:
  240. opthelp:
  241. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  242. goto end;
  243. case OPT_HELP:
  244. ret = 0;
  245. opt_help(ocsp_options);
  246. goto end;
  247. case OPT_OUTFILE:
  248. outfile = opt_arg();
  249. break;
  250. case OPT_TIMEOUT:
  251. #ifndef OPENSSL_NO_SOCK
  252. req_timeout = atoi(opt_arg());
  253. #endif
  254. break;
  255. case OPT_URL:
  256. OPENSSL_free(thost);
  257. OPENSSL_free(tport);
  258. OPENSSL_free(tpath);
  259. thost = tport = tpath = NULL;
  260. if (!OSSL_HTTP_parse_url(opt_arg(),
  261. &host, &port, &path, &use_ssl)) {
  262. BIO_printf(bio_err, "%s Error parsing URL\n", prog);
  263. goto end;
  264. }
  265. thost = host;
  266. tport = port;
  267. tpath = path;
  268. break;
  269. case OPT_HOST:
  270. host = opt_arg();
  271. break;
  272. case OPT_PORT:
  273. port = opt_arg();
  274. break;
  275. case OPT_IGNORE_ERR:
  276. ignore_err = 1;
  277. break;
  278. case OPT_NOVERIFY:
  279. noverify = 1;
  280. break;
  281. case OPT_NONCE:
  282. add_nonce = 2;
  283. break;
  284. case OPT_NO_NONCE:
  285. add_nonce = 0;
  286. break;
  287. case OPT_RESP_NO_CERTS:
  288. rflags |= OCSP_NOCERTS;
  289. break;
  290. case OPT_RESP_KEY_ID:
  291. rflags |= OCSP_RESPID_KEY;
  292. break;
  293. case OPT_NO_CERTS:
  294. sign_flags |= OCSP_NOCERTS;
  295. break;
  296. case OPT_NO_SIGNATURE_VERIFY:
  297. verify_flags |= OCSP_NOSIGS;
  298. break;
  299. case OPT_NO_CERT_VERIFY:
  300. verify_flags |= OCSP_NOVERIFY;
  301. break;
  302. case OPT_NO_CHAIN:
  303. verify_flags |= OCSP_NOCHAIN;
  304. break;
  305. case OPT_NO_CERT_CHECKS:
  306. verify_flags |= OCSP_NOCHECKS;
  307. break;
  308. case OPT_NO_EXPLICIT:
  309. verify_flags |= OCSP_NOEXPLICIT;
  310. break;
  311. case OPT_TRUST_OTHER:
  312. verify_flags |= OCSP_TRUSTOTHER;
  313. break;
  314. case OPT_NO_INTERN:
  315. verify_flags |= OCSP_NOINTERN;
  316. break;
  317. case OPT_BADSIG:
  318. badsig = 1;
  319. break;
  320. case OPT_TEXT:
  321. req_text = resp_text = 1;
  322. break;
  323. case OPT_REQ_TEXT:
  324. req_text = 1;
  325. break;
  326. case OPT_RESP_TEXT:
  327. resp_text = 1;
  328. break;
  329. case OPT_REQIN:
  330. reqin = opt_arg();
  331. break;
  332. case OPT_RESPIN:
  333. respin = opt_arg();
  334. break;
  335. case OPT_SIGNER:
  336. signfile = opt_arg();
  337. break;
  338. case OPT_VAFILE:
  339. verify_certfile = opt_arg();
  340. verify_flags |= OCSP_TRUSTOTHER;
  341. break;
  342. case OPT_SIGN_OTHER:
  343. sign_certfile = opt_arg();
  344. break;
  345. case OPT_VERIFY_OTHER:
  346. verify_certfile = opt_arg();
  347. break;
  348. case OPT_CAFILE:
  349. CAfile = opt_arg();
  350. break;
  351. case OPT_CAPATH:
  352. CApath = opt_arg();
  353. break;
  354. case OPT_CASTORE:
  355. CAstore = opt_arg();
  356. break;
  357. case OPT_NOCAFILE:
  358. noCAfile = 1;
  359. break;
  360. case OPT_NOCAPATH:
  361. noCApath = 1;
  362. break;
  363. case OPT_NOCASTORE:
  364. noCAstore = 1;
  365. break;
  366. case OPT_V_CASES:
  367. if (!opt_verify(o, vpm))
  368. goto end;
  369. vpmtouched++;
  370. break;
  371. case OPT_VALIDITY_PERIOD:
  372. opt_long(opt_arg(), &nsec);
  373. break;
  374. case OPT_STATUS_AGE:
  375. opt_long(opt_arg(), &maxage);
  376. break;
  377. case OPT_SIGNKEY:
  378. keyfile = opt_arg();
  379. break;
  380. case OPT_REQOUT:
  381. reqout = opt_arg();
  382. break;
  383. case OPT_RESPOUT:
  384. respout = opt_arg();
  385. break;
  386. case OPT_PATH:
  387. path = opt_arg();
  388. break;
  389. case OPT_ISSUER:
  390. issuer = load_cert(opt_arg(), FORMAT_UNDEF,
  391. "issuer certificate");
  392. if (issuer == NULL)
  393. goto end;
  394. if (issuers == NULL) {
  395. if ((issuers = sk_X509_new_null()) == NULL)
  396. goto end;
  397. }
  398. if (!sk_X509_push(issuers, issuer))
  399. goto end;
  400. break;
  401. case OPT_CERT:
  402. X509_free(cert);
  403. cert = load_cert(opt_arg(), FORMAT_UNDEF, "certificate");
  404. if (cert == NULL)
  405. goto end;
  406. if (cert_id_md == NULL)
  407. cert_id_md = EVP_sha1();
  408. if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
  409. goto end;
  410. if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
  411. goto end;
  412. trailing_md = 0;
  413. break;
  414. case OPT_SERIAL:
  415. if (cert_id_md == NULL)
  416. cert_id_md = EVP_sha1();
  417. if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
  418. goto end;
  419. if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
  420. goto end;
  421. trailing_md = 0;
  422. break;
  423. case OPT_INDEX:
  424. ridx_filename = opt_arg();
  425. break;
  426. case OPT_CA:
  427. rca_filename = opt_arg();
  428. break;
  429. case OPT_NMIN:
  430. opt_int(opt_arg(), &nmin);
  431. if (ndays == -1)
  432. ndays = 0;
  433. break;
  434. case OPT_REQUEST:
  435. opt_int(opt_arg(), &accept_count);
  436. break;
  437. case OPT_NDAYS:
  438. ndays = atoi(opt_arg());
  439. break;
  440. case OPT_RSIGNER:
  441. rsignfile = opt_arg();
  442. break;
  443. case OPT_RKEY:
  444. rkeyfile = opt_arg();
  445. break;
  446. case OPT_PASSIN:
  447. passinarg = opt_arg();
  448. break;
  449. case OPT_ROTHER:
  450. rcertfile = opt_arg();
  451. break;
  452. case OPT_RMD: /* Response MessageDigest */
  453. if (!opt_md(opt_arg(), &rsign_md))
  454. goto end;
  455. break;
  456. case OPT_RSIGOPT:
  457. if (rsign_sigopts == NULL)
  458. rsign_sigopts = sk_OPENSSL_STRING_new_null();
  459. if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg()))
  460. goto end;
  461. break;
  462. case OPT_HEADER:
  463. header = opt_arg();
  464. value = strchr(header, '=');
  465. if (value == NULL) {
  466. BIO_printf(bio_err, "Missing = in header key=value\n");
  467. goto opthelp;
  468. }
  469. *value++ = '\0';
  470. if (!X509V3_add_value(header, value, &headers))
  471. goto end;
  472. break;
  473. case OPT_RCID:
  474. resp_certid_md = EVP_get_digestbyname(opt_arg());
  475. if (resp_certid_md == NULL)
  476. goto opthelp;
  477. break;
  478. case OPT_MD:
  479. if (trailing_md) {
  480. BIO_printf(bio_err,
  481. "%s: Digest must be before -cert or -serial\n",
  482. prog);
  483. goto opthelp;
  484. }
  485. if (!opt_md(opt_unknown(), &cert_id_md))
  486. goto opthelp;
  487. trailing_md = 1;
  488. break;
  489. case OPT_MULTI:
  490. #ifdef HTTP_DAEMON
  491. multi = atoi(opt_arg());
  492. #endif
  493. break;
  494. case OPT_PROV_CASES:
  495. if (!opt_provider(o))
  496. goto end;
  497. break;
  498. }
  499. }
  500. if (trailing_md) {
  501. BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
  502. prog);
  503. goto opthelp;
  504. }
  505. argc = opt_num_rest();
  506. if (argc != 0)
  507. goto opthelp;
  508. /* Have we anything to do? */
  509. if (req == NULL && reqin == NULL
  510. && respin == NULL && !(port != NULL && ridx_filename != NULL))
  511. goto opthelp;
  512. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  513. if (out == NULL)
  514. goto end;
  515. if (req == NULL && (add_nonce != 2))
  516. add_nonce = 0;
  517. if (req == NULL && reqin != NULL) {
  518. derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
  519. if (derbio == NULL)
  520. goto end;
  521. req = d2i_OCSP_REQUEST_bio(derbio, NULL);
  522. BIO_free(derbio);
  523. if (req == NULL) {
  524. BIO_printf(bio_err, "Error reading OCSP request\n");
  525. goto end;
  526. }
  527. }
  528. if (req == NULL && port != NULL) {
  529. #ifndef OPENSSL_NO_SOCK
  530. acbio = http_server_init_bio(prog, port);
  531. if (acbio == NULL)
  532. goto end;
  533. #else
  534. BIO_printf(bio_err, "Cannot act as server - sockets not supported\n");
  535. goto end;
  536. #endif
  537. }
  538. if (rsignfile != NULL) {
  539. if (rkeyfile == NULL)
  540. rkeyfile = rsignfile;
  541. rsigner = load_cert(rsignfile, FORMAT_UNDEF,
  542. "responder certificate");
  543. if (rsigner == NULL) {
  544. BIO_printf(bio_err, "Error loading responder certificate\n");
  545. goto end;
  546. }
  547. if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM,
  548. NULL, "CA certificate"))
  549. goto end;
  550. if (rcertfile != NULL) {
  551. if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL,
  552. "responder other certificates"))
  553. goto end;
  554. }
  555. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  556. BIO_printf(bio_err, "Error getting password\n");
  557. goto end;
  558. }
  559. rkey = load_key(rkeyfile, FORMAT_PEM, 0, passin, NULL,
  560. "responder private key");
  561. if (rkey == NULL)
  562. goto end;
  563. }
  564. if (ridx_filename != NULL
  565. && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
  566. BIO_printf(bio_err,
  567. "Responder mode requires certificate, key, and CA.\n");
  568. goto end;
  569. }
  570. if (ridx_filename != NULL) {
  571. rdb = load_index(ridx_filename, NULL);
  572. if (rdb == NULL || index_index(rdb) <= 0) {
  573. ret = 1;
  574. goto end;
  575. }
  576. }
  577. #ifdef HTTP_DAEMON
  578. if (multi && acbio != NULL)
  579. spawn_loop(prog);
  580. if (acbio != NULL && req_timeout > 0)
  581. signal(SIGALRM, socket_timeout);
  582. #endif
  583. if (acbio != NULL)
  584. log_message(prog, LOG_INFO, "waiting for OCSP client connections...");
  585. redo_accept:
  586. if (acbio != NULL) {
  587. #ifdef HTTP_DAEMON
  588. if (index_changed(rdb)) {
  589. CA_DB *newrdb = load_index(ridx_filename, NULL);
  590. if (newrdb != NULL && index_index(newrdb) > 0) {
  591. free_index(rdb);
  592. rdb = newrdb;
  593. } else {
  594. free_index(newrdb);
  595. log_message(prog, LOG_ERR, "error reloading updated index: %s",
  596. ridx_filename);
  597. }
  598. }
  599. #endif
  600. req = NULL;
  601. res = do_responder(&req, &cbio, acbio, req_timeout);
  602. if (res == 0)
  603. goto redo_accept;
  604. if (req == NULL) {
  605. if (res == 1) {
  606. resp =
  607. OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
  608. NULL);
  609. send_ocsp_response(cbio, resp);
  610. }
  611. goto done_resp;
  612. }
  613. }
  614. if (req == NULL
  615. && (signfile != NULL || reqout != NULL
  616. || host != NULL || add_nonce || ridx_filename != NULL)) {
  617. BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
  618. goto end;
  619. }
  620. if (req != NULL && add_nonce) {
  621. if (!OCSP_request_add1_nonce(req, NULL, -1))
  622. goto end;
  623. }
  624. if (signfile != NULL) {
  625. if (keyfile == NULL)
  626. keyfile = signfile;
  627. signer = load_cert(signfile, FORMAT_UNDEF, "signer certificate");
  628. if (signer == NULL) {
  629. BIO_printf(bio_err, "Error loading signer certificate\n");
  630. goto end;
  631. }
  632. if (sign_certfile != NULL) {
  633. if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL,
  634. "signer certificates"))
  635. goto end;
  636. }
  637. key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL,
  638. "signer private key");
  639. if (key == NULL)
  640. goto end;
  641. if (!OCSP_request_sign
  642. (req, signer, key, NULL, sign_other, sign_flags)) {
  643. BIO_printf(bio_err, "Error signing OCSP request\n");
  644. goto end;
  645. }
  646. }
  647. if (req_text && req != NULL)
  648. OCSP_REQUEST_print(out, req, 0);
  649. if (reqout != NULL) {
  650. derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
  651. if (derbio == NULL)
  652. goto end;
  653. i2d_OCSP_REQUEST_bio(derbio, req);
  654. BIO_free(derbio);
  655. }
  656. if (rdb != NULL) {
  657. make_ocsp_response(bio_err, &resp, req, rdb, rca_cert, rsigner, rkey,
  658. rsign_md, rsign_sigopts, rother, rflags, nmin, ndays, badsig,
  659. resp_certid_md);
  660. if (cbio != NULL)
  661. send_ocsp_response(cbio, resp);
  662. } else if (host != NULL) {
  663. #ifndef OPENSSL_NO_SOCK
  664. resp = process_responder(req, host, path,
  665. port, use_ssl, headers, req_timeout);
  666. if (resp == NULL)
  667. goto end;
  668. #else
  669. BIO_printf(bio_err,
  670. "Error creating connect BIO - sockets not supported\n");
  671. goto end;
  672. #endif
  673. } else if (respin != NULL) {
  674. derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
  675. if (derbio == NULL)
  676. goto end;
  677. resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
  678. BIO_free(derbio);
  679. if (resp == NULL) {
  680. BIO_printf(bio_err, "Error reading OCSP response\n");
  681. goto end;
  682. }
  683. } else {
  684. ret = 0;
  685. goto end;
  686. }
  687. done_resp:
  688. if (respout != NULL) {
  689. derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
  690. if (derbio == NULL)
  691. goto end;
  692. i2d_OCSP_RESPONSE_bio(derbio, resp);
  693. BIO_free(derbio);
  694. }
  695. i = OCSP_response_status(resp);
  696. if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
  697. BIO_printf(out, "Responder Error: %s (%d)\n",
  698. OCSP_response_status_str(i), i);
  699. if (!ignore_err)
  700. goto end;
  701. }
  702. if (resp_text)
  703. OCSP_RESPONSE_print(out, resp, 0);
  704. /* If running as responder don't verify our own response */
  705. if (cbio != NULL) {
  706. /* If not unlimited, see if we took all we should. */
  707. if (accept_count != -1 && --accept_count <= 0) {
  708. ret = 0;
  709. goto end;
  710. }
  711. BIO_free_all(cbio);
  712. cbio = NULL;
  713. OCSP_REQUEST_free(req);
  714. req = NULL;
  715. OCSP_RESPONSE_free(resp);
  716. resp = NULL;
  717. goto redo_accept;
  718. }
  719. if (ridx_filename != NULL) {
  720. ret = 0;
  721. goto end;
  722. }
  723. if (store == NULL) {
  724. store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  725. CAstore, noCAstore);
  726. if (!store)
  727. goto end;
  728. }
  729. if (vpmtouched)
  730. X509_STORE_set1_param(store, vpm);
  731. if (verify_certfile != NULL) {
  732. if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL,
  733. "validator certificate"))
  734. goto end;
  735. }
  736. bs = OCSP_response_get1_basic(resp);
  737. if (bs == NULL) {
  738. BIO_printf(bio_err, "Error parsing response\n");
  739. goto end;
  740. }
  741. ret = 0;
  742. if (!noverify) {
  743. if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
  744. if (i == -1)
  745. BIO_printf(bio_err, "WARNING: no nonce in response\n");
  746. else {
  747. BIO_printf(bio_err, "Nonce Verify error\n");
  748. ret = 1;
  749. goto end;
  750. }
  751. }
  752. i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
  753. if (i <= 0 && issuers) {
  754. i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
  755. if (i > 0)
  756. ERR_clear_error();
  757. }
  758. if (i <= 0) {
  759. BIO_printf(bio_err, "Response Verify Failure\n");
  760. ERR_print_errors(bio_err);
  761. ret = 1;
  762. } else {
  763. BIO_printf(bio_err, "Response verify OK\n");
  764. }
  765. }
  766. print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
  767. end:
  768. ERR_print_errors(bio_err);
  769. X509_free(signer);
  770. X509_STORE_free(store);
  771. X509_VERIFY_PARAM_free(vpm);
  772. sk_OPENSSL_STRING_free(rsign_sigopts);
  773. EVP_PKEY_free(key);
  774. EVP_PKEY_free(rkey);
  775. X509_free(cert);
  776. sk_X509_pop_free(issuers, X509_free);
  777. X509_free(rsigner);
  778. sk_X509_pop_free(rca_cert, X509_free);
  779. free_index(rdb);
  780. BIO_free_all(cbio);
  781. BIO_free_all(acbio);
  782. BIO_free_all(out);
  783. OCSP_REQUEST_free(req);
  784. OCSP_RESPONSE_free(resp);
  785. OCSP_BASICRESP_free(bs);
  786. sk_OPENSSL_STRING_free(reqnames);
  787. sk_OCSP_CERTID_free(ids);
  788. sk_X509_pop_free(sign_other, X509_free);
  789. sk_X509_pop_free(verify_other, X509_free);
  790. sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
  791. OPENSSL_free(thost);
  792. OPENSSL_free(tport);
  793. OPENSSL_free(tpath);
  794. return ret;
  795. }
  796. #ifdef HTTP_DAEMON
  797. static int index_changed(CA_DB *rdb)
  798. {
  799. struct stat sb;
  800. if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
  801. if (rdb->dbst.st_mtime != sb.st_mtime
  802. || rdb->dbst.st_ctime != sb.st_ctime
  803. || rdb->dbst.st_ino != sb.st_ino
  804. || rdb->dbst.st_dev != sb.st_dev) {
  805. syslog(LOG_INFO, "index file changed, reloading");
  806. return 1;
  807. }
  808. }
  809. return 0;
  810. }
  811. #endif
  812. static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
  813. const EVP_MD *cert_id_md, X509 *issuer,
  814. STACK_OF(OCSP_CERTID) *ids)
  815. {
  816. OCSP_CERTID *id;
  817. if (issuer == NULL) {
  818. BIO_printf(bio_err, "No issuer certificate specified\n");
  819. return 0;
  820. }
  821. if (*req == NULL)
  822. *req = OCSP_REQUEST_new();
  823. if (*req == NULL)
  824. goto err;
  825. id = OCSP_cert_to_id(cert_id_md, cert, issuer);
  826. if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
  827. goto err;
  828. if (!OCSP_request_add0_id(*req, id))
  829. goto err;
  830. return 1;
  831. err:
  832. BIO_printf(bio_err, "Error Creating OCSP request\n");
  833. return 0;
  834. }
  835. static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
  836. const EVP_MD *cert_id_md, X509 *issuer,
  837. STACK_OF(OCSP_CERTID) *ids)
  838. {
  839. OCSP_CERTID *id;
  840. const X509_NAME *iname;
  841. ASN1_BIT_STRING *ikey;
  842. ASN1_INTEGER *sno;
  843. if (issuer == NULL) {
  844. BIO_printf(bio_err, "No issuer certificate specified\n");
  845. return 0;
  846. }
  847. if (*req == NULL)
  848. *req = OCSP_REQUEST_new();
  849. if (*req == NULL)
  850. goto err;
  851. iname = X509_get_subject_name(issuer);
  852. ikey = X509_get0_pubkey_bitstr(issuer);
  853. sno = s2i_ASN1_INTEGER(NULL, serial);
  854. if (sno == NULL) {
  855. BIO_printf(bio_err, "Error converting serial number %s\n", serial);
  856. return 0;
  857. }
  858. id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
  859. ASN1_INTEGER_free(sno);
  860. if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
  861. goto err;
  862. if (!OCSP_request_add0_id(*req, id))
  863. goto err;
  864. return 1;
  865. err:
  866. BIO_printf(bio_err, "Error Creating OCSP request\n");
  867. return 0;
  868. }
  869. static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
  870. STACK_OF(OPENSSL_STRING) *names,
  871. STACK_OF(OCSP_CERTID) *ids, long nsec,
  872. long maxage)
  873. {
  874. OCSP_CERTID *id;
  875. const char *name;
  876. int i, status, reason;
  877. ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
  878. if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
  879. || !sk_OCSP_CERTID_num(ids))
  880. return;
  881. for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
  882. id = sk_OCSP_CERTID_value(ids, i);
  883. name = sk_OPENSSL_STRING_value(names, i);
  884. BIO_printf(out, "%s: ", name);
  885. if (!OCSP_resp_find_status(bs, id, &status, &reason,
  886. &rev, &thisupd, &nextupd)) {
  887. BIO_puts(out, "ERROR: No Status found.\n");
  888. continue;
  889. }
  890. /*
  891. * Check validity: if invalid write to output BIO so we know which
  892. * response this refers to.
  893. */
  894. if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
  895. BIO_puts(out, "WARNING: Status times invalid.\n");
  896. ERR_print_errors(out);
  897. }
  898. BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
  899. BIO_puts(out, "\tThis Update: ");
  900. ASN1_GENERALIZEDTIME_print(out, thisupd);
  901. BIO_puts(out, "\n");
  902. if (nextupd) {
  903. BIO_puts(out, "\tNext Update: ");
  904. ASN1_GENERALIZEDTIME_print(out, nextupd);
  905. BIO_puts(out, "\n");
  906. }
  907. if (status != V_OCSP_CERTSTATUS_REVOKED)
  908. continue;
  909. if (reason != -1)
  910. BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
  911. BIO_puts(out, "\tRevocation Time: ");
  912. ASN1_GENERALIZEDTIME_print(out, rev);
  913. BIO_puts(out, "\n");
  914. }
  915. }
  916. static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
  917. CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
  918. EVP_PKEY *rkey, const EVP_MD *rmd,
  919. STACK_OF(OPENSSL_STRING) *sigopts,
  920. STACK_OF(X509) *rother, unsigned long flags,
  921. int nmin, int ndays, int badsig,
  922. const EVP_MD *resp_md)
  923. {
  924. ASN1_TIME *thisupd = NULL, *nextupd = NULL;
  925. OCSP_CERTID *cid;
  926. OCSP_BASICRESP *bs = NULL;
  927. int i, id_count;
  928. EVP_MD_CTX *mctx = NULL;
  929. EVP_PKEY_CTX *pkctx = NULL;
  930. id_count = OCSP_request_onereq_count(req);
  931. if (id_count <= 0) {
  932. *resp =
  933. OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
  934. goto end;
  935. }
  936. bs = OCSP_BASICRESP_new();
  937. thisupd = X509_gmtime_adj(NULL, 0);
  938. if (ndays != -1)
  939. nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
  940. /* Examine each certificate id in the request */
  941. for (i = 0; i < id_count; i++) {
  942. OCSP_ONEREQ *one;
  943. ASN1_INTEGER *serial;
  944. char **inf;
  945. int jj;
  946. int found = 0;
  947. ASN1_OBJECT *cert_id_md_oid;
  948. const EVP_MD *cert_id_md;
  949. OCSP_CERTID *cid_resp_md = NULL;
  950. one = OCSP_request_onereq_get0(req, i);
  951. cid = OCSP_onereq_get0_id(one);
  952. OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
  953. cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
  954. if (cert_id_md == NULL) {
  955. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  956. NULL);
  957. goto end;
  958. }
  959. for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
  960. X509 *ca_cert = sk_X509_value(ca, jj);
  961. OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
  962. if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
  963. found = 1;
  964. if (resp_md != NULL)
  965. cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
  966. }
  967. OCSP_CERTID_free(ca_id);
  968. }
  969. OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
  970. inf = lookup_serial(db, serial);
  971. /* at this point, we can have cid be an alias of cid_resp_md */
  972. cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
  973. if (!found) {
  974. OCSP_basic_add1_status(bs, cid,
  975. V_OCSP_CERTSTATUS_UNKNOWN,
  976. 0, NULL, thisupd, nextupd);
  977. continue;
  978. }
  979. if (inf == NULL) {
  980. OCSP_basic_add1_status(bs, cid,
  981. V_OCSP_CERTSTATUS_UNKNOWN,
  982. 0, NULL, thisupd, nextupd);
  983. } else if (inf[DB_type][0] == DB_TYPE_VAL) {
  984. OCSP_basic_add1_status(bs, cid,
  985. V_OCSP_CERTSTATUS_GOOD,
  986. 0, NULL, thisupd, nextupd);
  987. } else if (inf[DB_type][0] == DB_TYPE_REV) {
  988. ASN1_OBJECT *inst = NULL;
  989. ASN1_TIME *revtm = NULL;
  990. ASN1_GENERALIZEDTIME *invtm = NULL;
  991. OCSP_SINGLERESP *single;
  992. int reason = -1;
  993. unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
  994. single = OCSP_basic_add1_status(bs, cid,
  995. V_OCSP_CERTSTATUS_REVOKED,
  996. reason, revtm, thisupd, nextupd);
  997. if (invtm != NULL)
  998. OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
  999. invtm, 0, 0);
  1000. else if (inst != NULL)
  1001. OCSP_SINGLERESP_add1_ext_i2d(single,
  1002. NID_hold_instruction_code, inst,
  1003. 0, 0);
  1004. ASN1_OBJECT_free(inst);
  1005. ASN1_TIME_free(revtm);
  1006. ASN1_GENERALIZEDTIME_free(invtm);
  1007. }
  1008. OCSP_CERTID_free(cid_resp_md);
  1009. }
  1010. OCSP_copy_nonce(bs, req);
  1011. mctx = EVP_MD_CTX_new();
  1012. if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
  1013. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
  1014. goto end;
  1015. }
  1016. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  1017. char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  1018. if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
  1019. BIO_printf(err, "parameter error \"%s\"\n", sigopt);
  1020. ERR_print_errors(bio_err);
  1021. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  1022. NULL);
  1023. goto end;
  1024. }
  1025. }
  1026. if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
  1027. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
  1028. goto end;
  1029. }
  1030. if (badsig) {
  1031. const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
  1032. corrupt_signature(sig);
  1033. }
  1034. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
  1035. end:
  1036. EVP_MD_CTX_free(mctx);
  1037. ASN1_TIME_free(thisupd);
  1038. ASN1_TIME_free(nextupd);
  1039. OCSP_BASICRESP_free(bs);
  1040. }
  1041. static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
  1042. {
  1043. int i;
  1044. BIGNUM *bn = NULL;
  1045. char *itmp, *row[DB_NUMBER], **rrow;
  1046. for (i = 0; i < DB_NUMBER; i++)
  1047. row[i] = NULL;
  1048. bn = ASN1_INTEGER_to_BN(ser, NULL);
  1049. OPENSSL_assert(bn); /* FIXME: should report an error at this
  1050. * point and abort */
  1051. if (BN_is_zero(bn))
  1052. itmp = OPENSSL_strdup("00");
  1053. else
  1054. itmp = BN_bn2hex(bn);
  1055. row[DB_serial] = itmp;
  1056. BN_free(bn);
  1057. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1058. OPENSSL_free(itmp);
  1059. return rrow;
  1060. }
  1061. static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
  1062. int timeout)
  1063. {
  1064. #ifndef OPENSSL_NO_SOCK
  1065. return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_RESPONSE),
  1066. (ASN1_VALUE **)preq, NULL, pcbio, acbio,
  1067. prog, 1 /* accept_get */, timeout);
  1068. #else
  1069. BIO_printf(bio_err,
  1070. "Error getting OCSP request - sockets not supported\n");
  1071. *preq = NULL;
  1072. return 0;
  1073. #endif
  1074. }
  1075. static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp)
  1076. {
  1077. #ifndef OPENSSL_NO_SOCK
  1078. return http_server_send_asn1_resp(cbio, "application/ocsp-response",
  1079. ASN1_ITEM_rptr(OCSP_RESPONSE),
  1080. (const ASN1_VALUE *)resp);
  1081. #else
  1082. BIO_printf(bio_err,
  1083. "Error sending OCSP response - sockets not supported\n");
  1084. return 0;
  1085. #endif
  1086. }
  1087. #ifndef OPENSSL_NO_SOCK
  1088. OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
  1089. const char *host, const char *path,
  1090. const char *port, int use_ssl,
  1091. STACK_OF(CONF_VALUE) *headers,
  1092. int req_timeout)
  1093. {
  1094. SSL_CTX *ctx = NULL;
  1095. OCSP_RESPONSE *resp = NULL;
  1096. if (use_ssl == 1) {
  1097. ctx = SSL_CTX_new(TLS_client_method());
  1098. if (ctx == NULL) {
  1099. BIO_printf(bio_err, "Error creating SSL context.\n");
  1100. goto end;
  1101. }
  1102. SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
  1103. }
  1104. resp = (OCSP_RESPONSE *)
  1105. app_http_post_asn1(host, port, path, NULL, NULL /* no proxy used */,
  1106. ctx, headers, "application/ocsp-request",
  1107. (ASN1_VALUE *)req, ASN1_ITEM_rptr(OCSP_REQUEST),
  1108. req_timeout, ASN1_ITEM_rptr(OCSP_RESPONSE));
  1109. if (resp == NULL)
  1110. BIO_printf(bio_err, "Error querying OCSP responder\n");
  1111. end:
  1112. SSL_CTX_free(ctx);
  1113. return resp;
  1114. }
  1115. #endif