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