Skip to content

Commit 40302ac

Browse files
add TrustedSystemCAKeys sshd option for system CA load
1 parent 7bbc20f commit 40302ac

6 files changed

Lines changed: 107 additions & 2 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
@@ -433,6 +433,39 @@ static int SetupCTX(WOLFSSHD_CONFIG* conf, WOLFSSH_CTX** ctx,
433433
#endif /* WOLFSSH_OSSH_CERTS || WOLFSSH_CERTS */
434434

435435
#ifdef WOLFSSH_CERTS
436+
/* check if loading in system CA certs */
437+
if (ret == WS_SUCCESS && wolfSSHD_ConfigGetSystemCA(conf)) {
438+
WOLFSSL_CTX* sslCtx;
439+
440+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Using system CAs");
441+
sslCtx = wolfSSL_CTX_new(wolfSSLv23_method());
442+
if (sslCtx == NULL) {
443+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Unable to create temporary CTX");
444+
ret = WS_FATAL_ERROR;
445+
}
446+
447+
if (ret == WS_SUCCESS) {
448+
if (wolfSSL_CTX_load_system_CA_certs(sslCtx) != WOLFSSL_SUCCESS) {
449+
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Issue loading system CAs");
450+
ret = WS_FATAL_ERROR;
451+
}
452+
}
453+
454+
if (ret == WS_SUCCESS) {
455+
if (wolfSSH_SetCertManager(*ctx,
456+
wolfSSL_CTX_GetCertManager(sslCtx)) != WS_SUCCESS) {
457+
wolfSSH_Log(WS_LOG_INFO,
458+
"[SSHD] Issue copying over system CAs");
459+
ret = WS_FATAL_ERROR;
460+
}
461+
}
462+
463+
if (sslCtx != NULL) {
464+
wolfSSL_CTX_free(sslCtx);
465+
}
466+
}
467+
468+
/* load in CA certs from file set */
436469
if (ret == WS_SUCCESS) {
437470
char* caCert = wolfSSHD_ConfigGetUserCAKeysFile(conf);
438471
if (caCert != NULL) {

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
@@ -1109,6 +1109,7 @@ static INLINE void build_addr_ipv6(struct sockaddr_in6* addr, const char* peer,
11091109

11101110
#define BAD 0xFF
11111111

1112+
#ifndef WOLFSSL_BASE16
11121113
static const byte hexDecode[] =
11131114
{
11141115
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
@@ -1178,7 +1179,9 @@ static int Base16_Decode(const byte* in, word32 inLen,
11781179
*outLen = outIdx;
11791180
return 0;
11801181
}
1181-
1182+
#else
1183+
#include <wolfssl/wolfcrypt/coding.h>
1184+
#endif /* !WOLFSSL_BASE16 */
11821185

11831186
static void FreeBins(byte* b1, byte* b2, byte* b3, byte* b4)
11841187
{

0 commit comments

Comments
 (0)