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.patch3085
1 files changed, 3085 insertions, 0 deletions
diff --git a/debian/patches/gssapi.patch b/debian/patches/gssapi.patch
new file mode 100644
index 000000000..b3c437194
--- /dev/null
+++ b/debian/patches/gssapi.patch
@@ -0,0 +1,3085 @@
1From 06879e71614170580ffa7568ec5c009f60a9d084 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-08-19
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 | 221 +++++++++++++++++++++++++++++++-----
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 | 38 ++++++-
46 servconf.h | 3 +
47 ssh-gss.h | 41 ++++++-
48 ssh_config | 2 +
49 ssh_config.5 | 34 +++++-
50 sshconnect2.c | 124 +++++++++++++++++++-
51 sshd.c | 110 ++++++++++++++++++
52 sshd_config | 2 +
53 sshd_config.5 | 28 +++++
54 sshkey.c | 3 +-
55 sshkey.h | 1 +
56 32 files changed, 2005 insertions(+), 60 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 a9c8a90..7df9413 100644
363--- a/clientloop.c
364+++ b/clientloop.c
365@@ -114,6 +114,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@@ -1596,6 +1600,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 7e7e38e..6c7de98 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 b4d6598..216a9fd 100644
418--- a/configure.ac
419+++ b/configure.ac
420@@ -620,6 +620,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 60ac65f..5610f0b 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@@ -40,12 +40,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@@ -198,7 +353,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@@ -228,8 +383,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@@ -237,6 +426,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@@ -250,11 +452,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@@ -264,6 +471,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@@ -273,10 +484,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 e7b8c52..539862d 100644
929--- a/gss-serv.c
930+++ b/gss-serv.c
931@@ -1,7 +1,7 @@
932 /* $OpenBSD: gss-serv.c,v 1.28 2015/01/20 23:14:00 deraadt 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@@ -44,15 +44,21 @@
941 #include "channels.h"
942 #include "session.h"
943 #include "misc.h"
944+#include "servconf.h"
945+#include "uidswap.h"
946
947 #include "ssh-gss.h"
948+#include "monitor_wrap.h"
949+
950+extern ServerOptions options;
951
952 static ssh_gssapi_client gssapi_client =
953 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
954- GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
955+ GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, NULL,
956+ {NULL, NULL, NULL, NULL, NULL}, 0, 0};
957
958 ssh_gssapi_mech gssapi_null_mech =
959- { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
960+ { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
961
962 #ifdef KRB5
963 extern ssh_gssapi_mech gssapi_kerberos_mech;
964@@ -99,25 +105,32 @@ ssh_gssapi_acquire_cred(Gssctxt *ctx)
965 char lname[NI_MAXHOST];
966 gss_OID_set oidset;
967
968- gss_create_empty_oid_set(&status, &oidset);
969- gss_add_oid_set_member(&status, ctx->oid, &oidset);
970+ if (options.gss_strict_acceptor) {
971+ gss_create_empty_oid_set(&status, &oidset);
972+ gss_add_oid_set_member(&status, ctx->oid, &oidset);
973
974- if (gethostname(lname, sizeof(lname))) {
975- gss_release_oid_set(&status, &oidset);
976- return (-1);
977- }
978+ if (gethostname(lname, sizeof(lname))) {
979+ gss_release_oid_set(&status, &oidset);
980+ return (-1);
981+ }
982+
983+ if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
984+ gss_release_oid_set(&status, &oidset);
985+ return (ctx->major);
986+ }
987+
988+ if ((ctx->major = gss_acquire_cred(&ctx->minor,
989+ ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds,
990+ NULL, NULL)))
991+ ssh_gssapi_error(ctx);
992
993- if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
994 gss_release_oid_set(&status, &oidset);
995 return (ctx->major);
996+ } else {
997+ ctx->name = GSS_C_NO_NAME;
998+ ctx->creds = GSS_C_NO_CREDENTIAL;
999 }
1000-
1001- if ((ctx->major = gss_acquire_cred(&ctx->minor,
1002- ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
1003- ssh_gssapi_error(ctx);
1004-
1005- gss_release_oid_set(&status, &oidset);
1006- return (ctx->major);
1007+ return GSS_S_COMPLETE;
1008 }
1009
1010 /* Privileged */
1011@@ -132,6 +145,29 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
1012 }
1013
1014 /* Unprivileged */
1015+char *
1016+ssh_gssapi_server_mechanisms(void) {
1017+ gss_OID_set supported;
1018+
1019+ ssh_gssapi_supported_oids(&supported);
1020+ return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
1021+ NULL, NULL));
1022+}
1023+
1024+/* Unprivileged */
1025+int
1026+ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
1027+ const char *dummy) {
1028+ Gssctxt *ctx = NULL;
1029+ int res;
1030+
1031+ res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
1032+ ssh_gssapi_delete_ctx(&ctx);
1033+
1034+ return (res);
1035+}
1036+
1037+/* Unprivileged */
1038 void
1039 ssh_gssapi_supported_oids(gss_OID_set *oidset)
1040 {
1041@@ -141,7 +177,9 @@ ssh_gssapi_supported_oids(gss_OID_set *oidset)
1042 gss_OID_set supported;
1043
1044 gss_create_empty_oid_set(&min_status, oidset);
1045- gss_indicate_mechs(&min_status, &supported);
1046+
1047+ if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
1048+ return;
1049
1050 while (supported_mechs[i]->name != NULL) {
1051 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
1052@@ -267,8 +305,48 @@ OM_uint32
1053 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1054 {
1055 int i = 0;
1056+ int equal = 0;
1057+ gss_name_t new_name = GSS_C_NO_NAME;
1058+ gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
1059+
1060+ if (options.gss_store_rekey && client->used && ctx->client_creds) {
1061+ if (client->mech->oid.length != ctx->oid->length ||
1062+ (memcmp(client->mech->oid.elements,
1063+ ctx->oid->elements, ctx->oid->length) !=0)) {
1064+ debug("Rekeyed credentials have different mechanism");
1065+ return GSS_S_COMPLETE;
1066+ }
1067+
1068+ if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1069+ ctx->client_creds, ctx->oid, &new_name,
1070+ NULL, NULL, NULL))) {
1071+ ssh_gssapi_error(ctx);
1072+ return (ctx->major);
1073+ }
1074+
1075+ ctx->major = gss_compare_name(&ctx->minor, client->name,
1076+ new_name, &equal);
1077
1078- gss_buffer_desc ename;
1079+ if (GSS_ERROR(ctx->major)) {
1080+ ssh_gssapi_error(ctx);
1081+ return (ctx->major);
1082+ }
1083+
1084+ if (!equal) {
1085+ debug("Rekeyed credentials have different name");
1086+ return GSS_S_COMPLETE;
1087+ }
1088+
1089+ debug("Marking rekeyed credentials for export");
1090+
1091+ gss_release_name(&ctx->minor, &client->name);
1092+ gss_release_cred(&ctx->minor, &client->creds);
1093+ client->name = new_name;
1094+ client->creds = ctx->client_creds;
1095+ ctx->client_creds = GSS_C_NO_CREDENTIAL;
1096+ client->updated = 1;
1097+ return GSS_S_COMPLETE;
1098+ }
1099
1100 client->mech = NULL;
1101
1102@@ -283,6 +361,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1103 if (client->mech == NULL)
1104 return GSS_S_FAILURE;
1105
1106+ if (ctx->client_creds &&
1107+ (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1108+ ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
1109+ ssh_gssapi_error(ctx);
1110+ return (ctx->major);
1111+ }
1112+
1113 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
1114 &client->displayname, NULL))) {
1115 ssh_gssapi_error(ctx);
1116@@ -300,6 +385,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1117 return (ctx->major);
1118 }
1119
1120+ gss_release_buffer(&ctx->minor, &ename);
1121+
1122 /* We can't copy this structure, so we just move the pointer to it */
1123 client->creds = ctx->client_creds;
1124 ctx->client_creds = GSS_C_NO_CREDENTIAL;
1125@@ -347,7 +434,7 @@ ssh_gssapi_do_child(char ***envp, u_int *envsizep)
1126
1127 /* Privileged */
1128 int
1129-ssh_gssapi_userok(char *user)
1130+ssh_gssapi_userok(char *user, struct passwd *pw)
1131 {
1132 OM_uint32 lmin;
1133
1134@@ -357,9 +444,11 @@ ssh_gssapi_userok(char *user)
1135 return 0;
1136 }
1137 if (gssapi_client.mech && gssapi_client.mech->userok)
1138- if ((*gssapi_client.mech->userok)(&gssapi_client, user))
1139+ if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
1140+ gssapi_client.used = 1;
1141+ gssapi_client.store.owner = pw;
1142 return 1;
1143- else {
1144+ } else {
1145 /* Destroy delegated credentials if userok fails */
1146 gss_release_buffer(&lmin, &gssapi_client.displayname);
1147 gss_release_buffer(&lmin, &gssapi_client.exportedname);
1148@@ -373,14 +462,90 @@ ssh_gssapi_userok(char *user)
1149 return (0);
1150 }
1151
1152-/* Privileged */
1153-OM_uint32
1154-ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1155+/* These bits are only used for rekeying. The unpriviledged child is running
1156+ * as the user, the monitor is root.
1157+ *
1158+ * In the child, we want to :
1159+ * *) Ask the monitor to store our credentials into the store we specify
1160+ * *) If it succeeds, maybe do a PAM update
1161+ */
1162+
1163+/* Stuff for PAM */
1164+
1165+#ifdef USE_PAM
1166+static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg,
1167+ struct pam_response **resp, void *data)
1168 {
1169- ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
1170- gssbuf, gssmic, NULL);
1171+ return (PAM_CONV_ERR);
1172+}
1173+#endif
1174
1175- return (ctx->major);
1176+void
1177+ssh_gssapi_rekey_creds(void) {
1178+ int ok;
1179+ int ret;
1180+#ifdef USE_PAM
1181+ pam_handle_t *pamh = NULL;
1182+ struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
1183+ char *envstr;
1184+#endif
1185+
1186+ if (gssapi_client.store.filename == NULL &&
1187+ gssapi_client.store.envval == NULL &&
1188+ gssapi_client.store.envvar == NULL)
1189+ return;
1190+
1191+ ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
1192+
1193+ if (!ok)
1194+ return;
1195+
1196+ debug("Rekeyed credentials stored successfully");
1197+
1198+ /* Actually managing to play with the ssh pam stack from here will
1199+ * be next to impossible. In any case, we may want different options
1200+ * for rekeying. So, use our own :)
1201+ */
1202+#ifdef USE_PAM
1203+ if (!use_privsep) {
1204+ debug("Not even going to try and do PAM with privsep disabled");
1205+ return;
1206+ }
1207+
1208+ ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
1209+ &pamconv, &pamh);
1210+ if (ret)
1211+ return;
1212+
1213+ xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar,
1214+ gssapi_client.store.envval);
1215+
1216+ ret = pam_putenv(pamh, envstr);
1217+ if (!ret)
1218+ pam_setcred(pamh, PAM_REINITIALIZE_CRED);
1219+ pam_end(pamh, PAM_SUCCESS);
1220+#endif
1221+}
1222+
1223+int
1224+ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
1225+ int ok = 0;
1226+
1227+ /* Check we've got credentials to store */
1228+ if (!gssapi_client.updated)
1229+ return 0;
1230+
1231+ gssapi_client.updated = 0;
1232+
1233+ temporarily_use_uid(gssapi_client.store.owner);
1234+ if (gssapi_client.mech && gssapi_client.mech->updatecreds)
1235+ ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
1236+ else
1237+ debug("No update function for this mechanism");
1238+
1239+ restore_uid();
1240+
1241+ return ok;
1242 }
1243
1244 #endif
1245diff --git a/kex.c b/kex.c
1246index 8c2b001..be938ad 100644
1247--- a/kex.c
1248+++ b/kex.c
1249@@ -55,6 +55,10 @@
1250 #include "sshbuf.h"
1251 #include "digest.h"
1252
1253+#ifdef GSSAPI
1254+#include "ssh-gss.h"
1255+#endif
1256+
1257 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
1258 # if defined(HAVE_EVP_SHA256)
1259 # define evp_ssh_sha256 EVP_sha256
1260@@ -97,6 +101,14 @@ static const struct kexalg kexalgs[] = {
1261 #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
1262 { NULL, -1, -1, -1},
1263 };
1264+static const struct kexalg kexalg_prefixes[] = {
1265+#ifdef GSSAPI
1266+ { KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
1267+ { KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
1268+ { KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
1269+#endif
1270+ { NULL, -1, -1, -1 },
1271+};
1272
1273 char *
1274 kex_alg_list(char sep)
1275@@ -129,6 +141,10 @@ kex_alg_by_name(const char *name)
1276 if (strcmp(k->name, name) == 0)
1277 return k;
1278 }
1279+ for (k = kexalg_prefixes; k->name != NULL; k++) {
1280+ if (strncmp(k->name, name, strlen(k->name)) == 0)
1281+ return k;
1282+ }
1283 return NULL;
1284 }
1285
1286diff --git a/kex.h b/kex.h
1287index f70b81f..7194b14 100644
1288--- a/kex.h
1289+++ b/kex.h
1290@@ -93,6 +93,9 @@ enum kex_exchange {
1291 KEX_DH_GEX_SHA256,
1292 KEX_ECDH_SHA2,
1293 KEX_C25519_SHA256,
1294+ KEX_GSS_GRP1_SHA1,
1295+ KEX_GSS_GRP14_SHA1,
1296+ KEX_GSS_GEX_SHA1,
1297 KEX_MAX
1298 };
1299
1300@@ -139,6 +142,12 @@ struct kex {
1301 u_int flags;
1302 int hash_alg;
1303 int ec_nid;
1304+#ifdef GSSAPI
1305+ int gss_deleg_creds;
1306+ int gss_trust_dns;
1307+ char *gss_host;
1308+ char *gss_client;
1309+#endif
1310 char *client_version_string;
1311 char *server_version_string;
1312 int (*verify_host_key)(struct sshkey *, struct ssh *);
1313@@ -184,6 +193,11 @@ int kexecdh_server(struct ssh *);
1314 int kexc25519_client(struct ssh *);
1315 int kexc25519_server(struct ssh *);
1316
1317+#ifdef GSSAPI
1318+int kexgss_client(struct ssh *);
1319+int kexgss_server(struct ssh *);
1320+#endif
1321+
1322 int kex_dh_hash(const char *, const char *,
1323 const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
1324 const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
1325diff --git a/kexgssc.c b/kexgssc.c
1326new file mode 100644
1327index 0000000..a49bac2
1328--- /dev/null
1329+++ b/kexgssc.c
1330@@ -0,0 +1,336 @@
1331+/*
1332+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1333+ *
1334+ * Redistribution and use in source and binary forms, with or without
1335+ * modification, are permitted provided that the following conditions
1336+ * are met:
1337+ * 1. Redistributions of source code must retain the above copyright
1338+ * notice, this list of conditions and the following disclaimer.
1339+ * 2. Redistributions in binary form must reproduce the above copyright
1340+ * notice, this list of conditions and the following disclaimer in the
1341+ * documentation and/or other materials provided with the distribution.
1342+ *
1343+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
1344+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1345+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1346+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1347+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1348+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1349+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1350+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1351+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1352+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1353+ */
1354+
1355+#include "includes.h"
1356+
1357+#ifdef GSSAPI
1358+
1359+#include "includes.h"
1360+
1361+#include <openssl/crypto.h>
1362+#include <openssl/bn.h>
1363+
1364+#include <string.h>
1365+
1366+#include "xmalloc.h"
1367+#include "buffer.h"
1368+#include "ssh2.h"
1369+#include "key.h"
1370+#include "cipher.h"
1371+#include "kex.h"
1372+#include "log.h"
1373+#include "packet.h"
1374+#include "dh.h"
1375+#include "digest.h"
1376+
1377+#include "ssh-gss.h"
1378+
1379+int
1380+kexgss_client(struct ssh *ssh) {
1381+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1382+ gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
1383+ Gssctxt *ctxt;
1384+ OM_uint32 maj_status, min_status, ret_flags;
1385+ u_int klen, kout, slen = 0, strlen;
1386+ DH *dh;
1387+ BIGNUM *dh_server_pub = NULL;
1388+ BIGNUM *shared_secret = NULL;
1389+ BIGNUM *p = NULL;
1390+ BIGNUM *g = NULL;
1391+ u_char *kbuf;
1392+ u_char *serverhostkey = NULL;
1393+ u_char *empty = "";
1394+ char *msg;
1395+ int type = 0;
1396+ int first = 1;
1397+ int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
1398+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1399+ size_t hashlen;
1400+
1401+ /* Initialise our GSSAPI world */
1402+ ssh_gssapi_build_ctx(&ctxt);
1403+ if (ssh_gssapi_id_kex(ctxt, ssh->kex->name, ssh->kex->kex_type)
1404+ == GSS_C_NO_OID)
1405+ fatal("Couldn't identify host exchange");
1406+
1407+ if (ssh_gssapi_import_name(ctxt, ssh->kex->gss_host))
1408+ fatal("Couldn't import hostname");
1409+
1410+ if (ssh->kex->gss_client &&
1411+ ssh_gssapi_client_identity(ctxt, ssh->kex->gss_client))
1412+ fatal("Couldn't acquire client credentials");
1413+
1414+ switch (ssh->kex->kex_type) {
1415+ case KEX_GSS_GRP1_SHA1:
1416+ dh = dh_new_group1();
1417+ break;
1418+ case KEX_GSS_GRP14_SHA1:
1419+ dh = dh_new_group14();
1420+ break;
1421+ case KEX_GSS_GEX_SHA1:
1422+ debug("Doing group exchange\n");
1423+ nbits = dh_estimate(ssh->kex->we_need * 8);
1424+ packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
1425+ packet_put_int(min);
1426+ packet_put_int(nbits);
1427+ packet_put_int(max);
1428+
1429+ packet_send();
1430+
1431+ packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
1432+
1433+ if ((p = BN_new()) == NULL)
1434+ fatal("BN_new() failed");
1435+ packet_get_bignum2(p);
1436+ if ((g = BN_new()) == NULL)
1437+ fatal("BN_new() failed");
1438+ packet_get_bignum2(g);
1439+ packet_check_eom();
1440+
1441+ if (BN_num_bits(p) < min || BN_num_bits(p) > max)
1442+ fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
1443+ min, BN_num_bits(p), max);
1444+
1445+ dh = dh_new_group(g, p);
1446+ break;
1447+ default:
1448+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1449+ }
1450+
1451+ /* Step 1 - e is dh->pub_key */
1452+ dh_gen_key(dh, ssh->kex->we_need * 8);
1453+
1454+ /* This is f, we initialise it now to make life easier */
1455+ dh_server_pub = BN_new();
1456+ if (dh_server_pub == NULL)
1457+ fatal("dh_server_pub == NULL");
1458+
1459+ token_ptr = GSS_C_NO_BUFFER;
1460+
1461+ do {
1462+ debug("Calling gss_init_sec_context");
1463+
1464+ maj_status = ssh_gssapi_init_ctx(ctxt,
1465+ ssh->kex->gss_deleg_creds, token_ptr, &send_tok,
1466+ &ret_flags);
1467+
1468+ if (GSS_ERROR(maj_status)) {
1469+ if (send_tok.length != 0) {
1470+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1471+ packet_put_string(send_tok.value,
1472+ send_tok.length);
1473+ }
1474+ fatal("gss_init_context failed");
1475+ }
1476+
1477+ /* If we've got an old receive buffer get rid of it */
1478+ if (token_ptr != GSS_C_NO_BUFFER)
1479+ free(recv_tok.value);
1480+
1481+ if (maj_status == GSS_S_COMPLETE) {
1482+ /* If mutual state flag is not true, kex fails */
1483+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1484+ fatal("Mutual authentication failed");
1485+
1486+ /* If integ avail flag is not true kex fails */
1487+ if (!(ret_flags & GSS_C_INTEG_FLAG))
1488+ fatal("Integrity check failed");
1489+ }
1490+
1491+ /*
1492+ * If we have data to send, then the last message that we
1493+ * received cannot have been a 'complete'.
1494+ */
1495+ if (send_tok.length != 0) {
1496+ if (first) {
1497+ packet_start(SSH2_MSG_KEXGSS_INIT);
1498+ packet_put_string(send_tok.value,
1499+ send_tok.length);
1500+ packet_put_bignum2(dh->pub_key);
1501+ first = 0;
1502+ } else {
1503+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1504+ packet_put_string(send_tok.value,
1505+ send_tok.length);
1506+ }
1507+ packet_send();
1508+ gss_release_buffer(&min_status, &send_tok);
1509+
1510+ /* If we've sent them data, they should reply */
1511+ do {
1512+ type = packet_read();
1513+ if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
1514+ debug("Received KEXGSS_HOSTKEY");
1515+ if (serverhostkey)
1516+ fatal("Server host key received more than once");
1517+ serverhostkey =
1518+ packet_get_string(&slen);
1519+ }
1520+ } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
1521+
1522+ switch (type) {
1523+ case SSH2_MSG_KEXGSS_CONTINUE:
1524+ debug("Received GSSAPI_CONTINUE");
1525+ if (maj_status == GSS_S_COMPLETE)
1526+ fatal("GSSAPI Continue received from server when complete");
1527+ recv_tok.value = packet_get_string(&strlen);
1528+ recv_tok.length = strlen;
1529+ break;
1530+ case SSH2_MSG_KEXGSS_COMPLETE:
1531+ debug("Received GSSAPI_COMPLETE");
1532+ packet_get_bignum2(dh_server_pub);
1533+ msg_tok.value = packet_get_string(&strlen);
1534+ msg_tok.length = strlen;
1535+
1536+ /* Is there a token included? */
1537+ if (packet_get_char()) {
1538+ recv_tok.value=
1539+ packet_get_string(&strlen);
1540+ recv_tok.length = strlen;
1541+ /* If we're already complete - protocol error */
1542+ if (maj_status == GSS_S_COMPLETE)
1543+ packet_disconnect("Protocol error: received token when complete");
1544+ } else {
1545+ /* No token included */
1546+ if (maj_status != GSS_S_COMPLETE)
1547+ packet_disconnect("Protocol error: did not receive final token");
1548+ }
1549+ break;
1550+ case SSH2_MSG_KEXGSS_ERROR:
1551+ debug("Received Error");
1552+ maj_status = packet_get_int();
1553+ min_status = packet_get_int();
1554+ msg = packet_get_string(NULL);
1555+ (void) packet_get_string_ptr(NULL);
1556+ fatal("GSSAPI Error: \n%.400s",msg);
1557+ default:
1558+ packet_disconnect("Protocol error: didn't expect packet type %d",
1559+ type);
1560+ }
1561+ token_ptr = &recv_tok;
1562+ } else {
1563+ /* No data, and not complete */
1564+ if (maj_status != GSS_S_COMPLETE)
1565+ fatal("Not complete, and no token output");
1566+ }
1567+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
1568+
1569+ /*
1570+ * We _must_ have received a COMPLETE message in reply from the
1571+ * server, which will have set dh_server_pub and msg_tok
1572+ */
1573+
1574+ if (type != SSH2_MSG_KEXGSS_COMPLETE)
1575+ fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
1576+
1577+ /* Check f in range [1, p-1] */
1578+ if (!dh_pub_is_valid(dh, dh_server_pub))
1579+ packet_disconnect("bad server public DH value");
1580+
1581+ /* compute K=f^x mod p */
1582+ klen = DH_size(dh);
1583+ kbuf = xmalloc(klen);
1584+ kout = DH_compute_key(kbuf, dh_server_pub, dh);
1585+ if (kout < 0)
1586+ fatal("DH_compute_key: failed");
1587+
1588+ shared_secret = BN_new();
1589+ if (shared_secret == NULL)
1590+ fatal("kexgss_client: BN_new failed");
1591+
1592+ if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
1593+ fatal("kexdh_client: BN_bin2bn failed");
1594+
1595+ memset(kbuf, 0, klen);
1596+ free(kbuf);
1597+
1598+ hashlen = sizeof(hash);
1599+ switch (ssh->kex->kex_type) {
1600+ case KEX_GSS_GRP1_SHA1:
1601+ case KEX_GSS_GRP14_SHA1:
1602+ kex_dh_hash( ssh->kex->client_version_string,
1603+ ssh->kex->server_version_string,
1604+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1605+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1606+ (serverhostkey ? serverhostkey : empty), slen,
1607+ dh->pub_key, /* e */
1608+ dh_server_pub, /* f */
1609+ shared_secret, /* K */
1610+ hash, &hashlen
1611+ );
1612+ break;
1613+ case KEX_GSS_GEX_SHA1:
1614+ kexgex_hash(
1615+ ssh->kex->hash_alg,
1616+ ssh->kex->client_version_string,
1617+ ssh->kex->server_version_string,
1618+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1619+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1620+ (serverhostkey ? serverhostkey : empty), slen,
1621+ min, nbits, max,
1622+ dh->p, dh->g,
1623+ dh->pub_key,
1624+ dh_server_pub,
1625+ shared_secret,
1626+ hash, &hashlen
1627+ );
1628+ break;
1629+ default:
1630+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1631+ }
1632+
1633+ gssbuf.value = hash;
1634+ gssbuf.length = hashlen;
1635+
1636+ /* Verify that the hash matches the MIC we just got. */
1637+ if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
1638+ packet_disconnect("Hash's MIC didn't verify");
1639+
1640+ free(msg_tok.value);
1641+
1642+ DH_free(dh);
1643+ free(serverhostkey);
1644+ BN_clear_free(dh_server_pub);
1645+
1646+ /* save session id */
1647+ if (ssh->kex->session_id == NULL) {
1648+ ssh->kex->session_id_len = hashlen;
1649+ ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
1650+ memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
1651+ }
1652+
1653+ if (ssh->kex->gss_deleg_creds)
1654+ ssh_gssapi_credentials_updated(ctxt);
1655+
1656+ if (gss_kex_context == NULL)
1657+ gss_kex_context = ctxt;
1658+ else
1659+ ssh_gssapi_delete_ctx(&ctxt);
1660+
1661+ kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
1662+ BN_clear_free(shared_secret);
1663+ return kex_send_newkeys(ssh);
1664+}
1665+
1666+#endif /* GSSAPI */
1667diff --git a/kexgsss.c b/kexgsss.c
1668new file mode 100644
1669index 0000000..0847469
1670--- /dev/null
1671+++ b/kexgsss.c
1672@@ -0,0 +1,295 @@
1673+/*
1674+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1675+ *
1676+ * Redistribution and use in source and binary forms, with or without
1677+ * modification, are permitted provided that the following conditions
1678+ * are met:
1679+ * 1. Redistributions of source code must retain the above copyright
1680+ * notice, this list of conditions and the following disclaimer.
1681+ * 2. Redistributions in binary form must reproduce the above copyright
1682+ * notice, this list of conditions and the following disclaimer in the
1683+ * documentation and/or other materials provided with the distribution.
1684+ *
1685+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
1686+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1687+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1688+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1689+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1690+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1691+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1692+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1693+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1694+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1695+ */
1696+
1697+#include "includes.h"
1698+
1699+#ifdef GSSAPI
1700+
1701+#include <string.h>
1702+
1703+#include <openssl/crypto.h>
1704+#include <openssl/bn.h>
1705+
1706+#include "xmalloc.h"
1707+#include "buffer.h"
1708+#include "ssh2.h"
1709+#include "key.h"
1710+#include "cipher.h"
1711+#include "kex.h"
1712+#include "log.h"
1713+#include "packet.h"
1714+#include "dh.h"
1715+#include "ssh-gss.h"
1716+#include "monitor_wrap.h"
1717+#include "misc.h"
1718+#include "servconf.h"
1719+#include "digest.h"
1720+
1721+extern ServerOptions options;
1722+
1723+int
1724+kexgss_server(struct ssh *ssh)
1725+{
1726+ OM_uint32 maj_status, min_status;
1727+
1728+ /*
1729+ * Some GSSAPI implementations use the input value of ret_flags (an
1730+ * output variable) as a means of triggering mechanism specific
1731+ * features. Initializing it to zero avoids inadvertently
1732+ * activating this non-standard behaviour.
1733+ */
1734+
1735+ OM_uint32 ret_flags = 0;
1736+ gss_buffer_desc gssbuf, recv_tok, msg_tok;
1737+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1738+ Gssctxt *ctxt = NULL;
1739+ u_int slen, klen, kout;
1740+ u_char *kbuf;
1741+ DH *dh;
1742+ int min = -1, max = -1, nbits = -1;
1743+ BIGNUM *shared_secret = NULL;
1744+ BIGNUM *dh_client_pub = NULL;
1745+ int type = 0;
1746+ gss_OID oid;
1747+ char *mechs;
1748+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1749+ size_t hashlen;
1750+
1751+ /* Initialise GSSAPI */
1752+
1753+ /* If we're rekeying, privsep means that some of the private structures
1754+ * in the GSSAPI code are no longer available. This kludges them back
1755+ * into life
1756+ */
1757+ if (!ssh_gssapi_oid_table_ok()) {
1758+ mechs = ssh_gssapi_server_mechanisms();
1759+ free(mechs);
1760+ }
1761+
1762+ debug2("%s: Identifying %s", __func__, ssh->kex->name);
1763+ oid = ssh_gssapi_id_kex(NULL, ssh->kex->name, ssh->kex->kex_type);
1764+ if (oid == GSS_C_NO_OID)
1765+ fatal("Unknown gssapi mechanism");
1766+
1767+ debug2("%s: Acquiring credentials", __func__);
1768+
1769+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
1770+ fatal("Unable to acquire credentials for the server");
1771+
1772+ switch (ssh->kex->kex_type) {
1773+ case KEX_GSS_GRP1_SHA1:
1774+ dh = dh_new_group1();
1775+ break;
1776+ case KEX_GSS_GRP14_SHA1:
1777+ dh = dh_new_group14();
1778+ break;
1779+ case KEX_GSS_GEX_SHA1:
1780+ debug("Doing group exchange");
1781+ packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
1782+ min = packet_get_int();
1783+ nbits = packet_get_int();
1784+ max = packet_get_int();
1785+ min = MAX(DH_GRP_MIN, min);
1786+ max = MIN(DH_GRP_MAX, max);
1787+ packet_check_eom();
1788+ if (max < min || nbits < min || max < nbits)
1789+ fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
1790+ min, nbits, max);
1791+ dh = PRIVSEP(choose_dh(min, nbits, max));
1792+ if (dh == NULL)
1793+ packet_disconnect("Protocol error: no matching group found");
1794+
1795+ packet_start(SSH2_MSG_KEXGSS_GROUP);
1796+ packet_put_bignum2(dh->p);
1797+ packet_put_bignum2(dh->g);
1798+ packet_send();
1799+
1800+ packet_write_wait();
1801+ break;
1802+ default:
1803+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1804+ }
1805+
1806+ dh_gen_key(dh, ssh->kex->we_need * 8);
1807+
1808+ do {
1809+ debug("Wait SSH2_MSG_GSSAPI_INIT");
1810+ type = packet_read();
1811+ switch(type) {
1812+ case SSH2_MSG_KEXGSS_INIT:
1813+ if (dh_client_pub != NULL)
1814+ fatal("Received KEXGSS_INIT after initialising");
1815+ recv_tok.value = packet_get_string(&slen);
1816+ recv_tok.length = slen;
1817+
1818+ if ((dh_client_pub = BN_new()) == NULL)
1819+ fatal("dh_client_pub == NULL");
1820+
1821+ packet_get_bignum2(dh_client_pub);
1822+
1823+ /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
1824+ break;
1825+ case SSH2_MSG_KEXGSS_CONTINUE:
1826+ recv_tok.value = packet_get_string(&slen);
1827+ recv_tok.length = slen;
1828+ break;
1829+ default:
1830+ packet_disconnect(
1831+ "Protocol error: didn't expect packet type %d",
1832+ type);
1833+ }
1834+
1835+ maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
1836+ &send_tok, &ret_flags));
1837+
1838+ free(recv_tok.value);
1839+
1840+ if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
1841+ fatal("Zero length token output when incomplete");
1842+
1843+ if (dh_client_pub == NULL)
1844+ fatal("No client public key");
1845+
1846+ if (maj_status & GSS_S_CONTINUE_NEEDED) {
1847+ debug("Sending GSSAPI_CONTINUE");
1848+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1849+ packet_put_string(send_tok.value, send_tok.length);
1850+ packet_send();
1851+ gss_release_buffer(&min_status, &send_tok);
1852+ }
1853+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
1854+
1855+ if (GSS_ERROR(maj_status)) {
1856+ if (send_tok.length > 0) {
1857+ packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1858+ packet_put_string(send_tok.value, send_tok.length);
1859+ packet_send();
1860+ }
1861+ fatal("accept_ctx died");
1862+ }
1863+
1864+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1865+ fatal("Mutual Authentication flag wasn't set");
1866+
1867+ if (!(ret_flags & GSS_C_INTEG_FLAG))
1868+ fatal("Integrity flag wasn't set");
1869+
1870+ if (!dh_pub_is_valid(dh, dh_client_pub))
1871+ packet_disconnect("bad client public DH value");
1872+
1873+ klen = DH_size(dh);
1874+ kbuf = xmalloc(klen);
1875+ kout = DH_compute_key(kbuf, dh_client_pub, dh);
1876+ if (kout < 0)
1877+ fatal("DH_compute_key: failed");
1878+
1879+ shared_secret = BN_new();
1880+ if (shared_secret == NULL)
1881+ fatal("kexgss_server: BN_new failed");
1882+
1883+ if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
1884+ fatal("kexgss_server: BN_bin2bn failed");
1885+
1886+ memset(kbuf, 0, klen);
1887+ free(kbuf);
1888+
1889+ hashlen = sizeof(hash);
1890+ switch (ssh->kex->kex_type) {
1891+ case KEX_GSS_GRP1_SHA1:
1892+ case KEX_GSS_GRP14_SHA1:
1893+ kex_dh_hash(
1894+ ssh->kex->client_version_string, ssh->kex->server_version_string,
1895+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1896+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1897+ NULL, 0, /* Change this if we start sending host keys */
1898+ dh_client_pub, dh->pub_key, shared_secret,
1899+ hash, &hashlen
1900+ );
1901+ break;
1902+ case KEX_GSS_GEX_SHA1:
1903+ kexgex_hash(
1904+ ssh->kex->hash_alg,
1905+ ssh->kex->client_version_string, ssh->kex->server_version_string,
1906+ buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
1907+ buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
1908+ NULL, 0,
1909+ min, nbits, max,
1910+ dh->p, dh->g,
1911+ dh_client_pub,
1912+ dh->pub_key,
1913+ shared_secret,
1914+ hash, &hashlen
1915+ );
1916+ break;
1917+ default:
1918+ fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type);
1919+ }
1920+
1921+ BN_clear_free(dh_client_pub);
1922+
1923+ if (ssh->kex->session_id == NULL) {
1924+ ssh->kex->session_id_len = hashlen;
1925+ ssh->kex->session_id = xmalloc(ssh->kex->session_id_len);
1926+ memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len);
1927+ }
1928+
1929+ gssbuf.value = hash;
1930+ gssbuf.length = hashlen;
1931+
1932+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
1933+ fatal("Couldn't get MIC");
1934+
1935+ packet_start(SSH2_MSG_KEXGSS_COMPLETE);
1936+ packet_put_bignum2(dh->pub_key);
1937+ packet_put_string(msg_tok.value,msg_tok.length);
1938+
1939+ if (send_tok.length != 0) {
1940+ packet_put_char(1); /* true */
1941+ packet_put_string(send_tok.value, send_tok.length);
1942+ } else {
1943+ packet_put_char(0); /* false */
1944+ }
1945+ packet_send();
1946+
1947+ gss_release_buffer(&min_status, &send_tok);
1948+ gss_release_buffer(&min_status, &msg_tok);
1949+
1950+ if (gss_kex_context == NULL)
1951+ gss_kex_context = ctxt;
1952+ else
1953+ ssh_gssapi_delete_ctx(&ctxt);
1954+
1955+ DH_free(dh);
1956+
1957+ kex_derive_keys_bn(ssh, hash, hashlen, shared_secret);
1958+ BN_clear_free(shared_secret);
1959+ kex_send_newkeys(ssh);
1960+
1961+ /* If this was a rekey, then save out any delegated credentials we
1962+ * just exchanged. */
1963+ if (options.gss_store_rekey)
1964+ ssh_gssapi_rekey_creds();
1965+ return 0;
1966+}
1967+#endif /* GSSAPI */
1968diff --git a/monitor.c b/monitor.c
1969index bab6ce8..a2027e5 100644
1970--- a/monitor.c
1971+++ b/monitor.c
1972@@ -157,6 +157,8 @@ int mm_answer_gss_setup_ctx(int, Buffer *);
1973 int mm_answer_gss_accept_ctx(int, Buffer *);
1974 int mm_answer_gss_userok(int, Buffer *);
1975 int mm_answer_gss_checkmic(int, Buffer *);
1976+int mm_answer_gss_sign(int, Buffer *);
1977+int mm_answer_gss_updatecreds(int, Buffer *);
1978 #endif
1979
1980 #ifdef SSH_AUDIT_EVENTS
1981@@ -234,11 +236,18 @@ struct mon_table mon_dispatch_proto20[] = {
1982 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
1983 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
1984 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
1985+ {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
1986 #endif
1987 {0, 0, NULL}
1988 };
1989
1990 struct mon_table mon_dispatch_postauth20[] = {
1991+#ifdef GSSAPI
1992+ {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
1993+ {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
1994+ {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
1995+ {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
1996+#endif
1997 #ifdef WITH_OPENSSL
1998 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
1999 #endif
2000@@ -353,6 +362,10 @@ monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
2001 /* Permit requests for moduli and signatures */
2002 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
2003 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
2004+#ifdef GSSAPI
2005+ /* and for the GSSAPI key exchange */
2006+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
2007+#endif
2008 } else {
2009 mon_dispatch = mon_dispatch_proto15;
2010
2011@@ -461,6 +474,10 @@ monitor_child_postauth(struct monitor *pmonitor)
2012 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
2013 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
2014 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
2015+#ifdef GSSAPI
2016+ /* and for the GSSAPI key exchange */
2017+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
2018+#endif
2019 } else {
2020 mon_dispatch = mon_dispatch_postauth15;
2021 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
2022@@ -1860,6 +1877,13 @@ monitor_apply_keystate(struct monitor *pmonitor)
2023 # endif
2024 #endif /* WITH_OPENSSL */
2025 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
2026+#ifdef GSSAPI
2027+ if (options.gss_keyex) {
2028+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
2029+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
2030+ kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
2031+ }
2032+#endif
2033 kex->load_host_public_key=&get_hostkey_public_by_type;
2034 kex->load_host_private_key=&get_hostkey_private_by_type;
2035 kex->host_key_index=&get_hostkey_index;
2036@@ -1959,6 +1983,9 @@ mm_answer_gss_setup_ctx(int sock, Buffer *m)
2037 OM_uint32 major;
2038 u_int len;
2039
2040+ if (!options.gss_authentication && !options.gss_keyex)
2041+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2042+
2043 goid.elements = buffer_get_string(m, &len);
2044 goid.length = len;
2045
2046@@ -1986,6 +2013,9 @@ mm_answer_gss_accept_ctx(int sock, Buffer *m)
2047 OM_uint32 flags = 0; /* GSI needs this */
2048 u_int len;
2049
2050+ if (!options.gss_authentication && !options.gss_keyex)
2051+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2052+
2053 in.value = buffer_get_string(m, &len);
2054 in.length = len;
2055 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2056@@ -2003,6 +2033,7 @@ mm_answer_gss_accept_ctx(int sock, Buffer *m)
2057 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2058 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2059 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2060+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
2061 }
2062 return (0);
2063 }
2064@@ -2014,6 +2045,9 @@ mm_answer_gss_checkmic(int sock, Buffer *m)
2065 OM_uint32 ret;
2066 u_int len;
2067
2068+ if (!options.gss_authentication && !options.gss_keyex)
2069+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2070+
2071 gssbuf.value = buffer_get_string(m, &len);
2072 gssbuf.length = len;
2073 mic.value = buffer_get_string(m, &len);
2074@@ -2040,7 +2074,11 @@ mm_answer_gss_userok(int sock, Buffer *m)
2075 {
2076 int authenticated;
2077
2078- authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2079+ if (!options.gss_authentication && !options.gss_keyex)
2080+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2081+
2082+ authenticated = authctxt->valid &&
2083+ ssh_gssapi_userok(authctxt->user, authctxt->pw);
2084
2085 buffer_clear(m);
2086 buffer_put_int(m, authenticated);
2087@@ -2053,5 +2091,73 @@ mm_answer_gss_userok(int sock, Buffer *m)
2088 /* Monitor loop will terminate if authenticated */
2089 return (authenticated);
2090 }
2091+
2092+int
2093+mm_answer_gss_sign(int socket, Buffer *m)
2094+{
2095+ gss_buffer_desc data;
2096+ gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2097+ OM_uint32 major, minor;
2098+ u_int len;
2099+
2100+ if (!options.gss_authentication && !options.gss_keyex)
2101+ fatal("In GSSAPI monitor when GSSAPI is disabled");
2102+
2103+ data.value = buffer_get_string(m, &len);
2104+ data.length = len;
2105+ if (data.length != 20)
2106+ fatal("%s: data length incorrect: %d", __func__,
2107+ (int) data.length);
2108+
2109+ /* Save the session ID on the first time around */
2110+ if (session_id2_len == 0) {
2111+ session_id2_len = data.length;
2112+ session_id2 = xmalloc(session_id2_len);
2113+ memcpy(session_id2, data.value, session_id2_len);
2114+ }
2115+ major = ssh_gssapi_sign(gsscontext, &data, &hash);
2116+
2117+ free(data.value);
2118+
2119+ buffer_clear(m);
2120+ buffer_put_int(m, major);
2121+ buffer_put_string(m, hash.value, hash.length);
2122+
2123+ mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2124+
2125+ gss_release_buffer(&minor, &hash);
2126+
2127+ /* Turn on getpwnam permissions */
2128+ monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2129+
2130+ /* And credential updating, for when rekeying */
2131+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
2132+
2133+ return (0);
2134+}
2135+
2136+int
2137+mm_answer_gss_updatecreds(int socket, Buffer *m) {
2138+ ssh_gssapi_ccache store;
2139+ int ok;
2140+
2141+ store.filename = buffer_get_string(m, NULL);
2142+ store.envvar = buffer_get_string(m, NULL);
2143+ store.envval = buffer_get_string(m, NULL);
2144+
2145+ ok = ssh_gssapi_update_creds(&store);
2146+
2147+ free(store.filename);
2148+ free(store.envvar);
2149+ free(store.envval);
2150+
2151+ buffer_clear(m);
2152+ buffer_put_int(m, ok);
2153+
2154+ mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
2155+
2156+ return(0);
2157+}
2158+
2159 #endif /* GSSAPI */
2160
2161diff --git a/monitor.h b/monitor.h
2162index 93b8b66..bc50ade 100644
2163--- a/monitor.h
2164+++ b/monitor.h
2165@@ -65,6 +65,9 @@ enum monitor_reqtype {
2166 MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
2167 MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
2168
2169+ MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
2170+ MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
2171+
2172 };
2173
2174 struct mm_master;
2175diff --git a/monitor_wrap.c b/monitor_wrap.c
2176index b379f05..b667218 100644
2177--- a/monitor_wrap.c
2178+++ b/monitor_wrap.c
2179@@ -1068,7 +1068,7 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
2180 }
2181
2182 int
2183-mm_ssh_gssapi_userok(char *user)
2184+mm_ssh_gssapi_userok(char *user, struct passwd *pw)
2185 {
2186 Buffer m;
2187 int authenticated = 0;
2188@@ -1085,5 +1085,50 @@ mm_ssh_gssapi_userok(char *user)
2189 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
2190 return (authenticated);
2191 }
2192+
2193+OM_uint32
2194+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
2195+{
2196+ Buffer m;
2197+ OM_uint32 major;
2198+ u_int len;
2199+
2200+ buffer_init(&m);
2201+ buffer_put_string(&m, data->value, data->length);
2202+
2203+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, &m);
2204+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, &m);
2205+
2206+ major = buffer_get_int(&m);
2207+ hash->value = buffer_get_string(&m, &len);
2208+ hash->length = len;
2209+
2210+ buffer_free(&m);
2211+
2212+ return(major);
2213+}
2214+
2215+int
2216+mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
2217+{
2218+ Buffer m;
2219+ int ok;
2220+
2221+ buffer_init(&m);
2222+
2223+ buffer_put_cstring(&m, store->filename ? store->filename : "");
2224+ buffer_put_cstring(&m, store->envvar ? store->envvar : "");
2225+ buffer_put_cstring(&m, store->envval ? store->envval : "");
2226+
2227+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, &m);
2228+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, &m);
2229+
2230+ ok = buffer_get_int(&m);
2231+
2232+ buffer_free(&m);
2233+
2234+ return (ok);
2235+}
2236+
2237 #endif /* GSSAPI */
2238
2239diff --git a/monitor_wrap.h b/monitor_wrap.h
2240index e18784a..0c770e8 100644
2241--- a/monitor_wrap.h
2242+++ b/monitor_wrap.h
2243@@ -58,8 +58,10 @@ BIGNUM *mm_auth_rsa_generate_challenge(Key *);
2244 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
2245 OM_uint32 mm_ssh_gssapi_accept_ctx(Gssctxt *,
2246 gss_buffer_desc *, gss_buffer_desc *, OM_uint32 *);
2247-int mm_ssh_gssapi_userok(char *user);
2248+int mm_ssh_gssapi_userok(char *user, struct passwd *);
2249 OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
2250+OM_uint32 mm_ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
2251+int mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *);
2252 #endif
2253
2254 #ifdef USE_PAM
2255diff --git a/readconf.c b/readconf.c
2256index 42a2961..254dbce 100644
2257--- a/readconf.c
2258+++ b/readconf.c
2259@@ -147,6 +147,8 @@ typedef enum {
2260 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
2261 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
2262 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
2263+ oGssTrustDns, oGssKeyEx, oGssClientIdentity, oGssRenewalRekey,
2264+ oGssServerIdentity,
2265 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
2266 oSendEnv, oControlPath, oControlMaster, oControlPersist,
2267 oHashKnownHosts,
2268@@ -191,10 +193,19 @@ static struct {
2269 { "afstokenpassing", oUnsupported },
2270 #if defined(GSSAPI)
2271 { "gssapiauthentication", oGssAuthentication },
2272+ { "gssapikeyexchange", oGssKeyEx },
2273 { "gssapidelegatecredentials", oGssDelegateCreds },
2274+ { "gssapitrustdns", oGssTrustDns },
2275+ { "gssapiclientidentity", oGssClientIdentity },
2276+ { "gssapiserveridentity", oGssServerIdentity },
2277+ { "gssapirenewalforcesrekey", oGssRenewalRekey },
2278 #else
2279 { "gssapiauthentication", oUnsupported },
2280+ { "gssapikeyexchange", oUnsupported },
2281 { "gssapidelegatecredentials", oUnsupported },
2282+ { "gssapitrustdns", oUnsupported },
2283+ { "gssapiclientidentity", oUnsupported },
2284+ { "gssapirenewalforcesrekey", oUnsupported },
2285 #endif
2286 { "fallbacktorsh", oDeprecated },
2287 { "usersh", oDeprecated },
2288@@ -892,10 +903,30 @@ parse_time:
2289 intptr = &options->gss_authentication;
2290 goto parse_flag;
2291
2292+ case oGssKeyEx:
2293+ intptr = &options->gss_keyex;
2294+ goto parse_flag;
2295+
2296 case oGssDelegateCreds:
2297 intptr = &options->gss_deleg_creds;
2298 goto parse_flag;
2299
2300+ case oGssTrustDns:
2301+ intptr = &options->gss_trust_dns;
2302+ goto parse_flag;
2303+
2304+ case oGssClientIdentity:
2305+ charptr = &options->gss_client_identity;
2306+ goto parse_string;
2307+
2308+ case oGssServerIdentity:
2309+ charptr = &options->gss_server_identity;
2310+ goto parse_string;
2311+
2312+ case oGssRenewalRekey:
2313+ intptr = &options->gss_renewal_rekey;
2314+ goto parse_flag;
2315+
2316 case oBatchMode:
2317 intptr = &options->batch_mode;
2318 goto parse_flag;
2319@@ -1601,7 +1632,12 @@ initialize_options(Options * options)
2320 options->pubkey_authentication = -1;
2321 options->challenge_response_authentication = -1;
2322 options->gss_authentication = -1;
2323+ options->gss_keyex = -1;
2324 options->gss_deleg_creds = -1;
2325+ options->gss_trust_dns = -1;
2326+ options->gss_renewal_rekey = -1;
2327+ options->gss_client_identity = NULL;
2328+ options->gss_server_identity = NULL;
2329 options->password_authentication = -1;
2330 options->kbd_interactive_authentication = -1;
2331 options->kbd_interactive_devices = NULL;
2332@@ -1728,8 +1764,14 @@ fill_default_options(Options * options)
2333 options->challenge_response_authentication = 1;
2334 if (options->gss_authentication == -1)
2335 options->gss_authentication = 0;
2336+ if (options->gss_keyex == -1)
2337+ options->gss_keyex = 0;
2338 if (options->gss_deleg_creds == -1)
2339 options->gss_deleg_creds = 0;
2340+ if (options->gss_trust_dns == -1)
2341+ options->gss_trust_dns = 0;
2342+ if (options->gss_renewal_rekey == -1)
2343+ options->gss_renewal_rekey = 0;
2344 if (options->password_authentication == -1)
2345 options->password_authentication = 1;
2346 if (options->kbd_interactive_authentication == -1)
2347diff --git a/readconf.h b/readconf.h
2348index 576b9e3..ef39c4c 100644
2349--- a/readconf.h
2350+++ b/readconf.h
2351@@ -45,7 +45,12 @@ typedef struct {
2352 int challenge_response_authentication;
2353 /* Try S/Key or TIS, authentication. */
2354 int gss_authentication; /* Try GSS authentication */
2355+ int gss_keyex; /* Try GSS key exchange */
2356 int gss_deleg_creds; /* Delegate GSS credentials */
2357+ int gss_trust_dns; /* Trust DNS for GSS canonicalization */
2358+ int gss_renewal_rekey; /* Credential renewal forces rekey */
2359+ char *gss_client_identity; /* Principal to initiate GSSAPI with */
2360+ char *gss_server_identity; /* GSSAPI target principal */
2361 int password_authentication; /* Try password
2362 * authentication. */
2363 int kbd_interactive_authentication; /* Try keyboard-interactive auth. */
2364diff --git a/servconf.c b/servconf.c
2365index 3185462..f68c0d0 100644
2366--- a/servconf.c
2367+++ b/servconf.c
2368@@ -114,7 +114,10 @@ initialize_server_options(ServerOptions *options)
2369 options->kerberos_ticket_cleanup = -1;
2370 options->kerberos_get_afs_token = -1;
2371 options->gss_authentication=-1;
2372+ options->gss_keyex = -1;
2373 options->gss_cleanup_creds = -1;
2374+ options->gss_strict_acceptor = -1;
2375+ options->gss_store_rekey = -1;
2376 options->password_authentication = -1;
2377 options->kbd_interactive_authentication = -1;
2378 options->challenge_response_authentication = -1;
2379@@ -269,8 +272,14 @@ fill_default_server_options(ServerOptions *options)
2380 options->kerberos_get_afs_token = 0;
2381 if (options->gss_authentication == -1)
2382 options->gss_authentication = 0;
2383+ if (options->gss_keyex == -1)
2384+ options->gss_keyex = 0;
2385 if (options->gss_cleanup_creds == -1)
2386 options->gss_cleanup_creds = 1;
2387+ if (options->gss_strict_acceptor == -1)
2388+ options->gss_strict_acceptor = 1;
2389+ if (options->gss_store_rekey == -1)
2390+ options->gss_store_rekey = 0;
2391 if (options->password_authentication == -1)
2392 options->password_authentication = 1;
2393 if (options->kbd_interactive_authentication == -1)
2394@@ -391,7 +400,9 @@ typedef enum {
2395 sBanner, sUseDNS, sHostbasedAuthentication,
2396 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
2397 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
2398- sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
2399+ sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
2400+ sGssKeyEx, sGssStoreRekey,
2401+ sAcceptEnv, sPermitTunnel,
2402 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
2403 sUsePrivilegeSeparation, sAllowAgentForwarding,
2404 sHostCertificate,
2405@@ -462,10 +473,20 @@ static struct {
2406 #ifdef GSSAPI
2407 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
2408 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
2409+ { "gssapicleanupcreds", sGssCleanupCreds, SSHCFG_GLOBAL },
2410+ { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
2411+ { "gssapikeyexchange", sGssKeyEx, SSHCFG_GLOBAL },
2412+ { "gssapistorecredentialsonrekey", sGssStoreRekey, SSHCFG_GLOBAL },
2413 #else
2414 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
2415 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
2416+ { "gssapicleanupcreds", sUnsupported, SSHCFG_GLOBAL },
2417+ { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
2418+ { "gssapikeyexchange", sUnsupported, SSHCFG_GLOBAL },
2419+ { "gssapistorecredentialsonrekey", sUnsupported, SSHCFG_GLOBAL },
2420 #endif
2421+ { "gssusesessionccache", sUnsupported, SSHCFG_GLOBAL },
2422+ { "gssapiusesessioncredcache", sUnsupported, SSHCFG_GLOBAL },
2423 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
2424 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
2425 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
2426@@ -1166,10 +1187,22 @@ process_server_config_line(ServerOptions *options, char *line,
2427 intptr = &options->gss_authentication;
2428 goto parse_flag;
2429
2430+ case sGssKeyEx:
2431+ intptr = &options->gss_keyex;
2432+ goto parse_flag;
2433+
2434 case sGssCleanupCreds:
2435 intptr = &options->gss_cleanup_creds;
2436 goto parse_flag;
2437
2438+ case sGssStrictAcceptor:
2439+ intptr = &options->gss_strict_acceptor;
2440+ goto parse_flag;
2441+
2442+ case sGssStoreRekey:
2443+ intptr = &options->gss_store_rekey;
2444+ goto parse_flag;
2445+
2446 case sPasswordAuthentication:
2447 intptr = &options->password_authentication;
2448 goto parse_flag;
2449@@ -2125,7 +2158,10 @@ dump_config(ServerOptions *o)
2450 #endif
2451 #ifdef GSSAPI
2452 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2453+ dump_cfg_fmtint(sGssKeyEx, o->gss_keyex);
2454 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
2455+ dump_cfg_fmtint(sGssStrictAcceptor, o->gss_strict_acceptor);
2456+ dump_cfg_fmtint(sGssStoreRekey, o->gss_store_rekey);
2457 #endif
2458 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2459 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2460diff --git a/servconf.h b/servconf.h
2461index 9922f0c..d2ed4d7 100644
2462--- a/servconf.h
2463+++ b/servconf.h
2464@@ -115,7 +115,10 @@ typedef struct {
2465 int kerberos_get_afs_token; /* If true, try to get AFS token if
2466 * authenticated with Kerberos. */
2467 int gss_authentication; /* If true, permit GSSAPI authentication */
2468+ int gss_keyex; /* If true, permit GSSAPI key exchange */
2469 int gss_cleanup_creds; /* If true, destroy cred cache on logout */
2470+ int gss_strict_acceptor; /* If true, restrict the GSSAPI acceptor name */
2471+ int gss_store_rekey;
2472 int password_authentication; /* If true, permit password
2473 * authentication. */
2474 int kbd_interactive_authentication; /* If true, permit */
2475diff --git a/ssh-gss.h b/ssh-gss.h
2476index a99d7f0..914701b 100644
2477--- a/ssh-gss.h
2478+++ b/ssh-gss.h
2479@@ -1,6 +1,6 @@
2480 /* $OpenBSD: ssh-gss.h,v 1.11 2014/02/26 20:28:44 djm Exp $ */
2481 /*
2482- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
2483+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
2484 *
2485 * Redistribution and use in source and binary forms, with or without
2486 * modification, are permitted provided that the following conditions
2487@@ -61,10 +61,22 @@
2488
2489 #define SSH_GSS_OIDTYPE 0x06
2490
2491+#define SSH2_MSG_KEXGSS_INIT 30
2492+#define SSH2_MSG_KEXGSS_CONTINUE 31
2493+#define SSH2_MSG_KEXGSS_COMPLETE 32
2494+#define SSH2_MSG_KEXGSS_HOSTKEY 33
2495+#define SSH2_MSG_KEXGSS_ERROR 34
2496+#define SSH2_MSG_KEXGSS_GROUPREQ 40
2497+#define SSH2_MSG_KEXGSS_GROUP 41
2498+#define KEX_GSS_GRP1_SHA1_ID "gss-group1-sha1-"
2499+#define KEX_GSS_GRP14_SHA1_ID "gss-group14-sha1-"
2500+#define KEX_GSS_GEX_SHA1_ID "gss-gex-sha1-"
2501+
2502 typedef struct {
2503 char *filename;
2504 char *envvar;
2505 char *envval;
2506+ struct passwd *owner;
2507 void *data;
2508 } ssh_gssapi_ccache;
2509
2510@@ -72,8 +84,11 @@ typedef struct {
2511 gss_buffer_desc displayname;
2512 gss_buffer_desc exportedname;
2513 gss_cred_id_t creds;
2514+ gss_name_t name;
2515 struct ssh_gssapi_mech_struct *mech;
2516 ssh_gssapi_ccache store;
2517+ int used;
2518+ int updated;
2519 } ssh_gssapi_client;
2520
2521 typedef struct ssh_gssapi_mech_struct {
2522@@ -84,6 +99,7 @@ typedef struct ssh_gssapi_mech_struct {
2523 int (*userok) (ssh_gssapi_client *, char *);
2524 int (*localname) (ssh_gssapi_client *, char **);
2525 void (*storecreds) (ssh_gssapi_client *);
2526+ int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
2527 } ssh_gssapi_mech;
2528
2529 typedef struct {
2530@@ -94,10 +110,11 @@ typedef struct {
2531 gss_OID oid; /* client */
2532 gss_cred_id_t creds; /* server */
2533 gss_name_t client; /* server */
2534- gss_cred_id_t client_creds; /* server */
2535+ gss_cred_id_t client_creds; /* both */
2536 } Gssctxt;
2537
2538 extern ssh_gssapi_mech *supported_mechs[];
2539+extern Gssctxt *gss_kex_context;
2540
2541 int ssh_gssapi_check_oid(Gssctxt *, void *, size_t);
2542 void ssh_gssapi_set_oid_data(Gssctxt *, void *, size_t);
2543@@ -119,16 +136,32 @@ void ssh_gssapi_build_ctx(Gssctxt **);
2544 void ssh_gssapi_delete_ctx(Gssctxt **);
2545 OM_uint32 ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
2546 void ssh_gssapi_buildmic(Buffer *, const char *, const char *, const char *);
2547-int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *);
2548+int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *, const char *);
2549+OM_uint32 ssh_gssapi_client_identity(Gssctxt *, const char *);
2550+int ssh_gssapi_credentials_updated(Gssctxt *);
2551
2552 /* In the server */
2553+typedef int ssh_gssapi_check_fn(Gssctxt **, gss_OID, const char *,
2554+ const char *);
2555+char *ssh_gssapi_client_mechanisms(const char *, const char *);
2556+char *ssh_gssapi_kex_mechs(gss_OID_set, ssh_gssapi_check_fn *, const char *,
2557+ const char *);
2558+gss_OID ssh_gssapi_id_kex(Gssctxt *, char *, int);
2559+int ssh_gssapi_server_check_mech(Gssctxt **,gss_OID, const char *,
2560+ const char *);
2561 OM_uint32 ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
2562-int ssh_gssapi_userok(char *name);
2563+int ssh_gssapi_userok(char *name, struct passwd *);
2564 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
2565 void ssh_gssapi_do_child(char ***, u_int *);
2566 void ssh_gssapi_cleanup_creds(void);
2567 void ssh_gssapi_storecreds(void);
2568
2569+char *ssh_gssapi_server_mechanisms(void);
2570+int ssh_gssapi_oid_table_ok(void);
2571+
2572+int ssh_gssapi_update_creds(ssh_gssapi_ccache *store);
2573+void ssh_gssapi_rekey_creds(void);
2574+
2575 #endif /* GSSAPI */
2576
2577 #endif /* _SSH_GSS_H */
2578diff --git a/ssh_config b/ssh_config
2579index 03a228f..228e5ab 100644
2580--- a/ssh_config
2581+++ b/ssh_config
2582@@ -26,6 +26,8 @@
2583 # HostbasedAuthentication no
2584 # GSSAPIAuthentication no
2585 # GSSAPIDelegateCredentials no
2586+# GSSAPIKeyExchange no
2587+# GSSAPITrustDNS no
2588 # BatchMode no
2589 # CheckHostIP yes
2590 # AddressFamily any
2591diff --git a/ssh_config.5 b/ssh_config.5
2592index 140d0ba..4476171 100644
2593--- a/ssh_config.5
2594+++ b/ssh_config.5
2595@@ -743,11 +743,43 @@ Specifies whether user authentication based on GSSAPI is allowed.
2596 The default is
2597 .Dq no .
2598 Note that this option applies to protocol version 2 only.
2599+.It Cm GSSAPIKeyExchange
2600+Specifies whether key exchange based on GSSAPI may be used. When using
2601+GSSAPI key exchange the server need not have a host key.
2602+The default is
2603+.Dq no .
2604+Note that this option applies to protocol version 2 only.
2605+.It Cm GSSAPIClientIdentity
2606+If set, specifies the GSSAPI client identity that ssh should use when
2607+connecting to the server. The default is unset, which means that the default
2608+identity will be used.
2609+.It Cm GSSAPIServerIdentity
2610+If set, specifies the GSSAPI server identity that ssh should expect when
2611+connecting to the server. The default is unset, which means that the
2612+expected GSSAPI server identity will be determined from the target
2613+hostname.
2614 .It Cm GSSAPIDelegateCredentials
2615 Forward (delegate) credentials to the server.
2616 The default is
2617 .Dq no .
2618-Note that this option applies to protocol version 2 only.
2619+Note that this option applies to protocol version 2 connections using GSSAPI.
2620+.It Cm GSSAPIRenewalForcesRekey
2621+If set to
2622+.Dq yes
2623+then renewal of the client's GSSAPI credentials will force the rekeying of the
2624+ssh connection. With a compatible server, this can delegate the renewed
2625+credentials to a session on the server.
2626+The default is
2627+.Dq no .
2628+.It Cm GSSAPITrustDns
2629+Set to
2630+.Dq yes to indicate that the DNS is trusted to securely canonicalize
2631+the name of the host being connected to. If
2632+.Dq no, the hostname entered on the
2633+command line will be passed untouched to the GSSAPI library.
2634+The default is
2635+.Dq no .
2636+This option only applies to protocol version 2 connections using GSSAPI.
2637 .It Cm HashKnownHosts
2638 Indicates that
2639 .Xr ssh 1
2640diff --git a/sshconnect2.c b/sshconnect2.c
2641index ba56f64..faa8ec5 100644
2642--- a/sshconnect2.c
2643+++ b/sshconnect2.c
2644@@ -160,9 +160,34 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
2645 struct kex *kex;
2646 int r;
2647
2648+#ifdef GSSAPI
2649+ char *orig = NULL, *gss = NULL;
2650+ char *gss_host = NULL;
2651+#endif
2652+
2653 xxx_host = host;
2654 xxx_hostaddr = hostaddr;
2655
2656+#ifdef GSSAPI
2657+ if (options.gss_keyex) {
2658+ /* Add the GSSAPI mechanisms currently supported on this
2659+ * client to the key exchange algorithm proposal */
2660+ orig = myproposal[PROPOSAL_KEX_ALGS];
2661+
2662+ if (options.gss_trust_dns)
2663+ gss_host = (char *)get_canonical_hostname(1);
2664+ else
2665+ gss_host = host;
2666+
2667+ gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
2668+ if (gss) {
2669+ debug("Offering GSSAPI proposal: %s", gss);
2670+ xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
2671+ "%s,%s", gss, orig);
2672+ }
2673+ }
2674+#endif
2675+
2676 if (options.ciphers == (char *)-1) {
2677 logit("No valid ciphers for protocol version 2 given, using defaults.");
2678 options.ciphers = NULL;
2679@@ -200,6 +225,17 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
2680 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
2681 myproposal[PROPOSAL_KEX_ALGS]);
2682
2683+#ifdef GSSAPI
2684+ /* If we've got GSSAPI algorithms, then we also support the
2685+ * 'null' hostkey, as a last resort */
2686+ if (options.gss_keyex && gss) {
2687+ orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
2688+ xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
2689+ "%s,null", orig);
2690+ free(gss);
2691+ }
2692+#endif
2693+
2694 if (options.rekey_limit || options.rekey_interval)
2695 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
2696 (time_t)options.rekey_interval);
2697@@ -218,10 +254,30 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
2698 # endif
2699 #endif
2700 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
2701+#ifdef GSSAPI
2702+ if (options.gss_keyex) {
2703+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
2704+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
2705+ kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
2706+ }
2707+#endif
2708 kex->client_version_string=client_version_string;
2709 kex->server_version_string=server_version_string;
2710 kex->verify_host_key=&verify_host_key_callback;
2711
2712+#ifdef GSSAPI
2713+ if (options.gss_keyex) {
2714+ kex->gss_deleg_creds = options.gss_deleg_creds;
2715+ kex->gss_trust_dns = options.gss_trust_dns;
2716+ kex->gss_client = options.gss_client_identity;
2717+ if (options.gss_server_identity) {
2718+ kex->gss_host = options.gss_server_identity;
2719+ } else {
2720+ kex->gss_host = gss_host;
2721+ }
2722+ }
2723+#endif
2724+
2725 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
2726
2727 if (options.use_roaming && !kex->roaming) {
2728@@ -313,6 +369,7 @@ int input_gssapi_token(int type, u_int32_t, void *);
2729 int input_gssapi_hash(int type, u_int32_t, void *);
2730 int input_gssapi_error(int, u_int32_t, void *);
2731 int input_gssapi_errtok(int, u_int32_t, void *);
2732+int userauth_gsskeyex(Authctxt *authctxt);
2733 #endif
2734
2735 void userauth(Authctxt *, char *);
2736@@ -328,6 +385,11 @@ static char *authmethods_get(void);
2737
2738 Authmethod authmethods[] = {
2739 #ifdef GSSAPI
2740+ {"gssapi-keyex",
2741+ userauth_gsskeyex,
2742+ NULL,
2743+ &options.gss_authentication,
2744+ NULL},
2745 {"gssapi-with-mic",
2746 userauth_gssapi,
2747 NULL,
2748@@ -634,19 +696,31 @@ userauth_gssapi(Authctxt *authctxt)
2749 static u_int mech = 0;
2750 OM_uint32 min;
2751 int ok = 0;
2752+ const char *gss_host;
2753+
2754+ if (options.gss_server_identity)
2755+ gss_host = options.gss_server_identity;
2756+ else if (options.gss_trust_dns)
2757+ gss_host = get_canonical_hostname(1);
2758+ else
2759+ gss_host = authctxt->host;
2760
2761 /* Try one GSSAPI method at a time, rather than sending them all at
2762 * once. */
2763
2764 if (gss_supported == NULL)
2765- gss_indicate_mechs(&min, &gss_supported);
2766+ if (GSS_ERROR(gss_indicate_mechs(&min, &gss_supported))) {
2767+ gss_supported = NULL;
2768+ return 0;
2769+ }
2770
2771 /* Check to see if the mechanism is usable before we offer it */
2772 while (mech < gss_supported->count && !ok) {
2773 /* My DER encoding requires length<128 */
2774 if (gss_supported->elements[mech].length < 128 &&
2775 ssh_gssapi_check_mechanism(&gssctxt,
2776- &gss_supported->elements[mech], authctxt->host)) {
2777+ &gss_supported->elements[mech], gss_host,
2778+ options.gss_client_identity)) {
2779 ok = 1; /* Mechanism works */
2780 } else {
2781 mech++;
2782@@ -743,8 +817,8 @@ input_gssapi_response(int type, u_int32_t plen, void *ctxt)
2783 {
2784 Authctxt *authctxt = ctxt;
2785 Gssctxt *gssctxt;
2786- int oidlen;
2787- char *oidv;
2788+ u_int oidlen;
2789+ u_char *oidv;
2790
2791 if (authctxt == NULL)
2792 fatal("input_gssapi_response: no authentication context");
2793@@ -857,6 +931,48 @@ input_gssapi_error(int type, u_int32_t plen, void *ctxt)
2794 free(lang);
2795 return 0;
2796 }
2797+
2798+int
2799+userauth_gsskeyex(Authctxt *authctxt)
2800+{
2801+ Buffer b;
2802+ gss_buffer_desc gssbuf;
2803+ gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
2804+ OM_uint32 ms;
2805+
2806+ static int attempt = 0;
2807+ if (attempt++ >= 1)
2808+ return (0);
2809+
2810+ if (gss_kex_context == NULL) {
2811+ debug("No valid Key exchange context");
2812+ return (0);
2813+ }
2814+
2815+ ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
2816+ "gssapi-keyex");
2817+
2818+ gssbuf.value = buffer_ptr(&b);
2819+ gssbuf.length = buffer_len(&b);
2820+
2821+ if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
2822+ buffer_free(&b);
2823+ return (0);
2824+ }
2825+
2826+ packet_start(SSH2_MSG_USERAUTH_REQUEST);
2827+ packet_put_cstring(authctxt->server_user);
2828+ packet_put_cstring(authctxt->service);
2829+ packet_put_cstring(authctxt->method->name);
2830+ packet_put_string(mic.value, mic.length);
2831+ packet_send();
2832+
2833+ buffer_free(&b);
2834+ gss_release_buffer(&ms, &mic);
2835+
2836+ return (1);
2837+}
2838+
2839 #endif /* GSSAPI */
2840
2841 int
2842diff --git a/sshd.c b/sshd.c
2843index e1c767c..cf38bae 100644
2844--- a/sshd.c
2845+++ b/sshd.c
2846@@ -125,6 +125,10 @@
2847 #include "version.h"
2848 #include "ssherr.h"
2849
2850+#ifdef USE_SECURITY_SESSION_API
2851+#include <Security/AuthSession.h>
2852+#endif
2853+
2854 #ifndef O_NOCTTY
2855 #define O_NOCTTY 0
2856 #endif
2857@@ -1815,10 +1819,13 @@ main(int ac, char **av)
2858 logit("Disabling protocol version 1. Could not load host key");
2859 options.protocol &= ~SSH_PROTO_1;
2860 }
2861+#ifndef GSSAPI
2862+ /* The GSSAPI key exchange can run without a host key */
2863 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
2864 logit("Disabling protocol version 2. Could not load host key");
2865 options.protocol &= ~SSH_PROTO_2;
2866 }
2867+#endif
2868 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
2869 logit("sshd: no hostkeys available -- exiting.");
2870 exit(1);
2871@@ -2132,6 +2139,60 @@ main(int ac, char **av)
2872 remote_ip, remote_port,
2873 get_local_ipaddr(sock_in), get_local_port());
2874
2875+#ifdef USE_SECURITY_SESSION_API
2876+ /*
2877+ * Create a new security session for use by the new user login if
2878+ * the current session is the root session or we are not launched
2879+ * by inetd (eg: debugging mode or server mode). We do not
2880+ * necessarily need to create a session if we are launched from
2881+ * inetd because Panther xinetd will create a session for us.
2882+ *
2883+ * The only case where this logic will fail is if there is an
2884+ * inetd running in a non-root session which is not creating
2885+ * new sessions for us. Then all the users will end up in the
2886+ * same session (bad).
2887+ *
2888+ * When the client exits, the session will be destroyed for us
2889+ * automatically.
2890+ *
2891+ * We must create the session before any credentials are stored
2892+ * (including AFS pags, which happens a few lines below).
2893+ */
2894+ {
2895+ OSStatus err = 0;
2896+ SecuritySessionId sid = 0;
2897+ SessionAttributeBits sattrs = 0;
2898+
2899+ err = SessionGetInfo(callerSecuritySession, &sid, &sattrs);
2900+ if (err)
2901+ error("SessionGetInfo() failed with error %.8X",
2902+ (unsigned) err);
2903+ else
2904+ debug("Current Session ID is %.8X / Session Attributes are %.8X",
2905+ (unsigned) sid, (unsigned) sattrs);
2906+
2907+ if (inetd_flag && !(sattrs & sessionIsRoot))
2908+ debug("Running in inetd mode in a non-root session... "
2909+ "assuming inetd created the session for us.");
2910+ else {
2911+ debug("Creating new security session...");
2912+ err = SessionCreate(0, sessionHasTTY | sessionIsRemote);
2913+ if (err)
2914+ error("SessionCreate() failed with error %.8X",
2915+ (unsigned) err);
2916+
2917+ err = SessionGetInfo(callerSecuritySession, &sid,
2918+ &sattrs);
2919+ if (err)
2920+ error("SessionGetInfo() failed with error %.8X",
2921+ (unsigned) err);
2922+ else
2923+ debug("New Session ID is %.8X / Session Attributes are %.8X",
2924+ (unsigned) sid, (unsigned) sattrs);
2925+ }
2926+ }
2927+#endif
2928+
2929 /*
2930 * We don't want to listen forever unless the other side
2931 * successfully authenticates itself. So we set up an alarm which is
2932@@ -2561,6 +2622,48 @@ do_ssh2_kex(void)
2933 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
2934 list_hostkey_types());
2935
2936+#ifdef GSSAPI
2937+ {
2938+ char *orig;
2939+ char *gss = NULL;
2940+ char *newstr = NULL;
2941+ orig = myproposal[PROPOSAL_KEX_ALGS];
2942+
2943+ /*
2944+ * If we don't have a host key, then there's no point advertising
2945+ * the other key exchange algorithms
2946+ */
2947+
2948+ if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
2949+ orig = NULL;
2950+
2951+ if (options.gss_keyex)
2952+ gss = ssh_gssapi_server_mechanisms();
2953+ else
2954+ gss = NULL;
2955+
2956+ if (gss && orig)
2957+ xasprintf(&newstr, "%s,%s", gss, orig);
2958+ else if (gss)
2959+ newstr = gss;
2960+ else if (orig)
2961+ newstr = orig;
2962+
2963+ /*
2964+ * If we've got GSSAPI mechanisms, then we've got the 'null' host
2965+ * key alg, but we can't tell people about it unless its the only
2966+ * host key algorithm we support
2967+ */
2968+ if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0)
2969+ myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = "null";
2970+
2971+ if (newstr)
2972+ myproposal[PROPOSAL_KEX_ALGS] = newstr;
2973+ else
2974+ fatal("No supported key exchange algorithms");
2975+ }
2976+#endif
2977+
2978 /* start key exchange */
2979 if ((r = kex_setup(active_state, myproposal)) != 0)
2980 fatal("kex_setup: %s", ssh_err(r));
2981@@ -2575,6 +2678,13 @@ do_ssh2_kex(void)
2982 # endif
2983 #endif
2984 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
2985+#ifdef GSSAPI
2986+ if (options.gss_keyex) {
2987+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
2988+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
2989+ kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
2990+ }
2991+#endif
2992 kex->server = 1;
2993 kex->client_version_string=client_version_string;
2994 kex->server_version_string=server_version_string;
2995diff --git a/sshd_config b/sshd_config
2996index c9042ac..a71ad19 100644
2997--- a/sshd_config
2998+++ b/sshd_config
2999@@ -84,6 +84,8 @@ AuthorizedKeysFile .ssh/authorized_keys
3000 # GSSAPI options
3001 #GSSAPIAuthentication no
3002 #GSSAPICleanupCredentials yes
3003+#GSSAPIStrictAcceptorCheck yes
3004+#GSSAPIKeyExchange no
3005
3006 # Set this to 'yes' to enable PAM authentication, account processing,
3007 # and session processing. If this is enabled, PAM authentication will
3008diff --git a/sshd_config.5 b/sshd_config.5
3009index 6dce0c7..0331496 100644
3010--- a/sshd_config.5
3011+++ b/sshd_config.5
3012@@ -564,12 +564,40 @@ Specifies whether user authentication based on GSSAPI is allowed.
3013 The default is
3014 .Dq no .
3015 Note that this option applies to protocol version 2 only.
3016+.It Cm GSSAPIKeyExchange
3017+Specifies whether key exchange based on GSSAPI is allowed. GSSAPI key exchange
3018+doesn't rely on ssh keys to verify host identity.
3019+The default is
3020+.Dq no .
3021+Note that this option applies to protocol version 2 only.
3022 .It Cm GSSAPICleanupCredentials
3023 Specifies whether to automatically destroy the user's credentials cache
3024 on logout.
3025 The default is
3026 .Dq yes .
3027 Note that this option applies to protocol version 2 only.
3028+.It Cm GSSAPIStrictAcceptorCheck
3029+Determines whether to be strict about the identity of the GSSAPI acceptor
3030+a client authenticates against. If
3031+.Dq yes
3032+then the client must authenticate against the
3033+.Pa host
3034+service on the current hostname. If
3035+.Dq no
3036+then the client may authenticate against any service key stored in the
3037+machine's default store. This facility is provided to assist with operation
3038+on multi homed machines.
3039+The default is
3040+.Dq yes .
3041+Note that this option applies only to protocol version 2 GSSAPI connections,
3042+and setting it to
3043+.Dq no
3044+may only work with recent Kerberos GSSAPI libraries.
3045+.It Cm GSSAPIStoreCredentialsOnRekey
3046+Controls whether the user's GSSAPI credentials should be updated following a
3047+successful connection rekeying. This option can be used to accepted renewed
3048+or updated credentials from a compatible client. The default is
3049+.Dq no .
3050 .It Cm HostbasedAcceptedKeyTypes
3051 Specifies the key types that will be accepted for hostbased authentication
3052 as a comma-separated pattern list.
3053diff --git a/sshkey.c b/sshkey.c
3054index 4768790..cd5992e 100644
3055--- a/sshkey.c
3056+++ b/sshkey.c
3057@@ -116,6 +116,7 @@ static const struct keytype keytypes[] = {
3058 { "ssh-dss-cert-v00@openssh.com", "DSA-CERT-V00",
3059 KEY_DSA_CERT_V00, 0, 1 },
3060 #endif /* WITH_OPENSSL */
3061+ { "null", "null", KEY_NULL, 0, 0 },
3062 { NULL, NULL, -1, -1, 0 }
3063 };
3064
3065@@ -204,7 +205,7 @@ key_alg_list(int certs_only, int plain_only)
3066 const struct keytype *kt;
3067
3068 for (kt = keytypes; kt->type != -1; kt++) {
3069- if (kt->name == NULL)
3070+ if (kt->name == NULL || kt->type == KEY_NULL)
3071 continue;
3072 if ((certs_only && !kt->cert) || (plain_only && kt->cert))
3073 continue;
3074diff --git a/sshkey.h b/sshkey.h
3075index 62c1c3e..9314e85 100644
3076--- a/sshkey.h
3077+++ b/sshkey.h
3078@@ -64,6 +64,7 @@ enum sshkey_types {
3079 KEY_ED25519_CERT,
3080 KEY_RSA_CERT_V00,
3081 KEY_DSA_CERT_V00,
3082+ KEY_NULL,
3083 KEY_UNSPEC
3084 };
3085