Skip to content

Commit a57b577

Browse files
JacobBarthelmehkareem-wolfssl
authored andcommitted
add TrustedSystemCAKeys sshd option for system CA load
1 parent 4dabe1c commit a57b577

File tree

7 files changed

+109
-6
lines changed

7 files changed

+109
-6
lines changed

apps/wolfsshd/configuration.c

Lines changed: 46 additions & 2 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);
@@ -348,10 +349,11 @@ enum {
348349
OPT_FORCE_CMD = 19,
349350
OPT_HOST_CERT = 20,
350351
OPT_TRUSTED_USER_CA_KEYS = 21,
351-
OPT_PIDFILE = 22,
352+
OPT_TRUSTED_SYSTEM_CA_KEYS = 22,
353+
OPT_PIDFILE = 23,
352354
};
353355
enum {
354-
NUM_OPTIONS = 23
356+
NUM_OPTIONS = 24
355357
};
356358

357359
static const CONFIG_OPTION options[NUM_OPTIONS] = {
@@ -377,6 +379,7 @@ static const CONFIG_OPTION options[NUM_OPTIONS] = {
377379
{OPT_FORCE_CMD, "ForceCommand"},
378380
{OPT_HOST_CERT, "HostCertificate"},
379381
{OPT_TRUSTED_USER_CA_KEYS, "TrustedUserCAKeys"},
382+
{OPT_TRUSTED_SYSTEM_CA_KEYS, "TrustedSystemCAKeys"},
380383
{OPT_PIDFILE, "PidFile"},
381384
};
382385

@@ -1019,6 +1022,9 @@ static int HandleConfigOption(WOLFSSHD_CONFIG** conf, int opt,
10191022
/* TODO: Add logic to check if file exists? */
10201023
ret = wolfSSHD_ConfigSetUserCAKeysFile(*conf, value);
10211024
break;
1025+
case OPT_TRUSTED_SYSTEM_CA_KEYS:
1026+
ret = wolfSSHD_ConfigSetSystemCA(*conf, value);
1027+
break;
10221028
case OPT_PIDFILE:
10231029
ret = SetFileString(&(*conf)->pidFile, value, (*conf)->heap);
10241030
break;
@@ -1304,6 +1310,44 @@ char* wolfSSHD_ConfigGetHostCertFile(const WOLFSSHD_CONFIG* conf)
13041310
return ret;
13051311
}
13061312

1313+
1314+
/* getter function for if using system CAs
1315+
* return 1 if true and 0 if false */
1316+
int wolfSSHD_ConfigGetSystemCA(const WOLFSSHD_CONFIG* conf)
1317+
{
1318+
if (conf != NULL) {
1319+
return conf->useSystemCA;
1320+
}
1321+
return 0;
1322+
}
1323+
1324+
1325+
/* setter function for if using system CAs
1326+
* 'yes' if true and 'no' if false
1327+
* returns WS_SUCCESS on success */
1328+
int wolfSSHD_ConfigSetSystemCA(WOLFSSHD_CONFIG* conf, const char* value)
1329+
{
1330+
int ret = WS_SUCCESS;
1331+
1332+
if (conf != NULL) {
1333+
if (WSTRCMP(value, "yes") == 0) {
1334+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] System CAs enabled");
1335+
conf->useSystemCA = 1;
1336+
}
1337+
else if (WSTRCMP(value, "no") == 0) {
1338+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] System CAs disabled");
1339+
conf->useSystemCA = 0;
1340+
}
1341+
else {
1342+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] System CAs unexpected flag");
1343+
ret = WS_FATAL_ERROR;
1344+
}
1345+
}
1346+
1347+
return ret;
1348+
}
1349+
1350+
13071351
char* wolfSSHD_ConfigGetUserCAKeysFile(const WOLFSSHD_CONFIG* conf)
13081352
{
13091353
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
@@ -406,6 +406,39 @@ static int SetupCTX(WOLFSSHD_CONFIG* conf, WOLFSSH_CTX** ctx)
406406
#endif /* WOLFSSH_OSSH_CERTS || WOLFSSH_CERTS */
407407

408408
#ifdef WOLFSSH_CERTS
409+
/* check if loading in system CA certs */
410+
if (ret == WS_SUCCESS && wolfSSHD_ConfigGetSystemCA(conf)) {
411+
WOLFSSL_CTX* sslCtx;
412+
413+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Using system CAs");
414+
sslCtx = wolfSSL_CTX_new(wolfSSLv23_method());
415+
if (sslCtx == NULL) {
416+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Unable to create temporary CTX");
417+
ret = WS_FATAL_ERROR;
418+
}
419+
420+
if (ret == WS_SUCCESS) {
421+
if (wolfSSL_CTX_load_system_CA_certs(sslCtx) != WOLFSSL_SUCCESS) {
422+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Issue loading system CAs");
423+
ret = WS_FATAL_ERROR;
424+
}
425+
}
426+
427+
if (ret == WS_SUCCESS) {
428+
if (wolfSSH_SetCertManager(*ctx,
429+
wolfSSL_CTX_GetCertManager(sslCtx)) != WS_SUCCESS) {
430+
wolfSSH_Log(WS_LOG_INFO,
431+
"[SSHD] Issue copying over system CAs");
432+
ret = WS_FATAL_ERROR;
433+
}
434+
}
435+
436+
if (sslCtx != NULL) {
437+
wolfSSL_CTX_free(sslCtx);
438+
}
439+
}
440+
441+
/* load in CA certs from file set */
409442
if (ret == WS_SUCCESS) {
410443
char* caCert = wolfSSHD_ConfigGetUserCAKeysFile(conf);
411444
if (caCert != NULL) {

examples/echoserver/echoserver.c

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

28582858

2859-
void wolfSSL_Debugging_ON(void);
2860-
28612859
int wolfSSH_Echoserver(int argc, char** argv)
28622860
{
28632861
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,
@@ -1183,7 +1184,9 @@ static int Base16_Decode(const byte* in, word32 inLen,
11831184
*outLen = outIdx;
11841185
return 0;
11851186
}
1186-
1187+
#else
1188+
#include <wolfssl/wolfcrypt/coding.h>
1189+
#endif /* !WOLFSSL_BASE16 */
11871190

11881191
static void FreeBins(byte* b1, byte* b2, byte* b3, byte* b4)
11891192
{

0 commit comments

Comments
 (0)