2
0

ocsp.c 39 KB

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