Skip to content

Commit f8a0fc7

Browse files
committed
wolfKeyMgr v0.4
* Fix for stray `wolfSSL_CTX_free`. * Added mutex protection on ETSI client. * Added URL decoding to HTTP module. * Added SIGPIPE ignore to ETSI client. * Added test key/certificate for Apache HTTPD. * Added "-r" argument for key manager for the key update interval.
1 parent d611ffa commit f8a0fc7

File tree

17 files changed

+146
-39
lines changed

17 files changed

+146
-39
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ wolfKeyManager 0.3
9999
-f <str> Log file name, default None
100100
-o <num> Max open files, default 1024
101101
-s <num> Seconds to timeout, default 60
102+
-r <num> Key renewal timeout, default 3600
102103
-t <num> Thread pool size, default 48
103104
-d TLS Disable Mutual Authentication
104105
-k <pem> TLS Server TLS Key, default ./certs/server-key.pem
105106
-w <pass> TLS Server Key Password, default wolfssl
106107
-c <pem> TLS Server Certificate, default ./certs/server-cert.pem
107-
-c <pem> TLS CA Certificate, default ./certs/ca-cert.pem
108+
-A <pem> TLS CA Certificate, default ./certs/ca-cert.pem
108109
```
109110
110111
```sh

certs/test-cert.pem

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICajCCAg+gAwIBAgIUMkpxF7cixTbgy2s3ZtLYCCq7pmEwCgYIKoZIzj0EAwIw
3+
gYkxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEQMA4GA1UEBwwHU2VhdHRsZTEQ
4+
MA4GA1UECgwHd29sZlNTTDEUMBIGA1UECwwLRGV2ZWxvcG1lbnQxEjAQBgNVBAMM
5+
CWxvY2FsaG9zdDEfMB0GCSqGSIb3DQEJARYQaW5mb0B3b2xmc3NsLmNvbTAeFw0y
6+
MTAzMDExOTU2MjFaFw00MTAyMjQxOTU2MjFaMIGJMQswCQYDVQQGEwJVUzELMAkG
7+
A1UECAwCQ0ExEDAOBgNVBAcMB1NlYXR0bGUxEDAOBgNVBAoMB3dvbGZTU0wxFDAS
8+
BgNVBAsMC0RldmVsb3BtZW50MRIwEAYDVQQDDAlsb2NhbGhvc3QxHzAdBgkqhkiG
9+
9w0BCQEWEGluZm9Ad29sZnNzbC5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC
10+
AAToBp+YdaHpiRuvAWYXgK/mv7M1szpyfv7EXYDQXibu3moryej10+ReWc5neXgD
11+
VfQEZ1O6CkwPo7lZU6FBhGxmo1MwUTAdBgNVHQ4EFgQUvWcAwbZL6I6LHUZYMSdT
12+
FBzD3cswHwYDVR0jBBgwFoAUvWcAwbZL6I6LHUZYMSdTFBzD3cswDwYDVR0TAQH/
13+
BAUwAwEB/zAKBggqhkjOPQQDAgNJADBGAiEAtC5RgbHrOuOOZznMc62hjjmSHFCz
14+
3GJOj6dVuZkbFxMCIQC+RA9BvaXbUirbRZzyQThhN9ohZR5cu2G9PAwy7o1cXw==
15+
-----END CERTIFICATE-----

certs/test-key.pem

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN EC PARAMETERS-----
2+
BggqhkjOPQMBBw==
3+
-----END EC PARAMETERS-----
4+
-----BEGIN EC PRIVATE KEY-----
5+
MHcCAQEEIP59qE0IungH9iZIsR9XFytv308g4XQWnWC1XUSvyF3soAoGCCqGSM49
6+
AwEHoUQDQgAE6AafmHWh6YkbrwFmF4Cv5r+zNbM6cn7+xF2A0F4m7t5qK8no9dPk
7+
XlnOZ3l4A1X0BGdTugpMD6O5WVOhQYRsZg==
8+
-----END EC PRIVATE KEY-----

configure.ac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
AC_PREREQ(2.59)
77

8-
AC_INIT([wolfKeyManager],[0.3],[http://www.wolfssl.com])
8+
AC_INIT([wolfKeyManager],[0.4],[http://www.wolfssl.com])
99
AC_CONFIG_AUX_DIR(config)
1010
AC_CONFIG_HEADERS([wolfkeymgr/config.h])
1111
AC_CONFIG_MACRO_DIR(m4)
@@ -71,7 +71,7 @@ LT_PREREQ([2.2])
7171
LT_INIT([disable-static win32-dll])
7272

7373
# Shared library versioning
74-
WOLFKM_LIBRARY_VERSION=3:0:0
74+
WOLFKM_LIBRARY_VERSION=3:0:1
7575
# | | |
7676
# +------+ | +---+
7777
# | | |

src/keymanager.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static void Usage(void)
3737
WOLFKM_DEFAULT_LOG_NAME ? WOLFKM_DEFAULT_LOG_NAME : "None");
3838
printf("-o <num> Max open files, default %d\n", WOLFKM_DEFAULT_FILES);
3939
printf("-s <num> Seconds to timeout, default %d\n", WOLFKM_DEFAULT_TIMEOUT);
40+
printf("-r <num> Key renewal timeout, default %d\n", WOLFKM_KEY_RENEW_TIMEOUT);
4041
printf("-t <num> Thread pool size, default %ld\n",
4142
sysconf(_SC_NPROCESSORS_CONF));
4243
printf("-d TLS Disable Mutual Authentication\n");
@@ -70,7 +71,8 @@ int main(int argc, char** argv)
7071
struct event_base* mainBase = NULL; /* main thread's base */
7172
FILE* pidF = 0;
7273
svcInfo* etsiSvc = NULL;
73-
word32 timeoutSec = WOLFKM_DEFAULT_TIMEOUT;
74+
int sec;
75+
word32 timeoutSec = WOLFKM_DEFAULT_TIMEOUT, renewSec = WOLFKM_KEY_RENEW_TIMEOUT;
7476
int disableMutualAuth = 0; /* on by default */
7577
const char* serverKey = WOLFKM_ETSISVC_KEY;
7678
const char* serverKeyPass = WOLFKM_ETSISVC_KEY_PASSWORD;
@@ -79,7 +81,7 @@ int main(int argc, char** argv)
7981
signalArg sigArgInt, sigArgTerm;
8082

8183
/* argument processing */
82-
while ((ch = getopt(argc, argv, "?bis:t:o:f:l:dk:w:c:A:")) != -1) {
84+
while ((ch = getopt(argc, argv, "?bis:t:o:f:l:dk:w:c:A:r:")) != -1) {
8385
switch (ch) {
8486
case '?' :
8587
Usage();
@@ -91,15 +93,13 @@ int main(int argc, char** argv)
9193
core = 1;
9294
break;
9395
case 's' :
94-
{
95-
int sec = atoi(optarg);
96+
sec = atoi(optarg);
9697
if (sec < 0) {
9798
perror("timeout positive values only accepted");
9899
exit(EX_USAGE);
99100
}
100101
timeoutSec = (word32)sec;
101102
break;
102-
}
103103
case 't' :
104104
poolSize = atoi(optarg);
105105
break;
@@ -134,6 +134,14 @@ int main(int argc, char** argv)
134134
case 'A':
135135
caCert = optarg;
136136
break;
137+
case 'r':
138+
sec = atoi(optarg);
139+
if (sec < 0) {
140+
perror("timeout positive values only accepted");
141+
exit(EX_USAGE);
142+
}
143+
renewSec = (word32)sec;
144+
break;
137145

138146
default:
139147
Usage();
@@ -185,7 +193,7 @@ int main(int argc, char** argv)
185193
}
186194

187195
/* setup signal stuff */
188-
if (wolfKeyMgr_SigIgnore(SIGPIPE) == -1) {
196+
if (wolfSigIgnore(SIGPIPE) == -1) {
189197
XLOG(WOLFKM_LOG_ERROR, "Failed to ignore SIGPIPE\n");
190198
ret = EX_OSERR; goto exit;
191199
}
@@ -201,8 +209,11 @@ int main(int argc, char** argv)
201209
wolfKeyMgr_SetMaxFiles(maxFiles);
202210

203211
/********** ETSI Service **********/
204-
etsiSvc = wolfEtsiSvc_Init(mainBase, timeoutSec);
212+
etsiSvc = wolfEtsiSvc_Init(mainBase, renewSec);
205213
if (etsiSvc) {
214+
/* set socket timeut */
215+
wolfKeyMgr_SetTimeout(etsiSvc, timeoutSec);
216+
206217
ret = wolfKeyMgr_LoadCAFile(etsiSvc, caCert, WOLFSSL_FILETYPE_PEM);
207218
if (ret != 0) {
208219
XLOG(WOLFKM_LOG_ERROR, "Error loading ETSI TLS CA cert\n");

src/mod_etsi.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@
2323

2424
#include "wolfkeymgr/mod_etsi.h"
2525

26+
#include <signal.h>
27+
2628
struct EtsiClientCtx {
2729
WOLFSSL_CTX* sslCtx;
2830
WOLFSSL* ssl;
2931
EtsiClientType type;
32+
wolfSSL_Mutex lock;
3033
};
3134

35+
3236
EtsiClientCtx* wolfEtsiClientNew(void)
3337
{
3438
EtsiClientCtx* client = (EtsiClientCtx*)malloc(sizeof(EtsiClientCtx));
3539
if (client) {
3640
memset(client, 0, sizeof(EtsiClientCtx));
41+
wc_InitMutex(&client->lock);
3742
client->sslCtx = wolfTlsClientNew();
3843
if (client->sslCtx == NULL) {
3944
XLOG(WOLFKM_LOG_ERROR, "Error creating TLS client!\n");
@@ -47,10 +52,14 @@ EtsiClientCtx* wolfEtsiClientNew(void)
4752
int wolfEtsiClientSetKey(EtsiClientCtx* client, const char* keyFile,
4853
const char* keyPassword, const char* certFile, int fileType)
4954
{
55+
int ret;
5056
if (client == NULL) {
5157
return WOLFKM_BAD_ARGS;
5258
}
53-
return wolfTlsSetKey(client->sslCtx, keyFile, keyPassword, certFile, fileType);
59+
wc_LockMutex(&client->lock);
60+
ret = wolfTlsSetKey(client->sslCtx, keyFile, keyPassword, certFile, fileType);
61+
wc_UnLockMutex(&client->lock);
62+
return ret;
5463
}
5564

5665
int wolfEtsiClientAddCA(EtsiClientCtx* client, const char* caFile)
@@ -60,7 +69,9 @@ int wolfEtsiClientAddCA(EtsiClientCtx* client, const char* caFile)
6069
return WOLFKM_BAD_ARGS;
6170
}
6271

72+
wc_LockMutex(&client->lock);
6373
ret = wolfTlsAddCA(client->sslCtx, caFile);
74+
wc_UnLockMutex(&client->lock);
6475
return ret;
6576
}
6677

@@ -73,13 +84,15 @@ int wolfEtsiClientConnect(EtsiClientCtx* client, const char* host,
7384
return WOLFKM_BAD_ARGS;
7485
}
7586

87+
wc_LockMutex(&client->lock);
7688
ret = wolfTlsConnect(client->sslCtx, &client->ssl, host, port, timeoutSec);
7789
if (ret == 0) {
7890
XLOG(WOLFKM_LOG_INFO, "Connected to ETSI service\n");
7991
}
8092
else {
8193
XLOG(WOLFKM_LOG_ERROR, "Failure connecting to ETSI service %d\n", ret);
8294
}
95+
wc_UnLockMutex(&client->lock);
8396

8497
return ret;
8598
}
@@ -125,12 +138,14 @@ int wolfEtsiClientGet(EtsiClientCtx* client,
125138
return WOLFKM_BAD_ARGS;
126139
}
127140

141+
wc_LockMutex(&client->lock);
142+
128143
/* only send request if we need to */
129144
if (type != ETSI_CLIENT_PUSH || client->type != type) {
130145
ret = EtsiClientMakeRequest(type, fingerprint, request, &requestSz);
131146
if (ret != 0) {
132147
XLOG(WOLFKM_LOG_INFO, "EtsiClientMakeRequest failed: %d\n", ret);
133-
return ret;
148+
goto exit;
134149
}
135150

136151
/* send key request */
@@ -141,7 +156,7 @@ int wolfEtsiClientGet(EtsiClientCtx* client,
141156
if (ret < 0) {
142157
XLOG(WOLFKM_LOG_INFO, "DoClientSend failed: %d (%s)\n", ret,
143158
wolfSSL_ERR_reason_error_string(ret));
144-
return ret;
159+
goto exit;
145160
}
146161
pos += ret;
147162
}
@@ -181,6 +196,9 @@ int wolfEtsiClientGet(EtsiClientCtx* client,
181196
XLOG(WOLFKM_LOG_INFO, "Got ETSI response (%d bytes)\n", *responseSz);
182197
}
183198

199+
exit:
200+
wc_UnLockMutex(&client->lock);
201+
184202
return ret;
185203
}
186204

@@ -204,15 +222,18 @@ int wolfEtsiClientClose(EtsiClientCtx* client)
204222
int ret = 0;
205223
if (client && client->ssl) {
206224
/* send shutdown */
225+
wc_LockMutex(&client->lock);
207226
ret = wolfTlsClose(client->ssl, 1);
208227
client->ssl = NULL;
228+
wc_UnLockMutex(&client->lock);
209229
}
210230
return ret;
211231
}
212232

213233
void wolfEtsiClientFree(EtsiClientCtx* client)
214234
{
215235
if (client) {
236+
wc_LockMutex(&client->lock);
216237
if (client->ssl) {
217238
wolfTlsClose(client->ssl, 0);
218239
client->ssl = NULL;
@@ -221,12 +242,17 @@ void wolfEtsiClientFree(EtsiClientCtx* client)
221242
wolfTlsFree(client->sslCtx);
222243
client->sslCtx = NULL;
223244
}
245+
wc_UnLockMutex(&client->lock);
246+
wc_FreeMutex(&client->lock);
224247
free(client);
225248
}
226249
}
227250

228251
int wolfEtsiClientInit(void)
229252
{
253+
/* Ignore SIGPIPE */
254+
wolfSigIgnore(SIGPIPE);
255+
230256
#if 0
231257
wolfSSL_Debugging_ON();
232258
#endif

src/mod_http.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,39 @@ byte* wolfHttpUriDecode(const char *s, byte *dec)
476476
}
477477
return dec;
478478
}
479+
480+
int wolfHttpUrlDecode(HttpUrl* url, char* s)
481+
{
482+
char* dec;
483+
if (url == NULL || s == NULL) {
484+
return WOLFKM_BAD_ARGS;
485+
}
486+
memset(url, 0, sizeof(*url));
487+
488+
/* find :// */
489+
dec = strstr(s, "://");
490+
if (dec == NULL) {
491+
return WOLFKM_BAD_ARGS;
492+
}
493+
494+
*dec = '\0';
495+
url->protocol = s;
496+
s = dec+3;
497+
498+
/* find next "/" */
499+
url->domain = s;
500+
dec = strstr(s, "/");
501+
if (dec) {
502+
*dec = '\0';
503+
url->path = dec+1;
504+
}
505+
506+
/* find ":" */
507+
dec = strstr(s, ":");
508+
if (dec) {
509+
/* port specified */
510+
url->port = atoi(dec+1);
511+
*dec = '\0';
512+
}
513+
return 0;
514+
}

src/mod_tls.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ int wolfTlsAddCA(WOLFSSL_CTX* ctx, const char* caFile)
142142
if (ret != WOLFSSL_SUCCESS) {
143143
XLOG(WOLFKM_LOG_ERROR, "Can't load TLS CA %s into context. Error: %s (%d)\n",
144144
caFile, wolfSSL_ERR_reason_error_string(ret), ret);
145-
wolfSSL_CTX_free(ctx);
146145
return WOLFKM_BAD_FILE;
147146
}
148147

src/sock_mgr.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -823,19 +823,6 @@ static void AcceptCB(struct evconnlistener* listener, evutil_socket_t fd,
823823

824824

825825
/* --- PUBLIC FUNCTIONS --- */
826-
/* Clear action on supplied sig event */
827-
int wolfKeyMgr_SigIgnore(int sig)
828-
{
829-
struct sigaction sa;
830-
831-
sa.sa_handler = SIG_IGN;
832-
sa.sa_flags = 0;
833-
834-
if (sigemptyset(&sa.sa_mask) == -1 || sigaction(sig, &sa, 0) == -1)
835-
return -1;
836-
837-
return 0;
838-
}
839826

840827
/* Our signal handler callback */
841828
void wolfKeyMgr_SignalCb(evutil_socket_t fd, short event, void* arg)

src/svc_etsi.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct etsiSvcCtx {
3030
ecc_key key; /* last generated key */
3131
WC_RNG rng;
3232
double last; /* time last generated */
33-
word32 timeoutSec;
33+
word32 renewSec;
3434
word32 index;
3535
pthread_mutex_t lock; /* queue lock */
3636
pthread_t thread; /* key gen worker */
@@ -171,8 +171,8 @@ static void* KeyPushWorker(void* arg)
171171
/* push to any connected clients */
172172
wolfKeyMgr_NotifyAllClients(svc);
173173

174-
/* wait timeoutSec */
175-
sleep(svcCtx->timeoutSec);
174+
/* wait seconds */
175+
sleep(svcCtx->renewSec);
176176
} while (1);
177177

178178
return NULL;
@@ -342,7 +342,7 @@ void wolfEtsiSvc_WorkerFree(svcInfo* svc, void* svcThreadCtx)
342342
#endif /* WOLFKM_ETSI_SERVICE */
343343

344344

345-
svcInfo* wolfEtsiSvc_Init(struct event_base* mainBase, int timeoutSec)
345+
svcInfo* wolfEtsiSvc_Init(struct event_base* mainBase, int renewSec)
346346
{
347347
#ifdef WOLFKM_ETSI_SERVICE
348348
int ret;
@@ -358,9 +358,7 @@ svcInfo* wolfEtsiSvc_Init(struct event_base* mainBase, int timeoutSec)
358358

359359
pthread_mutex_init(&svcCtx->lock, NULL);
360360

361-
/* use the timeout to trigger sending new set of keys */
362-
wolfKeyMgr_SetTimeout(svc, timeoutSec);
363-
svcCtx->timeoutSec = timeoutSec;
361+
svcCtx->renewSec = renewSec;
364362

365363
/* start key generation thread */
366364
if (pthread_create(&svcCtx->thread, NULL, KeyPushWorker, svc) != 0) {
@@ -383,8 +381,8 @@ svcInfo* wolfEtsiSvc_Init(struct event_base* mainBase, int timeoutSec)
383381
return svc;
384382
#else
385383
(void)mainBase;
386-
(void)timeoutSec;
387-
(void)disableMutalAuth;
384+
(void)renewSec;
385+
388386
return NULL;
389387
#endif
390388
}

0 commit comments

Comments
 (0)