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