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