]> andersk Git - gssapi-openssh.git/blame - openssh/kexgsss.c
moved kexgss_client() from kexgss.c to kexgssc.c and
[gssapi-openssh.git] / openssh / kexgsss.c
CommitLineData
b46fa825 1/*
2 * Copyright (c) 2001,2002 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#include "canohost.h"
43
44void
45kexgss_server(Kex *kex)
46{
47
48 OM_uint32 maj_status, min_status;
49
50 /* Some GSSAPI implementations use the input value of ret_flags (an
51 * output variable) as a means of triggering mechanism specific
52 * features. Initializing it to zero avoids inadvertently
53 * activating this non-standard behaviour.*/
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 u_int slen;
67
68 /* Initialise GSSAPI */
69
70 debug2("%s: Identifying %s",__func__,kex->name);
71 oid=ssh_gssapi_id_kex(ctxt,kex->name);
72 if (oid==NULL) {
73 packet_disconnect("Unknown gssapi mechanism");
74 }
75
76 debug2("%s: Acquiring credentials",__func__);
77
78 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt,oid))))
79 packet_disconnect("Unable to acquire credentials for the server");
80
81 do {
82 debug("Wait SSH2_MSG_GSSAPI_INIT");
83 type = packet_read();
84 switch(type) {
85 case SSH2_MSG_KEXGSS_INIT:
86 if (dh_client_pub!=NULL)
87 packet_disconnect("Received KEXGSS_INIT after initialising");
88 recv_tok.value=packet_get_string(&slen);
89 recv_tok.length=slen; /* int vs. size_t */
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:
100 if (dh_client_pub == NULL)
101 packet_disconnect("Received KEXGSS_CONTINUE without initialising");
102 recv_tok.value=packet_get_string(&slen);
103 recv_tok.length=slen; /* int vs. size_t */
104 break;
105 default:
106 packet_disconnect("Protocol error: didn't expect packet type %d",
107 type);
108 }
109
110 maj_status=PRIVSEP(ssh_gssapi_accept_ctx(ctxt,&recv_tok,
111 &send_tok, &ret_flags));
112
113 gss_release_buffer(&min_status,&recv_tok);
114
115#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
116 if (ret_flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG) {
117 packet_disconnect("Limited proxy is not allowed in gssapi key exchange.");
118 }
119#endif
120
121 if (maj_status & GSS_S_CONTINUE_NEEDED) {
122 debug("Sending GSSAPI_CONTINUE");
123 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
124 packet_put_string(send_tok.value,send_tok.length);
125 packet_send();
126 packet_write_wait();
127 gss_release_buffer(&min_status, &send_tok);
128 }
129 } while (maj_status & GSS_S_CONTINUE_NEEDED);
130
131 if (GSS_ERROR(maj_status)) {
132 ssh_gssapi_send_error(oid,maj_status,min_status);
133 packet_disconnect("gssapi key exchange handshake failed");
134 }
135
136 debug("gss_complete");
137 if (!(ret_flags & GSS_C_MUTUAL_FLAG)) {
138 ssh_gssapi_send_error(oid,maj_status,min_status);
139 packet_disconnect("gssapi mutual authentication failed");
140 }
141
142 if (!(ret_flags & GSS_C_INTEG_FLAG)) {
143 ssh_gssapi_send_error(oid,maj_status,min_status);
144 packet_disconnect("gssapi channel integrity not established");
145 }
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 hash = kex_gssapi_hash(
163 kex->client_version_string,
164 kex->server_version_string,
165 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
166 buffer_ptr(&kex->my), buffer_len(&kex->my),
167 NULL, 0, /* Change this if we start sending host keys */
168 dh_client_pub,
169 dh->pub_key,
170 shared_secret
171 );
172 BN_free(dh_client_pub);
173
174 if (kex->session_id == NULL) {
175 kex->session_id_len = 20;
176 kex->session_id = xmalloc(kex->session_id_len);
177 memcpy(kex->session_id, hash, kex->session_id_len);
178 }
179
180 gssbuf.value = hash;
181 gssbuf.length = 20; /* Hashlen appears to always be 20 */
182
183 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok)))) {
184 if (ctxt) { /* may be NULL under privsep */
185 ssh_gssapi_send_error(ctxt->oid,maj_status,min_status);
186 } else {
187 ssh_gssapi_send_error(GSS_C_NO_OID,maj_status,min_status);
188 }
189 packet_disconnect("Couldn't get MIC");
190 }
191
192 packet_start(SSH2_MSG_KEXGSS_COMPLETE);
193 packet_put_bignum2(dh->pub_key);
194 packet_put_string((char *)msg_tok.value,msg_tok.length);
195
196 if (send_tok.length!=0) {
197 packet_put_char(1); /* true */
198 packet_put_string((char *)send_tok.value,send_tok.length);
199 } else {
200 packet_put_char(0); /* false */
201 }
202 packet_send();
203 packet_write_wait();
204
205 /* We used to store the client name and credentials here for later
206 * use. With privsep, its easier to do this as a by product of the
207 * call to accept_context, which stores delegated information when
208 * the context is complete */
209
210 gss_release_buffer(&min_status, &send_tok);
211
212 /* If we've got a context, delete it. It may be NULL if we've been
213 * using privsep */
214 ssh_gssapi_delete_ctx(&ctxt);
215
216 DH_free(dh);
217
218 kex_derive_keys(kex, hash, shared_secret);
219 BN_clear_free(shared_secret);
220 kex_finish(kex);
221}
222
223#endif /* GSSAPI */
This page took 0.129206 seconds and 5 git commands to generate.