blob: 57509973b5103f634ea1a8bb92b14e44d4ed3a1c [file] [log] [blame]
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00001/* $OpenBSD: ssh_api.c,v 1.15 2019/01/21 10:38:54 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 *);
djm@openbsd.org04c091f2019-01-19 21:43:56 +000043int _ssh_host_key_sign(struct ssh *, struct sshkey *, struct sshkey *,
44 u_char **, size_t *, const u_char *, size_t, const char *);
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
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000102 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
103 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
104 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
105 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
106 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_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
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000110 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100111# endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000112#endif /* WITH_OPENSSL */
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000113 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_server;
114 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000115 ssh->kex->load_host_public_key=&_ssh_host_public_key;
116 ssh->kex->load_host_private_key=&_ssh_host_private_key;
117 ssh->kex->sign=&_ssh_host_key_sign;
118 } else {
119#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000120 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
121 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
122 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
123 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
124 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000125 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
126 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100127# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000128 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100129# endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000130#endif /* WITH_OPENSSL */
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000131 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
132 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000133 ssh->kex->verify_host_key =&_ssh_verify_host_key;
134 }
135 *sshp = ssh;
136 return 0;
137}
138
139void
140ssh_free(struct ssh *ssh)
141{
142 struct key_entry *k;
143
144 ssh_packet_close(ssh);
145 /*
146 * we've only created the public keys variants in case we
147 * are a acting as a server.
148 */
149 while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
150 TAILQ_REMOVE(&ssh->public_keys, k, next);
151 if (ssh->kex && ssh->kex->server)
152 sshkey_free(k->key);
153 free(k);
154 }
155 while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
156 TAILQ_REMOVE(&ssh->private_keys, k, next);
157 free(k);
158 }
159 if (ssh->kex)
160 kex_free(ssh->kex);
161 free(ssh);
162}
163
164void
165ssh_set_app_data(struct ssh *ssh, void *app_data)
166{
167 ssh->app_data = app_data;
168}
169
170void *
171ssh_get_app_data(struct ssh *ssh)
172{
173 return ssh->app_data;
174}
175
176/* Returns < 0 on error, 0 otherwise */
177int
178ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
179{
180 struct sshkey *pubkey = NULL;
181 struct key_entry *k = NULL, *k_prv = NULL;
182 int r;
183
184 if (ssh->kex->server) {
185 if ((r = sshkey_from_private(key, &pubkey)) != 0)
186 return r;
187 if ((k = malloc(sizeof(*k))) == NULL ||
188 (k_prv = malloc(sizeof(*k_prv))) == NULL) {
189 free(k);
190 sshkey_free(pubkey);
191 return SSH_ERR_ALLOC_FAIL;
192 }
193 k_prv->key = key;
194 TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
195
196 /* add the public key, too */
197 k->key = pubkey;
198 TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
199 r = 0;
200 } else {
201 if ((k = malloc(sizeof(*k))) == NULL)
202 return SSH_ERR_ALLOC_FAIL;
203 k->key = key;
204 TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
205 r = 0;
206 }
207
208 return r;
209}
210
211int
212ssh_set_verify_host_key_callback(struct ssh *ssh,
213 int (*cb)(struct sshkey *, struct ssh *))
214{
215 if (cb == NULL || ssh->kex == NULL)
216 return SSH_ERR_INVALID_ARGUMENT;
217
218 ssh->kex->verify_host_key = cb;
219
220 return 0;
221}
222
223int
224ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
225{
226 return sshbuf_put(ssh_packet_get_input(ssh), data, len);
227}
228
229int
230ssh_packet_next(struct ssh *ssh, u_char *typep)
231{
232 int r;
233 u_int32_t seqnr;
234 u_char type;
235
236 /*
237 * Try to read a packet. Return SSH_MSG_NONE if no packet or not
238 * enough data.
239 */
240 *typep = SSH_MSG_NONE;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000241 if (sshbuf_len(ssh->kex->client_version) == 0 ||
242 sshbuf_len(ssh->kex->server_version) == 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000243 return _ssh_exchange_banner(ssh);
244 /*
245 * If we enough data and a dispatch function then
246 * call the function and get the next packet.
247 * Otherwise return the packet type to the caller so it
248 * can decide how to go on.
249 *
250 * We will only call the dispatch function for:
251 * 20-29 Algorithm negotiation
252 * 30-49 Key exchange method specific (numbers can be reused for
253 * different authentication methods)
254 */
255 for (;;) {
256 if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
257 return r;
258 if (type > 0 && type < DISPATCH_MAX &&
259 type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
260 ssh->dispatch[type] != NULL) {
261 if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
262 return r;
263 } else {
264 *typep = type;
265 return 0;
266 }
267 }
268}
269
270const u_char *
271ssh_packet_payload(struct ssh *ssh, size_t *lenp)
272{
273 return sshpkt_ptr(ssh, lenp);
274}
275
276int
277ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
278{
279 int r;
280
281 if ((r = sshpkt_start(ssh, type)) != 0 ||
282 (r = sshpkt_put(ssh, data, len)) != 0 ||
283 (r = sshpkt_send(ssh)) != 0)
284 return r;
285 return 0;
286}
287
288const u_char *
289ssh_output_ptr(struct ssh *ssh, size_t *len)
290{
291 struct sshbuf *output = ssh_packet_get_output(ssh);
292
293 *len = sshbuf_len(output);
294 return sshbuf_ptr(output);
295}
296
297int
298ssh_output_consume(struct ssh *ssh, size_t len)
299{
300 return sshbuf_consume(ssh_packet_get_output(ssh), len);
301}
302
303int
304ssh_output_space(struct ssh *ssh, size_t len)
305{
306 return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
307}
308
309int
310ssh_input_space(struct ssh *ssh, size_t len)
311{
312 return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
313}
314
315/* Read other side's version identification. */
316int
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000317_ssh_read_banner(struct ssh *ssh, struct sshbuf *banner)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000318{
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000319 struct sshbuf *input = ssh_packet_get_input(ssh);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000320 const char *mismatch = "Protocol mismatch.\r\n";
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000321 const u_char *s = sshbuf_ptr(input);
322 u_char c;
323 char *cp, *remote_version;
324 int r, remote_major, remote_minor, expect_nl;
325 size_t n, j;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000326
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000327 for (j = n = 0;;) {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000328 sshbuf_reset(banner);
329 expect_nl = 0;
330 for (;;) {
331 if (j >= sshbuf_len(input))
332 return 0; /* insufficient data in input buf */
333 c = s[j++];
334 if (c == '\r') {
335 expect_nl = 1;
336 continue;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000337 }
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000338 if (c == '\n')
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000339 break;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000340 if (expect_nl)
341 goto bad;
342 if ((r = sshbuf_put_u8(banner, c)) != 0)
343 return r;
344 if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN)
345 goto bad;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000346 }
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000347 if (sshbuf_len(banner) >= 4 &&
348 memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000349 break;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000350 if ((cp = sshbuf_dup_string(banner)) == NULL)
351 return SSH_ERR_ALLOC_FAIL;
352 debug("%s: %s", __func__, cp);
353 free(cp);
354 /* Accept lines before banner only on client */
355 if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) {
356 bad:
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000357 if ((r = sshbuf_put(ssh_packet_get_output(ssh),
358 mismatch, strlen(mismatch))) != 0)
359 return r;
360 return SSH_ERR_NO_PROTOCOL_VERSION;
361 }
362 }
363 if ((r = sshbuf_consume(input, j)) != 0)
364 return r;
365
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000366 if ((cp = sshbuf_dup_string(banner)) == NULL)
367 return SSH_ERR_ALLOC_FAIL;
368 /* XXX remote version must be the same size as banner for sscanf */
369 if ((remote_version = calloc(1, sshbuf_len(banner))) == NULL)
370 return SSH_ERR_ALLOC_FAIL;
371
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000372 /*
373 * Check that the versions match. In future this might accept
374 * several versions and set appropriate flags to handle them.
375 */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000376 if (sscanf(cp, "SSH-%d.%d-%[^\n]\n",
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000377 &remote_major, &remote_minor, remote_version) != 3)
378 return SSH_ERR_INVALID_FORMAT;
379 debug("Remote protocol version %d.%d, remote software version %.100s",
380 remote_major, remote_minor, remote_version);
381
382 ssh->compat = compat_datafellows(remote_version);
383 if (remote_major == 1 && remote_minor == 99) {
384 remote_major = 2;
385 remote_minor = 0;
386 }
387 if (remote_major != 2)
388 return SSH_ERR_PROTOCOL_MISMATCH;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000389 debug("Remote version string %.100s", cp);
390 free(cp);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000391 return 0;
392}
393
394/* Send our own protocol version identification. */
395int
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000396_ssh_send_banner(struct ssh *ssh, struct sshbuf *banner)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000397{
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000398 char *cp;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000399 int r;
400
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000401 if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000402 return r;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000403 if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0)
404 return r;
405 /* Remove trailing \r\n */
406 if ((r = sshbuf_consume_end(banner, 2)) != 0)
407 return r;
408 if ((cp = sshbuf_dup_string(banner)) == NULL)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000409 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000410 debug("Local version string %.100s", cp);
411 free(cp);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000412 return 0;
413}
414
415int
416_ssh_exchange_banner(struct ssh *ssh)
417{
418 struct kex *kex = ssh->kex;
419 int r;
420
421 /*
422 * if _ssh_read_banner() cannot parse a full version string
423 * it will return NULL and we end up calling it again.
424 */
425
426 r = 0;
427 if (kex->server) {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000428 if (sshbuf_len(ssh->kex->server_version) == 0)
429 r = _ssh_send_banner(ssh, ssh->kex->server_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000430 if (r == 0 &&
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000431 sshbuf_len(ssh->kex->server_version) != 0 &&
432 sshbuf_len(ssh->kex->client_version) == 0)
433 r = _ssh_read_banner(ssh, ssh->kex->client_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000434 } else {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000435 if (sshbuf_len(ssh->kex->server_version) == 0)
436 r = _ssh_read_banner(ssh, ssh->kex->server_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000437 if (r == 0 &&
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000438 sshbuf_len(ssh->kex->server_version) != 0 &&
439 sshbuf_len(ssh->kex->client_version) == 0)
440 r = _ssh_send_banner(ssh, ssh->kex->client_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000441 }
442 if (r != 0)
443 return r;
444 /* start initial kex as soon as we have exchanged the banners */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000445 if (sshbuf_len(ssh->kex->server_version) != 0 &&
446 sshbuf_len(ssh->kex->client_version) != 0) {
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000447 if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
448 (r = kex_send_kexinit(ssh)) != 0)
449 return r;
450 }
451 return 0;
452}
453
454struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000455_ssh_host_public_key(int type, int nid, struct ssh *ssh)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000456{
457 struct key_entry *k;
458
459 debug3("%s: need %d", __func__, type);
460 TAILQ_FOREACH(k, &ssh->public_keys, next) {
461 debug3("%s: check %s", __func__, sshkey_type(k->key));
djm@openbsd.org5104db72015-01-26 06:10:03 +0000462 if (k->key->type == type &&
463 (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000464 return (k->key);
465 }
466 return (NULL);
467}
468
469struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000470_ssh_host_private_key(int type, int nid, struct ssh *ssh)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000471{
472 struct key_entry *k;
473
474 debug3("%s: need %d", __func__, type);
475 TAILQ_FOREACH(k, &ssh->private_keys, next) {
476 debug3("%s: check %s", __func__, sshkey_type(k->key));
djm@openbsd.org5104db72015-01-26 06:10:03 +0000477 if (k->key->type == type &&
478 (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000479 return (k->key);
480 }
481 return (NULL);
482}
483
484int
485_ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
486{
487 struct key_entry *k;
488
489 debug3("%s: need %s", __func__, sshkey_type(hostkey));
490 TAILQ_FOREACH(k, &ssh->public_keys, next) {
491 debug3("%s: check %s", __func__, sshkey_type(k->key));
492 if (sshkey_equal_public(hostkey, k->key))
493 return (0); /* ok */
494 }
495 return (-1); /* failed */
496}
497
498/* offer hostkey algorithms in kexinit depending on registered keys */
499int
500_ssh_order_hostkeyalgs(struct ssh *ssh)
501{
502 struct key_entry *k;
503 char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
504 char **proposal;
505 size_t maxlen;
506 int ktype, r;
507
508 /* XXX we de-serialize ssh->kex->my, modify it, and change it */
509 if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
510 return r;
511 orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
512 if ((oavail = avail = strdup(orig)) == NULL) {
513 r = SSH_ERR_ALLOC_FAIL;
514 goto out;
515 }
516 maxlen = strlen(avail) + 1;
517 if ((replace = calloc(1, maxlen)) == NULL) {
518 r = SSH_ERR_ALLOC_FAIL;
519 goto out;
520 }
521 *replace = '\0';
522 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
523 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
524 continue;
525 TAILQ_FOREACH(k, &ssh->public_keys, next) {
526 if (k->key->type == ktype ||
527 (sshkey_is_cert(k->key) && k->key->type ==
528 sshkey_type_plain(ktype))) {
529 if (*replace != '\0')
530 strlcat(replace, ",", maxlen);
531 strlcat(replace, alg, maxlen);
532 break;
533 }
534 }
535 }
536 if (*replace != '\0') {
537 debug2("%s: orig/%d %s", __func__, ssh->kex->server, orig);
538 debug2("%s: replace/%d %s", __func__, ssh->kex->server, replace);
539 free(orig);
540 proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
541 replace = NULL; /* owned by proposal */
542 r = kex_prop2buf(ssh->kex->my, proposal);
543 }
544 out:
545 free(oavail);
546 free(replace);
547 kex_prop_free(proposal);
548 return r;
549}
550
551int
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000552_ssh_host_key_sign(struct ssh *ssh, struct sshkey *privkey,
553 struct sshkey *pubkey, u_char **signature, size_t *slen,
554 const u_char *data, size_t dlen, const char *alg)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000555{
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000556 return sshkey_sign(privkey, signature, slen, data, dlen,
557 alg, ssh->compat);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000558}