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