Skip to content

Commit 3cf234a

Browse files
JacobBarthelmehkareem-wolfssl
authored andcommitted
add TrustedSystemCAKeys sshd option for system CA load
1 parent 735cd89 commit 3cf234a

7 files changed

Lines changed: 107 additions & 4 deletions

File tree

apps/wolfsshd/configuration.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct WOLFSSHD_CONFIG {
8989
byte permitRootLogin:1;
9090
byte permitEmptyPasswords:1;
9191
byte authKeysFileSet:1; /* if not set then no explicit authorized keys */
92+
byte useSystemCA:1;
9293
};
9394

9495
int CountWhitespace(const char* in, int inSz, byte inv);
@@ -350,6 +351,7 @@ enum {
350351
OPT_TRUSTED_USER_CA_KEYS = 21,
351352
OPT_PIDFILE = 22,
352353
OPT_BANNER = 23,
354+
OPT_TRUSTED_SYSTEM_CA_KEYS = 24,
353355
};
354356
enum {
355357
NUM_OPTIONS = 24
@@ -378,6 +380,7 @@ static const CONFIG_OPTION options[NUM_OPTIONS] = {
378380
{OPT_FORCE_CMD, "ForceCommand"},
379381
{OPT_HOST_CERT, "HostCertificate"},
380382
{OPT_TRUSTED_USER_CA_KEYS, "TrustedUserCAKeys"},
383+
{OPT_TRUSTED_SYSTEM_CA_KEYS, "TrustedSystemCAKeys"},
381384
{OPT_PIDFILE, "PidFile"},
382385
{OPT_BANNER, "Banner"},
383386
};
@@ -1021,6 +1024,9 @@ static int HandleConfigOption(WOLFSSHD_CONFIG** conf, int opt,
10211024
/* TODO: Add logic to check if file exists? */
10221025
ret = wolfSSHD_ConfigSetUserCAKeysFile(*conf, value);
10231026
break;
1027+
case OPT_TRUSTED_SYSTEM_CA_KEYS:
1028+
ret = wolfSSHD_ConfigSetSystemCA(*conf, value);
1029+
break;
10241030
case OPT_PIDFILE:
10251031
ret = SetFileString(&(*conf)->pidFile, value, (*conf)->heap);
10261032
break;
@@ -1309,6 +1315,44 @@ char* wolfSSHD_ConfigGetHostCertFile(const WOLFSSHD_CONFIG* conf)
13091315
return ret;
13101316
}
13111317

1318+
1319+
/* getter function for if using system CAs
1320+
* return 1 if true and 0 if false */
1321+
int wolfSSHD_ConfigGetSystemCA(const WOLFSSHD_CONFIG* conf)
1322+
{
1323+
if (conf != NULL) {
1324+
return conf->useSystemCA;
1325+
}
1326+
return 0;
1327+
}
1328+
1329+
1330+
/* setter function for if using system CAs
1331+
* 'yes' if true and 'no' if false
1332+
* returns WS_SUCCESS on success */
1333+
int wolfSSHD_ConfigSetSystemCA(WOLFSSHD_CONFIG* conf, const char* value)
1334+
{
1335+
int ret = WS_SUCCESS;
1336+
1337+
if (conf != NULL) {
1338+
if (WSTRCMP(value, "yes") == 0) {
1339+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] System CAs enabled");
1340+
conf->useSystemCA = 1;
1341+
}
1342+
else if (WSTRCMP(value, "no") == 0) {
1343+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] System CAs disabled");
1344+
conf->useSystemCA = 0;
1345+
}
1346+
else {
1347+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] System CAs unexpected flag");
1348+
ret = WS_FATAL_ERROR;
1349+
}
1350+
}
1351+
1352+
return ret;
1353+
}
1354+
1355+
13121356
char* wolfSSHD_ConfigGetUserCAKeysFile(const WOLFSSHD_CONFIG* conf)
13131357
{
13141358
char* ret = NULL;

apps/wolfsshd/configuration.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ char* wolfSSHD_ConfigGetHostCertFile(const WOLFSSHD_CONFIG* conf);
4242
char* wolfSSHD_ConfigGetUserCAKeysFile(const WOLFSSHD_CONFIG* conf);
4343
int wolfSSHD_ConfigSetHostKeyFile(WOLFSSHD_CONFIG* conf, const char* file);
4444
int wolfSSHD_ConfigSetHostCertFile(WOLFSSHD_CONFIG* conf, const char* file);
45+
int wolfSSHD_ConfigSetSystemCA(WOLFSSHD_CONFIG* conf, const char* value);
46+
int wolfSSHD_ConfigGetSystemCA(const WOLFSSHD_CONFIG* conf);
4547
int wolfSSHD_ConfigSetUserCAKeysFile(WOLFSSHD_CONFIG* conf, const char* file);
4648
word16 wolfSSHD_ConfigGetPort(const WOLFSSHD_CONFIG* conf);
4749
char* wolfSSHD_ConfigGetAuthKeysFile(const WOLFSSHD_CONFIG* conf);

apps/wolfsshd/wolfsshd.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,39 @@ static int SetupCTX(WOLFSSHD_CONFIG* conf, WOLFSSH_CTX** ctx,
423423
#endif /* WOLFSSH_OSSH_CERTS || WOLFSSH_CERTS */
424424

425425
#ifdef WOLFSSH_CERTS
426+
/* check if loading in system CA certs */
427+
if (ret == WS_SUCCESS && wolfSSHD_ConfigGetSystemCA(conf)) {
428+
WOLFSSL_CTX* sslCtx;
429+
430+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Using system CAs");
431+
sslCtx = wolfSSL_CTX_new(wolfSSLv23_method());
432+
if (sslCtx == NULL) {
433+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Unable to create temporary CTX");
434+
ret = WS_FATAL_ERROR;
435+
}
436+
437+
if (ret == WS_SUCCESS) {
438+
if (wolfSSL_CTX_load_system_CA_certs(sslCtx) != WOLFSSL_SUCCESS) {
439+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Issue loading system CAs");
440+
ret = WS_FATAL_ERROR;
441+
}
442+
}
443+
444+
if (ret == WS_SUCCESS) {
445+
if (wolfSSH_SetCertManager(*ctx,
446+
wolfSSL_CTX_GetCertManager(sslCtx)) != WS_SUCCESS) {
447+
wolfSSH_Log(WS_LOG_INFO,
448+
"[SSHD] Issue copying over system CAs");
449+
ret = WS_FATAL_ERROR;
450+
}
451+
}
452+
453+
if (sslCtx != NULL) {
454+
wolfSSL_CTX_free(sslCtx);
455+
}
456+
}
457+
458+
/* load in CA certs from file set */
426459
if (ret == WS_SUCCESS) {
427460
char* caCert = wolfSSHD_ConfigGetUserCAKeysFile(conf);
428461
if (caCert != NULL) {

examples/echoserver/echoserver.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,8 +2855,6 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
28552855
#endif /* NO_WOLFSSH_SERVER */
28562856

28572857

2858-
void wolfSSL_Debugging_ON(void);
2859-
28602858
int wolfSSH_Echoserver(int argc, char** argv)
28612859
{
28622860
func_args args;

src/certman.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#endif
3737

3838

39-
#include <wolfssl/ssl.h>
4039
#include <wolfssl/ocsp.h>
4140
#include <wolfssl/wolfcrypt/error-crypt.h>
4241
#include <wolfssl/error-ssl.h>
@@ -84,6 +83,26 @@ struct WOLFSSH_CERTMAN {
8483
};
8584

8685

86+
/* used to import an external cert manager, frees and replaces existing manager
87+
* returns WS_SUCCESS on success
88+
*/
89+
int wolfSSH_SetCertManager(WOLFSSH_CTX* ctx, WOLFSSL_CERT_MANAGER* cm)
90+
{
91+
if (ctx == NULL || cm == NULL) {
92+
return WS_BAD_ARGUMENT;
93+
}
94+
95+
/* free up existing cm if present */
96+
if (ctx->certMan != NULL && ctx->certMan->cm != NULL) {
97+
wolfSSL_CertManagerFree(ctx->certMan->cm);
98+
}
99+
wolfSSL_CertManager_up_ref(cm);
100+
ctx->certMan->cm = cm;
101+
102+
return WS_SUCCESS;
103+
}
104+
105+
87106
static WOLFSSH_CERTMAN* _CertMan_init(WOLFSSH_CERTMAN* cm, void* heap)
88107
{
89108
WOLFSSH_CERTMAN* ret = NULL;

wolfssh/certman.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include <wolfssh/settings.h>
3232
#include <wolfssh/port.h>
33+
#include <wolfssl/ssl.h> /* included for WOLFSSL_CERT_MANAGER struct */
3334

3435
#ifdef __cplusplus
3536
extern "C" {
@@ -40,6 +41,9 @@ struct WOLFSSH_CERTMAN;
4041
typedef struct WOLFSSH_CERTMAN WOLFSSH_CERTMAN;
4142

4243

44+
WOLFSSH_API
45+
int wolfSSH_SetCertManager(WOLFSSH_CTX* ctx, WOLFSSL_CERT_MANAGER* cm);
46+
4347
WOLFSSH_API
4448
WOLFSSH_CERTMAN* wolfSSH_CERTMAN_new(void* heap);
4549

wolfssh/test.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@ static INLINE void build_addr_ipv6(struct sockaddr_in6* addr, const char* peer,
11171117

11181118
#define BAD 0xFF
11191119

1120+
#ifndef WOLFSSL_BASE16
11201121
static const byte hexDecode[] =
11211122
{
11221123
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
@@ -1186,7 +1187,9 @@ static int Base16_Decode(const byte* in, word32 inLen,
11861187
*outLen = outIdx;
11871188
return 0;
11881189
}
1189-
1190+
#else
1191+
#include <wolfssl/wolfcrypt/coding.h>
1192+
#endif /* !WOLFSSL_BASE16 */
11901193

11911194
static void FreeBins(byte* b1, byte* b2, byte* b3, byte* b4)
11921195
{

0 commit comments

Comments
 (0)