Coverage Report

Created: 2025-08-26 06:33

/src/openssh/ssh_api.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: ssh_api.c,v 1.32 2024/10/18 05:14:51 djm Exp $ */
2
/*
3
 * Copyright (c) 2012 Markus Friedl.  All rights reserved.
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include "includes.h"
19
20
#include <sys/types.h>
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
25
#include "ssh_api.h"
26
#include "compat.h"
27
#include "log.h"
28
#include "authfile.h"
29
#include "sshkey.h"
30
#include "dh.h"
31
#include "misc.h"
32
#include "ssh2.h"
33
#include "version.h"
34
#include "myproposal.h"
35
#include "ssherr.h"
36
#include "sshbuf.h"
37
38
#include "openbsd-compat/openssl-compat.h"
39
40
#include <string.h>
41
42
int _ssh_exchange_banner(struct ssh *);
43
int _ssh_send_banner(struct ssh *, struct sshbuf *);
44
int _ssh_read_banner(struct ssh *, struct sshbuf *);
45
int _ssh_order_hostkeyalgs(struct ssh *);
46
int _ssh_verify_host_key(struct sshkey *, struct ssh *);
47
struct sshkey *_ssh_host_public_key(int, int, struct ssh *);
48
struct sshkey *_ssh_host_private_key(int, int, struct ssh *);
49
int _ssh_host_key_sign(struct ssh *, struct sshkey *, struct sshkey *,
50
    u_char **, size_t *, const u_char *, size_t, const char *);
51
52
/*
53
 * stubs for privsep calls in the server side implementation of kex.
54
 */
55
int mm_sshkey_sign(struct sshkey *, u_char **, u_int *,
56
    const u_char *, u_int, const char *, const char *, const char *, u_int);
57
58
#ifdef WITH_OPENSSL
59
DH  *mm_choose_dh(int, int, int);
60
#endif
61
62
int
63
mm_sshkey_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
64
    const u_char *data, u_int datalen, const char *alg,
65
    const char *sk_provider, const char *sk_pin, u_int compat)
66
0
{
67
0
  size_t slen = 0;
68
0
  int ret;
69
70
0
  ret = sshkey_sign(key, sigp, &slen, data, datalen, alg,
71
0
      sk_provider, sk_pin, compat);
72
0
  *lenp = slen;
73
0
  return ret;
74
0
}
75
76
#ifdef WITH_OPENSSL
77
DH *
78
mm_choose_dh(int min, int nbits, int max)
79
193
{
80
193
  return choose_dh(min, nbits, max);
81
193
}
82
#endif
83
84
/* API */
85
86
int
87
ssh_init(struct ssh **sshp, int is_server, struct kex_params *kex_params)
88
178k
{
89
178k
  char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
90
178k
  char *populated[PROPOSAL_MAX];
91
178k
  struct ssh *ssh;
92
178k
  char **proposal;
93
178k
  static int called;
94
178k
  int r;
95
96
178k
  if (!called) {
97
1
    seed_rng();
98
1
    called = 1;
99
1
  }
100
101
178k
  if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL)
102
0
    return SSH_ERR_ALLOC_FAIL;
103
178k
  if (is_server)
104
89.3k
    ssh_packet_set_server(ssh);
105
106
  /* Initialize key exchange */
107
178k
  proposal = kex_params ? kex_params->proposal : myproposal;
108
178k
  kex_proposal_populate_entries(ssh, populated,
109
178k
      proposal[PROPOSAL_KEX_ALGS],
110
178k
      proposal[PROPOSAL_ENC_ALGS_CTOS],
111
178k
      proposal[PROPOSAL_MAC_ALGS_CTOS],
112
178k
      proposal[PROPOSAL_COMP_ALGS_CTOS],
113
178k
      proposal[PROPOSAL_SERVER_HOST_KEY_ALGS]);
114
178k
  r = kex_ready(ssh, populated);
115
178k
  kex_proposal_free_entries(populated);
116
178k
  if (r != 0) {
117
0
    ssh_free(ssh);
118
0
    return r;
119
0
  }
120
121
178k
  ssh->kex->server = is_server;
122
178k
  if (is_server) {
123
89.3k
#ifdef WITH_OPENSSL
124
89.3k
    ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
125
89.3k
    ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
126
89.3k
    ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
127
89.3k
    ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
128
89.3k
    ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
129
89.3k
    ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
130
89.3k
    ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
131
89.3k
# ifdef OPENSSL_HAS_ECC
132
89.3k
    ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
133
89.3k
# endif
134
89.3k
#endif /* WITH_OPENSSL */
135
89.3k
    ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_server;
136
89.3k
    ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
137
89.3k
    ssh->kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_server;
138
89.3k
    ssh->kex->load_host_public_key=&_ssh_host_public_key;
139
89.3k
    ssh->kex->load_host_private_key=&_ssh_host_private_key;
140
89.3k
    ssh->kex->sign=&_ssh_host_key_sign;
141
89.3k
  } else {
142
89.3k
#ifdef WITH_OPENSSL
143
89.3k
    ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
144
89.3k
    ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
145
89.3k
    ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
146
89.3k
    ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
147
89.3k
    ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
148
89.3k
    ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
149
89.3k
    ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
150
89.3k
# ifdef OPENSSL_HAS_ECC
151
89.3k
    ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
152
89.3k
# endif
153
89.3k
#endif /* WITH_OPENSSL */
154
89.3k
    ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
155
89.3k
    ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
156
89.3k
    ssh->kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_client;
157
89.3k
    ssh->kex->verify_host_key =&_ssh_verify_host_key;
158
89.3k
  }
159
178k
  *sshp = ssh;
160
178k
  return 0;
161
178k
}
162
163
void
164
ssh_free(struct ssh *ssh)
165
178k
{
166
178k
  struct key_entry *k;
167
168
178k
  if (ssh == NULL)
169
0
    return;
170
171
  /*
172
   * we've only created the public keys variants in case we
173
   * are a acting as a server.
174
   */
175
357k
  while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
176
178k
    TAILQ_REMOVE(&ssh->public_keys, k, next);
177
178k
    if (ssh->kex && ssh->kex->server)
178
89.3k
      sshkey_free(k->key);
179
178k
    free(k);
180
178k
  }
181
268k
  while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
182
89.3k
    TAILQ_REMOVE(&ssh->private_keys, k, next);
183
89.3k
    free(k);
184
89.3k
  }
185
178k
  ssh_packet_close(ssh);
186
178k
  free(ssh);
187
178k
}
188
189
void
190
ssh_set_app_data(struct ssh *ssh, void *app_data)
191
0
{
192
0
  ssh->app_data = app_data;
193
0
}
194
195
void *
196
ssh_get_app_data(struct ssh *ssh)
197
0
{
198
0
  return ssh->app_data;
199
0
}
200
201
/* Returns < 0 on error, 0 otherwise */
202
int
203
ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
204
178k
{
205
178k
  struct sshkey *pubkey = NULL;
206
178k
  struct key_entry *k = NULL, *k_prv = NULL;
207
178k
  int r;
208
209
178k
  if (ssh->kex->server) {
210
89.3k
    if ((r = sshkey_from_private(key, &pubkey)) != 0)
211
0
      return r;
212
89.3k
    if ((k = malloc(sizeof(*k))) == NULL ||
213
89.3k
        (k_prv = malloc(sizeof(*k_prv))) == NULL) {
214
0
      free(k);
215
0
      sshkey_free(pubkey);
216
0
      return SSH_ERR_ALLOC_FAIL;
217
0
    }
218
89.3k
    k_prv->key = key;
219
89.3k
    TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
220
221
    /* add the public key, too */
222
89.3k
    k->key = pubkey;
223
89.3k
    TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
224
89.3k
    r = 0;
225
89.3k
  } else {
226
89.3k
    if ((k = malloc(sizeof(*k))) == NULL)
227
0
      return SSH_ERR_ALLOC_FAIL;
228
89.3k
    k->key = key;
229
89.3k
    TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
230
89.3k
    r = 0;
231
89.3k
  }
232
233
178k
  return r;
234
178k
}
235
236
int
237
ssh_set_verify_host_key_callback(struct ssh *ssh,
238
    int (*cb)(struct sshkey *, struct ssh *))
239
0
{
240
0
  if (cb == NULL || ssh->kex == NULL)
241
0
    return SSH_ERR_INVALID_ARGUMENT;
242
243
0
  ssh->kex->verify_host_key = cb;
244
245
0
  return 0;
246
0
}
247
248
int
249
ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
250
168k
{
251
168k
  return sshbuf_put(ssh_packet_get_input(ssh), data, len);
252
168k
}
253
254
int
255
ssh_packet_next(struct ssh *ssh, u_char *typep)
256
294k
{
257
294k
  int r;
258
294k
  u_int32_t seqnr;
259
294k
  u_char type;
260
261
  /*
262
   * Try to read a packet. Return SSH_MSG_NONE if no packet or not
263
   * enough data.
264
   */
265
294k
  *typep = SSH_MSG_NONE;
266
294k
  if (sshbuf_len(ssh->kex->client_version) == 0 ||
267
294k
      sshbuf_len(ssh->kex->server_version) == 0)
268
187k
    return _ssh_exchange_banner(ssh);
269
  /*
270
   * If we enough data and a dispatch function then
271
   * call the function and get the next packet.
272
   * Otherwise return the packet type to the caller so it
273
   * can decide how to go on.
274
   *
275
   * We will only call the dispatch function for:
276
   *     20-29    Algorithm negotiation
277
   *     30-49    Key exchange method specific (numbers can be reused for
278
   *              different authentication methods)
279
   */
280
1.38M
  for (;;) {
281
1.38M
    if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
282
9.71k
      return r;
283
1.37M
    if (type > 0 && type < DISPATCH_MAX &&
284
1.37M
        type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
285
1.37M
        ssh->dispatch[type] != NULL) {
286
1.34M
      if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
287
66.1k
        return r;
288
1.34M
    } else {
289
31.1k
      *typep = type;
290
31.1k
      return 0;
291
31.1k
    }
292
1.37M
  }
293
106k
}
294
295
const u_char *
296
ssh_packet_payload(struct ssh *ssh, size_t *lenp)
297
0
{
298
0
  return sshpkt_ptr(ssh, lenp);
299
0
}
300
301
int
302
ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
303
0
{
304
0
  int r;
305
306
0
  if ((r = sshpkt_start(ssh, type)) != 0 ||
307
0
      (r = sshpkt_put(ssh, data, len)) != 0 ||
308
0
      (r = sshpkt_send(ssh)) != 0)
309
0
    return r;
310
0
  return 0;
311
0
}
312
313
const u_char *
314
ssh_output_ptr(struct ssh *ssh, size_t *len)
315
198k
{
316
198k
  struct sshbuf *output = ssh_packet_get_output(ssh);
317
318
198k
  *len = sshbuf_len(output);
319
198k
  return sshbuf_ptr(output);
320
198k
}
321
322
int
323
ssh_output_consume(struct ssh *ssh, size_t len)
324
136k
{
325
136k
  return sshbuf_consume(ssh_packet_get_output(ssh), len);
326
136k
}
327
328
int
329
ssh_output_space(struct ssh *ssh, size_t len)
330
0
{
331
0
  return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
332
0
}
333
334
int
335
ssh_input_space(struct ssh *ssh, size_t len)
336
0
{
337
0
  return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
338
0
}
339
340
/* Read other side's version identification. */
341
int
342
_ssh_read_banner(struct ssh *ssh, struct sshbuf *banner)
343
187k
{
344
187k
  struct sshbuf *input = ssh_packet_get_input(ssh);
345
187k
  const char *mismatch = "Protocol mismatch.\r\n";
346
187k
  const u_char *s = sshbuf_ptr(input);
347
187k
  u_char c;
348
187k
  char *cp = NULL, *remote_version = NULL;
349
187k
  int r = 0, remote_major, remote_minor, expect_nl;
350
187k
  size_t n, j;
351
352
1.56M
  for (j = n = 0;;) {
353
1.56M
    sshbuf_reset(banner);
354
1.56M
    expect_nl = 0;
355
10.6M
    for (;;) {
356
10.6M
      if (j >= sshbuf_len(input))
357
89.3k
        return 0; /* insufficient data in input buf */
358
10.5M
      c = s[j++];
359
10.5M
      if (c == '\r') {
360
52.5k
        expect_nl = 1;
361
52.5k
        continue;
362
52.5k
      }
363
10.5M
      if (c == '\n')
364
1.47M
        break;
365
9.03M
      if (expect_nl)
366
570
        goto bad;
367
9.03M
      if ((r = sshbuf_put_u8(banner, c)) != 0)
368
0
        return r;
369
9.03M
      if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN)
370
180
        goto bad;
371
9.03M
    }
372
1.47M
    if (sshbuf_len(banner) >= 4 &&
373
1.47M
        memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0)
374
91.7k
      break;
375
1.38M
    debug_f("%.*s", (int)sshbuf_len(banner),
376
1.38M
        sshbuf_ptr(banner));
377
    /* Accept lines before banner only on client */
378
1.38M
    if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) {
379
6.51k
  bad:
380
6.51k
      if ((r = sshbuf_put(ssh_packet_get_output(ssh),
381
6.51k
          mismatch, strlen(mismatch))) != 0)
382
0
        return r;
383
6.51k
      return SSH_ERR_NO_PROTOCOL_VERSION;
384
6.51k
    }
385
1.38M
  }
386
91.7k
  if ((r = sshbuf_consume(input, j)) != 0)
387
0
    return r;
388
389
  /* XXX remote version must be the same size as banner for sscanf */
390
91.7k
  if ((cp = sshbuf_dup_string(banner)) == NULL ||
391
91.7k
      (remote_version = calloc(1, sshbuf_len(banner))) == NULL) {
392
105
    r = SSH_ERR_ALLOC_FAIL;
393
105
    goto out;
394
105
  }
395
396
  /*
397
   * Check that the versions match.  In future this might accept
398
   * several versions and set appropriate flags to handle them.
399
   */
400
91.6k
  if (sscanf(cp, "SSH-%d.%d-%[^\n]\n",
401
91.6k
      &remote_major, &remote_minor, remote_version) != 3) {
402
210
    r = SSH_ERR_INVALID_FORMAT;
403
210
    goto out;
404
210
  }
405
91.4k
  debug("Remote protocol version %d.%d, remote software version %.100s",
406
91.4k
      remote_major, remote_minor, remote_version);
407
408
91.4k
  compat_banner(ssh, remote_version);
409
91.4k
  if  (remote_major == 1 && remote_minor == 99) {
410
195
    remote_major = 2;
411
195
    remote_minor = 0;
412
195
  }
413
91.4k
  if (remote_major != 2)
414
3.87k
    r = SSH_ERR_PROTOCOL_MISMATCH;
415
416
91.4k
  debug("Remote version string %.100s", cp);
417
91.7k
 out:
418
91.7k
  free(cp);
419
91.7k
  free(remote_version);
420
91.7k
  return r;
421
91.4k
}
422
423
/* Send our own protocol version identification. */
424
int
425
_ssh_send_banner(struct ssh *ssh, struct sshbuf *banner)
426
133k
{
427
133k
  char *cp;
428
133k
  int r;
429
430
133k
  if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0)
431
0
    return r;
432
133k
  if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0)
433
0
    return r;
434
  /* Remove trailing \r\n */
435
133k
  if ((r = sshbuf_consume_end(banner, 2)) != 0)
436
0
    return r;
437
133k
  if ((cp = sshbuf_dup_string(banner)) == NULL)
438
0
    return SSH_ERR_ALLOC_FAIL;
439
133k
  debug("Local version string %.100s", cp);
440
133k
  free(cp);
441
133k
  return 0;
442
133k
}
443
444
int
445
_ssh_exchange_banner(struct ssh *ssh)
446
187k
{
447
187k
  struct kex *kex = ssh->kex;
448
187k
  int r;
449
450
  /*
451
   * if _ssh_read_banner() cannot parse a full version string
452
   * it will return NULL and we end up calling it again.
453
   */
454
455
187k
  r = 0;
456
187k
  if (kex->server) {
457
138k
    if (sshbuf_len(ssh->kex->server_version) == 0)
458
89.3k
      r = _ssh_send_banner(ssh, ssh->kex->server_version);
459
138k
    if (r == 0 &&
460
138k
        sshbuf_len(ssh->kex->server_version) != 0 &&
461
138k
        sshbuf_len(ssh->kex->client_version) == 0)
462
138k
      r = _ssh_read_banner(ssh, ssh->kex->client_version);
463
138k
  } else {
464
48.7k
    if (sshbuf_len(ssh->kex->server_version) == 0)
465
48.7k
      r = _ssh_read_banner(ssh, ssh->kex->server_version);
466
48.7k
    if (r == 0 &&
467
48.7k
        sshbuf_len(ssh->kex->server_version) != 0 &&
468
48.7k
        sshbuf_len(ssh->kex->client_version) == 0)
469
44.6k
      r = _ssh_send_banner(ssh, ssh->kex->client_version);
470
48.7k
  }
471
187k
  if (r != 0)
472
10.6k
    return r;
473
  /* start initial kex as soon as we have exchanged the banners */
474
176k
  if (sshbuf_len(ssh->kex->server_version) != 0 &&
475
176k
      sshbuf_len(ssh->kex->client_version) != 0) {
476
87.5k
    if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
477
87.5k
        (r = kex_send_kexinit(ssh)) != 0)
478
0
      return r;
479
87.5k
  }
480
176k
  return 0;
481
176k
}
482
483
struct sshkey *
484
_ssh_host_public_key(int type, int nid, struct ssh *ssh)
485
840
{
486
840
  struct key_entry *k;
487
488
840
  debug3_f("need %d", type);
489
840
  TAILQ_FOREACH(k, &ssh->public_keys, next) {
490
840
    debug3_f("check %s", sshkey_type(k->key));
491
840
    if (k->key->type == type &&
492
840
        (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
493
840
      return (k->key);
494
840
  }
495
0
  return (NULL);
496
840
}
497
498
struct sshkey *
499
_ssh_host_private_key(int type, int nid, struct ssh *ssh)
500
840
{
501
840
  struct key_entry *k;
502
503
840
  debug3_f("need %d", type);
504
840
  TAILQ_FOREACH(k, &ssh->private_keys, next) {
505
840
    debug3_f("check %s", sshkey_type(k->key));
506
840
    if (k->key->type == type &&
507
840
        (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
508
840
      return (k->key);
509
840
  }
510
0
  return (NULL);
511
840
}
512
513
int
514
_ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
515
588
{
516
588
  struct key_entry *k;
517
518
588
  debug3_f("need %s", sshkey_type(hostkey));
519
588
  TAILQ_FOREACH(k, &ssh->public_keys, next) {
520
588
    debug3_f("check %s", sshkey_type(k->key));
521
588
    if (sshkey_equal_public(hostkey, k->key))
522
488
      return (0); /* ok */
523
588
  }
524
100
  return (-1); /* failed */
525
588
}
526
527
/* offer hostkey algorithms in kexinit depending on registered keys */
528
int
529
_ssh_order_hostkeyalgs(struct ssh *ssh)
530
87.5k
{
531
87.5k
  struct key_entry *k;
532
87.5k
  char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
533
87.5k
  char **proposal;
534
87.5k
  size_t maxlen;
535
87.5k
  int ktype, nid, r;
536
537
  /* XXX we de-serialize ssh->kex->my, modify it, and change it */
538
87.5k
  if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
539
0
    return r;
540
87.5k
  orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
541
87.5k
  if ((oavail = avail = strdup(orig)) == NULL) {
542
0
    r = SSH_ERR_ALLOC_FAIL;
543
0
    goto out;
544
0
  }
545
87.5k
  maxlen = strlen(avail) + 1;
546
87.5k
  if ((replace = calloc(1, maxlen)) == NULL) {
547
0
    r = SSH_ERR_ALLOC_FAIL;
548
0
    goto out;
549
0
  }
550
87.5k
  *replace = '\0';
551
175k
  while ((alg = strsep(&avail, ",")) && *alg != '\0') {
552
87.5k
    if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
553
0
      continue;
554
87.5k
    nid = sshkey_ecdsa_nid_from_name(alg);
555
87.5k
    TAILQ_FOREACH(k, &ssh->public_keys, next) {
556
87.5k
      if (k->key->type != ktype &&
557
87.5k
          (!sshkey_is_cert(k->key) ||
558
0
          k->key->type != sshkey_type_plain(ktype)))
559
0
        continue;
560
87.5k
      if (sshkey_type_plain(k->key->type) == KEY_ECDSA &&
561
87.5k
          k->key->ecdsa_nid != nid)
562
0
        continue;
563
      /* Candidate */
564
87.5k
      if (*replace != '\0')
565
0
        strlcat(replace, ",", maxlen);
566
87.5k
      strlcat(replace, alg, maxlen);
567
87.5k
      break;
568
87.5k
    }
569
87.5k
  }
570
87.5k
  if (*replace != '\0') {
571
87.5k
    debug2_f("orig/%d    %s", ssh->kex->server, orig);
572
87.5k
    debug2_f("replace/%d %s", ssh->kex->server, replace);
573
87.5k
    free(orig);
574
87.5k
    proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
575
87.5k
    replace = NULL; /* owned by proposal */
576
87.5k
    r = kex_prop2buf(ssh->kex->my, proposal);
577
87.5k
  }
578
87.5k
 out:
579
87.5k
  free(oavail);
580
87.5k
  free(replace);
581
87.5k
  kex_prop_free(proposal);
582
87.5k
  return r;
583
87.5k
}
584
585
int
586
_ssh_host_key_sign(struct ssh *ssh, struct sshkey *privkey,
587
    struct sshkey *pubkey, u_char **signature, size_t *slen,
588
    const u_char *data, size_t dlen, const char *alg)
589
779
{
590
779
  return sshkey_sign(privkey, signature, slen, data, dlen,
591
779
      alg, NULL, NULL, ssh->compat);
592
779
}