blob: 6ea40b5e7a537dc09a65fa088f5161478d55290e [file] [log] [blame]
djm@openbsd.org670104b2019-09-06 05:23:55 +00001/* $OpenBSD: ssh_api.c,v 1.17 2019/09/06 05:23:55 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
djm@openbsd.orgbe02d7c2019-09-06 04:53:27 +000020#include <sys/types.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000025#include "ssh_api.h"
26#include "compat.h"
27#include "log.h"
28#include "authfile.h"
29#include "sshkey.h"
30#include "misc.h"
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000031#include "ssh2.h"
32#include "version.h"
33#include "myproposal.h"
34#include "ssherr.h"
35#include "sshbuf.h"
36
Darren Tucker31b49522018-10-22 20:05:18 +110037#include "openbsd-compat/openssl-compat.h"
38
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000039#include <string.h>
40
41int _ssh_exchange_banner(struct ssh *);
djm@openbsd.org0a843d92018-12-27 03:25:24 +000042int _ssh_send_banner(struct ssh *, struct sshbuf *);
43int _ssh_read_banner(struct ssh *, struct sshbuf *);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000044int _ssh_order_hostkeyalgs(struct ssh *);
45int _ssh_verify_host_key(struct sshkey *, struct ssh *);
djm@openbsd.org5104db72015-01-26 06:10:03 +000046struct sshkey *_ssh_host_public_key(int, int, struct ssh *);
47struct sshkey *_ssh_host_private_key(int, int, struct ssh *);
djm@openbsd.org04c091f2019-01-19 21:43:56 +000048int _ssh_host_key_sign(struct ssh *, struct sshkey *, struct sshkey *,
49 u_char **, size_t *, const u_char *, size_t, const char *);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000050
51/*
52 * stubs for the server side implementation of kex.
53 * disable privsep so our stubs will never be called.
54 */
55int use_privsep = 0;
56int mm_sshkey_sign(struct sshkey *, u_char **, u_int *,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000057 u_char *, u_int, char *, u_int);
djm@openbsd.org670104b2019-09-06 05:23:55 +000058
59#ifdef WITH_OPENSSL
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000060DH *mm_choose_dh(int, int, int);
djm@openbsd.org670104b2019-09-06 05:23:55 +000061#endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000062
63/* Define these two variables here so that they are part of the library */
64u_char *session_id2 = NULL;
65u_int session_id2_len = 0;
66
67int
68mm_sshkey_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000069 u_char *data, u_int datalen, char *alg, u_int compat)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000070{
71 return (-1);
72}
73
djm@openbsd.org670104b2019-09-06 05:23:55 +000074#ifdef WITH_OPENSSL
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000075DH *
76mm_choose_dh(int min, int nbits, int max)
77{
78 return (NULL);
79}
djm@openbsd.org670104b2019-09-06 05:23:55 +000080#endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000081
82/* API */
83
84int
85ssh_init(struct ssh **sshp, int is_server, struct kex_params *kex_params)
86{
87 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
88 struct ssh *ssh;
89 char **proposal;
90 static int called;
91 int r;
92
93 if (!called) {
Damien Miller42c5ec42018-11-23 10:40:06 +110094 seed_rng();
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +000095 called = 1;
96 }
97
djm@openbsd.org4509b5d2015-01-30 01:13:33 +000098 if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL)
99 return SSH_ERR_ALLOC_FAIL;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000100 if (is_server)
101 ssh_packet_set_server(ssh);
102
103 /* Initialize key exchange */
104 proposal = kex_params ? kex_params->proposal : myproposal;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000105 if ((r = kex_ready(ssh, proposal)) != 0) {
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000106 ssh_free(ssh);
107 return r;
108 }
109 ssh->kex->server = is_server;
110 if (is_server) {
111#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000112 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
113 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
114 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
115 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
116 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000117 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
118 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100119# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000120 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100121# endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000122#endif /* WITH_OPENSSL */
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000123 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_server;
124 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000125 ssh->kex->load_host_public_key=&_ssh_host_public_key;
126 ssh->kex->load_host_private_key=&_ssh_host_private_key;
127 ssh->kex->sign=&_ssh_host_key_sign;
128 } else {
129#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000130 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
131 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
132 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
133 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
134 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000135 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
136 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100137# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000138 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100139# endif
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000140#endif /* WITH_OPENSSL */
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000141 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
142 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000143 ssh->kex->verify_host_key =&_ssh_verify_host_key;
144 }
145 *sshp = ssh;
146 return 0;
147}
148
149void
150ssh_free(struct ssh *ssh)
151{
152 struct key_entry *k;
153
154 ssh_packet_close(ssh);
155 /*
156 * we've only created the public keys variants in case we
157 * are a acting as a server.
158 */
159 while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
160 TAILQ_REMOVE(&ssh->public_keys, k, next);
161 if (ssh->kex && ssh->kex->server)
162 sshkey_free(k->key);
163 free(k);
164 }
165 while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
166 TAILQ_REMOVE(&ssh->private_keys, k, next);
167 free(k);
168 }
169 if (ssh->kex)
170 kex_free(ssh->kex);
171 free(ssh);
172}
173
174void
175ssh_set_app_data(struct ssh *ssh, void *app_data)
176{
177 ssh->app_data = app_data;
178}
179
180void *
181ssh_get_app_data(struct ssh *ssh)
182{
183 return ssh->app_data;
184}
185
186/* Returns < 0 on error, 0 otherwise */
187int
188ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
189{
190 struct sshkey *pubkey = NULL;
191 struct key_entry *k = NULL, *k_prv = NULL;
192 int r;
193
194 if (ssh->kex->server) {
195 if ((r = sshkey_from_private(key, &pubkey)) != 0)
196 return r;
197 if ((k = malloc(sizeof(*k))) == NULL ||
198 (k_prv = malloc(sizeof(*k_prv))) == NULL) {
199 free(k);
200 sshkey_free(pubkey);
201 return SSH_ERR_ALLOC_FAIL;
202 }
203 k_prv->key = key;
204 TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
205
206 /* add the public key, too */
207 k->key = pubkey;
208 TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
209 r = 0;
210 } else {
211 if ((k = malloc(sizeof(*k))) == NULL)
212 return SSH_ERR_ALLOC_FAIL;
213 k->key = key;
214 TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
215 r = 0;
216 }
217
218 return r;
219}
220
221int
222ssh_set_verify_host_key_callback(struct ssh *ssh,
223 int (*cb)(struct sshkey *, struct ssh *))
224{
225 if (cb == NULL || ssh->kex == NULL)
226 return SSH_ERR_INVALID_ARGUMENT;
227
228 ssh->kex->verify_host_key = cb;
229
230 return 0;
231}
232
233int
234ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
235{
236 return sshbuf_put(ssh_packet_get_input(ssh), data, len);
237}
238
239int
240ssh_packet_next(struct ssh *ssh, u_char *typep)
241{
242 int r;
243 u_int32_t seqnr;
244 u_char type;
245
246 /*
247 * Try to read a packet. Return SSH_MSG_NONE if no packet or not
248 * enough data.
249 */
250 *typep = SSH_MSG_NONE;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000251 if (sshbuf_len(ssh->kex->client_version) == 0 ||
252 sshbuf_len(ssh->kex->server_version) == 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000253 return _ssh_exchange_banner(ssh);
254 /*
255 * If we enough data and a dispatch function then
256 * call the function and get the next packet.
257 * Otherwise return the packet type to the caller so it
258 * can decide how to go on.
259 *
260 * We will only call the dispatch function for:
261 * 20-29 Algorithm negotiation
262 * 30-49 Key exchange method specific (numbers can be reused for
263 * different authentication methods)
264 */
265 for (;;) {
266 if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
267 return r;
268 if (type > 0 && type < DISPATCH_MAX &&
269 type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
270 ssh->dispatch[type] != NULL) {
271 if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
272 return r;
273 } else {
274 *typep = type;
275 return 0;
276 }
277 }
278}
279
280const u_char *
281ssh_packet_payload(struct ssh *ssh, size_t *lenp)
282{
283 return sshpkt_ptr(ssh, lenp);
284}
285
286int
287ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
288{
289 int r;
290
291 if ((r = sshpkt_start(ssh, type)) != 0 ||
292 (r = sshpkt_put(ssh, data, len)) != 0 ||
293 (r = sshpkt_send(ssh)) != 0)
294 return r;
295 return 0;
296}
297
298const u_char *
299ssh_output_ptr(struct ssh *ssh, size_t *len)
300{
301 struct sshbuf *output = ssh_packet_get_output(ssh);
302
303 *len = sshbuf_len(output);
304 return sshbuf_ptr(output);
305}
306
307int
308ssh_output_consume(struct ssh *ssh, size_t len)
309{
310 return sshbuf_consume(ssh_packet_get_output(ssh), len);
311}
312
313int
314ssh_output_space(struct ssh *ssh, size_t len)
315{
316 return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
317}
318
319int
320ssh_input_space(struct ssh *ssh, size_t len)
321{
322 return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
323}
324
325/* Read other side's version identification. */
326int
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000327_ssh_read_banner(struct ssh *ssh, struct sshbuf *banner)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000328{
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000329 struct sshbuf *input = ssh_packet_get_input(ssh);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000330 const char *mismatch = "Protocol mismatch.\r\n";
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000331 const u_char *s = sshbuf_ptr(input);
332 u_char c;
333 char *cp, *remote_version;
334 int r, remote_major, remote_minor, expect_nl;
335 size_t n, j;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000336
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000337 for (j = n = 0;;) {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000338 sshbuf_reset(banner);
339 expect_nl = 0;
340 for (;;) {
341 if (j >= sshbuf_len(input))
342 return 0; /* insufficient data in input buf */
343 c = s[j++];
344 if (c == '\r') {
345 expect_nl = 1;
346 continue;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000347 }
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000348 if (c == '\n')
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000349 break;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000350 if (expect_nl)
351 goto bad;
352 if ((r = sshbuf_put_u8(banner, c)) != 0)
353 return r;
354 if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN)
355 goto bad;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000356 }
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000357 if (sshbuf_len(banner) >= 4 &&
358 memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000359 break;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000360 if ((cp = sshbuf_dup_string(banner)) == NULL)
361 return SSH_ERR_ALLOC_FAIL;
362 debug("%s: %s", __func__, cp);
363 free(cp);
364 /* Accept lines before banner only on client */
365 if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) {
366 bad:
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000367 if ((r = sshbuf_put(ssh_packet_get_output(ssh),
368 mismatch, strlen(mismatch))) != 0)
369 return r;
370 return SSH_ERR_NO_PROTOCOL_VERSION;
371 }
372 }
373 if ((r = sshbuf_consume(input, j)) != 0)
374 return r;
375
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000376 if ((cp = sshbuf_dup_string(banner)) == NULL)
377 return SSH_ERR_ALLOC_FAIL;
378 /* XXX remote version must be the same size as banner for sscanf */
379 if ((remote_version = calloc(1, sshbuf_len(banner))) == NULL)
380 return SSH_ERR_ALLOC_FAIL;
381
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000382 /*
383 * Check that the versions match. In future this might accept
384 * several versions and set appropriate flags to handle them.
385 */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000386 if (sscanf(cp, "SSH-%d.%d-%[^\n]\n",
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000387 &remote_major, &remote_minor, remote_version) != 3)
388 return SSH_ERR_INVALID_FORMAT;
389 debug("Remote protocol version %d.%d, remote software version %.100s",
390 remote_major, remote_minor, remote_version);
391
392 ssh->compat = compat_datafellows(remote_version);
393 if (remote_major == 1 && remote_minor == 99) {
394 remote_major = 2;
395 remote_minor = 0;
396 }
397 if (remote_major != 2)
398 return SSH_ERR_PROTOCOL_MISMATCH;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000399 debug("Remote version string %.100s", cp);
400 free(cp);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000401 return 0;
402}
403
404/* Send our own protocol version identification. */
405int
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000406_ssh_send_banner(struct ssh *ssh, struct sshbuf *banner)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000407{
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000408 char *cp;
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000409 int r;
410
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000411 if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000412 return r;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000413 if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0)
414 return r;
415 /* Remove trailing \r\n */
416 if ((r = sshbuf_consume_end(banner, 2)) != 0)
417 return r;
418 if ((cp = sshbuf_dup_string(banner)) == NULL)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000419 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000420 debug("Local version string %.100s", cp);
421 free(cp);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000422 return 0;
423}
424
425int
426_ssh_exchange_banner(struct ssh *ssh)
427{
428 struct kex *kex = ssh->kex;
429 int r;
430
431 /*
432 * if _ssh_read_banner() cannot parse a full version string
433 * it will return NULL and we end up calling it again.
434 */
435
436 r = 0;
437 if (kex->server) {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000438 if (sshbuf_len(ssh->kex->server_version) == 0)
439 r = _ssh_send_banner(ssh, ssh->kex->server_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000440 if (r == 0 &&
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000441 sshbuf_len(ssh->kex->server_version) != 0 &&
442 sshbuf_len(ssh->kex->client_version) == 0)
443 r = _ssh_read_banner(ssh, ssh->kex->client_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000444 } else {
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000445 if (sshbuf_len(ssh->kex->server_version) == 0)
446 r = _ssh_read_banner(ssh, ssh->kex->server_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000447 if (r == 0 &&
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000448 sshbuf_len(ssh->kex->server_version) != 0 &&
449 sshbuf_len(ssh->kex->client_version) == 0)
450 r = _ssh_send_banner(ssh, ssh->kex->client_version);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000451 }
452 if (r != 0)
453 return r;
454 /* start initial kex as soon as we have exchanged the banners */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000455 if (sshbuf_len(ssh->kex->server_version) != 0 &&
456 sshbuf_len(ssh->kex->client_version) != 0) {
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000457 if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
458 (r = kex_send_kexinit(ssh)) != 0)
459 return r;
460 }
461 return 0;
462}
463
464struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000465_ssh_host_public_key(int type, int nid, struct ssh *ssh)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000466{
467 struct key_entry *k;
468
469 debug3("%s: need %d", __func__, type);
470 TAILQ_FOREACH(k, &ssh->public_keys, next) {
471 debug3("%s: check %s", __func__, sshkey_type(k->key));
djm@openbsd.org5104db72015-01-26 06:10:03 +0000472 if (k->key->type == type &&
473 (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000474 return (k->key);
475 }
476 return (NULL);
477}
478
479struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000480_ssh_host_private_key(int type, int nid, struct ssh *ssh)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000481{
482 struct key_entry *k;
483
484 debug3("%s: need %d", __func__, type);
485 TAILQ_FOREACH(k, &ssh->private_keys, next) {
486 debug3("%s: check %s", __func__, sshkey_type(k->key));
djm@openbsd.org5104db72015-01-26 06:10:03 +0000487 if (k->key->type == type &&
488 (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000489 return (k->key);
490 }
491 return (NULL);
492}
493
494int
495_ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
496{
497 struct key_entry *k;
498
499 debug3("%s: need %s", __func__, sshkey_type(hostkey));
500 TAILQ_FOREACH(k, &ssh->public_keys, next) {
501 debug3("%s: check %s", __func__, sshkey_type(k->key));
502 if (sshkey_equal_public(hostkey, k->key))
503 return (0); /* ok */
504 }
505 return (-1); /* failed */
506}
507
508/* offer hostkey algorithms in kexinit depending on registered keys */
509int
510_ssh_order_hostkeyalgs(struct ssh *ssh)
511{
512 struct key_entry *k;
513 char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
514 char **proposal;
515 size_t maxlen;
516 int ktype, r;
517
518 /* XXX we de-serialize ssh->kex->my, modify it, and change it */
519 if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
520 return r;
521 orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
522 if ((oavail = avail = strdup(orig)) == NULL) {
523 r = SSH_ERR_ALLOC_FAIL;
524 goto out;
525 }
526 maxlen = strlen(avail) + 1;
527 if ((replace = calloc(1, maxlen)) == NULL) {
528 r = SSH_ERR_ALLOC_FAIL;
529 goto out;
530 }
531 *replace = '\0';
532 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
533 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
534 continue;
535 TAILQ_FOREACH(k, &ssh->public_keys, next) {
536 if (k->key->type == ktype ||
537 (sshkey_is_cert(k->key) && k->key->type ==
538 sshkey_type_plain(ktype))) {
539 if (*replace != '\0')
540 strlcat(replace, ",", maxlen);
541 strlcat(replace, alg, maxlen);
542 break;
543 }
544 }
545 }
546 if (*replace != '\0') {
547 debug2("%s: orig/%d %s", __func__, ssh->kex->server, orig);
548 debug2("%s: replace/%d %s", __func__, ssh->kex->server, replace);
549 free(orig);
550 proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
551 replace = NULL; /* owned by proposal */
552 r = kex_prop2buf(ssh->kex->my, proposal);
553 }
554 out:
555 free(oavail);
556 free(replace);
557 kex_prop_free(proposal);
558 return r;
559}
560
561int
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000562_ssh_host_key_sign(struct ssh *ssh, struct sshkey *privkey,
563 struct sshkey *pubkey, u_char **signature, size_t *slen,
564 const u_char *data, size_t dlen, const char *alg)
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000565{
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000566 return sshkey_sign(privkey, signature, slen, data, dlen,
567 alg, ssh->compat);
markus@openbsd.orgf582f0e2015-01-19 20:30:23 +0000568}