ocsp.c 41 KB

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