ocsp.c 39 KB

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