]> andersk Git - gssapi-openssh.git/blame - openssh/kexgsss.c
openssh-3.6.1p1-gssapi-20030416.diff from Simon
[gssapi-openssh.git] / openssh / kexgsss.c
CommitLineData
73b00670 1/*
2 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
26
27#ifdef GSSAPI
28
29#include <openssl/crypto.h>
30#include <openssl/bn.h>
31
32#include "xmalloc.h"
33#include "buffer.h"
34#include "bufaux.h"
35#include "kex.h"
36#include "log.h"
37#include "packet.h"
38#include "dh.h"
39#include "ssh2.h"
40#include "ssh-gss.h"
41#include "monitor_wrap.h"
42
43static void kex_gss_send_error(Gssctxt *ctxt);
44
45void
46kexgss_server(Kex *kex)
47{
48 OM_uint32 maj_status, min_status;
49
50 /* Some GSSAPI implementations use the input value of ret_flags (an
3e196425 51 * output variable) as a means of triggering mechanism specific
52 * features. Initializing it to zero avoids inadvertently
53 * activating this non-standard behaviour.*/
73b00670 54
55 OM_uint32 ret_flags = 0;
56 gss_buffer_desc gssbuf,send_tok,recv_tok,msg_tok;
57 Gssctxt *ctxt = NULL;
58 unsigned int klen, kout;
59 unsigned char *kbuf;
60 unsigned char *hash;
61 DH *dh;
62 BIGNUM *shared_secret = NULL;
63 BIGNUM *dh_client_pub = NULL;
64 int type =0;
65 gss_OID oid;
66
67 /* Initialise GSSAPI */
68
69 debug2("%s: Identifying %s",__func__,kex->name);
3e196425 70 oid=ssh_gssapi_server_id_kex(kex->name);
73b00670 71 if (oid==NULL) {
72 fatal("Unknown gssapi mechanism");
73 }
74
75 debug2("%s: Acquiring credentials",__func__);
76
77 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt,oid)))) {
78 kex_gss_send_error(ctxt);
79 fatal("Unable to acquire credentials for the server");
80 }
81
82 do {
83 debug("Wait SSH2_MSG_GSSAPI_INIT");
84 type = packet_read();
85 switch(type) {
86 case SSH2_MSG_KEXGSS_INIT:
87 if (dh_client_pub!=NULL)
88 fatal("Received KEXGSS_INIT after initialising");
89 recv_tok.value=packet_get_string(&recv_tok.length);
90
91 dh_client_pub = BN_new();
92
93 if (dh_client_pub == NULL)
94 fatal("dh_client_pub == NULL");
95 packet_get_bignum2(dh_client_pub);
96
97 /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
98 break;
99 case SSH2_MSG_KEXGSS_CONTINUE:
73b00670 100 recv_tok.value=packet_get_string(&recv_tok.length);
101 break;
102 default:
103 packet_disconnect("Protocol error: didn't expect packet type %d",
104 type);
105 }
106
107 maj_status=PRIVSEP(ssh_gssapi_accept_ctx(ctxt,&recv_tok,
3e196425 108 &send_tok, &ret_flags));
73b00670 109
110 gss_release_buffer(&min_status,&recv_tok);
111
3e196425 112 if (maj_status!=GSS_S_COMPLETE && send_tok.length==0) {
113 fatal("Zero length token output when incomplete");
114 }
115
116 if (dh_client_pub == NULL)
117 fatal("No client public key");
118
119 if (maj_status & GSS_S_CONTINUE_NEEDED) {
73b00670 120 debug("Sending GSSAPI_CONTINUE");
121 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
122 packet_put_string(send_tok.value,send_tok.length);
123 packet_send();
124 packet_write_wait();
125 gss_release_buffer(&min_status, &send_tok);
126 }
127 } while (maj_status & GSS_S_CONTINUE_NEEDED);
128
129 if (GSS_ERROR(maj_status)) {
130 kex_gss_send_error(ctxt);
3e196425 131 if (send_tok.length>0) {
132 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
133 packet_put_string(send_tok.value,send_tok.length);
134 packet_send();
135 packet_write_wait();
136 }
73b00670 137 fatal("accept_ctx died");
138 }
139
140 debug("gss_complete");
141 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
142 fatal("mutual authentication flag wasn't set");
143
144 if (!(ret_flags & GSS_C_INTEG_FLAG))
145 fatal("Integrity flag wasn't set");
146
147 dh = dh_new_group1();
148 dh_gen_key(dh, kex->we_need * 8);
149
150 if (!dh_pub_is_valid(dh, dh_client_pub))
151 packet_disconnect("bad client public DH value");
152
153 klen = DH_size(dh);
154 kbuf = xmalloc(klen);
155 kout = DH_compute_key(kbuf, dh_client_pub, dh);
156
157 shared_secret = BN_new();
158 BN_bin2bn(kbuf, kout, shared_secret);
159 memset(kbuf, 0, klen);
160 xfree(kbuf);
161
162 /* The GSSAPI hash is identical to the Diffie Helman one */
163 hash = kex_dh_hash(
164 kex->client_version_string,
165 kex->server_version_string,
166 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
167 buffer_ptr(&kex->my), buffer_len(&kex->my),
168 NULL, 0, /* Change this if we start sending host keys */
169 dh_client_pub,
170 dh->pub_key,
171 shared_secret
172 );
173 BN_free(dh_client_pub);
174
175 if (kex->session_id == NULL) {
176 kex->session_id_len = 20;
177 kex->session_id = xmalloc(kex->session_id_len);
178 memcpy(kex->session_id, hash, kex->session_id_len);
179 }
180
181 gssbuf.value = hash;
182 gssbuf.length = 20; /* Hashlen appears to always be 20 */
183
184 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok)))) {
185 kex_gss_send_error(ctxt);
186 fatal("Couldn't get MIC");
187 }
188
189 packet_start(SSH2_MSG_KEXGSS_COMPLETE);
190 packet_put_bignum2(dh->pub_key);
191 packet_put_string((char *)msg_tok.value,msg_tok.length);
192
193 if (send_tok.length!=0) {
194 packet_put_char(1); /* true */
195 packet_put_string((char *)send_tok.value,send_tok.length);
196 } else {
197 packet_put_char(0); /* false */
198 }
199 packet_send();
200 packet_write_wait();
201
202 /* We used to store the client name and credentials here for later
203 * use. With privsep, its easier to do this as a by product of the
204 * call to accept_context, which stores delegated information when
205 * the context is complete */
206
207 gss_release_buffer(&min_status, &send_tok);
208
209 /* If we've got a context, delete it. It may be NULL if we've been
3e196425 210 * using privsep */
73b00670 211 ssh_gssapi_delete_ctx(&ctxt);
212
213 DH_free(dh);
214
215 kex_derive_keys(kex, hash, shared_secret);
216 BN_clear_free(shared_secret);
217 kex_finish(kex);
218}
219
220static void
221kex_gss_send_error(Gssctxt *ctxt) {
222 char *errstr;
223 OM_uint32 maj,min;
224
225 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
226 if (errstr) {
227 packet_start(SSH2_MSG_KEXGSS_ERROR);
228 packet_put_int(maj);
229 packet_put_int(min);
230 packet_put_cstring(errstr);
231 packet_put_cstring("");
232 packet_send();
233 packet_write_wait();
234 /* XXX - We should probably log the error locally here */
235 xfree(errstr);
236 }
237}
238#endif /* GSSAPI */
This page took 0.076633 seconds and 5 git commands to generate.