ocsp.c 41 KB

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