ocsp.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /*
  2. * Copyright 2001-2023 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 certificates"},
  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_certs = 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. n_responders = 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_certs, 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_certs == 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 (n_responders != 0 && 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. trace_log_message(-1, prog,
  614. LOG_INFO, "waiting for OCSP client connections...");
  615. redo_accept:
  616. if (acbio != NULL) {
  617. #ifdef HTTP_DAEMON
  618. if (index_changed(rdb)) {
  619. CA_DB *newrdb = load_index(ridx_filename, NULL);
  620. if (newrdb != NULL && index_index(newrdb) > 0) {
  621. free_index(rdb);
  622. rdb = newrdb;
  623. } else {
  624. free_index(newrdb);
  625. trace_log_message(-1, prog,
  626. LOG_ERR, "error reloading updated index: %s",
  627. ridx_filename);
  628. }
  629. }
  630. #endif
  631. req = NULL;
  632. res = do_responder(&req, &cbio, acbio, req_timeout);
  633. if (res == 0)
  634. goto redo_accept;
  635. if (req == NULL) {
  636. if (res == 1) {
  637. resp =
  638. OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
  639. NULL);
  640. send_ocsp_response(cbio, resp);
  641. }
  642. goto done_resp;
  643. }
  644. }
  645. if (req == NULL
  646. && (signfile != NULL || reqout != NULL
  647. || host != NULL || add_nonce || ridx_filename != NULL)) {
  648. BIO_printf(bio_err, "Need an OCSP request for this operation!\n");
  649. goto end;
  650. }
  651. if (req != NULL && add_nonce) {
  652. if (!OCSP_request_add1_nonce(req, NULL, -1))
  653. goto end;
  654. }
  655. if (signfile != NULL) {
  656. if (keyfile == NULL)
  657. keyfile = signfile;
  658. signer = load_cert(signfile, FORMAT_UNDEF, "signer certificate");
  659. if (signer == NULL) {
  660. BIO_printf(bio_err, "Error loading signer certificate\n");
  661. goto end;
  662. }
  663. if (sign_certfile != NULL) {
  664. if (!load_certs(sign_certfile, 0, &sign_other, NULL,
  665. "signer certificates"))
  666. goto end;
  667. }
  668. key = load_key(keyfile, FORMAT_UNDEF, 0, NULL, NULL,
  669. "signer private key");
  670. if (key == NULL)
  671. goto end;
  672. if (!OCSP_request_sign(req, signer, key, NULL,
  673. sign_other, sign_flags)) {
  674. BIO_printf(bio_err, "Error signing OCSP request\n");
  675. goto end;
  676. }
  677. }
  678. if (req_text && req != NULL)
  679. OCSP_REQUEST_print(out, req, 0);
  680. if (reqout != NULL) {
  681. derbio = bio_open_default(reqout, 'w', FORMAT_ASN1);
  682. if (derbio == NULL)
  683. goto end;
  684. i2d_OCSP_REQUEST_bio(derbio, req);
  685. BIO_free(derbio);
  686. }
  687. if (rdb != NULL) {
  688. make_ocsp_response(bio_err, &resp, req, rdb, rca_certs, rsigner, rkey,
  689. rsign_md, rsign_sigopts, rother, rflags, nmin, ndays,
  690. badsig, resp_certid_md);
  691. if (resp == NULL)
  692. goto end;
  693. if (cbio != NULL)
  694. send_ocsp_response(cbio, resp);
  695. } else if (host != NULL) {
  696. #ifndef OPENSSL_NO_SOCK
  697. resp = process_responder(req, host, port, path, opt_proxy, opt_no_proxy,
  698. use_ssl, headers, req_timeout);
  699. if (resp == NULL)
  700. goto end;
  701. #else
  702. BIO_printf(bio_err,
  703. "Error creating connect BIO - sockets not supported\n");
  704. goto end;
  705. #endif
  706. } else if (respin != NULL) {
  707. derbio = bio_open_default(respin, 'r', FORMAT_ASN1);
  708. if (derbio == NULL)
  709. goto end;
  710. resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
  711. BIO_free(derbio);
  712. if (resp == NULL) {
  713. BIO_printf(bio_err, "Error reading OCSP response\n");
  714. goto end;
  715. }
  716. } else {
  717. ret = 0;
  718. goto end;
  719. }
  720. done_resp:
  721. if (respout != NULL) {
  722. derbio = bio_open_default(respout, 'w', FORMAT_ASN1);
  723. if (derbio == NULL)
  724. goto end;
  725. i2d_OCSP_RESPONSE_bio(derbio, resp);
  726. BIO_free(derbio);
  727. }
  728. i = OCSP_response_status(resp);
  729. if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
  730. BIO_printf(out, "Responder Error: %s (%d)\n",
  731. OCSP_response_status_str(i), i);
  732. if (!ignore_err)
  733. goto end;
  734. }
  735. if (resp_text)
  736. OCSP_RESPONSE_print(out, resp, 0);
  737. /* If running as responder don't verify our own response */
  738. if (cbio != NULL) {
  739. /* If not unlimited, see if we took all we should. */
  740. if (accept_count != -1 && --accept_count <= 0) {
  741. ret = 0;
  742. goto end;
  743. }
  744. BIO_free_all(cbio);
  745. cbio = NULL;
  746. OCSP_REQUEST_free(req);
  747. req = NULL;
  748. OCSP_RESPONSE_free(resp);
  749. resp = NULL;
  750. goto redo_accept;
  751. }
  752. if (ridx_filename != NULL) {
  753. ret = 0;
  754. goto end;
  755. }
  756. if (store == NULL) {
  757. store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  758. CAstore, noCAstore);
  759. if (!store)
  760. goto end;
  761. }
  762. if (vpmtouched)
  763. X509_STORE_set1_param(store, vpm);
  764. if (verify_certfile != NULL) {
  765. if (!load_certs(verify_certfile, 0, &verify_other, NULL,
  766. "validator certificates"))
  767. goto end;
  768. }
  769. bs = OCSP_response_get1_basic(resp);
  770. if (bs == NULL) {
  771. BIO_printf(bio_err, "Error parsing response\n");
  772. goto end;
  773. }
  774. ret = 0;
  775. if (!noverify) {
  776. if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) {
  777. if (i == -1)
  778. BIO_printf(bio_err, "WARNING: no nonce in response\n");
  779. else {
  780. BIO_printf(bio_err, "Nonce Verify error\n");
  781. ret = 1;
  782. goto end;
  783. }
  784. }
  785. i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
  786. if (i <= 0 && issuers) {
  787. i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER);
  788. if (i > 0)
  789. ERR_clear_error();
  790. }
  791. if (i <= 0) {
  792. BIO_printf(bio_err, "Response Verify Failure\n");
  793. ERR_print_errors(bio_err);
  794. ret = 1;
  795. } else {
  796. BIO_printf(bio_err, "Response verify OK\n");
  797. }
  798. }
  799. if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
  800. ret = 1;
  801. end:
  802. ERR_print_errors(bio_err);
  803. X509_free(signer);
  804. X509_STORE_free(store);
  805. X509_VERIFY_PARAM_free(vpm);
  806. sk_OPENSSL_STRING_free(rsign_sigopts);
  807. EVP_PKEY_free(key);
  808. EVP_PKEY_free(rkey);
  809. EVP_MD_free(cert_id_md);
  810. EVP_MD_free(rsign_md);
  811. EVP_MD_free(resp_certid_md);
  812. X509_free(cert);
  813. OSSL_STACK_OF_X509_free(issuers);
  814. X509_free(rsigner);
  815. OSSL_STACK_OF_X509_free(rca_certs);
  816. free_index(rdb);
  817. BIO_free_all(cbio);
  818. BIO_free_all(acbio);
  819. BIO_free_all(out);
  820. OCSP_REQUEST_free(req);
  821. OCSP_RESPONSE_free(resp);
  822. OCSP_BASICRESP_free(bs);
  823. sk_OPENSSL_STRING_free(reqnames);
  824. sk_OCSP_CERTID_free(ids);
  825. OSSL_STACK_OF_X509_free(sign_other);
  826. OSSL_STACK_OF_X509_free(verify_other);
  827. sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
  828. OPENSSL_free(thost);
  829. OPENSSL_free(tport);
  830. OPENSSL_free(tpath);
  831. return ret;
  832. }
  833. #ifdef HTTP_DAEMON
  834. static int index_changed(CA_DB *rdb)
  835. {
  836. struct stat sb;
  837. if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
  838. if (rdb->dbst.st_mtime != sb.st_mtime
  839. || rdb->dbst.st_ctime != sb.st_ctime
  840. || rdb->dbst.st_ino != sb.st_ino
  841. || rdb->dbst.st_dev != sb.st_dev) {
  842. syslog(LOG_INFO, "index file changed, reloading");
  843. return 1;
  844. }
  845. }
  846. return 0;
  847. }
  848. #endif
  849. static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
  850. const EVP_MD *cert_id_md, X509 *issuer,
  851. STACK_OF(OCSP_CERTID) *ids)
  852. {
  853. OCSP_CERTID *id;
  854. if (issuer == NULL) {
  855. BIO_printf(bio_err, "No issuer certificate specified\n");
  856. return 0;
  857. }
  858. if (*req == NULL)
  859. *req = OCSP_REQUEST_new();
  860. if (*req == NULL)
  861. goto err;
  862. id = OCSP_cert_to_id(cert_id_md, cert, issuer);
  863. if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
  864. goto err;
  865. if (!OCSP_request_add0_id(*req, id))
  866. goto err;
  867. return 1;
  868. err:
  869. BIO_printf(bio_err, "Error Creating OCSP request\n");
  870. return 0;
  871. }
  872. static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
  873. const EVP_MD *cert_id_md, X509 *issuer,
  874. STACK_OF(OCSP_CERTID) *ids)
  875. {
  876. OCSP_CERTID *id;
  877. const X509_NAME *iname;
  878. ASN1_BIT_STRING *ikey;
  879. ASN1_INTEGER *sno;
  880. if (issuer == NULL) {
  881. BIO_printf(bio_err, "No issuer certificate specified\n");
  882. return 0;
  883. }
  884. if (*req == NULL)
  885. *req = OCSP_REQUEST_new();
  886. if (*req == NULL)
  887. goto err;
  888. iname = X509_get_subject_name(issuer);
  889. ikey = X509_get0_pubkey_bitstr(issuer);
  890. sno = s2i_ASN1_INTEGER(NULL, serial);
  891. if (sno == NULL) {
  892. BIO_printf(bio_err, "Error converting serial number %s\n", serial);
  893. return 0;
  894. }
  895. id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
  896. ASN1_INTEGER_free(sno);
  897. if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
  898. goto err;
  899. if (!OCSP_request_add0_id(*req, id))
  900. goto err;
  901. return 1;
  902. err:
  903. BIO_printf(bio_err, "Error Creating OCSP request\n");
  904. return 0;
  905. }
  906. static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
  907. STACK_OF(OPENSSL_STRING) *names,
  908. STACK_OF(OCSP_CERTID) *ids, long nsec,
  909. long maxage)
  910. {
  911. OCSP_CERTID *id;
  912. const char *name;
  913. int i, status, reason;
  914. ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
  915. int ret = 1;
  916. if (req == NULL || !sk_OPENSSL_STRING_num(names))
  917. return 1;
  918. if (bs == NULL || !sk_OCSP_CERTID_num(ids))
  919. return 0;
  920. for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
  921. id = sk_OCSP_CERTID_value(ids, i);
  922. name = sk_OPENSSL_STRING_value(names, i);
  923. BIO_printf(out, "%s: ", name);
  924. if (!OCSP_resp_find_status(bs, id, &status, &reason,
  925. &rev, &thisupd, &nextupd)) {
  926. BIO_puts(out, "ERROR: No Status found.\n");
  927. ret = 0;
  928. continue;
  929. }
  930. /*
  931. * Check validity: if invalid write to output BIO so we know which
  932. * response this refers to.
  933. */
  934. if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
  935. BIO_puts(out, "WARNING: Status times invalid.\n");
  936. ERR_print_errors(out);
  937. }
  938. BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
  939. BIO_puts(out, "\tThis Update: ");
  940. ASN1_GENERALIZEDTIME_print(out, thisupd);
  941. BIO_puts(out, "\n");
  942. if (nextupd) {
  943. BIO_puts(out, "\tNext Update: ");
  944. ASN1_GENERALIZEDTIME_print(out, nextupd);
  945. BIO_puts(out, "\n");
  946. }
  947. if (status != V_OCSP_CERTSTATUS_REVOKED)
  948. continue;
  949. if (reason != -1)
  950. BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
  951. BIO_puts(out, "\tRevocation Time: ");
  952. ASN1_GENERALIZEDTIME_print(out, rev);
  953. BIO_puts(out, "\n");
  954. }
  955. return ret;
  956. }
  957. static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
  958. CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
  959. EVP_PKEY *rkey, const EVP_MD *rmd,
  960. STACK_OF(OPENSSL_STRING) *sigopts,
  961. STACK_OF(X509) *rother, unsigned long flags,
  962. int nmin, int ndays, int badsig,
  963. const EVP_MD *resp_md)
  964. {
  965. ASN1_TIME *thisupd = NULL, *nextupd = NULL;
  966. OCSP_CERTID *cid;
  967. OCSP_BASICRESP *bs = NULL;
  968. int i, id_count;
  969. EVP_MD_CTX *mctx = NULL;
  970. EVP_PKEY_CTX *pkctx = NULL;
  971. id_count = OCSP_request_onereq_count(req);
  972. if (id_count <= 0) {
  973. *resp =
  974. OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
  975. goto end;
  976. }
  977. bs = OCSP_BASICRESP_new();
  978. thisupd = X509_gmtime_adj(NULL, 0);
  979. if (ndays != -1)
  980. nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
  981. /* Examine each certificate id in the request */
  982. for (i = 0; i < id_count; i++) {
  983. OCSP_ONEREQ *one;
  984. ASN1_INTEGER *serial;
  985. char **inf;
  986. int jj;
  987. int found = 0;
  988. ASN1_OBJECT *cert_id_md_oid;
  989. const EVP_MD *cert_id_md;
  990. OCSP_CERTID *cid_resp_md = NULL;
  991. one = OCSP_request_onereq_get0(req, i);
  992. cid = OCSP_onereq_get0_id(one);
  993. OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
  994. cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
  995. if (cert_id_md == NULL) {
  996. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  997. NULL);
  998. goto end;
  999. }
  1000. for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
  1001. X509 *ca_cert = sk_X509_value(ca, jj);
  1002. OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
  1003. if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
  1004. found = 1;
  1005. if (resp_md != NULL)
  1006. cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
  1007. }
  1008. OCSP_CERTID_free(ca_id);
  1009. }
  1010. OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
  1011. inf = lookup_serial(db, serial);
  1012. /* at this point, we can have cid be an alias of cid_resp_md */
  1013. cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
  1014. if (!found) {
  1015. OCSP_basic_add1_status(bs, cid,
  1016. V_OCSP_CERTSTATUS_UNKNOWN,
  1017. 0, NULL, thisupd, nextupd);
  1018. continue;
  1019. }
  1020. if (inf == NULL) {
  1021. OCSP_basic_add1_status(bs, cid,
  1022. V_OCSP_CERTSTATUS_UNKNOWN,
  1023. 0, NULL, thisupd, nextupd);
  1024. } else if (inf[DB_type][0] == DB_TYPE_VAL) {
  1025. OCSP_basic_add1_status(bs, cid,
  1026. V_OCSP_CERTSTATUS_GOOD,
  1027. 0, NULL, thisupd, nextupd);
  1028. } else if (inf[DB_type][0] == DB_TYPE_REV) {
  1029. ASN1_OBJECT *inst = NULL;
  1030. ASN1_TIME *revtm = NULL;
  1031. ASN1_GENERALIZEDTIME *invtm = NULL;
  1032. OCSP_SINGLERESP *single;
  1033. int reason = -1;
  1034. unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
  1035. single = OCSP_basic_add1_status(bs, cid,
  1036. V_OCSP_CERTSTATUS_REVOKED,
  1037. reason, revtm, thisupd, nextupd);
  1038. if (single == NULL) {
  1039. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  1040. NULL);
  1041. goto end;
  1042. }
  1043. if (invtm != NULL)
  1044. OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
  1045. invtm, 0, 0);
  1046. else if (inst != NULL)
  1047. OCSP_SINGLERESP_add1_ext_i2d(single,
  1048. NID_hold_instruction_code, inst,
  1049. 0, 0);
  1050. ASN1_OBJECT_free(inst);
  1051. ASN1_TIME_free(revtm);
  1052. ASN1_GENERALIZEDTIME_free(invtm);
  1053. }
  1054. OCSP_CERTID_free(cid_resp_md);
  1055. }
  1056. OCSP_copy_nonce(bs, req);
  1057. mctx = EVP_MD_CTX_new();
  1058. if (mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
  1059. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
  1060. goto end;
  1061. }
  1062. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  1063. char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  1064. if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
  1065. BIO_printf(err, "parameter error \"%s\"\n", sigopt);
  1066. ERR_print_errors(bio_err);
  1067. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  1068. NULL);
  1069. goto end;
  1070. }
  1071. }
  1072. if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
  1073. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
  1074. goto end;
  1075. }
  1076. if (badsig) {
  1077. const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
  1078. corrupt_signature(sig);
  1079. }
  1080. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
  1081. end:
  1082. EVP_MD_CTX_free(mctx);
  1083. ASN1_TIME_free(thisupd);
  1084. ASN1_TIME_free(nextupd);
  1085. OCSP_BASICRESP_free(bs);
  1086. }
  1087. static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
  1088. {
  1089. int i;
  1090. BIGNUM *bn = NULL;
  1091. char *itmp, *row[DB_NUMBER], **rrow;
  1092. for (i = 0; i < DB_NUMBER; i++)
  1093. row[i] = NULL;
  1094. bn = ASN1_INTEGER_to_BN(ser, NULL);
  1095. OPENSSL_assert(bn); /* FIXME: should report an error at this
  1096. * point and abort */
  1097. if (BN_is_zero(bn)) {
  1098. itmp = OPENSSL_strdup("00");
  1099. OPENSSL_assert(itmp);
  1100. } else {
  1101. itmp = BN_bn2hex(bn);
  1102. }
  1103. row[DB_serial] = itmp;
  1104. BN_free(bn);
  1105. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1106. OPENSSL_free(itmp);
  1107. return rrow;
  1108. }
  1109. static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
  1110. int timeout)
  1111. {
  1112. #ifndef OPENSSL_NO_SOCK
  1113. return http_server_get_asn1_req(ASN1_ITEM_rptr(OCSP_REQUEST),
  1114. (ASN1_VALUE **)preq, NULL, pcbio, acbio,
  1115. NULL /* found_keep_alive */,
  1116. prog, 1 /* accept_get */, timeout);
  1117. #else
  1118. BIO_printf(bio_err,
  1119. "Error getting OCSP request - sockets not supported\n");
  1120. *preq = NULL;
  1121. return 0;
  1122. #endif
  1123. }
  1124. static int send_ocsp_response(BIO *cbio, const OCSP_RESPONSE *resp)
  1125. {
  1126. #ifndef OPENSSL_NO_SOCK
  1127. return http_server_send_asn1_resp(prog, cbio,
  1128. 0 /* no keep-alive */,
  1129. "application/ocsp-response",
  1130. ASN1_ITEM_rptr(OCSP_RESPONSE),
  1131. (const ASN1_VALUE *)resp);
  1132. #else
  1133. BIO_printf(bio_err,
  1134. "Error sending OCSP response - sockets not supported\n");
  1135. return 0;
  1136. #endif
  1137. }
  1138. #ifndef OPENSSL_NO_SOCK
  1139. OCSP_RESPONSE *process_responder(OCSP_REQUEST *req, const char *host,
  1140. const char *port, const char *path,
  1141. const char *proxy, const char *no_proxy,
  1142. int use_ssl, STACK_OF(CONF_VALUE) *headers,
  1143. int req_timeout)
  1144. {
  1145. SSL_CTX *ctx = NULL;
  1146. OCSP_RESPONSE *resp = NULL;
  1147. if (use_ssl == 1) {
  1148. ctx = SSL_CTX_new(TLS_client_method());
  1149. if (ctx == NULL) {
  1150. BIO_printf(bio_err, "Error creating SSL context.\n");
  1151. goto end;
  1152. }
  1153. }
  1154. resp = (OCSP_RESPONSE *)
  1155. app_http_post_asn1(host, port, path, proxy, no_proxy,
  1156. ctx, headers, "application/ocsp-request",
  1157. (ASN1_VALUE *)req, ASN1_ITEM_rptr(OCSP_REQUEST),
  1158. "application/ocsp-response",
  1159. req_timeout, ASN1_ITEM_rptr(OCSP_RESPONSE));
  1160. if (resp == NULL)
  1161. BIO_printf(bio_err, "Error querying OCSP responder\n");
  1162. end:
  1163. SSL_CTX_free(ctx);
  1164. return resp;
  1165. }
  1166. #endif