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