]>
Commit | Line | Data |
---|---|---|
416fd2a8 | 1 | /* $OpenBSD: gss-genr.c,v 1.3 2003/11/21 11:57:03 djm Exp $ */ |
70791e56 | 2 | |
b9a54c29 | 3 | /* |
70791e56 | 4 | * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. |
5 | * | |
b9a54c29 | 6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 | */ | |
26 | ||
27 | #include "includes.h" | |
28 | ||
29 | #ifdef GSSAPI | |
30 | ||
b9a54c29 | 31 | #include "xmalloc.h" |
32 | #include "buffer.h" | |
33 | #include "bufaux.h" | |
b9a54c29 | 34 | #include "compat.h" |
35 | #include <openssl/evp.h> | |
b9a54c29 | 36 | #include "kex.h" |
37 | #include "log.h" | |
ff2d7a98 | 38 | #include "monitor_wrap.h" |
c2397a66 | 39 | #include "canohost.h" |
416fd2a8 | 40 | #include "ssh2.h" |
b9a54c29 | 41 | |
42 | #include "ssh-gss.h" | |
43 | ||
416fd2a8 | 44 | extern u_char *session_id2; |
45 | extern u_int session_id2_len; | |
46 | ||
88928908 | 47 | typedef struct { |
48 | char *encoded; | |
49 | gss_OID oid; | |
50 | } ssh_gss_kex_mapping; | |
51 | ||
52 | static ssh_gss_kex_mapping *gss_enc2oid; | |
b9a54c29 | 53 | |
54 | /* Return a list of the gss-group1-sha1-x mechanisms supported by this | |
55 | * program. | |
56 | * | |
88928908 | 57 | * On the client side, we don't need to worry about whether we 'know' |
58 | * about the mechanism or not - we assume that any mechanism that we've been | |
59 | * linked against is suitable for inclusion. | |
b9a54c29 | 60 | * |
88928908 | 61 | * XXX - We might want to make this configurable in the future, so as to |
62 | * XXX - allow the user control over which mechanisms to use. | |
b9a54c29 | 63 | */ |
64 | ||
65 | char * | |
88928908 | 66 | ssh_gssapi_client_mechanisms(char *host) { |
b9a54c29 | 67 | gss_OID_set supported; |
88928908 | 68 | OM_uint32 min_status; |
b9a54c29 | 69 | Buffer buf; |
70 | int i = 0; | |
88928908 | 71 | char *mechs; |
72 | char *encoded; | |
73 | int enclen; | |
74 | char digest[EVP_MAX_MD_SIZE]; | |
75 | char deroid[2]; | |
76 | const EVP_MD *evp_md = EVP_md5(); | |
77 | EVP_MD_CTX md; | |
78 | int oidpos=0; | |
79 | ||
b9a54c29 | 80 | if (datafellows & SSH_OLD_GSSAPI) return NULL; |
81 | ||
88928908 | 82 | gss_indicate_mechs(&min_status,&supported); |
83 | if (datafellows & SSH_BUG_GSSAPI_BER) { | |
84 | gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping) | |
85 | *((supported->count*2)+1)); | |
86 | } else { | |
87 | gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping) | |
88 | *(supported->count+1)); | |
89 | } | |
b9a54c29 | 90 | |
88928908 | 91 | buffer_init(&buf); |
92 | ||
93 | ||
94 | for (i=0;i<supported->count;i++) { | |
95 | ||
96 | gss_enc2oid[oidpos].encoded=NULL; | |
97 | ||
98 | if (supported->elements[i].length<128 && | |
99 | ssh_gssapi_check_mechanism(&(supported->elements[i]),host)) { | |
100 | ||
101 | /* Earlier versions of this code interpreted the | |
102 | * spec incorrectly with regard to OID encoding. They | |
103 | * also mis-encoded the krb5 OID. The following | |
104 | * _temporary_ code interfaces with these broken | |
105 | * servers */ | |
106 | ||
107 | if (datafellows & SSH_BUG_GSSAPI_BER) { | |
108 | char *bodge=NULL; | |
109 | gss_OID_desc krb5oid={9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"}; | |
110 | gss_OID_desc gsioid={9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"}; | |
111 | ||
112 | if (supported->elements[i].length==krb5oid.length && | |
113 | memcmp(supported->elements[i].elements, | |
114 | krb5oid.elements, krb5oid.length)==0) { | |
115 | bodge="Se3H81ismmOC3OE+FwYCiQ=="; | |
116 | } | |
117 | ||
118 | if (supported->elements[i].length==gsioid.length && | |
119 | memcmp(supported->elements[i].elements, | |
120 | gsioid.elements, gsioid.length)==0) { | |
121 | bodge="N3+k7/4wGxHyuP8Yxi4RhA=="; | |
122 | } | |
123 | ||
124 | if (bodge) { | |
125 | if (oidpos!=0) { | |
126 | buffer_put_char(&buf,','); | |
127 | } | |
128 | ||
129 | buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1); | |
130 | buffer_append(&buf, bodge, strlen(bodge)); | |
131 | ||
132 | gss_enc2oid[oidpos].oid=&(supported->elements[i]); | |
133 | gss_enc2oid[oidpos].encoded=bodge; | |
134 | ||
135 | oidpos++; | |
c2397a66 | 136 | } |
c2397a66 | 137 | } |
88928908 | 138 | |
139 | /* Add the required DER encoding octets and MD5 hash */ | |
140 | deroid[0]=0x06; /* Object Identifier */ | |
141 | deroid[1]=supported->elements[i].length; | |
142 | ||
143 | EVP_DigestInit(&md, evp_md); | |
144 | EVP_DigestUpdate(&md,deroid,2); | |
145 | EVP_DigestUpdate(&md, | |
146 | supported->elements[i].elements, | |
147 | supported->elements[i].length); | |
148 | EVP_DigestFinal(&md, digest, NULL); | |
149 | ||
150 | /* Base64 encode it */ | |
151 | encoded=xmalloc(EVP_MD_size(evp_md)*2); | |
152 | enclen=__b64_ntop(digest, EVP_MD_size(evp_md), | |
153 | encoded,EVP_MD_size(evp_md)*2); | |
154 | if (oidpos!=0) { | |
155 | buffer_put_char(&buf,','); | |
156 | } | |
157 | buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1); | |
158 | buffer_append(&buf, encoded, enclen); | |
159 | ||
160 | debug("Mechanism encoded as %s",encoded); | |
161 | ||
162 | gss_enc2oid[oidpos].oid=&(supported->elements[i]); | |
163 | gss_enc2oid[oidpos].encoded=encoded; | |
164 | oidpos++; | |
c2397a66 | 165 | } |
88928908 | 166 | } |
167 | gss_enc2oid[oidpos].oid=NULL; | |
168 | gss_enc2oid[oidpos].encoded=NULL; | |
b9a54c29 | 169 | |
170 | buffer_put_char(&buf,'\0'); | |
171 | ||
172 | mechs=xmalloc(buffer_len(&buf)); | |
173 | buffer_get(&buf,mechs,buffer_len(&buf)); | |
174 | buffer_free(&buf); | |
175 | if (strlen(mechs)==0) | |
88928908 | 176 | return(NULL); |
b9a54c29 | 177 | else |
88928908 | 178 | return(mechs); |
179 | } | |
180 | ||
181 | gss_OID | |
182 | ssh_gssapi_client_id_kex(Gssctxt *ctx, char *name) { | |
183 | int i=0; | |
184 | ||
185 | if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) { | |
186 | return(NULL); | |
187 | } | |
188 | ||
189 | name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the ID string */ | |
190 | ||
191 | while (gss_enc2oid[i].encoded!=NULL && | |
192 | strcmp(name,gss_enc2oid[i].encoded)!=0) { | |
193 | i++; | |
194 | } | |
195 | ||
196 | if (gss_enc2oid[i].oid!=NULL) { | |
197 | ssh_gssapi_set_oid(ctx,gss_enc2oid[i].oid); | |
198 | } | |
199 | ||
200 | return gss_enc2oid[i].oid; | |
b9a54c29 | 201 | } |
202 | ||
88928908 | 203 | /* Check that the OID in a data stream matches that in the context */ |
70791e56 | 204 | int |
205 | ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len) | |
206 | { | |
207 | return (ctx != NULL && ctx->oid != GSS_C_NO_OID && | |
208 | ctx->oid->length == len && | |
209 | memcmp(ctx->oid->elements, data, len) == 0); | |
88928908 | 210 | } |
70791e56 | 211 | |
b9a54c29 | 212 | /* Set the contexts OID from a data stream */ |
70791e56 | 213 | void |
214 | ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) | |
215 | { | |
216 | if (ctx->oid != GSS_C_NO_OID) { | |
217 | xfree(ctx->oid->elements); | |
218 | xfree(ctx->oid); | |
219 | } | |
220 | ctx->oid = xmalloc(sizeof(gss_OID_desc)); | |
221 | ctx->oid->length = len; | |
222 | ctx->oid->elements = xmalloc(len); | |
223 | memcpy(ctx->oid->elements, data, len); | |
b9a54c29 | 224 | } |
225 | ||
226 | /* Set the contexts OID */ | |
70791e56 | 227 | void |
228 | ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) | |
229 | { | |
230 | ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length); | |
b9a54c29 | 231 | } |
232 | ||
88928908 | 233 | /* All this effort to report an error ... */ |
88928908 | 234 | void |
70791e56 | 235 | ssh_gssapi_error(Gssctxt *ctxt) |
236 | { | |
237 | debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL)); | |
b9a54c29 | 238 | } |
239 | ||
88928908 | 240 | char * |
70791e56 | 241 | ssh_gssapi_last_error(Gssctxt *ctxt, |
242 | OM_uint32 *major_status, OM_uint32 *minor_status) | |
243 | { | |
88928908 | 244 | OM_uint32 lmin; |
70791e56 | 245 | gss_buffer_desc msg = GSS_C_EMPTY_BUFFER; |
246 | OM_uint32 ctx; | |
247 | Buffer b; | |
248 | char *ret; | |
249 | ||
250 | buffer_init(&b); | |
251 | ||
252 | if (major_status != NULL) | |
253 | *major_status = ctxt->major; | |
254 | if (minor_status != NULL) | |
255 | *minor_status = ctxt->minor; | |
256 | ||
257 | ctx = 0; | |
b9a54c29 | 258 | /* The GSSAPI error */ |
70791e56 | 259 | do { |
260 | gss_display_status(&lmin, ctxt->major, | |
261 | GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg); | |
262 | ||
263 | buffer_append(&b, msg.value, msg.length); | |
264 | buffer_put_char(&b, '\n'); | |
265 | ||
266 | gss_release_buffer(&lmin, &msg); | |
267 | } while (ctx != 0); | |
268 | ||
269 | /* The mechanism specific error */ | |
270 | do { | |
271 | gss_display_status(&lmin, ctxt->minor, | |
272 | GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg); | |
273 | ||
274 | buffer_append(&b, msg.value, msg.length); | |
275 | buffer_put_char(&b, '\n'); | |
276 | ||
277 | gss_release_buffer(&lmin, &msg); | |
278 | } while (ctx != 0); | |
279 | ||
280 | buffer_put_char(&b, '\0'); | |
281 | ret = xmalloc(buffer_len(&b)); | |
282 | buffer_get(&b, ret, buffer_len(&b)); | |
283 | buffer_free(&b); | |
284 | return (ret); | |
b9a54c29 | 285 | } |
286 | ||
70791e56 | 287 | /* |
288 | * Initialise our GSSAPI context. We use this opaque structure to contain all | |
b9a54c29 | 289 | * of the data which both the client and server need to persist across |
290 | * {accept,init}_sec_context calls, so that when we do it from the userauth | |
291 | * stuff life is a little easier | |
292 | */ | |
293 | void | |
ff2d7a98 | 294 | ssh_gssapi_build_ctx(Gssctxt **ctx) |
b9a54c29 | 295 | { |
70791e56 | 296 | *ctx = xmalloc(sizeof (Gssctxt)); |
297 | (*ctx)->major = 0; | |
298 | (*ctx)->minor = 0; | |
299 | (*ctx)->context = GSS_C_NO_CONTEXT; | |
300 | (*ctx)->name = GSS_C_NO_NAME; | |
301 | (*ctx)->oid = GSS_C_NO_OID; | |
302 | (*ctx)->creds = GSS_C_NO_CREDENTIAL; | |
303 | (*ctx)->client = GSS_C_NO_NAME; | |
304 | (*ctx)->client_creds = GSS_C_NO_CREDENTIAL; | |
b9a54c29 | 305 | } |
306 | ||
307 | /* Delete our context, providing it has been built correctly */ | |
308 | void | |
ff2d7a98 | 309 | ssh_gssapi_delete_ctx(Gssctxt **ctx) |
b9a54c29 | 310 | { |
98f19977 | 311 | #if !defined(MECHGLUE) |
b9a54c29 | 312 | OM_uint32 ms; |
98f19977 | 313 | #endif |
70791e56 | 314 | |
315 | if ((*ctx) == NULL) | |
ff2d7a98 | 316 | return; |
c2397a66 | 317 | #if !defined(MECHGLUE) /* mechglue has some memory management issues */ |
70791e56 | 318 | if ((*ctx)->context != GSS_C_NO_CONTEXT) |
319 | gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER); | |
ff2d7a98 | 320 | if ((*ctx)->name != GSS_C_NO_NAME) |
70791e56 | 321 | gss_release_name(&ms, &(*ctx)->name); |
ff2d7a98 | 322 | if ((*ctx)->oid != GSS_C_NO_OID) { |
323 | xfree((*ctx)->oid->elements); | |
324 | xfree((*ctx)->oid); | |
325 | (*ctx)->oid = GSS_C_NO_OID; | |
b9a54c29 | 326 | } |
ff2d7a98 | 327 | if ((*ctx)->creds != GSS_C_NO_CREDENTIAL) |
70791e56 | 328 | gss_release_cred(&ms, &(*ctx)->creds); |
ff2d7a98 | 329 | if ((*ctx)->client != GSS_C_NO_NAME) |
70791e56 | 330 | gss_release_name(&ms, &(*ctx)->client); |
ff2d7a98 | 331 | if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL) |
70791e56 | 332 | gss_release_cred(&ms, &(*ctx)->client_creds); |
c2397a66 | 333 | #endif |
70791e56 | 334 | |
ff2d7a98 | 335 | xfree(*ctx); |
70791e56 | 336 | *ctx = NULL; |
b9a54c29 | 337 | } |
338 | ||
70791e56 | 339 | /* |
340 | * Wrapper to init_sec_context | |
b9a54c29 | 341 | * Requires that the context contains: |
342 | * oid | |
70791e56 | 343 | * server name (from ssh_gssapi_import_name) |
b9a54c29 | 344 | */ |
70791e56 | 345 | OM_uint32 |
b9a54c29 | 346 | ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok, |
70791e56 | 347 | gss_buffer_desc* send_tok, OM_uint32 *flags) |
b9a54c29 | 348 | { |
b9a54c29 | 349 | int deleg_flag = 0; |
70791e56 | 350 | |
b9a54c29 | 351 | if (deleg_creds) { |
70791e56 | 352 | deleg_flag = GSS_C_DELEG_FLAG; |
b9a54c29 | 353 | debug("Delegating credentials"); |
354 | } | |
70791e56 | 355 | |
356 | ctx->major = gss_init_sec_context(&ctx->minor, | |
357 | GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid, | |
358 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag, | |
359 | 0, NULL, recv_tok, NULL, send_tok, flags, NULL); | |
360 | ||
361 | if (GSS_ERROR(ctx->major)) | |
362 | ssh_gssapi_error(ctx); | |
363 | ||
364 | return (ctx->major); | |
b9a54c29 | 365 | } |
366 | ||
b9a54c29 | 367 | /* Create a service name for the given host */ |
368 | OM_uint32 | |
70791e56 | 369 | ssh_gssapi_import_name(Gssctxt *ctx, const char *host) |
370 | { | |
88928908 | 371 | gss_buffer_desc gssbuf; |
b9a54c29 | 372 | char *xhost; |
70791e56 | 373 | |
b9a54c29 | 374 | /* Make a copy of the host name, in case it was returned by a |
375 | * previous call to gethostbyname(). */ | |
376 | xhost = xstrdup(host); | |
377 | ||
88928908 | 378 | /* Make sure we have the FQDN. Some GSSAPI implementations don't do |
b9a54c29 | 379 | * this for us themselves */ |
366115cd | 380 | resolve_localhost(&xhost); |
88928908 | 381 | |
70791e56 | 382 | gssbuf.length = sizeof("host@") + strlen(xhost); |
383 | gssbuf.value = xmalloc(gssbuf.length); | |
70b0961c | 384 | snprintf(gssbuf.value, gssbuf.length, "host@%s", xhost); |
70791e56 | 385 | |
386 | if ((ctx->major = gss_import_name(&ctx->minor, | |
387 | &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name))) | |
88928908 | 388 | ssh_gssapi_error(ctx); |
70791e56 | 389 | |
b9a54c29 | 390 | xfree(xhost); |
391 | xfree(gssbuf.value); | |
70791e56 | 392 | return (ctx->major); |
b9a54c29 | 393 | } |
394 | ||
395 | /* Acquire credentials for a server running on the current host. | |
396 | * Requires that the context structure contains a valid OID | |
ff2d7a98 | 397 | */ |
70791e56 | 398 | |
ff2d7a98 | 399 | /* Returns a GSSAPI error code */ |
b9a54c29 | 400 | OM_uint32 |
70791e56 | 401 | ssh_gssapi_acquire_cred(Gssctxt *ctx) |
402 | { | |
88928908 | 403 | OM_uint32 status; |
b9a54c29 | 404 | char lname[MAXHOSTNAMELEN]; |
405 | gss_OID_set oidset; | |
b9a54c29 | 406 | |
70791e56 | 407 | gss_create_empty_oid_set(&status, &oidset); |
408 | gss_add_oid_set_member(&status, ctx->oid, &oidset); | |
409 | ||
410 | if (gethostname(lname, MAXHOSTNAMELEN)) | |
411 | return (-1); | |
412 | ||
413 | if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) | |
414 | return (ctx->major); | |
415 | ||
416 | if ((ctx->major = gss_acquire_cred(&ctx->minor, | |
417 | ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL))) | |
88928908 | 418 | ssh_gssapi_error(ctx); |
70791e56 | 419 | |
88928908 | 420 | gss_release_oid_set(&status, &oidset); |
70791e56 | 421 | return (ctx->major); |
422 | } | |
423 | ||
416fd2a8 | 424 | OM_uint32 |
425 | ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash) | |
426 | { | |
427 | if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context, | |
428 | GSS_C_QOP_DEFAULT, buffer, hash))) | |
429 | ssh_gssapi_error(ctx); | |
430 | ||
431 | return (ctx->major); | |
432 | } | |
433 | ||
434 | void | |
435 | ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service, | |
436 | const char *context) | |
437 | { | |
438 | buffer_init(b); | |
439 | buffer_put_string(b, session_id2, session_id2_len); | |
440 | buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST); | |
441 | buffer_put_cstring(b, user); | |
442 | buffer_put_cstring(b, service); | |
443 | buffer_put_cstring(b, context); | |
444 | } | |
445 | ||
70791e56 | 446 | OM_uint32 |
447 | ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid) { | |
448 | if (*ctx) | |
449 | ssh_gssapi_delete_ctx(ctx); | |
450 | ssh_gssapi_build_ctx(ctx); | |
451 | ssh_gssapi_set_oid(*ctx, oid); | |
452 | return (ssh_gssapi_acquire_cred(*ctx)); | |
b9a54c29 | 453 | } |
ff2d7a98 | 454 | |
88928908 | 455 | int |
456 | ssh_gssapi_check_mechanism(gss_OID oid, char *host) { | |
457 | Gssctxt * ctx = NULL; | |
458 | gss_buffer_desc token; | |
459 | OM_uint32 major,minor; | |
460 | ||
461 | ssh_gssapi_build_ctx(&ctx); | |
462 | ssh_gssapi_set_oid(ctx,oid); | |
463 | ssh_gssapi_import_name(ctx,host); | |
464 | major=ssh_gssapi_init_ctx(ctx,0, GSS_C_NO_BUFFER, &token, NULL); | |
ff2d7a98 | 465 | gss_release_buffer(&minor,&token); |
88928908 | 466 | ssh_gssapi_delete_ctx(&ctx); |
467 | return(!GSS_ERROR(major)); | |
ff2d7a98 | 468 | } |
1c14df9e | 469 | |
b9a54c29 | 470 | #endif /* GSSAPI */ |