crl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /* crl.c
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #ifdef HAVE_CRL
  26. #include <cyassl/internal.h>
  27. #include <cyassl/error-ssl.h>
  28. #include <dirent.h>
  29. #include <sys/stat.h>
  30. #include <string.h>
  31. #ifdef HAVE_CRL_MONITOR
  32. static int StopMonitor(int mfd);
  33. #endif
  34. /* Initialze CRL members */
  35. int InitCRL(CYASSL_CRL* crl, CYASSL_CERT_MANAGER* cm)
  36. {
  37. CYASSL_ENTER("InitCRL");
  38. crl->cm = cm;
  39. crl->crlList = NULL;
  40. crl->monitors[0].path = NULL;
  41. crl->monitors[1].path = NULL;
  42. #ifdef HAVE_CRL_MONITOR
  43. crl->tid = 0;
  44. crl->mfd = -1; /* mfd for bsd is kqueue fd, eventfd for linux */
  45. #endif
  46. if (InitMutex(&crl->crlLock) != 0)
  47. return BAD_MUTEX_E;
  48. return 0;
  49. }
  50. /* Initialze CRL Entry */
  51. static int InitCRL_Entry(CRL_Entry* crle, DecodedCRL* dcrl)
  52. {
  53. CYASSL_ENTER("InitCRL_Entry");
  54. XMEMCPY(crle->issuerHash, dcrl->issuerHash, SHA_DIGEST_SIZE);
  55. /* XMEMCPY(crle->crlHash, dcrl->crlHash, SHA_DIGEST_SIZE);
  56. * copy the hash here if needed for optimized comparisons */
  57. XMEMCPY(crle->lastDate, dcrl->lastDate, MAX_DATE_SIZE);
  58. XMEMCPY(crle->nextDate, dcrl->nextDate, MAX_DATE_SIZE);
  59. crle->lastDateFormat = dcrl->lastDateFormat;
  60. crle->nextDateFormat = dcrl->nextDateFormat;
  61. crle->certs = dcrl->certs; /* take ownsership */
  62. dcrl->certs = NULL;
  63. crle->totalCerts = dcrl->totalCerts;
  64. return 0;
  65. }
  66. /* Free all CRL Entry resources */
  67. static void FreeCRL_Entry(CRL_Entry* crle)
  68. {
  69. RevokedCert* tmp = crle->certs;
  70. CYASSL_ENTER("FreeCRL_Entry");
  71. while(tmp) {
  72. RevokedCert* next = tmp->next;
  73. XFREE(tmp, NULL, DYNAMIC_TYPE_REVOKED);
  74. tmp = next;
  75. }
  76. }
  77. /* Free all CRL resources */
  78. void FreeCRL(CYASSL_CRL* crl, int dynamic)
  79. {
  80. CRL_Entry* tmp = crl->crlList;
  81. CYASSL_ENTER("FreeCRL");
  82. if (crl->monitors[0].path)
  83. XFREE(crl->monitors[0].path, NULL, DYNAMIC_TYPE_CRL_MONITOR);
  84. if (crl->monitors[1].path)
  85. XFREE(crl->monitors[1].path, NULL, DYNAMIC_TYPE_CRL_MONITOR);
  86. while(tmp) {
  87. CRL_Entry* next = tmp->next;
  88. FreeCRL_Entry(tmp);
  89. XFREE(tmp, NULL, DYNAMIC_TYPE_CRL_ENTRY);
  90. tmp = next;
  91. }
  92. #ifdef HAVE_CRL_MONITOR
  93. if (crl->tid != 0) {
  94. CYASSL_MSG("stopping monitor thread");
  95. if (StopMonitor(crl->mfd) == 0)
  96. pthread_join(crl->tid, NULL);
  97. else {
  98. CYASSL_MSG("stop monitor failed, cancel instead");
  99. pthread_cancel(crl->tid);
  100. }
  101. }
  102. #endif
  103. FreeMutex(&crl->crlLock);
  104. if (dynamic) /* free self */
  105. XFREE(crl, NULL, DYNAMIC_TYPE_CRL);
  106. }
  107. /* Is the cert ok with CRL, return 0 on success */
  108. int CheckCertCRL(CYASSL_CRL* crl, DecodedCert* cert)
  109. {
  110. CRL_Entry* crle;
  111. int foundEntry = 0;
  112. int ret = 0;
  113. CYASSL_ENTER("CheckCertCRL");
  114. if (LockMutex(&crl->crlLock) != 0) {
  115. CYASSL_MSG("LockMutex failed");
  116. return BAD_MUTEX_E;
  117. }
  118. crle = crl->crlList;
  119. while (crle) {
  120. if (XMEMCMP(crle->issuerHash, cert->issuerHash, SHA_DIGEST_SIZE) == 0) {
  121. CYASSL_MSG("Found CRL Entry on list");
  122. CYASSL_MSG("Checking next date validity");
  123. if (!ValidateDate(crle->nextDate, crle->nextDateFormat, AFTER)) {
  124. CYASSL_MSG("CRL next date is no longer valid");
  125. ret = ASN_AFTER_DATE_E;
  126. }
  127. else
  128. foundEntry = 1;
  129. break;
  130. }
  131. crle = crle->next;
  132. }
  133. if (foundEntry) {
  134. RevokedCert* rc = crle->certs;
  135. while (rc) {
  136. if (XMEMCMP(rc->serialNumber, cert->serial, rc->serialSz) == 0) {
  137. CYASSL_MSG("Cert revoked");
  138. ret = CRL_CERT_REVOKED;
  139. break;
  140. }
  141. rc = rc->next;
  142. }
  143. }
  144. UnLockMutex(&crl->crlLock);
  145. if (foundEntry == 0) {
  146. CYASSL_MSG("Couldn't find CRL for status check");
  147. ret = CRL_MISSING;
  148. if (crl->cm->cbMissingCRL) {
  149. char url[256];
  150. CYASSL_MSG("Issuing missing CRL callback");
  151. url[0] = '\0';
  152. if (cert->extCrlInfoSz < (int)sizeof(url) -1 ) {
  153. XMEMCPY(url, cert->extCrlInfo, cert->extCrlInfoSz);
  154. url[cert->extCrlInfoSz] = '\0';
  155. }
  156. else {
  157. CYASSL_MSG("CRL url too long");
  158. }
  159. crl->cm->cbMissingCRL(url);
  160. }
  161. }
  162. return ret;
  163. }
  164. /* Add Decoded CRL, 0 on success */
  165. static int AddCRL(CYASSL_CRL* crl, DecodedCRL* dcrl)
  166. {
  167. CRL_Entry* crle;
  168. CYASSL_ENTER("AddCRL");
  169. crle = (CRL_Entry*)XMALLOC(sizeof(CRL_Entry), NULL, DYNAMIC_TYPE_CRL_ENTRY);
  170. if (crle == NULL) {
  171. CYASSL_MSG("alloc CRL Entry failed");
  172. return -1;
  173. }
  174. if (InitCRL_Entry(crle, dcrl) < 0) {
  175. CYASSL_MSG("Init CRL Entry failed");
  176. XFREE(crle, NULL, DYNAMIC_TYPE_CRL_ENTRY);
  177. return -1;
  178. }
  179. if (LockMutex(&crl->crlLock) != 0) {
  180. CYASSL_MSG("LockMutex failed");
  181. FreeCRL_Entry(crle);
  182. XFREE(crle, NULL, DYNAMIC_TYPE_CRL_ENTRY);
  183. return BAD_MUTEX_E;
  184. }
  185. crle->next = crl->crlList;
  186. crl->crlList = crle;
  187. UnLockMutex(&crl->crlLock);
  188. return 0;
  189. }
  190. /* Load CRL File of type, SSL_SUCCESS on ok */
  191. int BufferLoadCRL(CYASSL_CRL* crl, const byte* buff, long sz, int type)
  192. {
  193. int ret = SSL_SUCCESS;
  194. const byte* myBuffer = buff; /* if DER ok, otherwise switch */
  195. buffer der;
  196. #ifdef CYASSL_SMALL_STACK
  197. DecodedCRL* dcrl;
  198. #else
  199. DecodedCRL dcrl[1];
  200. #endif
  201. der.buffer = NULL;
  202. CYASSL_ENTER("BufferLoadCRL");
  203. if (crl == NULL || buff == NULL || sz == 0)
  204. return BAD_FUNC_ARG;
  205. if (type == SSL_FILETYPE_PEM) {
  206. int eccKey = 0; /* not used */
  207. EncryptedInfo info;
  208. info.ctx = NULL;
  209. ret = PemToDer(buff, sz, CRL_TYPE, &der, NULL, &info, &eccKey);
  210. if (ret == 0) {
  211. myBuffer = der.buffer;
  212. sz = der.length;
  213. }
  214. else {
  215. CYASSL_MSG("Pem to Der failed");
  216. return -1;
  217. }
  218. }
  219. #ifdef CYASSL_SMALL_STACK
  220. dcrl = (DecodedCRL*)XMALLOC(sizeof(DecodedCRL), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  221. if (dcrl == NULL) {
  222. if (der.buffer)
  223. XFREE(der.buffer, NULL, DYNAMIC_TYPE_CRL);
  224. return MEMORY_E;
  225. }
  226. #endif
  227. InitDecodedCRL(dcrl);
  228. ret = ParseCRL(dcrl, myBuffer, (word32)sz, crl->cm);
  229. if (ret != 0) {
  230. CYASSL_MSG("ParseCRL error");
  231. }
  232. else {
  233. ret = AddCRL(crl, dcrl);
  234. if (ret != 0) {
  235. CYASSL_MSG("AddCRL error");
  236. }
  237. }
  238. FreeDecodedCRL(dcrl);
  239. #ifdef CYASSL_SMALL_STACK
  240. XFREE(dcrl, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  241. #endif
  242. if (der.buffer)
  243. XFREE(der.buffer, NULL, DYNAMIC_TYPE_CRL);
  244. return ret ? ret : SSL_SUCCESS; /* convert 0 to SSL_SUCCESS */
  245. }
  246. #ifdef HAVE_CRL_MONITOR
  247. /* read in new CRL entries and save new list */
  248. static int SwapLists(CYASSL_CRL* crl)
  249. {
  250. int ret;
  251. CRL_Entry* newList;
  252. #ifdef CYASSL_SMALL_STACK
  253. CYASSL_CRL* tmp;
  254. #else
  255. CYASSL_CRL tmp[1];
  256. #endif
  257. #ifdef CYASSL_SMALL_STACK
  258. tmp = (CYASSL_CRL*)XMALLOC(sizeof(CYASSL_CRL), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  259. if (tmp == NULL)
  260. return MEMORY_E;
  261. #endif
  262. if (InitCRL(tmp, crl->cm) < 0) {
  263. CYASSL_MSG("Init tmp CRL failed");
  264. #ifdef CYASSL_SMALL_STACK
  265. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  266. #endif
  267. return -1;
  268. }
  269. if (crl->monitors[0].path) {
  270. ret = LoadCRL(tmp, crl->monitors[0].path, SSL_FILETYPE_PEM, 0);
  271. if (ret != SSL_SUCCESS) {
  272. CYASSL_MSG("PEM LoadCRL on dir change failed");
  273. FreeCRL(tmp, 0);
  274. #ifdef CYASSL_SMALL_STACK
  275. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  276. #endif
  277. return -1;
  278. }
  279. }
  280. if (crl->monitors[1].path) {
  281. ret = LoadCRL(tmp, crl->monitors[1].path, SSL_FILETYPE_ASN1, 0);
  282. if (ret != SSL_SUCCESS) {
  283. CYASSL_MSG("DER LoadCRL on dir change failed");
  284. FreeCRL(tmp, 0);
  285. #ifdef CYASSL_SMALL_STACK
  286. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  287. #endif
  288. return -1;
  289. }
  290. }
  291. if (LockMutex(&crl->crlLock) != 0) {
  292. CYASSL_MSG("LockMutex failed");
  293. FreeCRL(tmp, 0);
  294. #ifdef CYASSL_SMALL_STACK
  295. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  296. #endif
  297. return -1;
  298. }
  299. newList = tmp->crlList;
  300. /* swap lists */
  301. tmp->crlList = crl->crlList;
  302. crl->crlList = newList;
  303. UnLockMutex(&crl->crlLock);
  304. FreeCRL(tmp, 0);
  305. #ifdef CYASSL_SMALL_STACK
  306. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  307. #endif
  308. return 0;
  309. }
  310. #if (defined(__MACH__) || defined(__FreeBSD__))
  311. #include <sys/types.h>
  312. #include <sys/event.h>
  313. #include <sys/time.h>
  314. #include <fcntl.h>
  315. #include <unistd.h>
  316. #ifdef __MACH__
  317. #define XEVENT_MODE O_EVTONLY
  318. #elif defined(__FreeBSD__)
  319. #define XEVENT_MODE EVFILT_VNODE
  320. #endif
  321. /* we need a unique kqueue user filter fd for crl in case user is doing custom
  322. * events too */
  323. #ifndef CRL_CUSTOM_FD
  324. #define CRL_CUSTOM_FD 123456
  325. #endif
  326. /* shutdown monitor thread, 0 on success */
  327. static int StopMonitor(int mfd)
  328. {
  329. struct kevent change;
  330. /* trigger custom shutdown */
  331. EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
  332. if (kevent(mfd, &change, 1, NULL, 0, NULL) < 0) {
  333. CYASSL_MSG("kevent trigger customer event failed");
  334. return -1;
  335. }
  336. return 0;
  337. }
  338. /* OS X monitoring */
  339. static void* DoMonitor(void* arg)
  340. {
  341. int fPEM, fDER;
  342. struct kevent change;
  343. CYASSL_CRL* crl = (CYASSL_CRL*)arg;
  344. CYASSL_ENTER("DoMonitor");
  345. crl->mfd = kqueue();
  346. if (crl->mfd == -1) {
  347. CYASSL_MSG("kqueue failed");
  348. return NULL;
  349. }
  350. /* listen for custom shutdown event */
  351. EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, EV_ADD, 0, 0, NULL);
  352. if (kevent(crl->mfd, &change, 1, NULL, 0, NULL) < 0) {
  353. CYASSL_MSG("kevent monitor customer event failed");
  354. close(crl->mfd);
  355. return NULL;
  356. }
  357. fPEM = -1;
  358. fDER = -1;
  359. if (crl->monitors[0].path) {
  360. fPEM = open(crl->monitors[0].path, XEVENT_MODE);
  361. if (fPEM == -1) {
  362. CYASSL_MSG("PEM event dir open failed");
  363. close(crl->mfd);
  364. return NULL;
  365. }
  366. }
  367. if (crl->monitors[1].path) {
  368. fDER = open(crl->monitors[1].path, XEVENT_MODE);
  369. if (fDER == -1) {
  370. CYASSL_MSG("DER event dir open failed");
  371. close(crl->mfd);
  372. return NULL;
  373. }
  374. }
  375. if (fPEM != -1)
  376. EV_SET(&change, fPEM, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_ONESHOT,
  377. NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0);
  378. if (fDER != -1)
  379. EV_SET(&change, fDER, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_ONESHOT,
  380. NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0);
  381. for (;;) {
  382. struct kevent event;
  383. int numEvents = kevent(crl->mfd, &change, 1, &event, 1, NULL);
  384. CYASSL_MSG("Got kevent");
  385. if (numEvents == -1) {
  386. CYASSL_MSG("kevent problem, continue");
  387. continue;
  388. }
  389. if (event.filter == EVFILT_USER) {
  390. CYASSL_MSG("Got user shutdown event, breaking out");
  391. break;
  392. }
  393. if (SwapLists(crl) < 0) {
  394. CYASSL_MSG("SwapLists problem, continue");
  395. }
  396. }
  397. if (fPEM != -1)
  398. close(fPEM);
  399. if (fDER != -1)
  400. close(fDER);
  401. close(crl->mfd);
  402. return NULL;
  403. }
  404. #elif defined(__linux__)
  405. #include <sys/types.h>
  406. #include <sys/inotify.h>
  407. #include <sys/eventfd.h>
  408. #include <unistd.h>
  409. #ifndef max
  410. static INLINE int max(int a, int b)
  411. {
  412. return a > b ? a : b;
  413. }
  414. #endif /* max */
  415. /* shutdown monitor thread, 0 on success */
  416. static int StopMonitor(int mfd)
  417. {
  418. word64 w64 = 1;
  419. /* write to our custom event */
  420. if (write(mfd, &w64, sizeof(w64)) < 0) {
  421. CYASSL_MSG("StopMonitor write failed");
  422. return -1;
  423. }
  424. return 0;
  425. }
  426. /* linux monitoring */
  427. static void* DoMonitor(void* arg)
  428. {
  429. int notifyFd;
  430. int wd = -1;
  431. CYASSL_CRL* crl = (CYASSL_CRL*)arg;
  432. #ifdef CYASSL_SMALL_STACK
  433. char* buff;
  434. #else
  435. char buff[8192];
  436. #endif
  437. CYASSL_ENTER("DoMonitor");
  438. crl->mfd = eventfd(0, 0); /* our custom shutdown event */
  439. if (crl->mfd < 0) {
  440. CYASSL_MSG("eventfd failed");
  441. return NULL;
  442. }
  443. notifyFd = inotify_init();
  444. if (notifyFd < 0) {
  445. CYASSL_MSG("inotify failed");
  446. close(crl->mfd);
  447. return NULL;
  448. }
  449. if (crl->monitors[0].path) {
  450. wd = inotify_add_watch(notifyFd, crl->monitors[0].path, IN_CLOSE_WRITE |
  451. IN_DELETE);
  452. if (wd < 0) {
  453. CYASSL_MSG("PEM notify add watch failed");
  454. close(crl->mfd);
  455. close(notifyFd);
  456. return NULL;
  457. }
  458. }
  459. if (crl->monitors[1].path) {
  460. wd = inotify_add_watch(notifyFd, crl->monitors[1].path, IN_CLOSE_WRITE |
  461. IN_DELETE);
  462. if (wd < 0) {
  463. CYASSL_MSG("DER notify add watch failed");
  464. close(crl->mfd);
  465. close(notifyFd);
  466. return NULL;
  467. }
  468. }
  469. #ifdef CYASSL_SMALL_STACK
  470. buff = (char*)XMALLOC(8192, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  471. if (buff == NULL)
  472. return NULL;
  473. #endif
  474. for (;;) {
  475. fd_set readfds;
  476. int result;
  477. int length;
  478. FD_ZERO(&readfds);
  479. FD_SET(notifyFd, &readfds);
  480. FD_SET(crl->mfd, &readfds);
  481. result = select(max(notifyFd, crl->mfd) + 1, &readfds, NULL, NULL,NULL);
  482. CYASSL_MSG("Got notify event");
  483. if (result < 0) {
  484. CYASSL_MSG("select problem, continue");
  485. continue;
  486. }
  487. if (FD_ISSET(crl->mfd, &readfds)) {
  488. CYASSL_MSG("got custom shutdown event, breaking out");
  489. break;
  490. }
  491. length = read(notifyFd, buff, 8192);
  492. if (length < 0) {
  493. CYASSL_MSG("notify read problem, continue");
  494. continue;
  495. }
  496. if (SwapLists(crl) < 0) {
  497. CYASSL_MSG("SwapLists problem, continue");
  498. }
  499. }
  500. #ifdef CYASSL_SMALL_STACK
  501. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  502. #endif
  503. if (wd > 0)
  504. inotify_rm_watch(notifyFd, wd);
  505. close(crl->mfd);
  506. close(notifyFd);
  507. return NULL;
  508. }
  509. #else
  510. #error "CRL monitor only currently supported on linux or mach"
  511. #endif /* MACH or linux */
  512. /* Start Monitoring the CRL path(s) in a thread */
  513. static int StartMonitorCRL(CYASSL_CRL* crl)
  514. {
  515. pthread_attr_t attr;
  516. CYASSL_ENTER("StartMonitorCRL");
  517. if (crl == NULL)
  518. return BAD_FUNC_ARG;
  519. if (crl->tid != 0) {
  520. CYASSL_MSG("Monitor thread already running");
  521. return MONITOR_RUNNING_E;
  522. }
  523. pthread_attr_init(&attr);
  524. if (pthread_create(&crl->tid, &attr, DoMonitor, crl) != 0) {
  525. CYASSL_MSG("Thread creation error");
  526. return THREAD_CREATE_E;
  527. }
  528. return SSL_SUCCESS;
  529. }
  530. #else /* HAVE_CRL_MONITOR */
  531. static int StartMonitorCRL(CYASSL_CRL* crl)
  532. {
  533. (void)crl;
  534. CYASSL_ENTER("StartMonitorCRL");
  535. CYASSL_MSG("Not compiled in");
  536. return NOT_COMPILED_IN;
  537. }
  538. #endif /* HAVE_CRL_MONITOR */
  539. /* Load CRL path files of type, SSL_SUCCESS on ok */
  540. int LoadCRL(CYASSL_CRL* crl, const char* path, int type, int monitor)
  541. {
  542. struct dirent* entry;
  543. DIR* dir;
  544. int ret = SSL_SUCCESS;
  545. #ifdef CYASSL_SMALL_STACK
  546. char* name;
  547. #else
  548. char name[MAX_FILENAME_SZ];
  549. #endif
  550. CYASSL_ENTER("LoadCRL");
  551. if (crl == NULL)
  552. return BAD_FUNC_ARG;
  553. dir = opendir(path);
  554. if (dir == NULL) {
  555. CYASSL_MSG("opendir path crl load failed");
  556. return BAD_PATH_ERROR;
  557. }
  558. #ifdef CYASSL_SMALL_STACK
  559. name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  560. if (name == NULL)
  561. return MEMORY_E;
  562. #endif
  563. while ( (entry = readdir(dir)) != NULL) {
  564. struct stat s;
  565. XMEMSET(name, 0, MAX_FILENAME_SZ);
  566. XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 2);
  567. XSTRNCAT(name, "/", 1);
  568. XSTRNCAT(name, entry->d_name, MAX_FILENAME_SZ/2);
  569. if (stat(name, &s) != 0) {
  570. CYASSL_MSG("stat on name failed");
  571. continue;
  572. }
  573. if (s.st_mode & S_IFREG) {
  574. if (type == SSL_FILETYPE_PEM) {
  575. if (strstr(entry->d_name, ".pem") == NULL) {
  576. CYASSL_MSG("not .pem file, skipping");
  577. continue;
  578. }
  579. }
  580. else {
  581. if (strstr(entry->d_name, ".der") == NULL &&
  582. strstr(entry->d_name, ".crl") == NULL) {
  583. CYASSL_MSG("not .der or .crl file, skipping");
  584. continue;
  585. }
  586. }
  587. if (ProcessFile(NULL, name, type, CRL_TYPE, NULL, 0, crl)
  588. != SSL_SUCCESS) {
  589. CYASSL_MSG("CRL file load failed, continuing");
  590. }
  591. }
  592. }
  593. #ifdef CYASSL_SMALL_STACK
  594. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  595. #endif
  596. if (monitor & CYASSL_CRL_MONITOR) {
  597. CYASSL_MSG("monitor path requested");
  598. if (type == SSL_FILETYPE_PEM) {
  599. crl->monitors[0].path = strdup(path);
  600. crl->monitors[0].type = SSL_FILETYPE_PEM;
  601. if (crl->monitors[0].path == NULL)
  602. ret = MEMORY_E;
  603. } else {
  604. crl->monitors[1].path = strdup(path);
  605. crl->monitors[1].type = SSL_FILETYPE_ASN1;
  606. if (crl->monitors[1].path == NULL)
  607. ret = MEMORY_E;
  608. }
  609. if (monitor & CYASSL_CRL_START_MON) {
  610. CYASSL_MSG("start monitoring requested");
  611. ret = StartMonitorCRL(crl);
  612. }
  613. }
  614. closedir(dir);
  615. return ret;
  616. }
  617. #endif /* HAVE_CRL */