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