forked from wolfSSL/wolfssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregress.c
More file actions
1975 lines (1589 loc) · 55 KB
/
regress.c
File metadata and controls
1975 lines (1589 loc) · 55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* regress.c
*
* Regression coverage for message ordering / keying state handling.
*
* Copyright (C) 2014-2026 wolfSSL Inc.
*
* This file is part of wolfSSH.
*
* wolfSSH is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef WOLFSSL_USER_SETTINGS
#include <wolfssl/wolfcrypt/settings.h>
#else
#include <wolfssl/options.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <wolfssh/port.h>
#include <wolfssh/ssh.h>
#include <wolfssh/internal.h>
#ifdef WOLFSSH_SFTP
#include <wolfssh/wolfsftp.h>
#endif
#include "apps/wolfssh/common.h"
#ifndef WOLFSSH_NO_ABORT
#define WABORT() abort()
#else
#define WABORT()
#endif
#define PrintError(description, result) do { \
printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
printf("\n expected: "); printf description; \
printf("\n result: "); printf result; printf("\n\n"); \
} while(0)
#define Fail(description, result) do { \
PrintError(description, result); \
WABORT(); \
} while(0)
#define Assert(test, description, result) if (!(test)) Fail(description, result)
#define AssertTrue(x) Assert((x), ("%s is true", #x), (#x " => FALSE"))
#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE"))
#define AssertNotNull(x) Assert((x), ("%s is not null", #x), (#x " => NULL"))
#define AssertIntEQ(x, y) do { int _x = (int)(x); int _y = (int)(y); \
Assert(_x == _y, ("%s == %s", #x, #y), ("%d != %d", _x, _y)); } while (0)
static void ResetSession(WOLFSSH* ssh)
{
if (ssh->handshake != NULL) {
WFREE(ssh->handshake, ssh->ctx->heap, DYNTYPE_HS);
ssh->handshake = NULL;
}
ssh->isKeying = 0;
ssh->connectState = CONNECT_BEGIN;
ssh->error = 0;
}
static HandshakeInfo* AllocHandshake(WOLFSSH* ssh)
{
HandshakeInfo* hs;
hs = (HandshakeInfo*)WMALLOC(sizeof(HandshakeInfo), ssh->ctx->heap,
DYNTYPE_HS);
AssertNotNull(hs);
WMEMSET(hs, 0, sizeof(HandshakeInfo));
hs->blockSz = MIN_BLOCK_SZ;
hs->eSz = (word32)sizeof(hs->e);
hs->xSz = (word32)sizeof(hs->x);
return hs;
}
/* Build a minimal SSH binary packet carrying only a message ID.
* Layout: uint32 packetLen, byte padLen, payload[msgId], pad[padLen].
* Choose padLen so total is 8-byte aligned for the clear transport case. */
static word32 BuildPacket(byte msgId, byte* out, word32 outSz)
{
byte padLen = 6; /* 1 (msgId) +1 (padLen) +6 = 8 */
word32 packetLen = 1 + 1 + padLen; /* payload + padLen field + pad */
word32 need = 4 + packetLen;
AssertTrue(outSz >= need);
out[0] = (byte)(packetLen >> 24);
out[1] = (byte)(packetLen >> 16);
out[2] = (byte)(packetLen >> 8);
out[3] = (byte)(packetLen);
out[4] = padLen;
out[5] = msgId;
WMEMSET(out + 6, 0, padLen);
return need;
}
static byte ParseMsgId(const byte* pkt, word32 sz)
{
AssertTrue(sz >= 6);
return pkt[5];
}
static word32 AppendByte(byte* buf, word32 bufSz, word32 idx, byte value)
{
AssertTrue(idx < bufSz);
buf[idx++] = value;
return idx;
}
static word32 AppendUint32(byte* buf, word32 bufSz, word32 idx, word32 value)
{
word32 netValue = htonl(value);
AssertTrue(idx + UINT32_SZ <= bufSz);
WMEMCPY(buf + idx, &netValue, UINT32_SZ);
idx += UINT32_SZ;
return idx;
}
static word32 AppendData(byte* buf, word32 bufSz, word32 idx,
const byte* data, word32 dataSz)
{
AssertTrue(idx + dataSz <= bufSz);
if (dataSz > 0) {
WMEMCPY(buf + idx, data, dataSz);
idx += dataSz;
}
return idx;
}
static word32 AppendString(byte* buf, word32 bufSz, word32 idx,
const char* value)
{
word32 valueSz = (word32)WSTRLEN(value);
idx = AppendUint32(buf, bufSz, idx, valueSz);
return AppendData(buf, bufSz, idx, (const byte*)value, valueSz);
}
static word32 WrapPacket(byte msgId, const byte* payload, word32 payloadSz,
byte* out, word32 outSz)
{
word32 idx = 0;
word32 packetLen;
word32 need;
byte padLen = MIN_PAD_LENGTH;
while (((UINT32_SZ + PAD_LENGTH_SZ + MSG_ID_SZ + payloadSz + padLen) %
MIN_BLOCK_SZ) != 0) {
padLen++;
}
packetLen = PAD_LENGTH_SZ + MSG_ID_SZ + payloadSz + padLen;
need = UINT32_SZ + packetLen;
AssertTrue(outSz >= need);
idx = AppendUint32(out, outSz, idx, packetLen);
idx = AppendByte(out, outSz, idx, padLen);
idx = AppendByte(out, outSz, idx, msgId);
idx = AppendData(out, outSz, idx, payload, payloadSz);
AssertTrue(idx + padLen <= outSz);
WMEMSET(out + idx, 0, padLen);
idx += padLen;
return idx;
}
static word32 BuildChannelOpenPacket(const char* type, word32 peerChannelId,
word32 peerInitialWindowSz, word32 peerMaxPacketSz,
const byte* extra, word32 extraSz, byte* out, word32 outSz)
{
byte payload[256];
word32 idx = 0;
idx = AppendString(payload, sizeof(payload), idx, type);
idx = AppendUint32(payload, sizeof(payload), idx, peerChannelId);
idx = AppendUint32(payload, sizeof(payload), idx, peerInitialWindowSz);
idx = AppendUint32(payload, sizeof(payload), idx, peerMaxPacketSz);
idx = AppendData(payload, sizeof(payload), idx, extra, extraSz);
return WrapPacket(MSGID_CHANNEL_OPEN, payload, idx, out, outSz);
}
static word32 BuildDisconnectPacket(word32 reason, byte* out, word32 outSz)
{
byte payload[64];
word32 idx = 0;
idx = AppendUint32(payload, sizeof(payload), idx, reason);
idx = AppendUint32(payload, sizeof(payload), idx, 0);
idx = AppendUint32(payload, sizeof(payload), idx, 0);
return WrapPacket(MSGID_DISCONNECT, payload, idx, out, outSz);
}
#ifdef WOLFSSH_FWD
static word32 BuildDirectTcpipExtra(const char* host, word32 hostPort,
const char* origin, word32 originPort, byte* out, word32 outSz)
{
word32 idx = 0;
idx = AppendString(out, outSz, idx, host);
idx = AppendUint32(out, outSz, idx, hostPort);
idx = AppendString(out, outSz, idx, origin);
idx = AppendUint32(out, outSz, idx, originPort);
return idx;
}
static word32 BuildGlobalRequestFwdPacket(const char* bindAddr, word32 bindPort,
int isCancel, byte wantReply, byte* out, word32 outSz)
{
byte payload[256];
word32 idx = 0;
const char* reqName = isCancel ? "cancel-tcpip-forward" : "tcpip-forward";
idx = AppendString(payload, sizeof(payload), idx, reqName);
idx = AppendByte (payload, sizeof(payload), idx, wantReply);
idx = AppendString(payload, sizeof(payload), idx, bindAddr);
idx = AppendUint32(payload, sizeof(payload), idx, bindPort);
return WrapPacket(MSGID_GLOBAL_REQUEST, payload, idx, out, outSz);
}
#endif
/* Simple in-memory transport harness */
typedef struct {
byte* in; /* data to feed into client */
word32 inSz;
word32 inOff;
byte* out; /* data written by client */
word32 outSz;
word32 outCap;
} MemIo;
static int MemRecv(WOLFSSH* ssh, void* buf, word32 sz, void* ctx)
{
(void)ssh;
MemIo* io = (MemIo*)ctx;
word32 remain = io->inSz - io->inOff;
if (remain == 0)
return WS_CBIO_ERR_WANT_READ;
if (sz > remain)
sz = remain;
WMEMCPY(buf, io->in + io->inOff, sz);
io->inOff += sz;
return (int)sz;
}
static int MemSend(WOLFSSH* ssh, void* buf, word32 sz, void* ctx)
{
(void)ssh;
MemIo* io = (MemIo*)ctx;
if (io->outSz + sz > io->outCap) {
return WS_CBIO_ERR_GENERAL;
}
WMEMCPY(io->out + io->outSz, buf, sz);
io->outSz += sz;
return (int)sz;
}
static void MemIoInit(MemIo* io, byte* in, word32 inSz, byte* out, word32 outCap)
{
io->in = in;
io->inSz = inSz;
io->inOff = 0;
io->out = out;
io->outSz = 0;
io->outCap = outCap;
}
typedef struct {
WOLFSSH_CTX* ctx;
WOLFSSH* ssh;
MemIo io;
byte out[256];
} ChannelOpenHarness;
static void InitChannelOpenHarness(ChannelOpenHarness* harness,
byte* in, word32 inSz)
{
WMEMSET(harness, 0, sizeof(*harness));
harness->ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
AssertNotNull(harness->ctx);
wolfSSH_SetIORecv(harness->ctx, MemRecv);
wolfSSH_SetIOSend(harness->ctx, MemSend);
harness->ssh = wolfSSH_new(harness->ctx);
AssertNotNull(harness->ssh);
MemIoInit(&harness->io, in, inSz, harness->out, sizeof(harness->out));
wolfSSH_SetIOReadCtx(harness->ssh, &harness->io);
wolfSSH_SetIOWriteCtx(harness->ssh, &harness->io);
harness->ssh->acceptState = ACCEPT_SERVER_USERAUTH_SENT;
}
static void FreeChannelOpenHarness(ChannelOpenHarness* harness)
{
if (harness->ssh != NULL)
wolfSSH_free(harness->ssh);
if (harness->ctx != NULL)
wolfSSH_CTX_free(harness->ctx);
}
#if !defined(NO_WOLFSSH_SERVER) && !defined(NO_WOLFSSH_CLIENT) && \
!defined(WOLFSSH_NO_RSA) && !defined(NO_FILESYSTEM)
#if !defined(WOLFSSH_NO_DH_GROUP14_SHA256)
#define KEXDH_REPLY_REGRESS_KEX_ALGO "diffie-hellman-group14-sha256"
#elif !defined(WOLFSSH_NO_DH_GROUP16_SHA512)
#define KEXDH_REPLY_REGRESS_KEX_ALGO "diffie-hellman-group16-sha512"
#elif !defined(WOLFSSH_NO_DH_GROUP14_SHA1)
#define KEXDH_REPLY_REGRESS_KEX_ALGO "diffie-hellman-group14-sha1"
#elif !defined(WOLFSSH_NO_DH_GROUP1_SHA1)
#define KEXDH_REPLY_REGRESS_KEX_ALGO "diffie-hellman-group1-sha1"
#endif
#endif
#ifdef KEXDH_REPLY_REGRESS_KEX_ALGO
#define REGRESS_DUPLEX_QUEUE_SZ 32768U
#define REGRESS_MUTATION_SCRATCH_SZ 4096U
#define REGRESS_SERVER_KEY_PATH "keys/server-key-rsa.der"
#define REGRESS_USERNAME "jill"
#define REGRESS_PASSWORD "upthehill"
#define REGRESS_MAX_HANDSHAKE_STEPS 2048
#define REGRESS_SSH_PROTO_PREFIX "SSH-"
#define REGRESS_SSH_PROTO_PREFIX_SZ 4U
typedef struct {
byte data[REGRESS_DUPLEX_QUEUE_SZ];
word32 len;
} DuplexQueue;
typedef struct {
byte enabled;
int parseError;
word32 matchedPackets;
word32 mutatedPackets;
byte scratch[REGRESS_MUTATION_SCRATCH_SZ];
word32 scratchSz;
} KexReplyMutator;
typedef struct DuplexEndpoint {
DuplexQueue inbound;
struct DuplexEndpoint* peer;
KexReplyMutator* mutator;
byte isServer;
} DuplexEndpoint;
typedef struct {
WOLFSSH_CTX* clientCtx;
WOLFSSH_CTX* serverCtx;
WOLFSSH* client;
WOLFSSH* server;
DuplexEndpoint clientIo;
DuplexEndpoint serverIo;
KexReplyMutator mutator;
} KexReplyHarness;
typedef struct {
int clientRet;
int clientErr;
int serverRet;
int serverErr;
int clientSuccess;
int serverSuccess;
word32 steps;
} KexReplyRunResult;
static word32 ReadUint32(const byte* buf)
{
return ((word32)buf[0] << 24) | ((word32)buf[1] << 16) |
((word32)buf[2] << 8) | (word32)buf[3];
}
static int ReadStringRef(word32* strSz, const byte** str,
const byte* buf, word32 len, word32* idx)
{
if (strSz == NULL || str == NULL || buf == NULL || idx == NULL) {
return WS_BAD_ARGUMENT;
}
if (*idx > len || len - *idx < LENGTH_SZ) {
return WS_PARSE_E;
}
*strSz = ReadUint32(buf + *idx);
*idx += LENGTH_SZ;
if (*strSz > len - *idx) {
return WS_PARSE_E;
}
*str = buf + *idx;
*idx += *strSz;
return WS_SUCCESS;
}
static word32 AppendBlob(byte* buf, word32 bufSz, word32 idx,
const byte* data, word32 dataSz)
{
idx = AppendUint32(buf, bufSz, idx, dataSz);
return AppendData(buf, bufSz, idx, data, dataSz);
}
static word32 LoadFileBuffer(const char* path, byte* buf, word32 bufSz)
{
WFILE* file;
long fileSz;
word32 readSz;
if (path == NULL || buf == NULL || bufSz == 0) {
return 0;
}
if (WFOPEN(NULL, &file, path, "rb") != 0 || file == WBADFILE) {
return 0;
}
WFSEEK(NULL, file, 0, WSEEK_END);
fileSz = WFTELL(NULL, file);
WREWIND(NULL, file);
if (fileSz <= 0 || (word32)fileSz > bufSz) {
WFCLOSE(NULL, file);
return 0;
}
readSz = (word32)WFREAD(NULL, buf, 1, fileSz, file);
WFCLOSE(NULL, file);
if (readSz != (word32)fileSz) {
return 0;
}
return readSz;
}
static int RegressionClientUserAuth(byte authType,
WS_UserAuthData* authData, void* ctx)
{
static const char password[] = REGRESS_PASSWORD;
(void)ctx;
if (authType != WOLFSSH_USERAUTH_PASSWORD || authData == NULL) {
return WOLFSSH_USERAUTH_INVALID_AUTHTYPE;
}
authData->sf.password.password = (byte*)password;
authData->sf.password.passwordSz = (word32)WSTRLEN(password);
return WOLFSSH_USERAUTH_SUCCESS;
}
static int RegressionServerUserAuth(byte authType,
WS_UserAuthData* authData, void* ctx)
{
static const char password[] = REGRESS_PASSWORD;
word32 passwordSz = (word32)WSTRLEN(password);
(void)ctx;
if (authType != WOLFSSH_USERAUTH_PASSWORD || authData == NULL) {
return WOLFSSH_USERAUTH_FAILURE;
}
if (authData->sf.password.password == NULL ||
authData->sf.password.passwordSz != passwordSz) {
return WOLFSSH_USERAUTH_FAILURE;
}
if (WMEMCMP(authData->sf.password.password, password, passwordSz) != 0) {
return WOLFSSH_USERAUTH_FAILURE;
}
return WOLFSSH_USERAUTH_SUCCESS;
}
static int AcceptAnyServerHostKey(const byte* pubKey, word32 pubKeySz,
void* ctx)
{
(void)pubKey;
(void)pubKeySz;
(void)ctx;
return 0;
}
static int QueueAppend(DuplexQueue* queue, const byte* data, word32 dataSz)
{
if (queue == NULL || data == NULL) {
return WS_BAD_ARGUMENT;
}
if (dataSz > sizeof(queue->data) - queue->len) {
return WS_BUFFER_E;
}
WMEMCPY(queue->data + queue->len, data, dataSz);
queue->len += dataSz;
return WS_SUCCESS;
}
static int RewriteSingleKexDhReplyPacket(const byte* packet, word32 packetSz,
const char* replacement, byte* out, word32 outSz, word32* outLen)
{
const byte* payload;
const byte* pubKey;
const byte* f;
const byte* sigBlob;
const byte* sigName;
const byte* sigData;
word32 packetLen, padLen, payloadSz;
word32 pubKeySz, fSz, sigBlobSz;
word32 sigNameSz, sigDataSz;
word32 idx = 0;
word32 innerIdx = 0;
word32 outerIdx = 0;
word32 innerSigSz;
byte payloadBuf[REGRESS_MUTATION_SCRATCH_SZ];
byte innerSig[REGRESS_MUTATION_SCRATCH_SZ];
if (replacement == NULL || out == NULL || outLen == NULL) {
return WS_BAD_ARGUMENT;
}
if (packetSz < UINT32_SZ + PAD_LENGTH_SZ + MSG_ID_SZ) {
return 0;
}
packetLen = ReadUint32(packet);
if (packetLen + UINT32_SZ != packetSz) {
return 0;
}
padLen = packet[UINT32_SZ];
if (packetLen < PAD_LENGTH_SZ + MSG_ID_SZ + padLen) {
return WS_PARSE_E;
}
if (packet[UINT32_SZ + PAD_LENGTH_SZ] != MSGID_KEXDH_REPLY) {
return 0;
}
payload = packet + UINT32_SZ + PAD_LENGTH_SZ + MSG_ID_SZ;
payloadSz = packetSz - UINT32_SZ - PAD_LENGTH_SZ - MSG_ID_SZ - padLen;
if (ReadStringRef(&pubKeySz, &pubKey, payload, payloadSz, &idx) !=
WS_SUCCESS) {
return WS_PARSE_E;
}
if (ReadStringRef(&fSz, &f, payload, payloadSz, &idx) != WS_SUCCESS) {
return WS_PARSE_E;
}
if (ReadStringRef(&sigBlobSz, &sigBlob, payload, payloadSz, &idx) !=
WS_SUCCESS) {
return WS_PARSE_E;
}
if (ReadStringRef(&sigNameSz, &sigName, sigBlob, sigBlobSz, &innerIdx) !=
WS_SUCCESS) {
return WS_PARSE_E;
}
if (ReadStringRef(&sigDataSz, &sigData, sigBlob, sigBlobSz, &innerIdx) !=
WS_SUCCESS) {
return WS_PARSE_E;
}
if (innerIdx != sigBlobSz) {
return WS_PARSE_E;
}
(void)sigName;
(void)sigNameSz;
innerSigSz = 0;
innerSigSz = AppendString(innerSig, sizeof(innerSig), innerSigSz,
replacement);
innerSigSz = AppendBlob(innerSig, sizeof(innerSig), innerSigSz,
sigData, sigDataSz);
outerIdx = 0;
outerIdx = AppendBlob(payloadBuf, sizeof(payloadBuf), outerIdx,
pubKey, pubKeySz);
outerIdx = AppendBlob(payloadBuf, sizeof(payloadBuf), outerIdx, f, fSz);
outerIdx = AppendBlob(payloadBuf, sizeof(payloadBuf), outerIdx,
innerSig, innerSigSz);
*outLen = WrapPacket(MSGID_KEXDH_REPLY, payloadBuf, outerIdx, out, outSz);
return 1;
}
static int RewriteKexDhReplySignatureName(const byte* packet, word32 packetSz,
const char* replacement, byte* out, word32 outSz, word32* outLen)
{
word32 offset = 0;
if (packet == NULL || replacement == NULL || out == NULL || outLen == NULL) {
return WS_BAD_ARGUMENT;
}
while (packetSz - offset >= UINT32_SZ + PAD_LENGTH_SZ + MSG_ID_SZ) {
word32 curPacketSz = ReadUint32(packet + offset) + UINT32_SZ;
int rewriteRet;
if (curPacketSz > packetSz - offset) {
return 0;
}
if (packet[offset + UINT32_SZ + PAD_LENGTH_SZ] == MSGID_KEXDH_REPLY) {
rewriteRet = RewriteSingleKexDhReplyPacket(packet + offset,
curPacketSz, replacement, out, outSz, outLen);
if (rewriteRet <= 0) {
return rewriteRet;
}
if (packetSz - offset - curPacketSz > outSz - *outLen) {
return WS_BUFFER_E;
}
WMEMCPY(out + *outLen, packet + offset + curPacketSz,
packetSz - offset - curPacketSz);
*outLen += packetSz - offset - curPacketSz;
return 1;
}
offset += curPacketSz;
}
return 0;
}
static int DuplexRecv(WOLFSSH* ssh, void* buf, word32 sz, void* ctx)
{
DuplexEndpoint* endpoint = (DuplexEndpoint*)ctx;
word32 readSz;
(void)ssh;
if (endpoint == NULL || buf == NULL) {
return WS_CBIO_ERR_GENERAL;
}
if (endpoint->inbound.len == 0) {
return WS_CBIO_ERR_WANT_READ;
}
readSz = sz;
if (readSz > endpoint->inbound.len) {
readSz = endpoint->inbound.len;
}
WMEMCPY(buf, endpoint->inbound.data, readSz);
endpoint->inbound.len -= readSz;
if (endpoint->inbound.len > 0) {
WMEMMOVE(endpoint->inbound.data, endpoint->inbound.data + readSz,
endpoint->inbound.len);
}
return (int)readSz;
}
static int DuplexSend(WOLFSSH* ssh, void* buf, word32 sz, void* ctx)
{
DuplexEndpoint* endpoint = (DuplexEndpoint*)ctx;
const byte* output = (const byte*)buf;
word32 outputSz = sz;
int ret;
(void)ssh;
if (endpoint == NULL || endpoint->peer == NULL || buf == NULL) {
return WS_CBIO_ERR_GENERAL;
}
if (endpoint->isServer && endpoint->mutator != NULL &&
endpoint->mutator->enabled &&
endpoint->mutator->mutatedPackets == 0 &&
outputSz >= UINT32_SZ + PAD_LENGTH_SZ + MSG_ID_SZ &&
!(outputSz >= REGRESS_SSH_PROTO_PREFIX_SZ &&
WMEMCMP(output, REGRESS_SSH_PROTO_PREFIX,
REGRESS_SSH_PROTO_PREFIX_SZ) == 0)) {
word32 mutatedSz = 0;
int mutateRet;
mutateRet = RewriteKexDhReplySignatureName(output, outputSz, "ssh-rsa",
endpoint->mutator->scratch,
(word32)sizeof(endpoint->mutator->scratch), &mutatedSz);
if (mutateRet < 0) {
endpoint->mutator->parseError = mutateRet;
return WS_CBIO_ERR_GENERAL;
}
if (mutateRet > 0) {
endpoint->mutator->matchedPackets++;
endpoint->mutator->mutatedPackets++;
endpoint->mutator->scratchSz = mutatedSz;
output = endpoint->mutator->scratch;
outputSz = mutatedSz;
}
}
ret = QueueAppend(&endpoint->peer->inbound, output, outputSz);
if (ret != WS_SUCCESS) {
return WS_CBIO_ERR_GENERAL;
}
return (int)sz;
}
static void InitDuplexPair(DuplexEndpoint* client, DuplexEndpoint* server,
KexReplyMutator* mutator)
{
WMEMSET(client, 0, sizeof(*client));
WMEMSET(server, 0, sizeof(*server));
client->peer = server;
server->peer = client;
server->mutator = mutator;
server->isServer = 1;
}
static void FreeKexReplyHarness(KexReplyHarness* harness)
{
if (harness->client != NULL) {
wolfSSH_free(harness->client);
}
if (harness->server != NULL) {
wolfSSH_free(harness->server);
}
if (harness->clientCtx != NULL) {
wolfSSH_CTX_free(harness->clientCtx);
}
if (harness->serverCtx != NULL) {
wolfSSH_CTX_free(harness->serverCtx);
}
}
static void InitKexReplyHarnessEx(KexReplyHarness* harness,
const char* keyAlgo, byte mutateReply, byte skipPublicKeyCheck)
{
byte keyBuf[2048];
word32 keySz;
WMEMSET(harness, 0, sizeof(*harness));
InitDuplexPair(&harness->clientIo, &harness->serverIo, &harness->mutator);
harness->mutator.enabled = mutateReply;
harness->clientCtx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL);
AssertNotNull(harness->clientCtx);
harness->serverCtx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
AssertNotNull(harness->serverCtx);
AssertIntEQ(wolfSSH_CTX_SetAlgoListKex(harness->clientCtx,
KEXDH_REPLY_REGRESS_KEX_ALGO), WS_SUCCESS);
AssertIntEQ(wolfSSH_CTX_SetAlgoListKex(harness->serverCtx,
KEXDH_REPLY_REGRESS_KEX_ALGO), WS_SUCCESS);
AssertIntEQ(wolfSSH_CTX_SetAlgoListKey(harness->clientCtx, keyAlgo),
WS_SUCCESS);
AssertIntEQ(wolfSSH_CTX_SetAlgoListKey(harness->serverCtx, keyAlgo),
WS_SUCCESS);
wolfSSH_SetIORecv(harness->clientCtx, DuplexRecv);
wolfSSH_SetIOSend(harness->clientCtx, DuplexSend);
wolfSSH_SetIORecv(harness->serverCtx, DuplexRecv);
wolfSSH_SetIOSend(harness->serverCtx, DuplexSend);
wolfSSH_SetUserAuth(harness->clientCtx, RegressionClientUserAuth);
wolfSSH_SetUserAuth(harness->serverCtx, RegressionServerUserAuth);
if (!skipPublicKeyCheck) {
wolfSSH_CTX_SetPublicKeyCheck(harness->clientCtx, AcceptAnyServerHostKey);
}
keySz = LoadFileBuffer(REGRESS_SERVER_KEY_PATH, keyBuf, sizeof(keyBuf));
AssertTrue(keySz > 0);
AssertIntEQ(wolfSSH_CTX_UsePrivateKey_buffer(harness->serverCtx, keyBuf,
keySz, WOLFSSH_FORMAT_ASN1), WS_SUCCESS);
harness->client = wolfSSH_new(harness->clientCtx);
AssertNotNull(harness->client);
harness->server = wolfSSH_new(harness->serverCtx);
AssertNotNull(harness->server);
wolfSSH_SetIOReadCtx(harness->client, &harness->clientIo);
wolfSSH_SetIOWriteCtx(harness->client, &harness->clientIo);
wolfSSH_SetIOReadCtx(harness->server, &harness->serverIo);
wolfSSH_SetIOWriteCtx(harness->server, &harness->serverIo);
AssertIntEQ(wolfSSH_SetUsername(harness->client, REGRESS_USERNAME),
WS_SUCCESS);
}
static void InitKexReplyHarness(KexReplyHarness* harness,
const char* keyAlgo, byte mutateReply)
{
InitKexReplyHarnessEx(harness, keyAlgo, mutateReply, 0);
}
static int IsHandshakeRetryable(int err)
{
return err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_AUTH_PENDING;
}
static void RunKexReplyHandshake(KexReplyHarness* harness,
KexReplyRunResult* result)
{
word32 step;
WMEMSET(result, 0, sizeof(*result));
result->clientRet = WS_FATAL_ERROR;
result->serverRet = WS_FATAL_ERROR;
for (step = 0; step < REGRESS_MAX_HANDSHAKE_STEPS; step++) {
if (!result->clientSuccess) {
result->clientRet = wolfSSH_connect(harness->client);
result->clientErr = wolfSSH_get_error(harness->client);
if (result->clientRet == WS_SUCCESS) {
result->clientSuccess = 1;
}
else if (!IsHandshakeRetryable(result->clientErr)) {
result->steps = step + 1;
return;
}
}
if (!result->serverSuccess) {
result->serverRet = wolfSSH_accept(harness->server);
result->serverErr = wolfSSH_get_error(harness->server);
if (result->serverRet == WS_SUCCESS) {
result->serverSuccess = 1;
}
else if (!IsHandshakeRetryable(result->serverErr)) {
result->steps = step + 1;
return;
}
}
if (result->clientSuccess && result->serverSuccess) {
result->steps = step + 1;
return;
}
}
result->steps = REGRESS_MAX_HANDSHAKE_STEPS;
}
static void AssertHandshakeSucceeds(const char* keyAlgo)
{
KexReplyHarness harness;
KexReplyRunResult result;
InitKexReplyHarness(&harness, keyAlgo, 0);
RunKexReplyHandshake(&harness, &result);
AssertTrue(result.clientSuccess);
AssertTrue(result.serverSuccess);
AssertIntEQ(harness.mutator.mutatedPackets, 0);
AssertIntEQ(harness.client->connectState, CONNECT_SERVER_CHANNEL_REQUEST_DONE);
AssertIntEQ(harness.server->acceptState, ACCEPT_CLIENT_SESSION_ESTABLISHED);
FreeKexReplyHarness(&harness);
}
static void AssertHandshakeRejectsMutatedReply(const char* keyAlgo)
{
KexReplyHarness harness;
KexReplyRunResult result;
InitKexReplyHarness(&harness, keyAlgo, 1);
RunKexReplyHandshake(&harness, &result);
AssertIntEQ(harness.mutator.parseError, 0);
AssertIntEQ(harness.mutator.matchedPackets, 1);
AssertIntEQ(harness.mutator.mutatedPackets, 1);
AssertFalse(result.clientSuccess);
AssertFalse(harness.client->connectState >= CONNECT_KEYED);
AssertTrue(result.clientRet == WS_FATAL_ERROR);
AssertTrue(result.clientErr != WS_WANT_READ && result.clientErr != WS_WANT_WRITE);
FreeKexReplyHarness(&harness);
}
#ifndef WOLFSSH_NO_RSA_SHA2_256
static void TestKexDhReplyRejectsRsaSha2_256SigNameDowngrade(void)
{
AssertHandshakeSucceeds("rsa-sha2-256");
AssertHandshakeRejectsMutatedReply("rsa-sha2-256");
}
#endif
#ifndef WOLFSSH_NO_RSA_SHA2_512
static void TestKexDhReplyRejectsRsaSha2_512SigNameDowngrade(void)
{
AssertHandshakeSucceeds("rsa-sha2-512");
AssertHandshakeRejectsMutatedReply("rsa-sha2-512");
}
#endif
static void AssertHandshakeRejectsWithNoPublicKeyCheck(const char* keyAlgo)
{
KexReplyHarness harness;
KexReplyRunResult result;
InitKexReplyHarnessEx(&harness, keyAlgo, 0, 1 /* skipPublicKeyCheck */);
RunKexReplyHandshake(&harness, &result);
AssertFalse(result.clientSuccess);
AssertTrue(result.clientRet == WS_FATAL_ERROR);
AssertTrue(result.clientErr != WS_WANT_READ && result.clientErr != WS_WANT_WRITE);
AssertIntEQ(result.clientErr, WS_PUBKEY_REJECTED_E);
AssertFalse(harness.client->connectState >= CONNECT_KEYED);
FreeKexReplyHarness(&harness);
}
static void TestKexDhReplyRejectsNoPublicKeyCheck(void)
{
#ifndef WOLFSSH_NO_RSA_SHA2_256
AssertHandshakeRejectsWithNoPublicKeyCheck("rsa-sha2-256");
#endif
#ifndef WOLFSSH_NO_RSA_SHA2_512
AssertHandshakeRejectsWithNoPublicKeyCheck("rsa-sha2-512");
#endif
}
#endif /* KEXDH_REPLY_REGRESS_KEX_ALGO */
static void AssertChannelOpenFailResponse(const ChannelOpenHarness* harness,
int ret)
{
byte msgId;
AssertIntEQ(ret, WS_SUCCESS);
AssertIntEQ(harness->io.inOff, harness->io.inSz);
AssertTrue(harness->io.outSz > 0);
AssertTrue(harness->io.outSz <= harness->io.outCap);
msgId = ParseMsgId(harness->io.out, harness->io.outSz);
AssertIntEQ(msgId, MSGID_CHANNEL_OPEN_FAIL);
AssertFalse(msgId == MSGID_REQUEST_FAILURE);
AssertIntEQ(harness->ssh->channelListSz, 0);
AssertTrue(harness->ssh->channelList == NULL);
}
#ifdef WOLFSSH_FWD
static word32 ParsePayloadLen(const byte* packet, word32 packetSz)
{
word32 packetLen;
byte padLen;
AssertNotNull(packet);
AssertTrue(packetSz >= 6);
WMEMCPY(&packetLen, packet, sizeof(packetLen));
packetLen = ntohl(packetLen);
padLen = packet[4];
AssertTrue(packetLen >= (word32)padLen + 1);
AssertTrue(packetSz >= packetLen + 4);
return packetLen - padLen - 1;
}
static const byte* ParseGlobalRequestName(const byte* packet, word32 packetSz,
word32* nameSz)
{
word32 packetLen;
word32 payloadLen;
word32 strSz;
const byte* payload;