Coverage Report

Created: 2026-11-11 07:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh2/src/kex.c
Line
Count
Source
1
/* Copyright (C) Sara Golemon <sarag@libssh2.org>
2
 * Copyright (C) Daniel Stenberg <daniel@haxx.se>
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 *
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 *    this list of conditions and the following disclaimer in the documentation
13
 *    and/or other materials provided with the distribution.
14
 *
15
 * 3. Neither the name of the copyright holder nor the names of its
16
 *    contributors may be used to endorse or promote products derived from this
17
 *    software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 *
31
 * SPDX-License-Identifier: BSD-3-Clause
32
 */
33
34
#include "libssh2_priv.h"
35
36
#include "transport.h"
37
#include "comp.h"
38
#include "mac.h"
39
40
#include <assert.h>
41
42
static void kex_value_hash(ssh2_hash_alg hash_alg, size_t digest_len,
43
                           LIBSSH2_SESSION *session,
44
                           struct kmdhgGPshakex_state *exchange_state,
45
                           unsigned char **data, size_t data_len,
46
                           const void *version)
47
0
{
48
0
    if(!digest_len || digest_len > MAX_SHA_DIGEST_LEN) {
49
0
        *data = NULL;
50
0
        return;
51
0
    }
52
53
0
    if(!*data)
54
0
        *data = SSH2_ALLOC(session, data_len + digest_len);
55
56
0
    if(*data) {
57
0
        size_t len = 0;
58
0
        while(len < data_len) {
59
0
            ssh2_hash_ctx ctx;
60
0
            int hok = ssh2_hash_init(&ctx, hash_alg);
61
0
            if(hok) {
62
0
                hok &= ssh2_hash_update(&ctx, exchange_state->k_value,
63
0
                                              exchange_state->k_value_len);
64
0
                hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp,
65
0
                                              digest_len);
66
0
                if(len)
67
0
                    hok &= ssh2_hash_update(&ctx, *data, len);
68
0
                else {
69
0
                    hok &= ssh2_hash_update(&ctx, version, 1);
70
0
                    hok &= ssh2_hash_update(&ctx, session->session_id,
71
0
                                                  session->session_id_len);
72
0
                }
73
0
                hok &= ssh2_hash_final(&ctx, *data + len, digest_len);
74
0
            }
75
0
            if(!hok) {
76
0
                SSH2_SAFEFREE(session, *data);
77
0
                return;
78
0
            }
79
0
            len += digest_len;
80
0
        }
81
0
    }
82
0
}
83
84
static int kex_proc_hostkey(LIBSSH2_SESSION *session, struct string_buf *buf,
85
                            const struct kmdhgGPshakex_state *exchange_state,
86
                            unsigned char *data, size_t data_len)
87
4
{
88
4
    size_t host_key_len;
89
90
    /* Parse reply */
91
4
    if(data) {
92
4
        if(data_len < 5)
93
0
            return ssh2_err(session, LIBSSH2_ERROR_PROTO,
94
0
                            "Unexpected packet length");
95
4
        buf->data = data;
96
4
        buf->len = data_len;
97
4
    }
98
0
    else {
99
0
        if(exchange_state->s_packet_len < 5)
100
0
            return ssh2_err(session, LIBSSH2_ERROR_PROTO,
101
0
                            "Unexpected packet length");
102
0
        buf->data = exchange_state->s_packet;
103
0
        buf->len = exchange_state->s_packet_len;
104
0
    }
105
106
4
    buf->dataptr = buf->data;
107
4
    buf->dataptr++; /* advance past type */
108
109
4
    if(session->server_hostkey) {
110
2
        SSH2_SAFEFREE(session, session->server_hostkey);
111
2
        session->server_hostkey_len = 0;
112
2
    }
113
114
4
    if(ssh2_copy_string(session, buf, &session->server_hostkey, &host_key_len))
115
0
        return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
116
0
                        "Could not copy host key");
117
118
4
    session->server_hostkey_len = (uint32_t)host_key_len;
119
120
#if LIBSSH2_MD5
121
    session->server_hostkey_md5_valid = ssh2_hash(SSH2_MD5_ALG,
122
        session->server_hostkey, session->server_hostkey_len,
123
        session->server_hostkey_md5, sizeof(session->server_hostkey_md5));
124
#ifdef LIBSSH2DEBUG
125
    {
126
        char fingerprint[SSH2_MD5_DIG_LEN * 3 + 1];
127
        char *fprint = fingerprint;
128
        int i;
129
        for(i = 0; i < SSH2_MD5_DIG_LEN; i++, fprint += 3)
130
            ssh2_snprintf(fprint, 4, "%02x:", session->server_hostkey_md5[i]);
131
        *(--fprint) = '\0';
132
        ssh2_deb((session, LIBSSH2_TRACE_KEX, "Server's MD5 Fingerprint: %s",
133
                  fingerprint));
134
    }
135
#endif /* LIBSSH2DEBUG */
136
#endif /* !LIBSSH2_MD5 */
137
138
4
    session->server_hostkey_sha1_valid = ssh2_hash(SSH2_SHA1_ALG,
139
4
        session->server_hostkey, session->server_hostkey_len,
140
4
        session->server_hostkey_sha1, sizeof(session->server_hostkey_sha1));
141
4
#ifdef LIBSSH2DEBUG
142
4
    {
143
4
        char fingerprint[SSH2_SHA1_DIG_LEN * 3 + 1];
144
4
        char *fprint = fingerprint;
145
4
        int i;
146
84
        for(i = 0; i < SSH2_SHA1_DIG_LEN; i++, fprint += 3)
147
80
            ssh2_snprintf(fprint, 4, "%02x:", session->server_hostkey_sha1[i]);
148
4
        *(--fprint) = '\0';
149
4
        ssh2_deb((session, LIBSSH2_TRACE_KEX, "Server's SHA1 Fingerprint: %s",
150
4
                  fingerprint));
151
4
    }
152
4
#endif /* LIBSSH2DEBUG */
153
154
4
    session->server_hostkey_sha256_valid = ssh2_hash(SSH2_SHA256_ALG,
155
4
        session->server_hostkey, session->server_hostkey_len,
156
4
        session->server_hostkey_sha256,
157
4
        sizeof(session->server_hostkey_sha256));
158
4
#ifdef LIBSSH2DEBUG
159
4
    {
160
4
        char *base64Fingerprint = NULL;
161
4
        ssh2_base64_encode(session,
162
4
                           (const char *)session->server_hostkey_sha256,
163
4
                           SSH2_SHA256_DIG_LEN, &base64Fingerprint);
164
4
        if(base64Fingerprint) {
165
4
            ssh2_deb((session, LIBSSH2_TRACE_KEX,
166
4
                      "Server's SHA256 Fingerprint: %s", base64Fingerprint));
167
4
            SSH2_FREE(session, base64Fingerprint);
168
4
        }
169
4
    }
170
4
#endif /* LIBSSH2DEBUG */
171
172
4
    if(!session->hostkey)
173
0
        return ssh2_err(session, LIBSSH2_ERROR_PROTO, "host key is NULL");
174
4
    if(session->hostkey->init(session, session->server_hostkey,
175
4
                              session->server_hostkey_len,
176
4
                              &session->server_hostkey_abstract))
177
0
        return ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
178
0
                        "Unable to initialize host key importer");
179
180
4
    return 0;
181
4
}
182
183
static int kex_finish(LIBSSH2_SESSION *session,
184
                      struct kmdhgGPshakex_state *exchange_state,
185
                      ssh2_hash_alg hash_alg, size_t digest_len)
186
0
{
187
0
    int rc;
188
0
    rc = ssh2_packet_require(session, SSH_MSG_NEWKEYS,
189
0
                             &exchange_state->tmp,
190
0
                             &exchange_state->tmp_len, 0, NULL, 0,
191
0
                             &exchange_state->req_state);
192
0
    if(rc == LIBSSH2_ERROR_EAGAIN)
193
0
        return rc;
194
0
    if(rc)
195
0
        return ssh2_err(session, rc, "Timed out waiting for NEWKEYS");
196
197
    /* The first key exchange has been performed,
198
       switch to active crypt/comp/mac mode */
199
0
    session->state |= SSH2_STATE_NEWKEYS;
200
0
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Received NEWKEYS message"));
201
202
    /* This actually ends up being packet_type(1)
203
       for this packet type anyway */
204
0
    SSH2_FREE(session, exchange_state->tmp);
205
206
0
    if(!session->session_id) {
207
0
        session->session_id = SSH2_ALLOC(session, digest_len);
208
0
        if(!session->session_id)
209
0
            return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
210
0
                            "Unable to allocate buffer for SHA digest");
211
0
        memcpy(session->session_id, exchange_state->h_sig_comp, digest_len);
212
0
        session->session_id_len = (uint32_t)digest_len;
213
0
        ssh2_deb((session, LIBSSH2_TRACE_KEX, "session_id calculated"));
214
0
    }
215
216
    /* Cleanup any existing cipher */
217
0
    if(session->local.crypt->dtor)
218
0
        session->local.crypt->dtor(session, &session->local.crypt_abstract);
219
220
    /* Calculate IV/Secret/Key for each direction */
221
0
    if(session->local.crypt->init) {
222
0
        unsigned char *iv = NULL, *secret = NULL;
223
0
        int free_iv = 0, free_secret = 0;
224
225
0
        kex_value_hash(hash_alg, digest_len, session, exchange_state,
226
0
                       &iv, session->local.crypt->iv_len, "A");
227
0
        if(!iv)
228
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
229
0
                            "Unable to generate IV for key exchange");
230
231
0
        kex_value_hash(hash_alg, digest_len, session, exchange_state,
232
0
                       &secret, session->local.crypt->secret_len, "C");
233
0
        if(!secret) {
234
0
            SSH2_FREE(session, iv);
235
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
236
0
                            "Unable to derive secret for key exchange");
237
0
        }
238
239
0
        if(session->local.crypt->init(session, session->local.crypt, iv,
240
0
                                      &free_iv, secret, &free_secret, 1, 0,
241
0
                                      &session->local.crypt_abstract)) {
242
0
            SSH2_FREE(session, iv);
243
0
            SSH2_FREE(session, secret);
244
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
245
0
                            "Unable to initialize local encryption context");
246
0
        }
247
248
0
        if(free_iv)
249
0
            ssh2_zero_free(session, iv, session->local.crypt->iv_len);
250
0
        if(free_secret)
251
0
            ssh2_zero_free(session, secret, session->local.crypt->secret_len);
252
0
    }
253
0
    ssh2_deb((session, LIBSSH2_TRACE_KEX,
254
0
              "Client to Server IV and Key calculated"));
255
256
0
    if(session->remote.crypt->dtor)
257
        /* Cleanup any existing cipher */
258
0
        session->remote.crypt->dtor(session, &session->remote.crypt_abstract);
259
260
0
    if(session->remote.crypt->init) {
261
0
        unsigned char *iv = NULL, *secret = NULL;
262
0
        int free_iv = 0, free_secret = 0;
263
264
0
        kex_value_hash(hash_alg, digest_len, session, exchange_state,
265
0
                       &iv, session->remote.crypt->iv_len, "B");
266
0
        if(!iv)
267
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
268
0
                            "Failed to derive remote IV during key exchange");
269
270
0
        kex_value_hash(hash_alg, digest_len, session, exchange_state,
271
0
                       &secret, session->remote.crypt->secret_len, "D");
272
0
        if(!secret) {
273
0
            SSH2_FREE(session, iv);
274
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
275
0
                            "Failed to derive remote encryption secret during "
276
0
                            "key exchange");
277
0
        }
278
0
        if(session->remote.crypt->init(session, session->remote.crypt, iv,
279
0
                                       &free_iv, secret, &free_secret, 0, 0,
280
0
                                       &session->remote.crypt_abstract)) {
281
0
            SSH2_FREE(session, iv);
282
0
            SSH2_FREE(session, secret);
283
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
284
0
                            "Failed to initialize remote encryption context");
285
0
        }
286
287
0
        if(free_iv)
288
0
            ssh2_zero_free(session, iv, session->remote.crypt->iv_len);
289
0
        if(free_secret)
290
0
            ssh2_zero_free(session, secret, session->remote.crypt->secret_len);
291
0
    }
292
0
    ssh2_deb((session, LIBSSH2_TRACE_KEX,
293
0
              "Server to Client IV and Key calculated"));
294
295
0
    if(session->local.mac->dtor)
296
0
        session->local.mac->dtor(session, &session->local.mac_abstract);
297
298
0
    if(session->local.mac->init) {
299
0
        unsigned char *key = NULL;
300
0
        int free_key = 0;
301
302
0
        kex_value_hash(hash_alg, digest_len, session, exchange_state,
303
0
                       &key, session->local.mac->key_len, "E");
304
0
        if(!key)
305
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
306
0
                            "Unable to derive client-to-server MAC key");
307
308
0
        session->local.mac->init(session, key, &free_key,
309
0
                                 &session->local.mac_abstract);
310
0
        if(free_key)
311
0
            ssh2_zero_free(session, key, session->local.mac->key_len);
312
0
    }
313
0
    ssh2_deb((session, LIBSSH2_TRACE_KEX,
314
0
              "Client to Server HMAC Key calculated"));
315
316
0
    if(session->remote.mac->dtor)
317
0
        session->remote.mac->dtor(session, &session->remote.mac_abstract);
318
319
0
    if(session->remote.mac->init) {
320
0
        unsigned char *key = NULL;
321
0
        int free_key = 0;
322
323
0
        kex_value_hash(hash_alg, digest_len, session, exchange_state,
324
0
                       &key, session->remote.mac->key_len, "F");
325
0
        if(!key)
326
0
            return ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
327
0
                            "Unable to derive server-to-client MAC key");
328
329
0
        session->remote.mac->init(session, key, &free_key,
330
0
                                  &session->remote.mac_abstract);
331
0
        if(free_key)
332
0
            ssh2_zero_free(session, key, session->remote.mac->key_len);
333
0
    }
334
0
    ssh2_deb((session, LIBSSH2_TRACE_KEX,
335
0
              "Server to Client HMAC Key calculated"));
336
337
    /* Initialize compression for each direction */
338
339
    /* Cleanup any existing compression */
340
0
    if(session->local.comp && session->local.comp->dtor)
341
0
        session->local.comp->dtor(session, 1, &session->local.comp_abstract);
342
343
0
    if(session->local.comp && session->local.comp->init &&
344
0
       session->local.comp->init(session, 1, &session->local.comp_abstract))
345
0
        return LIBSSH2_ERROR_KEX_FAILURE;
346
0
    ssh2_deb((session, LIBSSH2_TRACE_KEX,
347
0
              "Client to Server compression initialized"));
348
349
0
    if(session->remote.comp && session->remote.comp->dtor)
350
0
        session->remote.comp->dtor(session, 0, &session->remote.comp_abstract);
351
352
0
    if(session->remote.comp && session->remote.comp->init &&
353
0
       session->remote.comp->init(session, 0, &session->remote.comp_abstract))
354
0
        return LIBSSH2_ERROR_KEX_FAILURE;
355
0
    ssh2_deb((session, LIBSSH2_TRACE_KEX,
356
0
              "Server to Client compression initialized"));
357
358
0
    return 0;
359
0
}
360
361
static void kex_diffie_hellman_state_cleanup(
362
    LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state)
363
342
{
364
342
    ssh2_dh_dtor(&exchange_state->x);
365
342
    ssh2_bn_free(exchange_state->e);
366
342
    exchange_state->e = NULL;
367
342
    ssh2_bn_free(exchange_state->f);
368
342
    exchange_state->f = NULL;
369
342
    ssh2_bn_free(exchange_state->k);
370
342
    exchange_state->k = NULL;
371
342
    ssh2_bn_ctx_free(exchange_state->ctx);
372
342
    exchange_state->ctx = NULL;
373
374
342
    if(exchange_state->e_packet)
375
317
        SSH2_SAFEFREE(session, exchange_state->e_packet);
376
342
    if(exchange_state->s_packet)
377
0
        SSH2_SAFEFREE(session, exchange_state->s_packet);
378
342
    if(exchange_state->k_value) {
379
0
        ssh2_zero_free(session, exchange_state->k_value,
380
0
                                exchange_state->k_value_len);
381
0
        exchange_state->k_value = NULL;
382
0
    }
383
384
342
    exchange_state->state = ssh2_NB_state_idle;
385
342
}
386
387
static void kex_diffie_hellman_cleanup(
388
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
389
689
{
390
689
    if(key_state->state != ssh2_NB_state_idle) {
391
317
        ssh2_bn_free(key_state->p);
392
317
        key_state->p = NULL;
393
317
        ssh2_bn_free(key_state->g);
394
317
        key_state->g = NULL;
395
396
317
        if(key_state->data)
397
0
            SSH2_SAFEFREE(session, key_state->data);
398
317
        key_state->state = ssh2_NB_state_idle;
399
317
    }
400
401
689
    if(key_state->exchange_state.state != ssh2_NB_state_idle)
402
0
        kex_diffie_hellman_state_cleanup(session, &key_state->exchange_state);
403
689
}
404
405
/*
406
 * @abstract Diffie Hellman Key Exchange, Group Agnostic,
407
 * SHA Algorithm Agnostic
408
 * @result 0 on success, error code on failure
409
 */
410
static int kex_diffie_hellman_sha(LIBSSH2_SESSION *session,
411
                                  ssh2_bn *g,
412
                                  ssh2_bn *p,
413
                                  int group_order,
414
                                  ssh2_hash_alg hash_alg,
415
                                  size_t digest_len,
416
                                  unsigned char packet_type_init,
417
                                  unsigned char packet_type_reply,
418
                                  unsigned char *midhash,
419
                                  size_t midhash_len,
420
                                  struct kmdhgGPshakex_state *exchange_state)
421
342
{
422
342
    int ret = 0;
423
342
    int rc;
424
425
342
    if(exchange_state->state == ssh2_NB_state_idle) {
426
        /* Setup initial values */
427
342
        exchange_state->e_packet = NULL;
428
342
        exchange_state->s_packet = NULL;
429
342
        exchange_state->k_value = NULL;
430
342
        exchange_state->ctx = ssh2_bn_ctx_new();
431
342
        ssh2_dh_init(&exchange_state->x);
432
342
        exchange_state->e = ssh2_bn_init(); /* g^x mod p */
433
342
        exchange_state->f = NULL; /* g^(Random from server) mod p */
434
342
        exchange_state->k = ssh2_bn_init(); /* The shared secret: f^x mod p */
435
436
        /* Zero the whole thing out */
437
342
        memset(&exchange_state->req_state, 0,
438
342
               sizeof(exchange_state->req_state));
439
440
        /* Generate x and e */
441
342
        if(ssh2_bn_bits(p) > SSH2_DH_MAX_MODULUS_BITS) {
442
0
            ret = ssh2_err(session, LIBSSH2_ERROR_INVAL,
443
0
                           "DH modulus value is too large");
444
0
            goto clean_exit;
445
0
        }
446
447
342
        rc = ssh2_dh_key_pair(&exchange_state->x, exchange_state->e, g, p,
448
342
                              group_order, exchange_state->ctx);
449
342
        if(rc) {
450
0
            ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
451
0
                           "DH key pair generation failed");
452
0
            goto clean_exit;
453
0
        }
454
455
        /* Send KEX init */
456
        /* packet_type(1) + String Length(4) + leading 0(1) */
457
342
        exchange_state->e_packet_len = ssh2_bn_bytes(exchange_state->e) + 6;
458
342
        if(ssh2_bn_bits(exchange_state->e) % 8)
459
177
            exchange_state->e_packet_len--;  /* Leading 00 not needed */
460
461
342
        exchange_state->e_packet =
462
342
            SSH2_ALLOC(session, exchange_state->e_packet_len);
463
342
        if(!exchange_state->e_packet) {
464
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
465
0
                           "Out of memory error");
466
0
            goto clean_exit;
467
0
        }
468
342
        exchange_state->e_packet[0] = packet_type_init;
469
342
        ssh2_htonu32(exchange_state->e_packet + 1,
470
342
                     (uint32_t)(exchange_state->e_packet_len - 5));
471
342
        if(ssh2_bn_bits(exchange_state->e) % 8) {
472
177
            if(ssh2_bn_to_bin(exchange_state->e,
473
177
                              exchange_state->e_packet + 5)) {
474
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
475
0
                               "Cannot write exchange_state->e");
476
0
                goto clean_exit;
477
0
            }
478
177
        }
479
165
        else {
480
165
            exchange_state->e_packet[5] = 0;
481
165
            if(ssh2_bn_to_bin(exchange_state->e,
482
165
                              exchange_state->e_packet + 6)) {
483
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
484
0
                               "Cannot write exchange_state->e");
485
0
                goto clean_exit;
486
0
            }
487
165
        }
488
489
342
        ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sending KEX packet %u",
490
342
                  (unsigned int)packet_type_init));
491
342
        exchange_state->state = ssh2_NB_state_created;
492
342
    }
493
494
342
    if(exchange_state->state == ssh2_NB_state_created) {
495
342
        rc = ssh2_transport_send(session, exchange_state->e_packet,
496
342
                                 exchange_state->e_packet_len, NULL, 0);
497
342
        if(rc == LIBSSH2_ERROR_EAGAIN)
498
0
            return rc;
499
342
        else if(rc) {
500
0
            ret = ssh2_err(session, rc, "Unable to send KEX init message");
501
0
            goto clean_exit;
502
0
        }
503
342
        exchange_state->state = ssh2_NB_state_sent;
504
342
    }
505
506
342
    if(exchange_state->state == ssh2_NB_state_sent) {
507
342
        if(session->burn_optimistic_kexinit) {
508
            /* The first KEX packet to come along is the guess initially
509
             * sent by the server.  That guess turned out to be wrong so we
510
             * need to silently ignore it */
511
341
            int burn_type;
512
513
341
            ssh2_deb((session, LIBSSH2_TRACE_KEX,
514
341
                      "Waiting for badly guessed KEX packet (to be ignored)"));
515
341
            burn_type = ssh2_packet_burn(session, &exchange_state->burn_state);
516
341
            if(burn_type == LIBSSH2_ERROR_EAGAIN)
517
0
                return burn_type;
518
341
            else if(burn_type <= 0) {
519
                /* Failed to receive a packet */
520
24
                ret = burn_type;
521
24
                goto clean_exit;
522
24
            }
523
317
            session->burn_optimistic_kexinit = 0;
524
525
317
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Burnt packet of type: %02x",
526
317
                      (unsigned int)burn_type));
527
317
        }
528
529
318
        exchange_state->state = ssh2_NB_state_sent1;
530
318
    }
531
532
318
    if(exchange_state->state == ssh2_NB_state_sent1) {
533
        /* Wait for KEX reply */
534
318
        struct string_buf buf;
535
318
        ssh2_hash_ctx ctx;
536
318
        int err;
537
318
        int hok;
538
539
318
        rc = ssh2_packet_require(session, packet_type_reply,
540
318
                                 &exchange_state->s_packet,
541
318
                                 &exchange_state->s_packet_len, 0, NULL,
542
318
                                 0, &exchange_state->req_state);
543
318
        if(rc == LIBSSH2_ERROR_EAGAIN)
544
0
            return rc;
545
318
        if(rc) {
546
318
            ret = ssh2_err(session, LIBSSH2_ERROR_TIMEOUT,
547
318
                           "Timed out waiting for KEX reply");
548
318
            goto clean_exit;
549
318
        }
550
551
0
        ret = kex_proc_hostkey(session, &buf, exchange_state, NULL, 0);
552
0
        if(ret)
553
0
            goto clean_exit;
554
555
0
        if(ssh2_get_string(&buf, &exchange_state->f_value,
556
0
                           &exchange_state->f_value_len)) {
557
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
558
0
                           "Unable to get DH-SHA f value");
559
0
            goto clean_exit;
560
0
        }
561
562
0
        if(ssh2_bn_from_bin(&exchange_state->f,
563
0
                            exchange_state->f_value,
564
0
                            exchange_state->f_value_len)) {
565
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
566
0
                           "Invalid DH-SHA f value");
567
0
            goto clean_exit;
568
0
        }
569
570
0
        if(ssh2_get_string(&buf, &exchange_state->h_sig,
571
0
                           &exchange_state->h_sig_len)) {
572
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
573
0
                           "Unable to get DH-SHA h sig");
574
0
            goto clean_exit;
575
0
        }
576
577
        /* Compute the shared secret */
578
0
        if(ssh2_dh_secret(&exchange_state->x, exchange_state->k,
579
0
                          exchange_state->f, p, exchange_state->ctx)) {
580
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
581
0
                           "Invalid DH secret parameters");
582
0
            goto clean_exit;
583
0
        }
584
0
        exchange_state->k_value_len = ssh2_bn_bytes(exchange_state->k) + 5;
585
0
        if(ssh2_bn_bits(exchange_state->k) % 8)
586
0
            exchange_state->k_value_len--;  /* do not need leading 00 */
587
0
        exchange_state->k_value =
588
0
            SSH2_ALLOC(session, exchange_state->k_value_len);
589
0
        if(!exchange_state->k_value) {
590
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
591
0
                           "Unable to allocate buffer for DH-SHA K");
592
0
            goto clean_exit;
593
0
        }
594
0
        ssh2_htonu32(exchange_state->k_value,
595
0
                     (uint32_t)(exchange_state->k_value_len - 4));
596
0
        if(ssh2_bn_bits(exchange_state->k) % 8) {
597
0
            if(ssh2_bn_to_bin(exchange_state->k,
598
0
                              exchange_state->k_value + 4)) {
599
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
600
0
                               "Cannot write exchange_state->k");
601
0
                goto clean_exit;
602
0
            }
603
0
        }
604
0
        else {
605
0
            exchange_state->k_value[4] = 0;
606
0
            if(ssh2_bn_to_bin(exchange_state->k,
607
0
                              exchange_state->k_value + 5)) {
608
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
609
0
                               "Cannot write exchange_state->k");
610
0
                goto clean_exit;
611
0
            }
612
0
        }
613
614
0
        hok = ssh2_hash_init(&ctx, hash_alg);
615
0
        if(!hok) {
616
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HASH_INIT,
617
0
                           "Unable to initialize hash context DH-SHA");
618
0
            goto clean_exit;
619
0
        }
620
0
        if(session->local.banner) {
621
0
            ssh2_htonu32(exchange_state->h_sig_comp,
622
0
                (uint32_t)(strlen(session->local.banner) - 2));
623
0
            hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
624
0
            hok &= ssh2_hash_update(&ctx, session->local.banner,
625
0
                                    strlen(session->local.banner) - 2);
626
0
        }
627
0
        else {
628
0
            ssh2_htonu32(exchange_state->h_sig_comp,
629
0
                         sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1);
630
0
            hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
631
0
            hok &= ssh2_hash_update(&ctx, LIBSSH2_SSH_DEFAULT_BANNER,
632
0
                                    sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1);
633
0
        }
634
635
0
        ssh2_htonu32(exchange_state->h_sig_comp,
636
0
                     (uint32_t)strlen(session->remote.banner));
637
0
        hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
638
0
        hok &= ssh2_hash_update(&ctx, session->remote.banner,
639
0
                                strlen(session->remote.banner));
640
641
0
        ssh2_htonu32(exchange_state->h_sig_comp,
642
0
                     (uint32_t)session->local.kexinit_len);
643
0
        hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
644
0
        hok &= ssh2_hash_update(&ctx, session->local.kexinit,
645
0
                                      session->local.kexinit_len);
646
647
0
        ssh2_htonu32(exchange_state->h_sig_comp,
648
0
                     (uint32_t)session->remote.kexinit_len);
649
0
        hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
650
0
        hok &= ssh2_hash_update(&ctx, session->remote.kexinit,
651
0
                                      session->remote.kexinit_len);
652
653
0
        ssh2_htonu32(exchange_state->h_sig_comp, session->server_hostkey_len);
654
0
        hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
655
0
        hok &= ssh2_hash_update(&ctx, session->server_hostkey,
656
0
                                      session->server_hostkey_len);
657
658
0
        if(packet_type_init == SSH_MSG_KEX_DH_GEX_INIT) {
659
            /* diffie-hellman-group-exchange hashes additional fields */
660
0
            ssh2_htonu32(exchange_state->h_sig_comp, SSH2_DH_GEX_MINGROUP);
661
0
            ssh2_htonu32(exchange_state->h_sig_comp + 4, SSH2_DH_GEX_OPTGROUP);
662
0
            ssh2_htonu32(exchange_state->h_sig_comp + 8, SSH2_DH_GEX_MAXGROUP);
663
0
            hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 12);
664
0
        }
665
666
0
        if(midhash)
667
0
            hok &= ssh2_hash_update(&ctx, midhash, midhash_len);
668
669
0
        hok &= ssh2_hash_update(&ctx, exchange_state->e_packet + 1,
670
0
                                      exchange_state->e_packet_len - 1);
671
672
0
        ssh2_htonu32(exchange_state->h_sig_comp,
673
0
                     (uint32_t)exchange_state->f_value_len);
674
0
        hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
675
0
        hok &= ssh2_hash_update(&ctx, exchange_state->f_value,
676
0
                                      exchange_state->f_value_len);
677
678
0
        hok &= ssh2_hash_update(&ctx, exchange_state->k_value,
679
0
                                      exchange_state->k_value_len);
680
681
0
        hok &= ssh2_hash_final(&ctx, exchange_state->h_sig_comp,
682
0
                                     sizeof(exchange_state->h_sig_comp));
683
0
        if(!hok) {
684
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC,
685
0
                           "Failed to calculate hash DH-SHA");
686
0
            goto clean_exit;
687
0
        }
688
689
0
        err = session->hostkey->sig_verify(session,
690
0
                                           exchange_state->h_sig,
691
0
                                           exchange_state->h_sig_len,
692
0
                                           exchange_state->h_sig_comp,
693
0
                                           digest_len,
694
0
                                           &session->server_hostkey_abstract);
695
696
0
        if(err) {
697
0
            ssh2_deb((session, LIBSSH2_TRACE_KEX,
698
0
                      "Failed host key sig_verify(): %s: %d",
699
0
                      session->hostkey->name, err));
700
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_SIGN,
701
0
                           "Unable to verify host key signature DH-SHA");
702
0
            goto clean_exit;
703
0
        }
704
705
0
        ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sending NEWKEYS message"));
706
0
        exchange_state->c = SSH_MSG_NEWKEYS;
707
708
0
        exchange_state->state = ssh2_NB_state_sent2;
709
0
    }
710
711
0
    if(exchange_state->state == ssh2_NB_state_sent2) {
712
0
        rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0);
713
0
        if(rc == LIBSSH2_ERROR_EAGAIN)
714
0
            return rc;
715
0
        else if(rc) {
716
0
            ret = ssh2_err(session, rc,
717
0
                           "Unable to send NEWKEYS message DH-SHA");
718
0
            goto clean_exit;
719
0
        }
720
721
0
        exchange_state->state = ssh2_NB_state_sent3;
722
0
    }
723
724
0
    if(exchange_state->state == ssh2_NB_state_sent3) {
725
0
        ret = kex_finish(session, exchange_state, hash_alg, digest_len);
726
0
        if(ret == LIBSSH2_ERROR_EAGAIN)
727
0
            return ret;
728
0
    }
729
730
342
clean_exit:
731
342
    kex_diffie_hellman_state_cleanup(session, exchange_state);
732
733
342
    return ret;
734
0
}
735
736
#ifdef LIBSSH2_KEX_SHA1_ENABLE
737
/*
738
 * Diffie-Hellman Group1 (Actually Group2) Key Exchange using SHA1
739
 */
740
static int kex_method_diffie_hellman_group1_sha1_key_exchange(
741
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
742
{
743
    static const unsigned char p_value[128] = {
744
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
745
        0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
746
        0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
747
        0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
748
        0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22,
749
        0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
750
        0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B,
751
        0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
752
        0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
753
        0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
754
        0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B,
755
        0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
756
        0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5,
757
        0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
758
        0x49, 0x28, 0x66, 0x51, 0xEC, 0xE6, 0x53, 0x81,
759
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
760
    };
761
762
    int ret;
763
764
    if(key_state->state == ssh2_NB_state_idle) {
765
        /* g == 2 */
766
        key_state->p = NULL; /* SSH2 defined value (p_value) */
767
        key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */
768
769
        /* Initialize P and G */
770
        if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) {
771
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
772
                           "Failed to allocate key state g.");
773
            goto clean_exit;
774
        }
775
        if(ssh2_bn_from_bin(&key_state->p, p_value, 128)) {
776
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
777
                           "Failed to allocate key state p.");
778
            goto clean_exit;
779
        }
780
781
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
782
                  "Initiating Diffie-Hellman Group1 Key Exchange"));
783
784
        key_state->state = ssh2_NB_state_created;
785
    }
786
787
    ret = kex_diffie_hellman_sha(session, key_state->g, key_state->p, 128,
788
                                 SSH2_SHA1_ALG, SSH2_SHA1_DIG_LEN,
789
                                 SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY,
790
                                 NULL, 0, &key_state->exchange_state);
791
    if(ret == LIBSSH2_ERROR_EAGAIN)
792
        return ret;
793
794
clean_exit:
795
    kex_diffie_hellman_cleanup(session, key_state);
796
797
    return ret;
798
}
799
#endif
800
801
/*
802
 * Diffie-Hellman Group14 Key Exchange with hash function callback
803
 */
804
typedef int (*diffie_hellman_hash_func_t)(
805
    LIBSSH2_SESSION *session,
806
    ssh2_bn *g,
807
    ssh2_bn *p,
808
    int group_order,
809
    ssh2_hash_alg hash_alg,
810
    size_t digest_len,
811
    unsigned char packet_type_init,
812
    unsigned char packet_type_reply,
813
    unsigned char *midhash,
814
    size_t midhash_len,
815
    struct kmdhgGPshakex_state *exchange_state);
816
817
static int kex_method_diffie_hellman_group14_key_exchange(
818
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state,
819
    ssh2_hash_alg hash_alg,
820
    size_t digest_len,
821
    diffie_hellman_hash_func_t hashfunc)
822
287
{
823
287
    static const unsigned char p_value[256] = {
824
287
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
825
287
        0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
826
287
        0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
827
287
        0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
828
287
        0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22,
829
287
        0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
830
287
        0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B,
831
287
        0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
832
287
        0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
833
287
        0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
834
287
        0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B,
835
287
        0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
836
287
        0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5,
837
287
        0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
838
287
        0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D,
839
287
        0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05,
840
287
        0x98, 0xDA, 0x48, 0x36, 0x1C, 0x55, 0xD3, 0x9A,
841
287
        0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
842
287
        0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96,
843
287
        0x1C, 0x62, 0xF3, 0x56, 0x20, 0x85, 0x52, 0xBB,
844
287
        0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D,
845
287
        0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04,
846
287
        0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x18, 0x21, 0x7C,
847
287
        0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B,
848
287
        0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03,
849
287
        0x9B, 0x27, 0x83, 0xA2, 0xEC, 0x07, 0xA2, 0x8F,
850
287
        0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9,
851
287
        0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18,
852
287
        0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A, 0xE5,
853
287
        0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
854
287
        0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA, 0x68,
855
287
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
856
287
    };
857
287
    int ret;
858
859
287
    if(key_state->state == ssh2_NB_state_idle) {
860
287
        key_state->p = NULL; /* SSH2 defined value (p_value) */
861
287
        key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */
862
863
        /* g == 2 */
864
        /* Initialize P and G */
865
287
        if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) {
866
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
867
0
                           "Failed to allocate key state g.");
868
0
            goto clean_exit;
869
0
        }
870
287
        else if(ssh2_bn_from_bin(&key_state->p, p_value, 256)) {
871
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
872
0
                           "Failed to allocate key state p.");
873
0
            goto clean_exit;
874
0
        }
875
876
287
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
877
287
                  "Initiating Diffie-Hellman Group14 Key Exchange"));
878
879
287
        key_state->state = ssh2_NB_state_created;
880
287
    }
881
882
287
    ret = hashfunc(session, key_state->g, key_state->p, 256,
883
287
                   hash_alg, digest_len, SSH_MSG_KEXDH_INIT,
884
287
                   SSH_MSG_KEXDH_REPLY, NULL, 0, &key_state->exchange_state);
885
287
    if(ret == LIBSSH2_ERROR_EAGAIN)
886
0
        return ret;
887
888
287
clean_exit:
889
287
    kex_diffie_hellman_cleanup(session, key_state);
890
891
287
    return ret;
892
287
}
893
894
#ifdef LIBSSH2_KEX_SHA1_ENABLE
895
/*
896
 * Diffie-Hellman Group14 Key Exchange using SHA1
897
 */
898
static int kex_method_diffie_hellman_group14_sha1_key_exchange(
899
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
900
{
901
    return kex_method_diffie_hellman_group14_key_exchange(session, key_state,
902
        SSH2_SHA1_ALG, SSH2_SHA1_DIG_LEN, kex_diffie_hellman_sha);
903
}
904
#endif
905
906
/*
907
 * Diffie-Hellman Group14 Key Exchange using SHA256
908
 */
909
static int kex_method_diffie_hellman_group14_sha256_key_exchange(
910
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
911
287
{
912
287
    return kex_method_diffie_hellman_group14_key_exchange(session, key_state,
913
287
        SSH2_SHA256_ALG, SSH2_SHA256_DIG_LEN, kex_diffie_hellman_sha);
914
287
}
915
916
/*
917
 * Diffie-Hellman Group16 Key Exchange using SHA512
918
 */
919
static int kex_method_diffie_hellman_group16_sha512_key_exchange(
920
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
921
8
{
922
8
    static const unsigned char p_value[512] = {
923
8
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
924
8
        0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
925
8
        0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
926
8
        0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
927
8
        0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
928
8
        0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
929
8
        0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
930
8
        0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
931
8
        0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11,
932
8
        0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D,
933
8
        0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05, 0x98, 0xDA, 0x48, 0x36,
934
8
        0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
935
8
        0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56,
936
8
        0x20, 0x85, 0x52, 0xBB, 0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D,
937
8
        0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04, 0xF1, 0x74, 0x6C, 0x08,
938
8
        0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B,
939
8
        0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2,
940
8
        0xEC, 0x07, 0xA2, 0x8F, 0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9,
941
8
        0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18, 0x39, 0x95, 0x49, 0x7C,
942
8
        0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
943
8
        0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAA, 0xC4, 0x2D, 0xAD, 0x33, 0x17, 0x0D,
944
8
        0x04, 0x50, 0x7A, 0x33, 0xA8, 0x55, 0x21, 0xAB, 0xDF, 0x1C, 0xBA, 0x64,
945
8
        0xEC, 0xFB, 0x85, 0x04, 0x58, 0xDB, 0xEF, 0x0A, 0x8A, 0xEA, 0x71, 0x57,
946
8
        0x5D, 0x06, 0x0C, 0x7D, 0xB3, 0x97, 0x0F, 0x85, 0xA6, 0xE1, 0xE4, 0xC7,
947
8
        0xAB, 0xF5, 0xAE, 0x8C, 0xDB, 0x09, 0x33, 0xD7, 0x1E, 0x8C, 0x94, 0xE0,
948
8
        0x4A, 0x25, 0x61, 0x9D, 0xCE, 0xE3, 0xD2, 0x26, 0x1A, 0xD2, 0xEE, 0x6B,
949
8
        0xF1, 0x2F, 0xFA, 0x06, 0xD9, 0x8A, 0x08, 0x64, 0xD8, 0x76, 0x02, 0x73,
950
8
        0x3E, 0xC8, 0x6A, 0x64, 0x52, 0x1F, 0x2B, 0x18, 0x17, 0x7B, 0x20, 0x0C,
951
8
        0xBB, 0xE1, 0x17, 0x57, 0x7A, 0x61, 0x5D, 0x6C, 0x77, 0x09, 0x88, 0xC0,
952
8
        0xBA, 0xD9, 0x46, 0xE2, 0x08, 0xE2, 0x4F, 0xA0, 0x74, 0xE5, 0xAB, 0x31,
953
8
        0x43, 0xDB, 0x5B, 0xFC, 0xE0, 0xFD, 0x10, 0x8E, 0x4B, 0x82, 0xD1, 0x20,
954
8
        0xA9, 0x21, 0x08, 0x01, 0x1A, 0x72, 0x3C, 0x12, 0xA7, 0x87, 0xE6, 0xD7,
955
8
        0x88, 0x71, 0x9A, 0x10, 0xBD, 0xBA, 0x5B, 0x26, 0x99, 0xC3, 0x27, 0x18,
956
8
        0x6A, 0xF4, 0xE2, 0x3C, 0x1A, 0x94, 0x68, 0x34, 0xB6, 0x15, 0x0B, 0xDA,
957
8
        0x25, 0x83, 0xE9, 0xCA, 0x2A, 0xD4, 0x4C, 0xE8, 0xDB, 0xBB, 0xC2, 0xDB,
958
8
        0x04, 0xDE, 0x8E, 0xF9, 0x2E, 0x8E, 0xFC, 0x14, 0x1F, 0xBE, 0xCA, 0xA6,
959
8
        0x28, 0x7C, 0x59, 0x47, 0x4E, 0x6B, 0xC0, 0x5D, 0x99, 0xB2, 0x96, 0x4F,
960
8
        0xA0, 0x90, 0xC3, 0xA2, 0x23, 0x3B, 0xA1, 0x86, 0x51, 0x5B, 0xE7, 0xED,
961
8
        0x1F, 0x61, 0x29, 0x70, 0xCE, 0xE2, 0xD7, 0xAF, 0xB8, 0x1B, 0xDD, 0x76,
962
8
        0x21, 0x70, 0x48, 0x1C, 0xD0, 0x06, 0x91, 0x27, 0xD5, 0xB0, 0x5A, 0xA9,
963
8
        0x93, 0xB4, 0xEA, 0x98, 0x8D, 0x8F, 0xDD, 0xC1, 0x86, 0xFF, 0xB7, 0xDC,
964
8
        0x90, 0xA6, 0xC0, 0x8F, 0x4D, 0xF4, 0x35, 0xC9, 0x34, 0x06, 0x31, 0x99,
965
8
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
966
8
    };
967
8
    int ret;
968
969
8
    if(key_state->state == ssh2_NB_state_idle) {
970
8
        key_state->p = NULL; /* SSH2 defined value (p_value) */
971
8
        key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */
972
973
        /* g == 2 */
974
        /* Initialize P and G */
975
8
        if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) {
976
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
977
0
                           "Failed to allocate key state g.");
978
0
            goto clean_exit;
979
0
        }
980
8
        if(ssh2_bn_from_bin(&key_state->p, p_value, 512)) {
981
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
982
0
                           "Failed to allocate key state p.");
983
0
            goto clean_exit;
984
0
        }
985
986
8
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
987
8
                  "Initiating Diffie-Hellman Group16 Key Exchange"));
988
989
8
        key_state->state = ssh2_NB_state_created;
990
8
    }
991
992
8
    ret = kex_diffie_hellman_sha(session, key_state->g, key_state->p, 512,
993
8
                                 SSH2_SHA512_ALG, SSH2_SHA512_DIG_LEN,
994
8
                                 SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY,
995
8
                                 NULL, 0, &key_state->exchange_state);
996
8
    if(ret == LIBSSH2_ERROR_EAGAIN)
997
0
        return ret;
998
999
8
clean_exit:
1000
8
    kex_diffie_hellman_cleanup(session, key_state);
1001
1002
8
    return ret;
1003
8
}
1004
1005
/*
1006
 * Diffie-Hellman Group18 Key Exchange using SHA512
1007
 */
1008
static int kex_method_diffie_hellman_group18_sha512_key_exchange(
1009
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
1010
47
{
1011
47
    static const unsigned char p_value[1024] = {
1012
47
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
1013
47
        0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
1014
47
        0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
1015
47
        0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
1016
47
        0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
1017
47
        0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
1018
47
        0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
1019
47
        0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
1020
47
        0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11,
1021
47
        0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D,
1022
47
        0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05, 0x98, 0xDA, 0x48, 0x36,
1023
47
        0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
1024
47
        0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56,
1025
47
        0x20, 0x85, 0x52, 0xBB, 0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D,
1026
47
        0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04, 0xF1, 0x74, 0x6C, 0x08,
1027
47
        0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B,
1028
47
        0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2,
1029
47
        0xEC, 0x07, 0xA2, 0x8F, 0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9,
1030
47
        0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18, 0x39, 0x95, 0x49, 0x7C,
1031
47
        0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
1032
47
        0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAA, 0xC4, 0x2D, 0xAD, 0x33, 0x17, 0x0D,
1033
47
        0x04, 0x50, 0x7A, 0x33, 0xA8, 0x55, 0x21, 0xAB, 0xDF, 0x1C, 0xBA, 0x64,
1034
47
        0xEC, 0xFB, 0x85, 0x04, 0x58, 0xDB, 0xEF, 0x0A, 0x8A, 0xEA, 0x71, 0x57,
1035
47
        0x5D, 0x06, 0x0C, 0x7D, 0xB3, 0x97, 0x0F, 0x85, 0xA6, 0xE1, 0xE4, 0xC7,
1036
47
        0xAB, 0xF5, 0xAE, 0x8C, 0xDB, 0x09, 0x33, 0xD7, 0x1E, 0x8C, 0x94, 0xE0,
1037
47
        0x4A, 0x25, 0x61, 0x9D, 0xCE, 0xE3, 0xD2, 0x26, 0x1A, 0xD2, 0xEE, 0x6B,
1038
47
        0xF1, 0x2F, 0xFA, 0x06, 0xD9, 0x8A, 0x08, 0x64, 0xD8, 0x76, 0x02, 0x73,
1039
47
        0x3E, 0xC8, 0x6A, 0x64, 0x52, 0x1F, 0x2B, 0x18, 0x17, 0x7B, 0x20, 0x0C,
1040
47
        0xBB, 0xE1, 0x17, 0x57, 0x7A, 0x61, 0x5D, 0x6C, 0x77, 0x09, 0x88, 0xC0,
1041
47
        0xBA, 0xD9, 0x46, 0xE2, 0x08, 0xE2, 0x4F, 0xA0, 0x74, 0xE5, 0xAB, 0x31,
1042
47
        0x43, 0xDB, 0x5B, 0xFC, 0xE0, 0xFD, 0x10, 0x8E, 0x4B, 0x82, 0xD1, 0x20,
1043
47
        0xA9, 0x21, 0x08, 0x01, 0x1A, 0x72, 0x3C, 0x12, 0xA7, 0x87, 0xE6, 0xD7,
1044
47
        0x88, 0x71, 0x9A, 0x10, 0xBD, 0xBA, 0x5B, 0x26, 0x99, 0xC3, 0x27, 0x18,
1045
47
        0x6A, 0xF4, 0xE2, 0x3C, 0x1A, 0x94, 0x68, 0x34, 0xB6, 0x15, 0x0B, 0xDA,
1046
47
        0x25, 0x83, 0xE9, 0xCA, 0x2A, 0xD4, 0x4C, 0xE8, 0xDB, 0xBB, 0xC2, 0xDB,
1047
47
        0x04, 0xDE, 0x8E, 0xF9, 0x2E, 0x8E, 0xFC, 0x14, 0x1F, 0xBE, 0xCA, 0xA6,
1048
47
        0x28, 0x7C, 0x59, 0x47, 0x4E, 0x6B, 0xC0, 0x5D, 0x99, 0xB2, 0x96, 0x4F,
1049
47
        0xA0, 0x90, 0xC3, 0xA2, 0x23, 0x3B, 0xA1, 0x86, 0x51, 0x5B, 0xE7, 0xED,
1050
47
        0x1F, 0x61, 0x29, 0x70, 0xCE, 0xE2, 0xD7, 0xAF, 0xB8, 0x1B, 0xDD, 0x76,
1051
47
        0x21, 0x70, 0x48, 0x1C, 0xD0, 0x06, 0x91, 0x27, 0xD5, 0xB0, 0x5A, 0xA9,
1052
47
        0x93, 0xB4, 0xEA, 0x98, 0x8D, 0x8F, 0xDD, 0xC1, 0x86, 0xFF, 0xB7, 0xDC,
1053
47
        0x90, 0xA6, 0xC0, 0x8F, 0x4D, 0xF4, 0x35, 0xC9, 0x34, 0x02, 0x84, 0x92,
1054
47
        0x36, 0xC3, 0xFA, 0xB4, 0xD2, 0x7C, 0x70, 0x26, 0xC1, 0xD4, 0xDC, 0xB2,
1055
47
        0x60, 0x26, 0x46, 0xDE, 0xC9, 0x75, 0x1E, 0x76, 0x3D, 0xBA, 0x37, 0xBD,
1056
47
        0xF8, 0xFF, 0x94, 0x06, 0xAD, 0x9E, 0x53, 0x0E, 0xE5, 0xDB, 0x38, 0x2F,
1057
47
        0x41, 0x30, 0x01, 0xAE, 0xB0, 0x6A, 0x53, 0xED, 0x90, 0x27, 0xD8, 0x31,
1058
47
        0x17, 0x97, 0x27, 0xB0, 0x86, 0x5A, 0x89, 0x18, 0xDA, 0x3E, 0xDB, 0xEB,
1059
47
        0xCF, 0x9B, 0x14, 0xED, 0x44, 0xCE, 0x6C, 0xBA, 0xCE, 0xD4, 0xBB, 0x1B,
1060
47
        0xDB, 0x7F, 0x14, 0x47, 0xE6, 0xCC, 0x25, 0x4B, 0x33, 0x20, 0x51, 0x51,
1061
47
        0x2B, 0xD7, 0xAF, 0x42, 0x6F, 0xB8, 0xF4, 0x01, 0x37, 0x8C, 0xD2, 0xBF,
1062
47
        0x59, 0x83, 0xCA, 0x01, 0xC6, 0x4B, 0x92, 0xEC, 0xF0, 0x32, 0xEA, 0x15,
1063
47
        0xD1, 0x72, 0x1D, 0x03, 0xF4, 0x82, 0xD7, 0xCE, 0x6E, 0x74, 0xFE, 0xF6,
1064
47
        0xD5, 0x5E, 0x70, 0x2F, 0x46, 0x98, 0x0C, 0x82, 0xB5, 0xA8, 0x40, 0x31,
1065
47
        0x90, 0x0B, 0x1C, 0x9E, 0x59, 0xE7, 0xC9, 0x7F, 0xBE, 0xC7, 0xE8, 0xF3,
1066
47
        0x23, 0xA9, 0x7A, 0x7E, 0x36, 0xCC, 0x88, 0xBE, 0x0F, 0x1D, 0x45, 0xB7,
1067
47
        0xFF, 0x58, 0x5A, 0xC5, 0x4B, 0xD4, 0x07, 0xB2, 0x2B, 0x41, 0x54, 0xAA,
1068
47
        0xCC, 0x8F, 0x6D, 0x7E, 0xBF, 0x48, 0xE1, 0xD8, 0x14, 0xCC, 0x5E, 0xD2,
1069
47
        0x0F, 0x80, 0x37, 0xE0, 0xA7, 0x97, 0x15, 0xEE, 0xF2, 0x9B, 0xE3, 0x28,
1070
47
        0x06, 0xA1, 0xD5, 0x8B, 0xB7, 0xC5, 0xDA, 0x76, 0xF5, 0x50, 0xAA, 0x3D,
1071
47
        0x8A, 0x1F, 0xBF, 0xF0, 0xEB, 0x19, 0xCC, 0xB1, 0xA3, 0x13, 0xD5, 0x5C,
1072
47
        0xDA, 0x56, 0xC9, 0xEC, 0x2E, 0xF2, 0x96, 0x32, 0x38, 0x7F, 0xE8, 0xD7,
1073
47
        0x6E, 0x3C, 0x04, 0x68, 0x04, 0x3E, 0x8F, 0x66, 0x3F, 0x48, 0x60, 0xEE,
1074
47
        0x12, 0xBF, 0x2D, 0x5B, 0x0B, 0x74, 0x74, 0xD6, 0xE6, 0x94, 0xF9, 0x1E,
1075
47
        0x6D, 0xBE, 0x11, 0x59, 0x74, 0xA3, 0x92, 0x6F, 0x12, 0xFE, 0xE5, 0xE4,
1076
47
        0x38, 0x77, 0x7C, 0xB6, 0xA9, 0x32, 0xDF, 0x8C, 0xD8, 0xBE, 0xC4, 0xD0,
1077
47
        0x73, 0xB9, 0x31, 0xBA, 0x3B, 0xC8, 0x32, 0xB6, 0x8D, 0x9D, 0xD3, 0x00,
1078
47
        0x74, 0x1F, 0xA7, 0xBF, 0x8A, 0xFC, 0x47, 0xED, 0x25, 0x76, 0xF6, 0x93,
1079
47
        0x6B, 0xA4, 0x24, 0x66, 0x3A, 0xAB, 0x63, 0x9C, 0x5A, 0xE4, 0xF5, 0x68,
1080
47
        0x34, 0x23, 0xB4, 0x74, 0x2B, 0xF1, 0xC9, 0x78, 0x23, 0x8F, 0x16, 0xCB,
1081
47
        0xE3, 0x9D, 0x65, 0x2D, 0xE3, 0xFD, 0xB8, 0xBE, 0xFC, 0x84, 0x8A, 0xD9,
1082
47
        0x22, 0x22, 0x2E, 0x04, 0xA4, 0x03, 0x7C, 0x07, 0x13, 0xEB, 0x57, 0xA8,
1083
47
        0x1A, 0x23, 0xF0, 0xC7, 0x34, 0x73, 0xFC, 0x64, 0x6C, 0xEA, 0x30, 0x6B,
1084
47
        0x4B, 0xCB, 0xC8, 0x86, 0x2F, 0x83, 0x85, 0xDD, 0xFA, 0x9D, 0x4B, 0x7F,
1085
47
        0xA2, 0xC0, 0x87, 0xE8, 0x79, 0x68, 0x33, 0x03, 0xED, 0x5B, 0xDD, 0x3A,
1086
47
        0x06, 0x2B, 0x3C, 0xF5, 0xB3, 0xA2, 0x78, 0xA6, 0x6D, 0x2A, 0x13, 0xF8,
1087
47
        0x3F, 0x44, 0xF8, 0x2D, 0xDF, 0x31, 0x0E, 0xE0, 0x74, 0xAB, 0x6A, 0x36,
1088
47
        0x45, 0x97, 0xE8, 0x99, 0xA0, 0x25, 0x5D, 0xC1, 0x64, 0xF3, 0x1C, 0xC5,
1089
47
        0x08, 0x46, 0x85, 0x1D, 0xF9, 0xAB, 0x48, 0x19, 0x5D, 0xED, 0x7E, 0xA1,
1090
47
        0xB1, 0xD5, 0x10, 0xBD, 0x7E, 0xE7, 0x4D, 0x73, 0xFA, 0xF3, 0x6B, 0xC3,
1091
47
        0x1E, 0xCF, 0xA2, 0x68, 0x35, 0x90, 0x46, 0xF4, 0xEB, 0x87, 0x9F, 0x92,
1092
47
        0x40, 0x09, 0x43, 0x8B, 0x48, 0x1C, 0x6C, 0xD7, 0x88, 0x9A, 0x00, 0x2E,
1093
47
        0xD5, 0xEE, 0x38, 0x2B, 0xC9, 0x19, 0x0D, 0xA6, 0xFC, 0x02, 0x6E, 0x47,
1094
47
        0x95, 0x58, 0xE4, 0x47, 0x56, 0x77, 0xE9, 0xAA, 0x9E, 0x30, 0x50, 0xE2,
1095
47
        0x76, 0x56, 0x94, 0xDF, 0xC8, 0x1F, 0x56, 0xE8, 0x80, 0xB9, 0x6E, 0x71,
1096
47
        0x60, 0xC9, 0x80, 0xDD, 0x98, 0xED, 0xD3, 0xDF, 0xFF, 0xFF, 0xFF, 0xFF,
1097
47
        0xFF, 0xFF, 0xFF, 0xFF
1098
47
    };
1099
47
    int ret;
1100
1101
47
    if(key_state->state == ssh2_NB_state_idle) {
1102
47
        key_state->p = NULL; /* SSH2 defined value (p_value) */
1103
47
        key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */
1104
1105
        /* g == 2 */
1106
        /* Initialize P and G */
1107
47
        if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) {
1108
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
1109
0
                           "Failed to allocate key state g.");
1110
0
            goto clean_exit;
1111
0
        }
1112
47
        else if(ssh2_bn_from_bin(&key_state->p, p_value, 1024)) {
1113
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
1114
0
                           "Failed to allocate key state p.");
1115
0
            goto clean_exit;
1116
0
        }
1117
1118
47
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
1119
47
                  "Initiating Diffie-Hellman Group18 Key Exchange"));
1120
1121
47
        key_state->state = ssh2_NB_state_created;
1122
47
    }
1123
1124
47
    ret = kex_diffie_hellman_sha(session, key_state->g, key_state->p, 1024,
1125
47
                                 SSH2_SHA512_ALG, SSH2_SHA512_DIG_LEN,
1126
47
                                 SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY,
1127
47
                                 NULL, 0, &key_state->exchange_state);
1128
47
    if(ret == LIBSSH2_ERROR_EAGAIN)
1129
0
        return ret;
1130
1131
47
clean_exit:
1132
47
    kex_diffie_hellman_cleanup(session, key_state);
1133
1134
47
    return ret;
1135
47
}
1136
1137
#ifdef LIBSSH2_KEX_SHA1_ENABLE
1138
/*
1139
 * Diffie-Hellman Group Exchange Key Exchange using SHA1
1140
 * Negotiates random(ish) group for secret derivation
1141
 */
1142
static int kex_method_diffie_hellman_group_exchange_sha1_key_exchange(
1143
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
1144
{
1145
    int ret = 0;
1146
    int rc;
1147
1148
    if(key_state->state == ssh2_NB_state_idle) {
1149
        key_state->p = NULL;
1150
        key_state->g = NULL;
1151
        /* Ask for a P and G pair */
1152
        key_state->request[0] = SSH_MSG_KEX_DH_GEX_REQUEST;
1153
        ssh2_htonu32(key_state->request + 1, SSH2_DH_GEX_MINGROUP);
1154
        ssh2_htonu32(key_state->request + 5, SSH2_DH_GEX_OPTGROUP);
1155
        ssh2_htonu32(key_state->request + 9, SSH2_DH_GEX_MAXGROUP);
1156
        key_state->request_len = 13;
1157
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
1158
                  "Initiating Diffie-Hellman Group-Exchange SHA1"));
1159
1160
        key_state->state = ssh2_NB_state_created;
1161
    }
1162
1163
    if(key_state->state == ssh2_NB_state_created) {
1164
        rc = ssh2_transport_send(session, key_state->request,
1165
                                 key_state->request_len, NULL, 0);
1166
        if(rc == LIBSSH2_ERROR_EAGAIN)
1167
            return rc;
1168
        else if(rc) {
1169
            ret = ssh2_err(session, rc,
1170
                           "Unable to send Group Exchange Request");
1171
            goto dh_gex_clean_exit;
1172
        }
1173
1174
        key_state->state = ssh2_NB_state_sent;
1175
    }
1176
1177
    if(key_state->state == ssh2_NB_state_sent) {
1178
        rc = ssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP,
1179
                                 &key_state->data, &key_state->data_len,
1180
                                 0, NULL, 0, &key_state->req_state);
1181
        if(rc == LIBSSH2_ERROR_EAGAIN)
1182
            return rc;
1183
        else if(rc) {
1184
            ret = ssh2_err(session, rc, "Timeout waiting for GEX_GROUP reply");
1185
            goto dh_gex_clean_exit;
1186
        }
1187
1188
        key_state->state = ssh2_NB_state_sent1;
1189
    }
1190
1191
    if(key_state->state == ssh2_NB_state_sent1) {
1192
        size_t p_len, g_len;
1193
        unsigned char *p, *g;
1194
        struct string_buf buf;
1195
        size_t bits;
1196
1197
        if(key_state->data_len < 9) {
1198
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1199
                           "Unexpected key length DH-SHA1");
1200
            goto dh_gex_clean_exit;
1201
        }
1202
1203
        buf.data = key_state->data;
1204
        buf.dataptr = buf.data;
1205
        buf.len = key_state->data_len;
1206
1207
        buf.dataptr++; /* increment to big num */
1208
1209
        if(ssh2_get_bignum_bytes(&buf, &p, &p_len)) {
1210
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1211
                           "Unexpected value DH-SHA1 p");
1212
            goto dh_gex_clean_exit;
1213
        }
1214
1215
        if(ssh2_get_bignum_bytes(&buf, &g, &g_len)) {
1216
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1217
                           "Unexpected value DH-SHA1 g");
1218
            goto dh_gex_clean_exit;
1219
        }
1220
1221
        if(ssh2_bn_from_bin(&key_state->p, p, p_len)) {
1222
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "Invalid DH-SHA1 p");
1223
            goto dh_gex_clean_exit;
1224
        }
1225
1226
        bits = ssh2_bn_bits(key_state->p);
1227
        if(bits < SSH2_DH_GEX_MINGROUP ||
1228
           bits > SSH2_DH_GEX_MAXGROUP) {
1229
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1230
                           "DH-SHA1 p out of range");
1231
            goto dh_gex_clean_exit;
1232
        }
1233
1234
        if(ssh2_bn_from_bin(&key_state->g, g, g_len)) {
1235
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "Invalid DH-SHA1 g");
1236
            goto dh_gex_clean_exit;
1237
        }
1238
1239
        ret = kex_diffie_hellman_sha(session, key_state->g, key_state->p,
1240
                                     (int)p_len,
1241
                                     SSH2_SHA1_ALG, SSH2_SHA1_DIG_LEN,
1242
                                     SSH_MSG_KEX_DH_GEX_INIT,
1243
                                     SSH_MSG_KEX_DH_GEX_REPLY,
1244
                                     key_state->data + 1,
1245
                                     key_state->data_len - 1,
1246
                                     &key_state->exchange_state);
1247
        if(ret == LIBSSH2_ERROR_EAGAIN)
1248
            return ret;
1249
    }
1250
1251
dh_gex_clean_exit:
1252
    kex_diffie_hellman_cleanup(session, key_state);
1253
1254
    return ret;
1255
}
1256
#endif
1257
1258
/*
1259
 * Diffie-Hellman Group Exchange Key Exchange using SHA256
1260
 * Negotiates random(ish) group for secret derivation
1261
 */
1262
static int kex_method_diffie_hellman_group_exchange_sha256_key_exchange(
1263
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
1264
0
{
1265
0
    int ret = 0;
1266
0
    int rc;
1267
1268
0
    if(key_state->state == ssh2_NB_state_idle) {
1269
0
        key_state->p = NULL;
1270
0
        key_state->g = NULL;
1271
        /* Ask for a P and G pair */
1272
0
        key_state->request[0] = SSH_MSG_KEX_DH_GEX_REQUEST;
1273
0
        ssh2_htonu32(key_state->request + 1, SSH2_DH_GEX_MINGROUP);
1274
0
        ssh2_htonu32(key_state->request + 5, SSH2_DH_GEX_OPTGROUP);
1275
0
        ssh2_htonu32(key_state->request + 9, SSH2_DH_GEX_MAXGROUP);
1276
0
        key_state->request_len = 13;
1277
0
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
1278
0
                  "Initiating Diffie-Hellman Group-Exchange SHA256"));
1279
1280
0
        key_state->state = ssh2_NB_state_created;
1281
0
    }
1282
1283
0
    if(key_state->state == ssh2_NB_state_created) {
1284
0
        rc = ssh2_transport_send(session, key_state->request,
1285
0
                                 key_state->request_len, NULL, 0);
1286
0
        if(rc == LIBSSH2_ERROR_EAGAIN)
1287
0
            return rc;
1288
0
        else if(rc) {
1289
0
            ret = ssh2_err(session, rc,
1290
0
                           "Unable to send Group Exchange Request SHA256");
1291
0
            goto dh_gex_clean_exit;
1292
0
        }
1293
1294
0
        key_state->state = ssh2_NB_state_sent;
1295
0
    }
1296
1297
0
    if(key_state->state == ssh2_NB_state_sent) {
1298
0
        rc = ssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP,
1299
0
                                 &key_state->data, &key_state->data_len,
1300
0
                                 0, NULL, 0, &key_state->req_state);
1301
0
        if(rc == LIBSSH2_ERROR_EAGAIN)
1302
0
            return rc;
1303
0
        else if(rc) {
1304
0
            ret = ssh2_err(session, rc,
1305
0
                           "Timeout waiting for GEX_GROUP reply SHA256");
1306
0
            goto dh_gex_clean_exit;
1307
0
        }
1308
1309
0
        key_state->state = ssh2_NB_state_sent1;
1310
0
    }
1311
1312
0
    if(key_state->state == ssh2_NB_state_sent1) {
1313
0
        unsigned char *p, *g;
1314
0
        size_t p_len, g_len;
1315
0
        struct string_buf buf;
1316
0
        size_t bits;
1317
1318
0
        if(key_state->data_len < 9) {
1319
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1320
0
                           "Unexpected key length DH-SHA256");
1321
0
            goto dh_gex_clean_exit;
1322
0
        }
1323
1324
0
        buf.data = key_state->data;
1325
0
        buf.dataptr = buf.data;
1326
0
        buf.len = key_state->data_len;
1327
1328
0
        buf.dataptr++; /* increment to big num */
1329
1330
0
        if(ssh2_get_bignum_bytes(&buf, &p, &p_len)) {
1331
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1332
0
                           "Unexpected value DH-SHA256 p");
1333
0
            goto dh_gex_clean_exit;
1334
0
        }
1335
1336
0
        if(ssh2_get_bignum_bytes(&buf, &g, &g_len)) {
1337
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1338
0
                           "Unexpected value DH-SHA256 g");
1339
0
            goto dh_gex_clean_exit;
1340
0
        }
1341
1342
0
        if(ssh2_bn_from_bin(&key_state->p, p, p_len)) {
1343
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1344
0
                           "Invalid DH-SHA256 p");
1345
0
            goto dh_gex_clean_exit;
1346
0
        }
1347
1348
0
        bits = ssh2_bn_bits(key_state->p);
1349
0
        if(bits < SSH2_DH_GEX_MINGROUP ||
1350
0
           bits > SSH2_DH_GEX_MAXGROUP) {
1351
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1352
0
                           "DH-SHA256 p out of range");
1353
0
            goto dh_gex_clean_exit;
1354
0
        }
1355
1356
0
        if(ssh2_bn_from_bin(&key_state->g, g, g_len)) {
1357
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1358
0
                           "Invalid DH-SHA256 g");
1359
0
            goto dh_gex_clean_exit;
1360
0
        }
1361
1362
0
        ret = kex_diffie_hellman_sha(session, key_state->g, key_state->p,
1363
0
                                     (int)p_len,
1364
0
                                     SSH2_SHA256_ALG, SSH2_SHA256_DIG_LEN,
1365
0
                                     SSH_MSG_KEX_DH_GEX_INIT,
1366
0
                                     SSH_MSG_KEX_DH_GEX_REPLY,
1367
0
                                     key_state->data + 1,
1368
0
                                     key_state->data_len - 1,
1369
0
                                     &key_state->exchange_state);
1370
0
        if(ret == LIBSSH2_ERROR_EAGAIN)
1371
0
            return ret;
1372
0
    }
1373
1374
0
dh_gex_clean_exit:
1375
0
    kex_diffie_hellman_cleanup(session, key_state);
1376
1377
0
    return ret;
1378
0
}
1379
1380
#if LIBSSH2_ECDSA || LIBSSH2_ED25519
1381
/*
1382
 * Create and verify EC[+PQ hybrid] SHA hash with a given digest size
1383
 *
1384
 * Payload format:
1385
 *
1386
 * string   V_C, client's identification string (CR and LF excluded)
1387
 * string   V_S, server's identification string (CR and LF excluded)
1388
 * string   I_C, payload of the client's SSH_MSG_KEXINIT
1389
 * string   I_S, payload of the server's SSH_MSG_KEXINIT
1390
 * string   K_S, server's public host key
1391
 * string   Q_C, client's ephemeral public key octet string
1392
 * string   Q_S, server's ephemeral public key octet string
1393
 * mpint    K,   shared secret
1394
 */
1395
static int kex_method_ec_sha_hash_create_verify(
1396
    LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state,
1397
    unsigned char *public_key, size_t public_key_len,
1398
    unsigned char *public_pq_key, size_t public_pq_key_len,
1399
    unsigned char *server_public_key, size_t server_public_key_len,
1400
    ssh2_hash_alg hash_alg, size_t digest_len, const char *signerr)
1401
4
{
1402
4
    ssh2_hash_ctx ctx;
1403
4
    int err;
1404
4
    int hok;
1405
1406
4
    hok = ssh2_hash_init(&ctx, hash_alg);
1407
4
    if(!hok)
1408
0
        return ssh2_err(session, LIBSSH2_ERROR_HASH_INIT,
1409
0
                        "Unable to initialize hash context EC/ED");
1410
1411
4
    if(session->local.banner) {
1412
0
        ssh2_htonu32(exchange_state->h_sig_comp,
1413
0
                     (uint32_t)(strlen(session->local.banner) - 2));
1414
0
        hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1415
0
        hok &= ssh2_hash_update(&ctx, session->local.banner,
1416
0
                                strlen(session->local.banner) - 2);
1417
0
    }
1418
4
    else {
1419
4
        ssh2_htonu32(exchange_state->h_sig_comp,
1420
4
                     sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1);
1421
4
        hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1422
4
        hok &= ssh2_hash_update(&ctx, LIBSSH2_SSH_DEFAULT_BANNER,
1423
4
                                sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1);
1424
4
    }
1425
1426
4
    ssh2_htonu32(exchange_state->h_sig_comp,
1427
4
                 (uint32_t)strlen(session->remote.banner));
1428
4
    hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1429
4
    hok &= ssh2_hash_update(&ctx, session->remote.banner,
1430
4
                            strlen(session->remote.banner));
1431
1432
4
    ssh2_htonu32(exchange_state->h_sig_comp,
1433
4
                 (uint32_t)session->local.kexinit_len);
1434
4
    hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1435
4
    hok &= ssh2_hash_update(&ctx, session->local.kexinit,
1436
4
                                  session->local.kexinit_len);
1437
1438
4
    ssh2_htonu32(exchange_state->h_sig_comp,
1439
4
                 (uint32_t)session->remote.kexinit_len);
1440
4
    hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1441
4
    hok &= ssh2_hash_update(&ctx, session->remote.kexinit,
1442
4
                                  session->remote.kexinit_len);
1443
1444
4
    ssh2_htonu32(exchange_state->h_sig_comp, session->server_hostkey_len);
1445
4
    hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1446
4
    hok &= ssh2_hash_update(&ctx, session->server_hostkey,
1447
4
                                  session->server_hostkey_len);
1448
1449
4
    ssh2_htonu32(exchange_state->h_sig_comp,
1450
4
                 (uint32_t)(public_pq_key_len + public_key_len));
1451
4
    hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1452
4
    if(public_pq_key && public_pq_key_len)
1453
0
        hok &= ssh2_hash_update(&ctx, public_pq_key,
1454
4
                                      public_pq_key_len);
1455
4
    hok &= ssh2_hash_update(&ctx, public_key,
1456
4
                                  public_key_len);
1457
1458
4
    ssh2_htonu32(exchange_state->h_sig_comp, (uint32_t)server_public_key_len);
1459
4
    hok &= ssh2_hash_update(&ctx, exchange_state->h_sig_comp, 4);
1460
4
    hok &= ssh2_hash_update(&ctx, server_public_key,
1461
4
                                  server_public_key_len);
1462
1463
4
    hok &= ssh2_hash_update(&ctx, exchange_state->k_value,
1464
4
                                  exchange_state->k_value_len);
1465
1466
4
    hok &= ssh2_hash_final(&ctx, exchange_state->h_sig_comp,
1467
4
                                 sizeof(exchange_state->h_sig_comp));
1468
4
    if(!hok)
1469
0
        return ssh2_err(session, LIBSSH2_ERROR_HASH_CALC,
1470
0
                        "Failed to calculate hash EC/ED");
1471
1472
4
    err = session->hostkey->sig_verify(session,
1473
4
                                       exchange_state->h_sig,
1474
4
                                       exchange_state->h_sig_len,
1475
4
                                       exchange_state->h_sig_comp,
1476
4
                                       digest_len,
1477
4
                                       &session->server_hostkey_abstract);
1478
4
    if(err) {
1479
4
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
1480
4
                  "Failed host key sig_verify() EC/ED: %s: %d",
1481
4
                  session->hostkey->name, err));
1482
4
        return ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_SIGN, signerr);
1483
4
    }
1484
1485
0
    return LIBSSH2_ERROR_NONE;
1486
4
}
1487
#endif /* LIBSSH2_ECDSA || LIBSSH2_ED25519 */
1488
1489
#if LIBSSH2_ECDSA
1490
static void kex_ecdh_exchange_state_cleanup(
1491
    LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state)
1492
4
{
1493
4
    ssh2_bn_free(exchange_state->k);
1494
4
    exchange_state->k = NULL;
1495
1496
4
    if(exchange_state->k_value) {
1497
4
        ssh2_zero_free(session, exchange_state->k_value,
1498
4
                                exchange_state->k_value_len);
1499
4
        exchange_state->k_value = NULL;
1500
4
    }
1501
1502
4
    exchange_state->state = ssh2_NB_state_idle;
1503
4
}
1504
1505
static void kex_method_ecdh_cleanup(LIBSSH2_SESSION *session,
1506
                                    struct key_exchange_state_low *key_state)
1507
8
{
1508
8
    if(key_state->public_key_oct)
1509
4
        SSH2_SAFEFREE(session, key_state->public_key_oct);
1510
1511
8
    if(key_state->private_key) {
1512
4
        ssh2_ecdsa_free(key_state->private_key, session);
1513
4
        key_state->private_key = NULL;
1514
4
    }
1515
1516
8
    if(key_state->data)
1517
4
        SSH2_SAFEFREE(session, key_state->data);
1518
1519
8
    key_state->state = ssh2_NB_state_idle;
1520
1521
8
    if(key_state->exchange_state.state != ssh2_NB_state_idle)
1522
0
        kex_ecdh_exchange_state_cleanup(session, &key_state->exchange_state);
1523
8
}
1524
1525
/*
1526
 * Elliptic Curve Diffie Hellman Key Exchange
1527
 */
1528
static int kex_ecdh_sha2_nistp(LIBSSH2_SESSION *session, ssh2_curve_type curve,
1529
                               unsigned char *data, size_t data_len,
1530
                               unsigned char *public_key,
1531
                               size_t public_key_len, ssh2_ec_key *private_key,
1532
                               struct kmdhgGPshakex_state *exchange_state)
1533
4
{
1534
4
    int ret = 0;
1535
4
    int rc;
1536
1537
4
    ssh2_hash_alg hash_alg;
1538
4
    size_t digest_len = 0;
1539
1540
4
    if(curve == SSH2_EC_CURVE_NISTP256) {
1541
0
        hash_alg = SSH2_SHA256_ALG;
1542
0
        digest_len = SSH2_SHA256_DIG_LEN;
1543
0
    }
1544
4
    else if(curve == SSH2_EC_CURVE_NISTP384) {
1545
0
        hash_alg = SSH2_SHA384_ALG;
1546
0
        digest_len = SSH2_SHA384_DIG_LEN;
1547
0
    }
1548
4
    else if(curve == SSH2_EC_CURVE_NISTP521) {
1549
4
        hash_alg = SSH2_SHA512_ALG;
1550
4
        digest_len = SSH2_SHA512_DIG_LEN;
1551
4
    }
1552
0
    else {
1553
0
        ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
1554
0
                       "Unexpected ecdh-sha2-nistp curve type");
1555
0
        goto clean_exit;
1556
0
    }
1557
1558
4
    if(exchange_state->state == ssh2_NB_state_idle) {
1559
        /* Setup initial values */
1560
4
        exchange_state->k = ssh2_bn_init();
1561
4
        exchange_state->state = ssh2_NB_state_created;
1562
4
    }
1563
1564
4
    if(exchange_state->state == ssh2_NB_state_created) {
1565
        /* parse INIT reply data */
1566
1567
        /* host key K_S */
1568
4
        unsigned char *server_public_key;
1569
4
        size_t server_public_key_len;
1570
4
        struct string_buf buf;
1571
1572
4
        if(!data) {
1573
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1574
0
                           "Missing host key data");
1575
0
            goto clean_exit;
1576
0
        }
1577
1578
4
        ret = kex_proc_hostkey(session, &buf, exchange_state, data, data_len);
1579
4
        if(ret)
1580
0
            goto clean_exit;
1581
1582
        /* server public key Q_S */
1583
4
        if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) {
1584
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1585
0
                           "Unexpected ECDH server public key");
1586
0
            goto clean_exit;
1587
0
        }
1588
1589
        /* server signature */
1590
4
        if(ssh2_get_string(&buf, &exchange_state->h_sig,
1591
4
                           &exchange_state->h_sig_len)) {
1592
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
1593
0
                           "Unexpected ECDH server sig length");
1594
0
            goto clean_exit;
1595
0
        }
1596
1597
        /* Compute the shared secret K */
1598
4
        rc = ssh2_ecdh_gen_k(&exchange_state->k, session, private_key,
1599
4
                             server_public_key, server_public_key_len);
1600
4
        if(rc) {
1601
0
            ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
1602
0
                           "Unable to create ECDH shared secret");
1603
0
            goto clean_exit;
1604
0
        }
1605
1606
4
        exchange_state->k_value_len = ssh2_bn_bytes(exchange_state->k) + 5;
1607
4
        if(ssh2_bn_bits(exchange_state->k) % 8)
1608
3
            exchange_state->k_value_len--;  /* do not need leading 00 */
1609
4
        exchange_state->k_value =
1610
4
            SSH2_ALLOC(session, exchange_state->k_value_len);
1611
4
        if(!exchange_state->k_value) {
1612
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
1613
0
                           "Unable to allocate buffer for ECDH K");
1614
0
            goto clean_exit;
1615
0
        }
1616
4
        ssh2_htonu32(exchange_state->k_value,
1617
4
                     (uint32_t)(exchange_state->k_value_len - 4));
1618
4
        if(ssh2_bn_bits(exchange_state->k) % 8) {
1619
3
            if(ssh2_bn_to_bin(exchange_state->k,
1620
3
                              exchange_state->k_value + 4)) {
1621
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
1622
0
                               "Cannot write exchange_state->k");
1623
0
                goto clean_exit;
1624
0
            }
1625
3
        }
1626
1
        else {
1627
1
            exchange_state->k_value[4] = 0;
1628
1
            if(ssh2_bn_to_bin(exchange_state->k,
1629
1
                              exchange_state->k_value + 5)) {
1630
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
1631
0
                               "Cannot write exchange_state->k");
1632
0
                goto clean_exit;
1633
0
            }
1634
1
        }
1635
1636
        /* verify hash */
1637
4
        ret = kex_method_ec_sha_hash_create_verify(session, exchange_state,
1638
4
                 public_key, public_key_len, NULL, 0,
1639
4
                 server_public_key, server_public_key_len,
1640
4
                 hash_alg, digest_len,
1641
4
                 "Unable to verify host key signature ECDH");
1642
4
        if(ret)
1643
4
            goto clean_exit;
1644
1645
0
        exchange_state->c = SSH_MSG_NEWKEYS;
1646
0
        exchange_state->state = ssh2_NB_state_sent;
1647
0
    }
1648
1649
0
    if(exchange_state->state == ssh2_NB_state_sent) {
1650
0
        rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0);
1651
0
        if(rc == LIBSSH2_ERROR_EAGAIN)
1652
0
            return rc;
1653
0
        else if(rc) {
1654
0
            ret = ssh2_err(session, rc, "Unable to send NEWKEYS message ECDH");
1655
0
            goto clean_exit;
1656
0
        }
1657
1658
0
        exchange_state->state = ssh2_NB_state_sent2;
1659
0
    }
1660
1661
0
    if(exchange_state->state == ssh2_NB_state_sent2) {
1662
0
        ret = kex_finish(session, exchange_state, hash_alg, digest_len);
1663
0
        if(ret == LIBSSH2_ERROR_EAGAIN)
1664
0
            return ret;
1665
0
    }
1666
1667
4
clean_exit:
1668
4
    kex_ecdh_exchange_state_cleanup(session, exchange_state);
1669
1670
4
    return ret;
1671
0
}
1672
1673
/*
1674
 * returns the EC curve type by name used in key exchange
1675
 */
1676
static int kex_session_curve_type(const char *name,
1677
                                  ssh2_curve_type *out_curve)
1678
8
{
1679
8
    ssh2_curve_type curve;
1680
1681
8
    if(!name)
1682
0
        return -1;
1683
1684
8
    if(!strcmp(name, "mlkem768nistp256-sha256") ||
1685
8
       !strcmp(name, "ecdh-sha2-nistp256"))
1686
0
        curve = SSH2_EC_CURVE_NISTP256;
1687
8
    else if(!strcmp(name, "mlkem1024nistp384-sha384") ||
1688
8
            !strcmp(name, "ecdh-sha2-nistp384"))
1689
0
        curve = SSH2_EC_CURVE_NISTP384;
1690
8
    else if(!strcmp(name, "ecdh-sha2-nistp521"))
1691
8
        curve = SSH2_EC_CURVE_NISTP521;
1692
0
    else
1693
0
        return -1;
1694
1695
8
    if(out_curve)
1696
8
        *out_curve = curve;
1697
1698
8
    return 0;
1699
8
}
1700
1701
/*
1702
 * Elliptic Curve Diffie Hellman Key Exchange
1703
 * supports SHA256/384/512 hashes based on negotiated ecdh method
1704
 */
1705
static int kex_method_ecdh_key_exchange(
1706
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
1707
4
{
1708
4
    int ret = 0;
1709
4
    int rc = 0;
1710
4
    unsigned char *s;
1711
4
    ssh2_curve_type curve;
1712
1713
4
    if(key_state->state == ssh2_NB_state_idle) {
1714
4
        key_state->public_key_oct = NULL;
1715
4
        key_state->state = ssh2_NB_state_created;
1716
4
    }
1717
1718
4
    if(key_state->state == ssh2_NB_state_created) {
1719
4
        rc = kex_session_curve_type(session->kex->name, &curve);
1720
4
        if(rc) {
1721
0
            ret = ssh2_err(session, -1, "Unrecognized KEX nistp curve type");
1722
0
            goto ecdh_clean_exit;
1723
0
        }
1724
1725
4
        rc = ssh2_ecdsa_create_key(&key_state->private_key, session,
1726
4
                                   &key_state->public_key_oct,
1727
4
                                   &key_state->public_key_oct_len, curve);
1728
4
        if(rc) {
1729
0
            ret = ssh2_err(session, rc, "Unable to create private key");
1730
0
            goto ecdh_clean_exit;
1731
0
        }
1732
1733
4
        key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT;
1734
4
        s = key_state->request + 1;
1735
4
        ssh2_store_str(&s, key_state->public_key_oct,
1736
4
                           key_state->public_key_oct_len);
1737
4
        key_state->request_len = key_state->public_key_oct_len + 5;
1738
1739
4
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
1740
4
                  "Initiating ECDH SHA2 NISTP256"));
1741
1742
4
        key_state->state = ssh2_NB_state_sent;
1743
4
    }
1744
1745
4
    if(key_state->state == ssh2_NB_state_sent) {
1746
4
        rc = ssh2_transport_send(session, key_state->request,
1747
4
                                 key_state->request_len, NULL, 0);
1748
4
        if(rc == LIBSSH2_ERROR_EAGAIN)
1749
0
            return rc;
1750
4
        else if(rc) {
1751
0
            ret = ssh2_err(session, rc, "Unable to send ECDH_INIT");
1752
0
            goto ecdh_clean_exit;
1753
0
        }
1754
1755
4
        key_state->state = ssh2_NB_state_sent1;
1756
4
    }
1757
1758
4
    if(key_state->state == ssh2_NB_state_sent1) {
1759
4
        rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY,
1760
4
                                 &key_state->data, &key_state->data_len,
1761
4
                                 0, NULL, 0, &key_state->req_state);
1762
4
        if(rc == LIBSSH2_ERROR_EAGAIN)
1763
0
            return rc;
1764
4
        else if(rc) {
1765
0
            ret = ssh2_err(session, rc,
1766
0
                           "Timeout waiting for ECDH_REPLY reply");
1767
0
            goto ecdh_clean_exit;
1768
0
        }
1769
1770
4
        key_state->state = ssh2_NB_state_sent2;
1771
4
    }
1772
1773
4
    if(key_state->state == ssh2_NB_state_sent2) {
1774
4
        rc = kex_session_curve_type(session->kex->name, &curve);
1775
4
        if(rc) {
1776
0
            ret = ssh2_err(session, -1, "Unrecognized KEX nistp curve type");
1777
0
            goto ecdh_clean_exit;
1778
0
        }
1779
1780
4
        ret = kex_ecdh_sha2_nistp(session, curve, key_state->data,
1781
4
                                  key_state->data_len,
1782
4
                                  key_state->public_key_oct,
1783
4
                                  key_state->public_key_oct_len,
1784
4
                                  key_state->private_key,
1785
4
                                  &key_state->exchange_state);
1786
4
        if(ret == LIBSSH2_ERROR_EAGAIN)
1787
0
            return ret;
1788
4
    }
1789
1790
4
ecdh_clean_exit:
1791
1792
4
    kex_method_ecdh_cleanup(session, key_state);
1793
1794
4
    return ret;
1795
4
}
1796
1797
#if LIBSSH2_MLKEM
1798
static void kex_mlkem_nistp_exchange_state_cleanup(
1799
    LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state)
1800
{
1801
    ssh2_bn_free(exchange_state->k);
1802
    exchange_state->k = NULL;
1803
1804
    if(exchange_state->k_value) {
1805
        ssh2_zero_free(session, exchange_state->k_value,
1806
                                exchange_state->k_value_len);
1807
        exchange_state->k_value = NULL;
1808
    }
1809
1810
    exchange_state->state = ssh2_NB_state_idle;
1811
}
1812
1813
static void kex_method_mlkem_nistp_cleanup(
1814
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
1815
{
1816
    if(key_state->public_key_oct)
1817
        SSH2_SAFEFREE(session, key_state->public_key_oct);
1818
1819
    if(key_state->private_key) {
1820
        ssh2_ecdsa_free(key_state->private_key, session);
1821
        key_state->private_key = NULL;
1822
    }
1823
1824
    ssh2_zero_free(session,
1825
                   key_state->mlkem_public_key,
1826
                   key_state->mlkem_public_key_len);
1827
    key_state->mlkem_public_key = NULL;
1828
    key_state->mlkem_public_key_len = 0;
1829
1830
    ssh2_zero_free(session,
1831
                   key_state->mlkem_private_key,
1832
                   key_state->mlkem_private_key_len);
1833
    key_state->mlkem_private_key = NULL;
1834
    key_state->mlkem_private_key_len = 0;
1835
1836
    if(key_state->data)
1837
        SSH2_SAFEFREE(session, key_state->data);
1838
1839
    key_state->state = ssh2_NB_state_idle;
1840
1841
    if(key_state->exchange_state.state != ssh2_NB_state_idle)
1842
        kex_ecdh_exchange_state_cleanup(session, &key_state->exchange_state);
1843
}
1844
1845
static int kex_mlkem_nistp(LIBSSH2_SESSION *session,
1846
                           unsigned char *data, size_t data_len,
1847
                           unsigned char *public_t_key,
1848
                           size_t public_t_key_len,
1849
                           ssh2_ec_key *private_t_key,
1850
                           unsigned char *public_pq_key,
1851
                           unsigned char *private_pq_key,
1852
                           struct kmdhgGPshakex_state *exchange_state)
1853
{
1854
    int ret = 0;
1855
    int rc, mlkem_size;
1856
    ssh2_hash_alg hash_alg;
1857
    ssh2_curve_type curve;
1858
    size_t digest_len, mlkem_cipher_len, public_pq_key_len;
1859
    unsigned char *shared_secret = NULL;
1860
1861
    if(kex_session_curve_type(session->kex->name, &curve)) {
1862
        ret = ssh2_err(session, -1,
1863
                       "Unrecognized KEX hybrid nistp curve type");
1864
        goto clean_exit;
1865
    }
1866
1867
    switch(curve) {
1868
    case SSH2_EC_CURVE_NISTP256:
1869
        hash_alg = SSH2_SHA256_ALG;
1870
        digest_len = SSH2_SHA256_DIG_LEN;
1871
        mlkem_cipher_len = SSH2_MLKEM_768_CIPHERTEXT;
1872
        mlkem_size = 768;
1873
        public_pq_key_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN;
1874
        break;
1875
    case SSH2_EC_CURVE_NISTP384:
1876
        hash_alg = SSH2_SHA384_ALG;
1877
        digest_len = SSH2_SHA384_DIG_LEN;
1878
        mlkem_cipher_len = SSH2_MLKEM_1024_CIPHERTEXT;
1879
        mlkem_size = 1024;
1880
        public_pq_key_len = SSH2_MLKEM_1024_PUBLIC_KEY_LEN;
1881
        break;
1882
    default:
1883
        ret = ssh2_err(session, -1, "Unexpected KEX hybrid nistp curve type");
1884
        goto clean_exit;
1885
    }
1886
1887
    if(exchange_state->state == ssh2_NB_state_idle) {
1888
        /* Setup initial values */
1889
        exchange_state->k = ssh2_bn_init();
1890
        exchange_state->state = ssh2_NB_state_created;
1891
    }
1892
1893
    if(exchange_state->state == ssh2_NB_state_created) {
1894
        /* parse INIT reply data */
1895
        unsigned char *server_public_key;
1896
        size_t shared_secret_len;
1897
        size_t server_public_key_len;
1898
        struct string_buf buf;
1899
1900
        if(!data) {
1901
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1902
                           "Missing host key data");
1903
            goto clean_exit;
1904
        }
1905
1906
        ret = kex_proc_hostkey(session, &buf, exchange_state, data, data_len);
1907
        if(ret)
1908
            goto clean_exit;
1909
1910
        /* server public key Q_S */
1911
        if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) {
1912
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
1913
                           "Unexpected mlkemnistp server public key");
1914
            goto clean_exit;
1915
        }
1916
1917
        if(server_public_key_len <= mlkem_cipher_len) {
1918
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
1919
                           "Unexpected mlkemnistp server public key length");
1920
            goto clean_exit;
1921
        }
1922
1923
        /* server signature */
1924
        if(ssh2_get_string(&buf, &exchange_state->h_sig,
1925
                           &exchange_state->h_sig_len)) {
1926
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
1927
                           "Unexpected mlkemnistp server sig length");
1928
            goto clean_exit;
1929
        }
1930
1931
        exchange_state->k_value_len = digest_len + 4;
1932
1933
        exchange_state->k_value =
1934
            SSH2_ALLOC(session, exchange_state->k_value_len);
1935
        if(!exchange_state->k_value) {
1936
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
1937
                           "Unable to allocate buffer for K");
1938
            goto clean_exit;
1939
        }
1940
        ssh2_htonu32(exchange_state->k_value, (uint32_t)digest_len);
1941
1942
        /* Compute the ecdh shared secret K */
1943
        rc = ssh2_ecdh_gen_k(&exchange_state->k, session, private_t_key,
1944
                             server_public_key + mlkem_cipher_len,
1945
                             server_public_key_len - mlkem_cipher_len);
1946
        if(rc) {
1947
            ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
1948
                           "Unable to create ECDH shared secret");
1949
            goto clean_exit;
1950
        }
1951
1952
        shared_secret_len = SSH2_MLKEM_SHARED_SECRET_LEN +
1953
                            ssh2_bn_bytes(exchange_state->k);
1954
        shared_secret = SSH2_ALLOC(session, shared_secret_len);
1955
        if(!shared_secret) {
1956
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
1957
                           "Unable to allocate buffer for "
1958
                           "mlkemnistp shared secret");
1959
            goto clean_exit;
1960
        }
1961
1962
        /* Compute the ML-KEM shared secret */
1963
        rc = ssh2_mlkem_get_sk(shared_secret, mlkem_size, private_pq_key,
1964
                               server_public_key);
1965
        if(rc) {
1966
            ssh2_explicit_zero(shared_secret, shared_secret_len);
1967
            ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
1968
                           "Unable to create mlkem shared secret");
1969
            goto clean_exit;
1970
        }
1971
1972
        if(ssh2_bn_to_bin(exchange_state->k,
1973
                          shared_secret + SSH2_MLKEM_SHARED_SECRET_LEN)) {
1974
            ssh2_explicit_zero(shared_secret, shared_secret_len);
1975
            ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
1976
                           "Cannot write shared secret");
1977
            goto clean_exit;
1978
        }
1979
1980
        /* verify hash */
1981
        rc = ssh2_hash(hash_alg, shared_secret, shared_secret_len,
1982
                       exchange_state->k_value + 4, digest_len);
1983
        ssh2_explicit_zero(shared_secret, shared_secret_len);
1984
        if(!rc) {
1985
            ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC,
1986
                           "kex: failed to calculate hash");
1987
            goto clean_exit;
1988
        }
1989
1990
        ret = kex_method_ec_sha_hash_create_verify(session, exchange_state,
1991
                 public_t_key, public_t_key_len,
1992
                 public_pq_key, public_pq_key_len,
1993
                 server_public_key, server_public_key_len,
1994
                 hash_alg, digest_len,
1995
                 "Unable to verify host key signature mlkemnistp");
1996
        if(ret)
1997
            goto clean_exit;
1998
1999
        exchange_state->c = SSH_MSG_NEWKEYS;
2000
        exchange_state->state = ssh2_NB_state_sent;
2001
    }
2002
2003
    if(exchange_state->state == ssh2_NB_state_sent) {
2004
        rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0);
2005
        if(rc == LIBSSH2_ERROR_EAGAIN) {
2006
            ret = rc;
2007
            goto clean_free;
2008
        }
2009
        else if(rc) {
2010
            ret = ssh2_err(session, rc,
2011
                           "Unable to send NEWKEYS message mlkemnistp");
2012
            goto clean_exit;
2013
        }
2014
2015
        exchange_state->state = ssh2_NB_state_sent2;
2016
    }
2017
2018
    if(exchange_state->state == ssh2_NB_state_sent2) {
2019
        ret = kex_finish(session, exchange_state, hash_alg, digest_len);
2020
        if(ret == LIBSSH2_ERROR_EAGAIN)
2021
            goto clean_free;
2022
    }
2023
2024
clean_exit:
2025
    kex_mlkem_nistp_exchange_state_cleanup(session, exchange_state);
2026
2027
clean_free:
2028
    if(shared_secret)
2029
        SSH2_FREE(session, shared_secret);
2030
2031
    return ret;
2032
}
2033
2034
static int kex_method_mlkem_nistp_key_exchange(
2035
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
2036
{
2037
    int ret = 0;
2038
    int rc = 0;
2039
2040
    if(key_state->state == ssh2_NB_state_idle) {
2041
        key_state->public_key_oct = NULL;
2042
        key_state->state = ssh2_NB_state_created;
2043
    }
2044
2045
    if(key_state->state == ssh2_NB_state_created) {
2046
        ssh2_curve_type curve;
2047
        int mlkem_size;
2048
        size_t mlkem_public_key_len;
2049
        size_t mlkem_private_key_len;
2050
        unsigned char *s = NULL;
2051
2052
        if(kex_session_curve_type(session->kex->name, &curve)) {
2053
            ret = ssh2_err(session, -1,
2054
                           "Unrecognized KEX hybrid nistp curve type");
2055
            goto clean_exit;
2056
        }
2057
2058
        switch(curve) {
2059
        case SSH2_EC_CURVE_NISTP256:
2060
            mlkem_size = 768;
2061
            mlkem_public_key_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN;
2062
            mlkem_private_key_len = SSH2_MLKEM_768_PRIVATE_KEY_LEN;
2063
            break;
2064
        case SSH2_EC_CURVE_NISTP384:
2065
            mlkem_size = 1024;
2066
            mlkem_public_key_len = SSH2_MLKEM_1024_PUBLIC_KEY_LEN;
2067
            mlkem_private_key_len = SSH2_MLKEM_1024_PRIVATE_KEY_LEN;
2068
            break;
2069
        default:
2070
            ret = ssh2_err(session, -1,
2071
                           "Unexpected KEX hybrid nistp curve type");
2072
            goto clean_exit;
2073
        }
2074
2075
        rc = ssh2_ecdsa_create_key(&key_state->private_key, session,
2076
                                   &key_state->public_key_oct,
2077
                                   &key_state->public_key_oct_len, curve);
2078
        if(rc) {
2079
            ret = ssh2_err(session, rc, "Unable to create ecdh private key");
2080
            goto clean_exit;
2081
        }
2082
2083
        rc = ssh2_mlkem_new(session, mlkem_size,
2084
                            &key_state->mlkem_public_key,
2085
                            &key_state->mlkem_private_key);
2086
        if(rc) {
2087
            ret = ssh2_err(session, rc, "Unable to create mlkem private key");
2088
            goto clean_exit;
2089
        }
2090
2091
        key_state->mlkem_public_key_len = mlkem_public_key_len;
2092
        key_state->mlkem_private_key_len = mlkem_private_key_len;
2093
2094
        key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT;
2095
        s = key_state->request + 1;
2096
        ssh2_store_hybrid_str(&s, key_state->mlkem_public_key,
2097
                                  mlkem_public_key_len,
2098
                                  key_state->public_key_oct,
2099
                                  key_state->public_key_oct_len);
2100
        key_state->request_len = mlkem_public_key_len +
2101
                                 key_state->public_key_oct_len + 5;
2102
2103
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
2104
                  "Initiating mlkem-nistp hybrid SHA2"));
2105
2106
        key_state->state = ssh2_NB_state_sent;
2107
    }
2108
2109
    if(key_state->state == ssh2_NB_state_sent) {
2110
        rc = ssh2_transport_send(session, key_state->request,
2111
                                 key_state->request_len, NULL, 0);
2112
        if(rc == LIBSSH2_ERROR_EAGAIN)
2113
            return rc;
2114
        if(rc) {
2115
            ret = ssh2_err(session, rc, "Unable to send ECDH_INIT");
2116
            goto clean_exit;
2117
        }
2118
2119
        key_state->state = ssh2_NB_state_sent1;
2120
    }
2121
2122
    if(key_state->state == ssh2_NB_state_sent1) {
2123
        rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY,
2124
                                 &key_state->data, &key_state->data_len,
2125
                                 0, NULL, 0, &key_state->req_state);
2126
        if(rc == LIBSSH2_ERROR_EAGAIN)
2127
            return rc;
2128
        if(rc) {
2129
            ret = ssh2_err(session, rc,
2130
                           "Timeout waiting for ECDH_REPLY reply");
2131
            goto clean_exit;
2132
        }
2133
2134
        key_state->state = ssh2_NB_state_sent2;
2135
    }
2136
2137
    if(key_state->state == ssh2_NB_state_sent2) {
2138
        ret = kex_mlkem_nistp(session, key_state->data, key_state->data_len,
2139
                              key_state->public_key_oct,
2140
                              key_state->public_key_oct_len,
2141
                              key_state->private_key,
2142
                              key_state->mlkem_public_key,
2143
                              key_state->mlkem_private_key,
2144
                              &key_state->exchange_state);
2145
        if(ret == LIBSSH2_ERROR_EAGAIN)
2146
            return ret;
2147
    }
2148
2149
clean_exit:
2150
2151
    kex_method_mlkem_nistp_cleanup(session, key_state);
2152
2153
    return ret;
2154
}
2155
#endif /* LIBSSH2_MLKEM */
2156
#endif /* LIBSSH2_ECDSA */
2157
2158
#if LIBSSH2_ED25519
2159
static void kex_curve25519_exchange_state_cleanup(
2160
    LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state)
2161
0
{
2162
0
    ssh2_bn_free(exchange_state->k);
2163
0
    exchange_state->k = NULL;
2164
2165
0
    if(exchange_state->k_value) {
2166
0
        ssh2_zero_free(session, exchange_state->k_value,
2167
0
                                exchange_state->k_value_len);
2168
0
        exchange_state->k_value = NULL;
2169
0
    }
2170
2171
0
    exchange_state->state = ssh2_NB_state_idle;
2172
0
}
2173
2174
static void kex_method_curve25519_cleanup(
2175
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
2176
0
{
2177
0
    ssh2_zero_free(session,
2178
0
                   key_state->curve25519_public_key, SSH2_ED25519_KEY_LEN);
2179
0
    key_state->curve25519_public_key = NULL;
2180
2181
0
    ssh2_zero_free(session,
2182
0
                   key_state->curve25519_private_key, SSH2_ED25519_KEY_LEN);
2183
0
    key_state->curve25519_private_key = NULL;
2184
2185
0
    if(key_state->data)
2186
0
        SSH2_SAFEFREE(session, key_state->data);
2187
2188
0
    key_state->state = ssh2_NB_state_idle;
2189
2190
0
    if(key_state->exchange_state.state != ssh2_NB_state_idle)
2191
0
        kex_curve25519_exchange_state_cleanup(session,
2192
0
                                              &key_state->exchange_state);
2193
0
}
2194
2195
/*
2196
 * Elliptic Curve Key Exchange
2197
 */
2198
static int kex_curve25519_sha256(
2199
    LIBSSH2_SESSION *session,
2200
    unsigned char *data, size_t data_len,
2201
    unsigned char public_key[SSH2_ED25519_KEY_LEN],
2202
    unsigned char private_key[SSH2_ED25519_KEY_LEN],
2203
    struct kmdhgGPshakex_state *exchange_state)
2204
0
{
2205
0
    int ret = 0;
2206
0
    int rc;
2207
2208
0
    if(exchange_state->state == ssh2_NB_state_idle) {
2209
        /* Setup initial values */
2210
0
        exchange_state->k = ssh2_bn_init();
2211
0
        exchange_state->state = ssh2_NB_state_created;
2212
0
    }
2213
2214
0
    if(exchange_state->state == ssh2_NB_state_created) {
2215
        /* parse INIT reply data */
2216
0
        unsigned char *server_public_key;
2217
0
        size_t server_public_key_len;
2218
0
        size_t public_key_len = SSH2_ED25519_KEY_LEN;
2219
0
        struct string_buf buf;
2220
2221
0
        if(!data) {
2222
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
2223
0
                           "Missing host key data");
2224
0
            goto clean_exit;
2225
0
        }
2226
2227
0
        ret = kex_proc_hostkey(session, &buf, exchange_state, data, data_len);
2228
0
        if(ret)
2229
0
            goto clean_exit;
2230
2231
        /* server public key Q_S */
2232
0
        if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) {
2233
0
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
2234
0
                           "Unexpected curve25519 server public key");
2235
0
            goto clean_exit;
2236
0
        }
2237
2238
0
        if(server_public_key_len != public_key_len) {
2239
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
2240
0
                           "Unexpected curve25519 server public key length");
2241
0
            goto clean_exit;
2242
0
        }
2243
2244
        /* server signature */
2245
0
        if(ssh2_get_string(&buf, &exchange_state->h_sig,
2246
0
                           &exchange_state->h_sig_len)) {
2247
0
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
2248
0
                           "Unexpected curve25519 server sig length");
2249
0
            goto clean_exit;
2250
0
        }
2251
2252
        /* Compute the shared secret K */
2253
0
        rc = ssh2_curve25519_gen_k(&exchange_state->k, private_key,
2254
0
                                   server_public_key);
2255
0
        if(rc) {
2256
0
            ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
2257
0
                           "Unable to create curve25519 shared secret");
2258
0
            goto clean_exit;
2259
0
        }
2260
2261
0
        exchange_state->k_value_len = ssh2_bn_bytes(exchange_state->k) + 5;
2262
0
        if(ssh2_bn_bits(exchange_state->k) % 8)
2263
0
            exchange_state->k_value_len--;  /* do not need leading 00 */
2264
0
        exchange_state->k_value =
2265
0
            SSH2_ALLOC(session, exchange_state->k_value_len);
2266
0
        if(!exchange_state->k_value) {
2267
0
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
2268
0
                           "Unable to allocate buffer for K");
2269
0
            goto clean_exit;
2270
0
        }
2271
0
        ssh2_htonu32(exchange_state->k_value,
2272
0
                     (uint32_t)(exchange_state->k_value_len - 4));
2273
0
        if(ssh2_bn_bits(exchange_state->k) % 8) {
2274
0
            if(ssh2_bn_to_bin(exchange_state->k,
2275
0
                              exchange_state->k_value + 4)) {
2276
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
2277
0
                               "Cannot write exchange_state->k");
2278
0
                goto clean_exit;
2279
0
            }
2280
0
        }
2281
0
        else {
2282
0
            exchange_state->k_value[4] = 0;
2283
0
            if(ssh2_bn_to_bin(exchange_state->k,
2284
0
                              exchange_state->k_value + 5)) {
2285
0
                ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
2286
0
                               "Cannot write exchange_state->k");
2287
0
                goto clean_exit;
2288
0
            }
2289
0
        }
2290
2291
        /* verify hash */
2292
0
        ret = kex_method_ec_sha_hash_create_verify(session, exchange_state,
2293
0
                 public_key, public_key_len, NULL, 0,
2294
0
                 server_public_key, server_public_key_len,
2295
0
                 SSH2_SHA256_ALG, SSH2_SHA256_DIG_LEN,
2296
0
                 "Unable to verify host key signature curve25519");
2297
0
        if(ret)
2298
0
            goto clean_exit;
2299
2300
0
        exchange_state->c = SSH_MSG_NEWKEYS;
2301
0
        exchange_state->state = ssh2_NB_state_sent;
2302
0
    }
2303
2304
0
    if(exchange_state->state == ssh2_NB_state_sent) {
2305
0
        rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0);
2306
0
        if(rc == LIBSSH2_ERROR_EAGAIN)
2307
0
            return rc;
2308
0
        else if(rc) {
2309
0
            ret = ssh2_err(session, rc,
2310
0
                           "Unable to send NEWKEYS message curve25519");
2311
0
            goto clean_exit;
2312
0
        }
2313
2314
0
        exchange_state->state = ssh2_NB_state_sent2;
2315
0
    }
2316
2317
0
    if(exchange_state->state == ssh2_NB_state_sent2) {
2318
0
        ret = kex_finish(session, exchange_state,
2319
0
                         SSH2_SHA256_ALG, SSH2_SHA256_DIG_LEN);
2320
0
        if(ret == LIBSSH2_ERROR_EAGAIN)
2321
0
            return ret;
2322
0
    }
2323
2324
0
clean_exit:
2325
0
    kex_curve25519_exchange_state_cleanup(session, exchange_state);
2326
2327
0
    return ret;
2328
0
}
2329
2330
/*
2331
 * Elliptic Curve X25519 Key Exchange with SHA256 hash
2332
 */
2333
static int kex_method_curve25519_key_exchange(
2334
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
2335
0
{
2336
0
    int ret = 0;
2337
0
    int rc = 0;
2338
2339
0
    if(key_state->state == ssh2_NB_state_idle) {
2340
0
        key_state->public_key_oct = NULL;
2341
0
        key_state->state = ssh2_NB_state_created;
2342
0
    }
2343
2344
0
    if(key_state->state == ssh2_NB_state_created) {
2345
0
        unsigned char *s = NULL;
2346
2347
0
        rc = strcmp(session->kex->name, "curve25519-sha256@libssh.org");
2348
0
        if(rc)
2349
0
            rc = strcmp(session->kex->name, "curve25519-sha256");
2350
2351
0
        if(rc) {
2352
0
            ret = ssh2_err(session, -1,
2353
0
                           "Unrecognized KEX curve25519 curve type");
2354
0
            goto clean_exit;
2355
0
        }
2356
2357
0
        rc = ssh2_curve25519_new(session,
2358
0
                                 &key_state->curve25519_public_key,
2359
0
                                 &key_state->curve25519_private_key);
2360
0
        if(rc) {
2361
0
            ret = ssh2_err(session, rc, "Unable to create private key");
2362
0
            goto clean_exit;
2363
0
        }
2364
2365
0
        key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT;
2366
0
        s = key_state->request + 1;
2367
0
        ssh2_store_str(&s, key_state->curve25519_public_key,
2368
0
                           SSH2_ED25519_KEY_LEN);
2369
0
        key_state->request_len = SSH2_ED25519_KEY_LEN + 5;
2370
2371
0
        ssh2_deb((session, LIBSSH2_TRACE_KEX, "Initiating curve25519 SHA2"));
2372
2373
0
        key_state->state = ssh2_NB_state_sent;
2374
0
    }
2375
2376
0
    if(key_state->state == ssh2_NB_state_sent) {
2377
0
        rc = ssh2_transport_send(session, key_state->request,
2378
0
                                 key_state->request_len, NULL, 0);
2379
0
        if(rc == LIBSSH2_ERROR_EAGAIN)
2380
0
            return rc;
2381
0
        else if(rc) {
2382
0
            ret = ssh2_err(session, rc, "Unable to send ECDH_INIT");
2383
0
            goto clean_exit;
2384
0
        }
2385
2386
0
        key_state->state = ssh2_NB_state_sent1;
2387
0
    }
2388
2389
0
    if(key_state->state == ssh2_NB_state_sent1) {
2390
0
        rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY,
2391
0
                                 &key_state->data, &key_state->data_len,
2392
0
                                 0, NULL, 0, &key_state->req_state);
2393
0
        if(rc == LIBSSH2_ERROR_EAGAIN)
2394
0
            return rc;
2395
0
        else if(rc) {
2396
0
            ret = ssh2_err(session, rc,
2397
0
                           "Timeout waiting for ECDH_REPLY reply");
2398
0
            goto clean_exit;
2399
0
        }
2400
2401
0
        key_state->state = ssh2_NB_state_sent2;
2402
0
    }
2403
2404
0
    if(key_state->state == ssh2_NB_state_sent2) {
2405
0
        ret = kex_curve25519_sha256(session,
2406
0
                                    key_state->data, key_state->data_len,
2407
0
                                    key_state->curve25519_public_key,
2408
0
                                    key_state->curve25519_private_key,
2409
0
                                    &key_state->exchange_state);
2410
0
        if(ret == LIBSSH2_ERROR_EAGAIN)
2411
0
            return ret;
2412
0
    }
2413
2414
0
clean_exit:
2415
2416
0
    kex_method_curve25519_cleanup(session, key_state);
2417
2418
0
    return ret;
2419
0
}
2420
2421
#if LIBSSH2_MLKEM
2422
static void kex_mlkem768x25519_exchange_state_cleanup(
2423
    LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state)
2424
{
2425
    ssh2_bn_free(exchange_state->k);
2426
    exchange_state->k = NULL;
2427
2428
    if(exchange_state->k_value) {
2429
        ssh2_zero_free(session, exchange_state->k_value,
2430
                                exchange_state->k_value_len);
2431
        exchange_state->k_value = NULL;
2432
    }
2433
2434
    exchange_state->state = ssh2_NB_state_idle;
2435
}
2436
2437
static void kex_method_mlkem768x25519_cleanup(
2438
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
2439
{
2440
    ssh2_zero_free(session,
2441
                   key_state->curve25519_public_key, SSH2_ED25519_KEY_LEN);
2442
    key_state->curve25519_public_key = NULL;
2443
2444
    ssh2_zero_free(session,
2445
                   key_state->curve25519_private_key, SSH2_ED25519_KEY_LEN);
2446
    key_state->curve25519_private_key = NULL;
2447
2448
    ssh2_zero_free(session,
2449
                   key_state->mlkem_public_key,
2450
                   key_state->mlkem_public_key_len);
2451
    key_state->mlkem_public_key = NULL;
2452
    key_state->mlkem_public_key_len = 0;
2453
2454
    ssh2_zero_free(session,
2455
                   key_state->mlkem_private_key,
2456
                   key_state->mlkem_private_key_len);
2457
    key_state->mlkem_private_key = NULL;
2458
    key_state->mlkem_private_key_len = 0;
2459
2460
    if(key_state->data)
2461
        SSH2_SAFEFREE(session, key_state->data);
2462
2463
    key_state->state = ssh2_NB_state_idle;
2464
2465
    if(key_state->exchange_state.state != ssh2_NB_state_idle)
2466
        kex_curve25519_exchange_state_cleanup(session,
2467
                                              &key_state->exchange_state);
2468
}
2469
2470
static int kex_mlkem768x25519_sha256(
2471
    LIBSSH2_SESSION *session,
2472
    unsigned char *data, size_t data_len,
2473
    unsigned char public_t_key[SSH2_ED25519_KEY_LEN],
2474
    unsigned char private_t_key[SSH2_ED25519_KEY_LEN],
2475
    unsigned char public_pq_key[SSH2_MLKEM_768_PUBLIC_KEY_LEN],
2476
    unsigned char private_pq_key[SSH2_MLKEM_768_PRIVATE_KEY_LEN],
2477
    struct kmdhgGPshakex_state *exchange_state)
2478
{
2479
    int ret = 0;
2480
    int rc;
2481
2482
    if(exchange_state->state == ssh2_NB_state_idle) {
2483
        /* Setup initial values */
2484
        exchange_state->k = ssh2_bn_init();
2485
        exchange_state->state = ssh2_NB_state_created;
2486
    }
2487
2488
    if(exchange_state->state == ssh2_NB_state_created) {
2489
        /* parse INIT reply data */
2490
        unsigned char *server_public_key;
2491
        size_t public_t_key_len = SSH2_ED25519_KEY_LEN;
2492
        size_t public_pq_key_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN;
2493
        unsigned char shared_secret[SSH2_MLKEM_SHARED_SECRET_LEN +
2494
                                    SSH2_ED25519_KEY_LEN];
2495
        size_t server_public_key_len;
2496
        size_t x25519_len;
2497
        struct string_buf buf;
2498
2499
        if(!data) {
2500
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
2501
                           "Missing host key data");
2502
            goto clean_exit;
2503
        }
2504
2505
        ret = kex_proc_hostkey(session, &buf, exchange_state, data, data_len);
2506
        if(ret)
2507
            goto clean_exit;
2508
2509
        /* server public key Q_S */
2510
        if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) {
2511
            ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
2512
                           "Unexpected mlkem768x25519 server public key");
2513
            goto clean_exit;
2514
        }
2515
2516
        if(server_public_key_len !=
2517
           SSH2_MLKEM_768_CIPHERTEXT + SSH2_ED25519_KEY_LEN) {
2518
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
2519
                           "Unexpected mlkem768x25519 server "
2520
                           "public key length");
2521
            goto clean_exit;
2522
        }
2523
2524
        /* server signature */
2525
        if(ssh2_get_string(&buf, &exchange_state->h_sig,
2526
                           &exchange_state->h_sig_len)) {
2527
            ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT,
2528
                           "Unexpected mlkem768x25519 server sig length");
2529
            goto clean_exit;
2530
        }
2531
2532
        exchange_state->k_value_len = SSH2_SHA256_DIG_LEN + 4;
2533
2534
        exchange_state->k_value =
2535
            SSH2_ALLOC(session, exchange_state->k_value_len);
2536
        if(!exchange_state->k_value) {
2537
            ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
2538
                           "Unable to allocate buffer for K");
2539
            goto clean_exit;
2540
        }
2541
        ssh2_htonu32(exchange_state->k_value, SSH2_SHA256_DIG_LEN);
2542
2543
        /* Compute the ML-KEM shared secret */
2544
        rc = ssh2_mlkem_get_sk(shared_secret, 768,
2545
                               private_pq_key, server_public_key);
2546
        if(rc) {
2547
            ssh2_explicit_zero(shared_secret, sizeof(shared_secret));
2548
            ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
2549
                           "Unable to create mlkem shared secret");
2550
            goto clean_exit;
2551
        }
2552
2553
        /* Compute the x25519 shared secret K */
2554
        rc = ssh2_curve25519_gen_k(&exchange_state->k, private_t_key,
2555
                                   server_public_key +
2556
                                       SSH2_MLKEM_768_CIPHERTEXT);
2557
        if(rc) {
2558
            ssh2_explicit_zero(shared_secret, sizeof(shared_secret));
2559
            ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE,
2560
                           "Unable to create curve25519 shared secret");
2561
            goto clean_exit;
2562
        }
2563
2564
        /* The x25519 shared secret is a fixed-width 32-byte string, but
2565
           ssh2_bn_to_bin() omits leading zero bytes. Zero-fill the field and
2566
           write the value right-aligned; otherwise a secret with a leading
2567
           zero byte is left-shifted and the tail of shared_secret is hashed
2568
           uninitialized. */
2569
        x25519_len = ssh2_bn_bytes(exchange_state->k);
2570
        if(x25519_len == 0 || x25519_len > SSH2_ED25519_KEY_LEN) {
2571
            ssh2_explicit_zero(shared_secret, sizeof(shared_secret));
2572
            ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
2573
                           "Invalid curve25519 shared secret length");
2574
            goto clean_exit;
2575
        }
2576
        memset(shared_secret + SSH2_MLKEM_SHARED_SECRET_LEN, 0,
2577
               SSH2_ED25519_KEY_LEN);
2578
        if(ssh2_bn_to_bin(exchange_state->k,
2579
                          shared_secret + SSH2_MLKEM_SHARED_SECRET_LEN +
2580
                          SSH2_ED25519_KEY_LEN - x25519_len)) {
2581
            ssh2_explicit_zero(shared_secret, sizeof(shared_secret));
2582
            ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
2583
                           "Cannot write shared secret");
2584
            goto clean_exit;
2585
        }
2586
2587
        /* verify hash */
2588
        rc = ssh2_hash(SSH2_SHA256_ALG, shared_secret,
2589
                       SSH2_MLKEM_SHARED_SECRET_LEN + SSH2_ED25519_KEY_LEN,
2590
                       exchange_state->k_value + 4, SSH2_SHA256_DIG_LEN);
2591
        ssh2_explicit_zero(shared_secret, sizeof(shared_secret));
2592
        if(!rc) {
2593
            ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC,
2594
                           "kex: failed to calculate hash");
2595
            goto clean_exit;
2596
        }
2597
2598
        ret = kex_method_ec_sha_hash_create_verify(session, exchange_state,
2599
                 public_t_key, public_t_key_len,
2600
                 public_pq_key, public_pq_key_len,
2601
                 server_public_key, server_public_key_len,
2602
                 SSH2_SHA256_ALG, SSH2_SHA256_DIG_LEN,
2603
                 "Unable to verify host key signature mlkem768x25519");
2604
        if(ret)
2605
            goto clean_exit;
2606
2607
        exchange_state->c = SSH_MSG_NEWKEYS;
2608
        exchange_state->state = ssh2_NB_state_sent;
2609
    }
2610
2611
    if(exchange_state->state == ssh2_NB_state_sent) {
2612
        rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0);
2613
        if(rc == LIBSSH2_ERROR_EAGAIN)
2614
            return rc;
2615
        else if(rc) {
2616
            ret = ssh2_err(session, rc,
2617
                           "Unable to send NEWKEYS message mlkem768x25519");
2618
            goto clean_exit;
2619
        }
2620
2621
        exchange_state->state = ssh2_NB_state_sent2;
2622
    }
2623
2624
    if(exchange_state->state == ssh2_NB_state_sent2) {
2625
        ret = kex_finish(session, exchange_state,
2626
                         SSH2_SHA256_ALG, SSH2_SHA256_DIG_LEN);
2627
        if(ret == LIBSSH2_ERROR_EAGAIN)
2628
            return ret;
2629
    }
2630
2631
clean_exit:
2632
    kex_mlkem768x25519_exchange_state_cleanup(session, exchange_state);
2633
2634
    return ret;
2635
}
2636
2637
static int kex_method_mlkem768x25519_key_exchange(
2638
    LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state)
2639
{
2640
    int ret = 0;
2641
    int rc = 0;
2642
2643
    if(key_state->state == ssh2_NB_state_idle) {
2644
        key_state->public_key_oct = NULL;
2645
        key_state->state = ssh2_NB_state_created;
2646
    }
2647
2648
    if(key_state->state == ssh2_NB_state_created) {
2649
        int mlkem_size = 768;
2650
        size_t mlkem_public_key_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN;
2651
        size_t mlkem_private_key_len = SSH2_MLKEM_768_PRIVATE_KEY_LEN;
2652
        unsigned char *s = NULL;
2653
2654
        rc = ssh2_curve25519_new(session,
2655
                                 &key_state->curve25519_public_key,
2656
                                 &key_state->curve25519_private_key);
2657
        if(rc) {
2658
            ret = ssh2_err(session, rc, "Unable to create x25519 private key");
2659
            goto clean_exit;
2660
        }
2661
2662
        rc = ssh2_mlkem_new(session, mlkem_size,
2663
                            &key_state->mlkem_public_key,
2664
                            &key_state->mlkem_private_key);
2665
        if(rc) {
2666
            ret = ssh2_err(session, rc, "Unable to create mlkem private key");
2667
            goto clean_exit;
2668
        }
2669
2670
        key_state->mlkem_public_key_len = mlkem_public_key_len;
2671
        key_state->mlkem_private_key_len = mlkem_private_key_len;
2672
2673
        key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT;
2674
        s = key_state->request + 1;
2675
        ssh2_store_hybrid_str(&s, key_state->mlkem_public_key,
2676
                                  mlkem_public_key_len,
2677
                                  key_state->curve25519_public_key,
2678
                                  SSH2_ED25519_KEY_LEN);
2679
        key_state->request_len = mlkem_public_key_len +
2680
                                 SSH2_ED25519_KEY_LEN + 5;
2681
2682
        ssh2_deb((session, LIBSSH2_TRACE_KEX,
2683
                  "Initiating mlkem768x25519 SHA2"));
2684
2685
        key_state->state = ssh2_NB_state_sent;
2686
    }
2687
2688
    if(key_state->state == ssh2_NB_state_sent) {
2689
        rc = ssh2_transport_send(session, key_state->request,
2690
                                 key_state->request_len, NULL, 0);
2691
        if(rc == LIBSSH2_ERROR_EAGAIN)
2692
            return rc;
2693
        if(rc) {
2694
            ret = ssh2_err(session, rc, "Unable to send ECDH_INIT");
2695
            goto clean_exit;
2696
        }
2697
2698
        key_state->state = ssh2_NB_state_sent1;
2699
    }
2700
2701
    if(key_state->state == ssh2_NB_state_sent1) {
2702
        rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY,
2703
                                 &key_state->data, &key_state->data_len, 0,
2704
                                 NULL, 0, &key_state->req_state);
2705
        if(rc == LIBSSH2_ERROR_EAGAIN)
2706
            return rc;
2707
        if(rc) {
2708
            ret = ssh2_err(session, rc,
2709
                           "Timeout waiting for ECDH_REPLY reply");
2710
            goto clean_exit;
2711
        }
2712
2713
        key_state->state = ssh2_NB_state_sent2;
2714
    }
2715
2716
    if(key_state->state == ssh2_NB_state_sent2) {
2717
        ret = kex_mlkem768x25519_sha256(session, key_state->data,
2718
                                        key_state->data_len,
2719
                                        key_state->curve25519_public_key,
2720
                                        key_state->curve25519_private_key,
2721
                                        key_state->mlkem_public_key,
2722
                                        key_state->mlkem_private_key,
2723
                                        &key_state->exchange_state);
2724
2725
        if(ret == LIBSSH2_ERROR_EAGAIN)
2726
            return ret;
2727
    }
2728
2729
clean_exit:
2730
2731
    kex_method_mlkem768x25519_cleanup(session, key_state);
2732
2733
    return ret;
2734
}
2735
#endif /* LIBSSH2_MLKEM */
2736
#endif /* LIBSSH2_ED25519 */
2737
2738
352
#define KEX_METHOD_FLAG_REQ_ENC_HOSTKEY  0x0001
2739
352
#define KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY 0x0002
2740
2741
#ifdef LIBSSH2_KEX_SHA1_ENABLE
2742
static const struct kex_method kex_method_diffie_hellman_group1_sha1 = {
2743
    "diffie-hellman-group1-sha1",
2744
    kex_method_diffie_hellman_group1_sha1_key_exchange,
2745
    kex_diffie_hellman_cleanup,
2746
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2747
};
2748
2749
static const struct kex_method kex_method_diffie_hellman_group14_sha1 = {
2750
    "diffie-hellman-group14-sha1",
2751
    kex_method_diffie_hellman_group14_sha1_key_exchange,
2752
    kex_diffie_hellman_cleanup,
2753
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2754
};
2755
#endif
2756
2757
static const struct kex_method kex_method_diffie_hellman_group14_sha256 = {
2758
    "diffie-hellman-group14-sha256",
2759
    kex_method_diffie_hellman_group14_sha256_key_exchange,
2760
    kex_diffie_hellman_cleanup,
2761
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2762
};
2763
2764
static const struct kex_method kex_method_diffie_hellman_group16_sha512 = {
2765
    "diffie-hellman-group16-sha512",
2766
    kex_method_diffie_hellman_group16_sha512_key_exchange,
2767
    kex_diffie_hellman_cleanup,
2768
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2769
};
2770
2771
static const struct kex_method kex_method_diffie_hellman_group18_sha512 = {
2772
    "diffie-hellman-group18-sha512",
2773
    kex_method_diffie_hellman_group18_sha512_key_exchange,
2774
    kex_diffie_hellman_cleanup,
2775
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2776
};
2777
2778
#ifdef LIBSSH2_KEX_SHA1_ENABLE
2779
static const struct kex_method
2780
kex_method_diffie_hellman_group_exchange_sha1 = {
2781
    "diffie-hellman-group-exchange-sha1",
2782
    kex_method_diffie_hellman_group_exchange_sha1_key_exchange,
2783
    kex_diffie_hellman_cleanup,
2784
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2785
};
2786
#endif
2787
2788
static const struct kex_method
2789
kex_method_diffie_hellman_group_exchange_sha256 = {
2790
    "diffie-hellman-group-exchange-sha256",
2791
    kex_method_diffie_hellman_group_exchange_sha256_key_exchange,
2792
    kex_diffie_hellman_cleanup,
2793
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2794
};
2795
2796
#if LIBSSH2_ECDSA
2797
static const struct kex_method kex_method_ecdh_sha2_nistp256 = {
2798
    "ecdh-sha2-nistp256",
2799
    kex_method_ecdh_key_exchange,
2800
    kex_method_ecdh_cleanup,
2801
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2802
};
2803
2804
static const struct kex_method kex_method_ecdh_sha2_nistp384 = {
2805
    "ecdh-sha2-nistp384",
2806
    kex_method_ecdh_key_exchange,
2807
    kex_method_ecdh_cleanup,
2808
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2809
};
2810
2811
static const struct kex_method kex_method_ecdh_sha2_nistp521 = {
2812
    "ecdh-sha2-nistp521",
2813
    kex_method_ecdh_key_exchange,
2814
    kex_method_ecdh_cleanup,
2815
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2816
};
2817
#if LIBSSH2_MLKEM
2818
static const struct kex_method kex_method_ssh_mlkem768_nistp256_sha256 = {
2819
    "mlkem768nistp256-sha256",
2820
    kex_method_mlkem_nistp_key_exchange,
2821
    kex_method_mlkem_nistp_cleanup,
2822
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2823
};
2824
static const struct kex_method kex_method_ssh_mlkem1024_nistp384_sha384 = {
2825
    "mlkem1024nistp384-sha384",
2826
    kex_method_mlkem_nistp_key_exchange,
2827
    kex_method_mlkem_nistp_cleanup,
2828
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2829
};
2830
#endif /* LIBSSH2_MLKEM */
2831
#endif /* LIBSSH2_ECDSA */
2832
2833
#if LIBSSH2_ED25519
2834
static const struct kex_method kex_method_ssh_curve25519_sha256_libssh = {
2835
    "curve25519-sha256@libssh.org",
2836
    kex_method_curve25519_key_exchange,
2837
    kex_method_curve25519_cleanup,
2838
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2839
};
2840
static const struct kex_method kex_method_ssh_curve25519_sha256 = {
2841
    "curve25519-sha256",
2842
    kex_method_curve25519_key_exchange,
2843
    kex_method_curve25519_cleanup,
2844
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2845
};
2846
#if LIBSSH2_MLKEM
2847
static const struct kex_method kex_method_ssh_mlkem768_x25519_sha256 = {
2848
    "mlkem768x25519-sha256",
2849
    kex_method_mlkem768x25519_key_exchange,
2850
    kex_method_mlkem768x25519_cleanup,
2851
    KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY,
2852
};
2853
#endif /* LIBSSH2_MLKEM */
2854
#endif /* LIBSSH2_ED25519 */
2855
2856
/* this kex method signals that client can receive extensions
2857
 * as described in https://datatracker.ietf.org/doc/html/rfc8308
2858
 */
2859
2860
static const struct kex_method kex_method_extension_negotiation = {
2861
    "ext-info-c",
2862
    NULL,
2863
    NULL,
2864
    0,
2865
};
2866
2867
static const struct kex_method kex_method_strict_client_extension = {
2868
    "kex-strict-c-v00@openssh.com",
2869
    NULL,
2870
    NULL,
2871
    0,
2872
};
2873
2874
static const struct kex_method *kex_methods[] = {
2875
#if LIBSSH2_MLKEM
2876
#if LIBSSH2_ED25519
2877
    &kex_method_ssh_mlkem768_x25519_sha256,
2878
#endif
2879
#if LIBSSH2_ECDSA
2880
    &kex_method_ssh_mlkem768_nistp256_sha256,
2881
    &kex_method_ssh_mlkem1024_nistp384_sha384,
2882
#endif
2883
#endif /* LIBSSH2_MLKEM */
2884
#if LIBSSH2_ED25519
2885
    &kex_method_ssh_curve25519_sha256,
2886
    &kex_method_ssh_curve25519_sha256_libssh,
2887
#endif
2888
#if LIBSSH2_ECDSA
2889
    &kex_method_ecdh_sha2_nistp256,
2890
    &kex_method_ecdh_sha2_nistp384,
2891
    &kex_method_ecdh_sha2_nistp521,
2892
#endif
2893
    &kex_method_diffie_hellman_group_exchange_sha256,
2894
    &kex_method_diffie_hellman_group16_sha512,
2895
    &kex_method_diffie_hellman_group18_sha512,
2896
    &kex_method_diffie_hellman_group14_sha256,
2897
#ifdef LIBSSH2_KEX_SHA1_ENABLE
2898
    &kex_method_diffie_hellman_group14_sha1,
2899
    &kex_method_diffie_hellman_group1_sha1,
2900
    &kex_method_diffie_hellman_group_exchange_sha1,
2901
#endif
2902
    &kex_method_extension_negotiation,
2903
    &kex_method_strict_client_extension,
2904
    NULL
2905
};
2906
2907
struct common_method {
2908
    const char *name;
2909
};
2910
2911
/*
2912
 * Calculate the length of a particular method list's resulting string
2913
 * Includes SUM(strlen() of each individual method plus 1 (for comma)) - 1
2914
 * (because the last comma is not used)
2915
 * Another sign of bad coding practices gone mad. Pretend you do not see this.
2916
 */
2917
static size_t kex_method_strlen(const struct common_method **method)
2918
5.69k
{
2919
5.69k
    size_t len = 0;
2920
2921
5.69k
    if(!method || !*method)
2922
1.13k
        return 0;
2923
2924
38.1k
    while(*method && (*method)->name) {
2925
33.5k
        len += strlen((*method)->name) + 1;
2926
33.5k
        method++;
2927
33.5k
    }
2928
2929
4.55k
    return len - 1;
2930
5.69k
}
2931
2932
/*
2933
 * Generate formatted preference list in buf
2934
 */
2935
static uint32_t kex_method_list(unsigned char *buf, uint32_t list_strlen,
2936
                                const struct common_method **method)
2937
5.69k
{
2938
5.69k
    ssh2_htonu32(buf, list_strlen);
2939
5.69k
    buf += 4;
2940
2941
5.69k
    if(!method || !*method)
2942
1.13k
        return 4;
2943
2944
38.1k
    while(*method && (*method)->name) {
2945
33.5k
        uint32_t mlen = (uint32_t)strlen((*method)->name);
2946
33.5k
        memcpy(buf, (*method)->name, mlen);
2947
33.5k
        buf += mlen;
2948
33.5k
        *(buf++) = ',';
2949
33.5k
        method++;
2950
33.5k
    }
2951
2952
4.55k
    return list_strlen + 4;
2953
5.69k
}
2954
2955
#define KEX_METHOD_PREFS_LEN(prefvar, defaultvar)                       \
2956
5.69k
    (uint32_t)((prefvar) ? strlen(prefvar) :                            \
2957
5.69k
        kex_method_strlen((const struct common_method **)(defaultvar)))
2958
2959
#define KEX_METHOD_PREFS_STR(buf, prefvarlen, prefvar, defaultvar)            \
2960
5.69k
    do {                                                                      \
2961
5.69k
        if(prefvar) {                                                         \
2962
0
            ssh2_htonu32(buf, prefvarlen);                                    \
2963
0
            (buf) += 4;                                                       \
2964
0
            memcpy(buf, prefvar, prefvarlen);                                 \
2965
0
            (buf) += (prefvarlen);                                            \
2966
0
        }                                                                     \
2967
5.69k
        else                                                                  \
2968
5.69k
            (buf) += kex_method_list(buf, prefvarlen,                         \
2969
5.69k
                                (const struct common_method **)(defaultvar)); \
2970
5.69k
    } while(0)
2971
2972
/*
2973
 * Send SSH_MSG_KEXINIT packet
2974
 */
2975
static int kex_init(LIBSSH2_SESSION *session)
2976
569
{
2977
    /* 62 = packet_type(1) + cookie(16) + first_packet_follows(1) +
2978
       reserved(4) + length longs(40) */
2979
569
    size_t data_len = 62;
2980
569
    unsigned char *data, *s;
2981
569
    int rc;
2982
2983
569
    if(session->kexinit_state == ssh2_NB_state_idle) {
2984
569
        uint32_t kex_len, hostkey_len;
2985
569
        uint32_t crypt_cs_len, crypt_sc_len;
2986
569
        uint32_t comp_cs_len, comp_sc_len;
2987
569
        uint32_t mac_cs_len, mac_sc_len;
2988
569
        uint32_t lang_cs_len, lang_sc_len;
2989
2990
569
        kex_len = KEX_METHOD_PREFS_LEN(session->kex_prefs, kex_methods);
2991
569
        hostkey_len = KEX_METHOD_PREFS_LEN(session->hostkey_prefs,
2992
569
                                           ssh2_hostkey_methods());
2993
569
        crypt_cs_len = KEX_METHOD_PREFS_LEN(session->local.crypt_prefs,
2994
569
                                            ssh2_crypt_methods());
2995
569
        crypt_sc_len = KEX_METHOD_PREFS_LEN(session->remote.crypt_prefs,
2996
569
                                            ssh2_crypt_methods());
2997
569
        mac_cs_len = KEX_METHOD_PREFS_LEN(session->local.mac_prefs,
2998
569
                                          ssh2_mac_methods());
2999
569
        mac_sc_len = KEX_METHOD_PREFS_LEN(session->remote.mac_prefs,
3000
569
                                          ssh2_mac_methods());
3001
569
        comp_cs_len = KEX_METHOD_PREFS_LEN(session->local.comp_prefs,
3002
569
                                           ssh2_comp_methods(session));
3003
569
        comp_sc_len = KEX_METHOD_PREFS_LEN(session->remote.comp_prefs,
3004
569
                                           ssh2_comp_methods(session));
3005
569
        lang_cs_len = KEX_METHOD_PREFS_LEN(session->local.lang_prefs, NULL);
3006
569
        lang_sc_len = KEX_METHOD_PREFS_LEN(session->remote.lang_prefs, NULL);
3007
3008
569
        data_len += kex_len + hostkey_len + crypt_cs_len + crypt_sc_len +
3009
569
                    comp_cs_len + comp_sc_len + mac_cs_len + mac_sc_len +
3010
569
                    lang_cs_len + lang_sc_len;
3011
3012
569
        s = data = SSH2_ALLOC(session, data_len);
3013
569
        if(!data)
3014
0
            return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
3015
0
                            "Unable to allocate memory");
3016
3017
569
        *(s++) = SSH_MSG_KEXINIT;
3018
3019
569
        if(ssh2_random(s, 16)) {
3020
0
            SSH2_FREE(session, data);
3021
0
            return ssh2_err(session, LIBSSH2_ERROR_RANDGEN,
3022
0
                            "Unable to get random bytes for KEXINIT cookie");
3023
0
        }
3024
569
        s += 16;
3025
3026
        /* Enumerating through these lists twice is probably (certainly?)
3027
           inefficient from a CPU standpoint, but it saves multiple
3028
           malloc/realloc calls */
3029
569
        KEX_METHOD_PREFS_STR(s, kex_len, session->kex_prefs, kex_methods);
3030
569
        KEX_METHOD_PREFS_STR(s, hostkey_len, session->hostkey_prefs,
3031
569
                             ssh2_hostkey_methods());
3032
569
        KEX_METHOD_PREFS_STR(s, crypt_cs_len, session->local.crypt_prefs,
3033
569
                             ssh2_crypt_methods());
3034
569
        KEX_METHOD_PREFS_STR(s, crypt_sc_len, session->remote.crypt_prefs,
3035
569
                             ssh2_crypt_methods());
3036
569
        KEX_METHOD_PREFS_STR(s, mac_cs_len, session->local.mac_prefs,
3037
569
                             ssh2_mac_methods());
3038
569
        KEX_METHOD_PREFS_STR(s, mac_sc_len, session->remote.mac_prefs,
3039
569
                             ssh2_mac_methods());
3040
569
        KEX_METHOD_PREFS_STR(s, comp_cs_len, session->local.comp_prefs,
3041
569
                             ssh2_comp_methods(session));
3042
569
        KEX_METHOD_PREFS_STR(s, comp_sc_len, session->remote.comp_prefs,
3043
569
                             ssh2_comp_methods(session));
3044
569
        KEX_METHOD_PREFS_STR(s, lang_cs_len, session->local.lang_prefs, NULL);
3045
569
        KEX_METHOD_PREFS_STR(s, lang_sc_len, session->remote.lang_prefs, NULL);
3046
3047
        /* No optimistic KEX packet follows */
3048
        /* Deal with optimistic packets
3049
         * session->flags |= KEXINIT_OPTIMISTIC
3050
         * session->flags |= KEXINIT_METHODSMATCH
3051
         */
3052
569
        *(s++) = 0;
3053
3054
        /* Reserved == 0 */
3055
569
        ssh2_htonu32(s, 0);
3056
3057
569
#ifdef LIBSSH2DEBUG
3058
569
        {
3059
569
            unsigned char *p = data + 21; /* type(1) + cookie(16) + len(4) */
3060
3061
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent KEX: %.*s",
3062
569
                      (int)kex_len, p));
3063
569
            p += kex_len + 4;
3064
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent HOSTKEY: %.*s",
3065
569
                      (int)hostkey_len, p));
3066
569
            p += hostkey_len + 4;
3067
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent CRYPT_CS: %.*s",
3068
569
                      (int)crypt_cs_len, p));
3069
569
            p += crypt_cs_len + 4;
3070
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent CRYPT_SC: %.*s",
3071
569
                      (int)crypt_sc_len, p));
3072
569
            p += crypt_sc_len + 4;
3073
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent MAC_CS: %.*s",
3074
569
                      (int)mac_cs_len, p));
3075
569
            p += mac_cs_len + 4;
3076
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent MAC_SC: %.*s",
3077
569
                      (int)mac_sc_len, p));
3078
569
            p += mac_sc_len + 4;
3079
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent COMP_CS: %.*s",
3080
569
                      (int)comp_cs_len, p));
3081
569
            p += comp_cs_len + 4;
3082
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent COMP_SC: %.*s",
3083
569
                      (int)comp_sc_len, p));
3084
569
            p += comp_sc_len + 4;
3085
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent LANG_CS: %.*s",
3086
569
                      (int)lang_cs_len, p));
3087
569
            p += lang_cs_len + 4;
3088
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent LANG_SC: %.*s",
3089
569
                      (int)lang_sc_len, p));
3090
569
            p += lang_sc_len;
3091
569
            ssh2_deb((session, LIBSSH2_TRACE_KEX,
3092
569
                      "Sent first_kex_packet_follows: %02x", p[0]));
3093
569
        }
3094
569
#endif /* LIBSSH2DEBUG */
3095
3096
569
        session->kexinit_state = ssh2_NB_state_created;
3097
569
    }
3098
0
    else {
3099
0
        data = session->kexinit_data;
3100
0
        data_len = session->kexinit_data_len;
3101
        /* zap the variables to ensure there is NOT a double free later */
3102
0
        session->kexinit_data = NULL;
3103
0
        session->kexinit_data_len = 0;
3104
0
    }
3105
3106
569
    rc = ssh2_transport_send(session, data, data_len, NULL, 0);
3107
569
    if(rc == LIBSSH2_ERROR_EAGAIN) {
3108
0
        session->kexinit_data = data;
3109
0
        session->kexinit_data_len = data_len;
3110
0
        return rc;
3111
0
    }
3112
569
    else if(rc) {
3113
0
        SSH2_FREE(session, data);
3114
0
        session->kexinit_state = ssh2_NB_state_idle;
3115
0
        return ssh2_err(session, rc,
3116
0
                        "Unable to send KEXINIT packet to remote host");
3117
0
    }
3118
3119
569
    if(session->local.kexinit)
3120
0
        SSH2_FREE(session, session->local.kexinit);
3121
3122
569
    session->local.kexinit = data;
3123
569
    session->local.kexinit_len = data_len;
3124
3125
569
    session->kexinit_state = ssh2_NB_state_idle;
3126
3127
569
    return 0;
3128
569
}
3129
3130
/*
3131
 * Kex specific variant of strstr()
3132
 * Needle must be preceded by BOL or ',', and followed by ',' or EOL
3133
 */
3134
const char *ssh2_kex_agree_instr(const char *haystack, size_t haystack_len,
3135
                                 const char *needle, size_t needle_len)
3136
12.1k
{
3137
12.1k
    const char *s;
3138
12.1k
    const char *end_haystack;
3139
12.1k
    size_t left;
3140
3141
12.1k
    if(!haystack || !needle)
3142
0
        return NULL;
3143
3144
    /* Haystack too short to bother trying */
3145
12.1k
    if(haystack_len < needle_len || needle_len == 0)
3146
1.39k
        return NULL;
3147
3148
10.7k
    s = haystack;
3149
10.7k
    end_haystack = &haystack[haystack_len];
3150
10.7k
    left = end_haystack - s;
3151
3152
    /* Needle at start of haystack */
3153
10.7k
    if(!strncmp(haystack, needle, needle_len) &&
3154
1.05k
       (needle_len == haystack_len || haystack[needle_len] == ','))
3155
1.04k
        return haystack;
3156
3157
    /* Search until we run out of commas or we run out of haystack,
3158
       whichever comes first */
3159
    /* !checksrc! disable EQUALSNULL 1 */
3160
30.3k
    while((s = memchr(s, ',', left)) != NULL) {
3161
        /* Advance buffer past comma if we can */
3162
26.7k
        left = end_haystack - s;
3163
26.7k
        if(left >= 1 && left <= haystack_len && left > needle_len) {
3164
22.7k
            s++;
3165
22.7k
            left--;
3166
22.7k
        }
3167
3.93k
        else
3168
3.93k
            return NULL;
3169
3170
        /* Needle at X position */
3171
22.7k
        if(!strncmp(s, needle, needle_len) &&
3172
2.36k
           (((s - haystack) + needle_len) == haystack_len ||
3173
1.25k
            s[needle_len] == ','))
3174
2.13k
            return s;
3175
22.7k
    }
3176
3177
3.65k
    return NULL;
3178
9.72k
}
3179
3180
static const struct common_method *kex_get_method_by_name(
3181
    const char *name, size_t name_len,
3182
    const struct common_method **methodlist)
3183
0
{
3184
0
    while(*methodlist) {
3185
0
        if(strlen((*methodlist)->name) == name_len &&
3186
0
           !strncmp((*methodlist)->name, name, name_len))
3187
0
            return *methodlist;
3188
3189
0
        methodlist++;
3190
0
    }
3191
0
    return NULL;
3192
0
}
3193
3194
/*
3195
 * Agree on a host key which works with this kex
3196
 */
3197
static int kex_agree_hostkey(LIBSSH2_SESSION *session, size_t kex_flags,
3198
                             const char *hostkey, size_t hostkey_len)
3199
358
{
3200
358
    const struct hostkey_method **hostkeyp = ssh2_hostkey_methods();
3201
358
    const char *s;
3202
3203
358
    if(session->hostkey_prefs) {
3204
0
        s = session->hostkey_prefs;
3205
3206
0
        while(s && *s) {
3207
0
            const char *p = strchr(s, ',');
3208
0
            size_t method_len = p ? (size_t)(p - s) : strlen(s);
3209
0
            if(ssh2_kex_agree_instr(hostkey, hostkey_len, s, method_len)) {
3210
0
                const struct hostkey_method *method =
3211
0
                    (const struct hostkey_method *)kex_get_method_by_name(
3212
0
                        s, method_len,
3213
0
                        (const struct common_method **)hostkeyp);
3214
3215
0
                if(!method)
3216
0
                    return -1;  /* Invalid method -- Should never be reached */
3217
3218
                /* OK so far, but does it suit our purposes? (Encrypting
3219
                   vs Signing) */
3220
0
                if(((kex_flags & KEX_METHOD_FLAG_REQ_ENC_HOSTKEY) == 0 ||
3221
0
                    method->encrypt) &&
3222
                   /* Either this host key can do encryption or this kex
3223
                      does not require it */
3224
0
                   ((kex_flags & KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY) == 0 ||
3225
0
                    method->sig_verify)) {
3226
                        /* Either this host key can do signing or this kex
3227
                           does not require it */
3228
0
                    session->hostkey = method;
3229
0
                    return 0;
3230
0
                }
3231
0
            }
3232
3233
0
            s = p ? p + 1 : NULL;
3234
0
        }
3235
0
        return -1;
3236
0
    }
3237
3238
997
    while(hostkeyp && *hostkeyp && (*hostkeyp)->name) {
3239
991
        s = ssh2_kex_agree_instr(hostkey, hostkey_len,
3240
991
                                 (*hostkeyp)->name, strlen((*hostkeyp)->name));
3241
991
        if(s &&
3242
           /* OK so far, but does it suit our purposes? (Encrypting vs
3243
              Signing) */
3244
352
           ((kex_flags & KEX_METHOD_FLAG_REQ_ENC_HOSTKEY) == 0 ||
3245
0
            (*hostkeyp)->encrypt) &&
3246
           /* Either this host key can do encryption or this kex
3247
              does not require it */
3248
352
           ((kex_flags & KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY) == 0 ||
3249
352
            (*hostkeyp)->sig_verify)) {
3250
            /* Either this host key can do signing or this kex
3251
               does not require it */
3252
352
            session->hostkey = *hostkeyp;
3253
352
            return 0;
3254
352
        }
3255
639
        hostkeyp++;
3256
639
    }
3257
3258
6
    return -1;
3259
358
}
3260
3261
/*
3262
 * Agree on a Key Exchange method and a host key encoding type
3263
 */
3264
static int kex_agree_kex_hostkey(LIBSSH2_SESSION *session,
3265
                                 const char *kex, size_t kex_len,
3266
                                 const char *hostkey, size_t hostkey_len)
3267
363
{
3268
363
    static const char strict[] = "kex-strict-s-v00@openssh.com";
3269
363
    const struct kex_method **kexp = kex_methods;
3270
363
    const char *s;
3271
3272
363
    if(ssh2_kex_agree_instr(kex, kex_len, strict, sizeof(strict) - 1))
3273
353
        session->kex_strict = 1;
3274
3275
363
    if(session->kex_prefs) {
3276
0
        s = session->kex_prefs;
3277
3278
0
        while(s && *s) {
3279
0
            const char *q, *p = strchr(s, ',');
3280
0
            size_t method_len = p ? (size_t)(p - s) : strlen(s);
3281
0
            q = ssh2_kex_agree_instr(kex, kex_len, s, method_len);
3282
0
            if(q) {
3283
0
                const struct kex_method *method =
3284
0
                    (const struct kex_method *)kex_get_method_by_name(
3285
0
                        s, method_len, (const struct common_method **)kexp);
3286
3287
0
                if(!method)
3288
0
                    return -1;  /* Invalid method -- Should never be reached */
3289
3290
                /* We have agreed on a key exchange method,
3291
                 * Can we agree on a host key that works with this kex?
3292
                 */
3293
0
                if(method->exchange_keys &&
3294
0
                   kex_agree_hostkey(session, method->flags, hostkey,
3295
0
                                     hostkey_len) == 0) {
3296
0
                    session->kex = method;
3297
0
                    if(session->burn_optimistic_kexinit && kex == q)
3298
                        /* Server sent an optimistic packet, and client agrees
3299
                         * with preference cancel burning the first KEX_INIT
3300
                         * packet that comes in */
3301
0
                        session->burn_optimistic_kexinit = 0;
3302
3303
0
                    return 0;
3304
0
                }
3305
0
            }
3306
3307
0
            s = p ? p + 1 : NULL;
3308
0
        }
3309
0
        return -1;
3310
0
    }
3311
3312
3.22k
    while(*kexp && (*kexp)->name) {
3313
3.21k
        s = ssh2_kex_agree_instr(kex, kex_len,
3314
3.21k
                                 (*kexp)->name, strlen((*kexp)->name));
3315
3.21k
        if(s &&
3316
           /* We have agreed on a key exchange method,
3317
            * Can we agree on a host key that works with this kex?
3318
            */
3319
358
           kex_agree_hostkey(session, (*kexp)->flags, hostkey,
3320
358
                             hostkey_len) == 0) {
3321
352
            session->kex = *kexp;
3322
352
            if(session->burn_optimistic_kexinit && kex == s)
3323
                /* Server sent an optimistic packet, and client agrees
3324
                 * with preference cancel burning the first KEX_INIT
3325
                 * packet that comes in */
3326
0
                session->burn_optimistic_kexinit = 0;
3327
3328
352
            return 0;
3329
352
        }
3330
2.85k
        kexp++;
3331
2.85k
    }
3332
11
    return -1;
3333
363
}
3334
3335
/*
3336
 * Agree on a cipher algo
3337
 */
3338
static int kex_agree_crypt(LIBSSH2_SESSION *session,
3339
                           struct endpoint_data *endpoint,
3340
                           const char *crypt, size_t crypt_len)
3341
701
{
3342
701
    const struct crypt_method **cryptp = ssh2_crypt_methods();
3343
701
    const char *s;
3344
3345
701
    (void)session;
3346
3347
701
    if(endpoint->crypt_prefs) {
3348
0
        s = endpoint->crypt_prefs;
3349
3350
0
        while(s && *s) {
3351
0
            const char *p = strchr(s, ',');
3352
0
            size_t method_len = p ? (size_t)(p - s) : strlen(s);
3353
3354
0
            if(ssh2_kex_agree_instr(crypt, crypt_len, s, method_len)) {
3355
0
                const struct crypt_method *method =
3356
0
                    (const struct crypt_method *)kex_get_method_by_name(
3357
0
                        s, method_len, (const struct common_method **)cryptp);
3358
3359
0
                if(!method)
3360
0
                    return -1;  /* Invalid method -- Should never be reached */
3361
3362
0
                endpoint->crypt = method;
3363
0
                return 0;
3364
0
            }
3365
3366
0
            s = p ? p + 1 : NULL;
3367
0
        }
3368
0
        return -1;
3369
0
    }
3370
3371
3.39k
    while(*cryptp && (*cryptp)->name) {
3372
3.38k
        s = ssh2_kex_agree_instr(crypt, crypt_len,
3373
3.38k
                                 (*cryptp)->name, strlen((*cryptp)->name));
3374
3.38k
        if(s) {
3375
698
            endpoint->crypt = *cryptp;
3376
698
            return 0;
3377
698
        }
3378
2.69k
        cryptp++;
3379
2.69k
    }
3380
3381
3
    return -1;
3382
701
}
3383
3384
/*
3385
 * Agree on a message authentication hash
3386
 */
3387
static int kex_agree_mac(LIBSSH2_SESSION *session,
3388
                         struct endpoint_data *endpoint,
3389
                         const char *mac, size_t mac_len)
3390
698
{
3391
698
    const struct mac_method **macp = ssh2_mac_methods();
3392
698
    const struct mac_method *override;
3393
698
    const char *s;
3394
698
    (void)session;
3395
3396
698
    override = ssh2_mac_override(endpoint->crypt);
3397
698
    if(override) {
3398
        /* This crypto method has its own hmac method built-in, so a separate
3399
         * negotiation (and use) of a separate hmac method is unnecessary */
3400
0
        endpoint->mac = override;
3401
0
        return 0;
3402
0
    }
3403
3404
698
    if(endpoint->mac_prefs) {
3405
0
        s = endpoint->mac_prefs;
3406
3407
0
        while(s && *s) {
3408
0
            const char *p = strchr(s, ',');
3409
0
            size_t method_len = p ? (size_t)(p - s) : strlen(s);
3410
3411
0
            if(ssh2_kex_agree_instr(mac, mac_len, s, method_len)) {
3412
0
                const struct mac_method *method =
3413
0
                    (const struct mac_method *)kex_get_method_by_name(
3414
0
                        s, method_len, (const struct common_method **)macp);
3415
3416
0
                if(!method)
3417
0
                    return -1;  /* Invalid method -- Should never be reached */
3418
3419
0
                endpoint->mac = method;
3420
0
                return 0;
3421
0
            }
3422
3423
0
            s = p ? p + 1 : NULL;
3424
0
        }
3425
0
        return -1;
3426
0
    }
3427
3428
3.48k
    while(*macp && (*macp)->name) {
3429
3.48k
        s = ssh2_kex_agree_instr(mac, mac_len,
3430
3.48k
                                 (*macp)->name, strlen((*macp)->name));
3431
3.48k
        if(s) {
3432
696
            endpoint->mac = *macp;
3433
696
            return 0;
3434
696
        }
3435
2.79k
        macp++;
3436
2.79k
    }
3437
3438
2
    return -1;
3439
698
}
3440
3441
/*
3442
 * Agree on a compression scheme
3443
 */
3444
static int kex_agree_comp(LIBSSH2_SESSION *session,
3445
                          struct endpoint_data *endpoint,
3446
                          const char *comp, size_t comp_len)
3447
694
{
3448
694
    const struct comp_method **compp = ssh2_comp_methods(session);
3449
694
    const char *s;
3450
694
    (void)session;
3451
3452
694
    if(endpoint->comp_prefs) {
3453
0
        s = endpoint->comp_prefs;
3454
3455
0
        while(s && *s) {
3456
0
            const char *p = strchr(s, ',');
3457
0
            size_t method_len = p ? (size_t)(p - s) : strlen(s);
3458
3459
0
            if(ssh2_kex_agree_instr(comp, comp_len, s, method_len)) {
3460
0
                const struct comp_method *method =
3461
0
                    (const struct comp_method *)kex_get_method_by_name(
3462
0
                        s, method_len, (const struct common_method **)compp);
3463
3464
0
                if(!method)
3465
0
                    return -1;  /* Invalid method -- Should never be reached */
3466
3467
0
                endpoint->comp = method;
3468
0
                return 0;
3469
0
            }
3470
3471
0
            s = p ? p + 1 : NULL;
3472
0
        }
3473
0
        return -1;
3474
0
    }
3475
3476
694
    while(*compp && (*compp)->name) {
3477
694
        s = ssh2_kex_agree_instr(comp, comp_len,
3478
694
                                 (*compp)->name, strlen((*compp)->name));
3479
694
        if(s) {
3480
694
            endpoint->comp = *compp;
3481
694
            return 0;
3482
694
        }
3483
0
        compp++;
3484
0
    }
3485
3486
0
    return -1;
3487
694
}
3488
3489
/* TODO: When in server mode we need to turn this logic on its head
3490
 * The Client gets to make the final call on "agreed methods"
3491
 */
3492
3493
/*
3494
 * Decide which specific method to use of the methods offered by each party
3495
 */
3496
static int kex_agree_methods(LIBSSH2_SESSION *session, unsigned char *data,
3497
                             size_t data_len)
3498
535
{
3499
535
    char *kex, *hostkey, *crypt_cs, *crypt_sc, *comp_cs, *comp_sc,
3500
535
        *mac_cs, *mac_sc, *tmp;
3501
535
    size_t kex_len, hostkey_len, crypt_cs_len, crypt_sc_len, comp_cs_len;
3502
535
    size_t comp_sc_len, mac_cs_len, mac_sc_len;
3503
535
    struct string_buf buf;
3504
3505
535
    if(data_len < 17)
3506
0
        return -1;
3507
3508
535
    buf.data = data;
3509
535
    buf.len = data_len;
3510
535
    buf.dataptr = buf.data;
3511
535
    buf.dataptr++; /* advance past packet type */
3512
3513
    /* Skip cookie, do not worry, it is preserved in the kexinit field */
3514
535
    buf.dataptr += 16;
3515
3516
    /* Locate each string */
3517
535
    if(ssh2_get_chars(&buf, &kex, &kex_len))
3518
2
        return -1;
3519
533
    if(ssh2_get_chars(&buf, &hostkey, &hostkey_len))
3520
85
        return -1;
3521
448
    if(ssh2_get_chars(&buf, &crypt_cs, &crypt_cs_len))
3522
78
        return -1;
3523
370
    if(ssh2_get_chars(&buf, &crypt_sc, &crypt_sc_len))
3524
0
        return -1;
3525
370
    if(ssh2_get_chars(&buf, &mac_cs, &mac_cs_len))
3526
0
        return -1;
3527
370
    if(ssh2_get_chars(&buf, &mac_sc, &mac_sc_len))
3528
0
        return -1;
3529
370
    if(ssh2_get_chars(&buf, &comp_cs, &comp_cs_len))
3530
0
        return -1;
3531
370
    if(ssh2_get_chars(&buf, &comp_sc, &comp_sc_len))
3532
3
        return -1;
3533
3534
    /* read lang_cs and lang_sc but are unused */
3535
367
    if(ssh2_get_chars(&buf, &tmp, NULL))
3536
4
        return -1;
3537
363
    if(ssh2_get_chars(&buf, &tmp, NULL))
3538
0
        return -1;
3539
3540
    /* If the server sent an optimistic packet, assume that it guessed wrong.
3541
     * If the guess is determined to be right (by kex_agree_kex_hostkey())
3542
     * This flag is reset to zero so that it is not ignored */
3543
363
    if(ssh2_check_length(&buf, 1))
3544
363
        session->burn_optimistic_kexinit = *(buf.dataptr++);
3545
0
    else
3546
0
        return -1;
3547
3548
    /* Next uint32 in packet is all zeros (reserved) */
3549
3550
363
    if(kex_agree_kex_hostkey(session, kex, kex_len, hostkey, hostkey_len))
3551
11
        return -1;
3552
3553
352
    if(kex_agree_crypt(session, &session->local, crypt_cs, crypt_cs_len) ||
3554
349
       kex_agree_crypt(session, &session->remote, crypt_sc, crypt_sc_len))
3555
3
        return -1;
3556
3557
    /* This must happen after kex_agree_crypt since some MACs depend on the
3558
       negotiated crypto method */
3559
349
    if(kex_agree_mac(session, &session->local, mac_cs, mac_cs_len) ||
3560
349
       kex_agree_mac(session, &session->remote, mac_sc, mac_sc_len))
3561
2
        return -1;
3562
3563
347
    if(kex_agree_comp(session, &session->local, comp_cs, comp_cs_len) ||
3564
347
       kex_agree_comp(session, &session->remote, comp_sc, comp_sc_len))
3565
0
        return -1;
3566
3567
#if 0
3568
    if(kex_agree_lang(session, &session->local, lang_cs, lang_cs_len) ||
3569
       kex_agree_lang(session, &session->remote, lang_sc, lang_sc_len))
3570
        return -1;
3571
#endif
3572
3573
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on KEX method: %s",
3574
347
              session->kex->name));
3575
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on HOSTKEY method: %s",
3576
347
              session->hostkey->name));
3577
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on CRYPT_CS method: %s",
3578
347
              session->local.crypt->name));
3579
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on CRYPT_SC method: %s",
3580
347
              session->remote.crypt->name));
3581
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on MAC_CS method: %s",
3582
347
              session->local.mac->name));
3583
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on MAC_SC method: %s",
3584
347
              session->remote.mac->name));
3585
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on COMP_CS method: %s",
3586
347
              session->local.comp->name));
3587
347
    ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on COMP_SC method: %s",
3588
347
              session->remote.comp->name));
3589
3590
347
    return 0;
3591
347
}
3592
3593
/*
3594
 * Exchange keys
3595
 * Returns 0 on success, non-zero on failure
3596
 *
3597
 * Returns some errors without ssh2_err()
3598
 */
3599
int ssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange,
3600
                      struct key_exchange_state *key_state)
3601
569
{
3602
569
    int rc = 0;
3603
569
    int retcode;
3604
3605
569
    session->state |= SSH2_STATE_KEX_ACTIVE;
3606
3607
569
    if(key_state->state == ssh2_NB_state_idle) {
3608
        /* Prevent loop in packet_add() */
3609
569
        session->state |= SSH2_STATE_EXCHANGING_KEYS;
3610
3611
569
        if(reexchange) {
3612
499
            if(session->kex && session->kex->cleanup)
3613
338
                session->kex->cleanup(session, &key_state->key_state_low);
3614
3615
499
            session->kex = NULL;
3616
3617
499
            if(session->hostkey && session->hostkey->dtor)
3618
338
                session->hostkey->dtor(session,
3619
338
                                       &session->server_hostkey_abstract);
3620
499
            session->hostkey = NULL;
3621
499
        }
3622
3623
569
        key_state->state = ssh2_NB_state_created;
3624
569
    }
3625
3626
569
    if(!session->kex || !session->hostkey) {
3627
569
        if(key_state->state == ssh2_NB_state_created) {
3628
            /* Preserve in case of failure */
3629
569
            key_state->oldlocal = session->local.kexinit;
3630
569
            key_state->oldlocal_len = session->local.kexinit_len;
3631
3632
569
            session->local.kexinit = NULL;
3633
3634
569
            key_state->state = ssh2_NB_state_sent;
3635
569
        }
3636
3637
569
        if(key_state->state == ssh2_NB_state_sent) {
3638
569
            retcode = kex_init(session);
3639
569
            if(retcode == LIBSSH2_ERROR_EAGAIN) {
3640
0
                session->state &= ~SSH2_STATE_KEX_ACTIVE;
3641
0
                return retcode;
3642
0
            }
3643
569
            else if(retcode) {
3644
0
                session->local.kexinit = key_state->oldlocal;
3645
0
                session->local.kexinit_len = key_state->oldlocal_len;
3646
0
                key_state->state = ssh2_NB_state_idle;
3647
0
                session->state &= ~SSH2_STATE_INITIAL_KEX;
3648
0
                session->state &= ~SSH2_STATE_KEX_ACTIVE;
3649
0
                session->state &= ~SSH2_STATE_EXCHANGING_KEYS;
3650
0
                return LIBSSH2_ERROR_KEX_FAILURE;
3651
0
            }
3652
3653
569
            key_state->state = ssh2_NB_state_sent1;
3654
569
        }
3655
3656
569
        if(key_state->state == ssh2_NB_state_sent1) {
3657
569
            retcode =
3658
569
                ssh2_packet_require(session, SSH_MSG_KEXINIT,
3659
569
                                    &key_state->data,
3660
569
                                    &key_state->data_len, 0, NULL, 0,
3661
569
                                    &key_state->req_state);
3662
569
            if(retcode == LIBSSH2_ERROR_EAGAIN) {
3663
0
                session->state &= ~SSH2_STATE_KEX_ACTIVE;
3664
0
                return retcode;
3665
0
            }
3666
569
            else if(retcode) {
3667
34
                if(session->local.kexinit)
3668
34
                    SSH2_FREE(session, session->local.kexinit);
3669
34
                session->local.kexinit = key_state->oldlocal;
3670
34
                session->local.kexinit_len = key_state->oldlocal_len;
3671
34
                key_state->state = ssh2_NB_state_idle;
3672
34
                session->state &= ~SSH2_STATE_INITIAL_KEX;
3673
34
                session->state &= ~SSH2_STATE_KEX_ACTIVE;
3674
34
                session->state &= ~SSH2_STATE_EXCHANGING_KEYS;
3675
34
                return LIBSSH2_ERROR_KEX_FAILURE;
3676
34
            }
3677
3678
535
            if(session->remote.kexinit)
3679
25
                SSH2_FREE(session, session->remote.kexinit);
3680
535
            session->remote.kexinit = key_state->data;
3681
535
            session->remote.kexinit_len = key_state->data_len;
3682
535
            key_state->data = NULL;
3683
3684
535
            if(kex_agree_methods(session, session->remote.kexinit,
3685
535
                                 session->remote.kexinit_len))
3686
188
                rc = LIBSSH2_ERROR_KEX_FAILURE;
3687
3688
535
            key_state->state = ssh2_NB_state_sent2;
3689
535
        }
3690
569
    }
3691
0
    else
3692
0
        key_state->state = ssh2_NB_state_sent2;
3693
3694
535
    if(rc == 0 && session->kex && key_state->state == ssh2_NB_state_sent2) {
3695
347
        if(session->kex->exchange_keys)
3696
346
            retcode = session->kex->exchange_keys(session,
3697
346
                                                  &key_state->key_state_low);
3698
1
        else
3699
1
            retcode = LIBSSH2_ERROR_PROTO;  /* No exchange keys present */
3700
3701
347
        if(retcode == LIBSSH2_ERROR_EAGAIN) {
3702
0
            session->state &= ~SSH2_STATE_KEX_ACTIVE;
3703
0
            return retcode;
3704
0
        }
3705
347
        else if(retcode)
3706
347
            rc = ssh2_err(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE,
3707
347
                          "Unrecoverable error exchanging keys");
3708
347
    }
3709
3710
    /* Done with kexinit buffers */
3711
535
    if(session->local.kexinit)
3712
510
        SSH2_SAFEFREE(session, session->local.kexinit);
3713
535
    if(session->remote.kexinit)
3714
510
        SSH2_SAFEFREE(session, session->remote.kexinit);
3715
3716
535
    session->state &= ~SSH2_STATE_INITIAL_KEX;
3717
535
    session->state &= ~SSH2_STATE_KEX_ACTIVE;
3718
535
    session->state &= ~SSH2_STATE_EXCHANGING_KEYS;
3719
3720
535
    key_state->state = ssh2_NB_state_idle;
3721
3722
535
    return rc;
3723
535
}
3724
3725
/*
3726
 * Set preferred method
3727
 */
3728
int libssh2_session_method_pref(LIBSSH2_SESSION *session, int method_type,
3729
                                const char *prefs)
3730
0
{
3731
0
    char **prefvar, *s, *newprefs;
3732
0
    char *tmpprefs = NULL;
3733
0
    size_t prefs_len;
3734
0
    const struct common_method **mlist;
3735
3736
0
    if(!session || !prefs)
3737
0
        return LIBSSH2_ERROR_BAD_USE;
3738
3739
0
    prefs_len = strlen(prefs);
3740
3741
0
    switch(method_type) {
3742
0
    case LIBSSH2_METHOD_KEX:
3743
0
        prefvar = &session->kex_prefs;
3744
0
        mlist = (const struct common_method **)kex_methods;
3745
0
        break;
3746
3747
0
    case LIBSSH2_METHOD_HOSTKEY:
3748
0
        prefvar = &session->hostkey_prefs;
3749
0
        mlist = (const struct common_method **)ssh2_hostkey_methods();
3750
0
        break;
3751
3752
0
    case LIBSSH2_METHOD_CRYPT_CS:
3753
0
        prefvar = &session->local.crypt_prefs;
3754
0
        mlist = (const struct common_method **)ssh2_crypt_methods();
3755
0
        break;
3756
3757
0
    case LIBSSH2_METHOD_CRYPT_SC:
3758
0
        prefvar = &session->remote.crypt_prefs;
3759
0
        mlist = (const struct common_method **)ssh2_crypt_methods();
3760
0
        break;
3761
3762
0
    case LIBSSH2_METHOD_MAC_CS:
3763
0
        prefvar = &session->local.mac_prefs;
3764
0
        mlist = (const struct common_method **)ssh2_mac_methods();
3765
0
        break;
3766
3767
0
    case LIBSSH2_METHOD_MAC_SC:
3768
0
        prefvar = &session->remote.mac_prefs;
3769
0
        mlist = (const struct common_method **)ssh2_mac_methods();
3770
0
        break;
3771
3772
0
    case LIBSSH2_METHOD_COMP_CS:
3773
0
        prefvar = &session->local.comp_prefs;
3774
0
        mlist = (const struct common_method **)ssh2_comp_methods(session);
3775
0
        break;
3776
3777
0
    case LIBSSH2_METHOD_COMP_SC:
3778
0
        prefvar = &session->remote.comp_prefs;
3779
0
        mlist = (const struct common_method **)ssh2_comp_methods(session);
3780
0
        break;
3781
3782
0
    case LIBSSH2_METHOD_LANG_CS:
3783
0
        prefvar = &session->local.lang_prefs;
3784
0
        mlist = NULL;
3785
0
        break;
3786
3787
0
    case LIBSSH2_METHOD_LANG_SC:
3788
0
        prefvar = &session->remote.lang_prefs;
3789
0
        mlist = NULL;
3790
0
        break;
3791
3792
0
    case LIBSSH2_METHOD_SIGN_ALGO:
3793
0
        prefvar = &session->sign_algo_prefs;
3794
0
        mlist = NULL;
3795
0
        break;
3796
3797
0
    default:
3798
0
        return ssh2_err(session, LIBSSH2_ERROR_INVAL,
3799
0
                        "Invalid parameter specified for method_type");
3800
0
    }
3801
3802
0
    s = newprefs = SSH2_ALLOC(session, prefs_len + 1);
3803
0
    if(!newprefs) {
3804
0
        if(tmpprefs)
3805
0
            SSH2_FREE(session, tmpprefs);
3806
0
        return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
3807
0
                        "Error allocated space for method preferences");
3808
0
    }
3809
0
    memcpy(s, prefs, prefs_len + 1);
3810
3811
0
    while(s && *s && mlist) {
3812
0
        char *p = strchr(s, ',');
3813
0
        size_t method_len = p ? (size_t)(p - s) : strlen(s);
3814
3815
0
        if(!kex_get_method_by_name(s, method_len, mlist)) {
3816
            /* Strip out unsupported method */
3817
0
            if(p)
3818
0
                memmove(s, p + 1, strlen(s) - method_len);
3819
0
            else {
3820
0
                if(s > newprefs)
3821
0
                    *(--s) = '\0';
3822
0
                else
3823
0
                    *s = '\0';
3824
0
            }
3825
0
        }
3826
0
        else
3827
0
            s = p ? (p + 1) : NULL;
3828
0
    }
3829
3830
0
    if(tmpprefs)
3831
0
        SSH2_FREE(session, tmpprefs);
3832
3833
0
    if(!*newprefs) {
3834
0
        SSH2_FREE(session, newprefs);
3835
0
        return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
3836
0
                        "The requested method(s) are not currently supported");
3837
0
    }
3838
3839
    /* add method kex extension to the start of the user list */
3840
0
    if(method_type == LIBSSH2_METHOD_KEX) {
3841
0
        static const char kex_extensions[] =
3842
0
            "ext-info-c,kex-strict-c-v00@openssh.com,";
3843
0
        size_t kex_extensions_len = sizeof(kex_extensions) - 1;
3844
0
        size_t tmp_len = kex_extensions_len + strlen(newprefs);
3845
0
        tmpprefs = SSH2_ALLOC(session, tmp_len + 1);
3846
0
        if(!tmpprefs)
3847
0
            return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
3848
0
                            "Error allocated space for kex method"
3849
0
                            " preferences");
3850
3851
0
        memcpy(tmpprefs, kex_extensions, kex_extensions_len);
3852
0
        memcpy(tmpprefs + kex_extensions_len, newprefs, strlen(newprefs));
3853
0
        tmpprefs[tmp_len] = '\0';
3854
3855
0
        SSH2_FREE(session, newprefs);
3856
0
        newprefs = tmpprefs;
3857
0
    }
3858
3859
0
    if(*prefvar)
3860
0
        SSH2_FREE(session, *prefvar);
3861
0
    *prefvar = newprefs;
3862
3863
0
    return 0;
3864
0
}
3865
3866
/*
3867
 * returns a number of returned algorithms (a positive number) on success,
3868
 * a negative number on failure
3869
 */
3870
int libssh2_session_supported_algs(LIBSSH2_SESSION *session,
3871
                                   int method_type,
3872
                                   const char ***algs)
3873
0
{
3874
0
    unsigned int i;
3875
0
    unsigned int j;
3876
0
    unsigned int ialg;
3877
0
    const struct common_method **mlist;
3878
3879
0
    if(!session)
3880
0
        return LIBSSH2_ERROR_BAD_USE;
3881
3882
    /* to prevent coredumps due to dereferencing of NULL */
3883
0
    if(!algs)
3884
0
        return ssh2_err(session, LIBSSH2_ERROR_BAD_USE,
3885
0
                        "algs must not be NULL");
3886
3887
0
    switch(method_type) {
3888
0
    case LIBSSH2_METHOD_KEX:
3889
0
        mlist = (const struct common_method **)kex_methods;
3890
0
        break;
3891
3892
0
    case LIBSSH2_METHOD_HOSTKEY:
3893
0
        mlist = (const struct common_method **)ssh2_hostkey_methods();
3894
0
        break;
3895
3896
0
    case LIBSSH2_METHOD_CRYPT_CS:
3897
0
    case LIBSSH2_METHOD_CRYPT_SC:
3898
0
        mlist = (const struct common_method **)ssh2_crypt_methods();
3899
0
        break;
3900
3901
0
    case LIBSSH2_METHOD_MAC_CS:
3902
0
    case LIBSSH2_METHOD_MAC_SC:
3903
0
        mlist = (const struct common_method **)ssh2_mac_methods();
3904
0
        break;
3905
3906
0
    case LIBSSH2_METHOD_COMP_CS:
3907
0
    case LIBSSH2_METHOD_COMP_SC:
3908
0
        mlist = (const struct common_method **)ssh2_comp_methods(session);
3909
0
        break;
3910
3911
0
    case LIBSSH2_METHOD_SIGN_ALGO:
3912
        /* no built-in supported list due to backend support */
3913
0
        mlist = NULL;
3914
0
        break;
3915
3916
0
    default:
3917
0
        return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
3918
0
                        "Unrecognized method type");
3919
0
    } /* switch */
3920
3921
    /* weird situation */
3922
0
    if(!mlist)
3923
0
        return ssh2_err(session, LIBSSH2_ERROR_INVAL, "No algorithm found");
3924
3925
    /* mlist is looped through twice. The first time to find the number of
3926
       supported algorithms (needed to allocate the proper size of array) and
3927
       the second time to actually copy the pointers.  Typically this function
3928
       it not called often (typically at the beginning of a session) and
3929
       the number of algorithms (i.e. number of iterations in one loop) are
3930
       not expected to become high (typically it does not exceed 20) for quite
3931
       a long time.
3932
3933
       Thus double looping really should not be an issue and it is definitely
3934
       a better solution than reallocation several times. */
3935
3936
    /* count the number of supported algorithms */
3937
0
    for(i = 0, ialg = 0; mlist[i]; i++) {
3938
        /* do not count fields with NULL name */
3939
0
        if(mlist[i]->name)
3940
0
            ialg++;
3941
0
    }
3942
3943
    /* weird situation, no algorithm found */
3944
0
    if(ialg == 0)
3945
0
        return ssh2_err(session, LIBSSH2_ERROR_INVAL, "No algorithm found");
3946
3947
    /* allocate buffer */
3948
0
    *algs = SSH2_ALLOC(session, ialg * sizeof(const char *));
3949
0
    if(!*algs)
3950
0
        return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
3951
0
                        "Memory allocation failed");
3952
3953
    /* Past this point *algs must be deallocated in case of an error! */
3954
3955
    /* copy non-NULL pointers only */
3956
0
    for(i = 0, j = 0; mlist[i] && j < ialg; i++) {
3957
0
        if(!mlist[i]->name)
3958
            /* maybe a weird situation but if it occurs, do not include NULL
3959
               pointers */
3960
0
            continue;
3961
3962
        /* note that [] has higher priority than * (dereferencing) */
3963
0
        (*algs)[j++] = mlist[i]->name;
3964
0
    }
3965
3966
    /* correct number of pointers copied? (test the code above) */
3967
0
    if(j != ialg) {
3968
0
        SSH2_FREE(session, SSH2_UNCONST(*algs));  /* deallocate buffer */
3969
0
        *algs = NULL;
3970
0
        return ssh2_err(session, LIBSSH2_ERROR_BAD_USE, "Internal error");
3971
0
    }
3972
3973
0
    return ialg;
3974
0
}