ocsp.c 41 KB

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