wolfssl/src/crl.c

792 lines
19 KiB
C
Raw Normal View History

2012-05-11 23:22:16 +04:00
/* crl.c
*
2014-04-12 01:58:58 +04:00
* Copyright (C) 2006-2014 wolfSSL Inc.
2012-05-11 23:22:16 +04:00
*
2014-12-19 21:27:01 +03:00
* This file is part of wolfSSL. (formerly known as CyaSSL)
2012-05-11 23:22:16 +04:00
*
2014-12-19 21:27:01 +03:00
* wolfSSL is free software; you can redistribute it and/or modify
2012-05-11 23:22:16 +04:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
2014-12-19 21:27:01 +03:00
* wolfSSL is distributed in the hope that it will be useful,
2012-05-11 23:22:16 +04:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2014-04-12 01:58:58 +04:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2012-05-11 23:22:16 +04:00
*/
2014-12-19 00:06:27 +03:00
/* Name change compatibility layer */
2014-12-19 22:50:30 +03:00
#include <cyassl/ssl.h>
2014-12-19 00:06:27 +03:00
2012-05-11 23:22:16 +04:00
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2014-12-19 21:27:01 +03:00
#include <wolfssl/wolfcrypt/settings.h>
2012-05-11 23:22:16 +04:00
#ifdef HAVE_CRL
2014-12-19 21:27:01 +03:00
#include <wolfssl/internal.h>
#include <wolfssl/error-ssl.h>
2012-05-11 23:22:16 +04:00
2012-05-17 04:04:56 +04:00
#include <dirent.h>
#include <sys/stat.h>
2012-05-17 04:04:56 +04:00
#include <string.h>
2012-05-11 23:22:16 +04:00
#ifdef HAVE_CRL_MONITOR
static int StopMonitor(int mfd);
#endif
2014-05-01 20:30:17 +04:00
2012-05-11 23:22:16 +04:00
2012-05-17 04:04:56 +04:00
/* Initialze CRL members */
2014-12-19 21:27:01 +03:00
int InitCRL(WOLFSSL_CRL* crl, WOLFSSL_CERT_MANAGER* cm)
2012-05-17 04:04:56 +04:00
{
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("InitCRL");
2012-05-11 23:22:16 +04:00
2012-05-25 07:10:38 +04:00
crl->cm = cm;
crl->crlList = NULL;
2012-05-23 04:25:15 +04:00
crl->monitors[0].path = NULL;
crl->monitors[1].path = NULL;
#ifdef HAVE_CRL_MONITOR
crl->tid = 0;
2014-05-02 21:14:40 +04:00
crl->mfd = -1; /* mfd for bsd is kqueue fd, eventfd for linux */
2012-05-23 04:25:15 +04:00
#endif
2012-05-25 07:10:38 +04:00
if (InitMutex(&crl->crlLock) != 0)
return BAD_MUTEX_E;
2012-05-11 23:22:16 +04:00
2012-05-25 07:10:38 +04:00
return 0;
2012-05-17 04:04:56 +04:00
}
2012-05-11 23:22:16 +04:00
2012-05-17 04:04:56 +04:00
/* Initialze CRL Entry */
static int InitCRL_Entry(CRL_Entry* crle, DecodedCRL* dcrl)
{
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("InitCRL_Entry");
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
XMEMCPY(crle->issuerHash, dcrl->issuerHash, SHA_DIGEST_SIZE);
/* XMEMCPY(crle->crlHash, dcrl->crlHash, SHA_DIGEST_SIZE);
* copy the hash here if needed for optimized comparisons */
2012-05-25 07:10:38 +04:00
XMEMCPY(crle->lastDate, dcrl->lastDate, MAX_DATE_SIZE);
XMEMCPY(crle->nextDate, dcrl->nextDate, MAX_DATE_SIZE);
2012-05-25 01:07:59 +04:00
crle->lastDateFormat = dcrl->lastDateFormat;
crle->nextDateFormat = dcrl->nextDateFormat;
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
crle->certs = dcrl->certs; /* take ownsership */
dcrl->certs = NULL;
crle->totalCerts = dcrl->totalCerts;
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
return 0;
2012-05-17 04:04:56 +04:00
}
/* Free all CRL Entry resources */
static void FreeCRL_Entry(CRL_Entry* crle)
{
2012-05-25 07:10:38 +04:00
RevokedCert* tmp = crle->certs;
2012-05-17 04:04:56 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("FreeCRL_Entry");
2012-05-17 04:04:56 +04:00
while(tmp) {
RevokedCert* next = tmp->next;
XFREE(tmp, NULL, DYNAMIC_TYPE_REVOKED);
tmp = next;
}
}
/* Free all CRL resources */
2014-12-19 21:27:01 +03:00
void FreeCRL(WOLFSSL_CRL* crl, int dynamic)
2012-05-17 04:04:56 +04:00
{
2012-05-25 07:10:38 +04:00
CRL_Entry* tmp = crl->crlList;
2012-05-17 04:04:56 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("FreeCRL");
2012-05-17 04:04:56 +04:00
2012-05-23 04:25:15 +04:00
if (crl->monitors[0].path)
XFREE(crl->monitors[0].path, NULL, DYNAMIC_TYPE_CRL_MONITOR);
if (crl->monitors[1].path)
XFREE(crl->monitors[1].path, NULL, DYNAMIC_TYPE_CRL_MONITOR);
2012-05-17 04:04:56 +04:00
while(tmp) {
CRL_Entry* next = tmp->next;
FreeCRL_Entry(tmp);
XFREE(tmp, NULL, DYNAMIC_TYPE_CRL_ENTRY);
tmp = next;
}
2012-05-23 04:25:15 +04:00
#ifdef HAVE_CRL_MONITOR
if (crl->tid != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("stopping monitor thread");
if (StopMonitor(crl->mfd) == 0)
pthread_join(crl->tid, NULL);
else {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("stop monitor failed, cancel instead");
pthread_cancel(crl->tid);
}
2012-05-23 04:25:15 +04:00
}
#endif
2012-05-17 04:04:56 +04:00
FreeMutex(&crl->crlLock);
2012-11-27 23:36:36 +04:00
if (dynamic) /* free self */
XFREE(crl, NULL, DYNAMIC_TYPE_CRL);
2012-05-17 04:04:56 +04:00
}
/* Is the cert ok with CRL, return 0 on success */
2014-12-19 21:27:01 +03:00
int CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert)
2012-05-17 04:04:56 +04:00
{
2012-05-25 01:07:59 +04:00
CRL_Entry* crle;
int foundEntry = 0;
int ret = 0;
2012-05-17 04:04:56 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("CheckCertCRL");
2012-05-17 04:04:56 +04:00
2012-05-25 01:07:59 +04:00
if (LockMutex(&crl->crlLock) != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("LockMutex failed");
return BAD_MUTEX_E;
2012-05-25 01:07:59 +04:00
}
2012-05-17 04:04:56 +04:00
2012-05-25 01:07:59 +04:00
crle = crl->crlList;
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
while (crle) {
if (XMEMCMP(crle->issuerHash, cert->issuerHash, SHA_DIGEST_SIZE) == 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Found CRL Entry on list");
WOLFSSL_MSG("Checking next date validity");
2012-05-25 01:07:59 +04:00
if (!ValidateDate(crle->nextDate, crle->nextDateFormat, AFTER)) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("CRL next date is no longer valid");
2012-05-25 01:07:59 +04:00
ret = ASN_AFTER_DATE_E;
}
else
foundEntry = 1;
break;
2012-05-25 07:10:38 +04:00
}
2012-05-25 01:07:59 +04:00
crle = crle->next;
2012-05-25 07:10:38 +04:00
}
if (foundEntry) {
RevokedCert* rc = crle->certs;
while (rc) {
if (XMEMCMP(rc->serialNumber, cert->serial, rc->serialSz) == 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Cert revoked");
2012-05-25 07:10:38 +04:00
ret = CRL_CERT_REVOKED;
break;
2012-05-18 04:44:54 +04:00
}
2012-05-25 07:10:38 +04:00
rc = rc->next;
}
}
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
UnLockMutex(&crl->crlLock);
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
if (foundEntry == 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Couldn't find CRL for status check");
2012-05-25 07:10:38 +04:00
ret = CRL_MISSING;
if (crl->cm->cbMissingCRL) {
char url[256];
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Issuing missing CRL callback");
2012-05-25 07:10:38 +04:00
url[0] = '\0';
if (cert->extCrlInfoSz < (int)sizeof(url) -1 ) {
XMEMCPY(url, cert->extCrlInfo, cert->extCrlInfoSz);
url[cert->extCrlInfoSz] = '\0';
}
else {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("CRL url too long");
2012-05-25 07:10:38 +04:00
}
crl->cm->cbMissingCRL(url);
}
}
return ret;
2012-05-17 04:04:56 +04:00
}
/* Add Decoded CRL, 0 on success */
2014-12-19 21:27:01 +03:00
static int AddCRL(WOLFSSL_CRL* crl, DecodedCRL* dcrl)
2012-05-17 04:04:56 +04:00
{
2012-05-25 07:10:38 +04:00
CRL_Entry* crle;
2012-05-17 04:04:56 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("AddCRL");
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
crle = (CRL_Entry*)XMALLOC(sizeof(CRL_Entry), NULL, DYNAMIC_TYPE_CRL_ENTRY);
if (crle == NULL) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("alloc CRL Entry failed");
2012-05-25 07:10:38 +04:00
return -1;
}
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
if (InitCRL_Entry(crle, dcrl) < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Init CRL Entry failed");
2012-05-19 03:01:21 +04:00
XFREE(crle, NULL, DYNAMIC_TYPE_CRL_ENTRY);
2012-05-25 07:10:38 +04:00
return -1;
}
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
if (LockMutex(&crl->crlLock) != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("LockMutex failed");
2012-05-25 07:10:38 +04:00
FreeCRL_Entry(crle);
2012-05-19 03:01:21 +04:00
XFREE(crle, NULL, DYNAMIC_TYPE_CRL_ENTRY);
return BAD_MUTEX_E;
2012-05-25 07:10:38 +04:00
}
crle->next = crl->crlList;
crl->crlList = crle;
UnLockMutex(&crl->crlLock);
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
return 0;
2012-05-17 04:04:56 +04:00
}
/* Load CRL File of type, SSL_SUCCESS on ok */
2014-12-19 21:27:01 +03:00
int BufferLoadCRL(WOLFSSL_CRL* crl, const byte* buff, long sz, int type)
2012-05-17 04:04:56 +04:00
{
2012-05-25 07:10:38 +04:00
int ret = SSL_SUCCESS;
const byte* myBuffer = buff; /* if DER ok, otherwise switch */
buffer der;
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
DecodedCRL* dcrl;
#else
DecodedCRL dcrl[1];
#endif
2012-05-25 07:10:38 +04:00
der.buffer = NULL;
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("BufferLoadCRL");
2012-05-25 07:10:38 +04:00
if (crl == NULL || buff == NULL || sz == 0)
return BAD_FUNC_ARG;
if (type == SSL_FILETYPE_PEM) {
int eccKey = 0; /* not used */
EncryptedInfo info;
info.ctx = NULL;
ret = PemToDer(buff, sz, CRL_TYPE, &der, NULL, &info, &eccKey);
if (ret == 0) {
myBuffer = der.buffer;
sz = der.length;
2012-05-18 04:44:54 +04:00
}
2012-05-25 07:10:38 +04:00
else {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Pem to Der failed");
2012-05-25 07:10:38 +04:00
return -1;
}
}
2012-05-17 04:04:56 +04:00
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
dcrl = (DecodedCRL*)XMALLOC(sizeof(DecodedCRL), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (dcrl == NULL) {
if (der.buffer)
XFREE(der.buffer, NULL, DYNAMIC_TYPE_CRL);
return MEMORY_E;
}
#endif
InitDecodedCRL(dcrl);
ret = ParseCRL(dcrl, myBuffer, (word32)sz, crl->cm);
2012-05-25 07:10:38 +04:00
if (ret != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("ParseCRL error");
2012-05-25 07:10:38 +04:00
}
else {
ret = AddCRL(crl, dcrl);
2012-05-25 07:10:38 +04:00
if (ret != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("AddCRL error");
2012-05-25 07:10:38 +04:00
}
}
FreeDecodedCRL(dcrl);
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(dcrl, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-05-17 04:04:56 +04:00
2012-05-25 07:10:38 +04:00
if (der.buffer)
XFREE(der.buffer, NULL, DYNAMIC_TYPE_CRL);
return ret ? ret : SSL_SUCCESS; /* convert 0 to SSL_SUCCESS */
2012-05-17 04:04:56 +04:00
}
2012-05-23 04:25:15 +04:00
#ifdef HAVE_CRL_MONITOR
/* read in new CRL entries and save new list */
2014-12-19 21:27:01 +03:00
static int SwapLists(WOLFSSL_CRL* crl)
2012-05-23 04:25:15 +04:00
{
int ret;
CRL_Entry* newList;
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
WOLFSSL_CRL* tmp;
#else
2014-12-19 21:27:01 +03:00
WOLFSSL_CRL tmp[1];
#endif
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
tmp = (WOLFSSL_CRL*)XMALLOC(sizeof(WOLFSSL_CRL), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL)
return MEMORY_E;
#endif
2012-05-23 04:25:15 +04:00
if (InitCRL(tmp, crl->cm) < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Init tmp CRL failed");
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-05-23 04:25:15 +04:00
return -1;
}
if (crl->monitors[0].path) {
ret = LoadCRL(tmp, crl->monitors[0].path, SSL_FILETYPE_PEM, 0);
2012-05-23 04:25:15 +04:00
if (ret != SSL_SUCCESS) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("PEM LoadCRL on dir change failed");
FreeCRL(tmp, 0);
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-05-23 04:25:15 +04:00
return -1;
}
}
if (crl->monitors[1].path) {
ret = LoadCRL(tmp, crl->monitors[1].path, SSL_FILETYPE_ASN1, 0);
2012-05-23 04:25:15 +04:00
if (ret != SSL_SUCCESS) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("DER LoadCRL on dir change failed");
FreeCRL(tmp, 0);
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-05-23 04:25:15 +04:00
return -1;
}
}
if (LockMutex(&crl->crlLock) != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("LockMutex failed");
FreeCRL(tmp, 0);
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-05-23 04:25:15 +04:00
return -1;
}
newList = tmp->crlList;
2012-05-23 04:25:15 +04:00
/* swap lists */
tmp->crlList = crl->crlList;
2012-05-23 04:25:15 +04:00
crl->crlList = newList;
UnLockMutex(&crl->crlLock);
FreeCRL(tmp, 0);
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-05-23 04:25:15 +04:00
return 0;
}
2012-11-17 01:25:45 +04:00
#if (defined(__MACH__) || defined(__FreeBSD__))
2012-05-23 04:25:15 +04:00
2012-11-17 01:25:45 +04:00
#include <sys/types.h>
2012-05-23 04:25:15 +04:00
#include <sys/event.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
2012-05-23 04:25:15 +04:00
2012-11-17 01:25:45 +04:00
#ifdef __MACH__
#define XEVENT_MODE O_EVTONLY
#elif defined(__FreeBSD__)
#define XEVENT_MODE EVFILT_VNODE
#endif
/* we need a unique kqueue user filter fd for crl in case user is doing custom
* events too */
#ifndef CRL_CUSTOM_FD
#define CRL_CUSTOM_FD 123456
#endif
/* shutdown monitor thread, 0 on success */
static int StopMonitor(int mfd)
{
struct kevent change;
/* trigger custom shutdown */
EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
if (kevent(mfd, &change, 1, NULL, 0, NULL) < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("kevent trigger customer event failed");
return -1;
}
return 0;
}
2012-05-23 04:25:15 +04:00
/* OS X monitoring */
static void* DoMonitor(void* arg)
{
int fPEM, fDER;
2012-05-23 04:25:15 +04:00
struct kevent change;
2014-12-19 21:27:01 +03:00
WOLFSSL_CRL* crl = (WOLFSSL_CRL*)arg;
2012-05-23 04:25:15 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("DoMonitor");
2012-05-23 04:25:15 +04:00
crl->mfd = kqueue();
if (crl->mfd == -1) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("kqueue failed");
2012-05-23 04:25:15 +04:00
return NULL;
}
/* listen for custom shutdown event */
EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, EV_ADD, 0, 0, NULL);
if (kevent(crl->mfd, &change, 1, NULL, 0, NULL) < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("kevent monitor customer event failed");
close(crl->mfd);
return NULL;
}
2012-05-23 04:25:15 +04:00
fPEM = -1;
fDER = -1;
if (crl->monitors[0].path) {
2012-11-17 01:25:45 +04:00
fPEM = open(crl->monitors[0].path, XEVENT_MODE);
2012-05-23 04:25:15 +04:00
if (fPEM == -1) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("PEM event dir open failed");
close(crl->mfd);
2012-05-23 04:25:15 +04:00
return NULL;
}
}
if (crl->monitors[1].path) {
2012-11-17 01:25:45 +04:00
fDER = open(crl->monitors[1].path, XEVENT_MODE);
2012-05-23 04:25:15 +04:00
if (fDER == -1) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("DER event dir open failed");
close(crl->mfd);
2012-05-23 04:25:15 +04:00
return NULL;
}
}
if (fPEM != -1)
EV_SET(&change, fPEM, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_ONESHOT,
NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0);
if (fDER != -1)
EV_SET(&change, fDER, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_ONESHOT,
NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0);
for (;;) {
struct kevent event;
int numEvents = kevent(crl->mfd, &change, 1, &event, 1, NULL);
2012-05-23 04:25:15 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Got kevent");
2012-05-23 04:25:15 +04:00
if (numEvents == -1) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("kevent problem, continue");
2012-05-23 04:25:15 +04:00
continue;
}
if (event.filter == EVFILT_USER) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Got user shutdown event, breaking out");
break;
}
2012-05-23 04:25:15 +04:00
if (SwapLists(crl) < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("SwapLists problem, continue");
2012-05-23 04:25:15 +04:00
}
}
if (fPEM != -1)
close(fPEM);
if (fDER != -1)
close(fDER);
close(crl->mfd);
2012-05-23 04:25:15 +04:00
return NULL;
}
#elif defined(__linux__)
2012-05-23 04:25:15 +04:00
#include <sys/types.h>
2012-05-24 03:55:26 +04:00
#include <sys/inotify.h>
2014-05-02 21:14:40 +04:00
#include <sys/eventfd.h>
2012-05-24 03:55:26 +04:00
#include <unistd.h>
2012-05-23 04:25:15 +04:00
2014-05-02 21:14:40 +04:00
#ifndef max
static INLINE int max(int a, int b)
{
return a > b ? a : b;
}
#endif /* max */
/* shutdown monitor thread, 0 on success */
static int StopMonitor(int mfd)
{
2014-05-02 21:14:40 +04:00
word64 w64 = 1;
/* write to our custom event */
if (write(mfd, &w64, sizeof(w64)) < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("StopMonitor write failed");
2014-05-02 21:14:40 +04:00
return -1;
}
2014-05-02 21:14:40 +04:00
return 0;
}
2012-05-23 04:25:15 +04:00
/* linux monitoring */
static void* DoMonitor(void* arg)
{
int notifyFd;
2014-06-13 03:01:34 +04:00
int wd = -1;
2014-12-19 21:27:01 +03:00
WOLFSSL_CRL* crl = (WOLFSSL_CRL*)arg;
#ifdef WOLFSSL_SMALL_STACK
char* buff;
#else
char buff[8192];
#endif
2012-05-23 04:25:15 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("DoMonitor");
2012-05-23 04:25:15 +04:00
2014-05-02 21:14:40 +04:00
crl->mfd = eventfd(0, 0); /* our custom shutdown event */
if (crl->mfd < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("eventfd failed");
2014-05-02 21:14:40 +04:00
return NULL;
}
2012-05-23 04:25:15 +04:00
notifyFd = inotify_init();
if (notifyFd < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("inotify failed");
2014-05-02 21:14:40 +04:00
close(crl->mfd);
2012-05-23 04:25:15 +04:00
return NULL;
}
if (crl->monitors[0].path) {
2012-05-24 03:55:26 +04:00
wd = inotify_add_watch(notifyFd, crl->monitors[0].path, IN_CLOSE_WRITE |
2012-05-23 04:25:15 +04:00
IN_DELETE);
if (wd < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("PEM notify add watch failed");
2014-05-02 21:14:40 +04:00
close(crl->mfd);
close(notifyFd);
2012-05-23 04:25:15 +04:00
return NULL;
}
}
if (crl->monitors[1].path) {
2012-05-24 03:55:26 +04:00
wd = inotify_add_watch(notifyFd, crl->monitors[1].path, IN_CLOSE_WRITE |
2012-05-23 04:25:15 +04:00
IN_DELETE);
if (wd < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("DER notify add watch failed");
2014-05-02 21:14:40 +04:00
close(crl->mfd);
close(notifyFd);
2012-05-23 04:25:15 +04:00
return NULL;
}
}
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
buff = (char*)XMALLOC(8192, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (buff == NULL)
return NULL;
#endif
2012-05-23 04:25:15 +04:00
for (;;) {
fd_set readfds;
int result;
int length;
2014-05-02 21:14:40 +04:00
FD_ZERO(&readfds);
FD_SET(notifyFd, &readfds);
FD_SET(crl->mfd, &readfds);
result = select(max(notifyFd, crl->mfd) + 1, &readfds, NULL, NULL,NULL);
2012-05-23 04:25:15 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Got notify event");
2012-05-23 04:25:15 +04:00
2014-05-02 21:14:40 +04:00
if (result < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("select problem, continue");
2014-05-02 21:14:40 +04:00
continue;
}
if (FD_ISSET(crl->mfd, &readfds)) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("got custom shutdown event, breaking out");
2014-05-02 21:14:40 +04:00
break;
}
length = read(notifyFd, buff, 8192);
2012-05-23 04:25:15 +04:00
if (length < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("notify read problem, continue");
2012-05-23 04:25:15 +04:00
continue;
}
if (SwapLists(crl) < 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("SwapLists problem, continue");
2012-05-23 04:25:15 +04:00
}
}
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (wd > 0)
inotify_rm_watch(notifyFd, wd);
2014-05-02 21:14:40 +04:00
close(crl->mfd);
close(notifyFd);
2012-05-23 04:25:15 +04:00
return NULL;
}
#else
#error "CRL monitor only currently supported on linux or mach"
2012-05-23 04:25:15 +04:00
#endif /* MACH or linux */
/* Start Monitoring the CRL path(s) in a thread */
2014-12-19 21:27:01 +03:00
static int StartMonitorCRL(WOLFSSL_CRL* crl)
2012-05-23 04:25:15 +04:00
{
pthread_attr_t attr;
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("StartMonitorCRL");
2012-05-23 04:25:15 +04:00
if (crl == NULL)
return BAD_FUNC_ARG;
if (crl->tid != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Monitor thread already running");
2012-05-23 04:25:15 +04:00
return MONITOR_RUNNING_E;
}
pthread_attr_init(&attr);
if (pthread_create(&crl->tid, &attr, DoMonitor, crl) != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("Thread creation error");
2012-05-23 04:25:15 +04:00
return THREAD_CREATE_E;
}
return SSL_SUCCESS;
}
#else /* HAVE_CRL_MONITOR */
2014-12-19 21:27:01 +03:00
static int StartMonitorCRL(WOLFSSL_CRL* crl)
2012-05-23 04:25:15 +04:00
{
2012-11-02 01:14:40 +04:00
(void)crl;
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("StartMonitorCRL");
WOLFSSL_MSG("Not compiled in");
2012-06-05 22:31:00 +04:00
2012-05-23 04:25:15 +04:00
return NOT_COMPILED_IN;
}
#endif /* HAVE_CRL_MONITOR */
2012-05-17 04:04:56 +04:00
/* Load CRL path files of type, SSL_SUCCESS on ok */
2014-12-19 21:27:01 +03:00
int LoadCRL(WOLFSSL_CRL* crl, const char* path, int type, int monitor)
2012-05-17 04:04:56 +04:00
{
2012-05-25 07:10:38 +04:00
struct dirent* entry;
DIR* dir;
int ret = SSL_SUCCESS;
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
char* name;
#else
char name[MAX_FILENAME_SZ];
#endif
2012-05-25 07:10:38 +04:00
2014-12-19 21:27:01 +03:00
WOLFSSL_ENTER("LoadCRL");
2012-05-25 07:10:38 +04:00
if (crl == NULL)
return BAD_FUNC_ARG;
dir = opendir(path);
if (dir == NULL) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("opendir path crl load failed");
2012-05-25 07:10:38 +04:00
return BAD_PATH_ERROR;
}
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (name == NULL)
return MEMORY_E;
#endif
2012-05-25 07:10:38 +04:00
while ( (entry = readdir(dir)) != NULL) {
struct stat s;
XMEMSET(name, 0, MAX_FILENAME_SZ);
XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 2);
XSTRNCAT(name, "/", 1);
XSTRNCAT(name, entry->d_name, MAX_FILENAME_SZ/2);
if (stat(name, &s) != 0) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("stat on name failed");
continue;
}
if (s.st_mode & S_IFREG) {
2012-05-25 07:10:38 +04:00
if (type == SSL_FILETYPE_PEM) {
if (strstr(entry->d_name, ".pem") == NULL) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("not .pem file, skipping");
2012-05-25 07:10:38 +04:00
continue;
}
}
else {
if (strstr(entry->d_name, ".der") == NULL &&
strstr(entry->d_name, ".crl") == NULL) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("not .der or .crl file, skipping");
2012-05-25 07:10:38 +04:00
continue;
}
}
if (ProcessFile(NULL, name, type, CRL_TYPE, NULL, 0, crl)
2012-05-25 01:07:59 +04:00
!= SSL_SUCCESS) {
2014-12-19 21:27:01 +03:00
WOLFSSL_MSG("CRL file load failed, continuing");
2012-05-25 01:07:59 +04:00
}
2012-05-25 07:10:38 +04:00
}
}
2012-05-17 04:04:56 +04:00
2014-12-19 21:27:01 +03:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2014-12-19 21:27:01 +03:00
if (monitor & WOLFSSL_CRL_MONITOR) {
WOLFSSL_MSG("monitor path requested");
2012-05-23 04:25:15 +04:00
if (type == SSL_FILETYPE_PEM) {
crl->monitors[0].path = strdup(path);
crl->monitors[0].type = SSL_FILETYPE_PEM;
if (crl->monitors[0].path == NULL)
ret = MEMORY_E;
} else {
crl->monitors[1].path = strdup(path);
crl->monitors[1].type = SSL_FILETYPE_ASN1;
if (crl->monitors[1].path == NULL)
ret = MEMORY_E;
}
2014-12-19 21:27:01 +03:00
if (monitor & WOLFSSL_CRL_START_MON) {
WOLFSSL_MSG("start monitoring requested");
2012-05-23 04:25:15 +04:00
ret = StartMonitorCRL(crl);
}
}
2012-11-27 23:36:36 +04:00
closedir(dir);
2012-05-25 07:10:38 +04:00
return ret;
2012-05-17 04:04:56 +04:00
}
2012-05-11 23:22:16 +04:00
#endif /* HAVE_CRL */