Skip to content

Commit bdafc49

Browse files
committed
Improvement to examples. Add argument support to HTTP server. Improve error when key manager connection fails. Fix etsi_test help messages for fingerprint and context string.
1 parent fe6faab commit bdafc49

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

examples/etsi_test/etsi_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ static void Usage(void)
219219
printf("-A <pem> TLS CA Certificate, default %s\n", ETSI_TEST_CLIENT_CA);
220220
printf("-K <keyt> Key Type: SECP256R1, FFDHE_2048, X25519 or X448 (default %s)\n",
221221
wolfEtsiKeyGetTypeStr(ETSI_TEST_KEY_TYPE));
222-
printf("-F <fprint> Fingerprint used for multiple servers (first 80-bit of pkey hash as hex string)\n");
223-
printf("-C <name> Find key using public key name (hex string)\n");
222+
printf("-F <fprint> Fingerprint to find (first 80-bit of pkey hash as hex string)\n");
223+
printf("-C <name> Unique key name (used for multiple servers)\n");
224224
}
225225

226226
int etsi_test(int argc, char** argv)

examples/https/server.c

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626

2727
#include <signal.h> /* signal */
2828

29+
#ifndef EX_USAGE
30+
#define EX_USAGE 2
31+
#endif
32+
33+
#ifndef EXIT_FAILURE
34+
#define EXIT_FAILURE 1
35+
#endif
36+
2937
static volatile int mStop = 0;
3038
static WKM_SOCKET_T listenFd = WKM_SOCKET_INVALID;
3139

@@ -47,6 +55,17 @@ static int etsi_key_cb(EtsiKey* key, void* cbCtx)
4755
return ret;
4856
}
4957

58+
/* usage help */
59+
static void Usage(void)
60+
{
61+
printf("%s %s\n", "https/server", PACKAGE_VERSION);
62+
printf("-? Help, print this usage\n");
63+
printf("-d Disable ETSI Key Manager loading\n");
64+
printf("-p <num> Port to listen, default %d\n", HTTPS_TEST_PORT);
65+
printf("-l <num> Log Level (1=Error to 4=Debug), default %d\n", WOLFKM_DEFAULT_LOG_LEVEL);
66+
printf("-h <keymgr> Key Manager URL (default %s)\n", "https://" ETSI_TEST_HOST ":" ETSI_TEST_PORT_STR);
67+
}
68+
5069
int https_server_test(int argc, char** argv)
5170
{
5271
int ret;
@@ -58,21 +77,48 @@ int https_server_test(int argc, char** argv)
5877
HttpHeader headers[2];
5978
const char* body = HTTPS_TEST_RESPONSE;
6079
SOCKADDR_IN_T clientAddr;
80+
int port = HTTPS_TEST_PORT;
81+
enum log_level_t logLevel = WOLFKM_DEFAULT_LOG_LEVEL;
6182
const char* etsiServer = "https://" ETSI_TEST_HOST ":" ETSI_TEST_PORT_STR;
83+
int ch, useKeyMgr = 1;
6284

6385
signal(SIGINT, sig_handler);
6486

65-
/* TODO: Support arguments */
66-
(void)argc;
67-
(void)argv;
87+
/* argument processing */
88+
while ((ch = getopt(argc, argv, "?p:l:dh:")) != -1) {
89+
switch (ch) {
90+
case '?' :
91+
Usage();
92+
exit(EX_USAGE);
93+
case 'p' :
94+
port = atoi(optarg);
95+
break;
96+
case 'l' :
97+
logLevel = atoi(optarg);
98+
if (logLevel < WOLFKM_LOG_ERROR || logLevel > WOLFKM_LOG_DEBUG) {
99+
perror("loglevel [1:4] only");
100+
exit(EX_USAGE);
101+
}
102+
break;
103+
case 'd':
104+
useKeyMgr = 0;
105+
break;
106+
case 'h':
107+
etsiServer = optarg;
108+
break;
109+
default:
110+
Usage();
111+
exit(EX_USAGE);
112+
}
113+
}
68114

69-
printf("HTTPS Server: Port %d\n", HTTPS_TEST_PORT);
115+
printf("HTTPS Server: Port %d\n", port);
70116

71117
wolfSSL_Init();
72118

73119
/* log setup */
74120
/* wolfSSL_Debugging_ON(); */
75-
wolfKeyMgr_SetLogFile(NULL, 0, WOLFKM_LOG_DEBUG);
121+
wolfKeyMgr_SetLogFile(NULL, 0, logLevel);
76122

77123
ctx = wolfTlsServerNew();
78124
if (ctx == NULL) { ret = WOLFKM_BAD_MEMORY; goto exit; }
@@ -85,14 +131,18 @@ int https_server_test(int argc, char** argv)
85131
if (ret != 0) goto exit;
86132

87133
/* setup listener */
88-
ret = wolfSockListen(&listenFd, HTTPS_TEST_PORT);
134+
ret = wolfSockListen(&listenFd, port);
89135
if (ret != 0) goto exit;
90136

91137
do {
92-
ret = etsi_client_get_all(etsiServer, etsi_key_cb, ctx);
93-
if (ret != 0) {
94-
mStop = 1;
95-
goto end_sess;
138+
if (useKeyMgr) {
139+
ret = etsi_client_get_all(etsiServer, etsi_key_cb, ctx);
140+
if (ret != 0) {
141+
printf("\nFailure connecting to key manager\n");
142+
printf("Make sure ./src/wolfkeymgr is running\n");
143+
mStop = 1;
144+
goto end_sess;
145+
}
96146
}
97147

98148
ret = wolfTlsAccept(ctx, listenFd, &ssl, &clientAddr,

src/mod_etsi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int wolfEtsiClientConnect(EtsiClientCtx* client, const char* host,
8787
}
8888
else {
8989
XLOG(WOLFKM_LOG_ERROR, "Failure connecting to ETSI service %d\n", ret);
90+
ret = WOLFKM_BAD_HOST;
9091
}
9192
wc_UnLockMutex(&client->lock);
9293

0 commit comments

Comments
 (0)