]>
Commit | Line | Data |
---|---|---|
b46fa825 | 1 | /* |
23987cb8 | 2 | * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. |
b46fa825 | 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" | |
23987cb8 | 42 | |
43 | static void kex_gss_send_error(Gssctxt *ctxt); | |
b46fa825 | 44 | |
45 | void | |
46 | kexgss_server(Kex *kex) | |
47 | { | |
b46fa825 | 48 | OM_uint32 maj_status, min_status; |
49 | ||
50 | /* Some GSSAPI implementations use the input value of ret_flags (an | |
23987cb8 | 51 | * output variable) as a means of triggering mechanism specific |
52 | * features. Initializing it to zero avoids inadvertently | |
53 | * activating this non-standard behaviour.*/ | |
b46fa825 | 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; | |
b46fa825 | 65 | u_int slen; |
23987cb8 | 66 | gss_OID oid; |
b46fa825 | 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 | ||
23987cb8 | 78 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt,oid)))) { |
79 | kex_gss_send_error(ctxt); | |
80 | packet_disconnect("Unable to acquire credentials for the server"); | |
81 | } | |
82 | ||
b46fa825 | 83 | do { |
84 | debug("Wait SSH2_MSG_GSSAPI_INIT"); | |
85 | type = packet_read(); | |
86 | switch(type) { | |
87 | case SSH2_MSG_KEXGSS_INIT: | |
88 | if (dh_client_pub!=NULL) | |
89 | packet_disconnect("Received KEXGSS_INIT after initialising"); | |
90 | recv_tok.value=packet_get_string(&slen); | |
91 | recv_tok.length=slen; /* int vs. size_t */ | |
92 | ||
93 | dh_client_pub = BN_new(); | |
94 | ||
95 | if (dh_client_pub == NULL) | |
23987cb8 | 96 | packet_disconnect("dh_client_pub == NULL"); |
b46fa825 | 97 | packet_get_bignum2(dh_client_pub); |
98 | ||
99 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ | |
100 | break; | |
101 | case SSH2_MSG_KEXGSS_CONTINUE: | |
102 | if (dh_client_pub == NULL) | |
103 | packet_disconnect("Received KEXGSS_CONTINUE without initialising"); | |
104 | recv_tok.value=packet_get_string(&slen); | |
105 | recv_tok.length=slen; /* int vs. size_t */ | |
106 | break; | |
107 | default: | |
108 | packet_disconnect("Protocol error: didn't expect packet type %d", | |
109 | type); | |
110 | } | |
111 | ||
112 | maj_status=PRIVSEP(ssh_gssapi_accept_ctx(ctxt,&recv_tok, | |
23987cb8 | 113 | &send_tok, &ret_flags)); |
b46fa825 | 114 | |
115 | gss_release_buffer(&min_status,&recv_tok); | |
23987cb8 | 116 | |
b46fa825 | 117 | #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG |
118 | if (ret_flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG) { | |
119 | packet_disconnect("Limited proxy is not allowed in gssapi key exchange."); | |
120 | } | |
121 | #endif | |
23987cb8 | 122 | |
123 | if ((maj_status & GSS_S_CONTINUE_NEEDED) || | |
124 | (GSS_ERROR(maj_status) && send_tok.length>0)) { | |
b46fa825 | 125 | debug("Sending GSSAPI_CONTINUE"); |
126 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
127 | packet_put_string(send_tok.value,send_tok.length); | |
128 | packet_send(); | |
129 | packet_write_wait(); | |
130 | gss_release_buffer(&min_status, &send_tok); | |
131 | } | |
132 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | |
133 | ||
134 | if (GSS_ERROR(maj_status)) { | |
23987cb8 | 135 | kex_gss_send_error(ctxt); |
b46fa825 | 136 | packet_disconnect("gssapi key exchange handshake failed"); |
137 | } | |
23987cb8 | 138 | |
b46fa825 | 139 | debug("gss_complete"); |
23987cb8 | 140 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) |
141 | packet_disconnect("gssapi_mutual authentication failed"); | |
b46fa825 | 142 | |
23987cb8 | 143 | if (!(ret_flags & GSS_C_INTEG_FLAG)) |
b46fa825 | 144 | packet_disconnect("gssapi channel integrity not established"); |
23987cb8 | 145 | |
b46fa825 | 146 | dh = dh_new_group1(); |
147 | dh_gen_key(dh, kex->we_need * 8); | |
148 | ||
149 | if (!dh_pub_is_valid(dh, dh_client_pub)) | |
150 | packet_disconnect("bad client public DH value"); | |
151 | ||
152 | klen = DH_size(dh); | |
153 | kbuf = xmalloc(klen); | |
154 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
155 | ||
156 | shared_secret = BN_new(); | |
157 | BN_bin2bn(kbuf, kout, shared_secret); | |
158 | memset(kbuf, 0, klen); | |
159 | xfree(kbuf); | |
160 | ||
23987cb8 | 161 | /* The GSSAPI hash is identical to the Diffie Helman one */ |
162 | hash = kex_dh_hash( | |
b46fa825 | 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)))) { | |
23987cb8 | 184 | kex_gss_send_error(ctxt); |
185 | fatal("Couldn't get MIC"); | |
b46fa825 | 186 | } |
187 | ||
188 | packet_start(SSH2_MSG_KEXGSS_COMPLETE); | |
189 | packet_put_bignum2(dh->pub_key); | |
190 | packet_put_string((char *)msg_tok.value,msg_tok.length); | |
191 | ||
192 | if (send_tok.length!=0) { | |
193 | packet_put_char(1); /* true */ | |
194 | packet_put_string((char *)send_tok.value,send_tok.length); | |
195 | } else { | |
196 | packet_put_char(0); /* false */ | |
197 | } | |
198 | packet_send(); | |
199 | packet_write_wait(); | |
200 | ||
201 | /* We used to store the client name and credentials here for later | |
202 | * use. With privsep, its easier to do this as a by product of the | |
203 | * call to accept_context, which stores delegated information when | |
204 | * the context is complete */ | |
205 | ||
206 | gss_release_buffer(&min_status, &send_tok); | |
207 | ||
208 | /* If we've got a context, delete it. It may be NULL if we've been | |
23987cb8 | 209 | * using privsep */ |
b46fa825 | 210 | ssh_gssapi_delete_ctx(&ctxt); |
211 | ||
212 | DH_free(dh); | |
213 | ||
214 | kex_derive_keys(kex, hash, shared_secret); | |
215 | BN_clear_free(shared_secret); | |
216 | kex_finish(kex); | |
217 | } | |
218 | ||
23987cb8 | 219 | static void |
220 | kex_gss_send_error(Gssctxt *ctxt) { | |
221 | char *errstr; | |
222 | OM_uint32 maj,min; | |
223 | ||
224 | errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min)); | |
225 | if (errstr) { | |
226 | packet_start(SSH2_MSG_KEXGSS_ERROR); | |
227 | packet_put_int(maj); | |
228 | packet_put_int(min); | |
229 | packet_put_cstring(errstr); | |
230 | packet_put_cstring(""); | |
231 | packet_send(); | |
232 | packet_write_wait(); | |
233 | /* XXX - We should probably log the error locally here */ | |
234 | xfree(errstr); | |
235 | } | |
236 | } | |
b46fa825 | 237 | #endif /* GSSAPI */ |