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