2
0

ocsp.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /*
  2. * Copyright 2001-2018 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_NO_OCSP
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # ifdef OPENSSL_SYS_VMS
  14. # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined
  15. * on OpenVMS */
  16. # endif
  17. # include <stdio.h>
  18. # include <stdlib.h>
  19. # include <string.h>
  20. # include <time.h>
  21. # include <ctype.h>
  22. /* Needs to be included before the openssl headers */
  23. # include "apps.h"
  24. # include "progs.h"
  25. # include "internal/sockets.h"
  26. # include <openssl/e_os2.h>
  27. # include <openssl/crypto.h>
  28. # include <openssl/err.h>
  29. # include <openssl/ssl.h>
  30. # include <openssl/evp.h>
  31. # include <openssl/bn.h>
  32. # include <openssl/x509v3.h>
  33. # include <openssl/rand.h>
  34. #ifndef HAVE_FORK
  35. # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
  36. # define HAVE_FORK 0
  37. # else
  38. # define HAVE_FORK 1
  39. # endif
  40. #endif
  41. #if HAVE_FORK
  42. # undef NO_FORK
  43. #else
  44. # define NO_FORK
  45. #endif
  46. # if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \
  47. && !defined(OPENSSL_NO_POSIX_IO)
  48. # define OCSP_DAEMON
  49. # include <sys/types.h>
  50. # include <sys/wait.h>
  51. # include <syslog.h>
  52. # include <signal.h>
  53. # define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */
  54. # else
  55. # undef LOG_INFO
  56. # undef LOG_WARNING
  57. # undef LOG_ERR
  58. # define LOG_INFO 0
  59. # define LOG_WARNING 1
  60. # define LOG_ERR 2
  61. # endif
  62. # if defined(OPENSSL_SYS_VXWORKS)
  63. /* not supported */
  64. int setpgid(pid_t pid, pid_t pgid)
  65. {
  66. errno = ENOSYS;
  67. return 0;
  68. }
  69. /* not supported */
  70. pid_t fork(void)
  71. {
  72. errno = ENOSYS;
  73. return (pid_t) -1;
  74. }
  75. # endif
  76. /* Maximum leeway in validity period: default 5 minutes */
  77. # define MAX_VALIDITY_PERIOD (5 * 60)
  78. static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
  79. const EVP_MD *cert_id_md, X509 *issuer,
  80. STACK_OF(OCSP_CERTID) *ids);
  81. static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
  82. const EVP_MD *cert_id_md, X509 *issuer,
  83. STACK_OF(OCSP_CERTID) *ids);
  84. static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
  85. STACK_OF(OPENSSL_STRING) *names,
  86. STACK_OF(OCSP_CERTID) *ids, long nsec,
  87. long maxage);
  88. static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
  89. CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
  90. EVP_PKEY *rkey, const EVP_MD *md,
  91. STACK_OF(OPENSSL_STRING) *sigopts,
  92. STACK_OF(X509) *rother, unsigned long flags,
  93. int nmin, int ndays, int badsig,
  94. const EVP_MD *resp_md);
  95. static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser);
  96. static BIO *init_responder(const char *port);
  97. static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, int timeout);
  98. static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
  99. static void log_message(int level, const char *fmt, ...);
  100. static char *prog;
  101. static int multi = 0;
  102. # ifdef OCSP_DAEMON
  103. static int acfd = (int) INVALID_SOCKET;
  104. static int index_changed(CA_DB *);
  105. static void spawn_loop(void);
  106. static int print_syslog(const char *str, size_t len, void *levPtr);
  107. static void sock_timeout(int signum);
  108. # endif
  109. # ifndef OPENSSL_NO_SOCK
  110. static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
  111. const char *path,
  112. const STACK_OF(CONF_VALUE) *headers,
  113. OCSP_REQUEST *req, int req_timeout);
  114. # endif
  115. typedef enum OPTION_choice {
  116. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  117. OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT,
  118. OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE,
  119. OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS,
  120. OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN,
  121. OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER,
  122. OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT,
  123. OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER,
  124. OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH,
  125. OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT,
  126. OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL,
  127. OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER,
  128. OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_RSIGOPT, OPT_HEADER,
  129. OPT_RCID,
  130. OPT_V_ENUM,
  131. OPT_MD,
  132. OPT_MULTI
  133. } OPTION_CHOICE;
  134. const OPTIONS ocsp_options[] = {
  135. {"help", OPT_HELP, '-', "Display this summary"},
  136. {"out", OPT_OUTFILE, '>', "Output filename"},
  137. {"timeout", OPT_TIMEOUT, 'p',
  138. "Connection timeout (in seconds) to the OCSP responder"},
  139. {"url", OPT_URL, 's', "Responder URL"},
  140. {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"},
  141. {"port", OPT_PORT, 'p', "Port to run responder on"},
  142. {"ignore_err", OPT_IGNORE_ERR, '-',
  143. "Ignore error on OCSP request or response and continue running"},
  144. {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"},
  145. {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"},
  146. {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"},
  147. {"resp_no_certs", OPT_RESP_NO_CERTS, '-',
  148. "Don't include any certificates in response"},
  149. {"resp_key_id", OPT_RESP_KEY_ID, '-',
  150. "Identify response by signing certificate key ID"},
  151. # ifdef OCSP_DAEMON
  152. {"multi", OPT_MULTI, 'p', "run multiple responder processes"},
  153. # endif
  154. {"no_certs", OPT_NO_CERTS, '-',
  155. "Don't include any certificates in signed request"},
  156. {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-',
  157. "Don't check signature on response"},
  158. {"no_cert_verify", OPT_NO_CERT_VERIFY, '-',
  159. "Don't check signing certificate"},
  160. {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"},
  161. {"no_cert_checks", OPT_NO_CERT_CHECKS, '-',
  162. "Don't do additional checks on signing certificate"},
  163. {"no_explicit", OPT_NO_EXPLICIT, '-',
  164. "Do not explicitly check the chain, just verify the root"},
  165. {"trust_other", OPT_TRUST_OTHER, '-',
  166. "Don't verify additional certificates"},
  167. {"no_intern", OPT_NO_INTERN, '-',
  168. "Don't search certificates contained in response for signer"},
  169. {"badsig", OPT_BADSIG, '-',
  170. "Corrupt last byte of loaded OSCP response signature (for test)"},
  171. {"text", OPT_TEXT, '-', "Print text form of request and response"},
  172. {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"},
  173. {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"},
  174. {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"},
  175. {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"},
  176. {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"},
  177. {"VAfile", OPT_VAFILE, '<', "Validator certificates file"},
  178. {"sign_other", OPT_SIGN_OTHER, '<',
  179. "Additional certificates to include in signed request"},
  180. {"verify_other", OPT_VERIFY_OTHER, '<',
  181. "Additional certificates to search for signer"},
  182. {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
  183. {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"},
  184. {"no-CAfile", OPT_NOCAFILE, '-',
  185. "Do not load the default certificates file"},
  186. {"no-CApath", OPT_NOCAPATH, '-',
  187. "Do not load certificates from the default certificates directory"},
  188. {"validity_period", OPT_VALIDITY_PERIOD, 'u',
  189. "Maximum validity discrepancy in seconds"},
  190. {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"},
  191. {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"},
  192. {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"},
  193. {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"},
  194. {"path", OPT_PATH, 's', "Path to use in OCSP request"},
  195. {"issuer", OPT_ISSUER, '<', "Issuer certificate"},
  196. {"cert", OPT_CERT, '<', "Certificate to check"},
  197. {"serial", OPT_SERIAL, 's', "Serial number to check"},
  198. {"index", OPT_INDEX, '<', "Certificate status index file"},
  199. {"CA", OPT_CA, '<', "CA certificate"},
  200. {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"},
  201. {"nrequest", OPT_REQUEST, 'p',
  202. "Number of requests to accept (default unlimited)"},
  203. {"ndays", OPT_NDAYS, 'p', "Number of days before next update"},
  204. {"rsigner", OPT_RSIGNER, '<',
  205. "Responder certificate to sign responses with"},
  206. {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"},
  207. {"rother", OPT_ROTHER, '<', "Other certificates to include in response"},
  208. {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"},
  209. {"rsigopt", OPT_RSIGOPT, 's', "OCSP response signature parameter in n:v form"},
  210. {"header", OPT_HEADER, 's', "key=value header to add"},
  211. {"rcid", OPT_RCID, 's', "Use specified algorithm for cert id in response"},
  212. {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"},
  213. OPT_V_OPTIONS,
  214. {NULL}
  215. };
  216. int ocsp_main(int argc, char **argv)
  217. {
  218. BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL;
  219. const EVP_MD *cert_id_md = NULL, *rsign_md = NULL;
  220. STACK_OF(OPENSSL_STRING) *rsign_sigopts = NULL;
  221. int trailing_md = 0;
  222. CA_DB *rdb = NULL;
  223. EVP_PKEY *key = NULL, *rkey = NULL;
  224. OCSP_BASICRESP *bs = NULL;
  225. OCSP_REQUEST *req = NULL;
  226. OCSP_RESPONSE *resp = NULL;
  227. STACK_OF(CONF_VALUE) *headers = NULL;
  228. STACK_OF(OCSP_CERTID) *ids = NULL;
  229. STACK_OF(OPENSSL_STRING) *reqnames = NULL;
  230. STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL;
  231. STACK_OF(X509) *issuers = NULL;
  232. X509 *issuer = NULL, *cert = NULL;
  233. STACK_OF(X509) *rca_cert = NULL;
  234. const EVP_MD *resp_certid_md = NULL;
  235. X509 *signer = NULL, *rsigner = NULL;
  236. X509_STORE *store = NULL;
  237. X509_VERIFY_PARAM *vpm = NULL;
  238. const char *CAfile = NULL, *CApath = NULL;
  239. char *header, *value;
  240. char *host = NULL, *port = NULL, *path = "/", *outfile = NULL;
  241. char *rca_filename = NULL, *reqin = NULL, *respin = NULL;
  242. char *reqout = NULL, *respout = NULL, *ridx_filename = NULL;
  243. char *rsignfile = NULL, *rkeyfile = NULL;
  244. char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL;
  245. char *signfile = NULL, *keyfile = NULL;
  246. char *thost = NULL, *tport = NULL, *tpath = NULL;
  247. int noCAfile = 0, noCApath = 0;
  248. int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1;
  249. int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1;
  250. int req_text = 0, resp_text = 0, ret = 1;
  251. int req_timeout = -1;
  252. long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
  253. unsigned long sign_flags = 0, verify_flags = 0, rflags = 0;
  254. OPTION_CHOICE o;
  255. reqnames = sk_OPENSSL_STRING_new_null();
  256. if (reqnames == NULL)
  257. goto end;
  258. ids = sk_OCSP_CERTID_new_null();
  259. if (ids == NULL)
  260. goto end;
  261. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  262. return 1;
  263. prog = opt_init(argc, argv, ocsp_options);
  264. while ((o = opt_next()) != OPT_EOF) {
  265. switch (o) {
  266. case OPT_EOF:
  267. case OPT_ERR:
  268. opthelp:
  269. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  270. goto end;
  271. case OPT_HELP:
  272. ret = 0;
  273. opt_help(ocsp_options);
  274. goto end;
  275. case OPT_OUTFILE:
  276. outfile = opt_arg();
  277. break;
  278. case OPT_TIMEOUT:
  279. #ifndef OPENSSL_NO_SOCK
  280. req_timeout = atoi(opt_arg());
  281. #endif
  282. break;
  283. case OPT_URL:
  284. OPENSSL_free(thost);
  285. OPENSSL_free(tport);
  286. OPENSSL_free(tpath);
  287. thost = tport = tpath = NULL;
  288. if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) {
  289. BIO_printf(bio_err, "%s Error parsing URL\n", prog);
  290. goto end;
  291. }
  292. thost = host;
  293. tport = port;
  294. tpath = path;
  295. break;
  296. case OPT_HOST:
  297. host = opt_arg();
  298. break;
  299. case OPT_PORT:
  300. port = opt_arg();
  301. break;
  302. case OPT_IGNORE_ERR:
  303. ignore_err = 1;
  304. break;
  305. case OPT_NOVERIFY:
  306. noverify = 1;
  307. break;
  308. case OPT_NONCE:
  309. add_nonce = 2;
  310. break;
  311. case OPT_NO_NONCE:
  312. add_nonce = 0;
  313. break;
  314. case OPT_RESP_NO_CERTS:
  315. rflags |= OCSP_NOCERTS;
  316. break;
  317. case OPT_RESP_KEY_ID:
  318. rflags |= OCSP_RESPID_KEY;
  319. break;
  320. case OPT_NO_CERTS:
  321. sign_flags |= OCSP_NOCERTS;
  322. break;
  323. case OPT_NO_SIGNATURE_VERIFY:
  324. verify_flags |= OCSP_NOSIGS;
  325. break;
  326. case OPT_NO_CERT_VERIFY:
  327. verify_flags |= OCSP_NOVERIFY;
  328. break;
  329. case OPT_NO_CHAIN:
  330. verify_flags |= OCSP_NOCHAIN;
  331. break;
  332. case OPT_NO_CERT_CHECKS:
  333. verify_flags |= OCSP_NOCHECKS;
  334. break;
  335. case OPT_NO_EXPLICIT:
  336. verify_flags |= OCSP_NOEXPLICIT;
  337. break;
  338. case OPT_TRUST_OTHER:
  339. verify_flags |= OCSP_TRUSTOTHER;
  340. break;
  341. case OPT_NO_INTERN:
  342. verify_flags |= OCSP_NOINTERN;
  343. break;
  344. case OPT_BADSIG:
  345. badsig = 1;
  346. break;
  347. case OPT_TEXT:
  348. req_text = resp_text = 1;
  349. break;
  350. case OPT_REQ_TEXT:
  351. req_text = 1;
  352. break;
  353. case OPT_RESP_TEXT:
  354. resp_text = 1;
  355. break;
  356. case OPT_REQIN:
  357. reqin = opt_arg();
  358. break;
  359. case OPT_RESPIN:
  360. respin = opt_arg();
  361. break;
  362. case OPT_SIGNER:
  363. signfile = opt_arg();
  364. break;
  365. case OPT_VAFILE:
  366. verify_certfile = opt_arg();
  367. verify_flags |= OCSP_TRUSTOTHER;
  368. break;
  369. case OPT_SIGN_OTHER:
  370. sign_certfile = opt_arg();
  371. break;
  372. case OPT_VERIFY_OTHER:
  373. verify_certfile = opt_arg();
  374. break;
  375. case OPT_CAFILE:
  376. CAfile = opt_arg();
  377. break;
  378. case OPT_CAPATH:
  379. CApath = opt_arg();
  380. break;
  381. case OPT_NOCAFILE:
  382. noCAfile = 1;
  383. break;
  384. case OPT_NOCAPATH:
  385. noCApath = 1;
  386. break;
  387. case OPT_V_CASES:
  388. if (!opt_verify(o, vpm))
  389. goto end;
  390. vpmtouched++;
  391. break;
  392. case OPT_VALIDITY_PERIOD:
  393. opt_long(opt_arg(), &nsec);
  394. break;
  395. case OPT_STATUS_AGE:
  396. opt_long(opt_arg(), &maxage);
  397. break;
  398. case OPT_SIGNKEY:
  399. keyfile = opt_arg();
  400. break;
  401. case OPT_REQOUT:
  402. reqout = opt_arg();
  403. break;
  404. case OPT_RESPOUT:
  405. respout = opt_arg();
  406. break;
  407. case OPT_PATH:
  408. path = opt_arg();
  409. break;
  410. case OPT_ISSUER:
  411. issuer = load_cert(opt_arg(), FORMAT_PEM, "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. sk_X509_push(issuers, issuer);
  419. break;
  420. case OPT_CERT:
  421. X509_free(cert);
  422. cert = load_cert(opt_arg(), FORMAT_PEM, "certificate");
  423. if (cert == NULL)
  424. goto end;
  425. if (cert_id_md == NULL)
  426. cert_id_md = EVP_sha1();
  427. if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
  428. goto end;
  429. if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
  430. goto end;
  431. trailing_md = 0;
  432. break;
  433. case OPT_SERIAL:
  434. if (cert_id_md == NULL)
  435. cert_id_md = EVP_sha1();
  436. if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids))
  437. goto end;
  438. if (!sk_OPENSSL_STRING_push(reqnames, opt_arg()))
  439. goto end;
  440. trailing_md = 0;
  441. break;
  442. case OPT_INDEX:
  443. ridx_filename = opt_arg();
  444. break;
  445. case OPT_CA:
  446. rca_filename = opt_arg();
  447. break;
  448. case OPT_NMIN:
  449. opt_int(opt_arg(), &nmin);
  450. if (ndays == -1)
  451. ndays = 0;
  452. break;
  453. case OPT_REQUEST:
  454. opt_int(opt_arg(), &accept_count);
  455. break;
  456. case OPT_NDAYS:
  457. ndays = atoi(opt_arg());
  458. break;
  459. case OPT_RSIGNER:
  460. rsignfile = opt_arg();
  461. break;
  462. case OPT_RKEY:
  463. rkeyfile = opt_arg();
  464. break;
  465. case OPT_ROTHER:
  466. rcertfile = opt_arg();
  467. break;
  468. case OPT_RMD: /* Response MessageDigest */
  469. if (!opt_md(opt_arg(), &rsign_md))
  470. goto end;
  471. break;
  472. case OPT_RSIGOPT:
  473. if (rsign_sigopts == NULL)
  474. rsign_sigopts = sk_OPENSSL_STRING_new_null();
  475. if (rsign_sigopts == NULL || !sk_OPENSSL_STRING_push(rsign_sigopts, opt_arg()))
  476. goto end;
  477. break;
  478. case OPT_HEADER:
  479. header = opt_arg();
  480. value = strchr(header, '=');
  481. if (value == NULL) {
  482. BIO_printf(bio_err, "Missing = in header key=value\n");
  483. goto opthelp;
  484. }
  485. *value++ = '\0';
  486. if (!X509V3_add_value(header, value, &headers))
  487. goto end;
  488. break;
  489. case OPT_RCID:
  490. resp_certid_md = EVP_get_digestbyname(opt_arg());
  491. if (resp_certid_md == NULL)
  492. goto opthelp;
  493. break;
  494. case OPT_MD:
  495. if (trailing_md) {
  496. BIO_printf(bio_err,
  497. "%s: Digest must be before -cert or -serial\n",
  498. prog);
  499. goto opthelp;
  500. }
  501. if (!opt_md(opt_unknown(), &cert_id_md))
  502. goto opthelp;
  503. trailing_md = 1;
  504. break;
  505. case OPT_MULTI:
  506. # ifdef OCSP_DAEMON
  507. multi = atoi(opt_arg());
  508. # endif
  509. break;
  510. }
  511. }
  512. if (trailing_md) {
  513. BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n",
  514. prog);
  515. goto opthelp;
  516. }
  517. argc = opt_num_rest();
  518. if (argc != 0)
  519. goto opthelp;
  520. /* Have we anything to do? */
  521. if (req == NULL && reqin == NULL
  522. && respin == NULL && !(port != NULL && ridx_filename != NULL))
  523. goto opthelp;
  524. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  525. if (out == NULL)
  526. goto end;
  527. if (req == NULL && (add_nonce != 2))
  528. add_nonce = 0;
  529. if (req == NULL && reqin != NULL) {
  530. derbio = bio_open_default(reqin, 'r', FORMAT_ASN1);
  531. if (derbio == NULL)
  532. goto end;
  533. req = d2i_OCSP_REQUEST_bio(derbio, NULL);
  534. BIO_free(derbio);
  535. if (req == NULL) {
  536. BIO_printf(bio_err, "Error reading OCSP request\n");
  537. goto end;
  538. }
  539. }
  540. if (req == NULL && port != NULL) {
  541. acbio = init_responder(port);
  542. if (acbio == NULL)
  543. goto end;
  544. }
  545. if (rsignfile != NULL) {
  546. if (rkeyfile == NULL)
  547. rkeyfile = rsignfile;
  548. rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate");
  549. if (rsigner == NULL) {
  550. BIO_printf(bio_err, "Error loading responder certificate\n");
  551. goto end;
  552. }
  553. if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM,
  554. NULL, "CA certificate"))
  555. goto end;
  556. if (rcertfile != NULL) {
  557. if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL,
  558. "responder other certificates"))
  559. goto end;
  560. }
  561. rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL,
  562. "responder private key");
  563. if (rkey == NULL)
  564. goto end;
  565. }
  566. if (ridx_filename != NULL
  567. && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) {
  568. BIO_printf(bio_err,
  569. "Responder mode requires certificate, key, and CA.\n");
  570. goto end;
  571. }
  572. if (ridx_filename != NULL) {
  573. rdb = load_index(ridx_filename, NULL);
  574. if (rdb == NULL || index_index(rdb) <= 0) {
  575. ret = 1;
  576. goto end;
  577. }
  578. }
  579. # ifdef OCSP_DAEMON
  580. if (multi && acbio != NULL)
  581. spawn_loop();
  582. if (acbio != NULL && req_timeout > 0)
  583. signal(SIGALRM, sock_timeout);
  584. #endif
  585. if (acbio != NULL)
  586. log_message(LOG_INFO, "waiting for OCSP client connections...");
  587. redo_accept:
  588. if (acbio != NULL) {
  589. # ifdef OCSP_DAEMON
  590. if (index_changed(rdb)) {
  591. CA_DB *newrdb = load_index(ridx_filename, NULL);
  592. if (newrdb != NULL && index_index(newrdb) > 0) {
  593. free_index(rdb);
  594. rdb = newrdb;
  595. } else {
  596. free_index(newrdb);
  597. log_message(LOG_ERR, "error reloading updated index: %s",
  598. ridx_filename);
  599. }
  600. }
  601. # endif
  602. req = NULL;
  603. if (!do_responder(&req, &cbio, acbio, req_timeout))
  604. goto redo_accept;
  605. if (req == NULL) {
  606. resp =
  607. OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST,
  608. NULL);
  609. send_ocsp_response(cbio, resp);
  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, FORMAT_PEM, "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, FORMAT_PEM, 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, CApath, noCAfile, noCApath);
  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, FORMAT_PEM, NULL,
  731. "validator certificate"))
  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. print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage);
  765. end:
  766. ERR_print_errors(bio_err);
  767. X509_free(signer);
  768. X509_STORE_free(store);
  769. X509_VERIFY_PARAM_free(vpm);
  770. sk_OPENSSL_STRING_free(rsign_sigopts);
  771. EVP_PKEY_free(key);
  772. EVP_PKEY_free(rkey);
  773. X509_free(cert);
  774. sk_X509_pop_free(issuers, X509_free);
  775. X509_free(rsigner);
  776. sk_X509_pop_free(rca_cert, X509_free);
  777. free_index(rdb);
  778. BIO_free_all(cbio);
  779. BIO_free_all(acbio);
  780. BIO_free_all(out);
  781. OCSP_REQUEST_free(req);
  782. OCSP_RESPONSE_free(resp);
  783. OCSP_BASICRESP_free(bs);
  784. sk_OPENSSL_STRING_free(reqnames);
  785. sk_OCSP_CERTID_free(ids);
  786. sk_X509_pop_free(sign_other, X509_free);
  787. sk_X509_pop_free(verify_other, X509_free);
  788. sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
  789. OPENSSL_free(thost);
  790. OPENSSL_free(tport);
  791. OPENSSL_free(tpath);
  792. return ret;
  793. }
  794. static void
  795. log_message(int level, const char *fmt, ...)
  796. {
  797. va_list ap;
  798. va_start(ap, fmt);
  799. # ifdef OCSP_DAEMON
  800. if (multi) {
  801. char buf[1024];
  802. if (vsnprintf(buf, sizeof(buf), fmt, ap) > 0) {
  803. syslog(level, "%s", buf);
  804. }
  805. if (level >= LOG_ERR)
  806. ERR_print_errors_cb(print_syslog, &level);
  807. }
  808. # endif
  809. if (!multi) {
  810. BIO_printf(bio_err, "%s: ", prog);
  811. BIO_vprintf(bio_err, fmt, ap);
  812. BIO_printf(bio_err, "\n");
  813. }
  814. va_end(ap);
  815. }
  816. # ifdef OCSP_DAEMON
  817. static int print_syslog(const char *str, size_t len, void *levPtr)
  818. {
  819. int level = *(int *)levPtr;
  820. int ilen = (len > MAXERRLEN) ? MAXERRLEN : len;
  821. syslog(level, "%.*s", ilen, str);
  822. return ilen;
  823. }
  824. static int index_changed(CA_DB *rdb)
  825. {
  826. struct stat sb;
  827. if (rdb != NULL && stat(rdb->dbfname, &sb) != -1) {
  828. if (rdb->dbst.st_mtime != sb.st_mtime
  829. || rdb->dbst.st_ctime != sb.st_ctime
  830. || rdb->dbst.st_ino != sb.st_ino
  831. || rdb->dbst.st_dev != sb.st_dev) {
  832. syslog(LOG_INFO, "index file changed, reloading");
  833. return 1;
  834. }
  835. }
  836. return 0;
  837. }
  838. static void killall(int ret, pid_t *kidpids)
  839. {
  840. int i;
  841. for (i = 0; i < multi; ++i)
  842. if (kidpids[i] != 0)
  843. (void)kill(kidpids[i], SIGTERM);
  844. OPENSSL_free(kidpids);
  845. sleep(1);
  846. exit(ret);
  847. }
  848. static int termsig = 0;
  849. static void noteterm (int sig)
  850. {
  851. termsig = sig;
  852. }
  853. /*
  854. * Loop spawning up to `multi` child processes, only child processes return
  855. * from this function. The parent process loops until receiving a termination
  856. * signal, kills extant children and exits without returning.
  857. */
  858. static void spawn_loop(void)
  859. {
  860. pid_t *kidpids = NULL;
  861. int status;
  862. int procs = 0;
  863. int i;
  864. openlog(prog, LOG_PID, LOG_DAEMON);
  865. if (setpgid(0, 0)) {
  866. syslog(LOG_ERR, "fatal: error detaching from parent process group: %s",
  867. strerror(errno));
  868. exit(1);
  869. }
  870. kidpids = app_malloc(multi * sizeof(*kidpids), "child PID array");
  871. for (i = 0; i < multi; ++i)
  872. kidpids[i] = 0;
  873. signal(SIGINT, noteterm);
  874. signal(SIGTERM, noteterm);
  875. while (termsig == 0) {
  876. pid_t fpid;
  877. /*
  878. * Wait for a child to replace when we're at the limit.
  879. * Slow down if a child exited abnormally or waitpid() < 0
  880. */
  881. while (termsig == 0 && procs >= multi) {
  882. if ((fpid = waitpid(-1, &status, 0)) > 0) {
  883. for (i = 0; i < procs; ++i) {
  884. if (kidpids[i] == fpid) {
  885. kidpids[i] = 0;
  886. --procs;
  887. break;
  888. }
  889. }
  890. if (i >= multi) {
  891. syslog(LOG_ERR, "fatal: internal error: "
  892. "no matching child slot for pid: %ld",
  893. (long) fpid);
  894. killall(1, kidpids);
  895. }
  896. if (status != 0) {
  897. if (WIFEXITED(status))
  898. syslog(LOG_WARNING, "child process: %ld, exit status: %d",
  899. (long)fpid, WEXITSTATUS(status));
  900. else if (WIFSIGNALED(status))
  901. syslog(LOG_WARNING, "child process: %ld, term signal %d%s",
  902. (long)fpid, WTERMSIG(status),
  903. #ifdef WCOREDUMP
  904. WCOREDUMP(status) ? " (core dumped)" :
  905. #endif
  906. "");
  907. sleep(1);
  908. }
  909. break;
  910. } else if (errno != EINTR) {
  911. syslog(LOG_ERR, "fatal: waitpid(): %s", strerror(errno));
  912. killall(1, kidpids);
  913. }
  914. }
  915. if (termsig)
  916. break;
  917. switch(fpid = fork()) {
  918. case -1: /* error */
  919. /* System critically low on memory, pause and try again later */
  920. sleep(30);
  921. break;
  922. case 0: /* child */
  923. OPENSSL_free(kidpids);
  924. signal(SIGINT, SIG_DFL);
  925. signal(SIGTERM, SIG_DFL);
  926. if (termsig)
  927. _exit(0);
  928. if (RAND_poll() <= 0) {
  929. syslog(LOG_ERR, "fatal: RAND_poll() failed");
  930. _exit(1);
  931. }
  932. return;
  933. default: /* parent */
  934. for (i = 0; i < multi; ++i) {
  935. if (kidpids[i] == 0) {
  936. kidpids[i] = fpid;
  937. procs++;
  938. break;
  939. }
  940. }
  941. if (i >= multi) {
  942. syslog(LOG_ERR, "fatal: internal error: no free child slots");
  943. killall(1, kidpids);
  944. }
  945. break;
  946. }
  947. }
  948. /* The loop above can only break on termsig */
  949. syslog(LOG_INFO, "terminating on signal: %d", termsig);
  950. killall(0, kidpids);
  951. }
  952. # endif
  953. static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
  954. const EVP_MD *cert_id_md, X509 *issuer,
  955. STACK_OF(OCSP_CERTID) *ids)
  956. {
  957. OCSP_CERTID *id;
  958. if (issuer == NULL) {
  959. BIO_printf(bio_err, "No issuer certificate specified\n");
  960. return 0;
  961. }
  962. if (*req == NULL)
  963. *req = OCSP_REQUEST_new();
  964. if (*req == NULL)
  965. goto err;
  966. id = OCSP_cert_to_id(cert_id_md, cert, issuer);
  967. if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
  968. goto err;
  969. if (!OCSP_request_add0_id(*req, id))
  970. goto err;
  971. return 1;
  972. err:
  973. BIO_printf(bio_err, "Error Creating OCSP request\n");
  974. return 0;
  975. }
  976. static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
  977. const EVP_MD *cert_id_md, X509 *issuer,
  978. STACK_OF(OCSP_CERTID) *ids)
  979. {
  980. OCSP_CERTID *id;
  981. X509_NAME *iname;
  982. ASN1_BIT_STRING *ikey;
  983. ASN1_INTEGER *sno;
  984. if (issuer == NULL) {
  985. BIO_printf(bio_err, "No issuer certificate specified\n");
  986. return 0;
  987. }
  988. if (*req == NULL)
  989. *req = OCSP_REQUEST_new();
  990. if (*req == NULL)
  991. goto err;
  992. iname = X509_get_subject_name(issuer);
  993. ikey = X509_get0_pubkey_bitstr(issuer);
  994. sno = s2i_ASN1_INTEGER(NULL, serial);
  995. if (sno == NULL) {
  996. BIO_printf(bio_err, "Error converting serial number %s\n", serial);
  997. return 0;
  998. }
  999. id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
  1000. ASN1_INTEGER_free(sno);
  1001. if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
  1002. goto err;
  1003. if (!OCSP_request_add0_id(*req, id))
  1004. goto err;
  1005. return 1;
  1006. err:
  1007. BIO_printf(bio_err, "Error Creating OCSP request\n");
  1008. return 0;
  1009. }
  1010. static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
  1011. STACK_OF(OPENSSL_STRING) *names,
  1012. STACK_OF(OCSP_CERTID) *ids, long nsec,
  1013. long maxage)
  1014. {
  1015. OCSP_CERTID *id;
  1016. const char *name;
  1017. int i, status, reason;
  1018. ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
  1019. if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names)
  1020. || !sk_OCSP_CERTID_num(ids))
  1021. return;
  1022. for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) {
  1023. id = sk_OCSP_CERTID_value(ids, i);
  1024. name = sk_OPENSSL_STRING_value(names, i);
  1025. BIO_printf(out, "%s: ", name);
  1026. if (!OCSP_resp_find_status(bs, id, &status, &reason,
  1027. &rev, &thisupd, &nextupd)) {
  1028. BIO_puts(out, "ERROR: No Status found.\n");
  1029. continue;
  1030. }
  1031. /*
  1032. * Check validity: if invalid write to output BIO so we know which
  1033. * response this refers to.
  1034. */
  1035. if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
  1036. BIO_puts(out, "WARNING: Status times invalid.\n");
  1037. ERR_print_errors(out);
  1038. }
  1039. BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
  1040. BIO_puts(out, "\tThis Update: ");
  1041. ASN1_GENERALIZEDTIME_print(out, thisupd);
  1042. BIO_puts(out, "\n");
  1043. if (nextupd) {
  1044. BIO_puts(out, "\tNext Update: ");
  1045. ASN1_GENERALIZEDTIME_print(out, nextupd);
  1046. BIO_puts(out, "\n");
  1047. }
  1048. if (status != V_OCSP_CERTSTATUS_REVOKED)
  1049. continue;
  1050. if (reason != -1)
  1051. BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason));
  1052. BIO_puts(out, "\tRevocation Time: ");
  1053. ASN1_GENERALIZEDTIME_print(out, rev);
  1054. BIO_puts(out, "\n");
  1055. }
  1056. }
  1057. static void make_ocsp_response(BIO *err, OCSP_RESPONSE **resp, OCSP_REQUEST *req,
  1058. CA_DB *db, STACK_OF(X509) *ca, X509 *rcert,
  1059. EVP_PKEY *rkey, const EVP_MD *rmd,
  1060. STACK_OF(OPENSSL_STRING) *sigopts,
  1061. STACK_OF(X509) *rother, unsigned long flags,
  1062. int nmin, int ndays, int badsig,
  1063. const EVP_MD *resp_md)
  1064. {
  1065. ASN1_TIME *thisupd = NULL, *nextupd = NULL;
  1066. OCSP_CERTID *cid;
  1067. OCSP_BASICRESP *bs = NULL;
  1068. int i, id_count;
  1069. EVP_MD_CTX *mctx = NULL;
  1070. EVP_PKEY_CTX *pkctx = NULL;
  1071. id_count = OCSP_request_onereq_count(req);
  1072. if (id_count <= 0) {
  1073. *resp =
  1074. OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL);
  1075. goto end;
  1076. }
  1077. bs = OCSP_BASICRESP_new();
  1078. thisupd = X509_gmtime_adj(NULL, 0);
  1079. if (ndays != -1)
  1080. nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL);
  1081. /* Examine each certificate id in the request */
  1082. for (i = 0; i < id_count; i++) {
  1083. OCSP_ONEREQ *one;
  1084. ASN1_INTEGER *serial;
  1085. char **inf;
  1086. int jj;
  1087. int found = 0;
  1088. ASN1_OBJECT *cert_id_md_oid;
  1089. const EVP_MD *cert_id_md;
  1090. OCSP_CERTID *cid_resp_md = NULL;
  1091. one = OCSP_request_onereq_get0(req, i);
  1092. cid = OCSP_onereq_get0_id(one);
  1093. OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid);
  1094. cert_id_md = EVP_get_digestbyobj(cert_id_md_oid);
  1095. if (cert_id_md == NULL) {
  1096. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  1097. NULL);
  1098. goto end;
  1099. }
  1100. for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) {
  1101. X509 *ca_cert = sk_X509_value(ca, jj);
  1102. OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert);
  1103. if (OCSP_id_issuer_cmp(ca_id, cid) == 0) {
  1104. found = 1;
  1105. if (resp_md != NULL)
  1106. cid_resp_md = OCSP_cert_to_id(resp_md, NULL, ca_cert);
  1107. }
  1108. OCSP_CERTID_free(ca_id);
  1109. }
  1110. OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid);
  1111. inf = lookup_serial(db, serial);
  1112. /* at this point, we can have cid be an alias of cid_resp_md */
  1113. cid = (cid_resp_md != NULL) ? cid_resp_md : cid;
  1114. if (!found) {
  1115. OCSP_basic_add1_status(bs, cid,
  1116. V_OCSP_CERTSTATUS_UNKNOWN,
  1117. 0, NULL, thisupd, nextupd);
  1118. continue;
  1119. }
  1120. if (inf == NULL) {
  1121. OCSP_basic_add1_status(bs, cid,
  1122. V_OCSP_CERTSTATUS_UNKNOWN,
  1123. 0, NULL, thisupd, nextupd);
  1124. } else if (inf[DB_type][0] == DB_TYPE_VAL) {
  1125. OCSP_basic_add1_status(bs, cid,
  1126. V_OCSP_CERTSTATUS_GOOD,
  1127. 0, NULL, thisupd, nextupd);
  1128. } else if (inf[DB_type][0] == DB_TYPE_REV) {
  1129. ASN1_OBJECT *inst = NULL;
  1130. ASN1_TIME *revtm = NULL;
  1131. ASN1_GENERALIZEDTIME *invtm = NULL;
  1132. OCSP_SINGLERESP *single;
  1133. int reason = -1;
  1134. unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]);
  1135. single = OCSP_basic_add1_status(bs, cid,
  1136. V_OCSP_CERTSTATUS_REVOKED,
  1137. reason, revtm, thisupd, nextupd);
  1138. if (invtm != NULL)
  1139. OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date,
  1140. invtm, 0, 0);
  1141. else if (inst != NULL)
  1142. OCSP_SINGLERESP_add1_ext_i2d(single,
  1143. NID_hold_instruction_code, inst,
  1144. 0, 0);
  1145. ASN1_OBJECT_free(inst);
  1146. ASN1_TIME_free(revtm);
  1147. ASN1_GENERALIZEDTIME_free(invtm);
  1148. }
  1149. OCSP_CERTID_free(cid_resp_md);
  1150. }
  1151. OCSP_copy_nonce(bs, req);
  1152. mctx = EVP_MD_CTX_new();
  1153. if ( mctx == NULL || !EVP_DigestSignInit(mctx, &pkctx, rmd, NULL, rkey)) {
  1154. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, NULL);
  1155. goto end;
  1156. }
  1157. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  1158. char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  1159. if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
  1160. BIO_printf(err, "parameter error \"%s\"\n", sigopt);
  1161. ERR_print_errors(bio_err);
  1162. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR,
  1163. NULL);
  1164. goto end;
  1165. }
  1166. }
  1167. if (!OCSP_basic_sign_ctx(bs, rcert, mctx, rother, flags)) {
  1168. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, bs);
  1169. goto end;
  1170. }
  1171. if (badsig) {
  1172. const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs);
  1173. corrupt_signature(sig);
  1174. }
  1175. *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs);
  1176. end:
  1177. EVP_MD_CTX_free(mctx);
  1178. ASN1_TIME_free(thisupd);
  1179. ASN1_TIME_free(nextupd);
  1180. OCSP_BASICRESP_free(bs);
  1181. }
  1182. static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser)
  1183. {
  1184. int i;
  1185. BIGNUM *bn = NULL;
  1186. char *itmp, *row[DB_NUMBER], **rrow;
  1187. for (i = 0; i < DB_NUMBER; i++)
  1188. row[i] = NULL;
  1189. bn = ASN1_INTEGER_to_BN(ser, NULL);
  1190. OPENSSL_assert(bn); /* FIXME: should report an error at this
  1191. * point and abort */
  1192. if (BN_is_zero(bn))
  1193. itmp = OPENSSL_strdup("00");
  1194. else
  1195. itmp = BN_bn2hex(bn);
  1196. row[DB_serial] = itmp;
  1197. BN_free(bn);
  1198. rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
  1199. OPENSSL_free(itmp);
  1200. return rrow;
  1201. }
  1202. /* Quick and dirty OCSP server: read in and parse input request */
  1203. static BIO *init_responder(const char *port)
  1204. {
  1205. # ifdef OPENSSL_NO_SOCK
  1206. BIO_printf(bio_err,
  1207. "Error setting up accept BIO - sockets not supported.\n");
  1208. return NULL;
  1209. # else
  1210. BIO *acbio = NULL, *bufbio = NULL;
  1211. bufbio = BIO_new(BIO_f_buffer());
  1212. if (bufbio == NULL)
  1213. goto err;
  1214. acbio = BIO_new(BIO_s_accept());
  1215. if (acbio == NULL
  1216. || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
  1217. || BIO_set_accept_port(acbio, port) < 0) {
  1218. log_message(LOG_ERR, "Error setting up accept BIO");
  1219. goto err;
  1220. }
  1221. BIO_set_accept_bios(acbio, bufbio);
  1222. bufbio = NULL;
  1223. if (BIO_do_accept(acbio) <= 0) {
  1224. log_message(LOG_ERR, "Error starting accept");
  1225. goto err;
  1226. }
  1227. return acbio;
  1228. err:
  1229. BIO_free_all(acbio);
  1230. BIO_free(bufbio);
  1231. return NULL;
  1232. # endif
  1233. }
  1234. # ifndef OPENSSL_NO_SOCK
  1235. /*
  1236. * Decode %xx URL-decoding in-place. Ignores mal-formed sequences.
  1237. */
  1238. static int urldecode(char *p)
  1239. {
  1240. unsigned char *out = (unsigned char *)p;
  1241. unsigned char *save = out;
  1242. for (; *p; p++) {
  1243. if (*p != '%')
  1244. *out++ = *p;
  1245. else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
  1246. /* Don't check, can't fail because of ixdigit() call. */
  1247. *out++ = (OPENSSL_hexchar2int(p[1]) << 4)
  1248. | OPENSSL_hexchar2int(p[2]);
  1249. p += 2;
  1250. }
  1251. else
  1252. return -1;
  1253. }
  1254. *out = '\0';
  1255. return (int)(out - save);
  1256. }
  1257. # endif
  1258. # ifdef OCSP_DAEMON
  1259. static void sock_timeout(int signum)
  1260. {
  1261. if (acfd != (int)INVALID_SOCKET)
  1262. (void)shutdown(acfd, SHUT_RD);
  1263. }
  1264. # endif
  1265. static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio,
  1266. int timeout)
  1267. {
  1268. # ifdef OPENSSL_NO_SOCK
  1269. return 0;
  1270. # else
  1271. int len;
  1272. OCSP_REQUEST *req = NULL;
  1273. char inbuf[2048], reqbuf[2048];
  1274. char *p, *q;
  1275. BIO *cbio = NULL, *getbio = NULL, *b64 = NULL;
  1276. const char *client;
  1277. *preq = NULL;
  1278. /* Connection loss before accept() is routine, ignore silently */
  1279. if (BIO_do_accept(acbio) <= 0)
  1280. return 0;
  1281. cbio = BIO_pop(acbio);
  1282. *pcbio = cbio;
  1283. client = BIO_get_peer_name(cbio);
  1284. # ifdef OCSP_DAEMON
  1285. if (timeout > 0) {
  1286. (void) BIO_get_fd(cbio, &acfd);
  1287. alarm(timeout);
  1288. }
  1289. # endif
  1290. /* Read the request line. */
  1291. len = BIO_gets(cbio, reqbuf, sizeof(reqbuf));
  1292. if (len <= 0)
  1293. goto out;
  1294. if (strncmp(reqbuf, "GET ", 4) == 0) {
  1295. /* Expecting GET {sp} /URL {sp} HTTP/1.x */
  1296. for (p = reqbuf + 4; *p == ' '; ++p)
  1297. continue;
  1298. if (*p != '/') {
  1299. log_message(LOG_INFO, "Invalid request -- bad URL: %s", client);
  1300. goto out;
  1301. }
  1302. p++;
  1303. /* Splice off the HTTP version identifier. */
  1304. for (q = p; *q; q++)
  1305. if (*q == ' ')
  1306. break;
  1307. if (strncmp(q, " HTTP/1.", 8) != 0) {
  1308. log_message(LOG_INFO,
  1309. "Invalid request -- bad HTTP version: %s", client);
  1310. goto out;
  1311. }
  1312. *q = '\0';
  1313. /*
  1314. * Skip "GET / HTTP..." requests often used by load-balancers
  1315. */
  1316. if (p[1] == '\0')
  1317. goto out;
  1318. len = urldecode(p);
  1319. if (len <= 0) {
  1320. log_message(LOG_INFO,
  1321. "Invalid request -- bad URL encoding: %s", client);
  1322. goto out;
  1323. }
  1324. if ((getbio = BIO_new_mem_buf(p, len)) == NULL
  1325. || (b64 = BIO_new(BIO_f_base64())) == NULL) {
  1326. log_message(LOG_ERR, "Could not allocate base64 bio: %s", client);
  1327. goto out;
  1328. }
  1329. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  1330. getbio = BIO_push(b64, getbio);
  1331. } else if (strncmp(reqbuf, "POST ", 5) != 0) {
  1332. log_message(LOG_INFO, "Invalid request -- bad HTTP verb: %s", client);
  1333. goto out;
  1334. }
  1335. /* Read and skip past the headers. */
  1336. for (;;) {
  1337. len = BIO_gets(cbio, inbuf, sizeof(inbuf));
  1338. if (len <= 0)
  1339. goto out;
  1340. if ((inbuf[0] == '\r') || (inbuf[0] == '\n'))
  1341. break;
  1342. }
  1343. # ifdef OCSP_DAEMON
  1344. /* Clear alarm before we close the client socket */
  1345. alarm(0);
  1346. timeout = 0;
  1347. # endif
  1348. /* Try to read OCSP request */
  1349. if (getbio != NULL) {
  1350. req = d2i_OCSP_REQUEST_bio(getbio, NULL);
  1351. BIO_free_all(getbio);
  1352. } else {
  1353. req = d2i_OCSP_REQUEST_bio(cbio, NULL);
  1354. }
  1355. if (req == NULL)
  1356. log_message(LOG_ERR, "Error parsing OCSP request");
  1357. *preq = req;
  1358. out:
  1359. # ifdef OCSP_DAEMON
  1360. if (timeout > 0)
  1361. alarm(0);
  1362. acfd = (int)INVALID_SOCKET;
  1363. # endif
  1364. return 1;
  1365. # endif
  1366. }
  1367. static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
  1368. {
  1369. char http_resp[] =
  1370. "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n"
  1371. "Content-Length: %d\r\n\r\n";
  1372. if (cbio == NULL)
  1373. return 0;
  1374. BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL));
  1375. i2d_OCSP_RESPONSE_bio(cbio, resp);
  1376. (void)BIO_flush(cbio);
  1377. return 1;
  1378. }
  1379. # ifndef OPENSSL_NO_SOCK
  1380. static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
  1381. const char *path,
  1382. const STACK_OF(CONF_VALUE) *headers,
  1383. OCSP_REQUEST *req, int req_timeout)
  1384. {
  1385. int fd;
  1386. int rv;
  1387. int i;
  1388. int add_host = 1;
  1389. OCSP_REQ_CTX *ctx = NULL;
  1390. OCSP_RESPONSE *rsp = NULL;
  1391. fd_set confds;
  1392. struct timeval tv;
  1393. if (req_timeout != -1)
  1394. BIO_set_nbio(cbio, 1);
  1395. rv = BIO_do_connect(cbio);
  1396. if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) {
  1397. BIO_puts(bio_err, "Error connecting BIO\n");
  1398. return NULL;
  1399. }
  1400. if (BIO_get_fd(cbio, &fd) < 0) {
  1401. BIO_puts(bio_err, "Can't get connection fd\n");
  1402. goto err;
  1403. }
  1404. if (req_timeout != -1 && rv <= 0) {
  1405. FD_ZERO(&confds);
  1406. openssl_fdset(fd, &confds);
  1407. tv.tv_usec = 0;
  1408. tv.tv_sec = req_timeout;
  1409. rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
  1410. if (rv == 0) {
  1411. BIO_puts(bio_err, "Timeout on connect\n");
  1412. return NULL;
  1413. }
  1414. }
  1415. ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
  1416. if (ctx == NULL)
  1417. return NULL;
  1418. for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
  1419. CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
  1420. if (add_host == 1 && strcasecmp("host", hdr->name) == 0)
  1421. add_host = 0;
  1422. if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
  1423. goto err;
  1424. }
  1425. if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0)
  1426. goto err;
  1427. if (!OCSP_REQ_CTX_set1_req(ctx, req))
  1428. goto err;
  1429. for (;;) {
  1430. rv = OCSP_sendreq_nbio(&rsp, ctx);
  1431. if (rv != -1)
  1432. break;
  1433. if (req_timeout == -1)
  1434. continue;
  1435. FD_ZERO(&confds);
  1436. openssl_fdset(fd, &confds);
  1437. tv.tv_usec = 0;
  1438. tv.tv_sec = req_timeout;
  1439. if (BIO_should_read(cbio)) {
  1440. rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
  1441. } else if (BIO_should_write(cbio)) {
  1442. rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
  1443. } else {
  1444. BIO_puts(bio_err, "Unexpected retry condition\n");
  1445. goto err;
  1446. }
  1447. if (rv == 0) {
  1448. BIO_puts(bio_err, "Timeout on request\n");
  1449. break;
  1450. }
  1451. if (rv == -1) {
  1452. BIO_puts(bio_err, "Select error\n");
  1453. break;
  1454. }
  1455. }
  1456. err:
  1457. OCSP_REQ_CTX_free(ctx);
  1458. return rsp;
  1459. }
  1460. OCSP_RESPONSE *process_responder(OCSP_REQUEST *req,
  1461. const char *host, const char *path,
  1462. const char *port, int use_ssl,
  1463. STACK_OF(CONF_VALUE) *headers,
  1464. int req_timeout)
  1465. {
  1466. BIO *cbio = NULL;
  1467. SSL_CTX *ctx = NULL;
  1468. OCSP_RESPONSE *resp = NULL;
  1469. cbio = BIO_new_connect(host);
  1470. if (cbio == NULL) {
  1471. BIO_printf(bio_err, "Error creating connect BIO\n");
  1472. goto end;
  1473. }
  1474. if (port != NULL)
  1475. BIO_set_conn_port(cbio, port);
  1476. if (use_ssl == 1) {
  1477. BIO *sbio;
  1478. ctx = SSL_CTX_new(TLS_client_method());
  1479. if (ctx == NULL) {
  1480. BIO_printf(bio_err, "Error creating SSL context.\n");
  1481. goto end;
  1482. }
  1483. SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
  1484. sbio = BIO_new_ssl(ctx, 1);
  1485. cbio = BIO_push(sbio, cbio);
  1486. }
  1487. resp = query_responder(cbio, host, path, headers, req, req_timeout);
  1488. if (resp == NULL)
  1489. BIO_printf(bio_err, "Error querying OCSP responder\n");
  1490. end:
  1491. BIO_free_all(cbio);
  1492. SSL_CTX_free(ctx);
  1493. return resp;
  1494. }
  1495. # endif
  1496. #endif