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