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.patch3026
1 files changed, 3026 insertions, 0 deletions
diff --git a/debian/patches/gssapi.patch b/debian/patches/gssapi.patch
new file mode 100644
index 000000000..fd3b9b630
--- /dev/null
+++ b/debian/patches/gssapi.patch
@@ -0,0 +1,3026 @@
1From 8c27af53099b50387dda97c0aae36194197186f6 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
19Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1242
20Last-Updated: 2016-03-21
21
22Patch-Name: gssapi.patch
23---
24 ChangeLog.gssapi | 113 +++++++++++++++++++
25 Makefile.in | 3 +-
26 auth-krb5.c | 17 ++-
27 auth.c | 3 +-
28 auth2-gss.c | 48 +++++++-
29 auth2.c | 2 +
30 clientloop.c | 15 ++-
31 config.h.in | 6 +
32 configure.ac | 24 ++++
33 gss-genr.c | 275 ++++++++++++++++++++++++++++++++++++++++++++-
34 gss-serv-krb5.c | 85 ++++++++++++--
35 gss-serv.c | 185 +++++++++++++++++++++++++++---
36 kex.c | 16 +++
37 kex.h | 14 +++
38 kexgssc.c | 336 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
39 kexgsss.c | 294 ++++++++++++++++++++++++++++++++++++++++++++++++
40 monitor.c | 108 +++++++++++++++++-
41 monitor.h | 3 +
42 monitor_wrap.c | 47 +++++++-
43 monitor_wrap.h | 4 +-
44 readconf.c | 42 +++++++
45 readconf.h | 5 +
46 servconf.c | 28 ++++-
47 servconf.h | 2 +
48 ssh-gss.h | 41 ++++++-
49 ssh_config | 2 +
50 ssh_config.5 | 32 ++++++
51 sshconnect2.c | 120 +++++++++++++++++++-
52 sshd.c | 110 ++++++++++++++++++
53 sshd_config | 2 +
54 sshd_config.5 | 10 ++
55 sshkey.c | 3 +-
56 sshkey.h | 1 +
57 33 files changed, 1950 insertions(+), 46 deletions(-)
58 create mode 100644 ChangeLog.gssapi
59 create mode 100644 kexgssc.c
60 create mode 100644 kexgsss.c
61
62diff --git a/ChangeLog.gssapi b/ChangeLog.gssapi
63new file mode 100644
64index 0000000..f117a33
65--- /dev/null
66+++ b/ChangeLog.gssapi
67@@ -0,0 +1,113 @@
68+20110101
69+ - Finally update for OpenSSH 5.6p1
70+ - Add GSSAPIServerIdentity option from Jim Basney
71+
72+20100308
73+ - [ Makefile.in, key.c, key.h ]
74+ Updates for OpenSSH 5.4p1
75+ - [ servconf.c ]
76+ Include GSSAPI options in the sshd -T configuration dump, and flag
77+ some older configuration options as being unsupported. Thanks to Colin
78+ Watson.
79+ -
80+
81+20100124
82+ - [ sshconnect2.c ]
83+ Adapt to deal with additional element in Authmethod structure. Thanks to
84+ Colin Watson
85+
86+20090615
87+ - [ gss-genr.c gss-serv.c kexgssc.c kexgsss.c monitor.c sshconnect2.c
88+ sshd.c ]
89+ Fix issues identified by Greg Hudson following a code review
90+ Check return value of gss_indicate_mechs
91+ Protect GSSAPI calls in monitor, so they can only be used if enabled
92+ Check return values of bignum functions in key exchange
93+ Use BN_clear_free to clear other side's DH value
94+ Make ssh_gssapi_id_kex more robust
95+ Only configure kex table pointers if GSSAPI is enabled
96+ Don't leak mechanism list, or gss mechanism list
97+ Cast data.length before printing
98+ If serverkey isn't provided, use an empty string, rather than NULL
99+
100+20090201
101+ - [ gss-genr.c gss-serv.c kex.h kexgssc.c readconf.c readconf.h ssh-gss.h
102+ ssh_config.5 sshconnet2.c ]
103+ Add support for the GSSAPIClientIdentity option, which allows the user
104+ to specify which GSSAPI identity to use to contact a given server
105+
106+20080404
107+ - [ gss-serv.c ]
108+ Add code to actually implement GSSAPIStrictAcceptCheck, which had somehow
109+ been omitted from a previous version of this patch. Reported by Borislav
110+ Stoichkov
111+
112+20070317
113+ - [ gss-serv-krb5.c ]
114+ Remove C99ism, where new_ccname was being declared in the middle of a
115+ function
116+
117+20061220
118+ - [ servconf.c ]
119+ Make default for GSSAPIStrictAcceptorCheck be Yes, to match previous, and
120+ documented, behaviour. Reported by Dan Watson.
121+
122+20060910
123+ - [ gss-genr.c kexgssc.c kexgsss.c kex.h monitor.c sshconnect2.c sshd.c
124+ ssh-gss.h ]
125+ add support for gss-group14-sha1 key exchange mechanisms
126+ - [ gss-serv.c servconf.c servconf.h sshd_config sshd_config.5 ]
127+ Add GSSAPIStrictAcceptorCheck option to allow the disabling of
128+ acceptor principal checking on multi-homed machines.
129+ <Bugzilla #928>
130+ - [ sshd_config ssh_config ]
131+ Add settings for GSSAPIKeyExchange and GSSAPITrustDNS to the sample
132+ configuration files
133+ - [ kexgss.c kegsss.c sshconnect2.c sshd.c ]
134+ Code cleanup. Replace strlen/xmalloc/snprintf sequences with xasprintf()
135+ Limit length of error messages displayed by client
136+
137+20060909
138+ - [ gss-genr.c gss-serv.c ]
139+ move ssh_gssapi_acquire_cred() and ssh_gssapi_server_ctx to be server
140+ only, where they belong
141+ <Bugzilla #1225>
142+
143+20060829
144+ - [ gss-serv-krb5.c ]
145+ Fix CCAPI credentials cache name when creating KRB5CCNAME environment
146+ variable
147+
148+20060828
149+ - [ gss-genr.c ]
150+ Avoid Heimdal context freeing problem
151+ <Fixed upstream 20060829>
152+
153+20060818
154+ - [ gss-genr.c ssh-gss.h sshconnect2.c ]
155+ Make sure that SPENGO is disabled
156+ <Bugzilla #1218 - Fixed upstream 20060818>
157+
158+20060421
159+ - [ gssgenr.c, sshconnect2.c ]
160+ a few type changes (signed versus unsigned, int versus size_t) to
161+ fix compiler errors/warnings
162+ (from jbasney AT ncsa.uiuc.edu)
163+ - [ kexgssc.c, sshconnect2.c ]
164+ fix uninitialized variable warnings
165+ (from jbasney AT ncsa.uiuc.edu)
166+ - [ gssgenr.c ]
167+ pass oid to gss_display_status (helpful when using GSSAPI mechglue)
168+ (from jbasney AT ncsa.uiuc.edu)
169+ <Bugzilla #1220 >
170+ - [ gss-serv-krb5.c ]
171+ #ifdef HAVE_GSSAPI_KRB5 should be #ifdef HAVE_GSSAPI_KRB5_H
172+ (from jbasney AT ncsa.uiuc.edu)
173+ <Fixed upstream 20060304>
174+ - [ readconf.c, readconf.h, ssh_config.5, sshconnect2.c
175+ add client-side GssapiKeyExchange option
176+ (from jbasney AT ncsa.uiuc.edu)
177+ - [ sshconnect2.c ]
178+ add support for GssapiTrustDns option for gssapi-with-mic
179+ (from jbasney AT ncsa.uiuc.edu)
180+ <gssapi-with-mic support is Bugzilla #1008>
181diff --git a/Makefile.in b/Makefile.in
182index d401787..0954c63 100644
183--- a/Makefile.in
184+++ b/Makefile.in
185@@ -92,6 +92,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
186 kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
187 kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
188 kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
189+ kexgssc.o \
190 platform-pledge.o
191
192 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
193@@ -105,7 +106,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o auth-rsa.o auth-rh-rsa.o \
194 auth-skey.o auth-bsdauth.o auth2-hostbased.o auth2-kbdint.o \
195 auth2-none.o auth2-passwd.o auth2-pubkey.o \
196 monitor_mm.o monitor.o monitor_wrap.o auth-krb5.o \
197- auth2-gss.o gss-serv.o gss-serv-krb5.o \
198+ auth2-gss.o gss-serv.o gss-serv-krb5.o kexgsss.o \
199 loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
200 sftp-server.o sftp-common.o \
201 sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
202diff --git a/auth-krb5.c b/auth-krb5.c
203index d1c5a2f..f019fb1 100644
204--- a/auth-krb5.c
205+++ b/auth-krb5.c
206@@ -183,8 +183,13 @@ auth_krb5_password(Authctxt *authctxt, const char *password)
207
208 len = strlen(authctxt->krb5_ticket_file) + 6;
209 authctxt->krb5_ccname = xmalloc(len);
210+#ifdef USE_CCAPI
211+ snprintf(authctxt->krb5_ccname, len, "API:%s",
212+ authctxt->krb5_ticket_file);
213+#else
214 snprintf(authctxt->krb5_ccname, len, "FILE:%s",
215 authctxt->krb5_ticket_file);
216+#endif
217
218 #ifdef USE_PAM
219 if (options.use_pam)
220@@ -241,15 +246,22 @@ krb5_cleanup_proc(Authctxt *authctxt)
221 #ifndef HEIMDAL
222 krb5_error_code
223 ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
224- int tmpfd, ret, oerrno;
225+ int ret, oerrno;
226 char ccname[40];
227 mode_t old_umask;
228+#ifdef USE_CCAPI
229+ char cctemplate[] = "API:krb5cc_%d";
230+#else
231+ char cctemplate[] = "FILE:/tmp/krb5cc_%d_XXXXXXXXXX";
232+ int tmpfd;
233+#endif
234
235 ret = snprintf(ccname, sizeof(ccname),
236- "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
237+ cctemplate, geteuid());
238 if (ret < 0 || (size_t)ret >= sizeof(ccname))
239 return ENOMEM;
240
241+#ifndef USE_CCAPI
242 old_umask = umask(0177);
243 tmpfd = mkstemp(ccname + strlen("FILE:"));
244 oerrno = errno;
245@@ -266,6 +278,7 @@ ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
246 return oerrno;
247 }
248 close(tmpfd);
249+#endif
250
251 return (krb5_cc_resolve(ctx, ccname, ccache));
252 }
253diff --git a/auth.c b/auth.c
254index 214c2c7..bd6a026 100644
255--- a/auth.c
256+++ b/auth.c
257@@ -354,7 +354,8 @@ auth_root_allowed(const char *method)
258 case PERMIT_NO_PASSWD:
259 if (strcmp(method, "publickey") == 0 ||
260 strcmp(method, "hostbased") == 0 ||
261- strcmp(method, "gssapi-with-mic") == 0)
262+ strcmp(method, "gssapi-with-mic") == 0 ||
263+ strcmp(method, "gssapi-keyex") == 0)
264 return 1;
265 break;
266 case PERMIT_FORCED_ONLY:
267diff --git a/auth2-gss.c b/auth2-gss.c
268index 1ca8357..3b5036d 100644
269--- a/auth2-gss.c
270+++ b/auth2-gss.c
271@@ -1,7 +1,7 @@
272 /* $OpenBSD: auth2-gss.c,v 1.22 2015/01/19 20:07:45 markus Exp $ */
273
274 /*
275- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
276+ * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
277 *
278 * Redistribution and use in source and binary forms, with or without
279 * modification, are permitted provided that the following conditions
280@@ -53,6 +53,40 @@ static int input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
281 static int input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
282 static int input_gssapi_errtok(int, u_int32_t, void *);
283
284+/*
285+ * The 'gssapi_keyex' userauth mechanism.
286+ */
287+static int
288+userauth_gsskeyex(Authctxt *authctxt)
289+{
290+ int authenticated = 0;
291+ Buffer b;
292+ gss_buffer_desc mic, gssbuf;
293+ u_int len;
294+
295+ mic.value = packet_get_string(&len);
296+ mic.length = len;
297+
298+ packet_check_eom();
299+
300+ ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
301+ "gssapi-keyex");
302+
303+ gssbuf.value = buffer_ptr(&b);
304+ gssbuf.length = buffer_len(&b);
305+
306+ /* gss_kex_context is NULL with privsep, so we can't check it here */
307+ if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
308+ &gssbuf, &mic))))
309+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
310+ authctxt->pw));
311+
312+ buffer_free(&b);
313+ free(mic.value);
314+
315+ return (authenticated);
316+}
317+
318 /*
319 * We only support those mechanisms that we know about (ie ones that we know
320 * how to check local user kuserok and the like)
321@@ -238,7 +272,8 @@ input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
322
323 packet_check_eom();
324
325- authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
326+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
327+ authctxt->pw));
328
329 authctxt->postponed = 0;
330 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
331@@ -274,7 +309,8 @@ input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
332 gssbuf.length = buffer_len(&b);
333
334 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
335- authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
336+ authenticated =
337+ PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw));
338 else
339 logit("GSSAPI MIC check failed");
340
341@@ -290,6 +326,12 @@ input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
342 return 0;
343 }
344
345+Authmethod method_gsskeyex = {
346+ "gssapi-keyex",
347+ userauth_gsskeyex,
348+ &options.gss_authentication
349+};
350+
351 Authmethod method_gssapi = {
352 "gssapi-with-mic",
353 userauth_gssapi,
354diff --git a/auth2.c b/auth2.c
355index 7177962..3f49bdc 100644
356--- a/auth2.c
357+++ b/auth2.c
358@@ -70,6 +70,7 @@ extern Authmethod method_passwd;
359 extern Authmethod method_kbdint;
360 extern Authmethod method_hostbased;
361 #ifdef GSSAPI
362+extern Authmethod method_gsskeyex;
363 extern Authmethod method_gssapi;
364 #endif
365
366@@ -77,6 +78,7 @@ Authmethod *authmethods[] = {
367 &method_none,
368 &method_pubkey,
369 #ifdef GSSAPI
370+ &method_gsskeyex,
371 &method_gssapi,
372 #endif
373 &method_passwd,
374diff --git a/clientloop.c b/clientloop.c
375index 9820455..1567e4a 100644
376--- a/clientloop.c
377+++ b/clientloop.c
378@@ -114,6 +114,10 @@
379 #include "ssherr.h"
380 #include "hostfile.h"
381
382+#ifdef GSSAPI
383+#include "ssh-gss.h"
384+#endif
385+
386 /* import options */
387 extern Options options;
388
389@@ -1662,9 +1666,18 @@ client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
390 break;
391
392 /* Do channel operations unless rekeying in progress. */
393- if (!ssh_packet_is_rekeying(active_state))
394+ if (!ssh_packet_is_rekeying(active_state)) {
395 channel_after_select(readset, writeset);
396
397+#ifdef GSSAPI
398+ if (options.gss_renewal_rekey &&
399+ ssh_gssapi_credentials_updated(NULL)) {
400+ debug("credentials updated - forcing rekey");
401+ need_rekeying = 1;
402+ }
403+#endif
404+ }
405+
406 /* Buffer input from the connection. */
407 client_process_net_input(readset);
408
409diff --git a/config.h.in b/config.h.in
410index 89bf1b0..621c139 100644
411--- a/config.h.in
412+++ b/config.h.in
413@@ -1641,6 +1641,9 @@
414 /* Use btmp to log bad logins */
415 #undef USE_BTMP
416
417+/* platform uses an in-memory credentials cache */
418+#undef USE_CCAPI
419+
420 /* Use libedit for sftp */
421 #undef USE_LIBEDIT
422
423@@ -1656,6 +1659,9 @@
424 /* Use PIPES instead of a socketpair() */
425 #undef USE_PIPES
426
427+/* platform has the Security Authorization Session API */
428+#undef USE_SECURITY_SESSION_API
429+
430 /* Define if you have Solaris privileges */
431 #undef USE_SOLARIS_PRIVS
432
433diff --git a/configure.ac b/configure.ac
434index 7258cc0..5f1ff74 100644
435--- a/configure.ac
436+++ b/configure.ac
437@@ -632,6 +632,30 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
438 [Use tunnel device compatibility to OpenBSD])
439 AC_DEFINE([SSH_TUN_PREPEND_AF], [1],
440 [Prepend the address family to IP tunnel traffic])
441+ AC_MSG_CHECKING([if we have the Security Authorization Session API])
442+ AC_TRY_COMPILE([#include <Security/AuthSession.h>],
443+ [SessionCreate(0, 0);],
444+ [ac_cv_use_security_session_api="yes"
445+ AC_DEFINE([USE_SECURITY_SESSION_API], [1],
446+ [platform has the Security Authorization Session API])
447+ LIBS="$LIBS -framework Security"
448+ AC_MSG_RESULT([yes])],
449+ [ac_cv_use_security_session_api="no"
450+ AC_MSG_RESULT([no])])
451+ AC_MSG_CHECKING([if we have an in-memory credentials cache])
452+ AC_TRY_COMPILE(
453+ [#include <Kerberos/Kerberos.h>],
454+ [cc_context_t c;
455+ (void) cc_initialize (&c, 0, NULL, NULL);],
456+ [AC_DEFINE([USE_CCAPI], [1],
457+ [platform uses an in-memory credentials cache])
458+ LIBS="$LIBS -framework Security"
459+ AC_MSG_RESULT([yes])
460+ if test "x$ac_cv_use_security_session_api" = "xno"; then
461+ AC_MSG_ERROR([*** Need a security framework to use the credentials cache API ***])
462+ fi],
463+ [AC_MSG_RESULT([no])]
464+ )
465 m4_pattern_allow([AU_IPv])
466 AC_CHECK_DECL([AU_IPv4], [],
467 AC_DEFINE([AU_IPv4], [0], [System only supports IPv4 audit records])
468diff --git a/gss-genr.c b/gss-genr.c
469index d617d60..b4eca3f 100644
470--- a/gss-genr.c
471+++ b/gss-genr.c
472@@ -1,7 +1,7 @@
473 /* $OpenBSD: gss-genr.c,v 1.23 2015/01/20 23:14:00 deraadt Exp $ */
474
475 /*
476- * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
477+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
478 *
479 * Redistribution and use in source and binary forms, with or without
480 * modification, are permitted provided that the following conditions
481@@ -41,12 +41,167 @@
482 #include "buffer.h"
483 #include "log.h"
484 #include "ssh2.h"
485+#include "cipher.h"
486+#include "key.h"
487+#include "kex.h"
488+#include <openssl/evp.h>
489
490 #include "ssh-gss.h"
491
492 extern u_char *session_id2;
493 extern u_int session_id2_len;
494
495+typedef struct {
496+ char *encoded;
497+ gss_OID oid;
498+} ssh_gss_kex_mapping;
499+
500+/*
501+ * XXX - It would be nice to find a more elegant way of handling the
502+ * XXX passing of the key exchange context to the userauth routines
503+ */
504+
505+Gssctxt *gss_kex_context = NULL;
506+
507+static ssh_gss_kex_mapping *gss_enc2oid = NULL;
508+
509+int
510+ssh_gssapi_oid_table_ok(void) {
511+ return (gss_enc2oid != NULL);
512+}
513+
514+/*
515+ * Return a list of the gss-group1-sha1 mechanisms supported by this program
516+ *
517+ * We test mechanisms to ensure that we can use them, to avoid starting
518+ * a key exchange with a bad mechanism
519+ */
520+
521+char *
522+ssh_gssapi_client_mechanisms(const char *host, const char *client) {
523+ gss_OID_set gss_supported;
524+ OM_uint32 min_status;
525+
526+ if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
527+ return NULL;
528+
529+ return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
530+ host, client));
531+}
532+
533+char *
534+ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
535+ const char *host, const char *client) {
536+ Buffer buf;
537+ size_t i;
538+ int oidpos, enclen;
539+ char *mechs, *encoded;
540+ u_char digest[EVP_MAX_MD_SIZE];
541+ char deroid[2];
542+ const EVP_MD *evp_md = EVP_md5();
543+ EVP_MD_CTX md;
544+
545+ if (gss_enc2oid != NULL) {
546+ for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
547+ free(gss_enc2oid[i].encoded);
548+ free(gss_enc2oid);
549+ }
550+
551+ gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
552+ (gss_supported->count + 1));
553+
554+ buffer_init(&buf);
555+
556+ oidpos = 0;
557+ for (i = 0; i < gss_supported->count; i++) {
558+ if (gss_supported->elements[i].length < 128 &&
559+ (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
560+
561+ deroid[0] = SSH_GSS_OIDTYPE;
562+ deroid[1] = gss_supported->elements[i].length;
563+
564+ EVP_DigestInit(&md, evp_md);
565+ EVP_DigestUpdate(&md, deroid, 2);
566+ EVP_DigestUpdate(&md,
567+ gss_supported->elements[i].elements,
568+ gss_supported->elements[i].length);
569+ EVP_DigestFinal(&md, digest, NULL);
570+
571+ encoded = xmalloc(EVP_MD_size(evp_md) * 2);
572+ enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
573+ encoded, EVP_MD_size(evp_md) * 2);
574+
575+ if (oidpos != 0)
576+ buffer_put_char(&buf, ',');
577+
578+ buffer_append(&buf, KEX_GSS_GEX_SHA1_ID,
579+ sizeof(KEX_GSS_GEX_SHA1_ID) - 1);
580+ buffer_append(&buf, encoded, enclen);
581+ buffer_put_char(&buf, ',');
582+ buffer_append(&buf, KEX_GSS_GRP1_SHA1_ID,
583+ sizeof(KEX_GSS_GRP1_SHA1_ID) - 1);
584+ buffer_append(&buf, encoded, enclen);
585+ buffer_put_char(&buf, ',');
586+ buffer_append(&buf, KEX_GSS_GRP14_SHA1_ID,
587+ sizeof(KEX_GSS_GRP14_SHA1_ID) - 1);
588+ buffer_append(&buf, encoded, enclen);
589+
590+ gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
591+ gss_enc2oid[oidpos].encoded = encoded;
592+ oidpos++;
593+ }
594+ }
595+ gss_enc2oid[oidpos].oid = NULL;
596+ gss_enc2oid[oidpos].encoded = NULL;
597+
598+ buffer_put_char(&buf, '\0');
599+
600+ mechs = xmalloc(buffer_len(&buf));
601+ buffer_get(&buf, mechs, buffer_len(&buf));
602+ buffer_free(&buf);
603+
604+ if (strlen(mechs) == 0) {
605+ free(mechs);
606+ mechs = NULL;
607+ }
608+
609+ return (mechs);
610+}
611+
612+gss_OID
613+ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
614+ int i = 0;
615+
616+ switch (kex_type) {
617+ case KEX_GSS_GRP1_SHA1:
618+ if (strlen(name) < sizeof(KEX_GSS_GRP1_SHA1_ID))
619+ return GSS_C_NO_OID;
620+ name += sizeof(KEX_GSS_GRP1_SHA1_ID) - 1;
621+ break;
622+ case KEX_GSS_GRP14_SHA1:
623+ if (strlen(name) < sizeof(KEX_GSS_GRP14_SHA1_ID))
624+ return GSS_C_NO_OID;
625+ name += sizeof(KEX_GSS_GRP14_SHA1_ID) - 1;
626+ break;
627+ case KEX_GSS_GEX_SHA1:
628+ if (strlen(name) < sizeof(KEX_GSS_GEX_SHA1_ID))
629+ return GSS_C_NO_OID;
630+ name += sizeof(KEX_GSS_GEX_SHA1_ID) - 1;
631+ break;
632+ default:
633+ return GSS_C_NO_OID;
634+ }
635+
636+ while (gss_enc2oid[i].encoded != NULL &&
637+ strcmp(name, gss_enc2oid[i].encoded) != 0)
638+ i++;
639+
640+ if (gss_enc2oid[i].oid != NULL && ctx != NULL)
641+ ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
642+
643+ return gss_enc2oid[i].oid;
644+}
645+
646 /* Check that the OID in a data stream matches that in the context */
647 int
648 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
649@@ -199,7 +354,7 @@ ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
650 }
651
652 ctx->major = gss_init_sec_context(&ctx->minor,
653- GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
654+ ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
655 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
656 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
657
658@@ -229,8 +384,42 @@ ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
659 }
660
661 OM_uint32
662+ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
663+{
664+ gss_buffer_desc gssbuf;
665+ gss_name_t gssname;
666+ OM_uint32 status;
667+ gss_OID_set oidset;
668+
669+ gssbuf.value = (void *) name;
670+ gssbuf.length = strlen(gssbuf.value);
671+
672+ gss_create_empty_oid_set(&status, &oidset);
673+ gss_add_oid_set_member(&status, ctx->oid, &oidset);
674+
675+ ctx->major = gss_import_name(&ctx->minor, &gssbuf,
676+ GSS_C_NT_USER_NAME, &gssname);
677+
678+ if (!ctx->major)
679+ ctx->major = gss_acquire_cred(&ctx->minor,
680+ gssname, 0, oidset, GSS_C_INITIATE,
681+ &ctx->client_creds, NULL, NULL);
682+
683+ gss_release_name(&status, &gssname);
684+ gss_release_oid_set(&status, &oidset);
685+
686+ if (ctx->major)
687+ ssh_gssapi_error(ctx);
688+
689+ return(ctx->major);
690+}
691+
692+OM_uint32
693 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
694 {
695+ if (ctx == NULL)
696+ return -1;
697+
698 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
699 GSS_C_QOP_DEFAULT, buffer, hash)))
700 ssh_gssapi_error(ctx);
701@@ -238,6 +427,19 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
702 return (ctx->major);
703 }
704
705+/* Priviledged when used by server */
706+OM_uint32
707+ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
708+{
709+ if (ctx == NULL)
710+ return -1;
711+
712+ ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
713+ gssbuf, gssmic, NULL);
714+
715+ return (ctx->major);
716+}
717+
718 void
719 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
720 const char *context)
721@@ -251,11 +453,16 @@ ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
722 }
723
724 int
725-ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
726+ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host,
727+ const char *client)
728 {
729 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
730 OM_uint32 major, minor;
731 gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
732+ Gssctxt *intctx = NULL;
733+
734+ if (ctx == NULL)
735+ ctx = &intctx;
736
737 /* RFC 4462 says we MUST NOT do SPNEGO */
738 if (oid->length == spnego_oid.length &&
739@@ -265,6 +472,10 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
740 ssh_gssapi_build_ctx(ctx);
741 ssh_gssapi_set_oid(*ctx, oid);
742 major = ssh_gssapi_import_name(*ctx, host);
743+
744+ if (!GSS_ERROR(major) && client)
745+ major = ssh_gssapi_client_identity(*ctx, client);
746+
747 if (!GSS_ERROR(major)) {
748 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
749 NULL);
750@@ -274,10 +485,66 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
751 GSS_C_NO_BUFFER);
752 }
753
754- if (GSS_ERROR(major))
755+ if (GSS_ERROR(major) || intctx != NULL)
756 ssh_gssapi_delete_ctx(ctx);
757
758 return (!GSS_ERROR(major));
759 }
760
761+int
762+ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
763+ static gss_name_t saved_name = GSS_C_NO_NAME;
764+ static OM_uint32 saved_lifetime = 0;
765+ static gss_OID saved_mech = GSS_C_NO_OID;
766+ static gss_name_t name;
767+ static OM_uint32 last_call = 0;
768+ OM_uint32 lifetime, now, major, minor;
769+ int equal;
770+
771+ now = time(NULL);
772+
773+ if (ctxt) {
774+ debug("Rekey has happened - updating saved versions");
775+
776+ if (saved_name != GSS_C_NO_NAME)
777+ gss_release_name(&minor, &saved_name);
778+
779+ major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
780+ &saved_name, &saved_lifetime, NULL, NULL);
781+
782+ if (!GSS_ERROR(major)) {
783+ saved_mech = ctxt->oid;
784+ saved_lifetime+= now;
785+ } else {
786+ /* Handle the error */
787+ }
788+ return 0;
789+ }
790+
791+ if (now - last_call < 10)
792+ return 0;
793+
794+ last_call = now;
795+
796+ if (saved_mech == GSS_C_NO_OID)
797+ return 0;
798+
799+ major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
800+ &name, &lifetime, NULL, NULL);
801+ if (major == GSS_S_CREDENTIALS_EXPIRED)
802+ return 0;
803+ else if (GSS_ERROR(major))
804+ return 0;
805+
806+ major = gss_compare_name(&minor, saved_name, name, &equal);
807+ gss_release_name(&minor, &name);
808+ if (GSS_ERROR(major))
809+ return 0;
810+
811+ if (equal && (saved_lifetime < lifetime + now - 10))
812+ return 1;
813+
814+ return 0;
815+}
816+
817 #endif /* GSSAPI */
818diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c
819index 795992d..fd8b371 100644
820--- a/gss-serv-krb5.c
821+++ b/gss-serv-krb5.c
822@@ -1,7 +1,7 @@
823 /* $OpenBSD: gss-serv-krb5.c,v 1.8 2013/07/20 01:55:13 djm Exp $ */
824
825 /*
826- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
827+ * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
828 *
829 * Redistribution and use in source and binary forms, with or without
830 * modification, are permitted provided that the following conditions
831@@ -121,8 +121,8 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
832 krb5_error_code problem;
833 krb5_principal princ;
834 OM_uint32 maj_status, min_status;
835- int len;
836 const char *errmsg;
837+ const char *new_ccname;
838
839 if (client->creds == NULL) {
840 debug("No credentials stored");
841@@ -181,11 +181,16 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
842 return;
843 }
844
845- client->store.filename = xstrdup(krb5_cc_get_name(krb_context, ccache));
846+ new_ccname = krb5_cc_get_name(krb_context, ccache);
847+
848 client->store.envvar = "KRB5CCNAME";
849- len = strlen(client->store.filename) + 6;
850- client->store.envval = xmalloc(len);
851- snprintf(client->store.envval, len, "FILE:%s", client->store.filename);
852+#ifdef USE_CCAPI
853+ xasprintf(&client->store.envval, "API:%s", new_ccname);
854+ client->store.filename = NULL;
855+#else
856+ xasprintf(&client->store.envval, "FILE:%s", new_ccname);
857+ client->store.filename = xstrdup(new_ccname);
858+#endif
859
860 #ifdef USE_PAM
861 if (options.use_pam)
862@@ -197,6 +202,71 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
863 return;
864 }
865
866+int
867+ssh_gssapi_krb5_updatecreds(ssh_gssapi_ccache *store,
868+ ssh_gssapi_client *client)
869+{
870+ krb5_ccache ccache = NULL;
871+ krb5_principal principal = NULL;
872+ char *name = NULL;
873+ krb5_error_code problem;
874+ OM_uint32 maj_status, min_status;
875+
876+ if ((problem = krb5_cc_resolve(krb_context, store->envval, &ccache))) {
877+ logit("krb5_cc_resolve(): %.100s",
878+ krb5_get_err_text(krb_context, problem));
879+ return 0;
880+ }
881+
882+ /* Find out who the principal in this cache is */
883+ if ((problem = krb5_cc_get_principal(krb_context, ccache,
884+ &principal))) {
885+ logit("krb5_cc_get_principal(): %.100s",
886+ krb5_get_err_text(krb_context, problem));
887+ krb5_cc_close(krb_context, ccache);
888+ return 0;
889+ }
890+
891+ if ((problem = krb5_unparse_name(krb_context, principal, &name))) {
892+ logit("krb5_unparse_name(): %.100s",
893+ krb5_get_err_text(krb_context, problem));
894+ krb5_free_principal(krb_context, principal);
895+ krb5_cc_close(krb_context, ccache);
896+ return 0;
897+ }
898+
899+
900+ if (strcmp(name,client->exportedname.value)!=0) {
901+ debug("Name in local credentials cache differs. Not storing");
902+ krb5_free_principal(krb_context, principal);
903+ krb5_cc_close(krb_context, ccache);
904+ krb5_free_unparsed_name(krb_context, name);
905+ return 0;
906+ }
907+ krb5_free_unparsed_name(krb_context, name);
908+
909+ /* Name matches, so lets get on with it! */
910+
911+ if ((problem = krb5_cc_initialize(krb_context, ccache, principal))) {
912+ logit("krb5_cc_initialize(): %.100s",
913+ krb5_get_err_text(krb_context, problem));
914+ krb5_free_principal(krb_context, principal);
915+ krb5_cc_close(krb_context, ccache);
916+ return 0;
917+ }
918+
919+ krb5_free_principal(krb_context, principal);
920+
921+ if ((maj_status = gss_krb5_copy_ccache(&min_status, client->creds,
922+ ccache))) {
923+ logit("gss_krb5_copy_ccache() failed. Sorry!");
924+ krb5_cc_close(krb_context, ccache);
925+ return 0;
926+ }
927+
928+ return 1;
929+}
930+
931 ssh_gssapi_mech gssapi_kerberos_mech = {
932 "toWM5Slw5Ew8Mqkay+al2g==",
933 "Kerberos",
934@@ -204,7 +274,8 @@ ssh_gssapi_mech gssapi_kerberos_mech = {
935 NULL,
936 &ssh_gssapi_krb5_userok,
937 NULL,
938- &ssh_gssapi_krb5_storecreds
939+ &ssh_gssapi_krb5_storecreds,
940+ &ssh_gssapi_krb5_updatecreds
941 };
942
943 #endif /* KRB5 */
944diff --git a/gss-serv.c b/gss-serv.c
945index 53993d6..2f6baf7 100644
946--- a/gss-serv.c
947+++ b/gss-serv.c
948@@ -1,7 +1,7 @@
949 /* $OpenBSD: gss-serv.c,v 1.29 2015/05/22 03:50:02 djm Exp $ */
950
951 /*
952- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
953+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
954 *
955 * Redistribution and use in source and binary forms, with or without
956 * modification, are permitted provided that the following conditions
957@@ -45,17 +45,22 @@
958 #include "session.h"
959 #include "misc.h"
960 #include "servconf.h"
961+#include "uidswap.h"
962
963 #include "ssh-gss.h"
964+#include "monitor_wrap.h"
965+
966+extern ServerOptions options;
967
968 extern ServerOptions options;
969
970 static ssh_gssapi_client gssapi_client =
971 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
972- GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
973+ GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, NULL,
974+ {NULL, NULL, NULL, NULL, NULL}, 0, 0};
975
976 ssh_gssapi_mech gssapi_null_mech =
977- { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
978+ { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
979
980 #ifdef KRB5
981 extern ssh_gssapi_mech gssapi_kerberos_mech;
982@@ -142,6 +147,29 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
983 }
984
985 /* Unprivileged */
986+char *
987+ssh_gssapi_server_mechanisms(void) {
988+ gss_OID_set supported;
989+
990+ ssh_gssapi_supported_oids(&supported);
991+ return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
992+ NULL, NULL));
993+}
994+
995+/* Unprivileged */
996+int
997+ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
998+ const char *dummy) {
999+ Gssctxt *ctx = NULL;
1000+ int res;
1001+
1002+ res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
1003+ ssh_gssapi_delete_ctx(&ctx);
1004+
1005+ return (res);
1006+}
1007+
1008+/* Unprivileged */
1009 void
1010 ssh_gssapi_supported_oids(gss_OID_set *oidset)
1011 {
1012@@ -151,7 +179,9 @@ ssh_gssapi_supported_oids(gss_OID_set *oidset)
1013 gss_OID_set supported;
1014
1015 gss_create_empty_oid_set(&min_status, oidset);
1016- gss_indicate_mechs(&min_status, &supported);
1017+
1018+ if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
1019+ return;
1020
1021 while (supported_mechs[i]->name != NULL) {
1022 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
1023@@ -277,8 +307,48 @@ OM_uint32
1024 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1025 {
1026 int i = 0;
1027+ int equal = 0;
1028+ gss_name_t new_name = GSS_C_NO_NAME;
1029+ gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
1030+
1031+ if (options.gss_store_rekey && client->used && ctx->client_creds) {
1032+ if (client->mech->oid.length != ctx->oid->length ||
1033+ (memcmp(client->mech->oid.elements,
1034+ ctx->oid->elements, ctx->oid->length) !=0)) {
1035+ debug("Rekeyed credentials have different mechanism");
1036+ return GSS_S_COMPLETE;
1037+ }
1038+
1039+ if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1040+ ctx->client_creds, ctx->oid, &new_name,
1041+ NULL, NULL, NULL))) {
1042+ ssh_gssapi_error(ctx);
1043+ return (ctx->major);
1044+ }
1045+
1046+ ctx->major = gss_compare_name(&ctx->minor, client->name,
1047+ new_name, &equal);
1048+
1049+ if (GSS_ERROR(ctx->major)) {
1050+ ssh_gssapi_error(ctx);
1051+ return (ctx->major);
1052+ }
1053+
1054+ if (!equal) {
1055+ debug("Rekeyed credentials have different name");
1056+ return GSS_S_COMPLETE;
1057+ }
1058
1059- gss_buffer_desc ename;
1060+ debug("Marking rekeyed credentials for export");
1061+
1062+ gss_release_name(&ctx->minor, &client->name);
1063+ gss_release_cred(&ctx->minor, &client->creds);
1064+ client->name = new_name;
1065+ client->creds = ctx->client_creds;
1066+ ctx->client_creds = GSS_C_NO_CREDENTIAL;
1067+ client->updated = 1;
1068+ return GSS_S_COMPLETE;
1069+ }
1070
1071 client->mech = NULL;
1072
1073@@ -293,6 +363,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1074 if (client->mech == NULL)
1075 return GSS_S_FAILURE;
1076
1077+ if (ctx->client_creds &&
1078+ (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1079+ ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
1080+ ssh_gssapi_error(ctx);
1081+ return (ctx->major);
1082+ }
1083+
1084 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
1085 &client->displayname, NULL))) {
1086 ssh_gssapi_error(ctx);
1087@@ -310,6 +387,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1088 return (ctx->major);
1089 }
1090
1091+ gss_release_buffer(&ctx->minor, &ename);
1092+
1093 /* We can't copy this structure, so we just move the pointer to it */
1094 client->creds = ctx->client_creds;
1095 ctx->client_creds = GSS_C_NO_CREDENTIAL;
1096@@ -357,7 +436,7 @@ ssh_gssapi_do_child(char ***envp, u_int *envsizep)
1097
1098 /* Privileged */
1099 int
1100-ssh_gssapi_userok(char *user)
1101+ssh_gssapi_userok(char *user, struct passwd *pw)
1102 {
1103 OM_uint32 lmin;
1104
1105@@ -367,9 +446,11 @@ ssh_gssapi_userok(char *user)
1106 return 0;
1107 }
1108 if (gssapi_client.mech && gssapi_client.mech->userok)
1109- if ((*gssapi_client.mech->userok)(&gssapi_client, user))
1110+ if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
1111+ gssapi_client.used = 1;
1112+ gssapi_client.store.owner = pw;
1113 return 1;
1114- else {
1115+ } else {
1116 /* Destroy delegated credentials if userok fails */
1117 gss_release_buffer(&lmin, &gssapi_client.displayname);
1118 gss_release_buffer(&lmin, &gssapi_client.exportedname);
1119@@ -383,14 +464,90 @@ ssh_gssapi_userok(char *user)
1120 return (0);
1121 }
1122
1123-/* Privileged */
1124-OM_uint32
1125-ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1126+/* These bits are only used for rekeying. The unpriviledged child is running
1127+ * as the user, the monitor is root.
1128+ *
1129+ * In the child, we want to :
1130+ * *) Ask the monitor to store our credentials into the store we specify
1131+ * *) If it succeeds, maybe do a PAM update
1132+ */
1133+
1134+/* Stuff for PAM */
1135+
1136+#ifdef USE_PAM
1137+static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg,
1138+ struct pam_response **resp, void *data)
1139 {
1140- ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
1141- gssbuf, gssmic, NULL);
1142+ return (PAM_CONV_ERR);
1143+}
1144+#endif
1145
1146- return (ctx->major);
1147+void
1148+ssh_gssapi_rekey_creds(void) {
1149+ int ok;
1150+ int ret;
1151+#ifdef USE_PAM
1152+ pam_handle_t *pamh = NULL;
1153+ struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
1154+ char *envstr;
1155+#endif
1156+
1157+ if (gssapi_client.store.filename == NULL &&
1158+ gssapi_client.store.envval == NULL &&
1159+ gssapi_client.store.envvar == NULL)
1160+ return;
1161+
1162+ ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
1163+
1164+ if (!ok)
1165+ return;
1166+
1167+ debug("Rekeyed credentials stored successfully");
1168+
1169+ /* Actually managing to play with the ssh pam stack from here will
1170+ * be next to impossible. In any case, we may want different options
1171+ * for rekeying. So, use our own :)
1172+ */
1173+#ifdef USE_PAM
1174+ if (!use_privsep) {
1175+ debug("Not even going to try and do PAM with privsep disabled");
1176+ return;
1177+ }
1178+
1179+ ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
1180+ &pamconv, &pamh);
1181+ if (ret)
1182+ return;
1183+
1184+ xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar,
1185+ gssapi_client.store.envval);
1186+
1187+ ret = pam_putenv(pamh, envstr);
1188+ if (!ret)
1189+ pam_setcred(pamh, PAM_REINITIALIZE_CRED);
1190+ pam_end(pamh, PAM_SUCCESS);
1191+#endif
1192+}
1193+
1194+int
1195+ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
1196+ int ok = 0;
1197+
1198+ /* Check we've got credentials to store */
1199+ if (!gssapi_client.updated)
1200+ return 0;
1201+
1202+ gssapi_client.updated = 0;
1203+
1204+ temporarily_use_uid(gssapi_client.store.owner);
1205+ if (gssapi_client.mech && gssapi_client.mech->updatecreds)
1206+ ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
1207+ else
1208+ debug("No update function for this mechanism");
1209+
1210+ restore_uid();
1211+
1212+ return ok;
1213 }
1214
1215 #endif
1216diff --git a/kex.c b/kex.c
1217index d371f47..913e923 100644
1218--- a/kex.c
1219+++ b/kex.c
1220@@ -54,6 +54,10 @@
1221 #include "sshbuf.h"
1222 #include "digest.h"
1223
1224+#ifdef GSSAPI
1225+#include "ssh-gss.h"
1226+#endif
1227+
1228 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
1229 # if defined(HAVE_EVP_SHA256)
1230 # define evp_ssh_sha256 EVP_sha256
1231@@ -109,6 +113,14 @@ static const struct kexalg kexalgs[] = {
1232 #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
1233 { NULL, -1, -1, -1},
1234 };
1235+static const struct kexalg kexalg_prefixes[] = {
1236+#ifdef GSSAPI
1237+ { KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
1238+ { KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
1239+ { KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
1240+#endif
1241+ { NULL, -1, -1, -1 },
1242+};
1243
1244 char *
1245 kex_alg_list(char sep)
1246@@ -141,6 +153,10 @@ kex_alg_by_name(const char *name)
1247 if (strcmp(k->name, name) == 0)
1248 return k;
1249 }
1250+ for (k = kexalg_prefixes; k->name != NULL; k++) {
1251+ if (strncmp(k->name, name, strlen(k->name)) == 0)
1252+ return k;
1253+ }
1254 return NULL;
1255 }
1256
1257diff --git a/kex.h b/kex.h
1258index 1c58966..123ef83 100644
1259--- a/kex.h
1260+++ b/kex.h
1261@@ -92,6 +92,9 @@ enum kex_exchange {
1262 KEX_DH_GEX_SHA256,
1263 KEX_ECDH_SHA2,
1264 KEX_C25519_SHA256,
1265+ KEX_GSS_GRP1_SHA1,
1266+ KEX_GSS_GRP14_SHA1,
1267+ KEX_GSS_GEX_SHA1,
1268 KEX_MAX
1269 };
1270
1271@@ -140,6 +143,12 @@ struct kex {
1272 u_int flags;
1273 int hash_alg;
1274 int ec_nid;
1275+#ifdef GSSAPI
1276+ int gss_deleg_creds;
1277+ int gss_trust_dns;
1278+ char *gss_host;
1279+ char *gss_client;
1280+#endif
1281 char *client_version_string;
1282 char *server_version_string;
1283 char *failed_choice;
1284@@ -190,6 +199,11 @@ int kexecdh_server(struct ssh *);
1285 int kexc25519_client(struct ssh *);
1286 int kexc25519_server(struct ssh *);
1287
1288+#ifdef GSSAPI
1289+int kexgss_client(struct ssh *);
1290+int kexgss_server(struct ssh *);
1291+#endif
1292+
1293 int kex_dh_hash(const char *, const char *,
1294 const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
1295 const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
1296diff --git a/kexgssc.c b/kexgssc.c
1297new file mode 100644
1298index 0000000..a49bac2
1299--- /dev/null
1300+++ b/kexgssc.c
1301@@ -0,0 +1,336 @@
1302+/*
1303+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1304+ *
1305+ * Redistribution and use in source and binary forms, with or without
1306+ * modification, are permitted provided that the following conditions
1307+ * are met:
1308+ * 1. Redistributions of source code must retain the above copyright
1309+ * notice, this list of conditions and the following disclaimer.
1310+ * 2. Redistributions in binary form must reproduce the above copyright
1311+ * notice, this list of conditions and the following disclaimer in the
1312+ * documentation and/or other materials provided with the distribution.
1313+ *
1314+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
1315+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1316+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1317+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1318+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1319+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1320+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1321+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1322+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1323+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1324+ */
1325+
1326+#include "includes.h"
1327+
1328+#ifdef GSSAPI
1329+
1330+#include "includes.h"
1331+
1332+#include <openssl/crypto.h>
1333+#include <openssl/bn.h>
1334+
1335+#include <string.h>
1336+
1337+#include "xmalloc.h"
1338+#include "buffer.h"
1339+#include "ssh2.h"
1340+#include "key.h"
1341+#include "cipher.h"
1342+#include "kex.h"
1343+#include "log.h"
1344+#include "packet.h"
1345+#include "dh.h"
1346+#include "digest.h"
1347+
1348+#include "ssh-gss.h"
1349+
1350+int
1351+kexgss_client(struct ssh *ssh) {
1352+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1353+ gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
1354+ Gssctxt *ctxt;
1355+ OM_uint32 maj_status, min_status, ret_flags;
1356+ u_int klen, kout, slen = 0, strlen;
1357+ DH *dh;
1358+ BIGNUM *dh_server_pub = NULL;
1359+ BIGNUM *shared_secret = NULL;
1360+ BIGNUM *p = NULL;
1361+ BIGNUM *g = NULL;
1362+ u_char *kbuf;
1363+ u_char *serverhostkey = NULL;
1364+ u_char *empty = "";
1365+ char *msg;
1366+ int type = 0;
1367+ int first = 1;
1368+ int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
1369+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1370+ size_t hashlen;
1371+
1372+ /* Initialise our GSSAPI world */
1373+ ssh_gssapi_build_ctx(&ctxt);
1374+ if (ssh_gssapi_id_kex(ctxt, ssh->kex->name, ssh->kex->kex_type)
1375+ == GSS_C_NO_OID)
1376+ fatal("Couldn't identify host exchange");
1377+
1378+ if (ssh_gssapi_import_name(ctxt, ssh->kex->gss_host))
1379+ fatal("Couldn't import hostname");
1380+
1381+ if (ssh->kex->gss_client &&
1382+ ssh_gssapi_client_identity(ctxt, ssh->kex->gss_client))
1383+ fatal("Couldn't acquire client credentials");
1384+
1385+ switch (ssh->kex->kex_type) {
1386+ case KEX_GSS_GRP1_SHA1:
1387+ dh = dh_new_group1();
1388+ break;
1389+ case KEX_GSS_GRP14_SHA1:
1390+ dh = dh_new_group14();
1391+ break;
1392+ case KEX_GSS_GEX_SHA1:
1393+ debug("Doing group exchange\n");
1394+ nbits = dh_estimate(ssh->kex->we_need * 8);
1395+ packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
1396+ packet_put_int(min);
1397+ packet_put_int(nbits);
1398+ packet_put_int(max);
1399+
1400+ packet_send();
1401+
1402+ packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
1403+
1404+ if ((p = BN_new()) == NULL)
1405+ fatal("BN_new() failed");
1406+ packet_get_bignum2(p);
1407+ if ((g = BN_new()) == NULL)
1408+ fatal("BN_new() failed");
1409+ packet_get_bignum2(g);
1410+ packet_check_eom();
1411+
1412+ if (BN_num_bits(p) < min || BN_num_bits(p) > max)
1413+ fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
1414+ min, BN_num_bits(p), max);
1415+
1416+ dh = dh_new_group(g, p);
1417+ break;
1418+ default:
1419+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1420+ }
1421+
1422+ /* Step 1 - e is dh->pub_key */
1423+ dh_gen_key(dh, ssh->kex->we_need * 8);
1424+
1425+ /* This is f, we initialise it now to make life easier */
1426+ dh_server_pub = BN_new();
1427+ if (dh_server_pub == NULL)
1428+ fatal("dh_server_pub == NULL");
1429+
1430+ token_ptr = GSS_C_NO_BUFFER;
1431+
1432+ do {
1433+ debug("Calling gss_init_sec_context");
1434+
1435+ maj_status = ssh_gssapi_init_ctx(ctxt,
1436+ ssh->kex->gss_deleg_creds, token_ptr, &send_tok,
1437+ &ret_flags);
1438+
1439+ if (GSS_ERROR(maj_status)) {
1440+ if (send_tok.length != 0) {
1441+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1442+ packet_put_string(send_tok.value,
1443+ send_tok.length);
1444+ }
1445+ fatal("gss_init_context failed");
1446+ }
1447+
1448+ /* If we've got an old receive buffer get rid of it */
1449+ if (token_ptr != GSS_C_NO_BUFFER)
1450+ free(recv_tok.value);
1451+
1452+ if (maj_status == GSS_S_COMPLETE) {
1453+ /* If mutual state flag is not true, kex fails */
1454+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1455+ fatal("Mutual authentication failed");
1456+
1457+ /* If integ avail flag is not true kex fails */
1458+ if (!(ret_flags & GSS_C_INTEG_FLAG))
1459+ fatal("Integrity check failed");
1460+ }
1461+
1462+ /*
1463+ * If we have data to send, then the last message that we
1464+ * received cannot have been a 'complete'.
1465+ */
1466+ if (send_tok.length != 0) {
1467+ if (first) {
1468+ packet_start(SSH2_MSG_KEXGSS_INIT);
1469+ packet_put_string(send_tok.value,
1470+ send_tok.length);
1471+ packet_put_bignum2(dh->pub_key);
1472+ first = 0;
1473+ } else {
1474+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1475+ packet_put_string(send_tok.value,
1476+ send_tok.length);
1477+ }
1478+ packet_send();
1479+ gss_release_buffer(&min_status, &send_tok);
1480+
1481+ /* If we've sent them data, they should reply */
1482+ do {
1483+ type = packet_read();
1484+ if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
1485+ debug("Received KEXGSS_HOSTKEY");
1486+ if (serverhostkey)
1487+ fatal("Server host key received more than once");
1488+ serverhostkey =
1489+ packet_get_string(&slen);
1490+ }
1491+ } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
1492+
1493+ switch (type) {
1494+ case SSH2_MSG_KEXGSS_CONTINUE:
1495+ debug("Received GSSAPI_CONTINUE");
1496+ if (maj_status == GSS_S_COMPLETE)
1497+ fatal("GSSAPI Continue received from server when complete");
1498+ recv_tok.value = packet_get_string(&strlen);
1499+ recv_tok.length = strlen;
1500+ break;
1501+ case SSH2_MSG_KEXGSS_COMPLETE:
1502+ debug("Received GSSAPI_COMPLETE");
1503+ packet_get_bignum2(dh_server_pub);
1504+ msg_tok.value = packet_get_string(&strlen);
1505+ msg_tok.length = strlen;
1506+
1507+ /* Is there a token included? */
1508+ if (packet_get_char()) {
1509+ recv_tok.value=
1510+ packet_get_string(&strlen);
1511+ recv_tok.length = strlen;
1512+ /* If we're already complete - protocol error */
1513+ if (maj_status == GSS_S_COMPLETE)
1514+ packet_disconnect("Protocol error: received token when complete");
1515+ } else {
1516+ /* No token included */
1517+ if (maj_status != GSS_S_COMPLETE)
1518+ packet_disconnect("Protocol error: did not receive final token");
1519+ }
1520+ break;
1521+ case SSH2_MSG_KEXGSS_ERROR:
1522+ debug("Received Error");
1523+ maj_status = packet_get_int();
1524+ min_status = packet_get_int();
1525+ msg = packet_get_string(NULL);
1526+ (void) packet_get_string_ptr(NULL);
1527+ fatal("GSSAPI Error: \n%.400s",msg);
1528+ default:
1529+ packet_disconnect("Protocol error: didn't expect packet type %d",
1530+ type);
1531+ }
1532+ token_ptr = &recv_tok;
1533+ } else {
1534+ /* No data, and not complete */
1535+ if (maj_status != GSS_S_COMPLETE)
1536+ fatal("Not complete, and no token output");
1537+ }
1538+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
1539+
1540+ /*
1541+ * We _must_ have received a COMPLETE message in reply from the
1542+ * server, which will have set dh_server_pub and msg_tok
1543+ */
1544+
1545+ if (type != SSH2_MSG_KEXGSS_COMPLETE)
1546+ fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
1547+
1548+ /* Check f in range [1, p-1] */
1549+ if (!dh_pub_is_valid(dh, dh_server_pub))
1550+ packet_disconnect("bad server public DH value");
1551+
1552+ /* compute K=f^x mod p */
1553+ klen = DH_size(dh);
1554+ kbuf = xmalloc(klen);
1555+ kout = DH_compute_key(kbuf, dh_server_pub, dh);
1556+ if (kout < 0)
1557+ fatal("DH_compute_key: failed");
1558+
1559+ shared_secret = BN_new();
1560+ if (shared_secret == NULL)
1561+ fatal("kexgss_client: BN_new failed");
1562+
1563+ if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
1564+ fatal("kexdh_client: BN_bin2bn failed");
1565+
1566+ memset(kbuf, 0, klen);
1567+ free(kbuf);
1568+
1569+ hashlen = sizeof(hash);
1570+ switch (ssh->kex->kex_type) {
1571+ case KEX_GSS_GRP1_SHA1:
1572+ case KEX_GSS_GRP14_SHA1:
1573+ kex_dh_hash( ssh->kex->client_version_string,
1574+ ssh->kex->server_version_string,
1575+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1576+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1577+ (serverhostkey ? serverhostkey : empty), slen,
1578+ dh->pub_key, /* e */
1579+ dh_server_pub, /* f */
1580+ shared_secret, /* K */
1581+ hash, &hashlen
1582+ );
1583+ break;
1584+ case KEX_GSS_GEX_SHA1:
1585+ kexgex_hash(
1586+ ssh->kex->hash_alg,
1587+ ssh->kex->client_version_string,
1588+ ssh->kex->server_version_string,
1589+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1590+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1591+ (serverhostkey ? serverhostkey : empty), slen,
1592+ min, nbits, max,
1593+ dh->p, dh->g,
1594+ dh->pub_key,
1595+ dh_server_pub,
1596+ shared_secret,
1597+ hash, &hashlen
1598+ );
1599+ break;
1600+ default:
1601+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1602+ }
1603+
1604+ gssbuf.value = hash;
1605+ gssbuf.length = hashlen;
1606+
1607+ /* Verify that the hash matches the MIC we just got. */
1608+ if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
1609+ packet_disconnect("Hash's MIC didn't verify");
1610+
1611+ free(msg_tok.value);
1612+
1613+ DH_free(dh);
1614+ free(serverhostkey);
1615+ BN_clear_free(dh_server_pub);
1616+
1617+ /* save session id */
1618+ if (ssh->kex->session_id == NULL) {
1619+ ssh->kex->session_id_len = hashlen;
1620+ ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
1621+ memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
1622+ }
1623+
1624+ if (ssh->kex->gss_deleg_creds)
1625+ ssh_gssapi_credentials_updated(ctxt);
1626+
1627+ if (gss_kex_context == NULL)
1628+ gss_kex_context = ctxt;
1629+ else
1630+ ssh_gssapi_delete_ctx(&ctxt);
1631+
1632+ kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
1633+ BN_clear_free(shared_secret);
1634+ return kex_send_newkeys(ssh);
1635+}
1636+
1637+#endif /* GSSAPI */
1638diff --git a/kexgsss.c b/kexgsss.c
1639new file mode 100644
1640index 0000000..dd8ba1d
1641--- /dev/null
1642+++ b/kexgsss.c
1643@@ -0,0 +1,294 @@
1644+/*
1645+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1646+ *
1647+ * Redistribution and use in source and binary forms, with or without
1648+ * modification, are permitted provided that the following conditions
1649+ * are met:
1650+ * 1. Redistributions of source code must retain the above copyright
1651+ * notice, this list of conditions and the following disclaimer.
1652+ * 2. Redistributions in binary form must reproduce the above copyright
1653+ * notice, this list of conditions and the following disclaimer in the
1654+ * documentation and/or other materials provided with the distribution.
1655+ *
1656+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
1657+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1658+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1659+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1660+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1661+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1662+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1663+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1664+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1665+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1666+ */
1667+
1668+#include "includes.h"
1669+
1670+#ifdef GSSAPI
1671+
1672+#include <string.h>
1673+
1674+#include <openssl/crypto.h>
1675+#include <openssl/bn.h>
1676+
1677+#include "xmalloc.h"
1678+#include "buffer.h"
1679+#include "ssh2.h"
1680+#include "key.h"
1681+#include "cipher.h"
1682+#include "kex.h"
1683+#include "log.h"
1684+#include "packet.h"
1685+#include "dh.h"
1686+#include "ssh-gss.h"
1687+#include "monitor_wrap.h"
1688+#include "misc.h"
1689+#include "servconf.h"
1690+#include "digest.h"
1691+
1692+extern ServerOptions options;
1693+
1694+int
1695+kexgss_server(struct ssh *ssh)
1696+{
1697+ OM_uint32 maj_status, min_status;
1698+
1699+ /*
1700+ * Some GSSAPI implementations use the input value of ret_flags (an
1701+ * output variable) as a means of triggering mechanism specific
1702+ * features. Initializing it to zero avoids inadvertently
1703+ * activating this non-standard behaviour.
1704+ */
1705+
1706+ OM_uint32 ret_flags = 0;
1707+ gss_buffer_desc gssbuf, recv_tok, msg_tok;
1708+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1709+ Gssctxt *ctxt = NULL;
1710+ u_int slen, klen, kout;
1711+ u_char *kbuf;
1712+ DH *dh;
1713+ int min = -1, max = -1, nbits = -1;
1714+ BIGNUM *shared_secret = NULL;
1715+ BIGNUM *dh_client_pub = NULL;
1716+ int type = 0;
1717+ gss_OID oid;
1718+ char *mechs;
1719+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1720+ size_t hashlen;
1721+
1722+ /* Initialise GSSAPI */
1723+
1724+ /* If we're rekeying, privsep means that some of the private structures
1725+ * in the GSSAPI code are no longer available. This kludges them back
1726+ * into life
1727+ */
1728+ if (!ssh_gssapi_oid_table_ok()) {
1729+ mechs = ssh_gssapi_server_mechanisms();
1730+ free(mechs);
1731+ }
1732+
1733+ debug2("%s: Identifying %s", __func__, ssh->kex->name);
1734+ oid = ssh_gssapi_id_kex(NULL, ssh->kex->name, ssh->kex->kex_type);
1735+ if (oid == GSS_C_NO_OID)
1736+ fatal("Unknown gssapi mechanism");
1737+
1738+ debug2("%s: Acquiring credentials", __func__);
1739+
1740+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
1741+ fatal("Unable to acquire credentials for the server");
1742+
1743+ switch (ssh->kex->kex_type) {
1744+ case KEX_GSS_GRP1_SHA1:
1745+ dh = dh_new_group1();
1746+ break;
1747+ case KEX_GSS_GRP14_SHA1:
1748+ dh = dh_new_group14();
1749+ break;
1750+ case KEX_GSS_GEX_SHA1:
1751+ debug("Doing group exchange");
1752+ packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
1753+ min = packet_get_int();
1754+ nbits = packet_get_int();
1755+ max = packet_get_int();
1756+ packet_check_eom();
1757+ if (max < min || nbits < min || max < nbits)
1758+ fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
1759+ min, nbits, max);
1760+ dh = PRIVSEP(choose_dh(MAX(DH_GRP_MIN, min),
1761+ nbits, MIN(DH_GRP_MAX, max)));
1762+ if (dh == NULL)
1763+ packet_disconnect("Protocol error: no matching group found");
1764+
1765+ packet_start(SSH2_MSG_KEXGSS_GROUP);
1766+ packet_put_bignum2(dh->p);
1767+ packet_put_bignum2(dh->g);
1768+ packet_send();
1769+
1770+ packet_write_wait();
1771+ break;
1772+ default:
1773+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1774+ }
1775+
1776+ dh_gen_key(dh, ssh->kex->we_need * 8);
1777+
1778+ do {
1779+ debug("Wait SSH2_MSG_GSSAPI_INIT");
1780+ type = packet_read();
1781+ switch(type) {
1782+ case SSH2_MSG_KEXGSS_INIT:
1783+ if (dh_client_pub != NULL)
1784+ fatal("Received KEXGSS_INIT after initialising");
1785+ recv_tok.value = packet_get_string(&slen);
1786+ recv_tok.length = slen;
1787+
1788+ if ((dh_client_pub = BN_new()) == NULL)
1789+ fatal("dh_client_pub == NULL");
1790+
1791+ packet_get_bignum2(dh_client_pub);
1792+
1793+ /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
1794+ break;
1795+ case SSH2_MSG_KEXGSS_CONTINUE:
1796+ recv_tok.value = packet_get_string(&slen);
1797+ recv_tok.length = slen;
1798+ break;
1799+ default:
1800+ packet_disconnect(
1801+ "Protocol error: didn't expect packet type %d",
1802+ type);
1803+ }
1804+
1805+ maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
1806+ &send_tok, &ret_flags));
1807+
1808+ free(recv_tok.value);
1809+
1810+ if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
1811+ fatal("Zero length token output when incomplete");
1812+
1813+ if (dh_client_pub == NULL)
1814+ fatal("No client public key");
1815+
1816+ if (maj_status & GSS_S_CONTINUE_NEEDED) {
1817+ debug("Sending GSSAPI_CONTINUE");
1818+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1819+ packet_put_string(send_tok.value, send_tok.length);
1820+ packet_send();
1821+ gss_release_buffer(&min_status, &send_tok);
1822+ }
1823+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
1824+
1825+ if (GSS_ERROR(maj_status)) {
1826+ if (send_tok.length > 0) {
1827+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1828+ packet_put_string(send_tok.value, send_tok.length);
1829+ packet_send();
1830+ }
1831+ fatal("accept_ctx died");
1832+ }
1833+
1834+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1835+ fatal("Mutual Authentication flag wasn't set");
1836+
1837+ if (!(ret_flags & GSS_C_INTEG_FLAG))
1838+ fatal("Integrity flag wasn't set");
1839+
1840+ if (!dh_pub_is_valid(dh, dh_client_pub))
1841+ packet_disconnect("bad client public DH value");
1842+
1843+ klen = DH_size(dh);
1844+ kbuf = xmalloc(klen);
1845+ kout = DH_compute_key(kbuf, dh_client_pub, dh);
1846+ if (kout < 0)
1847+ fatal("DH_compute_key: failed");
1848+
1849+ shared_secret = BN_new();
1850+ if (shared_secret == NULL)
1851+ fatal("kexgss_server: BN_new failed");
1852+
1853+ if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
1854+ fatal("kexgss_server: BN_bin2bn failed");
1855+
1856+ memset(kbuf, 0, klen);
1857+ free(kbuf);
1858+
1859+ hashlen = sizeof(hash);
1860+ switch (ssh->kex->kex_type) {
1861+ case KEX_GSS_GRP1_SHA1:
1862+ case KEX_GSS_GRP14_SHA1:
1863+ kex_dh_hash(
1864+ ssh->kex->client_version_string, ssh->kex->server_version_string,
1865+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1866+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1867+ NULL, 0, /* Change this if we start sending host keys */
1868+ dh_client_pub, dh->pub_key, shared_secret,
1869+ hash, &hashlen
1870+ );
1871+ break;
1872+ case KEX_GSS_GEX_SHA1:
1873+ kexgex_hash(
1874+ ssh->kex->hash_alg,
1875+ ssh->kex->client_version_string, ssh->kex->server_version_string,
1876+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1877+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1878+ NULL, 0,
1879+ min, nbits, max,
1880+ dh->p, dh->g,
1881+ dh_client_pub,
1882+ dh->pub_key,
1883+ shared_secret,
1884+ hash, &hashlen
1885+ );
1886+ break;
1887+ default:
1888+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1889+ }
1890+
1891+ BN_clear_free(dh_client_pub);
1892+
1893+ if (ssh->kex->session_id == NULL) {
1894+ ssh->kex->session_id_len = hashlen;
1895+ ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
1896+ memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
1897+ }
1898+
1899+ gssbuf.value = hash;
1900+ gssbuf.length = hashlen;
1901+
1902+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
1903+ fatal("Couldn't get MIC");
1904+
1905+ packet_start(SSH2_MSG_KEXGSS_COMPLETE);
1906+ packet_put_bignum2(dh->pub_key);
1907+ packet_put_string(msg_tok.value,msg_tok.length);
1908+
1909+ if (send_tok.length != 0) {
1910+ packet_put_char(1); /* true */
1911+ packet_put_string(send_tok.value, send_tok.length);
1912+ } else {
1913+ packet_put_char(0); /* false */
1914+ }
1915+ packet_send();
1916+
1917+ gss_release_buffer(&min_status, &send_tok);
1918+ gss_release_buffer(&min_status, &msg_tok);
1919+
1920+ if (gss_kex_context == NULL)
1921+ gss_kex_context = ctxt;
1922+ else
1923+ ssh_gssapi_delete_ctx(&ctxt);
1924+
1925+ DH_free(dh);
1926+
1927+ kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
1928+ BN_clear_free(shared_secret);
1929+ kex_send_newkeys(ssh);
1930+
1931+ /* If this was a rekey, then save out any delegated credentials we
1932+ * just exchanged. */
1933+ if (options.gss_store_rekey)
1934+ ssh_gssapi_rekey_creds();
1935+ return 0;
1936+}
1937+#endif /* GSSAPI */
1938diff --git a/monitor.c b/monitor.c
1939index ac7dd30..6c82023 100644
1940--- a/monitor.c
1941+++ b/monitor.c
1942@@ -156,6 +156,8 @@ int mm_answer_gss_setup_ctx(int, Buffer *);
1943 int mm_answer_gss_accept_ctx(int, Buffer *);
1944 int mm_answer_gss_userok(int, Buffer *);
1945 int mm_answer_gss_checkmic(int, Buffer *);
1946+int mm_answer_gss_sign(int, Buffer *);
1947+int mm_answer_gss_updatecreds(int, Buffer *);
1948 #endif
1949
1950 #ifdef SSH_AUDIT_EVENTS
1951@@ -233,11 +235,18 @@ struct mon_table mon_dispatch_proto20[] = {
1952 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
1953 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
1954 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
1955+ {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
1956 #endif
1957 {0, 0, NULL}
1958 };
1959
1960 struct mon_table mon_dispatch_postauth20[] = {
1961+#ifdef GSSAPI
1962+ {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
1963+ {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
1964+ {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
1965+ {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
1966+#endif
1967 #ifdef WITH_OPENSSL
1968 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
1969 #endif
1970@@ -352,6 +361,10 @@ monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
1971 /* Permit requests for moduli and signatures */
1972 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
1973 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
1974+#ifdef GSSAPI
1975+ /* and for the GSSAPI key exchange */
1976+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
1977+#endif
1978 } else {
1979 mon_dispatch = mon_dispatch_proto15;
1980
1981@@ -460,6 +473,10 @@ monitor_child_postauth(struct monitor *pmonitor)
1982 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
1983 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
1984 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
1985+#ifdef GSSAPI
1986+ /* and for the GSSAPI key exchange */
1987+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
1988+#endif
1989 } else {
1990 mon_dispatch = mon_dispatch_postauth15;
1991 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
1992@@ -1861,6 +1878,13 @@ monitor_apply_keystate(struct monitor *pmonitor)
1993 # endif
1994 #endif /* WITH_OPENSSL */
1995 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1996+#ifdef GSSAPI
1997+ if (options.gss_keyex) {
1998+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1999+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
2000+ kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
2001+ }
2002+#endif
2003 kex->load_host_public_key=&get_hostkey_public_by_type;
2004 kex->load_host_private_key=&get_hostkey_private_by_type;
2005 kex->host_key_index=&get_hostkey_index;
2006@@ -1960,6 +1984,9 @@ mm_answer_gss_setup_ctx(int sock, Buffer *m)
2007 OM_uint32 major;
2008 u_int len;
2009
2010+ if (!options.gss_authentication && !options.gss_keyex)
2011+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2012+
2013 goid.elements = buffer_get_string(m, &len);
2014 goid.length = len;
2015
2016@@ -1987,6 +2014,9 @@ mm_answer_gss_accept_ctx(int sock, Buffer *m)
2017 OM_uint32 flags = 0; /* GSI needs this */
2018 u_int len;
2019
2020+ if (!options.gss_authentication && !options.gss_keyex)
2021+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2022+
2023 in.value = buffer_get_string(m, &len);
2024 in.length = len;
2025 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2026@@ -2004,6 +2034,7 @@ mm_answer_gss_accept_ctx(int sock, Buffer *m)
2027 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2028 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2029 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2030+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
2031 }
2032 return (0);
2033 }
2034@@ -2015,6 +2046,9 @@ mm_answer_gss_checkmic(int sock, Buffer *m)
2035 OM_uint32 ret;
2036 u_int len;
2037
2038+ if (!options.gss_authentication && !options.gss_keyex)
2039+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2040+
2041 gssbuf.value = buffer_get_string(m, &len);
2042 gssbuf.length = len;
2043 mic.value = buffer_get_string(m, &len);
2044@@ -2041,7 +2075,11 @@ mm_answer_gss_userok(int sock, Buffer *m)
2045 {
2046 int authenticated;
2047
2048- authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2049+ if (!options.gss_authentication && !options.gss_keyex)
2050+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2051+
2052+ authenticated = authctxt->valid &&
2053+ ssh_gssapi_userok(authctxt->user, authctxt->pw);
2054
2055 buffer_clear(m);
2056 buffer_put_int(m, authenticated);
2057@@ -2054,5 +2092,73 @@ mm_answer_gss_userok(int sock, Buffer *m)
2058 /* Monitor loop will terminate if authenticated */
2059 return (authenticated);
2060 }
2061+
2062+int
2063+mm_answer_gss_sign(int socket, Buffer *m)
2064+{
2065+ gss_buffer_desc data;
2066+ gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2067+ OM_uint32 major, minor;
2068+ u_int len;
2069+
2070+ if (!options.gss_authentication && !options.gss_keyex)
2071+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2072+
2073+ data.value = buffer_get_string(m, &len);
2074+ data.length = len;
2075+ if (data.length != 20)
2076+ fatal("%s: data length incorrect: %d", __func__,
2077+ (int) data.length);
2078+
2079+ /* Save the session ID on the first time around */
2080+ if (session_id2_len == 0) {
2081+ session_id2_len = data.length;
2082+ session_id2 = xmalloc(session_id2_len);
2083+ memcpy(session_id2, data.value, session_id2_len);
2084+ }
2085+ major = ssh_gssapi_sign(gsscontext, &data, &hash);
2086+
2087+ free(data.value);
2088+
2089+ buffer_clear(m);
2090+ buffer_put_int(m, major);
2091+ buffer_put_string(m, hash.value, hash.length);
2092+
2093+ mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2094+
2095+ gss_release_buffer(&minor, &hash);
2096+
2097+ /* Turn on getpwnam permissions */
2098+ monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2099+
2100+ /* And credential updating, for when rekeying */
2101+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
2102+
2103+ return (0);
2104+}
2105+
2106+int
2107+mm_answer_gss_updatecreds(int socket, Buffer *m) {
2108+ ssh_gssapi_ccache store;
2109+ int ok;
2110+
2111+ store.filename = buffer_get_string(m, NULL);
2112+ store.envvar = buffer_get_string(m, NULL);
2113+ store.envval = buffer_get_string(m, NULL);
2114+
2115+ ok = ssh_gssapi_update_creds(&store);
2116+
2117+ free(store.filename);
2118+ free(store.envvar);
2119+ free(store.envval);
2120+
2121+ buffer_clear(m);
2122+ buffer_put_int(m, ok);
2123+
2124+ mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
2125+
2126+ return(0);
2127+}
2128+
2129 #endif /* GSSAPI */
2130
2131diff --git a/monitor.h b/monitor.h
2132index 93b8b66..bc50ade 100644
2133--- a/monitor.h
2134+++ b/monitor.h
2135@@ -65,6 +65,9 @@ enum monitor_reqtype {
2136 MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
2137 MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
2138
2139+ MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
2140+ MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
2141+
2142 };
2143
2144 struct mm_master;
2145diff --git a/monitor_wrap.c b/monitor_wrap.c
2146index c5db6df..74fbd2e 100644
2147--- a/monitor_wrap.c
2148+++ b/monitor_wrap.c
2149@@ -1068,7 +1068,7 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
2150 }
2151
2152 int
2153-mm_ssh_gssapi_userok(char *user)
2154+mm_ssh_gssapi_userok(char *user, struct passwd *pw)
2155 {
2156 Buffer m;
2157 int authenticated = 0;
2158@@ -1085,5 +1085,50 @@ mm_ssh_gssapi_userok(char *user)
2159 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
2160 return (authenticated);
2161 }
2162+
2163+OM_uint32
2164+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
2165+{
2166+ Buffer m;
2167+ OM_uint32 major;
2168+ u_int len;
2169+
2170+ buffer_init(&m);
2171+ buffer_put_string(&m, data->value, data->length);
2172+
2173+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, &m);
2174+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, &m);
2175+
2176+ major = buffer_get_int(&m);
2177+ hash->value = buffer_get_string(&m, &len);
2178+ hash->length = len;
2179+
2180+ buffer_free(&m);
2181+
2182+ return(major);
2183+}
2184+
2185+int
2186+mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
2187+{
2188+ Buffer m;
2189+ int ok;
2190+
2191+ buffer_init(&m);
2192+
2193+ buffer_put_cstring(&m, store->filename ? store->filename : "");
2194+ buffer_put_cstring(&m, store->envvar ? store->envvar : "");
2195+ buffer_put_cstring(&m, store->envval ? store->envval : "");
2196+
2197+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, &m);
2198+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, &m);
2199+
2200+ ok = buffer_get_int(&m);
2201+
2202+ buffer_free(&m);
2203+
2204+ return (ok);
2205+}
2206+
2207 #endif /* GSSAPI */
2208
2209diff --git a/monitor_wrap.h b/monitor_wrap.h
2210index eb820ae..403f8d0 100644
2211--- a/monitor_wrap.h
2212+++ b/monitor_wrap.h
2213@@ -58,8 +58,10 @@ BIGNUM *mm_auth_rsa_generate_challenge(Key *);
2214 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
2215 OM_uint32 mm_ssh_gssapi_accept_ctx(Gssctxt *,
2216 gss_buffer_desc *, gss_buffer_desc *, OM_uint32 *);
2217-int mm_ssh_gssapi_userok(char *user);
2218+int mm_ssh_gssapi_userok(char *user, struct passwd *);
2219 OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
2220+OM_uint32 mm_ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
2221+int mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *);
2222 #endif
2223
2224 #ifdef USE_PAM
2225diff --git a/readconf.c b/readconf.c
2226index 69d4553..d2a3d4b 100644
2227--- a/readconf.c
2228+++ b/readconf.c
2229@@ -148,6 +148,8 @@ typedef enum {
2230 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
2231 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
2232 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
2233+ oGssTrustDns, oGssKeyEx, oGssClientIdentity, oGssRenewalRekey,
2234+ oGssServerIdentity,
2235 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
2236 oSendEnv, oControlPath, oControlMaster, oControlPersist,
2237 oHashKnownHosts,
2238@@ -193,10 +195,19 @@ static struct {
2239 { "afstokenpassing", oUnsupported },
2240 #if defined(GSSAPI)
2241 { "gssapiauthentication", oGssAuthentication },
2242+ { "gssapikeyexchange", oGssKeyEx },
2243 { "gssapidelegatecredentials", oGssDelegateCreds },
2244+ { "gssapitrustdns", oGssTrustDns },
2245+ { "gssapiclientidentity", oGssClientIdentity },
2246+ { "gssapiserveridentity", oGssServerIdentity },
2247+ { "gssapirenewalforcesrekey", oGssRenewalRekey },
2248 #else
2249 { "gssapiauthentication", oUnsupported },
2250+ { "gssapikeyexchange", oUnsupported },
2251 { "gssapidelegatecredentials", oUnsupported },
2252+ { "gssapitrustdns", oUnsupported },
2253+ { "gssapiclientidentity", oUnsupported },
2254+ { "gssapirenewalforcesrekey", oUnsupported },
2255 #endif
2256 { "fallbacktorsh", oDeprecated },
2257 { "usersh", oDeprecated },
2258@@ -926,10 +937,30 @@ parse_time:
2259 intptr = &options->gss_authentication;
2260 goto parse_flag;
2261
2262+ case oGssKeyEx:
2263+ intptr = &options->gss_keyex;
2264+ goto parse_flag;
2265+
2266 case oGssDelegateCreds:
2267 intptr = &options->gss_deleg_creds;
2268 goto parse_flag;
2269
2270+ case oGssTrustDns:
2271+ intptr = &options->gss_trust_dns;
2272+ goto parse_flag;
2273+
2274+ case oGssClientIdentity:
2275+ charptr = &options->gss_client_identity;
2276+ goto parse_string;
2277+
2278+ case oGssServerIdentity:
2279+ charptr = &options->gss_server_identity;
2280+ goto parse_string;
2281+
2282+ case oGssRenewalRekey:
2283+ intptr = &options->gss_renewal_rekey;
2284+ goto parse_flag;
2285+
2286 case oBatchMode:
2287 intptr = &options->batch_mode;
2288 goto parse_flag;
2289@@ -1648,7 +1679,12 @@ initialize_options(Options * options)
2290 options->pubkey_authentication = -1;
2291 options->challenge_response_authentication = -1;
2292 options->gss_authentication = -1;
2293+ options->gss_keyex = -1;
2294 options->gss_deleg_creds = -1;
2295+ options->gss_trust_dns = -1;
2296+ options->gss_renewal_rekey = -1;
2297+ options->gss_client_identity = NULL;
2298+ options->gss_server_identity = NULL;
2299 options->password_authentication = -1;
2300 options->kbd_interactive_authentication = -1;
2301 options->kbd_interactive_devices = NULL;
2302@@ -1777,8 +1813,14 @@ fill_default_options(Options * options)
2303 options->challenge_response_authentication = 1;
2304 if (options->gss_authentication == -1)
2305 options->gss_authentication = 0;
2306+ if (options->gss_keyex == -1)
2307+ options->gss_keyex = 0;
2308 if (options->gss_deleg_creds == -1)
2309 options->gss_deleg_creds = 0;
2310+ if (options->gss_trust_dns == -1)
2311+ options->gss_trust_dns = 0;
2312+ if (options->gss_renewal_rekey == -1)
2313+ options->gss_renewal_rekey = 0;
2314 if (options->password_authentication == -1)
2315 options->password_authentication = 1;
2316 if (options->kbd_interactive_authentication == -1)
2317diff --git a/readconf.h b/readconf.h
2318index c84d068..37a0555 100644
2319--- a/readconf.h
2320+++ b/readconf.h
2321@@ -45,7 +45,12 @@ typedef struct {
2322 int challenge_response_authentication;
2323 /* Try S/Key or TIS, authentication. */
2324 int gss_authentication; /* Try GSS authentication */
2325+ int gss_keyex; /* Try GSS key exchange */
2326 int gss_deleg_creds; /* Delegate GSS credentials */
2327+ int gss_trust_dns; /* Trust DNS for GSS canonicalization */
2328+ int gss_renewal_rekey; /* Credential renewal forces rekey */
2329+ char *gss_client_identity; /* Principal to initiate GSSAPI with */
2330+ char *gss_server_identity; /* GSSAPI target principal */
2331 int password_authentication; /* Try password
2332 * authentication. */
2333 int kbd_interactive_authentication; /* Try keyboard-interactive auth. */
2334diff --git a/servconf.c b/servconf.c
2335index b19d30e..b8af6dd 100644
2336--- a/servconf.c
2337+++ b/servconf.c
2338@@ -117,8 +117,10 @@ initialize_server_options(ServerOptions *options)
2339 options->kerberos_ticket_cleanup = -1;
2340 options->kerberos_get_afs_token = -1;
2341 options->gss_authentication=-1;
2342+ options->gss_keyex = -1;
2343 options->gss_cleanup_creds = -1;
2344 options->gss_strict_acceptor = -1;
2345+ options->gss_store_rekey = -1;
2346 options->password_authentication = -1;
2347 options->kbd_interactive_authentication = -1;
2348 options->challenge_response_authentication = -1;
2349@@ -287,10 +289,14 @@ fill_default_server_options(ServerOptions *options)
2350 options->kerberos_get_afs_token = 0;
2351 if (options->gss_authentication == -1)
2352 options->gss_authentication = 0;
2353+ if (options->gss_keyex == -1)
2354+ options->gss_keyex = 0;
2355 if (options->gss_cleanup_creds == -1)
2356 options->gss_cleanup_creds = 1;
2357 if (options->gss_strict_acceptor == -1)
2358- options->gss_strict_acceptor = 0;
2359+ options->gss_strict_acceptor = 1;
2360+ if (options->gss_store_rekey == -1)
2361+ options->gss_store_rekey = 0;
2362 if (options->password_authentication == -1)
2363 options->password_authentication = 1;
2364 if (options->kbd_interactive_authentication == -1)
2365@@ -419,6 +425,7 @@ typedef enum {
2366 sHostKeyAlgorithms,
2367 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
2368 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
2369+ sGssKeyEx, sGssStoreRekey,
2370 sAcceptEnv, sPermitTunnel,
2371 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
2372 sUsePrivilegeSeparation, sAllowAgentForwarding,
2373@@ -492,12 +499,20 @@ static struct {
2374 #ifdef GSSAPI
2375 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
2376 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
2377+ { "gssapicleanupcreds", sGssCleanupCreds, SSHCFG_GLOBAL },
2378 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
2379+ { "gssapikeyexchange", sGssKeyEx, SSHCFG_GLOBAL },
2380+ { "gssapistorecredentialsonrekey", sGssStoreRekey, SSHCFG_GLOBAL },
2381 #else
2382 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
2383 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
2384+ { "gssapicleanupcreds", sUnsupported, SSHCFG_GLOBAL },
2385 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
2386+ { "gssapikeyexchange", sUnsupported, SSHCFG_GLOBAL },
2387+ { "gssapistorecredentialsonrekey", sUnsupported, SSHCFG_GLOBAL },
2388 #endif
2389+ { "gssusesessionccache", sUnsupported, SSHCFG_GLOBAL },
2390+ { "gssapiusesessioncredcache", sUnsupported, SSHCFG_GLOBAL },
2391 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
2392 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
2393 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
2394@@ -1242,6 +1257,10 @@ process_server_config_line(ServerOptions *options, char *line,
2395 intptr = &options->gss_authentication;
2396 goto parse_flag;
2397
2398+ case sGssKeyEx:
2399+ intptr = &options->gss_keyex;
2400+ goto parse_flag;
2401+
2402 case sGssCleanupCreds:
2403 intptr = &options->gss_cleanup_creds;
2404 goto parse_flag;
2405@@ -1250,6 +1269,10 @@ process_server_config_line(ServerOptions *options, char *line,
2406 intptr = &options->gss_strict_acceptor;
2407 goto parse_flag;
2408
2409+ case sGssStoreRekey:
2410+ intptr = &options->gss_store_rekey;
2411+ goto parse_flag;
2412+
2413 case sPasswordAuthentication:
2414 intptr = &options->password_authentication;
2415 goto parse_flag;
2416@@ -2265,7 +2288,10 @@ dump_config(ServerOptions *o)
2417 #endif
2418 #ifdef GSSAPI
2419 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2420+ dump_cfg_fmtint(sGssKeyEx, o->gss_keyex);
2421 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
2422+ dump_cfg_fmtint(sGssStrictAcceptor, o->gss_strict_acceptor);
2423+ dump_cfg_fmtint(sGssStoreRekey, o->gss_store_rekey);
2424 #endif
2425 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2426 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2427diff --git a/servconf.h b/servconf.h
2428index f4137af..778ba17 100644
2429--- a/servconf.h
2430+++ b/servconf.h
2431@@ -118,8 +118,10 @@ typedef struct {
2432 int kerberos_get_afs_token; /* If true, try to get AFS token if
2433 * authenticated with Kerberos. */
2434 int gss_authentication; /* If true, permit GSSAPI authentication */
2435+ int gss_keyex; /* If true, permit GSSAPI key exchange */
2436 int gss_cleanup_creds; /* If true, destroy cred cache on logout */
2437 int gss_strict_acceptor; /* If true, restrict the GSSAPI acceptor name */
2438+ int gss_store_rekey;
2439 int password_authentication; /* If true, permit password
2440 * authentication. */
2441 int kbd_interactive_authentication; /* If true, permit */
2442diff --git a/ssh-gss.h b/ssh-gss.h
2443index a99d7f0..914701b 100644
2444--- a/ssh-gss.h
2445+++ b/ssh-gss.h
2446@@ -1,6 +1,6 @@
2447 /* $OpenBSD: ssh-gss.h,v 1.11 2014/02/26 20:28:44 djm Exp $ */
2448 /*
2449- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
2450+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
2451 *
2452 * Redistribution and use in source and binary forms, with or without
2453 * modification, are permitted provided that the following conditions
2454@@ -61,10 +61,22 @@
2455
2456 #define SSH_GSS_OIDTYPE 0x06
2457
2458+#define SSH2_MSG_KEXGSS_INIT 30
2459+#define SSH2_MSG_KEXGSS_CONTINUE 31
2460+#define SSH2_MSG_KEXGSS_COMPLETE 32
2461+#define SSH2_MSG_KEXGSS_HOSTKEY 33
2462+#define SSH2_MSG_KEXGSS_ERROR 34
2463+#define SSH2_MSG_KEXGSS_GROUPREQ 40
2464+#define SSH2_MSG_KEXGSS_GROUP 41
2465+#define KEX_GSS_GRP1_SHA1_ID "gss-group1-sha1-"
2466+#define KEX_GSS_GRP14_SHA1_ID "gss-group14-sha1-"
2467+#define KEX_GSS_GEX_SHA1_ID "gss-gex-sha1-"
2468+
2469 typedef struct {
2470 char *filename;
2471 char *envvar;
2472 char *envval;
2473+ struct passwd *owner;
2474 void *data;
2475 } ssh_gssapi_ccache;
2476
2477@@ -72,8 +84,11 @@ typedef struct {
2478 gss_buffer_desc displayname;
2479 gss_buffer_desc exportedname;
2480 gss_cred_id_t creds;
2481+ gss_name_t name;
2482 struct ssh_gssapi_mech_struct *mech;
2483 ssh_gssapi_ccache store;
2484+ int used;
2485+ int updated;
2486 } ssh_gssapi_client;
2487
2488 typedef struct ssh_gssapi_mech_struct {
2489@@ -84,6 +99,7 @@ typedef struct ssh_gssapi_mech_struct {
2490 int (*userok) (ssh_gssapi_client *, char *);
2491 int (*localname) (ssh_gssapi_client *, char **);
2492 void (*storecreds) (ssh_gssapi_client *);
2493+ int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
2494 } ssh_gssapi_mech;
2495
2496 typedef struct {
2497@@ -94,10 +110,11 @@ typedef struct {
2498 gss_OID oid; /* client */
2499 gss_cred_id_t creds; /* server */
2500 gss_name_t client; /* server */
2501- gss_cred_id_t client_creds; /* server */
2502+ gss_cred_id_t client_creds; /* both */
2503 } Gssctxt;
2504
2505 extern ssh_gssapi_mech *supported_mechs[];
2506+extern Gssctxt *gss_kex_context;
2507
2508 int ssh_gssapi_check_oid(Gssctxt *, void *, size_t);
2509 void ssh_gssapi_set_oid_data(Gssctxt *, void *, size_t);
2510@@ -119,16 +136,32 @@ void ssh_gssapi_build_ctx(Gssctxt **);
2511 void ssh_gssapi_delete_ctx(Gssctxt **);
2512 OM_uint32 ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
2513 void ssh_gssapi_buildmic(Buffer *, const char *, const char *, const char *);
2514-int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *);
2515+int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *, const char *);
2516+OM_uint32 ssh_gssapi_client_identity(Gssctxt *, const char *);
2517+int ssh_gssapi_credentials_updated(Gssctxt *);
2518
2519 /* In the server */
2520+typedef int ssh_gssapi_check_fn(Gssctxt **, gss_OID, const char *,
2521+ const char *);
2522+char *ssh_gssapi_client_mechanisms(const char *, const char *);
2523+char *ssh_gssapi_kex_mechs(gss_OID_set, ssh_gssapi_check_fn *, const char *,
2524+ const char *);
2525+gss_OID ssh_gssapi_id_kex(Gssctxt *, char *, int);
2526+int ssh_gssapi_server_check_mech(Gssctxt **,gss_OID, const char *,
2527+ const char *);
2528 OM_uint32 ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
2529-int ssh_gssapi_userok(char *name);
2530+int ssh_gssapi_userok(char *name, struct passwd *);
2531 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
2532 void ssh_gssapi_do_child(char ***, u_int *);
2533 void ssh_gssapi_cleanup_creds(void);
2534 void ssh_gssapi_storecreds(void);
2535
2536+char *ssh_gssapi_server_mechanisms(void);
2537+int ssh_gssapi_oid_table_ok(void);
2538+
2539+int ssh_gssapi_update_creds(ssh_gssapi_ccache *store);
2540+void ssh_gssapi_rekey_creds(void);
2541+
2542 #endif /* GSSAPI */
2543
2544 #endif /* _SSH_GSS_H */
2545diff --git a/ssh_config b/ssh_config
2546index 90fb63f..4e879cd 100644
2547--- a/ssh_config
2548+++ b/ssh_config
2549@@ -26,6 +26,8 @@
2550 # HostbasedAuthentication no
2551 # GSSAPIAuthentication no
2552 # GSSAPIDelegateCredentials no
2553+# GSSAPIKeyExchange no
2554+# GSSAPITrustDNS no
2555 # BatchMode no
2556 # CheckHostIP yes
2557 # AddressFamily any
2558diff --git a/ssh_config.5 b/ssh_config.5
2559index caf13a6..9060d5b 100644
2560--- a/ssh_config.5
2561+++ b/ssh_config.5
2562@@ -826,10 +826,42 @@ The default is
2563 Specifies whether user authentication based on GSSAPI is allowed.
2564 The default is
2565 .Dq no .
2566+.It Cm GSSAPIKeyExchange
2567+Specifies whether key exchange based on GSSAPI may be used. When using
2568+GSSAPI key exchange the server need not have a host key.
2569+The default is
2570+.Dq no .
2571+.It Cm GSSAPIClientIdentity
2572+If set, specifies the GSSAPI client identity that ssh should use when
2573+connecting to the server. The default is unset, which means that the default
2574+identity will be used.
2575+.It Cm GSSAPIServerIdentity
2576+If set, specifies the GSSAPI server identity that ssh should expect when
2577+connecting to the server. The default is unset, which means that the
2578+expected GSSAPI server identity will be determined from the target
2579+hostname.
2580 .It Cm GSSAPIDelegateCredentials
2581 Forward (delegate) credentials to the server.
2582 The default is
2583 .Dq no .
2584+.It Cm GSSAPIRenewalForcesRekey
2585+If set to
2586+.Dq yes
2587+then renewal of the client's GSSAPI credentials will force the rekeying of the
2588+ssh connection. With a compatible server, this can delegate the renewed
2589+credentials to a session on the server.
2590+The default is
2591+.Dq no .
2592+.It Cm GSSAPITrustDns
2593+Set to
2594+.Dq yes
2595+to indicate that the DNS is trusted to securely canonicalize
2596+the name of the host being connected to. If
2597+.Dq no ,
2598+the hostname entered on the
2599+command line will be passed untouched to the GSSAPI library.
2600+The default is
2601+.Dq no .
2602 .It Cm HashKnownHosts
2603 Indicates that
2604 .Xr ssh 1
2605diff --git a/sshconnect2.c b/sshconnect2.c
2606index f79c96b..b452eae 100644
2607--- a/sshconnect2.c
2608+++ b/sshconnect2.c
2609@@ -161,6 +161,11 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
2610 struct kex *kex;
2611 int r;
2612
2613+#ifdef GSSAPI
2614+ char *orig = NULL, *gss = NULL;
2615+ char *gss_host = NULL;
2616+#endif
2617+
2618 xxx_host = host;
2619 xxx_hostaddr = hostaddr;
2620
2621@@ -195,6 +200,33 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
2622 order_hostkeyalgs(host, hostaddr, port));
2623 }
2624
2625+#ifdef GSSAPI
2626+ if (options.gss_keyex) {
2627+ /* Add the GSSAPI mechanisms currently supported on this
2628+ * client to the key exchange algorithm proposal */
2629+ orig = myproposal[PROPOSAL_KEX_ALGS];
2630+
2631+ if (options.gss_trust_dns)
2632+ gss_host = (char *)get_canonical_hostname(1);
2633+ else
2634+ gss_host = host;
2635+
2636+ gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
2637+ if (gss) {
2638+ debug("Offering GSSAPI proposal: %s", gss);
2639+ xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
2640+ "%s,%s", gss, orig);
2641+
2642+ /* If we've got GSSAPI algorithms, then we also
2643+ * support the 'null' hostkey, as a last resort */
2644+ orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
2645+ xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
2646+ "%s,null", orig);
2647+ free(gss);
2648+ }
2649+ }
2650+#endif
2651+
2652 if (options.rekey_limit || options.rekey_interval)
2653 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
2654 (time_t)options.rekey_interval);
2655@@ -213,10 +245,30 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
2656 # endif
2657 #endif
2658 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
2659+#ifdef GSSAPI
2660+ if (options.gss_keyex) {
2661+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
2662+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
2663+ kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
2664+ }
2665+#endif
2666 kex->client_version_string=client_version_string;
2667 kex->server_version_string=server_version_string;
2668 kex->verify_host_key=&verify_host_key_callback;
2669
2670+#ifdef GSSAPI
2671+ if (options.gss_keyex) {
2672+ kex->gss_deleg_creds = options.gss_deleg_creds;
2673+ kex->gss_trust_dns = options.gss_trust_dns;
2674+ kex->gss_client = options.gss_client_identity;
2675+ if (options.gss_server_identity) {
2676+ kex->gss_host = options.gss_server_identity;
2677+ } else {
2678+ kex->gss_host = gss_host;
2679+ }
2680+ }
2681+#endif
2682+
2683 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
2684
2685 /* remove ext-info from the KEX proposals for rekeying */
2686@@ -311,6 +363,7 @@ int input_gssapi_token(int type, u_int32_t, void *);
2687 int input_gssapi_hash(int type, u_int32_t, void *);
2688 int input_gssapi_error(int, u_int32_t, void *);
2689 int input_gssapi_errtok(int, u_int32_t, void *);
2690+int userauth_gsskeyex(Authctxt *authctxt);
2691 #endif
2692
2693 void userauth(Authctxt *, char *);
2694@@ -326,6 +379,11 @@ static char *authmethods_get(void);
2695
2696 Authmethod authmethods[] = {
2697 #ifdef GSSAPI
2698+ {"gssapi-keyex",
2699+ userauth_gsskeyex,
2700+ NULL,
2701+ &options.gss_authentication,
2702+ NULL},
2703 {"gssapi-with-mic",
2704 userauth_gssapi,
2705 NULL,
2706@@ -656,19 +714,31 @@ userauth_gssapi(Authctxt *authctxt)
2707 static u_int mech = 0;
2708 OM_uint32 min;
2709 int ok = 0;
2710+ const char *gss_host;
2711+
2712+ if (options.gss_server_identity)
2713+ gss_host = options.gss_server_identity;
2714+ else if (options.gss_trust_dns)
2715+ gss_host = get_canonical_hostname(1);
2716+ else
2717+ gss_host = authctxt->host;
2718
2719 /* Try one GSSAPI method at a time, rather than sending them all at
2720 * once. */
2721
2722 if (gss_supported == NULL)
2723- gss_indicate_mechs(&min, &gss_supported);
2724+ if (GSS_ERROR(gss_indicate_mechs(&min, &gss_supported))) {
2725+ gss_supported = NULL;
2726+ return 0;
2727+ }
2728
2729 /* Check to see if the mechanism is usable before we offer it */
2730 while (mech < gss_supported->count && !ok) {
2731 /* My DER encoding requires length<128 */
2732 if (gss_supported->elements[mech].length < 128 &&
2733 ssh_gssapi_check_mechanism(&gssctxt,
2734- &gss_supported->elements[mech], authctxt->host)) {
2735+ &gss_supported->elements[mech], gss_host,
2736+ options.gss_client_identity)) {
2737 ok = 1; /* Mechanism works */
2738 } else {
2739 mech++;
2740@@ -765,8 +835,8 @@ input_gssapi_response(int type, u_int32_t plen, void *ctxt)
2741 {
2742 Authctxt *authctxt = ctxt;
2743 Gssctxt *gssctxt;
2744- int oidlen;
2745- char *oidv;
2746+ u_int oidlen;
2747+ u_char *oidv;
2748
2749 if (authctxt == NULL)
2750 fatal("input_gssapi_response: no authentication context");
2751@@ -879,6 +949,48 @@ input_gssapi_error(int type, u_int32_t plen, void *ctxt)
2752 free(lang);
2753 return 0;
2754 }
2755+
2756+int
2757+userauth_gsskeyex(Authctxt *authctxt)
2758+{
2759+ Buffer b;
2760+ gss_buffer_desc gssbuf;
2761+ gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
2762+ OM_uint32 ms;
2763+
2764+ static int attempt = 0;
2765+ if (attempt++ >= 1)
2766+ return (0);
2767+
2768+ if (gss_kex_context == NULL) {
2769+ debug("No valid Key exchange context");
2770+ return (0);
2771+ }
2772+
2773+ ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
2774+ "gssapi-keyex");
2775+
2776+ gssbuf.value = buffer_ptr(&b);
2777+ gssbuf.length = buffer_len(&b);
2778+
2779+ if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
2780+ buffer_free(&b);
2781+ return (0);
2782+ }
2783+
2784+ packet_start(SSH2_MSG_USERAUTH_REQUEST);
2785+ packet_put_cstring(authctxt->server_user);
2786+ packet_put_cstring(authctxt->service);
2787+ packet_put_cstring(authctxt->method->name);
2788+ packet_put_string(mic.value, mic.length);
2789+ packet_send();
2790+
2791+ buffer_free(&b);
2792+ gss_release_buffer(&ms, &mic);
2793+
2794+ return (1);
2795+}
2796+
2797 #endif /* GSSAPI */
2798
2799 int
2800diff --git a/sshd.c b/sshd.c
2801index 430569c..5cd9129 100644
2802--- a/sshd.c
2803+++ b/sshd.c
2804@@ -125,6 +125,10 @@
2805 #include "version.h"
2806 #include "ssherr.h"
2807
2808+#ifdef USE_SECURITY_SESSION_API
2809+#include <Security/AuthSession.h>
2810+#endif
2811+
2812 #ifndef O_NOCTTY
2813 #define O_NOCTTY 0
2814 #endif
2815@@ -1833,10 +1837,13 @@ main(int ac, char **av)
2816 logit("Disabling protocol version 1. Could not load host key");
2817 options.protocol &= ~SSH_PROTO_1;
2818 }
2819+#ifndef GSSAPI
2820+ /* The GSSAPI key exchange can run without a host key */
2821 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
2822 logit("Disabling protocol version 2. Could not load host key");
2823 options.protocol &= ~SSH_PROTO_2;
2824 }
2825+#endif
2826 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
2827 logit("sshd: no hostkeys available -- exiting.");
2828 exit(1);
2829@@ -2151,6 +2158,60 @@ main(int ac, char **av)
2830 remote_ip, remote_port, laddr, get_local_port());
2831 free(laddr);
2832
2833+#ifdef USE_SECURITY_SESSION_API
2834+ /*
2835+ * Create a new security session for use by the new user login if
2836+ * the current session is the root session or we are not launched
2837+ * by inetd (eg: debugging mode or server mode). We do not
2838+ * necessarily need to create a session if we are launched from
2839+ * inetd because Panther xinetd will create a session for us.
2840+ *
2841+ * The only case where this logic will fail is if there is an
2842+ * inetd running in a non-root session which is not creating
2843+ * new sessions for us. Then all the users will end up in the
2844+ * same session (bad).
2845+ *
2846+ * When the client exits, the session will be destroyed for us
2847+ * automatically.
2848+ *
2849+ * We must create the session before any credentials are stored
2850+ * (including AFS pags, which happens a few lines below).
2851+ */
2852+ {
2853+ OSStatus err = 0;
2854+ SecuritySessionId sid = 0;
2855+ SessionAttributeBits sattrs = 0;
2856+
2857+ err = SessionGetInfo(callerSecuritySession, &sid, &sattrs);
2858+ if (err)
2859+ error("SessionGetInfo() failed with error %.8X",
2860+ (unsigned) err);
2861+ else
2862+ debug("Current Session ID is %.8X / Session Attributes are %.8X",
2863+ (unsigned) sid, (unsigned) sattrs);
2864+
2865+ if (inetd_flag && !(sattrs & sessionIsRoot))
2866+ debug("Running in inetd mode in a non-root session... "
2867+ "assuming inetd created the session for us.");
2868+ else {
2869+ debug("Creating new security session...");
2870+ err = SessionCreate(0, sessionHasTTY | sessionIsRemote);
2871+ if (err)
2872+ error("SessionCreate() failed with error %.8X",
2873+ (unsigned) err);
2874+
2875+ err = SessionGetInfo(callerSecuritySession, &sid,
2876+ &sattrs);
2877+ if (err)
2878+ error("SessionGetInfo() failed with error %.8X",
2879+ (unsigned) err);
2880+ else
2881+ debug("New Session ID is %.8X / Session Attributes are %.8X",
2882+ (unsigned) sid, (unsigned) sattrs);
2883+ }
2884+ }
2885+#endif
2886+
2887 /*
2888 * We don't want to listen forever unless the other side
2889 * successfully authenticates itself. So we set up an alarm which is
2890@@ -2571,6 +2632,48 @@ do_ssh2_kex(void)
2891 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
2892 list_hostkey_types());
2893
2894+#ifdef GSSAPI
2895+ {
2896+ char *orig;
2897+ char *gss = NULL;
2898+ char *newstr = NULL;
2899+ orig = myproposal[PROPOSAL_KEX_ALGS];
2900+
2901+ /*
2902+ * If we don't have a host key, then there's no point advertising
2903+ * the other key exchange algorithms
2904+ */
2905+
2906+ if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
2907+ orig = NULL;
2908+
2909+ if (options.gss_keyex)
2910+ gss = ssh_gssapi_server_mechanisms();
2911+ else
2912+ gss = NULL;
2913+
2914+ if (gss && orig)
2915+ xasprintf(&newstr, "%s,%s", gss, orig);
2916+ else if (gss)
2917+ newstr = gss;
2918+ else if (orig)
2919+ newstr = orig;
2920+
2921+ /*
2922+ * If we've got GSSAPI mechanisms, then we've got the 'null' host
2923+ * key alg, but we can't tell people about it unless its the only
2924+ * host key algorithm we support
2925+ */
2926+ if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0)
2927+ myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = "null";
2928+
2929+ if (newstr)
2930+ myproposal[PROPOSAL_KEX_ALGS] = newstr;
2931+ else
2932+ fatal("No supported key exchange algorithms");
2933+ }
2934+#endif
2935+
2936 /* start key exchange */
2937 if ((r = kex_setup(active_state, myproposal)) != 0)
2938 fatal("kex_setup: %s", ssh_err(r));
2939@@ -2585,6 +2688,13 @@ do_ssh2_kex(void)
2940 # endif
2941 #endif
2942 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
2943+#ifdef GSSAPI
2944+ if (options.gss_keyex) {
2945+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
2946+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
2947+ kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
2948+ }
2949+#endif
2950 kex->server = 1;
2951 kex->client_version_string=client_version_string;
2952 kex->server_version_string=server_version_string;
2953diff --git a/sshd_config b/sshd_config
2954index a848d73..f103298 100644
2955--- a/sshd_config
2956+++ b/sshd_config
2957@@ -84,6 +84,8 @@ AuthorizedKeysFile .ssh/authorized_keys
2958 # GSSAPI options
2959 #GSSAPIAuthentication no
2960 #GSSAPICleanupCredentials yes
2961+#GSSAPIStrictAcceptorCheck yes
2962+#GSSAPIKeyExchange no
2963
2964 # Set this to 'yes' to enable PAM authentication, account processing,
2965 # and session processing. If this is enabled, PAM authentication will
2966diff --git a/sshd_config.5 b/sshd_config.5
2967index a37a3ac..c6d6858 100644
2968--- a/sshd_config.5
2969+++ b/sshd_config.5
2970@@ -623,6 +623,11 @@ The default is
2971 Specifies whether user authentication based on GSSAPI is allowed.
2972 The default is
2973 .Dq no .
2974+.It Cm GSSAPIKeyExchange
2975+Specifies whether key exchange based on GSSAPI is allowed. GSSAPI key exchange
2976+doesn't rely on ssh keys to verify host identity.
2977+The default is
2978+.Dq no .
2979 .It Cm GSSAPICleanupCredentials
2980 Specifies whether to automatically destroy the user's credentials cache
2981 on logout.
2982@@ -643,6 +648,11 @@ machine's default store.
2983 This facility is provided to assist with operation on multi homed machines.
2984 The default is
2985 .Dq yes .
2986+.It Cm GSSAPIStoreCredentialsOnRekey
2987+Controls whether the user's GSSAPI credentials should be updated following a
2988+successful connection rekeying. This option can be used to accepted renewed
2989+or updated credentials from a compatible client. The default is
2990+.Dq no .
2991 .It Cm HostbasedAcceptedKeyTypes
2992 Specifies the key types that will be accepted for hostbased authentication
2993 as a comma-separated pattern list.
2994diff --git a/sshkey.c b/sshkey.c
2995index 87b093e..e595b11 100644
2996--- a/sshkey.c
2997+++ b/sshkey.c
2998@@ -115,6 +115,7 @@ static const struct keytype keytypes[] = {
2999 # endif /* OPENSSL_HAS_NISTP521 */
3000 # endif /* OPENSSL_HAS_ECC */
3001 #endif /* WITH_OPENSSL */
3002+ { "null", "null", KEY_NULL, 0, 0, 0 },
3003 { NULL, NULL, -1, -1, 0, 0 }
3004 };
3005
3006@@ -203,7 +204,7 @@ key_alg_list(int certs_only, int plain_only)
3007 const struct keytype *kt;
3008
3009 for (kt = keytypes; kt->type != -1; kt++) {
3010- if (kt->name == NULL || kt->sigonly)
3011+ if (kt->name == NULL || kt->sigonly || kt->type == KEY_NULL)
3012 continue;
3013 if ((certs_only && !kt->cert) || (plain_only && kt->cert))
3014 continue;
3015diff --git a/sshkey.h b/sshkey.h
3016index a20a14f..2259cbb 100644
3017--- a/sshkey.h
3018+++ b/sshkey.h
3019@@ -62,6 +62,7 @@ enum sshkey_types {
3020 KEY_DSA_CERT,
3021 KEY_ECDSA_CERT,
3022 KEY_ED25519_CERT,
3023+ KEY_NULL,
3024 KEY_UNSPEC
3025 };
3026