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