From b68f98ae708e8f0f5e22e83a6b86c24a55529f48 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 16 Feb 2021 12:59:52 +0100 Subject: [PATCH 01/10] Removed function definitions when DEBUG is disabled --- src/log.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/log.c b/src/log.c index 5abaac0..2e57201 100644 --- a/src/log.c +++ b/src/log.c @@ -99,6 +99,7 @@ int wolfSSH_LogEnabled(void) } +#ifdef DEBUG_WOLFSSH #ifndef WOLFSSH_NO_DEFAULT_LOGGING_CB /* log level string */ static const char* GetLogStr(enum wolfSSH_LogLevel level) @@ -133,7 +134,6 @@ static const char* GetLogStr(enum wolfSSH_LogLevel level) } } - void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr) { char timeStr[24]; @@ -176,3 +176,14 @@ void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...) if (logFunction) logFunction(level, msgStr); } + +#else +void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr) +{ +} + +void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...) +{ +} + +#endif /* DEBUG_WOLFSSH */ From 5a4f31fb608293e08cb21924f3e6ed663f47f695 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 16 Feb 2021 13:01:29 +0100 Subject: [PATCH 02/10] When WOLFSSH_USER_IO is defined, don't declare default syscalls --- wolfssh/port.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wolfssh/port.h b/wolfssh/port.h index d7ca96c..7ee035e 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -1010,6 +1010,8 @@ extern "C" { #define WDIR HANDLE #endif /* NO_WOLFSSH_DIR */ +#elif defined(WOLFSSH_USER_IO) + /* User-defined I/O support */ #else #include /* used for rmdir */ #include /* used for mkdir, stat, and lstat */ From 4e76f3dd72da9a492bc7ef748e68baabe96279ca Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 16 Feb 2021 13:02:45 +0100 Subject: [PATCH 03/10] Set initial value to NULL for two variables --- src/ssh.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ssh.c b/src/ssh.c index e857266..f2a59dc 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -1379,8 +1379,8 @@ int wolfSSH_ReadKey_buffer(const byte* in, word32 inSz, int format, if (format == WOLFSSH_FORMAT_SSH) { char* c; char* last; - char* type; - char* key; + char* type = NULL; + char* key = NULL; /* SSH format is: From d7cd61d926bff2802310b94652693a7146b56e7f Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 16 Feb 2021 13:03:16 +0100 Subject: [PATCH 04/10] wolfSCP: allow overriding GETHDR, disable functions that call snprinf according to compile options --- src/wolfscp.c | 52 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/wolfscp.c b/src/wolfscp.c index d022230..5c2f3ab 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -235,6 +235,8 @@ static int ScpSourceInit(WOLFSSH* ssh) return WS_SUCCESS; } + +#ifndef NO_SCP_TIMESTAMP /* Sends timestamp information (access, modification) to peer. * * T 0 0 @@ -271,6 +273,10 @@ static int SendScpTimestamp(WOLFSSH* ssh) return ret; } +#endif + + + /* Sends file header (mode, file name) to peer. * @@ -282,28 +288,33 @@ static int SendScpFileHeader(WOLFSSH* ssh) { int ret = WS_SUCCESS, bufSz; char buf[DEFAULT_SCP_MSG_SZ]; + char *filehdr; if (ssh == NULL) return WS_BAD_ARGUMENT; +#ifndef WSCPFILEHDR WMEMSET(buf, 0, sizeof(buf)); - WSNPRINTF(buf, sizeof(buf), "C%04o %u %s\n", ssh->scpFileMode, ssh->scpFileSz, ssh->scpFileName); - - bufSz = (int)WSTRLEN(buf); - - ret = wolfSSH_stream_send(ssh, (byte*)buf, bufSz); + filehdr = buf; +#else + filehdr = WSCPFILEHDR(ssh); + if (!filehdr) + return WS_BAD_ARGUMENT; +#endif + bufSz = (int)WSTRLEN(filehdr); + ret = wolfSSH_stream_send(ssh, (byte*)filehdr, bufSz); if (ret != bufSz) { ret = WS_FATAL_ERROR; } else { - WLOG(WS_LOG_DEBUG, "scp: sent file header: %s", buf); + WLOG(WS_LOG_DEBUG, "scp: sent file header: %s", filehdr); ret = WS_SUCCESS; } - return ret; } +#ifndef NO_SCP_ENTER_DIRECTORY /* Sends directory message to peer, length is ignored but must * be present in message format (set to 0). * @@ -336,6 +347,7 @@ static int SendScpEnterDirectory(WOLFSSH* ssh) return ret; } +#endif /* Sends end directory message to peer. * @@ -507,6 +519,7 @@ int DoScpSource(WOLFSSH* ssh) continue; +#ifndef NO_SCP_TIMESTAMP case SCP_SEND_TIMESTAMP: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_TIMESTAMP"); @@ -518,7 +531,9 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_SEND_FILE_HEADER; continue; +#endif +#ifndef NO_SCP_ENTER_DIRECTORY case SCP_SEND_ENTER_DIRECTORY: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_ENTER_DIRECTORY"); @@ -530,6 +545,7 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_TRANSFER; continue; +#endif case SCP_SEND_EXIT_DIRECTORY: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_EXIT_DIRECTORY"); @@ -554,7 +570,6 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_DONE; continue; - case SCP_SEND_FILE_HEADER: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_FILE_HEADER"); @@ -879,7 +894,7 @@ static int GetScpFileSize(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpFileSz = atoi((char*)(buf + idx)); + ssh->scpFileSz = strtol((char *)(buf + idx), buf + spaceIdx, 10); /* restore space, increment idx to space */ buf[spaceIdx] = ' '; @@ -985,7 +1000,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpMTime = atoi((char*)(buf + idx)); + ssh->scpMTime = strtol((char*)(buf + idx), buf + spaceIdx, 10); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; @@ -1017,7 +1032,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpATime = atoi((char*)(buf + idx)); + ssh->scpATime = strtol((char*)(buf + idx), buf + spaceIdx, 10); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; @@ -1035,7 +1050,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, return ret; } - +#ifndef NO_FILESYSTEM /* checks for if directory is being renamed in command * * returns WS_SUCCESS on success @@ -1043,7 +1058,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) { /* case of file, not directory */ - char buf[cmdSz + 4]; + char buf[DEFAULT_SCP_MSG_SZ]; int sz = (int)WSTRLEN(ssh->scpBasePath); int idx; @@ -1054,6 +1069,7 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) WSTRNCPY(buf, ssh->scpBasePath, cmdSz); buf[sz] = '\0'; WSTRNCAT(buf, "/..", sizeof("/..")); + idx = wolfSSH_CleanPath(ssh, buf); if (idx < 0) { return WS_FATAL_ERROR; @@ -1110,16 +1126,15 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) return WS_SUCCESS; } +#endif /* helps with checking if the base path is a directory or file * returns WS_SUCCESS on success */ static int ParseBasePathHelper(WOLFSSH* ssh, int cmdSz) { - int ret; - - ret = ScpCheckForRename(ssh, cmdSz); - + int ret = 0; #ifndef NO_FILESYSTEM + ret = ScpCheckForRename(ssh, cmdSz); { ScpSendCtx ctx; @@ -1569,7 +1584,7 @@ int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd) return ret; } - +#ifndef NO_WOLFSSH_CLIENT static int wolfSSH_SCP_cmd(WOLFSSH* ssh, const char* localName, const char* remoteName, byte dir) { @@ -1635,6 +1650,7 @@ int wolfSSH_SCP_from(WOLFSSH* ssh, const char* src, const char* dst) /* src is passed to the server in the scp -f command */ /* dst is used locally to fopen and write for copy from */ } +#endif /* ! NO_WOLFSSH_CLIENT */ #if !defined(WOLFSSH_SCP_USER_CALLBACKS) From 882e662bbf2d524dcc960faf749cf369fa76f4e4 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 16 Feb 2021 13:15:52 +0100 Subject: [PATCH 05/10] Exclude wolfSCP_connect from server-only build --- src/wolfscp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wolfscp.c b/src/wolfscp.c index 5c2f3ab..4f8b04e 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -1556,6 +1556,7 @@ void* wolfSSH_GetScpSendCtx(WOLFSSH* ssh) } +#ifndef NO_WOLFSSH_CLIENT int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd) { int ret = WS_SUCCESS; @@ -1584,7 +1585,6 @@ int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd) return ret; } -#ifndef NO_WOLFSSH_CLIENT static int wolfSSH_SCP_cmd(WOLFSSH* ssh, const char* localName, const char* remoteName, byte dir) { From 52a1e13470c7619fa3bc86af1f0b59836d609fc9 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 16 Feb 2021 13:20:19 +0100 Subject: [PATCH 06/10] Fixed compiler warnings, pre-commit tests --- src/log.c | 6 +++++- src/wolfscp.c | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/log.c b/src/log.c index 2e57201..2fbef7c 100644 --- a/src/log.c +++ b/src/log.c @@ -57,8 +57,8 @@ #endif /* WOLFSSH_NO_DEFAULT_LOGGING_CB */ -static enum wolfSSH_LogLevel logLevel = WS_LOG_DEFAULT; #ifdef DEBUG_WOLFSSH + static enum wolfSSH_LogLevel logLevel = WS_LOG_DEFAULT; static int logEnable = 0; #endif @@ -180,10 +180,14 @@ void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...) #else void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr) { + (void)level; + (void)msgStr; } void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...) { + (void)level; + (void)fmt; } #endif /* DEBUG_WOLFSSH */ diff --git a/src/wolfscp.c b/src/wolfscp.c index 4f8b04e..88171e8 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -894,7 +894,7 @@ static int GetScpFileSize(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpFileSz = strtol((char *)(buf + idx), buf + spaceIdx, 10); + ssh->scpFileSz = strtol((char *)(buf + idx), NULL, 10); /* restore space, increment idx to space */ buf[spaceIdx] = ' '; @@ -1000,7 +1000,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpMTime = strtol((char*)(buf + idx), buf + spaceIdx, 10); + ssh->scpMTime = strtol((char*)(buf + idx), NULL, 10); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; @@ -1032,7 +1032,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpATime = strtol((char*)(buf + idx), buf + spaceIdx, 10); + ssh->scpATime = strtol((char*)(buf + idx), NULL, 10); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; From b95d43fd1ce293cec5890d965b636e5f753e9fb0 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 17 Feb 2021 07:19:46 +0100 Subject: [PATCH 07/10] Reverted strtol to atoi --- src/wolfscp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wolfscp.c b/src/wolfscp.c index 88171e8..2785395 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -894,7 +894,7 @@ static int GetScpFileSize(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpFileSz = strtol((char *)(buf + idx), NULL, 10); + ssh->scpFileSz = atoi((char *)(buf + idx)); /* restore space, increment idx to space */ buf[spaceIdx] = ' '; @@ -1000,7 +1000,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpMTime = strtol((char*)(buf + idx), NULL, 10); + ssh->scpMTime = atoi((char*)(buf + idx)); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; @@ -1032,7 +1032,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpATime = strtol((char*)(buf + idx), NULL, 10); + ssh->scpATime = atoi((char*)(buf + idx)); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; From 5169432e399dcb309eda458df0e2c44e5c2707d9 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 19 Feb 2021 15:01:27 +0100 Subject: [PATCH 08/10] Removed options for NO_SCP_TIMESTAMP, NO_SCP_ENTER_DIRECTORY --- keys/hansel-key-ecc.pem | 0 src/wolfscp.c | 8 -------- 2 files changed, 8 deletions(-) mode change 100644 => 100755 keys/hansel-key-ecc.pem diff --git a/keys/hansel-key-ecc.pem b/keys/hansel-key-ecc.pem old mode 100644 new mode 100755 diff --git a/src/wolfscp.c b/src/wolfscp.c index 2785395..c49a48a 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -236,7 +236,6 @@ static int ScpSourceInit(WOLFSSH* ssh) } -#ifndef NO_SCP_TIMESTAMP /* Sends timestamp information (access, modification) to peer. * * T 0 0 @@ -273,7 +272,6 @@ static int SendScpTimestamp(WOLFSSH* ssh) return ret; } -#endif @@ -314,7 +312,6 @@ static int SendScpFileHeader(WOLFSSH* ssh) return ret; } -#ifndef NO_SCP_ENTER_DIRECTORY /* Sends directory message to peer, length is ignored but must * be present in message format (set to 0). * @@ -347,7 +344,6 @@ static int SendScpEnterDirectory(WOLFSSH* ssh) return ret; } -#endif /* Sends end directory message to peer. * @@ -519,7 +515,6 @@ int DoScpSource(WOLFSSH* ssh) continue; -#ifndef NO_SCP_TIMESTAMP case SCP_SEND_TIMESTAMP: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_TIMESTAMP"); @@ -531,9 +526,7 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_SEND_FILE_HEADER; continue; -#endif -#ifndef NO_SCP_ENTER_DIRECTORY case SCP_SEND_ENTER_DIRECTORY: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_ENTER_DIRECTORY"); @@ -545,7 +538,6 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_TRANSFER; continue; -#endif case SCP_SEND_EXIT_DIRECTORY: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_EXIT_DIRECTORY"); From f128bea77991f8d2841438343798f9d3e8dc833e Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Mon, 22 Mar 2021 17:40:12 +0100 Subject: [PATCH 09/10] Fixes after reviewer's comments --- src/wolfscp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/wolfscp.c b/src/wolfscp.c index c49a48a..61c18b5 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -1042,7 +1042,6 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, return ret; } -#ifndef NO_FILESYSTEM /* checks for if directory is being renamed in command * * returns WS_SUCCESS on success @@ -1058,6 +1057,10 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) return WS_BUFFER_E; } + if (cmdSz + 4 > DEFAULT_SCP_MSG_SZ) { + return WS_BUFFER_E; + } + WSTRNCPY(buf, ssh->scpBasePath, cmdSz); buf[sz] = '\0'; WSTRNCAT(buf, "/..", sizeof("/..")); @@ -1118,15 +1121,14 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) return WS_SUCCESS; } -#endif /* helps with checking if the base path is a directory or file * returns WS_SUCCESS on success */ static int ParseBasePathHelper(WOLFSSH* ssh, int cmdSz) { int ret = 0; -#ifndef NO_FILESYSTEM ret = ScpCheckForRename(ssh, cmdSz); +#ifndef NO_FILESYSTEM { ScpSendCtx ctx; From fa0102eb894e02d6a2bccf49cf67092896c334a9 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Mon, 22 Mar 2021 17:48:26 +0100 Subject: [PATCH 10/10] Fixed permissions for private key --- keys/hansel-key-ecc.pem | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 keys/hansel-key-ecc.pem diff --git a/keys/hansel-key-ecc.pem b/keys/hansel-key-ecc.pem old mode 100755 new mode 100644