diff options
Diffstat (limited to 'kexgssc.c')
-rw-r--r-- | kexgssc.c | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/kexgssc.c b/kexgssc.c new file mode 100644 index 000000000..eee96dd23 --- /dev/null +++ b/kexgssc.c | |||
@@ -0,0 +1,242 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2001-2004 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 "canohost.h" | ||
40 | #include "ssh2.h" | ||
41 | #include "ssh-gss.h" | ||
42 | |||
43 | void | ||
44 | kexgss_client(Kex *kex) { | ||
45 | gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok, *token_ptr; | ||
46 | Gssctxt *ctxt; | ||
47 | OM_uint32 maj_status, min_status, ret_flags; | ||
48 | unsigned int klen, kout; | ||
49 | DH *dh; | ||
50 | BIGNUM *dh_server_pub = 0; | ||
51 | BIGNUM *shared_secret = 0; | ||
52 | unsigned char *kbuf; | ||
53 | unsigned char *hash; | ||
54 | unsigned char *serverhostkey; | ||
55 | char *msg; | ||
56 | char *lang; | ||
57 | int type = 0; | ||
58 | int first = 1; | ||
59 | int slen = 0; | ||
60 | u_int strlen; | ||
61 | |||
62 | ssh_gssapi_build_ctx(&ctxt); | ||
63 | if (ssh_gssapi_id_kex(ctxt,kex->name) == NULL) | ||
64 | fatal("Couldn't identify host exchange"); | ||
65 | |||
66 | if (ssh_gssapi_import_name(ctxt,get_canonical_hostname(1))) | ||
67 | fatal("Couldn't import hostname "); | ||
68 | |||
69 | /* This code should match that in ssh_dh1_client */ | ||
70 | |||
71 | /* Step 1 - e is dh->pub_key */ | ||
72 | dh = dh_new_group1(); | ||
73 | dh_gen_key(dh, kex->we_need * 8); | ||
74 | |||
75 | /* This is f, we initialise it now to make life easier */ | ||
76 | dh_server_pub = BN_new(); | ||
77 | if (dh_server_pub == NULL) | ||
78 | fatal("dh_server_pub == NULL"); | ||
79 | |||
80 | token_ptr = GSS_C_NO_BUFFER; | ||
81 | |||
82 | do { | ||
83 | debug("Calling gss_init_sec_context"); | ||
84 | |||
85 | maj_status = ssh_gssapi_init_ctx(ctxt, | ||
86 | kex->gss_deleg_creds, token_ptr, &send_tok, | ||
87 | &ret_flags); | ||
88 | |||
89 | if (GSS_ERROR(maj_status)) { | ||
90 | if (send_tok.length != 0) { | ||
91 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | ||
92 | packet_put_string(send_tok.value, | ||
93 | send_tok.length); | ||
94 | } | ||
95 | fatal("gss_init_context failed"); | ||
96 | } | ||
97 | |||
98 | /* If we've got an old receive buffer get rid of it */ | ||
99 | if (token_ptr != GSS_C_NO_BUFFER) | ||
100 | (void) gss_release_buffer(&min_status, &recv_tok); | ||
101 | |||
102 | if (maj_status == GSS_S_COMPLETE) { | ||
103 | /* If mutual state flag is not true, kex fails */ | ||
104 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) | ||
105 | fatal("Mutual authentication failed"); | ||
106 | |||
107 | /* If integ avail flag is not true kex fails */ | ||
108 | if (!(ret_flags & GSS_C_INTEG_FLAG)) | ||
109 | fatal("Integrity check failed"); | ||
110 | } | ||
111 | |||
112 | /* | ||
113 | * If we have data to send, then the last message that we | ||
114 | * received cannot have been a 'complete'. | ||
115 | */ | ||
116 | if (send_tok.length != 0) { | ||
117 | if (first) { | ||
118 | packet_start(SSH2_MSG_KEXGSS_INIT); | ||
119 | packet_put_string(send_tok.value, | ||
120 | send_tok.length); | ||
121 | packet_put_bignum2(dh->pub_key); | ||
122 | first = 0; | ||
123 | } else { | ||
124 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | ||
125 | packet_put_string(send_tok.value, | ||
126 | send_tok.length); | ||
127 | } | ||
128 | packet_send(); | ||
129 | |||
130 | /* If we've sent them data, they should reply */ | ||
131 | |||
132 | type = packet_read(); | ||
133 | switch (type) { | ||
134 | case SSH2_MSG_KEXGSS_HOSTKEY: | ||
135 | debug("Received KEXGSS_HOSTKEY"); | ||
136 | serverhostkey = packet_get_string(&slen); | ||
137 | break; | ||
138 | case SSH2_MSG_KEXGSS_CONTINUE: | ||
139 | debug("Received GSSAPI_CONTINUE"); | ||
140 | if (maj_status == GSS_S_COMPLETE) | ||
141 | fatal("GSSAPI Continue received from server when complete"); | ||
142 | recv_tok.value = packet_get_string(&strlen); | ||
143 | recv_tok.length = strlen; | ||
144 | break; | ||
145 | case SSH2_MSG_KEXGSS_COMPLETE: | ||
146 | debug("Received GSSAPI_COMPLETE"); | ||
147 | packet_get_bignum2(dh_server_pub); | ||
148 | msg_tok.value = packet_get_string(&strlen); | ||
149 | msg_tok.length = strlen; | ||
150 | |||
151 | /* Is there a token included? */ | ||
152 | if (packet_get_char()) { | ||
153 | recv_tok.value= | ||
154 | packet_get_string(&strlen); | ||
155 | recv_tok.length = strlen; | ||
156 | /* If we're already complete - protocol error */ | ||
157 | if (maj_status == GSS_S_COMPLETE) | ||
158 | packet_disconnect("Protocol error: received token when complete"); | ||
159 | } else { | ||
160 | /* No token included */ | ||
161 | if (maj_status != GSS_S_COMPLETE) | ||
162 | packet_disconnect("Protocol error: did not receive final token"); | ||
163 | } | ||
164 | break; | ||
165 | case SSH2_MSG_KEXGSS_ERROR: | ||
166 | debug("Received Error"); | ||
167 | maj_status = packet_get_int(); | ||
168 | min_status = packet_get_int(); | ||
169 | msg = packet_get_string(NULL); | ||
170 | lang = packet_get_string(NULL); | ||
171 | fprintf(stderr,"GSSAPI Error: \n%s",msg); | ||
172 | default: | ||
173 | packet_disconnect("Protocol error: didn't expect packet type %d", | ||
174 | type); | ||
175 | } | ||
176 | token_ptr = &recv_tok; | ||
177 | } else { | ||
178 | /* No data, and not complete */ | ||
179 | if (maj_status != GSS_S_COMPLETE) | ||
180 | fatal("Not complete, and no token output"); | ||
181 | } | ||
182 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | ||
183 | |||
184 | /* | ||
185 | * We _must_ have received a COMPLETE message in reply from the | ||
186 | * server, which will have set dh_server_pub and msg_tok | ||
187 | */ | ||
188 | |||
189 | if (type!=SSH2_MSG_KEXGSS_COMPLETE) | ||
190 | fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it"); | ||
191 | |||
192 | /* Check f in range [1, p-1] */ | ||
193 | if (!dh_pub_is_valid(dh, dh_server_pub)) | ||
194 | packet_disconnect("bad server public DH value"); | ||
195 | |||
196 | /* compute K=f^x mod p */ | ||
197 | klen = DH_size(dh); | ||
198 | kbuf = xmalloc(klen); | ||
199 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | ||
200 | |||
201 | shared_secret = BN_new(); | ||
202 | BN_bin2bn(kbuf,kout, shared_secret); | ||
203 | memset(kbuf, 0, klen); | ||
204 | xfree(kbuf); | ||
205 | |||
206 | /* The GSS hash is identical to the DH one */ | ||
207 | hash = kex_dh_hash( kex->client_version_string, | ||
208 | kex->server_version_string, | ||
209 | buffer_ptr(&kex->my), buffer_len(&kex->my), | ||
210 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | ||
211 | serverhostkey, slen, /* server host key */ | ||
212 | dh->pub_key, /* e */ | ||
213 | dh_server_pub, /* f */ | ||
214 | shared_secret /* K */ | ||
215 | ); | ||
216 | |||
217 | gssbuf.value = hash; | ||
218 | gssbuf.length = 20; | ||
219 | |||
220 | /* Verify that the hash matches the MIC we just got. */ | ||
221 | if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok))) | ||
222 | packet_disconnect("Hash's MIC didn't verify"); | ||
223 | |||
224 | DH_free(dh); | ||
225 | /* save session id */ | ||
226 | if (kex->session_id == NULL) { | ||
227 | kex->session_id_len = 20; | ||
228 | kex->session_id = xmalloc(kex->session_id_len); | ||
229 | memcpy(kex->session_id, hash, kex->session_id_len); | ||
230 | } | ||
231 | |||
232 | if (gss_kex_context == NULL) | ||
233 | gss_kex_context = ctxt; | ||
234 | else | ||
235 | ssh_gssapi_delete_ctx(&ctxt); | ||
236 | |||
237 | kex_derive_keys(kex, hash, shared_secret); | ||
238 | BN_clear_free(shared_secret); | ||
239 | kex_finish(kex); | ||
240 | } | ||
241 | |||
242 | #endif /* GSSAPI */ | ||