Coverage Report

Created: 2026-09-01 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/packet.c
Line
Count
Source
1
/*
2
 * packet.c - packet building functions
3
 *
4
 * This file is part of the SSH Library
5
 *
6
 * Copyright (c) 2003-2013 by Aris Adamantiadis
7
 *
8
 * The SSH Library is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or (at your
11
 * option) any later version.
12
 *
13
 * The SSH Library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16
 * License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with the SSH Library; see the file COPYING.  If not, write to
20
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
 * MA 02111-1307, USA.
22
 */
23
24
#include "config.h"
25
26
#include <stdlib.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <errno.h>
30
31
#ifndef _WIN32
32
#include <netinet/in.h>
33
#include <arpa/inet.h>
34
#endif
35
36
#include "libssh/priv.h"
37
#include "libssh/ssh2.h"
38
#include "libssh/crypto.h"
39
#include "libssh/buffer.h"
40
#include "libssh/packet.h"
41
#include "libssh/socket.h"
42
#include "libssh/channels.h"
43
#include "libssh/misc.h"
44
#include "libssh/session.h"
45
#include "libssh/messages.h"
46
#include "libssh/pcap.h"
47
#include "libssh/kex.h"
48
#include "libssh/auth.h"
49
#include "libssh/gssapi.h"
50
#include "libssh/bytearray.h"
51
#include "libssh/dh.h"
52
53
static ssh_packet_callback default_packet_handlers[]= {
54
  ssh_packet_disconnect_callback,          // SSH2_MSG_DISCONNECT                 1
55
  ssh_packet_ignore_callback,              // SSH2_MSG_IGNORE                     2
56
  ssh_packet_unimplemented,                // SSH2_MSG_UNIMPLEMENTED              3
57
  ssh_packet_debug_callback,               // SSH2_MSG_DEBUG                      4
58
#if WITH_SERVER
59
  ssh_packet_service_request,              // SSH2_MSG_SERVICE_REQUEST            5
60
#else
61
  NULL,
62
#endif
63
  ssh_packet_service_accept,               // SSH2_MSG_SERVICE_ACCEPT             6
64
  ssh_packet_ext_info,                     // SSH2_MSG_EXT_INFO                   7
65
  NULL, NULL, NULL, NULL, NULL, NULL,
66
  NULL, NULL, NULL, NULL, NULL, NULL,      //                                     8-19
67
  ssh_packet_kexinit,                      // SSH2_MSG_KEXINIT                    20
68
  ssh_packet_newkeys,                      // SSH2_MSG_NEWKEYS                    21
69
  NULL, NULL, NULL, NULL, NULL, NULL, NULL,
70
  NULL,                                    //                                     22-29
71
#if WITH_SERVER
72
  ssh_packet_kexdh_init,                   // SSH2_MSG_KEXDH_INIT                 30
73
                                           // SSH2_MSG_KEX_DH_GEX_REQUEST_OLD     30
74
#else
75
  NULL,
76
#endif
77
  NULL,                                    // SSH2_MSG_KEXDH_REPLY                31
78
                                           // SSH2_MSG_KEX_DH_GEX_GROUP           31
79
  NULL,                                    // SSH2_MSG_KEX_DH_GEX_INIT            32
80
  NULL,                                    // SSH2_MSG_KEX_DH_GEX_REPLY           33
81
  NULL,                                    // SSH2_MSG_KEX_DH_GEX_REQUEST         34
82
  NULL, NULL, NULL, NULL, NULL, NULL, NULL,
83
  NULL, NULL, NULL, NULL, NULL, NULL, NULL,
84
  NULL,                                    //                                     35-49
85
#if WITH_SERVER
86
  ssh_packet_userauth_request,             // SSH2_MSG_USERAUTH_REQUEST           50
87
#else
88
  NULL,
89
#endif
90
  ssh_packet_userauth_failure,             // SSH2_MSG_USERAUTH_FAILURE           51
91
  ssh_packet_userauth_success,             // SSH2_MSG_USERAUTH_SUCCESS           52
92
  ssh_packet_userauth_banner,              // SSH2_MSG_USERAUTH_BANNER            53
93
  NULL,NULL,NULL,NULL,NULL,NULL,           //                                     54-59
94
  ssh_packet_userauth_pk_ok,               // SSH2_MSG_USERAUTH_PK_OK             60
95
                                           // SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ  60
96
                                           // SSH2_MSG_USERAUTH_INFO_REQUEST    60
97
                                           // SSH2_MSG_USERAUTH_GSSAPI_RESPONSE   60
98
  ssh_packet_userauth_info_response,       // SSH2_MSG_USERAUTH_INFO_RESPONSE     61
99
                                           // SSH2_MSG_USERAUTH_GSSAPI_TOKEN      61
100
  NULL,                                    //                                     62
101
  NULL,                             // SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE 63
102
  NULL,                                    // SSH2_MSG_USERAUTH_GSSAPI_ERROR      64
103
  NULL,                                    // SSH2_MSG_USERAUTH_GSSAPI_ERRTOK     65
104
#if defined(WITH_GSSAPI) && defined(WITH_SERVER)
105
  ssh_packet_userauth_gssapi_mic,          // SSH2_MSG_USERAUTH_GSSAPI_MIC        66
106
#else /* WITH_GSSAPI && WITH_SERVER */
107
  NULL,
108
#endif /* WITH_GSSAPI && WITH_SERVER */
109
  NULL, NULL,
110
  NULL, NULL, NULL, NULL, NULL, NULL, NULL,
111
  NULL, NULL, NULL, NULL,                  //                                     67-79
112
#ifdef WITH_SERVER
113
  ssh_packet_global_request,               // SSH2_MSG_GLOBAL_REQUEST             80
114
#else /* WITH_SERVER */
115
  NULL,
116
#endif /* WITH_SERVER */
117
  ssh_request_success,                     // SSH2_MSG_REQUEST_SUCCESS            81
118
  ssh_request_denied,                      // SSH2_MSG_REQUEST_FAILURE            82
119
  NULL, NULL, NULL, NULL, NULL, NULL, NULL,//                                     83-89
120
  ssh_packet_channel_open,                 // SSH2_MSG_CHANNEL_OPEN               90
121
  ssh_packet_channel_open_conf,            // SSH2_MSG_CHANNEL_OPEN_CONFIRMATION  91
122
  ssh_packet_channel_open_fail,            // SSH2_MSG_CHANNEL_OPEN_FAILURE       92
123
  channel_rcv_change_window,               // SSH2_MSG_CHANNEL_WINDOW_ADJUST      93
124
  channel_rcv_data,                        // SSH2_MSG_CHANNEL_DATA               94
125
  channel_rcv_data,                        // SSH2_MSG_CHANNEL_EXTENDED_DATA      95
126
  channel_rcv_eof,                         // SSH2_MSG_CHANNEL_EOF                96
127
  channel_rcv_close,                       // SSH2_MSG_CHANNEL_CLOSE              97
128
  channel_rcv_request,                     // SSH2_MSG_CHANNEL_REQUEST            98
129
  ssh_packet_channel_success,              // SSH2_MSG_CHANNEL_SUCCESS            99
130
  ssh_packet_channel_failure,              // SSH2_MSG_CHANNEL_FAILURE            100
131
  [SSH2_MSG_PING - 1] = ssh_packet_ping,
132
  [SSH2_MSG_PONG - 1] = ssh_packet_pong,
133
};
134
135
/** @internal
136
 * @brief check if the received packet is allowed for the current session state
137
 * @param session current ssh_session
138
 * @returns SSH_PACKET_ALLOWED if the packet is allowed; SSH_PACKET_DENIED
139
 * if the packet arrived in wrong state; SSH_PACKET_UNKNOWN if the packet type
140
 * is unknown
141
 */
142
static enum ssh_packet_filter_result_e ssh_packet_incoming_filter(ssh_session session)
143
0
{
144
0
    enum ssh_packet_filter_result_e rc;
145
146
#ifdef DEBUG_PACKET
147
    SSH_LOG(SSH_LOG_PACKET, "Filtering packet type %d",
148
            session->in_packet.type);
149
#endif
150
151
0
    switch(session->in_packet.type) {
152
0
    case SSH2_MSG_DISCONNECT:                         // 1
153
        /*
154
         * States required:
155
         * - None
156
         *
157
         * Transitions:
158
         * - session->socket->state = SSH_SOCKET_CLOSED
159
         * - session->session_state = SSH_SESSION_STATE_ERROR
160
         * */
161
162
        /* Always allowed */
163
0
        rc = SSH_PACKET_ALLOWED;
164
0
        break;
165
0
    case SSH2_MSG_IGNORE:                             // 2
166
        /*
167
         * States required:
168
         * - None
169
         *
170
         * Transitions:
171
         * - None
172
         * */
173
174
        /* Always allowed */
175
0
        rc = SSH_PACKET_ALLOWED;
176
0
        break;
177
0
    case SSH2_MSG_UNIMPLEMENTED:                      // 3
178
        /*
179
         * States required:
180
         * - None
181
         *
182
         * Transitions:
183
         * - None
184
         * */
185
186
        /* Always allowed */
187
0
        rc = SSH_PACKET_ALLOWED;
188
0
        break;
189
0
    case SSH2_MSG_DEBUG:                              // 4
190
        /*
191
         * States required:
192
         * - None
193
         *
194
         * Transitions:
195
         * - None
196
         * */
197
198
        /* Always allowed */
199
0
        rc = SSH_PACKET_ALLOWED;
200
0
        break;
201
0
    case SSH2_MSG_SERVICE_REQUEST:                    // 5
202
        /* Server only */
203
204
        /*
205
         * States required:
206
         * - session->session_state == SSH_SESSION_STATE_AUTHENTICATING
207
         *   or session->session_state == SSH_SESSION_STATE_AUTHENTICATED
208
         * - session->dh_handshake_state == DH_STATE_FINISHED
209
         *
210
         * Transitions:
211
         * - None
212
         * */
213
214
        /* If this is a client, reject the message */
215
0
        if (session->client) {
216
0
            rc = SSH_PACKET_DENIED;
217
0
            break;
218
0
        }
219
220
0
        if ((session->session_state != SSH_SESSION_STATE_AUTHENTICATING) &&
221
0
            (session->session_state != SSH_SESSION_STATE_AUTHENTICATED))
222
0
        {
223
0
            rc = SSH_PACKET_DENIED;
224
0
            break;
225
0
        }
226
227
0
        if (session->dh_handshake_state != DH_STATE_FINISHED) {
228
0
            rc = SSH_PACKET_DENIED;
229
0
            break;
230
0
        }
231
232
0
        rc = SSH_PACKET_ALLOWED;
233
0
        break;
234
0
    case SSH2_MSG_SERVICE_ACCEPT:                     // 6
235
        /*
236
         * States required:
237
         * - session->session_state == SSH_SESSION_STATE_AUTHENTICATING
238
         *   or session->session_state == SSH_SESSION_STATE_AUTHENTICATED
239
         * - session->dh_handshake_state == DH_STATE_FINISHED
240
         * - session->auth.service_state == SSH_AUTH_SERVICE_SENT
241
         *
242
         * Transitions:
243
         * - auth.service_state = SSH_AUTH_SERVICE_ACCEPTED
244
         * */
245
246
0
        if ((session->session_state != SSH_SESSION_STATE_AUTHENTICATING) &&
247
0
            (session->session_state != SSH_SESSION_STATE_AUTHENTICATED))
248
0
        {
249
0
            rc = SSH_PACKET_DENIED;
250
0
            break;
251
0
        }
252
253
0
        if (session->dh_handshake_state != DH_STATE_FINISHED) {
254
0
            rc = SSH_PACKET_DENIED;
255
0
            break;
256
0
        }
257
258
        /* TODO check if only auth service can be requested */
259
0
        if (session->auth.service_state != SSH_AUTH_SERVICE_SENT) {
260
0
            rc = SSH_PACKET_DENIED;
261
0
            break;
262
0
        }
263
264
0
        rc = SSH_PACKET_ALLOWED;
265
0
        break;
266
0
    case SSH2_MSG_EXT_INFO:                           // 7
267
        /*
268
         * States required:
269
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
270
         *   or session->session_state == SSH_SESSION_STATE_AUTHENTICATED
271
         *   (re-exchange)
272
         * - dh_handshake_state == DH_STATE_FINISHED
273
         *
274
         * Transitions:
275
         * - None
276
         * */
277
278
0
        if ((session->session_state != SSH_SESSION_STATE_AUTHENTICATING) &&
279
0
            (session->session_state != SSH_SESSION_STATE_AUTHENTICATED))
280
0
        {
281
0
            rc = SSH_PACKET_DENIED;
282
0
            break;
283
0
        }
284
285
0
        if (session->dh_handshake_state != DH_STATE_FINISHED) {
286
0
            rc = SSH_PACKET_DENIED;
287
0
            break;
288
0
        }
289
290
0
        rc = SSH_PACKET_ALLOWED;
291
0
        break;
292
0
    case SSH2_MSG_KEXINIT:                            // 20
293
        /*
294
         * States required:
295
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
296
         *   or session_state == SSH_SESSION_STATE_INITIAL_KEX
297
         * - dh_handshake_state == DH_STATE_INIT
298
         *   or dh_handshake_state == DH_STATE_INIT_SENT (re-exchange)
299
         *   or dh_handshake_state == DH_STATE_REQUEST_SENT (dh-gex)
300
         *   or dh_handshake_state == DH_STATE_FINISHED (re-exchange)
301
         *
302
         * Transitions:
303
         * - session->dh_handshake_state = DH_STATE_INIT
304
         * - session->session_state = SSH_SESSION_STATE_KEXINIT_RECEIVED
305
         *
306
         * On server:
307
         * - session->session_state = SSH_SESSION_STATE_DH
308
         * */
309
310
0
        if ((session->session_state != SSH_SESSION_STATE_AUTHENTICATED) &&
311
0
            (session->session_state != SSH_SESSION_STATE_INITIAL_KEX))
312
0
        {
313
0
            rc = SSH_PACKET_DENIED;
314
0
            break;
315
0
        }
316
317
0
        if ((session->dh_handshake_state != DH_STATE_INIT) &&
318
0
            (session->dh_handshake_state != DH_STATE_INIT_SENT) &&
319
0
            (session->dh_handshake_state != DH_STATE_REQUEST_SENT) &&
320
0
            (session->dh_handshake_state != DH_STATE_FINISHED))
321
0
        {
322
0
            rc = SSH_PACKET_DENIED;
323
0
            break;
324
0
        }
325
326
0
        rc = SSH_PACKET_ALLOWED;
327
0
        break;
328
0
    case SSH2_MSG_NEWKEYS:                            // 21
329
        /*
330
         * States required:
331
         * - session_state == SSH_SESSION_STATE_DH
332
         * - dh_handshake_state == DH_STATE_NEWKEYS_SENT
333
         *
334
         * Transitions:
335
         * - session->dh_handshake_state = DH_STATE_FINISHED
336
         * - session->session_state = SSH_SESSION_STATE_AUTHENTICATING
337
         * if session->flags & SSH_SESSION_FLAG_AUTHENTICATED
338
         * - session->session_state = SSH_SESSION_STATE_AUTHENTICATED
339
         * */
340
341
        /* If DH has not been started, reject message */
342
0
        if (session->session_state != SSH_SESSION_STATE_DH) {
343
0
            rc = SSH_PACKET_DENIED;
344
0
            break;
345
0
        }
346
347
        /* Only allowed if dh_handshake_state is in NEWKEYS_SENT state */
348
0
        if (session->dh_handshake_state != DH_STATE_NEWKEYS_SENT) {
349
0
            rc = SSH_PACKET_DENIED;
350
0
            break;
351
0
        }
352
353
0
        rc = SSH_PACKET_ALLOWED;
354
0
        break;
355
0
    case SSH2_MSG_KEXDH_INIT:                         // 30
356
      // SSH2_MSG_KEX_ECDH_INIT:                      // 30
357
      // SSH2_MSG_KEX_HYBRID_INIT:                    // 30
358
      // SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:             // 30
359
360
        /* Server only */
361
362
        /*
363
         * States required:
364
         * - session_state == SSH_SESSION_STATE_DH
365
         * - dh_handshake_state == DH_STATE_INIT
366
         *
367
         * Transitions:
368
         * - session->dh_handshake_state = DH_STATE_INIT_SENT
369
         * then calls dh_handshake_server which triggers:
370
         * - session->dh_handshake_state = DH_STATE_NEWKEYS_SENT
371
         * */
372
373
0
        if (session->client) {
374
0
            rc = SSH_PACKET_DENIED;
375
0
            break;
376
0
        }
377
378
0
        if (session->session_state != SSH_SESSION_STATE_DH) {
379
0
            rc = SSH_PACKET_DENIED;
380
0
            break;
381
0
        }
382
383
        /* Only allowed if dh_handshake_state is in initial state */
384
0
        if (session->dh_handshake_state != DH_STATE_INIT) {
385
0
            rc = SSH_PACKET_DENIED;
386
0
            break;
387
0
        }
388
389
0
        rc = SSH_PACKET_ALLOWED;
390
0
        break;
391
0
    case SSH2_MSG_KEXDH_REPLY:                        // 31
392
      // SSH2_MSG_KEX_ECDH_REPLY:                     // 31
393
      // SSH2_MSG_KEX_HYBRID_REPLY:                   // 31
394
      // SSH2_MSG_KEX_DH_GEX_GROUP:                   // 31
395
396
        /* Client only */
397
398
        /*
399
         * States required:
400
         * - session_state == SSH_SESSION_STATE_DH
401
         * - dh_handshake_state == DH_STATE_INIT_SENT
402
         *   or dh_handshake_state == DH_STATE_REQUEST_SENT (dh-gex)
403
         *
404
         * Transitions:
405
         * - session->dh_handshake_state = DH_STATE_NEWKEYS_SENT
406
         * */
407
408
0
        if (session->server) {
409
0
            rc = SSH_PACKET_DENIED;
410
0
            break;
411
0
        }
412
413
0
        if (session->session_state != SSH_SESSION_STATE_DH) {
414
0
            rc = SSH_PACKET_DENIED;
415
0
            break;
416
0
        }
417
418
0
        if (session->dh_handshake_state != DH_STATE_INIT_SENT &&
419
0
            session->dh_handshake_state != DH_STATE_REQUEST_SENT) {
420
0
            rc = SSH_PACKET_DENIED;
421
0
            break;
422
0
        }
423
424
0
        rc = SSH_PACKET_ALLOWED;
425
0
        break;
426
0
    case SSH2_MSG_KEX_DH_GEX_INIT:                    // 32
427
      // SSH2_MSG_KEXGSS_COMPLETE:                    // 32
428
0
        if (ssh_kex_is_gss(session->next_crypto)) {
429
            /* SSH2_MSG_KEXGSS_COMPLETE */
430
            /* Client only */
431
432
            /*
433
             * States required:
434
             * - session_state == SSH_SESSION_STATE_DH
435
             * - dh_handshake_state == DH_STATE_INIT_SENT
436
             *
437
             * Transitions:
438
             * - session->dh_handshake_state = DH_STATE_INIT_SENT
439
             * then calls ssh_packet_client_gss_kex_reply which triggers:
440
             * - session->dh_handshake_state = DH_STATE_NEWKEYS_SENT
441
             * */
442
443
0
            if (!session->client) {
444
0
                rc = SSH_PACKET_DENIED;
445
0
                break;
446
0
            }
447
448
0
            if (session->session_state != SSH_SESSION_STATE_DH) {
449
0
                rc = SSH_PACKET_DENIED;
450
0
                break;
451
0
            }
452
453
0
            if (session->dh_handshake_state != DH_STATE_INIT_SENT) {
454
0
                rc = SSH_PACKET_DENIED;
455
0
                break;
456
0
            }
457
0
        } else {
458
            /* SSH2_MSG_KEX_DH_GEX_INIT */
459
            /* Server only */
460
461
            /*
462
             * States required:
463
             * - session_state == SSH_SESSION_STATE_DH
464
             * - dh_handshake_state == DH_STATE_GROUP_SENT
465
             *
466
             * Transitions:
467
             * - session->dh_handshake_state = DH_STATE_GROUP_SENT
468
             * then calls ssh_packet_server_dhgex_init which triggers:
469
             * - session->dh_handshake_state = DH_STATE_NEWKEYS_SENT
470
             * */
471
472
0
            if (session->client) {
473
0
                rc = SSH_PACKET_DENIED;
474
0
                break;
475
0
            }
476
477
0
            if (session->session_state != SSH_SESSION_STATE_DH) {
478
0
                rc = SSH_PACKET_DENIED;
479
0
                break;
480
0
            }
481
482
            /* Only allowed if dh_handshake_state is in initial state */
483
0
            if (session->dh_handshake_state != DH_STATE_GROUP_SENT) {
484
0
                rc = SSH_PACKET_DENIED;
485
0
                break;
486
0
            }
487
0
        }
488
0
        rc = SSH_PACKET_ALLOWED;
489
0
        break;
490
0
    case SSH2_MSG_KEX_DH_GEX_REPLY:                   // 33
491
492
        /* Client only */
493
494
        /*
495
         * States required:
496
         * - session_state == SSH_SESSION_STATE_DH
497
         * - dh_handshake_state == DH_STATE_INIT_SENT
498
         *
499
         * Transitions:
500
         * - session->dh_handshake_state = DH_STATE_NEWKEYS_SENT
501
         * */
502
503
0
        if (session->server) {
504
0
            rc = SSH_PACKET_DENIED;
505
0
            break;
506
0
        }
507
508
0
        if (session->session_state != SSH_SESSION_STATE_DH) {
509
0
            rc = SSH_PACKET_DENIED;
510
0
            break;
511
0
        }
512
513
0
        if (session->dh_handshake_state != DH_STATE_INIT_SENT) {
514
0
            rc = SSH_PACKET_DENIED;
515
0
            break;
516
0
        }
517
518
0
        rc = SSH_PACKET_ALLOWED;
519
0
        break;
520
0
    case SSH2_MSG_KEX_DH_GEX_REQUEST:                 // 34
521
522
        /* Server only */
523
524
        /*
525
         * States required:
526
         * - session_state == SSH_SESSION_STATE_DH
527
         * - dh_handshake_state == DH_STATE_INIT
528
         *
529
         * Transitions:
530
         * - session->dh_handshake_state = DH_STATE_INIT_SENT
531
         * then calls ssh_packet_server_dhgex_request which triggers:
532
         * - session->dh_handshake_state = DH_STATE_GROUP_SENT
533
         * */
534
535
0
        if (session->client) {
536
0
            rc = SSH_PACKET_DENIED;
537
0
            break;
538
0
        }
539
540
0
        if (session->session_state != SSH_SESSION_STATE_DH) {
541
0
            rc = SSH_PACKET_DENIED;
542
0
            break;
543
0
        }
544
545
        /* Only allowed if dh_handshake_state is in initial state */
546
0
        if (session->dh_handshake_state != DH_STATE_INIT) {
547
0
            rc = SSH_PACKET_DENIED;
548
0
            break;
549
0
        }
550
551
0
        rc = SSH_PACKET_ALLOWED;
552
0
        break;
553
0
    case SSH2_MSG_USERAUTH_REQUEST:                   // 50
554
        /* Server only */
555
556
        /*
557
         * States required:
558
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
559
         * - dh_handshake_state == DH_STATE_FINISHED
560
         *
561
         * Transitions:
562
         * - if authentication was successful:
563
         *   - session_state = SSH_SESSION_STATE_AUTHENTICATED
564
         * */
565
566
        /* If this is a client, reject the message */
567
0
        if (session->client) {
568
0
            rc = SSH_PACKET_DENIED;
569
0
            break;
570
0
        }
571
572
0
        if (session->dh_handshake_state != DH_STATE_FINISHED) {
573
0
            rc = SSH_PACKET_DENIED;
574
0
            break;
575
0
        }
576
577
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
578
0
            rc = SSH_PACKET_DENIED;
579
0
            break;
580
0
        }
581
582
0
        rc = SSH_PACKET_ALLOWED;
583
0
        break;
584
0
    case SSH2_MSG_USERAUTH_FAILURE:                   // 51
585
        /*
586
         * States required:
587
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
588
         * - dh_handshake_state == DH_STATE_FINISHED
589
         * - session->auth.state == SSH_AUTH_STATE_KBDINT_SENT
590
         *   or session->auth.state == SSH_AUTH_STATE_PUBKEY_OFFER_SENT
591
         *   or session->auth.state == SSH_AUTH_STATE_PUBKEY_AUTH_SENT
592
         *   or session->auth.state == SSH_AUTH_STATE_PASSWORD_AUTH_SENT
593
         *   or session->auth.state == SSH_AUTH_STATE_GSSAPI_MIC_SENT
594
         *
595
         * Transitions:
596
         * - if unpacking failed:
597
         *   - session->auth.state = SSH_AUTH_ERROR
598
         * - if failure was partial:
599
         *   - session->auth.state = SSH_AUTH_PARTIAL
600
         * - else:
601
         *   - session->auth.state = SSH_AUTH_STATE_FAILED
602
         * */
603
604
        /* If this is a server, reject the message */
605
0
        if (session->server) {
606
0
            rc = SSH_PACKET_DENIED;
607
0
            break;
608
0
        }
609
610
0
        if (session->dh_handshake_state != DH_STATE_FINISHED) {
611
0
            rc = SSH_PACKET_DENIED;
612
0
            break;
613
0
        }
614
615
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
616
0
            rc = SSH_PACKET_DENIED;
617
0
            break;
618
0
        }
619
620
0
        rc = SSH_PACKET_ALLOWED;
621
0
        break;
622
0
    case SSH2_MSG_USERAUTH_SUCCESS:                   // 52
623
        /*
624
         * States required:
625
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
626
         * - dh_handshake_state == DH_STATE_FINISHED
627
         * - session->auth.state == SSH_AUTH_STATE_KBDINT_SENT
628
         *   or session->auth.state == SSH_AUTH_STATE_PUBKEY_AUTH_SENT
629
         *   or session->auth.state == SSH_AUTH_STATE_PASSWORD_AUTH_SENT
630
         *   or session->auth.state == SSH_AUTH_STATE_GSSAPI_MIC_SENT
631
         *   or session->auth.state == SSH_AUTH_STATE_GSSAPI_KEYEX_MIC_SENT
632
         *   or session->auth.state == SSH_AUTH_STATE_AUTH_NONE_SENT
633
         *
634
         * Transitions:
635
         * - session->auth.state = SSH_AUTH_STATE_SUCCESS
636
         * - session->session_state = SSH_SESSION_STATE_AUTHENTICATED
637
         * - session->flags |= SSH_SESSION_FLAG_AUTHENTICATED
638
         * - sessions->auth.current_method = SSH_AUTH_METHOD_UNKNOWN
639
         * */
640
641
        /* If this is a server, reject the message */
642
0
        if (session->server) {
643
0
            rc = SSH_PACKET_DENIED;
644
0
            break;
645
0
        }
646
647
0
        if (session->dh_handshake_state != DH_STATE_FINISHED) {
648
0
            rc = SSH_PACKET_DENIED;
649
0
            break;
650
0
        }
651
652
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
653
0
            rc = SSH_PACKET_DENIED;
654
0
            break;
655
0
        }
656
657
0
        if ((session->auth.state != SSH_AUTH_STATE_KBDINT_SENT) &&
658
0
            (session->auth.state != SSH_AUTH_STATE_PUBKEY_AUTH_SENT) &&
659
0
            (session->auth.state != SSH_AUTH_STATE_PASSWORD_AUTH_SENT) &&
660
0
            (session->auth.state != SSH_AUTH_STATE_GSSAPI_MIC_SENT) &&
661
0
            (session->auth.state != SSH_AUTH_STATE_GSSAPI_KEYEX_MIC_SENT) &&
662
0
            (session->auth.state != SSH_AUTH_STATE_AUTH_NONE_SENT)) {
663
0
            rc = SSH_PACKET_DENIED;
664
0
            break;
665
0
        }
666
667
0
        rc = SSH_PACKET_ALLOWED;
668
0
        break;
669
0
    case SSH2_MSG_USERAUTH_BANNER:                    // 53
670
        /*
671
         * States required:
672
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
673
         *
674
         * Transitions:
675
         * - None
676
         * */
677
678
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
679
0
            rc = SSH_PACKET_DENIED;
680
0
            break;
681
0
        }
682
683
0
        rc = SSH_PACKET_ALLOWED;
684
0
        break;
685
0
    case SSH2_MSG_USERAUTH_PK_OK:                     // 60
686
      // SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ:          // 60
687
      // SSH2_MSG_USERAUTH_INFO_REQUEST:              // 60
688
      // SSH2_MSG_USERAUTH_GSSAPI_RESPONSE:           // 60
689
690
        /*
691
         * States required:
692
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
693
         * - session->auth.state == SSH_AUTH_STATE_KBDINT_SENT
694
         *   or
695
         *   session->auth.state == SSH_AUTH_STATE_GSSAPI_REQUEST_SENT
696
         *   or
697
         *   session->auth.state == SSH_AUTH_STATE_PUBKEY_OFFER_SENT
698
         *
699
         * Transitions:
700
         * Depending on the current state, the message is treated
701
         * differently:
702
         * - session->auth.state == SSH_AUTH_STATE_KBDINT_SENT
703
         *   - session->auth.state = SSH_AUTH_STATE_INFO
704
         * - session->auth.state == SSH_AUTH_STATE_GSSAPI_REQUEST_SENT
705
         *   - session->auth.state = SSH_AUTH_STATE_GSSAPI_TOKEN
706
         * - session->auth.state == SSH_AUTH_STATE_PUBKEY_OFFER_SENT
707
         *   - session->auth.state = SSH_AUTH_STATE_PK_OK
708
         * */
709
710
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
711
0
            rc = SSH_PACKET_DENIED;
712
0
            break;
713
0
        }
714
715
0
        if ((session->auth.state != SSH_AUTH_STATE_KBDINT_SENT) &&
716
0
            (session->auth.state != SSH_AUTH_STATE_PUBKEY_OFFER_SENT) &&
717
0
            (session->auth.state != SSH_AUTH_STATE_GSSAPI_REQUEST_SENT))
718
0
        {
719
0
            rc = SSH_PACKET_DENIED;
720
0
            break;
721
0
        }
722
723
0
        rc = SSH_PACKET_ALLOWED;
724
0
        break;
725
0
    case SSH2_MSG_USERAUTH_INFO_RESPONSE:             // 61
726
      // SSH2_MSG_USERAUTH_GSSAPI_TOKEN:              // 61
727
728
        /*
729
         * States required:
730
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
731
         * - session_state->auth.state == SSH_SESSION_STATE_GSSAPI_TOKEN
732
         *   or
733
         *   session_state->auth.state == SSH_SESSION_STATE_INFO
734
         *
735
         * Transitions:
736
         * - None
737
         * */
738
739
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
740
0
            rc = SSH_PACKET_DENIED;
741
0
            break;
742
0
        }
743
744
0
        if ((session->auth.state != SSH_AUTH_STATE_INFO) &&
745
0
            (session->auth.state != SSH_AUTH_STATE_GSSAPI_TOKEN))
746
0
        {
747
0
            rc = SSH_PACKET_DENIED;
748
0
            break;
749
0
        }
750
751
0
        rc = SSH_PACKET_ALLOWED;
752
0
        break;
753
0
    case SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE:  // 63
754
        /* Server only */
755
        /*
756
         * States required:
757
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
758
         * - session->gssapi->state == SSH_GSSAPI_STATE_RCV_MIC
759
         *
760
         * Transitions:
761
         * - None
762
         */
763
#ifdef WITH_GSSAPI
764
        if (session->client) {
765
            rc = SSH_PACKET_DENIED;
766
            break;
767
        }
768
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
769
            rc = SSH_PACKET_DENIED;
770
            break;
771
        }
772
        if (session->gssapi == NULL) {
773
            rc = SSH_PACKET_DENIED;
774
            break;
775
        }
776
        if (session->gssapi->state != SSH_GSSAPI_STATE_RCV_MIC) {
777
            rc = SSH_PACKET_DENIED;
778
            break;
779
        }
780
        rc = SSH_PACKET_ALLOWED;
781
        break;
782
#else
783
0
        rc = SSH_PACKET_DENIED;
784
0
        break;
785
0
#endif  /* WITH_GSSAPI */
786
0
    case SSH2_MSG_USERAUTH_GSSAPI_ERROR:              // 64
787
        /* Client only */
788
        /*
789
         * States required:
790
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
791
         *
792
         * Transitions:
793
         * - None
794
         */
795
#ifdef WITH_GSSAPI
796
        if (session->server) {
797
            rc = SSH_PACKET_DENIED;
798
            break;
799
        }
800
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
801
            rc = SSH_PACKET_DENIED;
802
            break;
803
        }
804
805
        rc = SSH_PACKET_ALLOWED;
806
        break;
807
#else
808
0
        rc = SSH_PACKET_DENIED;
809
0
        break;
810
0
#endif  /* WITH_GSSAPI */
811
0
    case SSH2_MSG_USERAUTH_GSSAPI_ERRTOK:             // 65
812
        /*
813
         * States required:
814
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
815
         *
816
         * Transitions:
817
         * - None
818
         */
819
#ifdef WITH_GSSAPI
820
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
821
            rc = SSH_PACKET_DENIED;
822
            break;
823
        }
824
825
        rc = SSH_PACKET_ALLOWED;
826
        break;
827
#else
828
0
        rc = SSH_PACKET_DENIED;
829
0
        break;
830
0
#endif  /* WITH_GSSAPI */
831
0
    case SSH2_MSG_USERAUTH_GSSAPI_MIC:                // 66
832
        /* Server only */
833
834
        /*
835
         * States required:
836
         * - session_state == SSH_SESSION_STATE_AUTHENTICATING
837
         * - session->gssapi->state == SSH_GSSAPI_STATE_RCV_MIC
838
         *
839
         * Transitions:
840
         * Depending on the result of the verification, the states are
841
         * changed:
842
         * - SSH_AUTH_SUCCESS:
843
         *   - session->session_state = SSH_SESSION_STATE_AUTHENTICATED
844
         *   - session->flags != SSH_SESSION_FLAG_AUTHENTICATED
845
         * - SSH_AUTH_PARTIAL:
846
         *   - None
847
         * - any other case:
848
         *   - None
849
         * */
850
#ifdef WITH_GSSAPI
851
        /* If this is a client, reject the message */
852
        if (session->client) {
853
            rc = SSH_PACKET_DENIED;
854
            break;
855
        }
856
857
        if (session->dh_handshake_state != DH_STATE_FINISHED) {
858
            rc = SSH_PACKET_DENIED;
859
            break;
860
        }
861
862
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATING) {
863
            rc = SSH_PACKET_DENIED;
864
            break;
865
        }
866
867
        rc = SSH_PACKET_ALLOWED;
868
        break;
869
#else
870
0
        rc = SSH_PACKET_DENIED;
871
0
        break;
872
0
#endif  /* WITH_GSSAPI */
873
0
    case SSH2_MSG_GLOBAL_REQUEST:                     // 80
874
        /*
875
         * States required:
876
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
877
         *
878
         * Transitions:
879
         * - None
880
         * */
881
882
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
883
0
            rc = SSH_PACKET_DENIED;
884
0
            break;
885
0
        }
886
887
0
        rc = SSH_PACKET_ALLOWED;
888
0
        break;
889
0
    case SSH2_MSG_REQUEST_SUCCESS:                    // 81
890
        /*
891
         * States required:
892
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
893
         *
894
         * Transitions:
895
         * - From channel->request_state == SSH_CHANNEL_REQ_STATE_PENDING
896
         * - To   channel->request_state = SSH_CHANNEL_REQ_STATE_ACCEPTED
897
         *
898
         * If not in a pending state, message is ignored in the callback handler.
899
         * */
900
901
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
902
0
            rc = SSH_PACKET_DENIED;
903
0
            break;
904
0
        }
905
906
0
        rc = SSH_PACKET_ALLOWED;
907
0
        break;
908
0
    case SSH2_MSG_REQUEST_FAILURE:                    // 82
909
        /*
910
         * States required:
911
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
912
         *
913
         * Transitions:
914
         * - From channel->request_state == SSH_CHANNEL_REQ_STATE_PENDING
915
         * - To   channel->request_state = SSH_CHANNEL_REQ_STATE_ACCEPTED
916
         *
917
         * If not in a pending state, message is ignored in the callback handler.
918
         * */
919
920
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
921
0
            rc = SSH_PACKET_DENIED;
922
0
            break;
923
0
        }
924
925
0
        rc = SSH_PACKET_ALLOWED;
926
0
        break;
927
0
    case SSH2_MSG_CHANNEL_OPEN:                       // 90
928
        /*
929
         * States required:
930
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
931
         *
932
         * Transitions:
933
         * - None
934
         * */
935
936
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
937
0
            rc = SSH_PACKET_DENIED;
938
0
            break;
939
0
        }
940
941
0
        rc = SSH_PACKET_ALLOWED;
942
0
        break;
943
0
    case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:          // 91
944
        /*
945
         * States required:
946
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
947
         *
948
         * Transitions:
949
         * - channel->state = SSH_CHANNEL_STATE_OPEN
950
         * - channel->flags &= ~SSH_CHANNEL_FLAG_NOT_BOUND
951
         * */
952
953
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
954
0
            rc = SSH_PACKET_DENIED;
955
0
            break;
956
0
        }
957
958
0
        rc = SSH_PACKET_ALLOWED;
959
0
        break;
960
0
    case SSH2_MSG_CHANNEL_OPEN_FAILURE:               // 92
961
        /*
962
         * States required:
963
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
964
         *
965
         * Transitions:
966
         * - channel->state = SSH_CHANNEL_STATE_OPEN_DENIED
967
         * */
968
969
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
970
0
            rc = SSH_PACKET_DENIED;
971
0
            break;
972
0
        }
973
974
0
        rc = SSH_PACKET_ALLOWED;
975
0
        break;
976
0
    case SSH2_MSG_CHANNEL_WINDOW_ADJUST:              // 93
977
        /*
978
         * States required:
979
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
980
         *
981
         * Transitions:
982
         * - None
983
         * */
984
985
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
986
0
            rc = SSH_PACKET_DENIED;
987
0
            break;
988
0
        }
989
990
0
        rc = SSH_PACKET_ALLOWED;
991
0
        break;
992
0
    case SSH2_MSG_CHANNEL_DATA:                       // 94
993
        /*
994
         * States required:
995
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
996
         *
997
         * Transitions:
998
         * - None
999
         * */
1000
1001
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
1002
0
            rc = SSH_PACKET_DENIED;
1003
0
            break;
1004
0
        }
1005
1006
0
        rc = SSH_PACKET_ALLOWED;
1007
0
        break;
1008
0
    case SSH2_MSG_CHANNEL_EXTENDED_DATA:              // 95
1009
        /*
1010
         * States required:
1011
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
1012
         *
1013
         * Transitions:
1014
         * - None
1015
         * */
1016
1017
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
1018
0
            rc = SSH_PACKET_DENIED;
1019
0
            break;
1020
0
        }
1021
1022
0
        rc = SSH_PACKET_ALLOWED;
1023
0
        break;
1024
0
    case SSH2_MSG_CHANNEL_EOF:                        // 96
1025
        /*
1026
         * States required:
1027
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
1028
         *
1029
         * Transitions:
1030
         * - None
1031
         * */
1032
1033
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
1034
0
            rc = SSH_PACKET_DENIED;
1035
0
            break;
1036
0
        }
1037
1038
0
        rc = SSH_PACKET_ALLOWED;
1039
0
        break;
1040
0
    case SSH2_MSG_CHANNEL_CLOSE:                      // 97
1041
        /*
1042
         * States required:
1043
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
1044
         *
1045
         * Transitions:
1046
         * - channel->state = SSH_CHANNEL_STATE_CLOSED
1047
         * - channel->flags |= SSH_CHANNEL_FLAG_CLOSED_REMOTE
1048
         * */
1049
1050
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
1051
0
            rc = SSH_PACKET_DENIED;
1052
0
            break;
1053
0
        }
1054
1055
0
        rc = SSH_PACKET_ALLOWED;
1056
0
        break;
1057
0
    case SSH2_MSG_CHANNEL_REQUEST:                    // 98
1058
        /*
1059
         * States required:
1060
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
1061
         *
1062
         * Transitions:
1063
         * - Depends on the request
1064
         * */
1065
1066
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
1067
0
            rc = SSH_PACKET_DENIED;
1068
0
            break;
1069
0
        }
1070
1071
0
        rc = SSH_PACKET_ALLOWED;
1072
0
        break;
1073
0
    case SSH2_MSG_CHANNEL_SUCCESS:                    // 99
1074
        /*
1075
         * States required:
1076
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
1077
         *
1078
         * Transitions:
1079
         * - From channel->request_state == SSH_CHANNEL_REQ_STATE_PENDING
1080
         * - To   channel->request_state = SSH_CHANNEL_REQ_STATE_ACCEPTED
1081
         *
1082
         * If not in a pending state, message is ignored in the callback handler.
1083
         * */
1084
1085
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
1086
0
            rc = SSH_PACKET_DENIED;
1087
0
            break;
1088
0
        }
1089
1090
0
        rc = SSH_PACKET_ALLOWED;
1091
0
        break;
1092
0
    case SSH2_MSG_CHANNEL_FAILURE:                    // 100
1093
        /*
1094
         * States required:
1095
         * - session_state == SSH_SESSION_STATE_AUTHENTICATED
1096
         *
1097
         * Transitions:
1098
         * - From channel->request_state == SSH_CHANNEL_REQ_STATE_PENDING
1099
         * - To   channel->request_state = SSH_CHANNEL_REQ_STATE_ACCEPTED
1100
         *
1101
         * If not in a pending state, message is ignored in the callback handler.
1102
         * */
1103
1104
0
        if (session->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
1105
0
            rc = SSH_PACKET_DENIED;
1106
0
            break;
1107
0
        }
1108
1109
0
        rc = SSH_PACKET_ALLOWED;
1110
0
        break;
1111
0
    case SSH2_MSG_PING: // 192
1112
0
    case SSH2_MSG_PONG: // 193
1113
        /*
1114
         * Transport-level ping/pong messages.
1115
         *
1116
         * Always allow PING and PONG messages through the filter.
1117
         * State checking (auth/rekey) is handled in the packet handler itself,
1118
         * following OpenSSH's approach of treating these as implicit
1119
         * transport-level messages (like IGNORE and DEBUG).
1120
         *
1121
         * States required:
1122
         * - None (always allowed)
1123
         *
1124
         * Transitions:
1125
         * - None
1126
         */
1127
0
        rc = SSH_PACKET_ALLOWED;
1128
0
        break;
1129
0
    default:
1130
        /* Unknown message, do not filter */
1131
0
        rc = SSH_PACKET_UNKNOWN;
1132
0
        goto end;
1133
0
    }
1134
1135
0
end:
1136
#ifdef DEBUG_PACKET
1137
    if (rc == SSH_PACKET_DENIED) {
1138
        SSH_LOG(SSH_LOG_PACKET, "REJECTED packet type %d: ",
1139
                session->in_packet.type);
1140
    }
1141
1142
    if (rc == SSH_PACKET_UNKNOWN) {
1143
        SSH_LOG(SSH_LOG_PACKET, "UNKNOWN packet type %d",
1144
                session->in_packet.type);
1145
    }
1146
#endif
1147
1148
0
    return rc;
1149
0
}
1150
1151
/* Returns current_crypto structure from the session.
1152
 * During key exchange (or rekey), after one of the sides
1153
 * sending NEWKEYS packet, this might return next_crypto for one
1154
 * of the directions that is ahead to send already queued packets
1155
 */
1156
struct ssh_crypto_struct *
1157
ssh_packet_get_current_crypto(ssh_session session,
1158
                              enum ssh_crypto_direction_e direction)
1159
0
{
1160
0
    struct ssh_crypto_struct *crypto = NULL;
1161
1162
0
    if (session == NULL) {
1163
0
        return NULL;
1164
0
    }
1165
1166
0
    if (session->current_crypto != NULL &&
1167
0
        session->current_crypto->used & direction) {
1168
0
        crypto = session->current_crypto;
1169
0
    } else if (session->next_crypto != NULL &&
1170
0
               session->next_crypto->used & direction) {
1171
0
        crypto = session->next_crypto;
1172
0
    } else {
1173
0
        return NULL;
1174
0
    }
1175
1176
0
    switch (direction) {
1177
0
    case SSH_DIRECTION_IN:
1178
0
        if (crypto->in_cipher != NULL) {
1179
0
            return crypto;
1180
0
        }
1181
0
        break;
1182
0
    case SSH_DIRECTION_OUT:
1183
0
        if (crypto->out_cipher != NULL) {
1184
0
            return crypto;
1185
0
        }
1186
0
        break;
1187
0
    case SSH_DIRECTION_BOTH:
1188
0
        if (crypto->in_cipher != NULL &&
1189
0
            crypto->out_cipher != NULL) {
1190
0
            return crypto;
1191
0
        }
1192
0
    }
1193
1194
0
    return NULL;
1195
0
}
1196
1197
0
#define MAX_PACKETS    (1UL<<31)
1198
1199
static bool ssh_packet_need_rekey(ssh_session session,
1200
                                  const uint32_t payloadsize)
1201
0
{
1202
0
    bool data_rekey_needed = false;
1203
0
    struct ssh_crypto_struct *crypto = NULL;
1204
0
    struct ssh_cipher_struct *out_cipher = NULL, *in_cipher = NULL;
1205
0
    uint32_t next_blocks;
1206
1207
    /* We can safely rekey only in authenticated state */
1208
0
    if ((session->flags & SSH_SESSION_FLAG_AUTHENTICATED) == 0) {
1209
0
        return false;
1210
0
    }
1211
1212
    /* Do not rekey if the rekey/key-exchange is in progress */
1213
0
    if (session->dh_handshake_state != DH_STATE_FINISHED) {
1214
0
        return false;
1215
0
    }
1216
1217
0
    crypto = ssh_packet_get_current_crypto(session, SSH_DIRECTION_BOTH);
1218
0
    if (crypto == NULL) {
1219
0
        return false;
1220
0
    }
1221
1222
0
    out_cipher = crypto->out_cipher;
1223
0
    in_cipher = crypto->in_cipher;
1224
1225
    /* Make sure we can send at least something for very small limits */
1226
0
    if ((out_cipher->packets == 0) && (in_cipher->packets == 0)) {
1227
0
        return false;
1228
0
    }
1229
1230
    /* Time based rekeying */
1231
0
    if (session->opts.rekey_time != 0 &&
1232
0
        ssh_timeout_elapsed(&session->last_rekey_time,
1233
0
                            session->opts.rekey_time)) {
1234
0
        return true;
1235
0
    }
1236
1237
    /* RFC4344, Section 3.1 Recommends rekeying after 2^31 packets in either
1238
     * direction to avoid possible information leakage through the MAC tag
1239
     */
1240
0
    if (out_cipher->packets > MAX_PACKETS ||
1241
0
        in_cipher->packets > MAX_PACKETS) {
1242
0
        return true;
1243
0
    }
1244
1245
    /* Data-based rekeying:
1246
     *  * For outgoing packets we can still delay them
1247
     *  * Incoming packets need to be processed anyway, but we can
1248
     *    signalize our intention to rekey
1249
     */
1250
0
    next_blocks = payloadsize / out_cipher->blocksize;
1251
0
    data_rekey_needed = (out_cipher->max_blocks != 0 &&
1252
0
                         out_cipher->blocks + next_blocks > out_cipher->max_blocks) ||
1253
0
                         (in_cipher->max_blocks != 0 &&
1254
0
                         in_cipher->blocks + next_blocks > in_cipher->max_blocks);
1255
1256
0
    SSH_LOG(SSH_LOG_PACKET,
1257
0
            "rekey: [data_rekey_needed=%d, out_blocks=%" PRIu64 ", in_blocks=%" PRIu64 "]",
1258
0
            data_rekey_needed,
1259
0
            out_cipher->blocks + next_blocks,
1260
0
            in_cipher->blocks + next_blocks);
1261
1262
0
    return data_rekey_needed;
1263
0
}
1264
1265
/* in nonblocking mode, socket_read will read as much as it can, and return */
1266
/* SSH_OK if it has read at least len bytes, otherwise, SSH_AGAIN. */
1267
/* in blocking mode, it will read at least len bytes and will block until it's ok. */
1268
1269
/** @internal
1270
 * @brief handles a data received event
1271
 *
1272
 * Processes up to one packet from the given buffer and calls the handlers
1273
 * for the different packet types or an exception handler callback. If the
1274
 * buffer does not contain a complete packet, nothing is processed and zero
1275
 * is returned. So typically this function needs to be called in a loop until
1276
 * it returns zero to properly handle multiple packets in the buffer.
1277
 *
1278
 * @param data        pointer to the data received
1279
 * @param receivedlen length of data received. It might not be enough for a complete packet
1280
 * @param user        pointer to current ssh_session
1281
 *
1282
 * @returns number of bytes read and processed. Zero means only partial packet
1283
 * received.
1284
 */
1285
size_t
1286
ssh_packet_socket_callback(const void *data, size_t receivedlen, void *user)
1287
0
{
1288
0
    ssh_session session = (ssh_session)user;
1289
0
    uint32_t blocksize = 8;
1290
0
    uint32_t lenfield_blocksize = 8;
1291
0
    size_t current_macsize = 0;
1292
0
    uint8_t *ptr = NULL;
1293
0
    ssize_t to_be_read;
1294
0
    int rc;
1295
0
    uint8_t *cleartext_packet = NULL;
1296
0
    uint8_t *packet_second_block = NULL;
1297
0
    uint8_t *mac = NULL;
1298
0
    size_t packet_remaining, packet_offset;
1299
0
    uint32_t packet_len, compsize, payloadsize;
1300
0
    uint8_t padding;
1301
0
    size_t processed = 0; /* number of bytes processed from the callback */
1302
0
    enum ssh_packet_filter_result_e filter_result;
1303
0
    struct ssh_crypto_struct *crypto = NULL;
1304
0
    bool etm = false;
1305
0
    uint32_t etm_packet_offset = 0;
1306
0
    bool ok;
1307
1308
0
    crypto = ssh_packet_get_current_crypto(session, SSH_DIRECTION_IN);
1309
0
    if (crypto != NULL) {
1310
0
        current_macsize = hmac_digest_len(crypto->in_hmac);
1311
0
        blocksize = crypto->in_cipher->blocksize;
1312
0
        lenfield_blocksize = crypto->in_cipher->lenfield_blocksize;
1313
0
        etm = crypto->in_hmac_etm;
1314
0
    }
1315
1316
0
    if (etm) {
1317
        /* In EtM mode packet size is unencrypted. This means
1318
         * we need to use this offset and set the block size
1319
         * that is part of the encrypted part to 0.
1320
         */
1321
0
        etm_packet_offset = sizeof(uint32_t);
1322
0
        lenfield_blocksize = 0;
1323
0
    } else if (lenfield_blocksize == 0) {
1324
0
        lenfield_blocksize = blocksize;
1325
0
    }
1326
0
    if (data == NULL) {
1327
0
        goto error;
1328
0
    }
1329
1330
0
    if (session->session_state == SSH_SESSION_STATE_ERROR) {
1331
0
        goto error;
1332
0
    }
1333
#ifdef DEBUG_PACKET
1334
    SSH_LOG(SSH_LOG_PACKET,
1335
            "rcv packet cb (len=%zu, state=%s)",
1336
            receivedlen,
1337
            session->packet_state == PACKET_STATE_INIT ?
1338
                "INIT" :
1339
                session->packet_state == PACKET_STATE_SIZEREAD ?
1340
                    "SIZE_READ" :
1341
                    session->packet_state == PACKET_STATE_PROCESSING ?
1342
                    "PROCESSING" : "unknown");
1343
#endif
1344
0
    switch (session->packet_state) {
1345
0
        case PACKET_STATE_INIT:
1346
0
            if (receivedlen < lenfield_blocksize + etm_packet_offset) {
1347
                /*
1348
                 * We didn't receive enough data to read either at least one
1349
                 * block size or the unencrypted length in EtM mode.
1350
                 */
1351
#ifdef DEBUG_PACKET
1352
                SSH_LOG(SSH_LOG_PACKET,
1353
                        "Waiting for more data (%zu < %u)",
1354
                        receivedlen,
1355
                        lenfield_blocksize);
1356
#endif
1357
0
                return 0;
1358
0
            }
1359
1360
0
            session->in_packet = (struct packet_struct) {
1361
0
                .type = 0,
1362
0
            };
1363
1364
0
            if (session->in_buffer) {
1365
0
                rc = ssh_buffer_reinit(session->in_buffer);
1366
0
                if (rc < 0) {
1367
0
                    goto error;
1368
0
                }
1369
0
            } else {
1370
0
                session->in_buffer = ssh_buffer_new();
1371
0
                if (session->in_buffer == NULL) {
1372
0
                    goto error;
1373
0
                }
1374
0
            }
1375
1376
0
            if (!etm) {
1377
0
                ptr = ssh_buffer_allocate(session->in_buffer,
1378
0
                                          lenfield_blocksize);
1379
0
                if (ptr == NULL) {
1380
0
                    goto error;
1381
0
                }
1382
0
                packet_len = ssh_packet_decrypt_len(session, ptr,
1383
0
                                                    (uint8_t *)data);
1384
0
                to_be_read = packet_len - lenfield_blocksize + sizeof(uint32_t);
1385
0
            } else {
1386
                /* Length is unencrypted in case of Encrypt-then-MAC */
1387
0
                packet_len = PULL_BE_U32(data, 0);
1388
0
                to_be_read = packet_len - etm_packet_offset;
1389
0
            }
1390
1391
0
            processed += lenfield_blocksize + etm_packet_offset;
1392
0
            if (packet_len > MAX_PACKET_LEN) {
1393
0
                ssh_set_error(session,
1394
0
                              SSH_FATAL,
1395
0
                              "read_packet(): Packet len too high(%" PRIu32 " %.4" PRIx32 ")",
1396
0
                              packet_len, packet_len);
1397
0
                goto error;
1398
0
            }
1399
0
            if (to_be_read < 0) {
1400
                /* remote sshd sends invalid sizes? */
1401
0
                ssh_set_error(session,
1402
0
                              SSH_FATAL,
1403
0
                              "Given numbers of bytes left to be read < 0 (%zd)!",
1404
0
                              to_be_read);
1405
0
                goto error;
1406
0
            }
1407
1408
0
            session->in_packet.len = packet_len;
1409
0
            session->packet_state = PACKET_STATE_SIZEREAD;
1410
0
            FALL_THROUGH;
1411
0
        case PACKET_STATE_SIZEREAD:
1412
0
            packet_len = session->in_packet.len;
1413
0
            packet_offset = processed = lenfield_blocksize + etm_packet_offset;
1414
0
            to_be_read = packet_len + sizeof(uint32_t) + current_macsize;
1415
            /* if to_be_read is zero, the whole packet was blocksize bytes. */
1416
0
            if (to_be_read != 0) {
1417
0
                if (receivedlen < (unsigned long)to_be_read) {
1418
                    /* give up, not enough data in buffer */
1419
0
                    SSH_LOG(SSH_LOG_PACKET,
1420
0
                            "packet: partial packet (read len) "
1421
0
                            "[len=%" PRIu32 ", receivedlen=%zu, to_be_read=%zd]",
1422
0
                            packet_len,
1423
0
                            receivedlen,
1424
0
                            to_be_read);
1425
0
                    return 0;
1426
0
                }
1427
1428
0
                packet_second_block = (uint8_t*)data + packet_offset;
1429
0
                processed = to_be_read - current_macsize;
1430
0
            }
1431
1432
0
            if (packet_offset - sizeof(uint32_t) > (size_t)packet_len) {
1433
0
                ssh_set_error(session,
1434
0
                              SSH_FATAL,
1435
0
                              "Invalid packet length %" PRIu32 ", required %zu",
1436
0
                              packet_len,
1437
0
                              packet_offset + sizeof(uint32_t));
1438
0
                goto error;
1439
0
            }
1440
1441
            /* remaining encrypted bytes from the packet, MAC not included */
1442
0
            packet_remaining = packet_len - (packet_offset - sizeof(uint32_t));
1443
0
            cleartext_packet = ssh_buffer_allocate(session->in_buffer,
1444
0
                                                   (uint32_t)packet_remaining);
1445
0
            if (cleartext_packet == NULL) {
1446
0
                goto error;
1447
0
            }
1448
1449
0
            if (packet_second_block != NULL) {
1450
0
                if (crypto != NULL) {
1451
0
                    mac = packet_second_block + packet_remaining;
1452
1453
0
                    if (crypto->in_hmac != SSH_HMAC_NONE && etm) {
1454
0
                        rc = ssh_packet_hmac_verify(session,
1455
0
                                                    data,
1456
0
                                                    processed,
1457
0
                                                    mac,
1458
0
                                                    crypto->in_hmac);
1459
0
                        if (rc < 0) {
1460
0
                            ssh_set_error(session, SSH_FATAL, "HMAC error");
1461
0
                            goto error;
1462
0
                        }
1463
0
                    }
1464
                    /*
1465
                     * Decrypt the packet. In case of EtM mode, the length is
1466
                     * already known as it's unencrypted. In the other case,
1467
                     * lenfield_blocksize bytes already have been decrypted.
1468
                     */
1469
0
                    if (packet_remaining > 0) {
1470
0
                        rc = ssh_packet_decrypt(session,
1471
0
                                                cleartext_packet,
1472
0
                                                (uint8_t *)data,
1473
0
                                                packet_offset,
1474
0
                                                processed - packet_offset);
1475
0
                        if (rc < 0) {
1476
0
                            ssh_set_error(session,
1477
0
                                          SSH_FATAL,
1478
0
                                          "Decryption error");
1479
0
                            goto error;
1480
0
                        }
1481
0
                    }
1482
1483
0
                    if (crypto->in_hmac != SSH_HMAC_NONE && !etm) {
1484
0
                        ssh_buffer in = session->in_buffer;
1485
0
                        rc = ssh_packet_hmac_verify(session,
1486
0
                                                    ssh_buffer_get(in),
1487
0
                                                    ssh_buffer_get_len(in),
1488
0
                                                    mac,
1489
0
                                                    crypto->in_hmac);
1490
0
                        if (rc < 0) {
1491
0
                            ssh_set_error(session, SSH_FATAL, "HMAC error");
1492
0
                            goto error;
1493
0
                        }
1494
0
                    }
1495
0
                    processed += current_macsize;
1496
0
                } else {
1497
0
                    memcpy(cleartext_packet,
1498
0
                           packet_second_block,
1499
0
                           packet_remaining);
1500
0
                }
1501
0
            }
1502
1503
0
#ifdef WITH_PCAP
1504
0
            if (session->pcap_ctx != NULL) {
1505
0
                ssh_pcap_context_write(session->pcap_ctx,
1506
0
                                       SSH_PCAP_DIR_IN,
1507
0
                                       ssh_buffer_get(session->in_buffer),
1508
0
                                       ssh_buffer_get_len(session->in_buffer),
1509
0
                                       ssh_buffer_get_len(session->in_buffer));
1510
0
            }
1511
0
#endif
1512
1513
0
            if (!etm) {
1514
                /* skip the size field which has been processed before */
1515
0
                ssh_buffer_pass_bytes(session->in_buffer, sizeof(uint32_t));
1516
0
            }
1517
1518
0
            rc = ssh_buffer_get_u8(session->in_buffer, &padding);
1519
0
            if (rc == 0) {
1520
0
                ssh_set_error(session,
1521
0
                              SSH_FATAL,
1522
0
                              "Packet too short to read padding");
1523
0
                goto error;
1524
0
            }
1525
1526
0
            if (padding > ssh_buffer_get_len(session->in_buffer)) {
1527
0
                ssh_set_error(session,
1528
0
                              SSH_FATAL,
1529
0
                              "Invalid padding: %d (%" PRIu32 " left)",
1530
0
                              padding,
1531
0
                              ssh_buffer_get_len(session->in_buffer));
1532
0
                goto error;
1533
0
            }
1534
0
            ssh_buffer_pass_bytes_end(session->in_buffer, padding);
1535
0
            compsize = ssh_buffer_get_len(session->in_buffer);
1536
1537
0
            if (crypto && crypto->do_compress_in &&
1538
0
                ssh_buffer_get_len(session->in_buffer) > 0) {
1539
0
                rc = decompress_buffer(session, session->in_buffer,
1540
0
                                       MAX_PACKET_LEN);
1541
0
                if (rc < 0) {
1542
0
                    goto error;
1543
0
                }
1544
0
            }
1545
0
            payloadsize = ssh_buffer_get_len(session->in_buffer);
1546
0
            if (session->recv_seq == UINT32_MAX) {
1547
                /* Overflowing sequence numbers is always fishy */
1548
0
                if (crypto == NULL) {
1549
                    /* don't allow sequence number overflow when unencrypted */
1550
0
                    ssh_set_error(session,
1551
0
                                  SSH_FATAL,
1552
0
                                  "Incoming sequence number overflow");
1553
0
                    goto error;
1554
0
                } else {
1555
0
                    SSH_LOG(SSH_LOG_WARNING,
1556
0
                            "Incoming sequence number overflow");
1557
0
                }
1558
0
            }
1559
0
            session->recv_seq++;
1560
0
            if (crypto != NULL) {
1561
0
                struct ssh_cipher_struct *cipher = NULL;
1562
1563
0
                cipher = crypto->in_cipher;
1564
0
                cipher->packets++;
1565
0
                cipher->blocks += payloadsize / cipher->blocksize;
1566
0
            }
1567
0
            if (session->raw_counter != NULL) {
1568
0
                session->raw_counter->in_bytes += payloadsize;
1569
0
                session->raw_counter->in_packets++;
1570
0
            }
1571
1572
            /*
1573
             * We don't want to rewrite a new packet while still executing the
1574
             * packet callbacks
1575
             */
1576
0
            session->packet_state = PACKET_STATE_PROCESSING;
1577
0
            ssh_packet_parse_type(session);
1578
0
            SSH_LOG(SSH_LOG_PACKET,
1579
0
                    "packet: read type %hhd [len=%" PRIu32 ",padding=%hhd,"
1580
0
                    "comp=%" PRIu32 ",payload=%" PRIu32 "]",
1581
0
                    session->in_packet.type, packet_len, padding, compsize,
1582
0
                    payloadsize);
1583
0
            if (crypto == NULL) {
1584
                /* In strict kex, only a few packets are allowed. Taint the session
1585
                 * if we received packets that are normally allowed but to be
1586
                 * refused if we are in strict kex when KEX is over.
1587
                 */
1588
0
                uint8_t type = session->in_packet.type;
1589
1590
0
                if (type != SSH2_MSG_KEXINIT && type != SSH2_MSG_NEWKEYS &&
1591
0
                    (type < SSH2_MSG_KEXDH_INIT ||
1592
0
                     type > SSH2_MSG_KEX_DH_GEX_REQUEST)) {
1593
0
                    session->flags |= SSH_SESSION_FLAG_KEX_TAINTED;
1594
0
                }
1595
0
            }
1596
            /* Check if the packet is expected */
1597
0
            filter_result = ssh_packet_incoming_filter(session);
1598
1599
0
            switch (filter_result) {
1600
0
            case SSH_PACKET_ALLOWED:
1601
                /* Execute callbacks */
1602
0
                ssh_packet_process(session, session->in_packet.type);
1603
0
                break;
1604
0
            case SSH_PACKET_DENIED:
1605
0
                ssh_set_error(session,
1606
0
                              SSH_FATAL,
1607
0
                              "Packet filter: rejected packet (type %d)",
1608
0
                              session->in_packet.type);
1609
0
                goto error;
1610
0
            case SSH_PACKET_UNKNOWN:
1611
0
                if (crypto == NULL) {
1612
0
                    session->flags |= SSH_SESSION_FLAG_KEX_TAINTED;
1613
0
                }
1614
0
                ssh_packet_send_unimplemented(session, session->recv_seq - 1);
1615
0
                break;
1616
0
            }
1617
1618
0
            session->packet_state = PACKET_STATE_INIT;
1619
0
            if (processed < receivedlen) {
1620
0
                SSH_LOG(SSH_LOG_PACKET,
1621
0
                        "packet: %zu bytes still remaining in socket buffer "
1622
0
                        "after processing",
1623
0
                        receivedlen-processed);
1624
0
            }
1625
1626
0
            ok = ssh_packet_need_rekey(session, 0);
1627
0
            if (ok) {
1628
0
                SSH_LOG(SSH_LOG_PACKET, "Incoming packet triggered rekey");
1629
0
                rc = ssh_send_rekex(session);
1630
0
                if (rc != SSH_OK) {
1631
0
                    SSH_LOG(SSH_LOG_PACKET, "Rekey failed: rc = %d", rc);
1632
0
                    goto error;
1633
0
                }
1634
0
            }
1635
1636
0
            return processed;
1637
0
        case PACKET_STATE_PROCESSING:
1638
0
            SSH_LOG(SSH_LOG_PACKET, "Nested packet processing. Delaying.");
1639
0
            return 0;
1640
0
    }
1641
1642
0
    ssh_set_error(session,
1643
0
                  SSH_FATAL,
1644
0
                  "Invalid state into packet_read2(): %d",
1645
0
                  session->packet_state);
1646
1647
0
error:
1648
0
    session->session_state = SSH_SESSION_STATE_ERROR;
1649
0
    SSH_LOG(SSH_LOG_PACKET, "Packet: processed %zu bytes", processed);
1650
0
    return processed;
1651
0
}
1652
1653
static void ssh_packet_socket_controlflow_callback(int code, void *userdata)
1654
0
{
1655
0
    ssh_session session = userdata;
1656
0
    struct ssh_iterator *it = NULL;
1657
0
    ssh_channel channel = NULL;
1658
1659
0
    if (code == SSH_SOCKET_FLOW_WRITEWONTBLOCK) {
1660
0
        SSH_LOG(SSH_LOG_TRACE, "sending channel_write_wontblock callback");
1661
1662
        /* the out pipe is empty so we can forward this to channels */
1663
0
        it = ssh_list_get_iterator(session->channels);
1664
0
        while (it != NULL) {
1665
0
            channel = ssh_iterator_value(ssh_channel, it);
1666
0
            ssh_callbacks_execute_list(channel->callbacks,
1667
0
                                       ssh_channel_callbacks,
1668
0
                                       channel_write_wontblock_function,
1669
0
                                       session,
1670
0
                                       channel,
1671
0
                                       channel->remote_window);
1672
0
            it = it->next;
1673
0
        }
1674
0
    }
1675
0
}
1676
1677
void ssh_packet_register_socket_callback(ssh_session session, ssh_socket s)
1678
0
{
1679
0
    struct ssh_socket_callbacks_struct *callbacks = &session->socket_callbacks;
1680
1681
0
    callbacks->data = ssh_packet_socket_callback;
1682
0
    callbacks->connected = NULL;
1683
0
    callbacks->controlflow = ssh_packet_socket_controlflow_callback;
1684
0
    callbacks->userdata = session;
1685
0
    ssh_socket_set_callbacks(s, callbacks);
1686
0
}
1687
1688
/** @internal
1689
 * @brief sets the callbacks for the packet layer
1690
 */
1691
void
1692
ssh_packet_set_callbacks(ssh_session session, ssh_packet_callbacks callbacks)
1693
0
{
1694
0
    if (session->packet_callbacks == NULL) {
1695
0
        session->packet_callbacks = ssh_list_new();
1696
0
        if (session->packet_callbacks == NULL) {
1697
0
            ssh_set_error_oom(session);
1698
0
            return;
1699
0
        }
1700
0
    }
1701
0
    ssh_list_append(session->packet_callbacks, callbacks);
1702
0
}
1703
1704
/** @internal
1705
 * @brief remove the callbacks from the packet layer
1706
 */
1707
void
1708
ssh_packet_remove_callbacks(ssh_session session, ssh_packet_callbacks callbacks)
1709
0
{
1710
0
    struct ssh_iterator *it = NULL;
1711
1712
0
    it = ssh_list_find(session->packet_callbacks, callbacks);
1713
0
    if (it != NULL) {
1714
0
        ssh_list_remove(session->packet_callbacks, it);
1715
0
    }
1716
0
}
1717
1718
/** @internal
1719
 * @brief sets the default packet handlers
1720
 */
1721
void ssh_packet_set_default_callbacks(ssh_session session)
1722
0
{
1723
0
    struct ssh_packet_callbacks_struct *c = &session->default_packet_callbacks;
1724
1725
0
    c->start = 1;
1726
0
    c->n_callbacks = sizeof(default_packet_handlers) / sizeof(ssh_packet_callback);
1727
0
    c->user = session;
1728
0
    c->callbacks = default_packet_handlers;
1729
0
    ssh_packet_set_callbacks(session, c);
1730
0
}
1731
1732
/** @internal
1733
 * @brief dispatch the call of packet handlers callbacks for a received packet
1734
 * @param type type of packet
1735
 */
1736
void ssh_packet_process(ssh_session session, uint8_t type)
1737
0
{
1738
0
    struct ssh_iterator *i = NULL;
1739
0
    int rc = SSH_PACKET_NOT_USED;
1740
0
    ssh_packet_callbacks cb;
1741
1742
0
    SSH_LOG(SSH_LOG_PACKET, "Dispatching handler for packet type %d", type);
1743
0
    if (session->packet_callbacks == NULL) {
1744
0
        SSH_LOG(SSH_LOG_RARE, "Packet callback is not initialized !");
1745
0
        return;
1746
0
    }
1747
1748
0
    i = ssh_list_get_iterator(session->packet_callbacks);
1749
0
    while (i != NULL) {
1750
0
        cb = ssh_iterator_value(ssh_packet_callbacks, i);
1751
0
        i = i->next;
1752
1753
0
        if (!cb) {
1754
0
            continue;
1755
0
        }
1756
1757
0
        if (cb->start > type) {
1758
0
            continue;
1759
0
        }
1760
1761
0
        if (cb->start + cb->n_callbacks <= type) {
1762
0
            continue;
1763
0
        }
1764
1765
0
        if (cb->callbacks[type - cb->start] == NULL) {
1766
0
            continue;
1767
0
        }
1768
1769
0
        rc = cb->callbacks[type - cb->start](session, type, session->in_buffer,
1770
0
                                             cb->user);
1771
0
        if (rc == SSH_PACKET_USED) {
1772
0
            break;
1773
0
        }
1774
0
    }
1775
1776
0
    if (rc == SSH_PACKET_NOT_USED) {
1777
0
        SSH_LOG(SSH_LOG_RARE, "Couldn't do anything with packet type %d", type);
1778
0
        rc = ssh_packet_send_unimplemented(session, session->recv_seq - 1);
1779
0
        if (rc != SSH_OK) {
1780
0
            SSH_LOG(SSH_LOG_RARE, "Failed to send unimplemented: %s",
1781
0
                    ssh_get_error(session));
1782
0
        }
1783
0
        if (session->current_crypto == NULL) {
1784
0
            session->flags |= SSH_SESSION_FLAG_KEX_TAINTED;
1785
0
        }
1786
0
    }
1787
0
}
1788
1789
/** @internal
1790
 * @brief sends a SSH_MSG_NEWKEYS when enabling the new negotiated ciphers
1791
 * @param session the SSH session
1792
 * @return SSH_ERROR on error, else SSH_OK
1793
 */
1794
int ssh_packet_send_newkeys(ssh_session session)
1795
0
{
1796
0
    int rc;
1797
1798
    /* Send the MSG_NEWKEYS */
1799
0
    rc = ssh_buffer_add_u8(session->out_buffer, SSH2_MSG_NEWKEYS);
1800
0
    if (rc < 0) {
1801
0
        return rc;
1802
0
    }
1803
1804
0
    rc = ssh_packet_send(session);
1805
0
    if (rc == SSH_ERROR) {
1806
0
        return rc;
1807
0
    }
1808
0
    SSH_LOG(SSH_LOG_DEBUG, "SSH_MSG_NEWKEYS sent");
1809
0
    return rc;
1810
0
}
1811
1812
/** @internal
1813
 * @brief sends a SSH_MSG_UNIMPLEMENTED answer to an unhandled packet
1814
 * @param session the SSH session
1815
 * @param seqnum the sequence number of the unknown packet
1816
 * @return SSH_ERROR on error, else SSH_OK
1817
 */
1818
0
int ssh_packet_send_unimplemented(ssh_session session, uint32_t seqnum){
1819
0
    int rc;
1820
1821
0
    rc = ssh_buffer_pack(session->out_buffer,
1822
0
                         "bd",
1823
0
                         SSH2_MSG_UNIMPLEMENTED,
1824
0
                         seqnum);
1825
0
    if (rc != SSH_OK) {
1826
0
        ssh_set_error_oom(session);
1827
0
        return SSH_ERROR;
1828
0
    }
1829
0
    rc = ssh_packet_send(session);
1830
1831
0
    return rc;
1832
0
}
1833
1834
/** @internal
1835
 * @brief handles a SSH_MSG_UNIMPLEMENTED packet
1836
 */
1837
0
SSH_PACKET_CALLBACK(ssh_packet_unimplemented){
1838
0
    uint32_t seq;
1839
0
    int rc;
1840
1841
0
    (void)session; /* unused */
1842
0
    (void)type;
1843
0
    (void)user;
1844
1845
0
    rc = ssh_buffer_unpack(packet, "d", &seq);
1846
0
    if (rc != SSH_OK) {
1847
0
        SSH_LOG(SSH_LOG_TRACE,
1848
0
                "Could not unpack SSH_MSG_UNIMPLEMENTED packet");
1849
0
        return SSH_PACKET_USED;
1850
0
    }
1851
1852
0
    SSH_LOG(SSH_LOG_RARE,
1853
0
            "Received SSH_MSG_UNIMPLEMENTED (sequence number %" PRIu32 ")",seq);
1854
1855
0
    return SSH_PACKET_USED;
1856
0
}
1857
1858
/** @internal
1859
 * @parse the "Type" header field of a packet and updates the session
1860
 */
1861
int ssh_packet_parse_type(struct ssh_session_struct *session)
1862
0
{
1863
0
    session->in_packet = (struct packet_struct) {
1864
0
        .type = 0,
1865
0
    };
1866
1867
0
    if (session->in_buffer == NULL) {
1868
0
        return SSH_ERROR;
1869
0
    }
1870
1871
0
    if (ssh_buffer_get_u8(session->in_buffer, &session->in_packet.type) == 0) {
1872
0
        ssh_set_error(session, SSH_FATAL, "Packet too short to read type");
1873
0
        return SSH_ERROR;
1874
0
    }
1875
1876
0
    session->in_packet.valid = 1;
1877
1878
0
    return SSH_OK;
1879
0
}
1880
1881
/*
1882
 * This function places the outgoing packet buffer into an outgoing
1883
 * socket buffer
1884
 */
1885
0
static int ssh_packet_write(ssh_session session) {
1886
0
  int rc = SSH_ERROR;
1887
1888
0
  rc=ssh_socket_write(session->socket,
1889
0
      ssh_buffer_get(session->out_buffer),
1890
0
      ssh_buffer_get_len(session->out_buffer));
1891
1892
0
  return rc;
1893
0
}
1894
1895
static int packet_send2(ssh_session session)
1896
0
{
1897
0
    unsigned int blocksize = 8;
1898
0
    unsigned int lenfield_blocksize = 0;
1899
0
    enum ssh_hmac_e hmac_type;
1900
0
    uint32_t currentlen = ssh_buffer_get_len(session->out_buffer);
1901
0
    struct ssh_crypto_struct *crypto = NULL;
1902
0
    unsigned char *hmac = NULL;
1903
0
    uint8_t padding_data[32] = { 0 };
1904
0
    uint8_t padding_size;
1905
0
    uint32_t finallen, payloadsize, compsize;
1906
0
    uint8_t header[5] = {0};
1907
0
    uint8_t type, *payload;
1908
0
    int rc = SSH_ERROR;
1909
0
    bool etm = false;
1910
0
    int etm_packet_offset = 0;
1911
1912
0
    crypto = ssh_packet_get_current_crypto(session, SSH_DIRECTION_OUT);
1913
0
    if (crypto) {
1914
0
        blocksize = crypto->out_cipher->blocksize;
1915
0
        lenfield_blocksize = crypto->out_cipher->lenfield_blocksize;
1916
0
        hmac_type = crypto->out_hmac;
1917
0
        etm = crypto->out_hmac_etm;
1918
0
    } else {
1919
0
        hmac_type = session->next_crypto->out_hmac;
1920
0
    }
1921
1922
0
    payload = (uint8_t *)ssh_buffer_get(session->out_buffer);
1923
0
    type = payload[0]; /* type is the first byte of the packet now */
1924
1925
0
    payloadsize = currentlen;
1926
0
    if (etm) {
1927
0
        etm_packet_offset = sizeof(uint32_t);
1928
0
        lenfield_blocksize = 0;
1929
0
    }
1930
1931
0
    if (crypto != NULL && crypto->do_compress_out &&
1932
0
        ssh_buffer_get_len(session->out_buffer) > 0) {
1933
0
        rc = compress_buffer(session,session->out_buffer);
1934
0
        if (rc < 0) {
1935
0
            goto error;
1936
0
        }
1937
0
        currentlen = ssh_buffer_get_len(session->out_buffer);
1938
0
    }
1939
0
    compsize = currentlen;
1940
    /* compressed payload + packet len (4) + padding_size len (1) */
1941
    /* totallen - lenfield_blocksize - etm_packet_offset must be equal to 0 (mod blocksize) */
1942
0
    padding_size = (blocksize - ((blocksize - lenfield_blocksize - etm_packet_offset + currentlen + 5) % blocksize));
1943
0
    if (padding_size < 4) {
1944
0
        padding_size += blocksize;
1945
0
    }
1946
1947
0
    if (crypto != NULL) {
1948
0
        int ok;
1949
1950
0
        ok = ssh_get_random(padding_data, padding_size, 0);
1951
0
        if (!ok) {
1952
0
            ssh_set_error(session, SSH_FATAL, "PRNG error");
1953
0
            goto error;
1954
0
        }
1955
0
    }
1956
1957
0
    finallen = currentlen - etm_packet_offset + padding_size + 1;
1958
1959
0
    PUSH_BE_U32(header, 0, finallen);
1960
0
    PUSH_BE_U8(header, 4, padding_size);
1961
1962
0
    rc = ssh_buffer_prepend_data(session->out_buffer,
1963
0
                                 header,
1964
0
                                 sizeof(header));
1965
0
    if (rc < 0) {
1966
0
        goto error;
1967
0
    }
1968
1969
0
    rc = ssh_buffer_add_data(session->out_buffer, padding_data, padding_size);
1970
0
    if (rc < 0) {
1971
0
        goto error;
1972
0
    }
1973
1974
0
#ifdef WITH_PCAP
1975
0
    if (session->pcap_ctx != NULL) {
1976
0
        ssh_pcap_context_write(session->pcap_ctx,
1977
0
                               SSH_PCAP_DIR_OUT,
1978
0
                               ssh_buffer_get(session->out_buffer),
1979
0
                               ssh_buffer_get_len(session->out_buffer),
1980
0
                               ssh_buffer_get_len(session->out_buffer));
1981
0
    }
1982
0
#endif
1983
1984
0
    hmac = ssh_packet_encrypt(session,
1985
0
                              ssh_buffer_get(session->out_buffer),
1986
0
                              ssh_buffer_get_len(session->out_buffer));
1987
    /* XXX This returns null before switching on crypto, with none MAC
1988
     * and on various errors.
1989
     * We should distinguish between these cases to avoid hiding errors. */
1990
0
    if (hmac != NULL) {
1991
0
        rc = ssh_buffer_add_data(session->out_buffer,
1992
0
                                 hmac,
1993
0
                                 (uint32_t)hmac_digest_len(hmac_type));
1994
0
        if (rc < 0) {
1995
0
            goto error;
1996
0
        }
1997
0
    }
1998
1999
0
    rc = ssh_packet_write(session);
2000
0
    if (rc == SSH_ERROR) {
2001
0
        goto error;
2002
0
    }
2003
0
    session->send_seq++;
2004
0
    if (crypto != NULL) {
2005
0
        struct ssh_cipher_struct *cipher = NULL;
2006
2007
0
        cipher = crypto->out_cipher;
2008
0
        cipher->packets++;
2009
0
        cipher->blocks += payloadsize / cipher->blocksize;
2010
0
    }
2011
0
    if (session->raw_counter != NULL) {
2012
0
        session->raw_counter->out_bytes += payloadsize;
2013
0
        session->raw_counter->out_packets++;
2014
0
    }
2015
2016
0
    SSH_LOG(SSH_LOG_PACKET,
2017
0
            "packet: wrote [type=%u, len=%" PRIu32 ", padding_size=%hhd, comp=%" PRIu32 ", "
2018
0
            "payload=%" PRIu32 "]",
2019
0
            type,
2020
0
            finallen,
2021
0
            padding_size,
2022
0
            compsize,
2023
0
            payloadsize);
2024
2025
0
    rc = ssh_buffer_reinit(session->out_buffer);
2026
0
    if (rc < 0) {
2027
0
        rc = SSH_ERROR;
2028
0
        goto error;
2029
0
    }
2030
2031
    /* We sent the NEWKEYS so any further packet needs to be encrypted
2032
     * with the new keys. We can not switch both directions (need to decrypt
2033
     * peer NEWKEYS) and we do not want to wait for the peer NEWKEYS
2034
     * too, so we will switch only the OUT direction now.
2035
     */
2036
0
    if (type == SSH2_MSG_NEWKEYS) {
2037
0
        rc = ssh_packet_set_newkeys(session, SSH_DIRECTION_OUT);
2038
0
    }
2039
0
error:
2040
0
    return rc; /* SSH_OK, AGAIN or ERROR */
2041
0
}
2042
2043
static bool
2044
ssh_packet_is_kex(unsigned char type)
2045
0
{
2046
0
    return type >= SSH2_MSG_DISCONNECT &&
2047
0
           type <= SSH2_MSG_KEX_DH_GEX_REQUEST &&
2048
0
           type != SSH2_MSG_SERVICE_REQUEST &&
2049
0
           type != SSH2_MSG_SERVICE_ACCEPT &&
2050
0
           type != SSH2_MSG_IGNORE &&
2051
0
           type != SSH2_MSG_EXT_INFO;
2052
0
}
2053
2054
static bool
2055
ssh_packet_in_rekey(ssh_session session)
2056
0
{
2057
    /* We know we are rekeying if we are authenticated and the DH
2058
     * status is not finished, but we only queue packets until we've
2059
     * sent our NEWKEYS.
2060
     */
2061
0
    return (session->flags & SSH_SESSION_FLAG_AUTHENTICATED) &&
2062
0
           (session->dh_handshake_state != DH_STATE_FINISHED) &&
2063
0
           (session->dh_handshake_state != DH_STATE_NEWKEYS_SENT);
2064
0
}
2065
2066
int ssh_packet_send(ssh_session session)
2067
0
{
2068
0
    uint32_t payloadsize;
2069
0
    uint8_t type, *payload;
2070
0
    bool need_rekey, in_rekey;
2071
0
    int rc;
2072
2073
0
    if (session->socket == NULL || !ssh_socket_is_open(session->socket)) {
2074
0
        ssh_buffer_reinit(session->out_buffer);
2075
0
        return SSH_ERROR;
2076
0
    }
2077
2078
0
    payloadsize = ssh_buffer_get_len(session->out_buffer);
2079
0
    if (payloadsize < 1) {
2080
0
        return SSH_ERROR;
2081
0
    }
2082
2083
0
    payload = (uint8_t *)ssh_buffer_get(session->out_buffer);
2084
0
    type = payload[0]; /* type is the first byte of the packet now */
2085
0
    need_rekey = ssh_packet_need_rekey(session, payloadsize);
2086
0
    in_rekey = ssh_packet_in_rekey(session);
2087
2088
    /* The rekey is triggered here. After that, only the key exchange
2089
     * packets can be sent, until we send our NEWKEYS.
2090
     */
2091
0
    if (need_rekey || (in_rekey && !ssh_packet_is_kex(type))) {
2092
0
        if (need_rekey) {
2093
0
            SSH_LOG(SSH_LOG_PACKET, "Outgoing packet triggered rekey");
2094
0
        }
2095
        /* Queue the current packet -- we will send it after the rekey */
2096
0
        SSH_LOG(SSH_LOG_PACKET, "Queuing packet type %d", type);
2097
0
        rc = ssh_list_append(session->out_queue, session->out_buffer);
2098
0
        if (rc != SSH_OK) {
2099
0
            return SSH_ERROR;
2100
0
        }
2101
0
        session->out_buffer = ssh_buffer_new();
2102
0
        if (session->out_buffer == NULL) {
2103
0
            ssh_set_error_oom(session);
2104
0
            return SSH_ERROR;
2105
0
        }
2106
2107
0
        if (need_rekey) {
2108
            /* Send the KEXINIT packet instead.
2109
             * This recursively calls the packet_send(), but it should
2110
             * not get into rekeying again.
2111
             * After that we need to handle the key exchange responses
2112
             * up to the point where we can send the rest of the queue.
2113
             */
2114
0
            return ssh_send_rekex(session);
2115
0
        }
2116
0
        return SSH_OK;
2117
0
    }
2118
2119
    /* Send the packet normally */
2120
0
    rc = packet_send2(session);
2121
2122
    /* We finished the key exchange so we can try to send our queue now */
2123
0
    if (rc == SSH_OK && type == SSH2_MSG_NEWKEYS) {
2124
0
        struct ssh_iterator *it = NULL;
2125
2126
0
        if (session->flags & SSH_SESSION_FLAG_KEX_STRICT) {
2127
            /* reset packet sequence number when running in strict kex mode */
2128
0
            session->send_seq = 0;
2129
0
        }
2130
0
        for (it = ssh_list_get_iterator(session->out_queue);
2131
0
             it != NULL;
2132
0
             it = ssh_list_get_iterator(session->out_queue)) {
2133
0
            struct ssh_buffer_struct *next_buffer = NULL;
2134
2135
            /* Peek only -- do not remove from queue yet */
2136
0
            next_buffer = ssh_iterator_value(struct ssh_buffer_struct *, it);
2137
0
            payloadsize = ssh_buffer_get_len(next_buffer);
2138
0
            if (ssh_packet_need_rekey(session, payloadsize)) {
2139
                /* Sigh ... we still can not send this packet. Repeat. */
2140
0
                SSH_LOG(SSH_LOG_PACKET, "Queued packet triggered rekey");
2141
0
                return ssh_send_rekex(session);
2142
0
            }
2143
0
            SSH_BUFFER_FREE(session->out_buffer);
2144
0
            session->out_buffer = ssh_list_pop_head(struct ssh_buffer_struct *,
2145
0
                                                    session->out_queue);
2146
0
            payload = (uint8_t *)ssh_buffer_get(session->out_buffer);
2147
0
            type = payload[0];
2148
0
            SSH_LOG(SSH_LOG_PACKET, "Dequeue packet type %d", type);
2149
0
            rc = packet_send2(session);
2150
0
            if (rc != SSH_OK) {
2151
0
                return rc;
2152
0
            }
2153
0
        }
2154
0
    }
2155
2156
0
    return rc;
2157
0
}
2158
2159
/**
2160
 * @brief Check whether the ping@openssh.com extension is available.
2161
 *
2162
 * This function allows an application to determine whether ping can be sent
2163
 * to the current peer.
2164
 *
2165
 * @note Use ssh_send_ping() only after verifying support with this function.
2166
 *
2167
 * @param[in]  session  The SSH session handle.
2168
 *
2169
 * @return true if the extension was negotiated, false otherwise or if
2170
 *         session is NULL.
2171
 *
2172
 * @see ssh_send_ping()
2173
 */
2174
bool ssh_is_ping_supported(ssh_session session)
2175
0
{
2176
0
    if (session == NULL) {
2177
0
        return false;
2178
0
    }
2179
0
    return (session->extensions & SSH_EXT_PING) != 0;
2180
0
}
2181
2182
/**
2183
 * @brief Send a transport-level SSH ping message.
2184
 *
2185
 * This function sends a SSH2_MSG_PING packet to the remote peer.
2186
 * Optional payload data is sent as part of the ping message.
2187
 * Use ssh_is_ping_supported() to check whether the remote peer supports
2188
 * pings before calling this function.
2189
 *
2190
 * @param[in]  session  The SSH session handle.
2191
 * @param[in]  data     Optional payload data (may be NULL).
2192
 * @param[in]  len      Length of the payload data in bytes.
2193
 *
2194
 * @return SSH_OK on success, SSH_ERROR on failure.
2195
 *
2196
 * @see ssh_is_ping_supported()
2197
 */
2198
int ssh_send_ping(ssh_session session, const void *data, size_t len)
2199
0
{
2200
0
    int rc;
2201
0
    ssh_string payload = NULL;
2202
2203
0
    if (session == NULL) {
2204
0
        return SSH_ERROR;
2205
0
    }
2206
2207
    /* Only send ping if the peer has advertised ping@openssh.com */
2208
0
    if (!ssh_is_ping_supported(session)) {
2209
0
        ssh_set_error(session,
2210
0
                      SSH_REQUEST_DENIED,
2211
0
                      "ping@openssh.com extension was not negotiated");
2212
0
        return SSH_ERROR;
2213
0
    }
2214
2215
0
    payload = ssh_string_from_data(data, len);
2216
0
    if (payload == NULL) {
2217
0
        ssh_set_error_oom(session);
2218
0
        return SSH_ERROR;
2219
0
    }
2220
2221
0
    rc = ssh_buffer_pack(session->out_buffer, "bS", SSH2_MSG_PING, payload);
2222
0
    SSH_STRING_FREE(payload);
2223
0
    if (rc != SSH_OK) {
2224
0
        ssh_set_error_oom(session);
2225
0
        ssh_buffer_reinit(session->out_buffer);
2226
0
        return SSH_ERROR;
2227
0
    }
2228
2229
0
    rc = ssh_packet_send(session);
2230
0
    if (rc != SSH_OK) {
2231
0
        return SSH_ERROR;
2232
0
    }
2233
0
    session->pending_pings++;
2234
2235
0
    return SSH_OK;
2236
0
}
2237
2238
static void
2239
ssh_init_rekey_state(struct ssh_session_struct *session,
2240
                     struct ssh_cipher_struct *cipher)
2241
0
{
2242
    /* Reset the counters: should be NOOP */
2243
0
    cipher->packets = 0;
2244
0
    cipher->blocks = 0;
2245
2246
    /* Default rekey limits for ciphers as specified in RFC4344, Section 3.2 */
2247
0
    if (cipher->blocksize >= 16) {
2248
        /* For larger block size (L bits) use maximum of 2**(L/4) blocks */
2249
0
        cipher->max_blocks = (uint64_t)1 << (cipher->blocksize*2);
2250
0
    } else {
2251
        /* For smaller blocks use limit of 1 GB as recommended in RFC4253 */
2252
0
        cipher->max_blocks = ((uint64_t)1 << 30) / cipher->blocksize;
2253
0
    }
2254
    /* If we have limit provided by user, use the smaller one */
2255
0
    if (session->opts.rekey_data != 0) {
2256
0
        cipher->max_blocks = MIN(cipher->max_blocks,
2257
0
                                 session->opts.rekey_data / cipher->blocksize);
2258
0
    }
2259
2260
0
    SSH_LOG(SSH_LOG_DEBUG,
2261
0
            "Set rekey after %" PRIu64 " blocks",
2262
0
            cipher->max_blocks);
2263
0
}
2264
2265
/*
2266
 * Once we got SSH2_MSG_NEWKEYS we can switch next_crypto and
2267
 * current_crypto for our desired direction
2268
 */
2269
int
2270
ssh_packet_set_newkeys(ssh_session session,
2271
                       enum ssh_crypto_direction_e direction)
2272
0
{
2273
0
    struct ssh_cipher_struct *in_cipher = NULL, *out_cipher = NULL;
2274
0
    int rc;
2275
2276
0
    SSH_LOG(SSH_LOG_TRACE,
2277
0
            "called, direction =%s%s",
2278
0
            direction & SSH_DIRECTION_IN ? " IN " : "",
2279
0
            direction & SSH_DIRECTION_OUT ? " OUT " : "");
2280
2281
0
    if (session->next_crypto == NULL) {
2282
0
        return SSH_ERROR;
2283
0
    }
2284
2285
0
    session->next_crypto->used |= direction;
2286
0
    if (session->current_crypto != NULL) {
2287
0
        if (session->current_crypto->used & direction) {
2288
0
            SSH_LOG(SSH_LOG_TRACE, "This direction isn't used anymore.");
2289
0
        }
2290
        /* Mark the current requested direction unused */
2291
0
        session->current_crypto->used &= ~direction;
2292
0
    }
2293
2294
    /* Both sides switched: do the actual switch now */
2295
0
    if (session->next_crypto->used == SSH_DIRECTION_BOTH) {
2296
0
        size_t session_id_len;
2297
2298
0
        if (session->current_crypto != NULL) {
2299
0
            crypto_free(session->current_crypto);
2300
0
            session->current_crypto = NULL;
2301
0
        }
2302
2303
0
        session->current_crypto = session->next_crypto;
2304
0
        session->current_crypto->used = SSH_DIRECTION_BOTH;
2305
2306
        /* Initialize the next_crypto structure */
2307
0
        session->next_crypto = crypto_new();
2308
0
        if (session->next_crypto == NULL) {
2309
0
            ssh_set_error_oom(session);
2310
0
            return SSH_ERROR;
2311
0
        }
2312
2313
0
        session_id_len = session->current_crypto->session_id_len;
2314
0
        session->next_crypto->session_id = malloc(session_id_len);
2315
0
        if (session->next_crypto->session_id == NULL) {
2316
0
            ssh_set_error_oom(session);
2317
0
            return SSH_ERROR;
2318
0
        }
2319
2320
0
        memcpy(session->next_crypto->session_id,
2321
0
               session->current_crypto->session_id,
2322
0
               session_id_len);
2323
0
        session->next_crypto->session_id_len = session_id_len;
2324
2325
0
        return SSH_OK;
2326
0
    }
2327
2328
    /* Initialize common structures so the next context can be used in
2329
     * either direction */
2330
0
    if (session->client) {
2331
        /* The server has this part already done */
2332
0
        rc = ssh_make_sessionid(session);
2333
0
        if (rc != SSH_OK) {
2334
0
            return SSH_ERROR;
2335
0
        }
2336
2337
        /*
2338
         * Set the cryptographic functions for the next crypto
2339
         * (it is needed for ssh_generate_session_keys for key lengths)
2340
         */
2341
0
        rc = crypt_set_algorithms_client(session);
2342
0
        if (rc < 0) {
2343
0
            return SSH_ERROR;
2344
0
        }
2345
0
    }
2346
2347
0
    if (ssh_generate_session_keys(session) < 0) {
2348
0
        return SSH_ERROR;
2349
0
    }
2350
2351
0
    in_cipher = session->next_crypto->in_cipher;
2352
0
    out_cipher = session->next_crypto->out_cipher;
2353
0
    if (in_cipher == NULL || out_cipher == NULL) {
2354
0
        return SSH_ERROR;
2355
0
    }
2356
2357
    /* Initialize rekeying states */
2358
0
    ssh_init_rekey_state(session, out_cipher);
2359
0
    ssh_init_rekey_state(session, in_cipher);
2360
0
    if (session->opts.rekey_time != 0) {
2361
0
        ssh_timestamp_init(&session->last_rekey_time);
2362
0
        SSH_LOG(SSH_LOG_DEBUG, "Set rekey after %" PRIu32 " seconds",
2363
0
                session->opts.rekey_time/1000);
2364
0
    }
2365
2366
0
    if (in_cipher->set_decrypt_key) {
2367
        /* Initialize the encryption and decryption keys in next_crypto */
2368
0
        rc = in_cipher->set_decrypt_key(in_cipher,
2369
0
                                        session->next_crypto->decryptkey,
2370
0
                                        session->next_crypto->decryptIV);
2371
0
        if (rc < 0) {
2372
            /* On error, make sure it is not used */
2373
0
            session->next_crypto->used = 0;
2374
0
            return SSH_ERROR;
2375
0
        }
2376
0
    }
2377
2378
0
    if (out_cipher->set_encrypt_key) {
2379
0
        rc = out_cipher->set_encrypt_key(out_cipher,
2380
0
                                         session->next_crypto->encryptkey,
2381
0
                                         session->next_crypto->encryptIV);
2382
0
        if (rc < 0) {
2383
            /* On error, make sure it is not used */
2384
0
            session->next_crypto->used = 0;
2385
0
            return SSH_ERROR;
2386
0
        }
2387
0
    }
2388
2389
0
    return SSH_OK;
2390
0
}