summaryrefslogtreecommitdiff
path: root/debian/patches/gssapi.patch
diff options
context:
space:
mode:
Diffstat (limited to 'debian/patches/gssapi.patch')
-rw-r--r--debian/patches/gssapi.patch4088
1 files changed, 4088 insertions, 0 deletions
diff --git a/debian/patches/gssapi.patch b/debian/patches/gssapi.patch
new file mode 100644
index 000000000..45d131d27
--- /dev/null
+++ b/debian/patches/gssapi.patch
@@ -0,0 +1,4088 @@
1From 7ce79be85036c4b36937f1b1ba85f6094068412c Mon Sep 17 00:00:00 2001
2From: Simon Wilkinson <simon@sxw.org.uk>
3Date: Sun, 9 Feb 2014 16:09:48 +0000
4Subject: GSSAPI key exchange support
5
6This patch has been rejected upstream: "None of the OpenSSH developers are
7in favour of adding this, and this situation has not changed for several
8years. This is not a slight on Simon's patch, which is of fine quality, but
9just that a) we don't trust GSSAPI implementations that much and b) we don't
10like adding new KEX since they are pre-auth attack surface. This one is
11particularly scary, since it requires hooks out to typically root-owned
12system resources."
13
14However, quite a lot of people rely on this in Debian, and it's better to
15have it merged into the main openssh package rather than having separate
16-krb5 packages (as we used to have). It seems to have a generally good
17security history.
18
19Origin: other, https://github.com/openssh-gsskex/openssh-gsskex/commits/debian/master
20Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1242
21Last-Updated: 2019-06-05
22
23Patch-Name: gssapi.patch
24---
25 Makefile.in | 3 +-
26 auth-krb5.c | 17 +-
27 auth.c | 96 +-------
28 auth2-gss.c | 56 ++++-
29 auth2.c | 2 +
30 canohost.c | 93 ++++++++
31 canohost.h | 3 +
32 clientloop.c | 15 +-
33 configure.ac | 24 ++
34 gss-genr.c | 300 +++++++++++++++++++++++-
35 gss-serv-krb5.c | 85 ++++++-
36 gss-serv.c | 186 +++++++++++++--
37 hmac.c | 1 +
38 kex.c | 66 +++++-
39 kex.h | 29 +++
40 kexdh.c | 10 +
41 kexgen.c | 2 +-
42 kexgssc.c | 606 ++++++++++++++++++++++++++++++++++++++++++++++++
43 kexgsss.c | 474 +++++++++++++++++++++++++++++++++++++
44 mac.c | 1 +
45 monitor.c | 139 ++++++++++-
46 monitor.h | 2 +
47 monitor_wrap.c | 57 ++++-
48 monitor_wrap.h | 4 +-
49 readconf.c | 70 ++++++
50 readconf.h | 6 +
51 servconf.c | 47 ++++
52 servconf.h | 3 +
53 session.c | 10 +-
54 ssh-gss.h | 50 +++-
55 ssh.1 | 8 +
56 ssh.c | 4 +-
57 ssh_config | 2 +
58 ssh_config.5 | 57 +++++
59 sshconnect2.c | 140 ++++++++++-
60 sshd.c | 120 +++++++++-
61 sshd_config | 2 +
62 sshd_config.5 | 30 +++
63 sshkey.c | 3 +-
64 sshkey.h | 1 +
65 40 files changed, 2664 insertions(+), 160 deletions(-)
66 create mode 100644 kexgssc.c
67 create mode 100644 kexgsss.c
68
69diff --git a/Makefile.in b/Makefile.in
70index 6f001bb36..c31821acc 100644
71--- a/Makefile.in
72+++ b/Makefile.in
73@@ -100,6 +100,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
74 kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
75 kexgexc.o kexgexs.o \
76 sntrup4591761.o kexsntrup4591761x25519.o kexgen.o \
77+ kexgssc.o \
78 platform-pledge.o platform-tracing.o platform-misc.o
79
80
81@@ -114,7 +115,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o \
82 auth-bsdauth.o auth2-hostbased.o auth2-kbdint.o \
83 auth2-none.o auth2-passwd.o auth2-pubkey.o \
84 monitor.o monitor_wrap.o auth-krb5.o \
85- auth2-gss.o gss-serv.o gss-serv-krb5.o \
86+ auth2-gss.o gss-serv.o gss-serv-krb5.o kexgsss.o \
87 loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
88 sftp-server.o sftp-common.o \
89 sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
90diff --git a/auth-krb5.c b/auth-krb5.c
91index 3096f1c8e..204752e1b 100644
92--- a/auth-krb5.c
93+++ b/auth-krb5.c
94@@ -182,8 +182,13 @@ auth_krb5_password(Authctxt *authctxt, const char *password)
95
96 len = strlen(authctxt->krb5_ticket_file) + 6;
97 authctxt->krb5_ccname = xmalloc(len);
98+#ifdef USE_CCAPI
99+ snprintf(authctxt->krb5_ccname, len, "API:%s",
100+ authctxt->krb5_ticket_file);
101+#else
102 snprintf(authctxt->krb5_ccname, len, "FILE:%s",
103 authctxt->krb5_ticket_file);
104+#endif
105
106 #ifdef USE_PAM
107 if (options.use_pam)
108@@ -240,15 +245,22 @@ krb5_cleanup_proc(Authctxt *authctxt)
109 #ifndef HEIMDAL
110 krb5_error_code
111 ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
112- int tmpfd, ret, oerrno;
113+ int ret, oerrno;
114 char ccname[40];
115 mode_t old_umask;
116+#ifdef USE_CCAPI
117+ char cctemplate[] = "API:krb5cc_%d";
118+#else
119+ char cctemplate[] = "FILE:/tmp/krb5cc_%d_XXXXXXXXXX";
120+ int tmpfd;
121+#endif
122
123 ret = snprintf(ccname, sizeof(ccname),
124- "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
125+ cctemplate, geteuid());
126 if (ret < 0 || (size_t)ret >= sizeof(ccname))
127 return ENOMEM;
128
129+#ifndef USE_CCAPI
130 old_umask = umask(0177);
131 tmpfd = mkstemp(ccname + strlen("FILE:"));
132 oerrno = errno;
133@@ -265,6 +277,7 @@ ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
134 return oerrno;
135 }
136 close(tmpfd);
137+#endif
138
139 return (krb5_cc_resolve(ctx, ccname, ccache));
140 }
141diff --git a/auth.c b/auth.c
142index 8696f258e..f7a23afba 100644
143--- a/auth.c
144+++ b/auth.c
145@@ -399,7 +399,8 @@ auth_root_allowed(struct ssh *ssh, const char *method)
146 case PERMIT_NO_PASSWD:
147 if (strcmp(method, "publickey") == 0 ||
148 strcmp(method, "hostbased") == 0 ||
149- strcmp(method, "gssapi-with-mic") == 0)
150+ strcmp(method, "gssapi-with-mic") == 0 ||
151+ strcmp(method, "gssapi-keyex") == 0)
152 return 1;
153 break;
154 case PERMIT_FORCED_ONLY:
155@@ -723,99 +724,6 @@ fakepw(void)
156 return (&fake);
157 }
158
159-/*
160- * Returns the remote DNS hostname as a string. The returned string must not
161- * be freed. NB. this will usually trigger a DNS query the first time it is
162- * called.
163- * This function does additional checks on the hostname to mitigate some
164- * attacks on legacy rhosts-style authentication.
165- * XXX is RhostsRSAAuthentication vulnerable to these?
166- * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
167- */
168-
169-static char *
170-remote_hostname(struct ssh *ssh)
171-{
172- struct sockaddr_storage from;
173- socklen_t fromlen;
174- struct addrinfo hints, *ai, *aitop;
175- char name[NI_MAXHOST], ntop2[NI_MAXHOST];
176- const char *ntop = ssh_remote_ipaddr(ssh);
177-
178- /* Get IP address of client. */
179- fromlen = sizeof(from);
180- memset(&from, 0, sizeof(from));
181- if (getpeername(ssh_packet_get_connection_in(ssh),
182- (struct sockaddr *)&from, &fromlen) < 0) {
183- debug("getpeername failed: %.100s", strerror(errno));
184- return strdup(ntop);
185- }
186-
187- ipv64_normalise_mapped(&from, &fromlen);
188- if (from.ss_family == AF_INET6)
189- fromlen = sizeof(struct sockaddr_in6);
190-
191- debug3("Trying to reverse map address %.100s.", ntop);
192- /* Map the IP address to a host name. */
193- if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
194- NULL, 0, NI_NAMEREQD) != 0) {
195- /* Host name not found. Use ip address. */
196- return strdup(ntop);
197- }
198-
199- /*
200- * if reverse lookup result looks like a numeric hostname,
201- * someone is trying to trick us by PTR record like following:
202- * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
203- */
204- memset(&hints, 0, sizeof(hints));
205- hints.ai_socktype = SOCK_DGRAM; /*dummy*/
206- hints.ai_flags = AI_NUMERICHOST;
207- if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
208- logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
209- name, ntop);
210- freeaddrinfo(ai);
211- return strdup(ntop);
212- }
213-
214- /* Names are stored in lowercase. */
215- lowercase(name);
216-
217- /*
218- * Map it back to an IP address and check that the given
219- * address actually is an address of this host. This is
220- * necessary because anyone with access to a name server can
221- * define arbitrary names for an IP address. Mapping from
222- * name to IP address can be trusted better (but can still be
223- * fooled if the intruder has access to the name server of
224- * the domain).
225- */
226- memset(&hints, 0, sizeof(hints));
227- hints.ai_family = from.ss_family;
228- hints.ai_socktype = SOCK_STREAM;
229- if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
230- logit("reverse mapping checking getaddrinfo for %.700s "
231- "[%s] failed.", name, ntop);
232- return strdup(ntop);
233- }
234- /* Look for the address from the list of addresses. */
235- for (ai = aitop; ai; ai = ai->ai_next) {
236- if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
237- sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
238- (strcmp(ntop, ntop2) == 0))
239- break;
240- }
241- freeaddrinfo(aitop);
242- /* If we reached the end of the list, the address was not there. */
243- if (ai == NULL) {
244- /* Address not found for the host name. */
245- logit("Address %.100s maps to %.600s, but this does not "
246- "map back to the address.", ntop, name);
247- return strdup(ntop);
248- }
249- return strdup(name);
250-}
251-
252 /*
253 * Return the canonical name of the host in the other side of the current
254 * connection. The host name is cached, so it is efficient to call this
255diff --git a/auth2-gss.c b/auth2-gss.c
256index 9351e0428..d6446c0cf 100644
257--- a/auth2-gss.c
258+++ b/auth2-gss.c
259@@ -1,7 +1,7 @@
260 /* $OpenBSD: auth2-gss.c,v 1.29 2018/07/31 03:10:27 djm Exp $ */
261
262 /*
263- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
264+ * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
265 *
266 * Redistribution and use in source and binary forms, with or without
267 * modification, are permitted provided that the following conditions
268@@ -54,6 +54,48 @@ static int input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh);
269 static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
270 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
271
272+/*
273+ * The 'gssapi_keyex' userauth mechanism.
274+ */
275+static int
276+userauth_gsskeyex(struct ssh *ssh)
277+{
278+ Authctxt *authctxt = ssh->authctxt;
279+ int r, authenticated = 0;
280+ struct sshbuf *b = NULL;
281+ gss_buffer_desc mic, gssbuf;
282+ u_char *p;
283+ size_t len;
284+
285+ if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
286+ (r = sshpkt_get_end(ssh)) != 0)
287+ fatal("%s: %s", __func__, ssh_err(r));
288+
289+ if ((b = sshbuf_new()) == NULL)
290+ fatal("%s: sshbuf_new failed", __func__);
291+
292+ mic.value = p;
293+ mic.length = len;
294+
295+ ssh_gssapi_buildmic(b, authctxt->user, authctxt->service,
296+ "gssapi-keyex");
297+
298+ if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
299+ fatal("%s: sshbuf_mutable_ptr failed", __func__);
300+ gssbuf.length = sshbuf_len(b);
301+
302+ /* gss_kex_context is NULL with privsep, so we can't check it here */
303+ if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
304+ &gssbuf, &mic))))
305+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
306+ authctxt->pw, 1));
307+
308+ sshbuf_free(b);
309+ free(mic.value);
310+
311+ return (authenticated);
312+}
313+
314 /*
315 * We only support those mechanisms that we know about (ie ones that we know
316 * how to check local user kuserok and the like)
317@@ -260,7 +302,8 @@ input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh)
318 if ((r = sshpkt_get_end(ssh)) != 0)
319 fatal("%s: %s", __func__, ssh_err(r));
320
321- authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
322+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
323+ authctxt->pw, 1));
324
325 if ((!use_privsep || mm_is_monitor()) &&
326 (displayname = ssh_gssapi_displayname()) != NULL)
327@@ -306,7 +349,8 @@ input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
328 gssbuf.length = sshbuf_len(b);
329
330 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
331- authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
332+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
333+ authctxt->pw, 0));
334 else
335 logit("GSSAPI MIC check failed");
336
337@@ -326,6 +370,12 @@ input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
338 return 0;
339 }
340
341+Authmethod method_gsskeyex = {
342+ "gssapi-keyex",
343+ userauth_gsskeyex,
344+ &options.gss_authentication
345+};
346+
347 Authmethod method_gssapi = {
348 "gssapi-with-mic",
349 userauth_gssapi,
350diff --git a/auth2.c b/auth2.c
351index 16ae1a363..7417eafa4 100644
352--- a/auth2.c
353+++ b/auth2.c
354@@ -75,6 +75,7 @@ extern Authmethod method_passwd;
355 extern Authmethod method_kbdint;
356 extern Authmethod method_hostbased;
357 #ifdef GSSAPI
358+extern Authmethod method_gsskeyex;
359 extern Authmethod method_gssapi;
360 #endif
361
362@@ -82,6 +83,7 @@ Authmethod *authmethods[] = {
363 &method_none,
364 &method_pubkey,
365 #ifdef GSSAPI
366+ &method_gsskeyex,
367 &method_gssapi,
368 #endif
369 &method_passwd,
370diff --git a/canohost.c b/canohost.c
371index f71a08568..404731d24 100644
372--- a/canohost.c
373+++ b/canohost.c
374@@ -35,6 +35,99 @@
375 #include "canohost.h"
376 #include "misc.h"
377
378+/*
379+ * Returns the remote DNS hostname as a string. The returned string must not
380+ * be freed. NB. this will usually trigger a DNS query the first time it is
381+ * called.
382+ * This function does additional checks on the hostname to mitigate some
383+ * attacks on legacy rhosts-style authentication.
384+ * XXX is RhostsRSAAuthentication vulnerable to these?
385+ * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
386+ */
387+
388+char *
389+remote_hostname(struct ssh *ssh)
390+{
391+ struct sockaddr_storage from;
392+ socklen_t fromlen;
393+ struct addrinfo hints, *ai, *aitop;
394+ char name[NI_MAXHOST], ntop2[NI_MAXHOST];
395+ const char *ntop = ssh_remote_ipaddr(ssh);
396+
397+ /* Get IP address of client. */
398+ fromlen = sizeof(from);
399+ memset(&from, 0, sizeof(from));
400+ if (getpeername(ssh_packet_get_connection_in(ssh),
401+ (struct sockaddr *)&from, &fromlen) < 0) {
402+ debug("getpeername failed: %.100s", strerror(errno));
403+ return strdup(ntop);
404+ }
405+
406+ ipv64_normalise_mapped(&from, &fromlen);
407+ if (from.ss_family == AF_INET6)
408+ fromlen = sizeof(struct sockaddr_in6);
409+
410+ debug3("Trying to reverse map address %.100s.", ntop);
411+ /* Map the IP address to a host name. */
412+ if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
413+ NULL, 0, NI_NAMEREQD) != 0) {
414+ /* Host name not found. Use ip address. */
415+ return strdup(ntop);
416+ }
417+
418+ /*
419+ * if reverse lookup result looks like a numeric hostname,
420+ * someone is trying to trick us by PTR record like following:
421+ * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
422+ */
423+ memset(&hints, 0, sizeof(hints));
424+ hints.ai_socktype = SOCK_DGRAM; /*dummy*/
425+ hints.ai_flags = AI_NUMERICHOST;
426+ if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
427+ logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
428+ name, ntop);
429+ freeaddrinfo(ai);
430+ return strdup(ntop);
431+ }
432+
433+ /* Names are stored in lowercase. */
434+ lowercase(name);
435+
436+ /*
437+ * Map it back to an IP address and check that the given
438+ * address actually is an address of this host. This is
439+ * necessary because anyone with access to a name server can
440+ * define arbitrary names for an IP address. Mapping from
441+ * name to IP address can be trusted better (but can still be
442+ * fooled if the intruder has access to the name server of
443+ * the domain).
444+ */
445+ memset(&hints, 0, sizeof(hints));
446+ hints.ai_family = from.ss_family;
447+ hints.ai_socktype = SOCK_STREAM;
448+ if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
449+ logit("reverse mapping checking getaddrinfo for %.700s "
450+ "[%s] failed.", name, ntop);
451+ return strdup(ntop);
452+ }
453+ /* Look for the address from the list of addresses. */
454+ for (ai = aitop; ai; ai = ai->ai_next) {
455+ if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
456+ sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
457+ (strcmp(ntop, ntop2) == 0))
458+ break;
459+ }
460+ freeaddrinfo(aitop);
461+ /* If we reached the end of the list, the address was not there. */
462+ if (ai == NULL) {
463+ /* Address not found for the host name. */
464+ logit("Address %.100s maps to %.600s, but this does not "
465+ "map back to the address.", ntop, name);
466+ return strdup(ntop);
467+ }
468+ return strdup(name);
469+}
470+
471 void
472 ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
473 {
474diff --git a/canohost.h b/canohost.h
475index 26d62855a..0cadc9f18 100644
476--- a/canohost.h
477+++ b/canohost.h
478@@ -15,6 +15,9 @@
479 #ifndef _CANOHOST_H
480 #define _CANOHOST_H
481
482+struct ssh;
483+
484+char *remote_hostname(struct ssh *);
485 char *get_peer_ipaddr(int);
486 int get_peer_port(int);
487 char *get_local_ipaddr(int);
488diff --git a/clientloop.c b/clientloop.c
489index 086c0dfe8..9b90c64f3 100644
490--- a/clientloop.c
491+++ b/clientloop.c
492@@ -112,6 +112,10 @@
493 #include "ssherr.h"
494 #include "hostfile.h"
495
496+#ifdef GSSAPI
497+#include "ssh-gss.h"
498+#endif
499+
500 /* import options */
501 extern Options options;
502
503@@ -1374,9 +1378,18 @@ client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
504 break;
505
506 /* Do channel operations unless rekeying in progress. */
507- if (!ssh_packet_is_rekeying(ssh))
508+ if (!ssh_packet_is_rekeying(ssh)) {
509 channel_after_select(ssh, readset, writeset);
510
511+#ifdef GSSAPI
512+ if (options.gss_renewal_rekey &&
513+ ssh_gssapi_credentials_updated(NULL)) {
514+ debug("credentials updated - forcing rekey");
515+ need_rekeying = 1;
516+ }
517+#endif
518+ }
519+
520 /* Buffer input from the connection. */
521 client_process_net_input(ssh, readset);
522
523diff --git a/configure.ac b/configure.ac
524index 30be6c182..2869f7042 100644
525--- a/configure.ac
526+++ b/configure.ac
527@@ -665,6 +665,30 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
528 [Use tunnel device compatibility to OpenBSD])
529 AC_DEFINE([SSH_TUN_PREPEND_AF], [1],
530 [Prepend the address family to IP tunnel traffic])
531+ AC_MSG_CHECKING([if we have the Security Authorization Session API])
532+ AC_TRY_COMPILE([#include <Security/AuthSession.h>],
533+ [SessionCreate(0, 0);],
534+ [ac_cv_use_security_session_api="yes"
535+ AC_DEFINE([USE_SECURITY_SESSION_API], [1],
536+ [platform has the Security Authorization Session API])
537+ LIBS="$LIBS -framework Security"
538+ AC_MSG_RESULT([yes])],
539+ [ac_cv_use_security_session_api="no"
540+ AC_MSG_RESULT([no])])
541+ AC_MSG_CHECKING([if we have an in-memory credentials cache])
542+ AC_TRY_COMPILE(
543+ [#include <Kerberos/Kerberos.h>],
544+ [cc_context_t c;
545+ (void) cc_initialize (&c, 0, NULL, NULL);],
546+ [AC_DEFINE([USE_CCAPI], [1],
547+ [platform uses an in-memory credentials cache])
548+ LIBS="$LIBS -framework Security"
549+ AC_MSG_RESULT([yes])
550+ if test "x$ac_cv_use_security_session_api" = "xno"; then
551+ AC_MSG_ERROR([*** Need a security framework to use the credentials cache API ***])
552+ fi],
553+ [AC_MSG_RESULT([no])]
554+ )
555 m4_pattern_allow([AU_IPv])
556 AC_CHECK_DECL([AU_IPv4], [],
557 AC_DEFINE([AU_IPv4], [0], [System only supports IPv4 audit records])
558diff --git a/gss-genr.c b/gss-genr.c
559index d56257b4a..763a63ffa 100644
560--- a/gss-genr.c
561+++ b/gss-genr.c
562@@ -1,7 +1,7 @@
563 /* $OpenBSD: gss-genr.c,v 1.26 2018/07/10 09:13:30 djm Exp $ */
564
565 /*
566- * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
567+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
568 *
569 * Redistribution and use in source and binary forms, with or without
570 * modification, are permitted provided that the following conditions
571@@ -41,12 +41,36 @@
572 #include "sshbuf.h"
573 #include "log.h"
574 #include "ssh2.h"
575+#include "cipher.h"
576+#include "sshkey.h"
577+#include "kex.h"
578+#include "digest.h"
579+#include "packet.h"
580
581 #include "ssh-gss.h"
582
583 extern u_char *session_id2;
584 extern u_int session_id2_len;
585
586+typedef struct {
587+ char *encoded;
588+ gss_OID oid;
589+} ssh_gss_kex_mapping;
590+
591+/*
592+ * XXX - It would be nice to find a more elegant way of handling the
593+ * XXX passing of the key exchange context to the userauth routines
594+ */
595+
596+Gssctxt *gss_kex_context = NULL;
597+
598+static ssh_gss_kex_mapping *gss_enc2oid = NULL;
599+
600+int
601+ssh_gssapi_oid_table_ok(void) {
602+ return (gss_enc2oid != NULL);
603+}
604+
605 /* sshbuf_get for gss_buffer_desc */
606 int
607 ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
608@@ -62,6 +86,162 @@ ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
609 return 0;
610 }
611
612+/* sshpkt_get of gss_buffer_desc */
613+int
614+ssh_gssapi_sshpkt_get_buffer_desc(struct ssh *ssh, gss_buffer_desc *g)
615+{
616+ int r;
617+ u_char *p;
618+ size_t len;
619+
620+ if ((r = sshpkt_get_string(ssh, &p, &len)) != 0)
621+ return r;
622+ g->value = p;
623+ g->length = len;
624+ return 0;
625+}
626+
627+/*
628+ * Return a list of the gss-group1-sha1 mechanisms supported by this program
629+ *
630+ * We test mechanisms to ensure that we can use them, to avoid starting
631+ * a key exchange with a bad mechanism
632+ */
633+
634+char *
635+ssh_gssapi_client_mechanisms(const char *host, const char *client,
636+ const char *kex) {
637+ gss_OID_set gss_supported = NULL;
638+ OM_uint32 min_status;
639+
640+ if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
641+ return NULL;
642+
643+ return ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
644+ host, client, kex);
645+}
646+
647+char *
648+ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
649+ const char *host, const char *client, const char *kex) {
650+ struct sshbuf *buf = NULL;
651+ size_t i;
652+ int r = SSH_ERR_ALLOC_FAIL;
653+ int oidpos, enclen;
654+ char *mechs, *encoded;
655+ u_char digest[SSH_DIGEST_MAX_LENGTH];
656+ char deroid[2];
657+ struct ssh_digest_ctx *md = NULL;
658+ char *s, *cp, *p;
659+
660+ if (gss_enc2oid != NULL) {
661+ for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
662+ free(gss_enc2oid[i].encoded);
663+ free(gss_enc2oid);
664+ }
665+
666+ gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
667+ (gss_supported->count + 1));
668+
669+ if ((buf = sshbuf_new()) == NULL)
670+ fatal("%s: sshbuf_new failed", __func__);
671+
672+ oidpos = 0;
673+ s = cp = xstrdup(kex);
674+ for (i = 0; i < gss_supported->count; i++) {
675+ if (gss_supported->elements[i].length < 128 &&
676+ (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
677+
678+ deroid[0] = SSH_GSS_OIDTYPE;
679+ deroid[1] = gss_supported->elements[i].length;
680+
681+ if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
682+ (r = ssh_digest_update(md, deroid, 2)) != 0 ||
683+ (r = ssh_digest_update(md,
684+ gss_supported->elements[i].elements,
685+ gss_supported->elements[i].length)) != 0 ||
686+ (r = ssh_digest_final(md, digest, sizeof(digest))) != 0)
687+ fatal("%s: digest failed: %s", __func__,
688+ ssh_err(r));
689+ ssh_digest_free(md);
690+ md = NULL;
691+
692+ encoded = xmalloc(ssh_digest_bytes(SSH_DIGEST_MD5)
693+ * 2);
694+ enclen = __b64_ntop(digest,
695+ ssh_digest_bytes(SSH_DIGEST_MD5), encoded,
696+ ssh_digest_bytes(SSH_DIGEST_MD5) * 2);
697+
698+ cp = strncpy(s, kex, strlen(kex));
699+ for ((p = strsep(&cp, ",")); p && *p != '\0';
700+ (p = strsep(&cp, ","))) {
701+ if (sshbuf_len(buf) != 0 &&
702+ (r = sshbuf_put_u8(buf, ',')) != 0)
703+ fatal("%s: sshbuf_put_u8 error: %s",
704+ __func__, ssh_err(r));
705+ if ((r = sshbuf_put(buf, p, strlen(p))) != 0 ||
706+ (r = sshbuf_put(buf, encoded, enclen)) != 0)
707+ fatal("%s: sshbuf_put error: %s",
708+ __func__, ssh_err(r));
709+ }
710+
711+ gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
712+ gss_enc2oid[oidpos].encoded = encoded;
713+ oidpos++;
714+ }
715+ }
716+ free(s);
717+ gss_enc2oid[oidpos].oid = NULL;
718+ gss_enc2oid[oidpos].encoded = NULL;
719+
720+ if ((mechs = sshbuf_dup_string(buf)) == NULL)
721+ fatal("%s: sshbuf_dup_string failed", __func__);
722+
723+ sshbuf_free(buf);
724+
725+ if (strlen(mechs) == 0) {
726+ free(mechs);
727+ mechs = NULL;
728+ }
729+
730+ return (mechs);
731+}
732+
733+gss_OID
734+ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
735+ int i = 0;
736+
737+#define SKIP_KEX_NAME(type) \
738+ case type: \
739+ if (strlen(name) < sizeof(type##_ID)) \
740+ return GSS_C_NO_OID; \
741+ name += sizeof(type##_ID) - 1; \
742+ break;
743+
744+ switch (kex_type) {
745+ SKIP_KEX_NAME(KEX_GSS_GRP1_SHA1)
746+ SKIP_KEX_NAME(KEX_GSS_GRP14_SHA1)
747+ SKIP_KEX_NAME(KEX_GSS_GRP14_SHA256)
748+ SKIP_KEX_NAME(KEX_GSS_GRP16_SHA512)
749+ SKIP_KEX_NAME(KEX_GSS_GEX_SHA1)
750+ SKIP_KEX_NAME(KEX_GSS_NISTP256_SHA256)
751+ SKIP_KEX_NAME(KEX_GSS_C25519_SHA256)
752+ default:
753+ return GSS_C_NO_OID;
754+ }
755+
756+#undef SKIP_KEX_NAME
757+
758+ while (gss_enc2oid[i].encoded != NULL &&
759+ strcmp(name, gss_enc2oid[i].encoded) != 0)
760+ i++;
761+
762+ if (gss_enc2oid[i].oid != NULL && ctx != NULL)
763+ ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
764+
765+ return gss_enc2oid[i].oid;
766+}
767+
768 /* Check that the OID in a data stream matches that in the context */
769 int
770 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
771@@ -218,7 +398,7 @@ ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
772 }
773
774 ctx->major = gss_init_sec_context(&ctx->minor,
775- GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
776+ ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
777 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
778 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
779
780@@ -247,9 +427,43 @@ ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
781 return (ctx->major);
782 }
783
784+OM_uint32
785+ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
786+{
787+ gss_buffer_desc gssbuf;
788+ gss_name_t gssname;
789+ OM_uint32 status;
790+ gss_OID_set oidset;
791+
792+ gssbuf.value = (void *) name;
793+ gssbuf.length = strlen(gssbuf.value);
794+
795+ gss_create_empty_oid_set(&status, &oidset);
796+ gss_add_oid_set_member(&status, ctx->oid, &oidset);
797+
798+ ctx->major = gss_import_name(&ctx->minor, &gssbuf,
799+ GSS_C_NT_USER_NAME, &gssname);
800+
801+ if (!ctx->major)
802+ ctx->major = gss_acquire_cred(&ctx->minor,
803+ gssname, 0, oidset, GSS_C_INITIATE,
804+ &ctx->client_creds, NULL, NULL);
805+
806+ gss_release_name(&status, &gssname);
807+ gss_release_oid_set(&status, &oidset);
808+
809+ if (ctx->major)
810+ ssh_gssapi_error(ctx);
811+
812+ return(ctx->major);
813+}
814+
815 OM_uint32
816 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
817 {
818+ if (ctx == NULL)
819+ return -1;
820+
821 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
822 GSS_C_QOP_DEFAULT, buffer, hash)))
823 ssh_gssapi_error(ctx);
824@@ -257,6 +471,19 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
825 return (ctx->major);
826 }
827
828+/* Priviledged when used by server */
829+OM_uint32
830+ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
831+{
832+ if (ctx == NULL)
833+ return -1;
834+
835+ ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
836+ gssbuf, gssmic, NULL);
837+
838+ return (ctx->major);
839+}
840+
841 void
842 ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
843 const char *context)
844@@ -273,11 +500,16 @@ ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
845 }
846
847 int
848-ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
849+ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host,
850+ const char *client)
851 {
852 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
853 OM_uint32 major, minor;
854 gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
855+ Gssctxt *intctx = NULL;
856+
857+ if (ctx == NULL)
858+ ctx = &intctx;
859
860 /* RFC 4462 says we MUST NOT do SPNEGO */
861 if (oid->length == spnego_oid.length &&
862@@ -287,6 +519,10 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
863 ssh_gssapi_build_ctx(ctx);
864 ssh_gssapi_set_oid(*ctx, oid);
865 major = ssh_gssapi_import_name(*ctx, host);
866+
867+ if (!GSS_ERROR(major) && client)
868+ major = ssh_gssapi_client_identity(*ctx, client);
869+
870 if (!GSS_ERROR(major)) {
871 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
872 NULL);
873@@ -296,10 +532,66 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
874 GSS_C_NO_BUFFER);
875 }
876
877- if (GSS_ERROR(major))
878+ if (GSS_ERROR(major) || intctx != NULL)
879 ssh_gssapi_delete_ctx(ctx);
880
881 return (!GSS_ERROR(major));
882 }
883
884+int
885+ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
886+ static gss_name_t saved_name = GSS_C_NO_NAME;
887+ static OM_uint32 saved_lifetime = 0;
888+ static gss_OID saved_mech = GSS_C_NO_OID;
889+ static gss_name_t name;
890+ static OM_uint32 last_call = 0;
891+ OM_uint32 lifetime, now, major, minor;
892+ int equal;
893+
894+ now = time(NULL);
895+
896+ if (ctxt) {
897+ debug("Rekey has happened - updating saved versions");
898+
899+ if (saved_name != GSS_C_NO_NAME)
900+ gss_release_name(&minor, &saved_name);
901+
902+ major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
903+ &saved_name, &saved_lifetime, NULL, NULL);
904+
905+ if (!GSS_ERROR(major)) {
906+ saved_mech = ctxt->oid;
907+ saved_lifetime+= now;
908+ } else {
909+ /* Handle the error */
910+ }
911+ return 0;
912+ }
913+
914+ if (now - last_call < 10)
915+ return 0;
916+
917+ last_call = now;
918+
919+ if (saved_mech == GSS_C_NO_OID)
920+ return 0;
921+
922+ major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
923+ &name, &lifetime, NULL, NULL);
924+ if (major == GSS_S_CREDENTIALS_EXPIRED)
925+ return 0;
926+ else if (GSS_ERROR(major))
927+ return 0;
928+
929+ major = gss_compare_name(&minor, saved_name, name, &equal);
930+ gss_release_name(&minor, &name);
931+ if (GSS_ERROR(major))
932+ return 0;
933+
934+ if (equal && (saved_lifetime < lifetime + now - 10))
935+ return 1;
936+
937+ return 0;
938+}
939+
940 #endif /* GSSAPI */
941diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c
942index a151bc1e4..ef9beb67c 100644
943--- a/gss-serv-krb5.c
944+++ b/gss-serv-krb5.c
945@@ -1,7 +1,7 @@
946 /* $OpenBSD: gss-serv-krb5.c,v 1.9 2018/07/09 21:37:55 markus Exp $ */
947
948 /*
949- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
950+ * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
951 *
952 * Redistribution and use in source and binary forms, with or without
953 * modification, are permitted provided that the following conditions
954@@ -120,8 +120,8 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
955 krb5_error_code problem;
956 krb5_principal princ;
957 OM_uint32 maj_status, min_status;
958- int len;
959 const char *errmsg;
960+ const char *new_ccname;
961
962 if (client->creds == NULL) {
963 debug("No credentials stored");
964@@ -180,11 +180,16 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
965 return;
966 }
967
968- client->store.filename = xstrdup(krb5_cc_get_name(krb_context, ccache));
969+ new_ccname = krb5_cc_get_name(krb_context, ccache);
970+
971 client->store.envvar = "KRB5CCNAME";
972- len = strlen(client->store.filename) + 6;
973- client->store.envval = xmalloc(len);
974- snprintf(client->store.envval, len, "FILE:%s", client->store.filename);
975+#ifdef USE_CCAPI
976+ xasprintf(&client->store.envval, "API:%s", new_ccname);
977+ client->store.filename = NULL;
978+#else
979+ xasprintf(&client->store.envval, "FILE:%s", new_ccname);
980+ client->store.filename = xstrdup(new_ccname);
981+#endif
982
983 #ifdef USE_PAM
984 if (options.use_pam)
985@@ -196,6 +201,71 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
986 return;
987 }
988
989+int
990+ssh_gssapi_krb5_updatecreds(ssh_gssapi_ccache *store,
991+ ssh_gssapi_client *client)
992+{
993+ krb5_ccache ccache = NULL;
994+ krb5_principal principal = NULL;
995+ char *name = NULL;
996+ krb5_error_code problem;
997+ OM_uint32 maj_status, min_status;
998+
999+ if ((problem = krb5_cc_resolve(krb_context, store->envval, &ccache))) {
1000+ logit("krb5_cc_resolve(): %.100s",
1001+ krb5_get_err_text(krb_context, problem));
1002+ return 0;
1003+ }
1004+
1005+ /* Find out who the principal in this cache is */
1006+ if ((problem = krb5_cc_get_principal(krb_context, ccache,
1007+ &principal))) {
1008+ logit("krb5_cc_get_principal(): %.100s",
1009+ krb5_get_err_text(krb_context, problem));
1010+ krb5_cc_close(krb_context, ccache);
1011+ return 0;
1012+ }
1013+
1014+ if ((problem = krb5_unparse_name(krb_context, principal, &name))) {
1015+ logit("krb5_unparse_name(): %.100s",
1016+ krb5_get_err_text(krb_context, problem));
1017+ krb5_free_principal(krb_context, principal);
1018+ krb5_cc_close(krb_context, ccache);
1019+ return 0;
1020+ }
1021+
1022+
1023+ if (strcmp(name,client->exportedname.value)!=0) {
1024+ debug("Name in local credentials cache differs. Not storing");
1025+ krb5_free_principal(krb_context, principal);
1026+ krb5_cc_close(krb_context, ccache);
1027+ krb5_free_unparsed_name(krb_context, name);
1028+ return 0;
1029+ }
1030+ krb5_free_unparsed_name(krb_context, name);
1031+
1032+ /* Name matches, so lets get on with it! */
1033+
1034+ if ((problem = krb5_cc_initialize(krb_context, ccache, principal))) {
1035+ logit("krb5_cc_initialize(): %.100s",
1036+ krb5_get_err_text(krb_context, problem));
1037+ krb5_free_principal(krb_context, principal);
1038+ krb5_cc_close(krb_context, ccache);
1039+ return 0;
1040+ }
1041+
1042+ krb5_free_principal(krb_context, principal);
1043+
1044+ if ((maj_status = gss_krb5_copy_ccache(&min_status, client->creds,
1045+ ccache))) {
1046+ logit("gss_krb5_copy_ccache() failed. Sorry!");
1047+ krb5_cc_close(krb_context, ccache);
1048+ return 0;
1049+ }
1050+
1051+ return 1;
1052+}
1053+
1054 ssh_gssapi_mech gssapi_kerberos_mech = {
1055 "toWM5Slw5Ew8Mqkay+al2g==",
1056 "Kerberos",
1057@@ -203,7 +273,8 @@ ssh_gssapi_mech gssapi_kerberos_mech = {
1058 NULL,
1059 &ssh_gssapi_krb5_userok,
1060 NULL,
1061- &ssh_gssapi_krb5_storecreds
1062+ &ssh_gssapi_krb5_storecreds,
1063+ &ssh_gssapi_krb5_updatecreds
1064 };
1065
1066 #endif /* KRB5 */
1067diff --git a/gss-serv.c b/gss-serv.c
1068index ab3a15f0f..1d47870e7 100644
1069--- a/gss-serv.c
1070+++ b/gss-serv.c
1071@@ -1,7 +1,7 @@
1072 /* $OpenBSD: gss-serv.c,v 1.31 2018/07/09 21:37:55 markus Exp $ */
1073
1074 /*
1075- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
1076+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1077 *
1078 * Redistribution and use in source and binary forms, with or without
1079 * modification, are permitted provided that the following conditions
1080@@ -44,17 +44,19 @@
1081 #include "session.h"
1082 #include "misc.h"
1083 #include "servconf.h"
1084+#include "uidswap.h"
1085
1086 #include "ssh-gss.h"
1087+#include "monitor_wrap.h"
1088
1089 extern ServerOptions options;
1090
1091 static ssh_gssapi_client gssapi_client =
1092- { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
1093- GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
1094+ { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER, GSS_C_NO_CREDENTIAL,
1095+ GSS_C_NO_NAME, NULL, {NULL, NULL, NULL, NULL, NULL}, 0, 0};
1096
1097 ssh_gssapi_mech gssapi_null_mech =
1098- { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
1099+ { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
1100
1101 #ifdef KRB5
1102 extern ssh_gssapi_mech gssapi_kerberos_mech;
1103@@ -140,6 +142,29 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
1104 return (ssh_gssapi_acquire_cred(*ctx));
1105 }
1106
1107+/* Unprivileged */
1108+char *
1109+ssh_gssapi_server_mechanisms(void) {
1110+ if (supported_oids == NULL)
1111+ ssh_gssapi_prepare_supported_oids();
1112+ return (ssh_gssapi_kex_mechs(supported_oids,
1113+ &ssh_gssapi_server_check_mech, NULL, NULL,
1114+ options.gss_kex_algorithms));
1115+}
1116+
1117+/* Unprivileged */
1118+int
1119+ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
1120+ const char *dummy) {
1121+ Gssctxt *ctx = NULL;
1122+ int res;
1123+
1124+ res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
1125+ ssh_gssapi_delete_ctx(&ctx);
1126+
1127+ return (res);
1128+}
1129+
1130 /* Unprivileged */
1131 void
1132 ssh_gssapi_supported_oids(gss_OID_set *oidset)
1133@@ -150,7 +175,9 @@ ssh_gssapi_supported_oids(gss_OID_set *oidset)
1134 gss_OID_set supported;
1135
1136 gss_create_empty_oid_set(&min_status, oidset);
1137- gss_indicate_mechs(&min_status, &supported);
1138+
1139+ if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
1140+ return;
1141
1142 while (supported_mechs[i]->name != NULL) {
1143 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
1144@@ -276,8 +303,48 @@ OM_uint32
1145 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1146 {
1147 int i = 0;
1148+ int equal = 0;
1149+ gss_name_t new_name = GSS_C_NO_NAME;
1150+ gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
1151+
1152+ if (options.gss_store_rekey && client->used && ctx->client_creds) {
1153+ if (client->mech->oid.length != ctx->oid->length ||
1154+ (memcmp(client->mech->oid.elements,
1155+ ctx->oid->elements, ctx->oid->length) !=0)) {
1156+ debug("Rekeyed credentials have different mechanism");
1157+ return GSS_S_COMPLETE;
1158+ }
1159+
1160+ if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1161+ ctx->client_creds, ctx->oid, &new_name,
1162+ NULL, NULL, NULL))) {
1163+ ssh_gssapi_error(ctx);
1164+ return (ctx->major);
1165+ }
1166+
1167+ ctx->major = gss_compare_name(&ctx->minor, client->name,
1168+ new_name, &equal);
1169+
1170+ if (GSS_ERROR(ctx->major)) {
1171+ ssh_gssapi_error(ctx);
1172+ return (ctx->major);
1173+ }
1174+
1175+ if (!equal) {
1176+ debug("Rekeyed credentials have different name");
1177+ return GSS_S_COMPLETE;
1178+ }
1179
1180- gss_buffer_desc ename;
1181+ debug("Marking rekeyed credentials for export");
1182+
1183+ gss_release_name(&ctx->minor, &client->name);
1184+ gss_release_cred(&ctx->minor, &client->creds);
1185+ client->name = new_name;
1186+ client->creds = ctx->client_creds;
1187+ ctx->client_creds = GSS_C_NO_CREDENTIAL;
1188+ client->updated = 1;
1189+ return GSS_S_COMPLETE;
1190+ }
1191
1192 client->mech = NULL;
1193
1194@@ -292,6 +359,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1195 if (client->mech == NULL)
1196 return GSS_S_FAILURE;
1197
1198+ if (ctx->client_creds &&
1199+ (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1200+ ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
1201+ ssh_gssapi_error(ctx);
1202+ return (ctx->major);
1203+ }
1204+
1205 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
1206 &client->displayname, NULL))) {
1207 ssh_gssapi_error(ctx);
1208@@ -309,6 +383,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1209 return (ctx->major);
1210 }
1211
1212+ gss_release_buffer(&ctx->minor, &ename);
1213+
1214 /* We can't copy this structure, so we just move the pointer to it */
1215 client->creds = ctx->client_creds;
1216 ctx->client_creds = GSS_C_NO_CREDENTIAL;
1217@@ -356,19 +432,23 @@ ssh_gssapi_do_child(char ***envp, u_int *envsizep)
1218
1219 /* Privileged */
1220 int
1221-ssh_gssapi_userok(char *user)
1222+ssh_gssapi_userok(char *user, struct passwd *pw, int kex)
1223 {
1224 OM_uint32 lmin;
1225
1226+ (void) kex; /* used in privilege separation */
1227+
1228 if (gssapi_client.exportedname.length == 0 ||
1229 gssapi_client.exportedname.value == NULL) {
1230 debug("No suitable client data");
1231 return 0;
1232 }
1233 if (gssapi_client.mech && gssapi_client.mech->userok)
1234- if ((*gssapi_client.mech->userok)(&gssapi_client, user))
1235+ if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
1236+ gssapi_client.used = 1;
1237+ gssapi_client.store.owner = pw;
1238 return 1;
1239- else {
1240+ } else {
1241 /* Destroy delegated credentials if userok fails */
1242 gss_release_buffer(&lmin, &gssapi_client.displayname);
1243 gss_release_buffer(&lmin, &gssapi_client.exportedname);
1244@@ -382,14 +462,90 @@ ssh_gssapi_userok(char *user)
1245 return (0);
1246 }
1247
1248-/* Privileged */
1249-OM_uint32
1250-ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1251+/* These bits are only used for rekeying. The unpriviledged child is running
1252+ * as the user, the monitor is root.
1253+ *
1254+ * In the child, we want to :
1255+ * *) Ask the monitor to store our credentials into the store we specify
1256+ * *) If it succeeds, maybe do a PAM update
1257+ */
1258+
1259+/* Stuff for PAM */
1260+
1261+#ifdef USE_PAM
1262+static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg,
1263+ struct pam_response **resp, void *data)
1264 {
1265- ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
1266- gssbuf, gssmic, NULL);
1267+ return (PAM_CONV_ERR);
1268+}
1269+#endif
1270
1271- return (ctx->major);
1272+void
1273+ssh_gssapi_rekey_creds(void) {
1274+ int ok;
1275+#ifdef USE_PAM
1276+ int ret;
1277+ pam_handle_t *pamh = NULL;
1278+ struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
1279+ char *envstr;
1280+#endif
1281+
1282+ if (gssapi_client.store.filename == NULL &&
1283+ gssapi_client.store.envval == NULL &&
1284+ gssapi_client.store.envvar == NULL)
1285+ return;
1286+
1287+ ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
1288+
1289+ if (!ok)
1290+ return;
1291+
1292+ debug("Rekeyed credentials stored successfully");
1293+
1294+ /* Actually managing to play with the ssh pam stack from here will
1295+ * be next to impossible. In any case, we may want different options
1296+ * for rekeying. So, use our own :)
1297+ */
1298+#ifdef USE_PAM
1299+ if (!use_privsep) {
1300+ debug("Not even going to try and do PAM with privsep disabled");
1301+ return;
1302+ }
1303+
1304+ ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
1305+ &pamconv, &pamh);
1306+ if (ret)
1307+ return;
1308+
1309+ xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar,
1310+ gssapi_client.store.envval);
1311+
1312+ ret = pam_putenv(pamh, envstr);
1313+ if (!ret)
1314+ pam_setcred(pamh, PAM_REINITIALIZE_CRED);
1315+ pam_end(pamh, PAM_SUCCESS);
1316+#endif
1317+}
1318+
1319+int
1320+ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
1321+ int ok = 0;
1322+
1323+ /* Check we've got credentials to store */
1324+ if (!gssapi_client.updated)
1325+ return 0;
1326+
1327+ gssapi_client.updated = 0;
1328+
1329+ temporarily_use_uid(gssapi_client.store.owner);
1330+ if (gssapi_client.mech && gssapi_client.mech->updatecreds)
1331+ ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
1332+ else
1333+ debug("No update function for this mechanism");
1334+
1335+ restore_uid();
1336+
1337+ return ok;
1338 }
1339
1340 /* Privileged */
1341diff --git a/hmac.c b/hmac.c
1342index 1c879640c..a29f32c5c 100644
1343--- a/hmac.c
1344+++ b/hmac.c
1345@@ -19,6 +19,7 @@
1346
1347 #include <sys/types.h>
1348 #include <string.h>
1349+#include <stdlib.h>
1350
1351 #include "sshbuf.h"
1352 #include "digest.h"
1353diff --git a/kex.c b/kex.c
1354index 34808b5c3..a2a4794e8 100644
1355--- a/kex.c
1356+++ b/kex.c
1357@@ -55,11 +55,16 @@
1358 #include "misc.h"
1359 #include "dispatch.h"
1360 #include "monitor.h"
1361+#include "xmalloc.h"
1362
1363 #include "ssherr.h"
1364 #include "sshbuf.h"
1365 #include "digest.h"
1366
1367+#ifdef GSSAPI
1368+#include "ssh-gss.h"
1369+#endif
1370+
1371 /* prototype */
1372 static int kex_choose_conf(struct ssh *);
1373 static int kex_input_newkeys(int, u_int32_t, struct ssh *);
1374@@ -113,15 +118,28 @@ static const struct kexalg kexalgs[] = {
1375 #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
1376 { NULL, -1, -1, -1},
1377 };
1378+static const struct kexalg gss_kexalgs[] = {
1379+#ifdef GSSAPI
1380+ { KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
1381+ { KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
1382+ { KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
1383+ { KEX_GSS_GRP14_SHA256_ID, KEX_GSS_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
1384+ { KEX_GSS_GRP16_SHA512_ID, KEX_GSS_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
1385+ { KEX_GSS_NISTP256_SHA256_ID, KEX_GSS_NISTP256_SHA256,
1386+ NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
1387+ { KEX_GSS_C25519_SHA256_ID, KEX_GSS_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1388+#endif
1389+ { NULL, -1, -1, -1 },
1390+};
1391
1392-char *
1393-kex_alg_list(char sep)
1394+static char *
1395+kex_alg_list_internal(char sep, const struct kexalg *algs)
1396 {
1397 char *ret = NULL, *tmp;
1398 size_t nlen, rlen = 0;
1399 const struct kexalg *k;
1400
1401- for (k = kexalgs; k->name != NULL; k++) {
1402+ for (k = algs; k->name != NULL; k++) {
1403 if (ret != NULL)
1404 ret[rlen++] = sep;
1405 nlen = strlen(k->name);
1406@@ -136,6 +154,18 @@ kex_alg_list(char sep)
1407 return ret;
1408 }
1409
1410+char *
1411+kex_alg_list(char sep)
1412+{
1413+ return kex_alg_list_internal(sep, kexalgs);
1414+}
1415+
1416+char *
1417+kex_gss_alg_list(char sep)
1418+{
1419+ return kex_alg_list_internal(sep, gss_kexalgs);
1420+}
1421+
1422 static const struct kexalg *
1423 kex_alg_by_name(const char *name)
1424 {
1425@@ -145,6 +175,10 @@ kex_alg_by_name(const char *name)
1426 if (strcmp(k->name, name) == 0)
1427 return k;
1428 }
1429+ for (k = gss_kexalgs; k->name != NULL; k++) {
1430+ if (strncmp(k->name, name, strlen(k->name)) == 0)
1431+ return k;
1432+ }
1433 return NULL;
1434 }
1435
1436@@ -301,6 +335,29 @@ kex_assemble_names(char **listp, const char *def, const char *all)
1437 return r;
1438 }
1439
1440+/* Validate GSS KEX method name list */
1441+int
1442+kex_gss_names_valid(const char *names)
1443+{
1444+ char *s, *cp, *p;
1445+
1446+ if (names == NULL || *names == '\0')
1447+ return 0;
1448+ s = cp = xstrdup(names);
1449+ for ((p = strsep(&cp, ",")); p && *p != '\0';
1450+ (p = strsep(&cp, ","))) {
1451+ if (strncmp(p, "gss-", 4) != 0
1452+ || kex_alg_by_name(p) == NULL) {
1453+ error("Unsupported KEX algorithm \"%.100s\"", p);
1454+ free(s);
1455+ return 0;
1456+ }
1457+ }
1458+ debug3("gss kex names ok: [%s]", names);
1459+ free(s);
1460+ return 1;
1461+}
1462+
1463 /* put algorithm proposal into buffer */
1464 int
1465 kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
1466@@ -657,6 +714,9 @@ kex_free(struct kex *kex)
1467 sshbuf_free(kex->server_version);
1468 sshbuf_free(kex->client_pub);
1469 free(kex->session_id);
1470+#ifdef GSSAPI
1471+ free(kex->gss_host);
1472+#endif /* GSSAPI */
1473 free(kex->failed_choice);
1474 free(kex->hostkey_alg);
1475 free(kex->name);
1476diff --git a/kex.h b/kex.h
1477index 6d446d1cc..2d5f1d4ed 100644
1478--- a/kex.h
1479+++ b/kex.h
1480@@ -103,6 +103,15 @@ enum kex_exchange {
1481 KEX_ECDH_SHA2,
1482 KEX_C25519_SHA256,
1483 KEX_KEM_SNTRUP4591761X25519_SHA512,
1484+#ifdef GSSAPI
1485+ KEX_GSS_GRP1_SHA1,
1486+ KEX_GSS_GRP14_SHA1,
1487+ KEX_GSS_GRP14_SHA256,
1488+ KEX_GSS_GRP16_SHA512,
1489+ KEX_GSS_GEX_SHA1,
1490+ KEX_GSS_NISTP256_SHA256,
1491+ KEX_GSS_C25519_SHA256,
1492+#endif
1493 KEX_MAX
1494 };
1495
1496@@ -154,6 +163,12 @@ struct kex {
1497 u_int flags;
1498 int hash_alg;
1499 int ec_nid;
1500+#ifdef GSSAPI
1501+ int gss_deleg_creds;
1502+ int gss_trust_dns;
1503+ char *gss_host;
1504+ char *gss_client;
1505+#endif
1506 char *failed_choice;
1507 int (*verify_host_key)(struct sshkey *, struct ssh *);
1508 struct sshkey *(*load_host_public_key)(int, int, struct ssh *);
1509@@ -175,8 +190,10 @@ struct kex {
1510
1511 int kex_names_valid(const char *);
1512 char *kex_alg_list(char);
1513+char *kex_gss_alg_list(char);
1514 char *kex_names_cat(const char *, const char *);
1515 int kex_assemble_names(char **, const char *, const char *);
1516+int kex_gss_names_valid(const char *);
1517
1518 int kex_exchange_identification(struct ssh *, int, const char *);
1519
1520@@ -203,6 +220,12 @@ int kexgex_client(struct ssh *);
1521 int kexgex_server(struct ssh *);
1522 int kex_gen_client(struct ssh *);
1523 int kex_gen_server(struct ssh *);
1524+#if defined(GSSAPI) && defined(WITH_OPENSSL)
1525+int kexgssgex_client(struct ssh *);
1526+int kexgssgex_server(struct ssh *);
1527+int kexgss_client(struct ssh *);
1528+int kexgss_server(struct ssh *);
1529+#endif
1530
1531 int kex_dh_keypair(struct kex *);
1532 int kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
1533@@ -235,6 +258,12 @@ int kexgex_hash(int, const struct sshbuf *, const struct sshbuf *,
1534 const BIGNUM *, const u_char *, size_t,
1535 u_char *, size_t *);
1536
1537+int kex_gen_hash(int hash_alg, const struct sshbuf *client_version,
1538+ const struct sshbuf *server_version, const struct sshbuf *client_kexinit,
1539+ const struct sshbuf *server_kexinit, const struct sshbuf *server_host_key_blob,
1540+ const struct sshbuf *client_pub, const struct sshbuf *server_pub,
1541+ const struct sshbuf *shared_secret, u_char *hash, size_t *hashlen);
1542+
1543 void kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
1544 __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
1545 __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
1546diff --git a/kexdh.c b/kexdh.c
1547index 67133e339..edaa46762 100644
1548--- a/kexdh.c
1549+++ b/kexdh.c
1550@@ -48,13 +48,23 @@ kex_dh_keygen(struct kex *kex)
1551 {
1552 switch (kex->kex_type) {
1553 case KEX_DH_GRP1_SHA1:
1554+#ifdef GSSAPI
1555+ case KEX_GSS_GRP1_SHA1:
1556+#endif
1557 kex->dh = dh_new_group1();
1558 break;
1559 case KEX_DH_GRP14_SHA1:
1560 case KEX_DH_GRP14_SHA256:
1561+#ifdef GSSAPI
1562+ case KEX_GSS_GRP14_SHA1:
1563+ case KEX_GSS_GRP14_SHA256:
1564+#endif
1565 kex->dh = dh_new_group14();
1566 break;
1567 case KEX_DH_GRP16_SHA512:
1568+#ifdef GSSAPI
1569+ case KEX_GSS_GRP16_SHA512:
1570+#endif
1571 kex->dh = dh_new_group16();
1572 break;
1573 case KEX_DH_GRP18_SHA512:
1574diff --git a/kexgen.c b/kexgen.c
1575index 2abbb9ef6..569dc83f3 100644
1576--- a/kexgen.c
1577+++ b/kexgen.c
1578@@ -43,7 +43,7 @@
1579 static int input_kex_gen_init(int, u_int32_t, struct ssh *);
1580 static int input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh);
1581
1582-static int
1583+int
1584 kex_gen_hash(
1585 int hash_alg,
1586 const struct sshbuf *client_version,
1587diff --git a/kexgssc.c b/kexgssc.c
1588new file mode 100644
1589index 000000000..f6e1405eb
1590--- /dev/null
1591+++ b/kexgssc.c
1592@@ -0,0 +1,606 @@
1593+/*
1594+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1595+ *
1596+ * Redistribution and use in source and binary forms, with or without
1597+ * modification, are permitted provided that the following conditions
1598+ * are met:
1599+ * 1. Redistributions of source code must retain the above copyright
1600+ * notice, this list of conditions and the following disclaimer.
1601+ * 2. Redistributions in binary form must reproduce the above copyright
1602+ * notice, this list of conditions and the following disclaimer in the
1603+ * documentation and/or other materials provided with the distribution.
1604+ *
1605+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
1606+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1607+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1608+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1609+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1610+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1611+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1612+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1613+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1614+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1615+ */
1616+
1617+#include "includes.h"
1618+
1619+#if defined(GSSAPI) && defined(WITH_OPENSSL)
1620+
1621+#include "includes.h"
1622+
1623+#include <openssl/crypto.h>
1624+#include <openssl/bn.h>
1625+
1626+#include <string.h>
1627+
1628+#include "xmalloc.h"
1629+#include "sshbuf.h"
1630+#include "ssh2.h"
1631+#include "sshkey.h"
1632+#include "cipher.h"
1633+#include "kex.h"
1634+#include "log.h"
1635+#include "packet.h"
1636+#include "dh.h"
1637+#include "digest.h"
1638+#include "ssherr.h"
1639+
1640+#include "ssh-gss.h"
1641+
1642+int
1643+kexgss_client(struct ssh *ssh)
1644+{
1645+ struct kex *kex = ssh->kex;
1646+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER,
1647+ recv_tok = GSS_C_EMPTY_BUFFER,
1648+ gssbuf, msg_tok = GSS_C_EMPTY_BUFFER, *token_ptr;
1649+ Gssctxt *ctxt;
1650+ OM_uint32 maj_status, min_status, ret_flags;
1651+ struct sshbuf *server_blob = NULL;
1652+ struct sshbuf *shared_secret = NULL;
1653+ struct sshbuf *server_host_key_blob = NULL;
1654+ struct sshbuf *empty = NULL;
1655+ u_char *msg;
1656+ int type = 0;
1657+ int first = 1;
1658+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1659+ size_t hashlen;
1660+ u_char c;
1661+ int r;
1662+
1663+ /* Initialise our GSSAPI world */
1664+ ssh_gssapi_build_ctx(&ctxt);
1665+ if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
1666+ == GSS_C_NO_OID)
1667+ fatal("Couldn't identify host exchange");
1668+
1669+ if (ssh_gssapi_import_name(ctxt, kex->gss_host))
1670+ fatal("Couldn't import hostname");
1671+
1672+ if (kex->gss_client &&
1673+ ssh_gssapi_client_identity(ctxt, kex->gss_client))
1674+ fatal("Couldn't acquire client credentials");
1675+
1676+ /* Step 1 */
1677+ switch (kex->kex_type) {
1678+ case KEX_GSS_GRP1_SHA1:
1679+ case KEX_GSS_GRP14_SHA1:
1680+ case KEX_GSS_GRP14_SHA256:
1681+ case KEX_GSS_GRP16_SHA512:
1682+ r = kex_dh_keypair(kex);
1683+ break;
1684+ case KEX_GSS_NISTP256_SHA256:
1685+ r = kex_ecdh_keypair(kex);
1686+ break;
1687+ case KEX_GSS_C25519_SHA256:
1688+ r = kex_c25519_keypair(kex);
1689+ break;
1690+ default:
1691+ fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
1692+ }
1693+ if (r != 0)
1694+ return r;
1695+
1696+ token_ptr = GSS_C_NO_BUFFER;
1697+
1698+ do {
1699+ debug("Calling gss_init_sec_context");
1700+
1701+ maj_status = ssh_gssapi_init_ctx(ctxt,
1702+ kex->gss_deleg_creds, token_ptr, &send_tok,
1703+ &ret_flags);
1704+
1705+ if (GSS_ERROR(maj_status)) {
1706+ /* XXX Useles code: Missing send? */
1707+ if (send_tok.length != 0) {
1708+ if ((r = sshpkt_start(ssh,
1709+ SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
1710+ (r = sshpkt_put_string(ssh, send_tok.value,
1711+ send_tok.length)) != 0)
1712+ fatal("sshpkt failed: %s", ssh_err(r));
1713+ }
1714+ fatal("gss_init_context failed");
1715+ }
1716+
1717+ /* If we've got an old receive buffer get rid of it */
1718+ if (token_ptr != GSS_C_NO_BUFFER)
1719+ gss_release_buffer(&min_status, &recv_tok);
1720+
1721+ if (maj_status == GSS_S_COMPLETE) {
1722+ /* If mutual state flag is not true, kex fails */
1723+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1724+ fatal("Mutual authentication failed");
1725+
1726+ /* If integ avail flag is not true kex fails */
1727+ if (!(ret_flags & GSS_C_INTEG_FLAG))
1728+ fatal("Integrity check failed");
1729+ }
1730+
1731+ /*
1732+ * If we have data to send, then the last message that we
1733+ * received cannot have been a 'complete'.
1734+ */
1735+ if (send_tok.length != 0) {
1736+ if (first) {
1737+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_INIT)) != 0 ||
1738+ (r = sshpkt_put_string(ssh, send_tok.value,
1739+ send_tok.length)) != 0 ||
1740+ (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0)
1741+ fatal("failed to construct packet: %s", ssh_err(r));
1742+ first = 0;
1743+ } else {
1744+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
1745+ (r = sshpkt_put_string(ssh, send_tok.value,
1746+ send_tok.length)) != 0)
1747+ fatal("failed to construct packet: %s", ssh_err(r));
1748+ }
1749+ if ((r = sshpkt_send(ssh)) != 0)
1750+ fatal("failed to send packet: %s", ssh_err(r));
1751+ gss_release_buffer(&min_status, &send_tok);
1752+
1753+ /* If we've sent them data, they should reply */
1754+ do {
1755+ type = ssh_packet_read(ssh);
1756+ if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
1757+ debug("Received KEXGSS_HOSTKEY");
1758+ if (server_host_key_blob)
1759+ fatal("Server host key received more than once");
1760+ if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
1761+ fatal("Failed to read server host key: %s", ssh_err(r));
1762+ }
1763+ } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
1764+
1765+ switch (type) {
1766+ case SSH2_MSG_KEXGSS_CONTINUE:
1767+ debug("Received GSSAPI_CONTINUE");
1768+ if (maj_status == GSS_S_COMPLETE)
1769+ fatal("GSSAPI Continue received from server when complete");
1770+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
1771+ &recv_tok)) != 0 ||
1772+ (r = sshpkt_get_end(ssh)) != 0)
1773+ fatal("Failed to read token: %s", ssh_err(r));
1774+ break;
1775+ case SSH2_MSG_KEXGSS_COMPLETE:
1776+ debug("Received GSSAPI_COMPLETE");
1777+ if (msg_tok.value != NULL)
1778+ fatal("Received GSSAPI_COMPLETE twice?");
1779+ if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
1780+ (r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
1781+ &msg_tok)) != 0)
1782+ fatal("Failed to read message: %s", ssh_err(r));
1783+
1784+ /* Is there a token included? */
1785+ if ((r = sshpkt_get_u8(ssh, &c)) != 0)
1786+ fatal("sshpkt failed: %s", ssh_err(r));
1787+ if (c) {
1788+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(
1789+ ssh, &recv_tok)) != 0)
1790+ fatal("Failed to read token: %s", ssh_err(r));
1791+ /* If we're already complete - protocol error */
1792+ if (maj_status == GSS_S_COMPLETE)
1793+ sshpkt_disconnect(ssh, "Protocol error: received token when complete");
1794+ } else {
1795+ /* No token included */
1796+ if (maj_status != GSS_S_COMPLETE)
1797+ sshpkt_disconnect(ssh, "Protocol error: did not receive final token");
1798+ }
1799+ if ((r = sshpkt_get_end(ssh)) != 0) {
1800+ fatal("Expecting end of packet.");
1801+ }
1802+ break;
1803+ case SSH2_MSG_KEXGSS_ERROR:
1804+ debug("Received Error");
1805+ if ((r = sshpkt_get_u32(ssh, &maj_status)) != 0 ||
1806+ (r = sshpkt_get_u32(ssh, &min_status)) != 0 ||
1807+ (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1808+ (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* lang tag */
1809+ (r = sshpkt_get_end(ssh)) != 0)
1810+ fatal("sshpkt_get failed: %s", ssh_err(r));
1811+ fatal("GSSAPI Error: \n%.400s", msg);
1812+ default:
1813+ sshpkt_disconnect(ssh, "Protocol error: didn't expect packet type %d",
1814+ type);
1815+ }
1816+ token_ptr = &recv_tok;
1817+ } else {
1818+ /* No data, and not complete */
1819+ if (maj_status != GSS_S_COMPLETE)
1820+ fatal("Not complete, and no token output");
1821+ }
1822+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
1823+
1824+ /*
1825+ * We _must_ have received a COMPLETE message in reply from the
1826+ * server, which will have set server_blob and msg_tok
1827+ */
1828+
1829+ if (type != SSH2_MSG_KEXGSS_COMPLETE)
1830+ fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
1831+
1832+ /* compute shared secret */
1833+ switch (kex->kex_type) {
1834+ case KEX_GSS_GRP1_SHA1:
1835+ case KEX_GSS_GRP14_SHA1:
1836+ case KEX_GSS_GRP14_SHA256:
1837+ case KEX_GSS_GRP16_SHA512:
1838+ r = kex_dh_dec(kex, server_blob, &shared_secret);
1839+ break;
1840+ case KEX_GSS_C25519_SHA256:
1841+ if (sshbuf_ptr(server_blob)[sshbuf_len(server_blob)] & 0x80)
1842+ fatal("The received key has MSB of last octet set!");
1843+ r = kex_c25519_dec(kex, server_blob, &shared_secret);
1844+ break;
1845+ case KEX_GSS_NISTP256_SHA256:
1846+ if (sshbuf_len(server_blob) != 65)
1847+ fatal("The received NIST-P256 key did not match"
1848+ "expected length (expected 65, got %zu)", sshbuf_len(server_blob));
1849+
1850+ if (sshbuf_ptr(server_blob)[0] != POINT_CONVERSION_UNCOMPRESSED)
1851+ fatal("The received NIST-P256 key does not have first octet 0x04");
1852+
1853+ r = kex_ecdh_dec(kex, server_blob, &shared_secret);
1854+ break;
1855+ default:
1856+ r = SSH_ERR_INVALID_ARGUMENT;
1857+ break;
1858+ }
1859+ if (r != 0)
1860+ goto out;
1861+
1862+ if ((empty = sshbuf_new()) == NULL) {
1863+ r = SSH_ERR_ALLOC_FAIL;
1864+ goto out;
1865+ }
1866+
1867+ hashlen = sizeof(hash);
1868+ if ((r = kex_gen_hash(
1869+ kex->hash_alg,
1870+ kex->client_version,
1871+ kex->server_version,
1872+ kex->my,
1873+ kex->peer,
1874+ (server_host_key_blob ? server_host_key_blob : empty),
1875+ kex->client_pub,
1876+ server_blob,
1877+ shared_secret,
1878+ hash, &hashlen)) != 0)
1879+ fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
1880+
1881+ gssbuf.value = hash;
1882+ gssbuf.length = hashlen;
1883+
1884+ /* Verify that the hash matches the MIC we just got. */
1885+ if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
1886+ sshpkt_disconnect(ssh, "Hash's MIC didn't verify");
1887+
1888+ gss_release_buffer(&min_status, &msg_tok);
1889+
1890+ if (kex->gss_deleg_creds)
1891+ ssh_gssapi_credentials_updated(ctxt);
1892+
1893+ if (gss_kex_context == NULL)
1894+ gss_kex_context = ctxt;
1895+ else
1896+ ssh_gssapi_delete_ctx(&ctxt);
1897+
1898+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
1899+ r = kex_send_newkeys(ssh);
1900+
1901+out:
1902+ explicit_bzero(hash, sizeof(hash));
1903+ explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
1904+ sshbuf_free(empty);
1905+ sshbuf_free(server_host_key_blob);
1906+ sshbuf_free(server_blob);
1907+ sshbuf_free(shared_secret);
1908+ sshbuf_free(kex->client_pub);
1909+ kex->client_pub = NULL;
1910+ return r;
1911+}
1912+
1913+int
1914+kexgssgex_client(struct ssh *ssh)
1915+{
1916+ struct kex *kex = ssh->kex;
1917+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER,
1918+ recv_tok = GSS_C_EMPTY_BUFFER, gssbuf,
1919+ msg_tok = GSS_C_EMPTY_BUFFER, *token_ptr;
1920+ Gssctxt *ctxt;
1921+ OM_uint32 maj_status, min_status, ret_flags;
1922+ struct sshbuf *shared_secret = NULL;
1923+ BIGNUM *p = NULL;
1924+ BIGNUM *g = NULL;
1925+ struct sshbuf *buf = NULL;
1926+ struct sshbuf *server_host_key_blob = NULL;
1927+ struct sshbuf *server_blob = NULL;
1928+ BIGNUM *dh_server_pub = NULL;
1929+ u_char *msg;
1930+ int type = 0;
1931+ int first = 1;
1932+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1933+ size_t hashlen;
1934+ const BIGNUM *pub_key, *dh_p, *dh_g;
1935+ int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
1936+ struct sshbuf *empty = NULL;
1937+ u_char c;
1938+ int r;
1939+
1940+ /* Initialise our GSSAPI world */
1941+ ssh_gssapi_build_ctx(&ctxt);
1942+ if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
1943+ == GSS_C_NO_OID)
1944+ fatal("Couldn't identify host exchange");
1945+
1946+ if (ssh_gssapi_import_name(ctxt, kex->gss_host))
1947+ fatal("Couldn't import hostname");
1948+
1949+ if (kex->gss_client &&
1950+ ssh_gssapi_client_identity(ctxt, kex->gss_client))
1951+ fatal("Couldn't acquire client credentials");
1952+
1953+ debug("Doing group exchange");
1954+ nbits = dh_estimate(kex->dh_need * 8);
1955+
1956+ kex->min = DH_GRP_MIN;
1957+ kex->max = DH_GRP_MAX;
1958+ kex->nbits = nbits;
1959+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_GROUPREQ)) != 0 ||
1960+ (r = sshpkt_put_u32(ssh, min)) != 0 ||
1961+ (r = sshpkt_put_u32(ssh, nbits)) != 0 ||
1962+ (r = sshpkt_put_u32(ssh, max)) != 0 ||
1963+ (r = sshpkt_send(ssh)) != 0)
1964+ fatal("Failed to construct a packet: %s", ssh_err(r));
1965+
1966+ if ((r = ssh_packet_read_expect(ssh, SSH2_MSG_KEXGSS_GROUP)) != 0)
1967+ fatal("Error: %s", ssh_err(r));
1968+
1969+ if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
1970+ (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
1971+ (r = sshpkt_get_end(ssh)) != 0)
1972+ fatal("shpkt_get_bignum2 failed: %s", ssh_err(r));
1973+
1974+ if (BN_num_bits(p) < min || BN_num_bits(p) > max)
1975+ fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
1976+ min, BN_num_bits(p), max);
1977+
1978+ if ((kex->dh = dh_new_group(g, p)) == NULL)
1979+ fatal("dn_new_group() failed");
1980+ p = g = NULL; /* belong to kex->dh now */
1981+
1982+ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
1983+ goto out;
1984+ DH_get0_key(kex->dh, &pub_key, NULL);
1985+
1986+ token_ptr = GSS_C_NO_BUFFER;
1987+
1988+ do {
1989+ /* Step 2 - call GSS_Init_sec_context() */
1990+ debug("Calling gss_init_sec_context");
1991+
1992+ maj_status = ssh_gssapi_init_ctx(ctxt,
1993+ kex->gss_deleg_creds, token_ptr, &send_tok,
1994+ &ret_flags);
1995+
1996+ if (GSS_ERROR(maj_status)) {
1997+ /* XXX Useles code: Missing send? */
1998+ if (send_tok.length != 0) {
1999+ if ((r = sshpkt_start(ssh,
2000+ SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2001+ (r = sshpkt_put_string(ssh, send_tok.value,
2002+ send_tok.length)) != 0)
2003+ fatal("sshpkt failed: %s", ssh_err(r));
2004+ }
2005+ fatal("gss_init_context failed");
2006+ }
2007+
2008+ /* If we've got an old receive buffer get rid of it */
2009+ if (token_ptr != GSS_C_NO_BUFFER)
2010+ gss_release_buffer(&min_status, &recv_tok);
2011+
2012+ if (maj_status == GSS_S_COMPLETE) {
2013+ /* If mutual state flag is not true, kex fails */
2014+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
2015+ fatal("Mutual authentication failed");
2016+
2017+ /* If integ avail flag is not true kex fails */
2018+ if (!(ret_flags & GSS_C_INTEG_FLAG))
2019+ fatal("Integrity check failed");
2020+ }
2021+
2022+ /*
2023+ * If we have data to send, then the last message that we
2024+ * received cannot have been a 'complete'.
2025+ */
2026+ if (send_tok.length != 0) {
2027+ if (first) {
2028+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_INIT)) != 0 ||
2029+ (r = sshpkt_put_string(ssh, send_tok.value,
2030+ send_tok.length)) != 0 ||
2031+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0)
2032+ fatal("sshpkt failed: %s", ssh_err(r));
2033+ first = 0;
2034+ } else {
2035+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2036+ (r = sshpkt_put_string(ssh,send_tok.value,
2037+ send_tok.length)) != 0)
2038+ fatal("sshpkt failed: %s", ssh_err(r));
2039+ }
2040+ if ((r = sshpkt_send(ssh)) != 0)
2041+ fatal("sshpkt_send failed: %s", ssh_err(r));
2042+ gss_release_buffer(&min_status, &send_tok);
2043+
2044+ /* If we've sent them data, they should reply */
2045+ do {
2046+ type = ssh_packet_read(ssh);
2047+ if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
2048+ debug("Received KEXGSS_HOSTKEY");
2049+ if (server_host_key_blob)
2050+ fatal("Server host key received more than once");
2051+ if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
2052+ fatal("sshpkt failed: %s", ssh_err(r));
2053+ }
2054+ } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
2055+
2056+ switch (type) {
2057+ case SSH2_MSG_KEXGSS_CONTINUE:
2058+ debug("Received GSSAPI_CONTINUE");
2059+ if (maj_status == GSS_S_COMPLETE)
2060+ fatal("GSSAPI Continue received from server when complete");
2061+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2062+ &recv_tok)) != 0 ||
2063+ (r = sshpkt_get_end(ssh)) != 0)
2064+ fatal("sshpkt failed: %s", ssh_err(r));
2065+ break;
2066+ case SSH2_MSG_KEXGSS_COMPLETE:
2067+ debug("Received GSSAPI_COMPLETE");
2068+ if (msg_tok.value != NULL)
2069+ fatal("Received GSSAPI_COMPLETE twice?");
2070+ if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
2071+ (r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2072+ &msg_tok)) != 0)
2073+ fatal("sshpkt failed: %s", ssh_err(r));
2074+
2075+ /* Is there a token included? */
2076+ if ((r = sshpkt_get_u8(ssh, &c)) != 0)
2077+ fatal("sshpkt failed: %s", ssh_err(r));
2078+ if (c) {
2079+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(
2080+ ssh, &recv_tok)) != 0 ||
2081+ (r = sshpkt_get_end(ssh)) != 0)
2082+ fatal("sshpkt failed: %s", ssh_err(r));
2083+ /* If we're already complete - protocol error */
2084+ if (maj_status == GSS_S_COMPLETE)
2085+ sshpkt_disconnect(ssh, "Protocol error: received token when complete");
2086+ } else {
2087+ /* No token included */
2088+ if (maj_status != GSS_S_COMPLETE)
2089+ sshpkt_disconnect(ssh, "Protocol error: did not receive final token");
2090+ }
2091+ break;
2092+ case SSH2_MSG_KEXGSS_ERROR:
2093+ debug("Received Error");
2094+ if ((r = sshpkt_get_u32(ssh, &maj_status)) != 0 ||
2095+ (r = sshpkt_get_u32(ssh, &min_status)) != 0 ||
2096+ (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
2097+ (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* lang tag */
2098+ (r = sshpkt_get_end(ssh)) != 0)
2099+ fatal("sshpkt failed: %s", ssh_err(r));
2100+ fatal("GSSAPI Error: \n%.400s", msg);
2101+ default:
2102+ sshpkt_disconnect(ssh, "Protocol error: didn't expect packet type %d",
2103+ type);
2104+ }
2105+ token_ptr = &recv_tok;
2106+ } else {
2107+ /* No data, and not complete */
2108+ if (maj_status != GSS_S_COMPLETE)
2109+ fatal("Not complete, and no token output");
2110+ }
2111+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
2112+
2113+ /*
2114+ * We _must_ have received a COMPLETE message in reply from the
2115+ * server, which will have set dh_server_pub and msg_tok
2116+ */
2117+
2118+ if (type != SSH2_MSG_KEXGSS_COMPLETE)
2119+ fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
2120+
2121+ /* 7. C verifies that the key Q_S is valid */
2122+ /* 8. C computes shared secret */
2123+ if ((buf = sshbuf_new()) == NULL ||
2124+ (r = sshbuf_put_stringb(buf, server_blob)) != 0 ||
2125+ (r = sshbuf_get_bignum2(buf, &dh_server_pub)) != 0)
2126+ goto out;
2127+ sshbuf_free(buf);
2128+ buf = NULL;
2129+
2130+ if ((shared_secret = sshbuf_new()) == NULL) {
2131+ r = SSH_ERR_ALLOC_FAIL;
2132+ goto out;
2133+ }
2134+
2135+ if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
2136+ goto out;
2137+ if ((empty = sshbuf_new()) == NULL) {
2138+ r = SSH_ERR_ALLOC_FAIL;
2139+ goto out;
2140+ }
2141+
2142+ DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
2143+ hashlen = sizeof(hash);
2144+ if ((r = kexgex_hash(
2145+ kex->hash_alg,
2146+ kex->client_version,
2147+ kex->server_version,
2148+ kex->my,
2149+ kex->peer,
2150+ (server_host_key_blob ? server_host_key_blob : empty),
2151+ kex->min, kex->nbits, kex->max,
2152+ dh_p, dh_g,
2153+ pub_key,
2154+ dh_server_pub,
2155+ sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
2156+ hash, &hashlen)) != 0)
2157+ fatal("Failed to calculate hash: %s", ssh_err(r));
2158+
2159+ gssbuf.value = hash;
2160+ gssbuf.length = hashlen;
2161+
2162+ /* Verify that the hash matches the MIC we just got. */
2163+ if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
2164+ sshpkt_disconnect(ssh, "Hash's MIC didn't verify");
2165+
2166+ gss_release_buffer(&min_status, &msg_tok);
2167+
2168+ /* save session id */
2169+ if (kex->session_id == NULL) {
2170+ kex->session_id_len = hashlen;
2171+ kex->session_id = xmalloc(kex->session_id_len);
2172+ memcpy(kex->session_id, hash, kex->session_id_len);
2173+ }
2174+
2175+ if (kex->gss_deleg_creds)
2176+ ssh_gssapi_credentials_updated(ctxt);
2177+
2178+ if (gss_kex_context == NULL)
2179+ gss_kex_context = ctxt;
2180+ else
2181+ ssh_gssapi_delete_ctx(&ctxt);
2182+
2183+ /* Finally derive the keys and send them */
2184+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
2185+ r = kex_send_newkeys(ssh);
2186+out:
2187+ sshbuf_free(buf);
2188+ sshbuf_free(server_blob);
2189+ sshbuf_free(empty);
2190+ explicit_bzero(hash, sizeof(hash));
2191+ DH_free(kex->dh);
2192+ kex->dh = NULL;
2193+ BN_clear_free(dh_server_pub);
2194+ sshbuf_free(shared_secret);
2195+ sshbuf_free(server_host_key_blob);
2196+ return r;
2197+}
2198+#endif /* defined(GSSAPI) && defined(WITH_OPENSSL) */
2199diff --git a/kexgsss.c b/kexgsss.c
2200new file mode 100644
2201index 000000000..60bc02deb
2202--- /dev/null
2203+++ b/kexgsss.c
2204@@ -0,0 +1,474 @@
2205+/*
2206+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
2207+ *
2208+ * Redistribution and use in source and binary forms, with or without
2209+ * modification, are permitted provided that the following conditions
2210+ * are met:
2211+ * 1. Redistributions of source code must retain the above copyright
2212+ * notice, this list of conditions and the following disclaimer.
2213+ * 2. Redistributions in binary form must reproduce the above copyright
2214+ * notice, this list of conditions and the following disclaimer in the
2215+ * documentation and/or other materials provided with the distribution.
2216+ *
2217+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
2218+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2219+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2220+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2221+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2222+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2223+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2224+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2225+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2226+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2227+ */
2228+
2229+#include "includes.h"
2230+
2231+#if defined(GSSAPI) && defined(WITH_OPENSSL)
2232+
2233+#include <string.h>
2234+
2235+#include <openssl/crypto.h>
2236+#include <openssl/bn.h>
2237+
2238+#include "xmalloc.h"
2239+#include "sshbuf.h"
2240+#include "ssh2.h"
2241+#include "sshkey.h"
2242+#include "cipher.h"
2243+#include "kex.h"
2244+#include "log.h"
2245+#include "packet.h"
2246+#include "dh.h"
2247+#include "ssh-gss.h"
2248+#include "monitor_wrap.h"
2249+#include "misc.h" /* servconf.h needs misc.h for struct ForwardOptions */
2250+#include "servconf.h"
2251+#include "ssh-gss.h"
2252+#include "digest.h"
2253+#include "ssherr.h"
2254+
2255+extern ServerOptions options;
2256+
2257+int
2258+kexgss_server(struct ssh *ssh)
2259+{
2260+ struct kex *kex = ssh->kex;
2261+ OM_uint32 maj_status, min_status;
2262+
2263+ /*
2264+ * Some GSSAPI implementations use the input value of ret_flags (an
2265+ * output variable) as a means of triggering mechanism specific
2266+ * features. Initializing it to zero avoids inadvertently
2267+ * activating this non-standard behaviour.
2268+ */
2269+
2270+ OM_uint32 ret_flags = 0;
2271+ gss_buffer_desc gssbuf, recv_tok, msg_tok;
2272+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
2273+ Gssctxt *ctxt = NULL;
2274+ struct sshbuf *shared_secret = NULL;
2275+ struct sshbuf *client_pubkey = NULL;
2276+ struct sshbuf *server_pubkey = NULL;
2277+ struct sshbuf *empty = sshbuf_new();
2278+ int type = 0;
2279+ gss_OID oid;
2280+ char *mechs;
2281+ u_char hash[SSH_DIGEST_MAX_LENGTH];
2282+ size_t hashlen;
2283+ int r;
2284+
2285+ /* Initialise GSSAPI */
2286+
2287+ /* If we're rekeying, privsep means that some of the private structures
2288+ * in the GSSAPI code are no longer available. This kludges them back
2289+ * into life
2290+ */
2291+ if (!ssh_gssapi_oid_table_ok()) {
2292+ mechs = ssh_gssapi_server_mechanisms();
2293+ free(mechs);
2294+ }
2295+
2296+ debug2("%s: Identifying %s", __func__, kex->name);
2297+ oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
2298+ if (oid == GSS_C_NO_OID)
2299+ fatal("Unknown gssapi mechanism");
2300+
2301+ debug2("%s: Acquiring credentials", __func__);
2302+
2303+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
2304+ fatal("Unable to acquire credentials for the server");
2305+
2306+ do {
2307+ debug("Wait SSH2_MSG_KEXGSS_INIT");
2308+ type = ssh_packet_read(ssh);
2309+ switch(type) {
2310+ case SSH2_MSG_KEXGSS_INIT:
2311+ if (client_pubkey != NULL)
2312+ fatal("Received KEXGSS_INIT after initialising");
2313+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2314+ &recv_tok)) != 0 ||
2315+ (r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 ||
2316+ (r = sshpkt_get_end(ssh)) != 0)
2317+ fatal("sshpkt failed: %s", ssh_err(r));
2318+
2319+ switch (kex->kex_type) {
2320+ case KEX_GSS_GRP1_SHA1:
2321+ case KEX_GSS_GRP14_SHA1:
2322+ case KEX_GSS_GRP14_SHA256:
2323+ case KEX_GSS_GRP16_SHA512:
2324+ r = kex_dh_enc(kex, client_pubkey, &server_pubkey,
2325+ &shared_secret);
2326+ break;
2327+ case KEX_GSS_NISTP256_SHA256:
2328+ r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey,
2329+ &shared_secret);
2330+ break;
2331+ case KEX_GSS_C25519_SHA256:
2332+ r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
2333+ &shared_secret);
2334+ break;
2335+ default:
2336+ fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
2337+ }
2338+ if (r != 0)
2339+ goto out;
2340+
2341+ /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
2342+ break;
2343+ case SSH2_MSG_KEXGSS_CONTINUE:
2344+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2345+ &recv_tok)) != 0 ||
2346+ (r = sshpkt_get_end(ssh)) != 0)
2347+ fatal("sshpkt failed: %s", ssh_err(r));
2348+ break;
2349+ default:
2350+ sshpkt_disconnect(ssh,
2351+ "Protocol error: didn't expect packet type %d",
2352+ type);
2353+ }
2354+
2355+ maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
2356+ &send_tok, &ret_flags));
2357+
2358+ gss_release_buffer(&min_status, &recv_tok);
2359+
2360+ if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
2361+ fatal("Zero length token output when incomplete");
2362+
2363+ if (client_pubkey == NULL)
2364+ fatal("No client public key");
2365+
2366+ if (maj_status & GSS_S_CONTINUE_NEEDED) {
2367+ debug("Sending GSSAPI_CONTINUE");
2368+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2369+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2370+ (r = sshpkt_send(ssh)) != 0)
2371+ fatal("sshpkt failed: %s", ssh_err(r));
2372+ gss_release_buffer(&min_status, &send_tok);
2373+ }
2374+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
2375+
2376+ if (GSS_ERROR(maj_status)) {
2377+ if (send_tok.length > 0) {
2378+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2379+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2380+ (r = sshpkt_send(ssh)) != 0)
2381+ fatal("sshpkt failed: %s", ssh_err(r));
2382+ }
2383+ fatal("accept_ctx died");
2384+ }
2385+
2386+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
2387+ fatal("Mutual Authentication flag wasn't set");
2388+
2389+ if (!(ret_flags & GSS_C_INTEG_FLAG))
2390+ fatal("Integrity flag wasn't set");
2391+
2392+ hashlen = sizeof(hash);
2393+ if ((r = kex_gen_hash(
2394+ kex->hash_alg,
2395+ kex->client_version,
2396+ kex->server_version,
2397+ kex->peer,
2398+ kex->my,
2399+ empty,
2400+ client_pubkey,
2401+ server_pubkey,
2402+ shared_secret,
2403+ hash, &hashlen)) != 0)
2404+ goto out;
2405+
2406+ gssbuf.value = hash;
2407+ gssbuf.length = hashlen;
2408+
2409+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt, &gssbuf, &msg_tok))))
2410+ fatal("Couldn't get MIC");
2411+
2412+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_COMPLETE)) != 0 ||
2413+ (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
2414+ (r = sshpkt_put_string(ssh, msg_tok.value, msg_tok.length)) != 0)
2415+ fatal("sshpkt failed: %s", ssh_err(r));
2416+
2417+ if (send_tok.length != 0) {
2418+ if ((r = sshpkt_put_u8(ssh, 1)) != 0 || /* true */
2419+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0)
2420+ fatal("sshpkt failed: %s", ssh_err(r));
2421+ } else {
2422+ if ((r = sshpkt_put_u8(ssh, 0)) != 0) /* false */
2423+ fatal("sshpkt failed: %s", ssh_err(r));
2424+ }
2425+ if ((r = sshpkt_send(ssh)) != 0)
2426+ fatal("sshpkt_send failed: %s", ssh_err(r));
2427+
2428+ gss_release_buffer(&min_status, &send_tok);
2429+ gss_release_buffer(&min_status, &msg_tok);
2430+
2431+ if (gss_kex_context == NULL)
2432+ gss_kex_context = ctxt;
2433+ else
2434+ ssh_gssapi_delete_ctx(&ctxt);
2435+
2436+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
2437+ r = kex_send_newkeys(ssh);
2438+
2439+ /* If this was a rekey, then save out any delegated credentials we
2440+ * just exchanged. */
2441+ if (options.gss_store_rekey)
2442+ ssh_gssapi_rekey_creds();
2443+out:
2444+ sshbuf_free(empty);
2445+ explicit_bzero(hash, sizeof(hash));
2446+ sshbuf_free(shared_secret);
2447+ sshbuf_free(client_pubkey);
2448+ sshbuf_free(server_pubkey);
2449+ return r;
2450+}
2451+
2452+int
2453+kexgssgex_server(struct ssh *ssh)
2454+{
2455+ struct kex *kex = ssh->kex;
2456+ OM_uint32 maj_status, min_status;
2457+
2458+ /*
2459+ * Some GSSAPI implementations use the input value of ret_flags (an
2460+ * output variable) as a means of triggering mechanism specific
2461+ * features. Initializing it to zero avoids inadvertently
2462+ * activating this non-standard behaviour.
2463+ */
2464+
2465+ OM_uint32 ret_flags = 0;
2466+ gss_buffer_desc gssbuf, recv_tok, msg_tok;
2467+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
2468+ Gssctxt *ctxt = NULL;
2469+ struct sshbuf *shared_secret = NULL;
2470+ int type = 0;
2471+ gss_OID oid;
2472+ char *mechs;
2473+ u_char hash[SSH_DIGEST_MAX_LENGTH];
2474+ size_t hashlen;
2475+ BIGNUM *dh_client_pub = NULL;
2476+ const BIGNUM *pub_key, *dh_p, *dh_g;
2477+ int min = -1, max = -1, nbits = -1;
2478+ int cmin = -1, cmax = -1; /* client proposal */
2479+ struct sshbuf *empty = sshbuf_new();
2480+ int r;
2481+
2482+ /* Initialise GSSAPI */
2483+
2484+ /* If we're rekeying, privsep means that some of the private structures
2485+ * in the GSSAPI code are no longer available. This kludges them back
2486+ * into life
2487+ */
2488+ if (!ssh_gssapi_oid_table_ok())
2489+ if ((mechs = ssh_gssapi_server_mechanisms()))
2490+ free(mechs);
2491+
2492+ debug2("%s: Identifying %s", __func__, kex->name);
2493+ oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
2494+ if (oid == GSS_C_NO_OID)
2495+ fatal("Unknown gssapi mechanism");
2496+
2497+ debug2("%s: Acquiring credentials", __func__);
2498+
2499+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
2500+ fatal("Unable to acquire credentials for the server");
2501+
2502+ /* 5. S generates an ephemeral key pair (do the allocations early) */
2503+ debug("Doing group exchange");
2504+ ssh_packet_read_expect(ssh, SSH2_MSG_KEXGSS_GROUPREQ);
2505+ /* store client proposal to provide valid signature */
2506+ if ((r = sshpkt_get_u32(ssh, &cmin)) != 0 ||
2507+ (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
2508+ (r = sshpkt_get_u32(ssh, &cmax)) != 0 ||
2509+ (r = sshpkt_get_end(ssh)) != 0)
2510+ fatal("sshpkt failed: %s", ssh_err(r));
2511+ kex->nbits = nbits;
2512+ kex->min = cmin;
2513+ kex->max = cmax;
2514+ min = MAX(DH_GRP_MIN, cmin);
2515+ max = MIN(DH_GRP_MAX, cmax);
2516+ nbits = MAXIMUM(DH_GRP_MIN, nbits);
2517+ nbits = MINIMUM(DH_GRP_MAX, nbits);
2518+ if (max < min || nbits < min || max < nbits)
2519+ fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
2520+ min, nbits, max);
2521+ kex->dh = PRIVSEP(choose_dh(min, nbits, max));
2522+ if (kex->dh == NULL) {
2523+ sshpkt_disconnect(ssh, "Protocol error: no matching group found");
2524+ fatal("Protocol error: no matching group found");
2525+ }
2526+
2527+ DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
2528+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_GROUP)) != 0 ||
2529+ (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
2530+ (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
2531+ (r = sshpkt_send(ssh)) != 0)
2532+ fatal("sshpkt failed: %s", ssh_err(r));
2533+
2534+ if ((r = ssh_packet_write_wait(ssh)) != 0)
2535+ fatal("ssh_packet_write_wait: %s", ssh_err(r));
2536+
2537+ /* Compute our exchange value in parallel with the client */
2538+ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
2539+ goto out;
2540+
2541+ do {
2542+ debug("Wait SSH2_MSG_GSSAPI_INIT");
2543+ type = ssh_packet_read(ssh);
2544+ switch(type) {
2545+ case SSH2_MSG_KEXGSS_INIT:
2546+ if (dh_client_pub != NULL)
2547+ fatal("Received KEXGSS_INIT after initialising");
2548+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2549+ &recv_tok)) != 0 ||
2550+ (r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
2551+ (r = sshpkt_get_end(ssh)) != 0)
2552+ fatal("sshpkt failed: %s", ssh_err(r));
2553+
2554+ /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
2555+ break;
2556+ case SSH2_MSG_KEXGSS_CONTINUE:
2557+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2558+ &recv_tok)) != 0 ||
2559+ (r = sshpkt_get_end(ssh)) != 0)
2560+ fatal("sshpkt failed: %s", ssh_err(r));
2561+ break;
2562+ default:
2563+ sshpkt_disconnect(ssh,
2564+ "Protocol error: didn't expect packet type %d",
2565+ type);
2566+ }
2567+
2568+ maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
2569+ &send_tok, &ret_flags));
2570+
2571+ gss_release_buffer(&min_status, &recv_tok);
2572+
2573+ if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
2574+ fatal("Zero length token output when incomplete");
2575+
2576+ if (dh_client_pub == NULL)
2577+ fatal("No client public key");
2578+
2579+ if (maj_status & GSS_S_CONTINUE_NEEDED) {
2580+ debug("Sending GSSAPI_CONTINUE");
2581+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2582+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2583+ (r = sshpkt_send(ssh)) != 0)
2584+ fatal("sshpkt failed: %s", ssh_err(r));
2585+ gss_release_buffer(&min_status, &send_tok);
2586+ }
2587+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
2588+
2589+ if (GSS_ERROR(maj_status)) {
2590+ if (send_tok.length > 0) {
2591+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2592+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2593+ (r = sshpkt_send(ssh)) != 0)
2594+ fatal("sshpkt failed: %s", ssh_err(r));
2595+ }
2596+ fatal("accept_ctx died");
2597+ }
2598+
2599+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
2600+ fatal("Mutual Authentication flag wasn't set");
2601+
2602+ if (!(ret_flags & GSS_C_INTEG_FLAG))
2603+ fatal("Integrity flag wasn't set");
2604+
2605+ /* calculate shared secret */
2606+ if ((shared_secret = sshbuf_new()) == NULL) {
2607+ r = SSH_ERR_ALLOC_FAIL;
2608+ goto out;
2609+ }
2610+ if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
2611+ goto out;
2612+
2613+ DH_get0_key(kex->dh, &pub_key, NULL);
2614+ DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
2615+ hashlen = sizeof(hash);
2616+ if ((r = kexgex_hash(
2617+ kex->hash_alg,
2618+ kex->client_version,
2619+ kex->server_version,
2620+ kex->peer,
2621+ kex->my,
2622+ empty,
2623+ cmin, nbits, cmax,
2624+ dh_p, dh_g,
2625+ dh_client_pub,
2626+ pub_key,
2627+ sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
2628+ hash, &hashlen)) != 0)
2629+ fatal("kexgex_hash failed: %s", ssh_err(r));
2630+
2631+ gssbuf.value = hash;
2632+ gssbuf.length = hashlen;
2633+
2634+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt, &gssbuf, &msg_tok))))
2635+ fatal("Couldn't get MIC");
2636+
2637+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_COMPLETE)) != 0 ||
2638+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
2639+ (r = sshpkt_put_string(ssh, msg_tok.value, msg_tok.length)) != 0)
2640+ fatal("sshpkt failed: %s", ssh_err(r));
2641+
2642+ if (send_tok.length != 0) {
2643+ if ((r = sshpkt_put_u8(ssh, 1)) != 0 || /* true */
2644+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0)
2645+ fatal("sshpkt failed: %s", ssh_err(r));
2646+ } else {
2647+ if ((r = sshpkt_put_u8(ssh, 0)) != 0) /* false */
2648+ fatal("sshpkt failed: %s", ssh_err(r));
2649+ }
2650+ if ((r = sshpkt_send(ssh)) != 0)
2651+ fatal("sshpkt failed: %s", ssh_err(r));
2652+
2653+ gss_release_buffer(&min_status, &send_tok);
2654+ gss_release_buffer(&min_status, &msg_tok);
2655+
2656+ if (gss_kex_context == NULL)
2657+ gss_kex_context = ctxt;
2658+ else
2659+ ssh_gssapi_delete_ctx(&ctxt);
2660+
2661+ /* Finally derive the keys and send them */
2662+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
2663+ r = kex_send_newkeys(ssh);
2664+
2665+ /* If this was a rekey, then save out any delegated credentials we
2666+ * just exchanged. */
2667+ if (options.gss_store_rekey)
2668+ ssh_gssapi_rekey_creds();
2669+out:
2670+ sshbuf_free(empty);
2671+ explicit_bzero(hash, sizeof(hash));
2672+ DH_free(kex->dh);
2673+ kex->dh = NULL;
2674+ BN_clear_free(dh_client_pub);
2675+ sshbuf_free(shared_secret);
2676+ return r;
2677+}
2678+#endif /* defined(GSSAPI) && defined(WITH_OPENSSL) */
2679diff --git a/mac.c b/mac.c
2680index 51dc11d76..3d11eba62 100644
2681--- a/mac.c
2682+++ b/mac.c
2683@@ -29,6 +29,7 @@
2684
2685 #include <string.h>
2686 #include <stdio.h>
2687+#include <stdlib.h>
2688
2689 #include "digest.h"
2690 #include "hmac.h"
2691diff --git a/monitor.c b/monitor.c
2692index 60e529444..0766d6ef5 100644
2693--- a/monitor.c
2694+++ b/monitor.c
2695@@ -147,6 +147,8 @@ int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
2696 int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
2697 int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
2698 int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
2699+int mm_answer_gss_sign(struct ssh *, int, struct sshbuf *);
2700+int mm_answer_gss_updatecreds(struct ssh *, int, struct sshbuf *);
2701 #endif
2702
2703 #ifdef SSH_AUDIT_EVENTS
2704@@ -219,11 +221,18 @@ struct mon_table mon_dispatch_proto20[] = {
2705 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
2706 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
2707 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
2708+ {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
2709 #endif
2710 {0, 0, NULL}
2711 };
2712
2713 struct mon_table mon_dispatch_postauth20[] = {
2714+#ifdef GSSAPI
2715+ {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
2716+ {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
2717+ {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
2718+ {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
2719+#endif
2720 #ifdef WITH_OPENSSL
2721 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
2722 #endif
2723@@ -292,6 +301,10 @@ monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
2724 /* Permit requests for moduli and signatures */
2725 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
2726 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
2727+#ifdef GSSAPI
2728+ /* and for the GSSAPI key exchange */
2729+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
2730+#endif
2731
2732 /* The first few requests do not require asynchronous access */
2733 while (!authenticated) {
2734@@ -405,6 +418,10 @@ monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
2735 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
2736 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
2737 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
2738+#ifdef GSSAPI
2739+ /* and for the GSSAPI key exchange */
2740+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
2741+#endif
2742
2743 if (auth_opts->permit_pty_flag) {
2744 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
2745@@ -1687,6 +1704,17 @@ monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
2746 # ifdef OPENSSL_HAS_ECC
2747 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
2748 # endif
2749+# ifdef GSSAPI
2750+ if (options.gss_keyex) {
2751+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
2752+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
2753+ kex->kex[KEX_GSS_GRP14_SHA256] = kexgss_server;
2754+ kex->kex[KEX_GSS_GRP16_SHA512] = kexgss_server;
2755+ kex->kex[KEX_GSS_GEX_SHA1] = kexgssgex_server;
2756+ kex->kex[KEX_GSS_NISTP256_SHA256] = kexgss_server;
2757+ kex->kex[KEX_GSS_C25519_SHA256] = kexgss_server;
2758+ }
2759+# endif
2760 #endif /* WITH_OPENSSL */
2761 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
2762 kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
2763@@ -1780,8 +1808,8 @@ mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2764 u_char *p;
2765 int r;
2766
2767- if (!options.gss_authentication)
2768- fatal("%s: GSSAPI authentication not enabled", __func__);
2769+ if (!options.gss_authentication && !options.gss_keyex)
2770+ fatal("%s: GSSAPI not enabled", __func__);
2771
2772 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
2773 fatal("%s: buffer error: %s", __func__, ssh_err(r));
2774@@ -1813,8 +1841,8 @@ mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2775 OM_uint32 flags = 0; /* GSI needs this */
2776 int r;
2777
2778- if (!options.gss_authentication)
2779- fatal("%s: GSSAPI authentication not enabled", __func__);
2780+ if (!options.gss_authentication && !options.gss_keyex)
2781+ fatal("%s: GSSAPI not enabled", __func__);
2782
2783 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
2784 fatal("%s: buffer error: %s", __func__, ssh_err(r));
2785@@ -1834,6 +1862,7 @@ mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2786 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2787 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2788 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2789+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
2790 }
2791 return (0);
2792 }
2793@@ -1845,8 +1874,8 @@ mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
2794 OM_uint32 ret;
2795 int r;
2796
2797- if (!options.gss_authentication)
2798- fatal("%s: GSSAPI authentication not enabled", __func__);
2799+ if (!options.gss_authentication && !options.gss_keyex)
2800+ fatal("%s: GSSAPI not enabled", __func__);
2801
2802 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
2803 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
2804@@ -1872,13 +1901,17 @@ mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
2805 int
2806 mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
2807 {
2808- int r, authenticated;
2809+ int r, authenticated, kex;
2810 const char *displayname;
2811
2812- if (!options.gss_authentication)
2813- fatal("%s: GSSAPI authentication not enabled", __func__);
2814+ if (!options.gss_authentication && !options.gss_keyex)
2815+ fatal("%s: GSSAPI not enabled", __func__);
2816
2817- authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2818+ if ((r = sshbuf_get_u32(m, &kex)) != 0)
2819+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2820+
2821+ authenticated = authctxt->valid &&
2822+ ssh_gssapi_userok(authctxt->user, authctxt->pw, kex);
2823
2824 sshbuf_reset(m);
2825 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
2826@@ -1887,7 +1920,11 @@ mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
2827 debug3("%s: sending result %d", __func__, authenticated);
2828 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2829
2830- auth_method = "gssapi-with-mic";
2831+ if (kex) {
2832+ auth_method = "gssapi-keyex";
2833+ } else {
2834+ auth_method = "gssapi-with-mic";
2835+ }
2836
2837 if ((displayname = ssh_gssapi_displayname()) != NULL)
2838 auth2_record_info(authctxt, "%s", displayname);
2839@@ -1895,5 +1932,85 @@ mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
2840 /* Monitor loop will terminate if authenticated */
2841 return (authenticated);
2842 }
2843+
2844+int
2845+mm_answer_gss_sign(struct ssh *ssh, int socket, struct sshbuf *m)
2846+{
2847+ gss_buffer_desc data;
2848+ gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2849+ OM_uint32 major, minor;
2850+ size_t len;
2851+ u_char *p = NULL;
2852+ int r;
2853+
2854+ if (!options.gss_authentication && !options.gss_keyex)
2855+ fatal("%s: GSSAPI not enabled", __func__);
2856+
2857+ if ((r = sshbuf_get_string(m, &p, &len)) != 0)
2858+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2859+ data.value = p;
2860+ data.length = len;
2861+ /* Lengths of SHA-1, SHA-256 and SHA-512 hashes that are used */
2862+ if (data.length != 20 && data.length != 32 && data.length != 64)
2863+ fatal("%s: data length incorrect: %d", __func__,
2864+ (int) data.length);
2865+
2866+ /* Save the session ID on the first time around */
2867+ if (session_id2_len == 0) {
2868+ session_id2_len = data.length;
2869+ session_id2 = xmalloc(session_id2_len);
2870+ memcpy(session_id2, data.value, session_id2_len);
2871+ }
2872+ major = ssh_gssapi_sign(gsscontext, &data, &hash);
2873+
2874+ free(data.value);
2875+
2876+ sshbuf_reset(m);
2877+
2878+ if ((r = sshbuf_put_u32(m, major)) != 0 ||
2879+ (r = sshbuf_put_string(m, hash.value, hash.length)) != 0)
2880+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2881+
2882+ mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2883+
2884+ gss_release_buffer(&minor, &hash);
2885+
2886+ /* Turn on getpwnam permissions */
2887+ monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2888+
2889+ /* And credential updating, for when rekeying */
2890+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
2891+
2892+ return (0);
2893+}
2894+
2895+int
2896+mm_answer_gss_updatecreds(struct ssh *ssh, int socket, struct sshbuf *m) {
2897+ ssh_gssapi_ccache store;
2898+ int r, ok;
2899+
2900+ if (!options.gss_authentication && !options.gss_keyex)
2901+ fatal("%s: GSSAPI not enabled", __func__);
2902+
2903+ if ((r = sshbuf_get_string(m, (u_char **)&store.filename, NULL)) != 0 ||
2904+ (r = sshbuf_get_string(m, (u_char **)&store.envvar, NULL)) != 0 ||
2905+ (r = sshbuf_get_string(m, (u_char **)&store.envval, NULL)) != 0)
2906+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2907+
2908+ ok = ssh_gssapi_update_creds(&store);
2909+
2910+ free(store.filename);
2911+ free(store.envvar);
2912+ free(store.envval);
2913+
2914+ sshbuf_reset(m);
2915+ if ((r = sshbuf_put_u32(m, ok)) != 0)
2916+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2917+
2918+ mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
2919+
2920+ return(0);
2921+}
2922+
2923 #endif /* GSSAPI */
2924
2925diff --git a/monitor.h b/monitor.h
2926index 683e5e071..2b1a2d590 100644
2927--- a/monitor.h
2928+++ b/monitor.h
2929@@ -63,6 +63,8 @@ enum monitor_reqtype {
2930 MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
2931 MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
2932
2933+ MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
2934+ MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
2935 };
2936
2937 struct ssh;
2938diff --git a/monitor_wrap.c b/monitor_wrap.c
2939index 186e8f022..8e4c1c1f8 100644
2940--- a/monitor_wrap.c
2941+++ b/monitor_wrap.c
2942@@ -978,13 +978,15 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
2943 }
2944
2945 int
2946-mm_ssh_gssapi_userok(char *user)
2947+mm_ssh_gssapi_userok(char *user, struct passwd *pw, int kex)
2948 {
2949 struct sshbuf *m;
2950 int r, authenticated = 0;
2951
2952 if ((m = sshbuf_new()) == NULL)
2953 fatal("%s: sshbuf_new failed", __func__);
2954+ if ((r = sshbuf_put_u32(m, kex)) != 0)
2955+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2956
2957 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
2958 mm_request_receive_expect(pmonitor->m_recvfd,
2959@@ -997,4 +999,57 @@ mm_ssh_gssapi_userok(char *user)
2960 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
2961 return (authenticated);
2962 }
2963+
2964+OM_uint32
2965+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
2966+{
2967+ struct sshbuf *m;
2968+ OM_uint32 major;
2969+ int r;
2970+
2971+ if ((m = sshbuf_new()) == NULL)
2972+ fatal("%s: sshbuf_new failed", __func__);
2973+ if ((r = sshbuf_put_string(m, data->value, data->length)) != 0)
2974+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2975+
2976+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, m);
2977+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, m);
2978+
2979+ if ((r = sshbuf_get_u32(m, &major)) != 0 ||
2980+ (r = ssh_gssapi_get_buffer_desc(m, hash)) != 0)
2981+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2982+
2983+ sshbuf_free(m);
2984+
2985+ return (major);
2986+}
2987+
2988+int
2989+mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
2990+{
2991+ struct sshbuf *m;
2992+ int r, ok;
2993+
2994+ if ((m = sshbuf_new()) == NULL)
2995+ fatal("%s: sshbuf_new failed", __func__);
2996+
2997+ if ((r = sshbuf_put_cstring(m,
2998+ store->filename ? store->filename : "")) != 0 ||
2999+ (r = sshbuf_put_cstring(m,
3000+ store->envvar ? store->envvar : "")) != 0 ||
3001+ (r = sshbuf_put_cstring(m,
3002+ store->envval ? store->envval : "")) != 0)
3003+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
3004+
3005+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, m);
3006+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, m);
3007+
3008+ if ((r = sshbuf_get_u32(m, &ok)) != 0)
3009+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
3010+
3011+ sshbuf_free(m);
3012+
3013+ return (ok);
3014+}
3015+
3016 #endif /* GSSAPI */
3017diff --git a/monitor_wrap.h b/monitor_wrap.h
3018index fdebb3aa4..69164a8c0 100644
3019--- a/monitor_wrap.h
3020+++ b/monitor_wrap.h
3021@@ -61,8 +61,10 @@ int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
3022 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
3023 OM_uint32 mm_ssh_gssapi_accept_ctx(Gssctxt *,
3024 gss_buffer_desc *, gss_buffer_desc *, OM_uint32 *);
3025-int mm_ssh_gssapi_userok(char *user);
3026+int mm_ssh_gssapi_userok(char *user, struct passwd *, int kex);
3027 OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
3028+OM_uint32 mm_ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
3029+int mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *);
3030 #endif
3031
3032 #ifdef USE_PAM
3033diff --git a/readconf.c b/readconf.c
3034index ec497e79f..4d699e5f1 100644
3035--- a/readconf.c
3036+++ b/readconf.c
3037@@ -67,6 +67,7 @@
3038 #include "uidswap.h"
3039 #include "myproposal.h"
3040 #include "digest.h"
3041+#include "ssh-gss.h"
3042
3043 /* Format of the configuration file:
3044
3045@@ -162,6 +163,8 @@ typedef enum {
3046 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
3047 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
3048 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
3049+ oGssTrustDns, oGssKeyEx, oGssClientIdentity, oGssRenewalRekey,
3050+ oGssServerIdentity, oGssKexAlgorithms,
3051 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
3052 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
3053 oHashKnownHosts,
3054@@ -202,10 +205,22 @@ static struct {
3055 /* Sometimes-unsupported options */
3056 #if defined(GSSAPI)
3057 { "gssapiauthentication", oGssAuthentication },
3058+ { "gssapikeyexchange", oGssKeyEx },
3059 { "gssapidelegatecredentials", oGssDelegateCreds },
3060+ { "gssapitrustdns", oGssTrustDns },
3061+ { "gssapiclientidentity", oGssClientIdentity },
3062+ { "gssapiserveridentity", oGssServerIdentity },
3063+ { "gssapirenewalforcesrekey", oGssRenewalRekey },
3064+ { "gssapikexalgorithms", oGssKexAlgorithms },
3065 # else
3066 { "gssapiauthentication", oUnsupported },
3067+ { "gssapikeyexchange", oUnsupported },
3068 { "gssapidelegatecredentials", oUnsupported },
3069+ { "gssapitrustdns", oUnsupported },
3070+ { "gssapiclientidentity", oUnsupported },
3071+ { "gssapiserveridentity", oUnsupported },
3072+ { "gssapirenewalforcesrekey", oUnsupported },
3073+ { "gssapikexalgorithms", oUnsupported },
3074 #endif
3075 #ifdef ENABLE_PKCS11
3076 { "pkcs11provider", oPKCS11Provider },
3077@@ -983,10 +998,42 @@ parse_time:
3078 intptr = &options->gss_authentication;
3079 goto parse_flag;
3080
3081+ case oGssKeyEx:
3082+ intptr = &options->gss_keyex;
3083+ goto parse_flag;
3084+
3085 case oGssDelegateCreds:
3086 intptr = &options->gss_deleg_creds;
3087 goto parse_flag;
3088
3089+ case oGssTrustDns:
3090+ intptr = &options->gss_trust_dns;
3091+ goto parse_flag;
3092+
3093+ case oGssClientIdentity:
3094+ charptr = &options->gss_client_identity;
3095+ goto parse_string;
3096+
3097+ case oGssServerIdentity:
3098+ charptr = &options->gss_server_identity;
3099+ goto parse_string;
3100+
3101+ case oGssRenewalRekey:
3102+ intptr = &options->gss_renewal_rekey;
3103+ goto parse_flag;
3104+
3105+ case oGssKexAlgorithms:
3106+ arg = strdelim(&s);
3107+ if (!arg || *arg == '\0')
3108+ fatal("%.200s line %d: Missing argument.",
3109+ filename, linenum);
3110+ if (!kex_gss_names_valid(arg))
3111+ fatal("%.200s line %d: Bad GSSAPI KexAlgorithms '%s'.",
3112+ filename, linenum, arg ? arg : "<NONE>");
3113+ if (*activep && options->gss_kex_algorithms == NULL)
3114+ options->gss_kex_algorithms = xstrdup(arg);
3115+ break;
3116+
3117 case oBatchMode:
3118 intptr = &options->batch_mode;
3119 goto parse_flag;
3120@@ -1854,7 +1901,13 @@ initialize_options(Options * options)
3121 options->pubkey_authentication = -1;
3122 options->challenge_response_authentication = -1;
3123 options->gss_authentication = -1;
3124+ options->gss_keyex = -1;
3125 options->gss_deleg_creds = -1;
3126+ options->gss_trust_dns = -1;
3127+ options->gss_renewal_rekey = -1;
3128+ options->gss_client_identity = NULL;
3129+ options->gss_server_identity = NULL;
3130+ options->gss_kex_algorithms = NULL;
3131 options->password_authentication = -1;
3132 options->kbd_interactive_authentication = -1;
3133 options->kbd_interactive_devices = NULL;
3134@@ -2000,8 +2053,18 @@ fill_default_options(Options * options)
3135 options->challenge_response_authentication = 1;
3136 if (options->gss_authentication == -1)
3137 options->gss_authentication = 0;
3138+ if (options->gss_keyex == -1)
3139+ options->gss_keyex = 0;
3140 if (options->gss_deleg_creds == -1)
3141 options->gss_deleg_creds = 0;
3142+ if (options->gss_trust_dns == -1)
3143+ options->gss_trust_dns = 0;
3144+ if (options->gss_renewal_rekey == -1)
3145+ options->gss_renewal_rekey = 0;
3146+#ifdef GSSAPI
3147+ if (options->gss_kex_algorithms == NULL)
3148+ options->gss_kex_algorithms = strdup(GSS_KEX_DEFAULT_KEX);
3149+#endif
3150 if (options->password_authentication == -1)
3151 options->password_authentication = 1;
3152 if (options->kbd_interactive_authentication == -1)
3153@@ -2616,7 +2679,14 @@ dump_client_config(Options *o, const char *host)
3154 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
3155 #ifdef GSSAPI
3156 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
3157+ dump_cfg_fmtint(oGssKeyEx, o->gss_keyex);
3158 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
3159+ dump_cfg_fmtint(oGssTrustDns, o->gss_trust_dns);
3160+ dump_cfg_fmtint(oGssRenewalRekey, o->gss_renewal_rekey);
3161+ dump_cfg_string(oGssClientIdentity, o->gss_client_identity);
3162+ dump_cfg_string(oGssServerIdentity, o->gss_server_identity);
3163+ dump_cfg_string(oGssKexAlgorithms, o->gss_kex_algorithms ?
3164+ o->gss_kex_algorithms : GSS_KEX_DEFAULT_KEX);
3165 #endif /* GSSAPI */
3166 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
3167 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
3168diff --git a/readconf.h b/readconf.h
3169index 8e36bf32a..0bff6d80a 100644
3170--- a/readconf.h
3171+++ b/readconf.h
3172@@ -40,7 +40,13 @@ typedef struct {
3173 int challenge_response_authentication;
3174 /* Try S/Key or TIS, authentication. */
3175 int gss_authentication; /* Try GSS authentication */
3176+ int gss_keyex; /* Try GSS key exchange */
3177 int gss_deleg_creds; /* Delegate GSS credentials */
3178+ int gss_trust_dns; /* Trust DNS for GSS canonicalization */
3179+ int gss_renewal_rekey; /* Credential renewal forces rekey */
3180+ char *gss_client_identity; /* Principal to initiate GSSAPI with */
3181+ char *gss_server_identity; /* GSSAPI target principal */
3182+ char *gss_kex_algorithms; /* GSSAPI kex methods to be offered by client. */
3183 int password_authentication; /* Try password
3184 * authentication. */
3185 int kbd_interactive_authentication; /* Try keyboard-interactive auth. */
3186diff --git a/servconf.c b/servconf.c
3187index ffac5d2c7..ffdad31e7 100644
3188--- a/servconf.c
3189+++ b/servconf.c
3190@@ -64,6 +64,7 @@
3191 #include "auth.h"
3192 #include "myproposal.h"
3193 #include "digest.h"
3194+#include "ssh-gss.h"
3195
3196 static void add_listen_addr(ServerOptions *, const char *,
3197 const char *, int);
3198@@ -124,8 +125,11 @@ initialize_server_options(ServerOptions *options)
3199 options->kerberos_ticket_cleanup = -1;
3200 options->kerberos_get_afs_token = -1;
3201 options->gss_authentication=-1;
3202+ options->gss_keyex = -1;
3203 options->gss_cleanup_creds = -1;
3204 options->gss_strict_acceptor = -1;
3205+ options->gss_store_rekey = -1;
3206+ options->gss_kex_algorithms = NULL;
3207 options->password_authentication = -1;
3208 options->kbd_interactive_authentication = -1;
3209 options->challenge_response_authentication = -1;
3210@@ -351,10 +355,18 @@ fill_default_server_options(ServerOptions *options)
3211 options->kerberos_get_afs_token = 0;
3212 if (options->gss_authentication == -1)
3213 options->gss_authentication = 0;
3214+ if (options->gss_keyex == -1)
3215+ options->gss_keyex = 0;
3216 if (options->gss_cleanup_creds == -1)
3217 options->gss_cleanup_creds = 1;
3218 if (options->gss_strict_acceptor == -1)
3219 options->gss_strict_acceptor = 1;
3220+ if (options->gss_store_rekey == -1)
3221+ options->gss_store_rekey = 0;
3222+#ifdef GSSAPI
3223+ if (options->gss_kex_algorithms == NULL)
3224+ options->gss_kex_algorithms = strdup(GSS_KEX_DEFAULT_KEX);
3225+#endif
3226 if (options->password_authentication == -1)
3227 options->password_authentication = 1;
3228 if (options->kbd_interactive_authentication == -1)
3229@@ -498,6 +510,7 @@ typedef enum {
3230 sHostKeyAlgorithms,
3231 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
3232 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
3233+ sGssKeyEx, sGssKexAlgorithms, sGssStoreRekey,
3234 sAcceptEnv, sSetEnv, sPermitTunnel,
3235 sMatch, sPermitOpen, sPermitListen, sForceCommand, sChrootDirectory,
3236 sUsePrivilegeSeparation, sAllowAgentForwarding,
3237@@ -572,12 +585,22 @@ static struct {
3238 #ifdef GSSAPI
3239 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
3240 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
3241+ { "gssapicleanupcreds", sGssCleanupCreds, SSHCFG_GLOBAL },
3242 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
3243+ { "gssapikeyexchange", sGssKeyEx, SSHCFG_GLOBAL },
3244+ { "gssapistorecredentialsonrekey", sGssStoreRekey, SSHCFG_GLOBAL },
3245+ { "gssapikexalgorithms", sGssKexAlgorithms, SSHCFG_GLOBAL },
3246 #else
3247 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
3248 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
3249+ { "gssapicleanupcreds", sUnsupported, SSHCFG_GLOBAL },
3250 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
3251+ { "gssapikeyexchange", sUnsupported, SSHCFG_GLOBAL },
3252+ { "gssapistorecredentialsonrekey", sUnsupported, SSHCFG_GLOBAL },
3253+ { "gssapikexalgorithms", sUnsupported, SSHCFG_GLOBAL },
3254 #endif
3255+ { "gssusesessionccache", sUnsupported, SSHCFG_GLOBAL },
3256+ { "gssapiusesessioncredcache", sUnsupported, SSHCFG_GLOBAL },
3257 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
3258 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
3259 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
3260@@ -1485,6 +1508,10 @@ process_server_config_line(ServerOptions *options, char *line,
3261 intptr = &options->gss_authentication;
3262 goto parse_flag;
3263
3264+ case sGssKeyEx:
3265+ intptr = &options->gss_keyex;
3266+ goto parse_flag;
3267+
3268 case sGssCleanupCreds:
3269 intptr = &options->gss_cleanup_creds;
3270 goto parse_flag;
3271@@ -1493,6 +1520,22 @@ process_server_config_line(ServerOptions *options, char *line,
3272 intptr = &options->gss_strict_acceptor;
3273 goto parse_flag;
3274
3275+ case sGssStoreRekey:
3276+ intptr = &options->gss_store_rekey;
3277+ goto parse_flag;
3278+
3279+ case sGssKexAlgorithms:
3280+ arg = strdelim(&cp);
3281+ if (!arg || *arg == '\0')
3282+ fatal("%.200s line %d: Missing argument.",
3283+ filename, linenum);
3284+ if (!kex_gss_names_valid(arg))
3285+ fatal("%.200s line %d: Bad GSSAPI KexAlgorithms '%s'.",
3286+ filename, linenum, arg ? arg : "<NONE>");
3287+ if (*activep && options->gss_kex_algorithms == NULL)
3288+ options->gss_kex_algorithms = xstrdup(arg);
3289+ break;
3290+
3291 case sPasswordAuthentication:
3292 intptr = &options->password_authentication;
3293 goto parse_flag;
3294@@ -2579,6 +2622,10 @@ dump_config(ServerOptions *o)
3295 #ifdef GSSAPI
3296 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
3297 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
3298+ dump_cfg_fmtint(sGssKeyEx, o->gss_keyex);
3299+ dump_cfg_fmtint(sGssStrictAcceptor, o->gss_strict_acceptor);
3300+ dump_cfg_fmtint(sGssStoreRekey, o->gss_store_rekey);
3301+ dump_cfg_string(sGssKexAlgorithms, o->gss_kex_algorithms);
3302 #endif
3303 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
3304 dump_cfg_fmtint(sKbdInteractiveAuthentication,
3305diff --git a/servconf.h b/servconf.h
3306index 54e0a8d8d..a476d5220 100644
3307--- a/servconf.h
3308+++ b/servconf.h
3309@@ -126,8 +126,11 @@ typedef struct {
3310 int kerberos_get_afs_token; /* If true, try to get AFS token if
3311 * authenticated with Kerberos. */
3312 int gss_authentication; /* If true, permit GSSAPI authentication */
3313+ int gss_keyex; /* If true, permit GSSAPI key exchange */
3314 int gss_cleanup_creds; /* If true, destroy cred cache on logout */
3315 int gss_strict_acceptor; /* If true, restrict the GSSAPI acceptor name */
3316+ int gss_store_rekey;
3317+ char *gss_kex_algorithms; /* GSSAPI kex methods to be offered by client. */
3318 int password_authentication; /* If true, permit password
3319 * authentication. */
3320 int kbd_interactive_authentication; /* If true, permit */
3321diff --git a/session.c b/session.c
3322index ac06b08e9..ac3d9d19d 100644
3323--- a/session.c
3324+++ b/session.c
3325@@ -2674,13 +2674,19 @@ do_cleanup(struct ssh *ssh, Authctxt *authctxt)
3326
3327 #ifdef KRB5
3328 if (options.kerberos_ticket_cleanup &&
3329- authctxt->krb5_ctx)
3330+ authctxt->krb5_ctx) {
3331+ temporarily_use_uid(authctxt->pw);
3332 krb5_cleanup_proc(authctxt);
3333+ restore_uid();
3334+ }
3335 #endif
3336
3337 #ifdef GSSAPI
3338- if (options.gss_cleanup_creds)
3339+ if (options.gss_cleanup_creds) {
3340+ temporarily_use_uid(authctxt->pw);
3341 ssh_gssapi_cleanup_creds();
3342+ restore_uid();
3343+ }
3344 #endif
3345
3346 /* remove agent socket */
3347diff --git a/ssh-gss.h b/ssh-gss.h
3348index 36180d07a..70dd36658 100644
3349--- a/ssh-gss.h
3350+++ b/ssh-gss.h
3351@@ -1,6 +1,6 @@
3352 /* $OpenBSD: ssh-gss.h,v 1.14 2018/07/10 09:13:30 djm Exp $ */
3353 /*
3354- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
3355+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
3356 *
3357 * Redistribution and use in source and binary forms, with or without
3358 * modification, are permitted provided that the following conditions
3359@@ -61,10 +61,30 @@
3360
3361 #define SSH_GSS_OIDTYPE 0x06
3362
3363+#define SSH2_MSG_KEXGSS_INIT 30
3364+#define SSH2_MSG_KEXGSS_CONTINUE 31
3365+#define SSH2_MSG_KEXGSS_COMPLETE 32
3366+#define SSH2_MSG_KEXGSS_HOSTKEY 33
3367+#define SSH2_MSG_KEXGSS_ERROR 34
3368+#define SSH2_MSG_KEXGSS_GROUPREQ 40
3369+#define SSH2_MSG_KEXGSS_GROUP 41
3370+#define KEX_GSS_GRP1_SHA1_ID "gss-group1-sha1-"
3371+#define KEX_GSS_GRP14_SHA1_ID "gss-group14-sha1-"
3372+#define KEX_GSS_GRP14_SHA256_ID "gss-group14-sha256-"
3373+#define KEX_GSS_GRP16_SHA512_ID "gss-group16-sha512-"
3374+#define KEX_GSS_GEX_SHA1_ID "gss-gex-sha1-"
3375+#define KEX_GSS_NISTP256_SHA256_ID "gss-nistp256-sha256-"
3376+#define KEX_GSS_C25519_SHA256_ID "gss-curve25519-sha256-"
3377+
3378+#define GSS_KEX_DEFAULT_KEX \
3379+ KEX_GSS_GEX_SHA1_ID "," \
3380+ KEX_GSS_GRP14_SHA1_ID
3381+
3382 typedef struct {
3383 char *filename;
3384 char *envvar;
3385 char *envval;
3386+ struct passwd *owner;
3387 void *data;
3388 } ssh_gssapi_ccache;
3389
3390@@ -72,8 +92,11 @@ typedef struct {
3391 gss_buffer_desc displayname;
3392 gss_buffer_desc exportedname;
3393 gss_cred_id_t creds;
3394+ gss_name_t name;
3395 struct ssh_gssapi_mech_struct *mech;
3396 ssh_gssapi_ccache store;
3397+ int used;
3398+ int updated;
3399 } ssh_gssapi_client;
3400
3401 typedef struct ssh_gssapi_mech_struct {
3402@@ -84,6 +107,7 @@ typedef struct ssh_gssapi_mech_struct {
3403 int (*userok) (ssh_gssapi_client *, char *);
3404 int (*localname) (ssh_gssapi_client *, char **);
3405 void (*storecreds) (ssh_gssapi_client *);
3406+ int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
3407 } ssh_gssapi_mech;
3408
3409 typedef struct {
3410@@ -94,10 +118,11 @@ typedef struct {
3411 gss_OID oid; /* client */
3412 gss_cred_id_t creds; /* server */
3413 gss_name_t client; /* server */
3414- gss_cred_id_t client_creds; /* server */
3415+ gss_cred_id_t client_creds; /* both */
3416 } Gssctxt;
3417
3418 extern ssh_gssapi_mech *supported_mechs[];
3419+extern Gssctxt *gss_kex_context;
3420
3421 int ssh_gssapi_check_oid(Gssctxt *, void *, size_t);
3422 void ssh_gssapi_set_oid_data(Gssctxt *, void *, size_t);
3423@@ -109,6 +134,7 @@ OM_uint32 ssh_gssapi_test_oid_supported(OM_uint32 *, gss_OID, int *);
3424
3425 struct sshbuf;
3426 int ssh_gssapi_get_buffer_desc(struct sshbuf *, gss_buffer_desc *);
3427+int ssh_gssapi_sshpkt_get_buffer_desc(struct ssh *, gss_buffer_desc *);
3428
3429 OM_uint32 ssh_gssapi_import_name(Gssctxt *, const char *);
3430 OM_uint32 ssh_gssapi_init_ctx(Gssctxt *, int,
3431@@ -123,17 +149,33 @@ void ssh_gssapi_delete_ctx(Gssctxt **);
3432 OM_uint32 ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
3433 void ssh_gssapi_buildmic(struct sshbuf *, const char *,
3434 const char *, const char *);
3435-int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *);
3436+int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *, const char *);
3437+OM_uint32 ssh_gssapi_client_identity(Gssctxt *, const char *);
3438+int ssh_gssapi_credentials_updated(Gssctxt *);
3439
3440 /* In the server */
3441+typedef int ssh_gssapi_check_fn(Gssctxt **, gss_OID, const char *,
3442+ const char *);
3443+char *ssh_gssapi_client_mechanisms(const char *, const char *, const char *);
3444+char *ssh_gssapi_kex_mechs(gss_OID_set, ssh_gssapi_check_fn *, const char *,
3445+ const char *, const char *);
3446+gss_OID ssh_gssapi_id_kex(Gssctxt *, char *, int);
3447+int ssh_gssapi_server_check_mech(Gssctxt **,gss_OID, const char *,
3448+ const char *);
3449 OM_uint32 ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
3450-int ssh_gssapi_userok(char *name);
3451+int ssh_gssapi_userok(char *name, struct passwd *, int kex);
3452 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
3453 void ssh_gssapi_do_child(char ***, u_int *);
3454 void ssh_gssapi_cleanup_creds(void);
3455 void ssh_gssapi_storecreds(void);
3456 const char *ssh_gssapi_displayname(void);
3457
3458+char *ssh_gssapi_server_mechanisms(void);
3459+int ssh_gssapi_oid_table_ok(void);
3460+
3461+int ssh_gssapi_update_creds(ssh_gssapi_ccache *store);
3462+void ssh_gssapi_rekey_creds(void);
3463+
3464 #endif /* GSSAPI */
3465
3466 #endif /* _SSH_GSS_H */
3467diff --git a/ssh.1 b/ssh.1
3468index 9480eba8d..a1c7d2305 100644
3469--- a/ssh.1
3470+++ b/ssh.1
3471@@ -497,7 +497,13 @@ For full details of the options listed below, and their possible values, see
3472 .It GatewayPorts
3473 .It GlobalKnownHostsFile
3474 .It GSSAPIAuthentication
3475+.It GSSAPIKeyExchange
3476+.It GSSAPIClientIdentity
3477 .It GSSAPIDelegateCredentials
3478+.It GSSAPIKexAlgorithms
3479+.It GSSAPIRenewalForcesRekey
3480+.It GSSAPIServerIdentity
3481+.It GSSAPITrustDns
3482 .It HashKnownHosts
3483 .It Host
3484 .It HostbasedAuthentication
3485@@ -573,6 +579,8 @@ flag),
3486 (supported message integrity codes),
3487 .Ar kex
3488 (key exchange algorithms),
3489+.Ar kex-gss
3490+(GSSAPI key exchange algorithms),
3491 .Ar key
3492 (key types),
3493 .Ar key-cert
3494diff --git a/ssh.c b/ssh.c
3495index 91e7c3511..42be7d88f 100644
3496--- a/ssh.c
3497+++ b/ssh.c
3498@@ -736,6 +736,8 @@ main(int ac, char **av)
3499 cp = mac_alg_list('\n');
3500 else if (strcmp(optarg, "kex") == 0)
3501 cp = kex_alg_list('\n');
3502+ else if (strcmp(optarg, "kex-gss") == 0)
3503+ cp = kex_gss_alg_list('\n');
3504 else if (strcmp(optarg, "key") == 0)
3505 cp = sshkey_alg_list(0, 0, 0, '\n');
3506 else if (strcmp(optarg, "key-cert") == 0)
3507@@ -748,7 +750,7 @@ main(int ac, char **av)
3508 cp = xstrdup("2");
3509 else if (strcmp(optarg, "help") == 0) {
3510 cp = xstrdup(
3511- "cipher\ncipher-auth\nkex\nkey\n"
3512+ "cipher\ncipher-auth\nkex\nkex-gss\nkey\n"
3513 "key-cert\nkey-plain\nmac\n"
3514 "protocol-version\nsig");
3515 }
3516diff --git a/ssh_config b/ssh_config
3517index 5e8ef548b..1ff999b68 100644
3518--- a/ssh_config
3519+++ b/ssh_config
3520@@ -24,6 +24,8 @@
3521 # HostbasedAuthentication no
3522 # GSSAPIAuthentication no
3523 # GSSAPIDelegateCredentials no
3524+# GSSAPIKeyExchange no
3525+# GSSAPITrustDNS no
3526 # BatchMode no
3527 # CheckHostIP yes
3528 # AddressFamily any
3529diff --git a/ssh_config.5 b/ssh_config.5
3530index 412629637..c3c8b274a 100644
3531--- a/ssh_config.5
3532+++ b/ssh_config.5
3533@@ -754,10 +754,67 @@ The default is
3534 Specifies whether user authentication based on GSSAPI is allowed.
3535 The default is
3536 .Cm no .
3537+.It Cm GSSAPIClientIdentity
3538+If set, specifies the GSSAPI client identity that ssh should use when
3539+connecting to the server. The default is unset, which means that the default
3540+identity will be used.
3541 .It Cm GSSAPIDelegateCredentials
3542 Forward (delegate) credentials to the server.
3543 The default is
3544 .Cm no .
3545+.It Cm GSSAPIKeyExchange
3546+Specifies whether key exchange based on GSSAPI may be used. When using
3547+GSSAPI key exchange the server need not have a host key.
3548+The default is
3549+.Dq no .
3550+.It Cm GSSAPIRenewalForcesRekey
3551+If set to
3552+.Dq yes
3553+then renewal of the client's GSSAPI credentials will force the rekeying of the
3554+ssh connection. With a compatible server, this will delegate the renewed
3555+credentials to a session on the server.
3556+.Pp
3557+Checks are made to ensure that credentials are only propagated when the new
3558+credentials match the old ones on the originating client and where the
3559+receiving server still has the old set in its cache.
3560+.Pp
3561+The default is
3562+.Dq no .
3563+.Pp
3564+For this to work
3565+.Cm GSSAPIKeyExchange
3566+needs to be enabled in the server and also used by the client.
3567+.It Cm GSSAPIServerIdentity
3568+If set, specifies the GSSAPI server identity that ssh should expect when
3569+connecting to the server. The default is unset, which means that the
3570+expected GSSAPI server identity will be determined from the target
3571+hostname.
3572+.It Cm GSSAPITrustDns
3573+Set to
3574+.Dq yes
3575+to indicate that the DNS is trusted to securely canonicalize
3576+the name of the host being connected to. If
3577+.Dq no ,
3578+the hostname entered on the
3579+command line will be passed untouched to the GSSAPI library.
3580+The default is
3581+.Dq no .
3582+.It Cm GSSAPIKexAlgorithms
3583+The list of key exchange algorithms that are offered for GSSAPI
3584+key exchange. Possible values are
3585+.Bd -literal -offset 3n
3586+gss-gex-sha1-,
3587+gss-group1-sha1-,
3588+gss-group14-sha1-,
3589+gss-group14-sha256-,
3590+gss-group16-sha512-,
3591+gss-nistp256-sha256-,
3592+gss-curve25519-sha256-
3593+.Ed
3594+.Pp
3595+The default is
3596+.Dq gss-gex-sha1-,gss-group14-sha1- .
3597+This option only applies to protocol version 2 connections using GSSAPI.
3598 .It Cm HashKnownHosts
3599 Indicates that
3600 .Xr ssh 1
3601diff --git a/sshconnect2.c b/sshconnect2.c
3602index dffee90b1..4020371ae 100644
3603--- a/sshconnect2.c
3604+++ b/sshconnect2.c
3605@@ -78,8 +78,6 @@
3606 #endif
3607
3608 /* import */
3609-extern char *client_version_string;
3610-extern char *server_version_string;
3611 extern Options options;
3612
3613 /*
3614@@ -161,6 +159,11 @@ ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
3615 char *s, *all_key;
3616 int r;
3617
3618+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3619+ char *orig = NULL, *gss = NULL;
3620+ char *gss_host = NULL;
3621+#endif
3622+
3623 xxx_host = host;
3624 xxx_hostaddr = hostaddr;
3625
3626@@ -193,6 +196,35 @@ ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
3627 order_hostkeyalgs(host, hostaddr, port));
3628 }
3629
3630+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3631+ if (options.gss_keyex) {
3632+ /* Add the GSSAPI mechanisms currently supported on this
3633+ * client to the key exchange algorithm proposal */
3634+ orig = myproposal[PROPOSAL_KEX_ALGS];
3635+
3636+ if (options.gss_server_identity)
3637+ gss_host = xstrdup(options.gss_server_identity);
3638+ else if (options.gss_trust_dns)
3639+ gss_host = remote_hostname(ssh);
3640+ else
3641+ gss_host = xstrdup(host);
3642+
3643+ gss = ssh_gssapi_client_mechanisms(gss_host,
3644+ options.gss_client_identity, options.gss_kex_algorithms);
3645+ if (gss) {
3646+ debug("Offering GSSAPI proposal: %s", gss);
3647+ xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
3648+ "%s,%s", gss, orig);
3649+
3650+ /* If we've got GSSAPI algorithms, then we also support the
3651+ * 'null' hostkey, as a last resort */
3652+ orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
3653+ xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
3654+ "%s,null", orig);
3655+ }
3656+ }
3657+#endif
3658+
3659 if (options.rekey_limit || options.rekey_interval)
3660 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
3661 options.rekey_interval);
3662@@ -211,16 +243,46 @@ ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
3663 # ifdef OPENSSL_HAS_ECC
3664 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
3665 # endif
3666+# ifdef GSSAPI
3667+ if (options.gss_keyex) {
3668+ ssh->kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
3669+ ssh->kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
3670+ ssh->kex->kex[KEX_GSS_GRP14_SHA256] = kexgss_client;
3671+ ssh->kex->kex[KEX_GSS_GRP16_SHA512] = kexgss_client;
3672+ ssh->kex->kex[KEX_GSS_GEX_SHA1] = kexgssgex_client;
3673+ ssh->kex->kex[KEX_GSS_NISTP256_SHA256] = kexgss_client;
3674+ ssh->kex->kex[KEX_GSS_C25519_SHA256] = kexgss_client;
3675+ }
3676+# endif
3677 #endif
3678 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
3679 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
3680 ssh->kex->verify_host_key=&verify_host_key_callback;
3681
3682+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3683+ if (options.gss_keyex) {
3684+ ssh->kex->gss_deleg_creds = options.gss_deleg_creds;
3685+ ssh->kex->gss_trust_dns = options.gss_trust_dns;
3686+ ssh->kex->gss_client = options.gss_client_identity;
3687+ ssh->kex->gss_host = gss_host;
3688+ }
3689+#endif
3690+
3691 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
3692
3693 /* remove ext-info from the KEX proposals for rekeying */
3694 myproposal[PROPOSAL_KEX_ALGS] =
3695 compat_kex_proposal(options.kex_algorithms);
3696+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3697+ /* repair myproposal after it was crumpled by the */
3698+ /* ext-info removal above */
3699+ if (gss) {
3700+ orig = myproposal[PROPOSAL_KEX_ALGS];
3701+ xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
3702+ "%s,%s", gss, orig);
3703+ free(gss);
3704+ }
3705+#endif
3706 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
3707 fatal("kex_prop2buf: %s", ssh_err(r));
3708
3709@@ -317,6 +379,7 @@ static int input_gssapi_response(int type, u_int32_t, struct ssh *);
3710 static int input_gssapi_token(int type, u_int32_t, struct ssh *);
3711 static int input_gssapi_error(int, u_int32_t, struct ssh *);
3712 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
3713+static int userauth_gsskeyex(struct ssh *);
3714 #endif
3715
3716 void userauth(struct ssh *, char *);
3717@@ -333,6 +396,11 @@ static char *authmethods_get(void);
3718
3719 Authmethod authmethods[] = {
3720 #ifdef GSSAPI
3721+ {"gssapi-keyex",
3722+ userauth_gsskeyex,
3723+ NULL,
3724+ &options.gss_keyex,
3725+ NULL},
3726 {"gssapi-with-mic",
3727 userauth_gssapi,
3728 userauth_gssapi_cleanup,
3729@@ -698,12 +766,25 @@ userauth_gssapi(struct ssh *ssh)
3730 OM_uint32 min;
3731 int r, ok = 0;
3732 gss_OID mech = NULL;
3733+ char *gss_host;
3734+
3735+ if (options.gss_server_identity)
3736+ gss_host = xstrdup(options.gss_server_identity);
3737+ else if (options.gss_trust_dns)
3738+ gss_host = remote_hostname(ssh);
3739+ else
3740+ gss_host = xstrdup(authctxt->host);
3741
3742 /* Try one GSSAPI method at a time, rather than sending them all at
3743 * once. */
3744
3745 if (authctxt->gss_supported_mechs == NULL)
3746- gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
3747+ if (GSS_ERROR(gss_indicate_mechs(&min,
3748+ &authctxt->gss_supported_mechs))) {
3749+ authctxt->gss_supported_mechs = NULL;
3750+ free(gss_host);
3751+ return 0;
3752+ }
3753
3754 /* Check to see whether the mechanism is usable before we offer it */
3755 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
3756@@ -712,13 +793,15 @@ userauth_gssapi(struct ssh *ssh)
3757 elements[authctxt->mech_tried];
3758 /* My DER encoding requires length<128 */
3759 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
3760- mech, authctxt->host)) {
3761+ mech, gss_host, options.gss_client_identity)) {
3762 ok = 1; /* Mechanism works */
3763 } else {
3764 authctxt->mech_tried++;
3765 }
3766 }
3767
3768+ free(gss_host);
3769+
3770 if (!ok || mech == NULL)
3771 return 0;
3772
3773@@ -958,6 +1041,55 @@ input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
3774 free(lang);
3775 return r;
3776 }
3777+
3778+int
3779+userauth_gsskeyex(struct ssh *ssh)
3780+{
3781+ struct sshbuf *b = NULL;
3782+ Authctxt *authctxt = ssh->authctxt;
3783+ gss_buffer_desc gssbuf;
3784+ gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
3785+ OM_uint32 ms;
3786+ int r;
3787+
3788+ static int attempt = 0;
3789+ if (attempt++ >= 1)
3790+ return (0);
3791+
3792+ if (gss_kex_context == NULL) {
3793+ debug("No valid Key exchange context");
3794+ return (0);
3795+ }
3796+
3797+ if ((b = sshbuf_new()) == NULL)
3798+ fatal("%s: sshbuf_new failed", __func__);
3799+
3800+ ssh_gssapi_buildmic(b, authctxt->server_user, authctxt->service,
3801+ "gssapi-keyex");
3802+
3803+ if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
3804+ fatal("%s: sshbuf_mutable_ptr failed", __func__);
3805+ gssbuf.length = sshbuf_len(b);
3806+
3807+ if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
3808+ sshbuf_free(b);
3809+ return (0);
3810+ }
3811+
3812+ if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
3813+ (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
3814+ (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
3815+ (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
3816+ (r = sshpkt_put_string(ssh, mic.value, mic.length)) != 0 ||
3817+ (r = sshpkt_send(ssh)) != 0)
3818+ fatal("%s: %s", __func__, ssh_err(r));
3819+
3820+ sshbuf_free(b);
3821+ gss_release_buffer(&ms, &mic);
3822+
3823+ return (1);
3824+}
3825+
3826 #endif /* GSSAPI */
3827
3828 static int
3829diff --git a/sshd.c b/sshd.c
3830index cbd3bce91..98680721b 100644
3831--- a/sshd.c
3832+++ b/sshd.c
3833@@ -123,6 +123,10 @@
3834 #include "version.h"
3835 #include "ssherr.h"
3836
3837+#ifdef USE_SECURITY_SESSION_API
3838+#include <Security/AuthSession.h>
3839+#endif
3840+
3841 /* Re-exec fds */
3842 #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
3843 #define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
3844@@ -796,8 +800,8 @@ notify_hostkeys(struct ssh *ssh)
3845 }
3846 debug3("%s: sent %u hostkeys", __func__, nkeys);
3847 if (nkeys == 0)
3848- fatal("%s: no hostkeys", __func__);
3849- if ((r = sshpkt_send(ssh)) != 0)
3850+ debug3("%s: no hostkeys", __func__);
3851+ else if ((r = sshpkt_send(ssh)) != 0)
3852 sshpkt_fatal(ssh, r, "%s: send", __func__);
3853 sshbuf_free(buf);
3854 }
3855@@ -1769,7 +1773,8 @@ main(int ac, char **av)
3856 free(fp);
3857 }
3858 accumulate_host_timing_secret(cfg, NULL);
3859- if (!sensitive_data.have_ssh2_key) {
3860+ /* The GSSAPI key exchange can run without a host key */
3861+ if (!sensitive_data.have_ssh2_key && !options.gss_keyex) {
3862 logit("sshd: no hostkeys available -- exiting.");
3863 exit(1);
3864 }
3865@@ -2064,6 +2069,60 @@ main(int ac, char **av)
3866 rdomain == NULL ? "" : "\"");
3867 free(laddr);
3868
3869+#ifdef USE_SECURITY_SESSION_API
3870+ /*
3871+ * Create a new security session for use by the new user login if
3872+ * the current session is the root session or we are not launched
3873+ * by inetd (eg: debugging mode or server mode). We do not
3874+ * necessarily need to create a session if we are launched from
3875+ * inetd because Panther xinetd will create a session for us.
3876+ *
3877+ * The only case where this logic will fail is if there is an
3878+ * inetd running in a non-root session which is not creating
3879+ * new sessions for us. Then all the users will end up in the
3880+ * same session (bad).
3881+ *
3882+ * When the client exits, the session will be destroyed for us
3883+ * automatically.
3884+ *
3885+ * We must create the session before any credentials are stored
3886+ * (including AFS pags, which happens a few lines below).
3887+ */
3888+ {
3889+ OSStatus err = 0;
3890+ SecuritySessionId sid = 0;
3891+ SessionAttributeBits sattrs = 0;
3892+
3893+ err = SessionGetInfo(callerSecuritySession, &sid, &sattrs);
3894+ if (err)
3895+ error("SessionGetInfo() failed with error %.8X",
3896+ (unsigned) err);
3897+ else
3898+ debug("Current Session ID is %.8X / Session Attributes are %.8X",
3899+ (unsigned) sid, (unsigned) sattrs);
3900+
3901+ if (inetd_flag && !(sattrs & sessionIsRoot))
3902+ debug("Running in inetd mode in a non-root session... "
3903+ "assuming inetd created the session for us.");
3904+ else {
3905+ debug("Creating new security session...");
3906+ err = SessionCreate(0, sessionHasTTY | sessionIsRemote);
3907+ if (err)
3908+ error("SessionCreate() failed with error %.8X",
3909+ (unsigned) err);
3910+
3911+ err = SessionGetInfo(callerSecuritySession, &sid,
3912+ &sattrs);
3913+ if (err)
3914+ error("SessionGetInfo() failed with error %.8X",
3915+ (unsigned) err);
3916+ else
3917+ debug("New Session ID is %.8X / Session Attributes are %.8X",
3918+ (unsigned) sid, (unsigned) sattrs);
3919+ }
3920+ }
3921+#endif
3922+
3923 /*
3924 * We don't want to listen forever unless the other side
3925 * successfully authenticates itself. So we set up an alarm which is
3926@@ -2260,6 +2319,48 @@ do_ssh2_kex(struct ssh *ssh)
3927 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
3928 list_hostkey_types());
3929
3930+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3931+ {
3932+ char *orig;
3933+ char *gss = NULL;
3934+ char *newstr = NULL;
3935+ orig = myproposal[PROPOSAL_KEX_ALGS];
3936+
3937+ /*
3938+ * If we don't have a host key, then there's no point advertising
3939+ * the other key exchange algorithms
3940+ */
3941+
3942+ if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
3943+ orig = NULL;
3944+
3945+ if (options.gss_keyex)
3946+ gss = ssh_gssapi_server_mechanisms();
3947+ else
3948+ gss = NULL;
3949+
3950+ if (gss && orig)
3951+ xasprintf(&newstr, "%s,%s", gss, orig);
3952+ else if (gss)
3953+ newstr = gss;
3954+ else if (orig)
3955+ newstr = orig;
3956+
3957+ /*
3958+ * If we've got GSSAPI mechanisms, then we've got the 'null' host
3959+ * key alg, but we can't tell people about it unless its the only
3960+ * host key algorithm we support
3961+ */
3962+ if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0)
3963+ myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = "null";
3964+
3965+ if (newstr)
3966+ myproposal[PROPOSAL_KEX_ALGS] = newstr;
3967+ else
3968+ fatal("No supported key exchange algorithms");
3969+ }
3970+#endif
3971+
3972 /* start key exchange */
3973 if ((r = kex_setup(ssh, myproposal)) != 0)
3974 fatal("kex_setup: %s", ssh_err(r));
3975@@ -2275,7 +2376,18 @@ do_ssh2_kex(struct ssh *ssh)
3976 # ifdef OPENSSL_HAS_ECC
3977 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
3978 # endif
3979-#endif
3980+# ifdef GSSAPI
3981+ if (options.gss_keyex) {
3982+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
3983+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
3984+ kex->kex[KEX_GSS_GRP14_SHA256] = kexgss_server;
3985+ kex->kex[KEX_GSS_GRP16_SHA512] = kexgss_server;
3986+ kex->kex[KEX_GSS_GEX_SHA1] = kexgssgex_server;
3987+ kex->kex[KEX_GSS_NISTP256_SHA256] = kexgss_server;
3988+ kex->kex[KEX_GSS_C25519_SHA256] = kexgss_server;
3989+ }
3990+# endif
3991+#endif /* WITH_OPENSSL */
3992 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
3993 kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
3994 kex->load_host_public_key=&get_hostkey_public_by_type;
3995diff --git a/sshd_config b/sshd_config
3996index 19b7c91a1..2c48105f8 100644
3997--- a/sshd_config
3998+++ b/sshd_config
3999@@ -69,6 +69,8 @@ AuthorizedKeysFile .ssh/authorized_keys
4000 # GSSAPI options
4001 #GSSAPIAuthentication no
4002 #GSSAPICleanupCredentials yes
4003+#GSSAPIStrictAcceptorCheck yes
4004+#GSSAPIKeyExchange no
4005
4006 # Set this to 'yes' to enable PAM authentication, account processing,
4007 # and session processing. If this is enabled, PAM authentication will
4008diff --git a/sshd_config.5 b/sshd_config.5
4009index b224f2929..2baa6622b 100644
4010--- a/sshd_config.5
4011+++ b/sshd_config.5
4012@@ -653,6 +653,11 @@ Specifies whether to automatically destroy the user's credentials cache
4013 on logout.
4014 The default is
4015 .Cm yes .
4016+.It Cm GSSAPIKeyExchange
4017+Specifies whether key exchange based on GSSAPI is allowed. GSSAPI key exchange
4018+doesn't rely on ssh keys to verify host identity.
4019+The default is
4020+.Cm no .
4021 .It Cm GSSAPIStrictAcceptorCheck
4022 Determines whether to be strict about the identity of the GSSAPI acceptor
4023 a client authenticates against.
4024@@ -667,6 +672,31 @@ machine's default store.
4025 This facility is provided to assist with operation on multi homed machines.
4026 The default is
4027 .Cm yes .
4028+.It Cm GSSAPIStoreCredentialsOnRekey
4029+Controls whether the user's GSSAPI credentials should be updated following a
4030+successful connection rekeying. This option can be used to accepted renewed
4031+or updated credentials from a compatible client. The default is
4032+.Dq no .
4033+.Pp
4034+For this to work
4035+.Cm GSSAPIKeyExchange
4036+needs to be enabled in the server and also used by the client.
4037+.It Cm GSSAPIKexAlgorithms
4038+The list of key exchange algorithms that are accepted by GSSAPI
4039+key exchange. Possible values are
4040+.Bd -literal -offset 3n
4041+gss-gex-sha1-,
4042+gss-group1-sha1-,
4043+gss-group14-sha1-,
4044+gss-group14-sha256-,
4045+gss-group16-sha512-,
4046+gss-nistp256-sha256-,
4047+gss-curve25519-sha256-
4048+.Ed
4049+.Pp
4050+The default is
4051+.Dq gss-gex-sha1-,gss-group14-sha1- .
4052+This option only applies to protocol version 2 connections using GSSAPI.
4053 .It Cm HostbasedAcceptedKeyTypes
4054 Specifies the key types that will be accepted for hostbased authentication
4055 as a list of comma-separated patterns.
4056diff --git a/sshkey.c b/sshkey.c
4057index ad1957762..789cd61ef 100644
4058--- a/sshkey.c
4059+++ b/sshkey.c
4060@@ -135,6 +135,7 @@ static const struct keytype keytypes[] = {
4061 # endif /* OPENSSL_HAS_NISTP521 */
4062 # endif /* OPENSSL_HAS_ECC */
4063 #endif /* WITH_OPENSSL */
4064+ { "null", "null", NULL, KEY_NULL, 0, 0, 0 },
4065 { NULL, NULL, NULL, -1, -1, 0, 0 }
4066 };
4067
4068@@ -223,7 +224,7 @@ sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
4069 const struct keytype *kt;
4070
4071 for (kt = keytypes; kt->type != -1; kt++) {
4072- if (kt->name == NULL)
4073+ if (kt->name == NULL || kt->type == KEY_NULL)
4074 continue;
4075 if (!include_sigonly && kt->sigonly)
4076 continue;
4077diff --git a/sshkey.h b/sshkey.h
4078index a91e60436..c11106c93 100644
4079--- a/sshkey.h
4080+++ b/sshkey.h
4081@@ -65,6 +65,7 @@ enum sshkey_types {
4082 KEY_ED25519_CERT,
4083 KEY_XMSS,
4084 KEY_XMSS_CERT,
4085+ KEY_NULL,
4086 KEY_UNSPEC
4087 };
4088