blob: ab209c4ca9301443d6bed27bb6f0f2b24e386fb1 [file] [log] [blame]
djm@openbsd.org0a843d92018-12-27 03:25:24 +00001/* $OpenBSD: ssh_api.c,v 1.9 2018/12/27 03:25:25 djm Exp $ */
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +00002/*
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
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000020#include "ssh_api.h"
21#include "compat.h"
22#include "log.h"
23#include "authfile.h"
24#include "sshkey.h"
25#include "misc.h"
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000026#include "ssh2.h"
27#include "version.h"
28#include "myproposal.h"
29#include "ssherr.h"
30#include "sshbuf.h"
31
Darren Tucker31b49522018-10-22 20:05:18 +110032#include "openbsd-compat/openssl-compat.h"
33
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000034#include <string.h>
35
36int _ssh_exchange_banner(struct ssh *);
djm@openbsd.org0a843d92018-12-27 03:25:24 +000037int _ssh_send_banner(struct ssh *, struct sshbuf *);
38int _ssh_read_banner(struct ssh *, struct sshbuf *);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000039int _ssh_order_hostkeyalgs(struct ssh *);
40int _ssh_verify_host_key(struct sshkey *, struct ssh *);
djm@openbsd.org5104db72015-01-26 06:10:03 +000041struct sshkey *_ssh_host_public_key(int, int, struct ssh *);
42struct sshkey *_ssh_host_private_key(int, int, struct ssh *);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000043int _ssh_host_key_sign(struct sshkey *, struct sshkey *,
44 u_char **, size_t *, const u_char *, size_t, const char *, u_int);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000045
46/*
47 * stubs for the server side implementation of kex.
48 * disable privsep so our stubs will never be called.
49 */
50int use_privsep = 0;
51int mm_sshkey_sign(struct sshkey *, u_char **, u_int *,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000052 u_char *, u_int, char *, u_int);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000053DH *mm_choose_dh(int, int, int);
54
55/* Define these two variables here so that they are part of the library */
56u_char *session_id2 = NULL;
57u_int session_id2_len = 0;
58
59int
60mm_sshkey_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000061 u_char *data, u_int datalen, char *alg, u_int compat)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000062{
63 return (-1);
64}
65
66DH *
67mm_choose_dh(int min, int nbits, int max)
68{
69 return (NULL);
70}
71
72/* API */
73
74int
75ssh_init(struct ssh **sshp, int is_server, struct kex_params *kex_params)
76{
77 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
78 struct ssh *ssh;
79 char **proposal;
80 static int called;
81 int r;
82
83 if (!called) {
Damien Miller42c5ec42018-11-23 10:40:06 +110084 seed_rng();
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000085 called = 1;
86 }
87
djm@openbsd.org4509b5d2015-01-30 01:13:33 +000088 if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL)
89 return SSH_ERR_ALLOC_FAIL;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000090 if (is_server)
91 ssh_packet_set_server(ssh);
92
93 /* Initialize key exchange */
94 proposal = kex_params ? kex_params->proposal : myproposal;
djm@openbsd.org0a843d92018-12-27 03:25:24 +000095 if ((r = kex_ready(ssh, proposal)) != 0) {
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000096 ssh_free(ssh);
97 return r;
98 }
99 ssh->kex->server = is_server;
100 if (is_server) {
101#ifdef WITH_OPENSSL
102 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
103 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000104 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
105 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
106 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000107 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
108 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100109# ifdef OPENSSL_HAS_ECC
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000110 ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100111# endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000112#endif /* WITH_OPENSSL */
113 ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_server;
114 ssh->kex->load_host_public_key=&_ssh_host_public_key;
115 ssh->kex->load_host_private_key=&_ssh_host_private_key;
116 ssh->kex->sign=&_ssh_host_key_sign;
117 } else {
118#ifdef WITH_OPENSSL
119 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
120 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000121 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
122 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
123 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000124 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
125 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100126# ifdef OPENSSL_HAS_ECC
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000127 ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100128# endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000129#endif /* WITH_OPENSSL */
130 ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
131 ssh->kex->verify_host_key =&_ssh_verify_host_key;
132 }
133 *sshp = ssh;
134 return 0;
135}
136
137void
138ssh_free(struct ssh *ssh)
139{
140 struct key_entry *k;
141
142 ssh_packet_close(ssh);
143 /*
144 * we've only created the public keys variants in case we
145 * are a acting as a server.
146 */
147 while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
148 TAILQ_REMOVE(&ssh->public_keys, k, next);
149 if (ssh->kex && ssh->kex->server)
150 sshkey_free(k->key);
151 free(k);
152 }
153 while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
154 TAILQ_REMOVE(&ssh->private_keys, k, next);
155 free(k);
156 }
157 if (ssh->kex)
158 kex_free(ssh->kex);
159 free(ssh);
160}
161
162void
163ssh_set_app_data(struct ssh *ssh, void *app_data)
164{
165 ssh->app_data = app_data;
166}
167
168void *
169ssh_get_app_data(struct ssh *ssh)
170{
171 return ssh->app_data;
172}
173
174/* Returns < 0 on error, 0 otherwise */
175int
176ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
177{
178 struct sshkey *pubkey = NULL;
179 struct key_entry *k = NULL, *k_prv = NULL;
180 int r;
181
182 if (ssh->kex->server) {
183 if ((r = sshkey_from_private(key, &pubkey)) != 0)
184 return r;
185 if ((k = malloc(sizeof(*k))) == NULL ||
186 (k_prv = malloc(sizeof(*k_prv))) == NULL) {
187 free(k);
188 sshkey_free(pubkey);
189 return SSH_ERR_ALLOC_FAIL;
190 }
191 k_prv->key = key;
192 TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
193
194 /* add the public key, too */
195 k->key = pubkey;
196 TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
197 r = 0;
198 } else {
199 if ((k = malloc(sizeof(*k))) == NULL)
200 return SSH_ERR_ALLOC_FAIL;
201 k->key = key;
202 TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
203 r = 0;
204 }
205
206 return r;
207}
208
209int
210ssh_set_verify_host_key_callback(struct ssh *ssh,
211 int (*cb)(struct sshkey *, struct ssh *))
212{
213 if (cb == NULL || ssh->kex == NULL)
214 return SSH_ERR_INVALID_ARGUMENT;
215
216 ssh->kex->verify_host_key = cb;
217
218 return 0;
219}
220
221int
222ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
223{
224 return sshbuf_put(ssh_packet_get_input(ssh), data, len);
225}
226
227int
228ssh_packet_next(struct ssh *ssh, u_char *typep)
229{
230 int r;
231 u_int32_t seqnr;
232 u_char type;
233
234 /*
235 * Try to read a packet. Return SSH_MSG_NONE if no packet or not
236 * enough data.
237 */
238 *typep = SSH_MSG_NONE;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000239 if (sshbuf_len(ssh->kex->client_version) == 0 ||
240 sshbuf_len(ssh->kex->server_version) == 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000241 return _ssh_exchange_banner(ssh);
242 /*
243 * If we enough data and a dispatch function then
244 * call the function and get the next packet.
245 * Otherwise return the packet type to the caller so it
246 * can decide how to go on.
247 *
248 * We will only call the dispatch function for:
249 * 20-29 Algorithm negotiation
250 * 30-49 Key exchange method specific (numbers can be reused for
251 * different authentication methods)
252 */
253 for (;;) {
254 if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
255 return r;
256 if (type > 0 && type < DISPATCH_MAX &&
257 type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
258 ssh->dispatch[type] != NULL) {
259 if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
260 return r;
261 } else {
262 *typep = type;
263 return 0;
264 }
265 }
266}
267
268const u_char *
269ssh_packet_payload(struct ssh *ssh, size_t *lenp)
270{
271 return sshpkt_ptr(ssh, lenp);
272}
273
274int
275ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
276{
277 int r;
278
279 if ((r = sshpkt_start(ssh, type)) != 0 ||
280 (r = sshpkt_put(ssh, data, len)) != 0 ||
281 (r = sshpkt_send(ssh)) != 0)
282 return r;
283 return 0;
284}
285
286const u_char *
287ssh_output_ptr(struct ssh *ssh, size_t *len)
288{
289 struct sshbuf *output = ssh_packet_get_output(ssh);
290
291 *len = sshbuf_len(output);
292 return sshbuf_ptr(output);
293}
294
295int
296ssh_output_consume(struct ssh *ssh, size_t len)
297{
298 return sshbuf_consume(ssh_packet_get_output(ssh), len);
299}
300
301int
302ssh_output_space(struct ssh *ssh, size_t len)
303{
304 return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
305}
306
307int
308ssh_input_space(struct ssh *ssh, size_t len)
309{
310 return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
311}
312
313/* Read other side's version identification. */
314int
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000315_ssh_read_banner(struct ssh *ssh, struct sshbuf *banner)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000316{
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000317 struct sshbuf *input = ssh_packet_get_input(ssh);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000318 const char *mismatch = "Protocol mismatch.\r\n";
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000319 const u_char *s = sshbuf_ptr(input);
320 u_char c;
321 char *cp, *remote_version;
322 int r, remote_major, remote_minor, expect_nl;
323 size_t n, j;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000324
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000325 for (j = n = 0;;) {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000326 sshbuf_reset(banner);
327 expect_nl = 0;
328 for (;;) {
329 if (j >= sshbuf_len(input))
330 return 0; /* insufficient data in input buf */
331 c = s[j++];
332 if (c == '\r') {
333 expect_nl = 1;
334 continue;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000335 }
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000336 if (c == '\n')
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000337 break;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000338 if (expect_nl)
339 goto bad;
340 if ((r = sshbuf_put_u8(banner, c)) != 0)
341 return r;
342 if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN)
343 goto bad;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000344 }
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000345 if (sshbuf_len(banner) >= 4 &&
346 memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000347 break;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000348 if ((cp = sshbuf_dup_string(banner)) == NULL)
349 return SSH_ERR_ALLOC_FAIL;
350 debug("%s: %s", __func__, cp);
351 free(cp);
352 /* Accept lines before banner only on client */
353 if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) {
354 bad:
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000355 if ((r = sshbuf_put(ssh_packet_get_output(ssh),
356 mismatch, strlen(mismatch))) != 0)
357 return r;
358 return SSH_ERR_NO_PROTOCOL_VERSION;
359 }
360 }
361 if ((r = sshbuf_consume(input, j)) != 0)
362 return r;
363
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000364 if ((cp = sshbuf_dup_string(banner)) == NULL)
365 return SSH_ERR_ALLOC_FAIL;
366 /* XXX remote version must be the same size as banner for sscanf */
367 if ((remote_version = calloc(1, sshbuf_len(banner))) == NULL)
368 return SSH_ERR_ALLOC_FAIL;
369
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000370 /*
371 * Check that the versions match. In future this might accept
372 * several versions and set appropriate flags to handle them.
373 */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000374 if (sscanf(cp, "SSH-%d.%d-%[^\n]\n",
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000375 &remote_major, &remote_minor, remote_version) != 3)
376 return SSH_ERR_INVALID_FORMAT;
377 debug("Remote protocol version %d.%d, remote software version %.100s",
378 remote_major, remote_minor, remote_version);
379
380 ssh->compat = compat_datafellows(remote_version);
381 if (remote_major == 1 && remote_minor == 99) {
382 remote_major = 2;
383 remote_minor = 0;
384 }
385 if (remote_major != 2)
386 return SSH_ERR_PROTOCOL_MISMATCH;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000387 debug("Remote version string %.100s", cp);
388 free(cp);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000389 return 0;
390}
391
392/* Send our own protocol version identification. */
393int
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000394_ssh_send_banner(struct ssh *ssh, struct sshbuf *banner)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000395{
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000396 char *cp;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000397 int r;
398
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000399 if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000400 return r;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000401 if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0)
402 return r;
403 /* Remove trailing \r\n */
404 if ((r = sshbuf_consume_end(banner, 2)) != 0)
405 return r;
406 if ((cp = sshbuf_dup_string(banner)) == NULL)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000407 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000408 debug("Local version string %.100s", cp);
409 free(cp);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000410 return 0;
411}
412
413int
414_ssh_exchange_banner(struct ssh *ssh)
415{
416 struct kex *kex = ssh->kex;
417 int r;
418
419 /*
420 * if _ssh_read_banner() cannot parse a full version string
421 * it will return NULL and we end up calling it again.
422 */
423
424 r = 0;
425 if (kex->server) {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000426 if (sshbuf_len(ssh->kex->server_version) == 0)
427 r = _ssh_send_banner(ssh, ssh->kex->server_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000428 if (r == 0 &&
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000429 sshbuf_len(ssh->kex->server_version) != 0 &&
430 sshbuf_len(ssh->kex->client_version) == 0)
431 r = _ssh_read_banner(ssh, ssh->kex->client_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000432 } else {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000433 if (sshbuf_len(ssh->kex->server_version) == 0)
434 r = _ssh_read_banner(ssh, ssh->kex->server_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000435 if (r == 0 &&
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000436 sshbuf_len(ssh->kex->server_version) != 0 &&
437 sshbuf_len(ssh->kex->client_version) == 0)
438 r = _ssh_send_banner(ssh, ssh->kex->client_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000439 }
440 if (r != 0)
441 return r;
442 /* start initial kex as soon as we have exchanged the banners */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000443 if (sshbuf_len(ssh->kex->server_version) != 0 &&
444 sshbuf_len(ssh->kex->client_version) != 0) {
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000445 if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
446 (r = kex_send_kexinit(ssh)) != 0)
447 return r;
448 }
449 return 0;
450}
451
452struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000453_ssh_host_public_key(int type, int nid, struct ssh *ssh)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000454{
455 struct key_entry *k;
456
457 debug3("%s: need %d", __func__, type);
458 TAILQ_FOREACH(k, &ssh->public_keys, next) {
459 debug3("%s: check %s", __func__, sshkey_type(k->key));
djm@openbsd.org5104db72015-01-26 06:10:03 +0000460 if (k->key->type == type &&
461 (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000462 return (k->key);
463 }
464 return (NULL);
465}
466
467struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000468_ssh_host_private_key(int type, int nid, struct ssh *ssh)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000469{
470 struct key_entry *k;
471
472 debug3("%s: need %d", __func__, type);
473 TAILQ_FOREACH(k, &ssh->private_keys, next) {
474 debug3("%s: check %s", __func__, sshkey_type(k->key));
djm@openbsd.org5104db72015-01-26 06:10:03 +0000475 if (k->key->type == type &&
476 (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000477 return (k->key);
478 }
479 return (NULL);
480}
481
482int
483_ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
484{
485 struct key_entry *k;
486
487 debug3("%s: need %s", __func__, sshkey_type(hostkey));
488 TAILQ_FOREACH(k, &ssh->public_keys, next) {
489 debug3("%s: check %s", __func__, sshkey_type(k->key));
490 if (sshkey_equal_public(hostkey, k->key))
491 return (0); /* ok */
492 }
493 return (-1); /* failed */
494}
495
496/* offer hostkey algorithms in kexinit depending on registered keys */
497int
498_ssh_order_hostkeyalgs(struct ssh *ssh)
499{
500 struct key_entry *k;
501 char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
502 char **proposal;
503 size_t maxlen;
504 int ktype, r;
505
506 /* XXX we de-serialize ssh->kex->my, modify it, and change it */
507 if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
508 return r;
509 orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
510 if ((oavail = avail = strdup(orig)) == NULL) {
511 r = SSH_ERR_ALLOC_FAIL;
512 goto out;
513 }
514 maxlen = strlen(avail) + 1;
515 if ((replace = calloc(1, maxlen)) == NULL) {
516 r = SSH_ERR_ALLOC_FAIL;
517 goto out;
518 }
519 *replace = '\0';
520 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
521 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
522 continue;
523 TAILQ_FOREACH(k, &ssh->public_keys, next) {
524 if (k->key->type == ktype ||
525 (sshkey_is_cert(k->key) && k->key->type ==
526 sshkey_type_plain(ktype))) {
527 if (*replace != '\0')
528 strlcat(replace, ",", maxlen);
529 strlcat(replace, alg, maxlen);
530 break;
531 }
532 }
533 }
534 if (*replace != '\0') {
535 debug2("%s: orig/%d %s", __func__, ssh->kex->server, orig);
536 debug2("%s: replace/%d %s", __func__, ssh->kex->server, replace);
537 free(orig);
538 proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
539 replace = NULL; /* owned by proposal */
540 r = kex_prop2buf(ssh->kex->my, proposal);
541 }
542 out:
543 free(oavail);
544 free(replace);
545 kex_prop_free(proposal);
546 return r;
547}
548
549int
550_ssh_host_key_sign(struct sshkey *privkey, struct sshkey *pubkey,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000551 u_char **signature, size_t *slen, const u_char *data, size_t dlen,
552 const char *alg, u_int compat)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000553{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000554 return sshkey_sign(privkey, signature, slen, data, dlen, alg, compat);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000555}