Skip to content

Commit 5cd826c

Browse files
authored
Merge pull request #795 from JacobBarthelmeh/decrypt
adjust highwater check location to avoid masking return value
2 parents e32ea90 + 1422697 commit 5cd826c

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

examples/echoserver/echoserver.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ static int sftp_worker(thread_ctx_t* threadCtx)
13081308
s = (WS_SOCKET_T)wolfSSH_get_fd(ssh);
13091309

13101310
do {
1311-
if (wolfSSH_SFTP_PendingSend(ssh)) {
1311+
if (ret == WS_WANT_WRITE || wolfSSH_SFTP_PendingSend(ssh)) {
13121312
/* Yes, process the SFTP data. */
13131313
ret = wolfSSH_SFTP_read(ssh);
13141314
error = wolfSSH_get_error(ssh);
@@ -1362,6 +1362,12 @@ static int sftp_worker(thread_ctx_t* threadCtx)
13621362
break;
13631363
}
13641364
if (ret != WS_SUCCESS && ret != WS_CHAN_RXD) {
1365+
if (ret == WS_WANT_WRITE) {
1366+
/* recall wolfSSH_worker here because is likely our custom
1367+
* highwater callback that returned up a WS_WANT_WRITE */
1368+
ret = wolfSSH_worker(ssh, NULL);
1369+
continue; /* continue on if our send got a want write */
1370+
}
13651371
/* If not successful and no channel data, leave. */
13661372
break;
13671373
}
@@ -2439,6 +2445,7 @@ static void ShowUsage(void)
24392445
"to use\n");
24402446
printf(" -m <list> set the comma separated list of mac algos to use\n");
24412447
printf(" -b <num> test user auth would block\n");
2448+
printf(" -H set test highwater callback\n");
24422449
}
24432450

24442451

@@ -2463,6 +2470,36 @@ static INLINE void SignalTcpReady(tcp_ready* ready, word16 port)
24632470
WOLFSSL_RETURN_FROM_THREAD(0); \
24642471
} while(0)
24652472

2473+
2474+
static byte wantwrite = 0; /*flag to return want write on first highwater call*/
2475+
static int my_highwaterCb(byte dir, void* ctx)
2476+
{
2477+
int ret = WS_SUCCESS;
2478+
2479+
WOLFSSH_UNUSED(dir);
2480+
2481+
printf("my_highwaterCb called\n");
2482+
if (ctx) {
2483+
WOLFSSH* ssh = (WOLFSSH*)ctx;
2484+
2485+
printf("HIGHWATER MARK: (%u) %s", wolfSSH_GetHighwater(ssh),
2486+
(dir == WOLFSSH_HWSIDE_RECEIVE) ? "receive\n" : "transmit\n");
2487+
if (dir == WOLFSSH_HWSIDE_RECEIVE) {
2488+
if (!wantwrite) {
2489+
ret = WS_WANT_WRITE;
2490+
wantwrite = 1;
2491+
printf("Forcing a want write on first highwater callback\n");
2492+
}
2493+
else {
2494+
ret = wolfSSH_TriggerKeyExchange(ssh);
2495+
}
2496+
}
2497+
2498+
}
2499+
2500+
return ret;
2501+
}
2502+
24662503
THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
24672504
{
24682505
func_args* serverArgs = (func_args*)args;
@@ -2479,6 +2516,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
24792516
WS_UserAuthData_Keyboard kbAuthData;
24802517
#endif
24812518
WS_SOCKET_T listenFd = WOLFSSH_SOCKET_INVALID;
2519+
int useCustomHighWaterCb = 0;
24822520
word32 defaultHighwater = EXAMPLE_HIGHWATER_MARK;
24832521
word32 threadCount = 0;
24842522
const char* keyList = NULL;
@@ -2513,7 +2551,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
25132551
#endif
25142552

25152553
if (argc > 0) {
2516-
const char* optlist = "?1a:d:efEp:R:Ni:j:i:I:J:K:P:k:b:x:m:c:s:";
2554+
const char* optlist = "?1a:d:efEp:R:Ni:j:i:I:J:K:P:k:b:x:m:c:s:H";
25172555
myoptind = 0;
25182556
while ((ch = mygetopt(argc, argv, optlist)) != -1) {
25192557
switch (ch) {
@@ -2625,6 +2663,10 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
26252663
#endif
26262664
break;
26272665

2666+
case 'H':
2667+
useCustomHighWaterCb = 1;
2668+
break;
2669+
26282670
default:
26292671
ShowUsage();
26302672
serverArgs->return_code = MY_EX_USAGE;
@@ -2987,6 +3029,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
29873029
WFREE(threadCtx, NULL, 0);
29883030
ES_ERROR("Couldn't allocate SSH data.\n");
29893031
}
3032+
29903033
#ifdef WOLFSSH_STATIC_MEMORY
29913034
wolfSSH_MemoryConnPrintStats(heap);
29923035
#endif
@@ -2995,12 +3038,24 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
29953038
#ifdef WOLFSSH_KEYBOARD_INTERACTIVE
29963039
wolfSSH_SetKeyboardAuthCtx(ssh, &kbAuthData);
29973040
#endif
3041+
29983042
/* Use the session object for its own highwater callback ctx */
29993043
if (defaultHighwater > 0) {
30003044
wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
30013045
wolfSSH_SetHighwater(ssh, defaultHighwater);
30023046
}
30033047

3048+
if (useCustomHighWaterCb) {
3049+
if (defaultHighwater == EXAMPLE_HIGHWATER_MARK) {
3050+
defaultHighwater = 2000; /* lower the highwater mark to hit the
3051+
* callback sooner */
3052+
}
3053+
printf("Registering highwater callback that returns want write\n");
3054+
wolfSSH_SetHighwaterCb(ctx, defaultHighwater, my_highwaterCb);
3055+
wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
3056+
wolfSSH_SetHighwater(ssh, defaultHighwater);
3057+
}
3058+
30043059
#ifdef WOLFSSH_SFTP
30053060
if (SetDefaultSftpPath(ssh, defaultSftpPath) != 0) {
30063061
ES_ERROR("Couldn't store default sftp path.\n");

scripts/sftp.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ if [ $RESULT -ne 0 ]; then
105105
exit 1
106106
fi
107107

108+
# Test want write return from highwater callback
109+
if [ $nonblockingOnly = 0 ]; then
110+
echo "Test want write return from highwater callback"
111+
./examples/echoserver/echoserver -H -N -1 -R $ready_file &
112+
server_pid=$!
113+
create_port
114+
./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p $port -g -r $PWD/README.md-2 -l $PWD/README.md
115+
RESULT=$?
116+
remove_ready_file
117+
rm -f $PWD/README.md-2
118+
if [ $RESULT -ne 0 ]; then
119+
echo -e "\n\nfailed to connect"
120+
do_cleanup
121+
exit 1
122+
fi
123+
fi
124+
108125
# Test of setting directory
109126
if [ $nonblockingOnly = 0 ]; then
110127
echo "Test of setting directory"

src/internal.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9661,9 +9661,6 @@ static INLINE int Decrypt(WOLFSSH* ssh, byte* plain, const byte* input,
96619661

96629662
ssh->rxCount += sz;
96639663

9664-
if (ret == WS_SUCCESS)
9665-
ret = HighwaterCheck(ssh, WOLFSSH_HWSIDE_RECEIVE);
9666-
96679664
return ret;
96689665
}
96699666

@@ -9932,9 +9929,6 @@ static INLINE int DecryptAead(WOLFSSH* ssh, byte* plain,
99329929
AeadIncrementExpIv(ssh->peerKeys.iv);
99339930
ssh->rxCount += sz;
99349931

9935-
if (ret == WS_SUCCESS)
9936-
ret = HighwaterCheck(ssh, WOLFSSH_HWSIDE_RECEIVE);
9937-
99389932
return ret;
99399933
}
99409934
#endif /* WOLFSSH_NO_AEAD */
@@ -9970,6 +9964,14 @@ int DoReceive(WOLFSSH* ssh)
99709964
ssh->error = ret;
99719965
return WS_FATAL_ERROR;
99729966
}
9967+
9968+
ret = HighwaterCheck(ssh, WOLFSSH_HWSIDE_RECEIVE);
9969+
if (ret != WS_SUCCESS) {
9970+
WLOG(WS_LOG_DEBUG, "PR: First HighwaterCheck fail");
9971+
ssh->error = ret;
9972+
ret = WS_FATAL_ERROR;
9973+
break;
9974+
}
99739975
}
99749976
NO_BREAK;
99759977

@@ -10054,6 +10056,14 @@ int DoReceive(WOLFSSH* ssh)
1005410056
}
1005510057
}
1005610058
ssh->processReplyState = PROCESS_PACKET;
10059+
10060+
ret = HighwaterCheck(ssh, WOLFSSH_HWSIDE_RECEIVE);
10061+
if (ret != WS_SUCCESS) {
10062+
WLOG(WS_LOG_DEBUG, "PR: HighwaterCheck fail");
10063+
ssh->error = ret;
10064+
ret = WS_FATAL_ERROR;
10065+
break;
10066+
}
1005710067
NO_BREAK;
1005810068

1005910069
case PROCESS_PACKET:

0 commit comments

Comments
 (0)