From c552af214e308d3e6f81b4601485d42834cc075f Mon Sep 17 00:00:00 2001 From: John Safranek Date: Sun, 19 Jul 2020 17:17:09 -0700 Subject: [PATCH] SSH-AGENT 1. Replace strsep() with strtok_r() for portability with Win32. 2. Check return code from fopen as well as the provided fd. 3. Remove a redundant null check when using the agent to sign. --- src/agent.c | 5 ----- src/ssh.c | 14 ++++++-------- wolfssh/port.h | 8 ++++++++ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/agent.c b/src/agent.c index 2543bfc..ef54a59 100644 --- a/src/agent.c +++ b/src/agent.c @@ -1669,11 +1669,6 @@ int wolfSSH_AGENT_SignRequest(WOLFSSH* ssh, ret = WS_BAD_ARGUMENT; } - if (ret == WS_SUCCESS) { - if (agent == NULL) - ret = WS_AGENT_NULL_E; - } - if (ret == WS_SUCCESS) { agent = ssh->agent; if (ssh->ctx->agentCb) diff --git a/src/ssh.c b/src/ssh.c index c34a91e..ad655b9 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -1366,8 +1366,6 @@ char* wolfSSH_GetUsername(WOLFSSH* ssh) #include #include -#define WSTRDUP(x,y) strdup((x)) -#define WSTRSEP(x,y) strsep((x),(y)) int wolfSSH_ReadKey_buffer(const byte* in, word32 inSz, int format, byte** out, word32* outSz, const byte** outType, word32* outTypeSz, void* heap) @@ -1381,8 +1379,8 @@ int wolfSSH_ReadKey_buffer(const byte* in, word32 inSz, int format, return WS_BAD_ARGUMENT; if (format == WOLFSSH_FORMAT_SSH) { - char* dup; char* c; + char* last; char* type; char* key; @@ -1390,9 +1388,9 @@ int wolfSSH_ReadKey_buffer(const byte* in, word32 inSz, int format, SSH format is: type AAAABASE64ENCODEDKEYDATA comment */ - c = dup = WSTRDUP((const char*)in, heap); - type = WSTRSEP(&c, " \n"); - key = WSTRSEP(&c, " \n"); + c = WSTRDUP((const char*)in, heap); + type = WSTRTOK(c, " \n", &last); + key = WSTRTOK(NULL, " \n", &last); if (type != NULL && key != NULL) { const char* name; @@ -1423,7 +1421,7 @@ int wolfSSH_ReadKey_buffer(const byte* in, word32 inSz, int format, if (ret != 0) ret = WS_ERROR; - WFREE(dup, heap, DYNTYPE_STRING); + WFREE(c, heap, DYNTYPE_STRING); } else if (format == WOLFSSH_FORMAT_ASN1) { byte* newKey; @@ -1505,7 +1503,7 @@ int wolfSSH_ReadKey_file(const char* name, return WS_BAD_ARGUMENT; ret = WFOPEN(&file, name, "rb"); - if (file == WBADFILE) return WS_BAD_FILE_E; + if (ret != 0 || file == WBADFILE) return WS_BAD_FILE_E; if (WFSEEK(file, 0, WSEEK_END) != 0) { WFCLOSE(file); return WS_BAD_FILE_E; diff --git a/wolfssh/port.h b/wolfssh/port.h index 06a789c..ccb2288 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -307,18 +307,24 @@ extern "C" { #define WSTRNCASECMP(s1,s2,n) _strnicmp((s1),(s2),(n)) #define WSNPRINTF(s,n,f,...) _snprintf_s((s),(n),(n),(f),##__VA_ARGS__) #define WVSNPRINTF(s,n,f,...) _vsnprintf_s((s),(n),(n),(f),##__VA_ARGS__) + #define WSTRTOK(s1,s2,s3) strtok_s((s1),(s2),(s3)) + #define WSTRDUP(s,h) _strdup((s)) #elif defined(MICROCHIP_MPLAB_HARMONY) || defined(MICROCHIP_PIC32) #include #define WSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n)) #define WSTRNCASECMP(s1, s2, n) strncmp((s1), (s2), (n)) #define WSNPRINTF(s,n,f,...) snprintf((s),(n),(f),##__VA_ARGS__) #define WVSNPRINTF(s,n,f,...) vsnprintf((s),(n),(f),##__VA_ARGS__) + #define WSTRTOK(s1,s2,s3) strtok_r((s1),(s2),(s3)) + #define WSTRDUP(s,h) strdup((s)) #elif defined(RENESAS_CSPLUS) #include #define WSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n)) #define WSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n)) #define WSNPRINTF(s,n,f,...) snprintf((s),(n),(f),__VA_ARGS__) #define WVSNPRINTF(s,n,f,...) vsnprintf((s),(n),(f),__VA_ARGS__) + #define WSTRTOK(s1,s2,s3) strtok_r((s1),(s2),(s3)) + #define WSTRDUP(s,h) strdup((s)) #else #ifndef FREESCALE_MQX #include @@ -327,6 +333,8 @@ extern "C" { #define WSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n)) #define WSNPRINTF(s,n,f,...) snprintf((s),(n),(f),##__VA_ARGS__) #define WVSNPRINTF(s,n,f,...) vsnprintf((s),(n),(f),##__VA_ARGS__) + #define WSTRTOK(s1,s2,s3) strtok_r((s1),(s2),(s3)) + #define WSTRDUP(s,h) strdup((s)) #endif #endif /* WSTRING_USER */