]>
Commit | Line | Data |
---|---|---|
2e437378 | 1 | /* $OpenBSD: gss-genr.c,v 1.17 2006/08/29 12:02:30 dtucker Exp $ */ |
70791e56 | 2 | |
b9a54c29 | 3 | /* |
2e437378 | 4 | * Copyright (c) 2001-2006 Simon Wilkinson. All rights reserved. |
70791e56 | 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 | ||
2e437378 | 31 | #include <sys/types.h> |
32 | #include <sys/param.h> | |
33 | ||
34 | #include <stdarg.h> | |
35 | #include <string.h> | |
36 | #include <unistd.h> | |
37 | ||
b9a54c29 | 38 | #include "xmalloc.h" |
2e437378 | 39 | #include "buffer.h" |
b9a54c29 | 40 | #include "log.h" |
c2397a66 | 41 | #include "canohost.h" |
416fd2a8 | 42 | #include "ssh2.h" |
2e437378 | 43 | #include "cipher.h" |
44 | #include "key.h" | |
45 | #include "kex.h" | |
34fee935 | 46 | #include <openssl/evp.h> |
b9a54c29 | 47 | |
48 | #include "ssh-gss.h" | |
49 | ||
416fd2a8 | 50 | extern u_char *session_id2; |
51 | extern u_int session_id2_len; | |
52 | ||
88928908 | 53 | typedef struct { |
54 | char *encoded; | |
55 | gss_OID oid; | |
56 | } ssh_gss_kex_mapping; | |
b9a54c29 | 57 | |
34fee935 | 58 | /* |
59 | * XXX - It would be nice to find a more elegant way of handling the | |
60 | * XXX passing of the key exchange context to the userauth routines | |
61 | */ | |
62 | ||
63 | Gssctxt *gss_kex_context = NULL; | |
64 | ||
65 | static ssh_gss_kex_mapping *gss_enc2oid = NULL; | |
66 | ||
67 | int | |
68 | ssh_gssapi_oid_table_ok() { | |
69 | return (gss_enc2oid != NULL); | |
70 | } | |
71 | ||
72 | /* | |
73 | * Return a list of the gss-group1-sha1 mechanisms supported by this program | |
b9a54c29 | 74 | * |
34fee935 | 75 | * We test mechanisms to ensure that we can use them, to avoid starting |
76 | * a key exchange with a bad mechanism | |
b9a54c29 | 77 | */ |
34fee935 | 78 | |
34fee935 | 79 | char * |
80 | ssh_gssapi_client_mechanisms(const char *host) { | |
81 | gss_OID_set gss_supported; | |
82 | OM_uint32 min_status; | |
83 | ||
84 | gss_indicate_mechs(&min_status, &gss_supported); | |
85 | ||
86 | return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism, | |
2e437378 | 87 | host)); |
34fee935 | 88 | } |
89 | ||
90 | char * | |
91 | ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check, | |
2e437378 | 92 | const char *data) { |
34fee935 | 93 | Buffer buf; |
94 | size_t i; | |
95 | int oidpos, enclen; | |
96 | char *mechs, *encoded; | |
e00da40d | 97 | u_char digest[EVP_MAX_MD_SIZE]; |
34fee935 | 98 | char deroid[2]; |
99 | const EVP_MD *evp_md = EVP_md5(); | |
100 | EVP_MD_CTX md; | |
101 | ||
102 | if (gss_enc2oid != NULL) { | |
2e437378 | 103 | for (i = 0; gss_enc2oid[i].encoded != NULL; i++) |
34fee935 | 104 | xfree(gss_enc2oid[i].encoded); |
105 | xfree(gss_enc2oid); | |
88928908 | 106 | } |
34fee935 | 107 | |
2e437378 | 108 | gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) * |
109 | (gss_supported->count + 1)); | |
34fee935 | 110 | |
88928908 | 111 | buffer_init(&buf); |
112 | ||
34fee935 | 113 | oidpos = 0; |
2e437378 | 114 | for (i = 0; i < gss_supported->count; i++) { |
34fee935 | 115 | if (gss_supported->elements[i].length < 128 && |
2e437378 | 116 | (*check)(NULL, &(gss_supported->elements[i]), data)) { |
88928908 | 117 | |
34fee935 | 118 | deroid[0] = SSH_GSS_OIDTYPE; |
119 | deroid[1] = gss_supported->elements[i].length; | |
88928908 | 120 | |
121 | EVP_DigestInit(&md, evp_md); | |
34fee935 | 122 | EVP_DigestUpdate(&md, deroid, 2); |
88928908 | 123 | EVP_DigestUpdate(&md, |
34fee935 | 124 | gss_supported->elements[i].elements, |
125 | gss_supported->elements[i].length); | |
88928908 | 126 | EVP_DigestFinal(&md, digest, NULL); |
88928908 | 127 | |
2e437378 | 128 | encoded = xmalloc(EVP_MD_size(evp_md) * 2); |
34fee935 | 129 | enclen = __b64_ntop(digest, EVP_MD_size(evp_md), |
2e437378 | 130 | encoded, EVP_MD_size(evp_md) * 2); |
34fee935 | 131 | |
132 | if (oidpos != 0) | |
2e437378 | 133 | buffer_put_char(&buf, ','); |
88928908 | 134 | |
34fee935 | 135 | buffer_append(&buf, KEX_GSS_GEX_SHA1_ID, |
2e437378 | 136 | sizeof(KEX_GSS_GEX_SHA1_ID) - 1); |
34fee935 | 137 | buffer_append(&buf, encoded, enclen); |
2e437378 | 138 | buffer_put_char(&buf, ','); |
34fee935 | 139 | buffer_append(&buf, KEX_GSS_GRP1_SHA1_ID, |
2e437378 | 140 | sizeof(KEX_GSS_GRP1_SHA1_ID) - 1); |
141 | buffer_append(&buf, encoded, enclen); | |
142 | buffer_put_char(&buf, ','); | |
143 | buffer_append(&buf, KEX_GSS_GRP14_SHA1_ID, | |
144 | sizeof(KEX_GSS_GRP14_SHA1_ID) - 1); | |
34fee935 | 145 | buffer_append(&buf, encoded, enclen); |
146 | ||
147 | gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]); | |
148 | gss_enc2oid[oidpos].encoded = encoded; | |
88928908 | 149 | oidpos++; |
c2397a66 | 150 | } |
88928908 | 151 | } |
34fee935 | 152 | gss_enc2oid[oidpos].oid = NULL; |
153 | gss_enc2oid[oidpos].encoded = NULL; | |
154 | ||
155 | buffer_put_char(&buf, '\0'); | |
156 | ||
157 | mechs = xmalloc(buffer_len(&buf)); | |
158 | buffer_get(&buf, mechs, buffer_len(&buf)); | |
b9a54c29 | 159 | buffer_free(&buf); |
88928908 | 160 | |
34fee935 | 161 | if (strlen(mechs) == 0) { |
162 | xfree(mechs); | |
163 | mechs = NULL; | |
88928908 | 164 | } |
165 | ||
34fee935 | 166 | return (mechs); |
167 | } | |
168 | ||
169 | gss_OID | |
2e437378 | 170 | ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) { |
34fee935 | 171 | int i = 0; |
2e437378 | 172 | |
173 | switch (kex_type) { | |
174 | case KEX_GSS_GRP1_SHA1: | |
175 | name += sizeof(KEX_GSS_GRP1_SHA1_ID) - 1; | |
176 | break; | |
177 | case KEX_GSS_GRP14_SHA1: | |
178 | name += sizeof(KEX_GSS_GRP14_SHA1_ID) - 1; | |
179 | break; | |
180 | case KEX_GSS_GEX_SHA1: | |
181 | name += sizeof(KEX_GSS_GEX_SHA1_ID) - 1; | |
182 | break; | |
183 | default: | |
184 | return GSS_C_NO_OID; | |
88928908 | 185 | } |
34fee935 | 186 | |
187 | while (gss_enc2oid[i].encoded != NULL && | |
2e437378 | 188 | strcmp(name, gss_enc2oid[i].encoded) != 0) |
34fee935 | 189 | i++; |
88928908 | 190 | |
34fee935 | 191 | if (gss_enc2oid[i].oid != NULL && ctx != NULL) |
192 | ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid); | |
193 | ||
88928908 | 194 | return gss_enc2oid[i].oid; |
b9a54c29 | 195 | } |
196 | ||
88928908 | 197 | /* Check that the OID in a data stream matches that in the context */ |
70791e56 | 198 | int |
199 | ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len) | |
200 | { | |
201 | return (ctx != NULL && ctx->oid != GSS_C_NO_OID && | |
202 | ctx->oid->length == len && | |
203 | memcmp(ctx->oid->elements, data, len) == 0); | |
88928908 | 204 | } |
70791e56 | 205 | |
b9a54c29 | 206 | /* Set the contexts OID from a data stream */ |
70791e56 | 207 | void |
208 | ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) | |
209 | { | |
210 | if (ctx->oid != GSS_C_NO_OID) { | |
211 | xfree(ctx->oid->elements); | |
212 | xfree(ctx->oid); | |
213 | } | |
214 | ctx->oid = xmalloc(sizeof(gss_OID_desc)); | |
215 | ctx->oid->length = len; | |
216 | ctx->oid->elements = xmalloc(len); | |
217 | memcpy(ctx->oid->elements, data, len); | |
b9a54c29 | 218 | } |
219 | ||
220 | /* Set the contexts OID */ | |
70791e56 | 221 | void |
222 | ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) | |
223 | { | |
224 | ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length); | |
b9a54c29 | 225 | } |
226 | ||
88928908 | 227 | /* All this effort to report an error ... */ |
88928908 | 228 | void |
70791e56 | 229 | ssh_gssapi_error(Gssctxt *ctxt) |
230 | { | |
2e437378 | 231 | char *s; |
232 | ||
233 | s = ssh_gssapi_last_error(ctxt, NULL, NULL); | |
234 | debug("%s", s); | |
235 | xfree(s); | |
b9a54c29 | 236 | } |
237 | ||
88928908 | 238 | char * |
34fee935 | 239 | ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status, |
240 | OM_uint32 *minor_status) | |
70791e56 | 241 | { |
88928908 | 242 | OM_uint32 lmin; |
70791e56 | 243 | gss_buffer_desc msg = GSS_C_EMPTY_BUFFER; |
244 | OM_uint32 ctx; | |
245 | Buffer b; | |
246 | char *ret; | |
247 | ||
248 | buffer_init(&b); | |
249 | ||
250 | if (major_status != NULL) | |
251 | *major_status = ctxt->major; | |
252 | if (minor_status != NULL) | |
253 | *minor_status = ctxt->minor; | |
254 | ||
255 | ctx = 0; | |
b9a54c29 | 256 | /* The GSSAPI error */ |
70791e56 | 257 | do { |
258 | gss_display_status(&lmin, ctxt->major, | |
259 | GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg); | |
260 | ||
261 | buffer_append(&b, msg.value, msg.length); | |
262 | buffer_put_char(&b, '\n'); | |
263 | ||
264 | gss_release_buffer(&lmin, &msg); | |
265 | } while (ctx != 0); | |
266 | ||
267 | /* The mechanism specific error */ | |
268 | do { | |
269 | gss_display_status(&lmin, ctxt->minor, | |
270 | GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg); | |
271 | ||
272 | buffer_append(&b, msg.value, msg.length); | |
273 | buffer_put_char(&b, '\n'); | |
274 | ||
275 | gss_release_buffer(&lmin, &msg); | |
276 | } while (ctx != 0); | |
277 | ||
278 | buffer_put_char(&b, '\0'); | |
279 | ret = xmalloc(buffer_len(&b)); | |
280 | buffer_get(&b, ret, buffer_len(&b)); | |
281 | buffer_free(&b); | |
282 | return (ret); | |
b9a54c29 | 283 | } |
284 | ||
70791e56 | 285 | /* |
286 | * Initialise our GSSAPI context. We use this opaque structure to contain all | |
b9a54c29 | 287 | * of the data which both the client and server need to persist across |
288 | * {accept,init}_sec_context calls, so that when we do it from the userauth | |
289 | * stuff life is a little easier | |
290 | */ | |
291 | void | |
ff2d7a98 | 292 | ssh_gssapi_build_ctx(Gssctxt **ctx) |
b9a54c29 | 293 | { |
2e437378 | 294 | *ctx = xcalloc(1, sizeof (Gssctxt)); |
70791e56 | 295 | (*ctx)->context = GSS_C_NO_CONTEXT; |
296 | (*ctx)->name = GSS_C_NO_NAME; | |
297 | (*ctx)->oid = GSS_C_NO_OID; | |
298 | (*ctx)->creds = GSS_C_NO_CREDENTIAL; | |
299 | (*ctx)->client = GSS_C_NO_NAME; | |
300 | (*ctx)->client_creds = GSS_C_NO_CREDENTIAL; | |
b9a54c29 | 301 | } |
302 | ||
303 | /* Delete our context, providing it has been built correctly */ | |
304 | void | |
ff2d7a98 | 305 | ssh_gssapi_delete_ctx(Gssctxt **ctx) |
b9a54c29 | 306 | { |
98f19977 | 307 | #if !defined(MECHGLUE) |
b9a54c29 | 308 | OM_uint32 ms; |
98f19977 | 309 | #endif |
70791e56 | 310 | |
311 | if ((*ctx) == NULL) | |
ff2d7a98 | 312 | return; |
c2397a66 | 313 | #if !defined(MECHGLUE) /* mechglue has some memory management issues */ |
70791e56 | 314 | if ((*ctx)->context != GSS_C_NO_CONTEXT) |
315 | gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER); | |
ff2d7a98 | 316 | if ((*ctx)->name != GSS_C_NO_NAME) |
70791e56 | 317 | gss_release_name(&ms, &(*ctx)->name); |
ff2d7a98 | 318 | if ((*ctx)->oid != GSS_C_NO_OID) { |
319 | xfree((*ctx)->oid->elements); | |
320 | xfree((*ctx)->oid); | |
321 | (*ctx)->oid = GSS_C_NO_OID; | |
b9a54c29 | 322 | } |
ff2d7a98 | 323 | if ((*ctx)->creds != GSS_C_NO_CREDENTIAL) |
70791e56 | 324 | gss_release_cred(&ms, &(*ctx)->creds); |
ff2d7a98 | 325 | if ((*ctx)->client != GSS_C_NO_NAME) |
70791e56 | 326 | gss_release_name(&ms, &(*ctx)->client); |
ff2d7a98 | 327 | if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL) |
70791e56 | 328 | gss_release_cred(&ms, &(*ctx)->client_creds); |
c2397a66 | 329 | #endif |
70791e56 | 330 | |
ff2d7a98 | 331 | xfree(*ctx); |
70791e56 | 332 | *ctx = NULL; |
b9a54c29 | 333 | } |
334 | ||
70791e56 | 335 | /* |
336 | * Wrapper to init_sec_context | |
b9a54c29 | 337 | * Requires that the context contains: |
338 | * oid | |
70791e56 | 339 | * server name (from ssh_gssapi_import_name) |
b9a54c29 | 340 | */ |
70791e56 | 341 | OM_uint32 |
b9a54c29 | 342 | ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok, |
70791e56 | 343 | gss_buffer_desc* send_tok, OM_uint32 *flags) |
b9a54c29 | 344 | { |
b9a54c29 | 345 | int deleg_flag = 0; |
70791e56 | 346 | |
b9a54c29 | 347 | if (deleg_creds) { |
70791e56 | 348 | deleg_flag = GSS_C_DELEG_FLAG; |
b9a54c29 | 349 | debug("Delegating credentials"); |
350 | } | |
70791e56 | 351 | |
352 | ctx->major = gss_init_sec_context(&ctx->minor, | |
353 | GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid, | |
354 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag, | |
355 | 0, NULL, recv_tok, NULL, send_tok, flags, NULL); | |
356 | ||
357 | if (GSS_ERROR(ctx->major)) | |
358 | ssh_gssapi_error(ctx); | |
359 | ||
360 | return (ctx->major); | |
b9a54c29 | 361 | } |
362 | ||
b9a54c29 | 363 | /* Create a service name for the given host */ |
364 | OM_uint32 | |
70791e56 | 365 | ssh_gssapi_import_name(Gssctxt *ctx, const char *host) |
366 | { | |
88928908 | 367 | gss_buffer_desc gssbuf; |
b9a54c29 | 368 | char *xhost; |
2e437378 | 369 | char *val; |
70791e56 | 370 | |
b9a54c29 | 371 | /* Make a copy of the host name, in case it was returned by a |
372 | * previous call to gethostbyname(). */ | |
373 | xhost = xstrdup(host); | |
374 | ||
88928908 | 375 | /* Make sure we have the FQDN. Some GSSAPI implementations don't do |
b9a54c29 | 376 | * this for us themselves */ |
366115cd | 377 | resolve_localhost(&xhost); |
88928908 | 378 | |
2e437378 | 379 | xasprintf(&val, "host@%s", xhost); |
380 | gssbuf.value = val; | |
381 | gssbuf.length = strlen(gssbuf.value); | |
70791e56 | 382 | |
383 | if ((ctx->major = gss_import_name(&ctx->minor, | |
384 | &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name))) | |
88928908 | 385 | ssh_gssapi_error(ctx); |
70791e56 | 386 | |
b9a54c29 | 387 | xfree(xhost); |
388 | xfree(gssbuf.value); | |
70791e56 | 389 | return (ctx->major); |
b9a54c29 | 390 | } |
391 | ||
416fd2a8 | 392 | OM_uint32 |
393 | ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash) | |
394 | { | |
34fee935 | 395 | if (ctx == NULL) |
396 | return -1; | |
397 | ||
416fd2a8 | 398 | if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context, |
399 | GSS_C_QOP_DEFAULT, buffer, hash))) | |
400 | ssh_gssapi_error(ctx); | |
401 | ||
402 | return (ctx->major); | |
403 | } | |
404 | ||
34fee935 | 405 | /* Priviledged when used by server */ |
406 | OM_uint32 | |
407 | ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) | |
408 | { | |
409 | if (ctx == NULL) | |
410 | return -1; | |
411 | ||
412 | ctx->major = gss_verify_mic(&ctx->minor, ctx->context, | |
413 | gssbuf, gssmic, NULL); | |
414 | ||
415 | return (ctx->major); | |
416 | } | |
417 | ||
416fd2a8 | 418 | void |
419 | ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service, | |
420 | const char *context) | |
421 | { | |
422 | buffer_init(b); | |
423 | buffer_put_string(b, session_id2, session_id2_len); | |
424 | buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST); | |
425 | buffer_put_cstring(b, user); | |
426 | buffer_put_cstring(b, service); | |
427 | buffer_put_cstring(b, context); | |
428 | } | |
429 | ||
2e437378 | 430 | int |
431 | ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host) | |
e00da40d | 432 | { |
2e437378 | 433 | gss_buffer_desc token = GSS_C_EMPTY_BUFFER; |
434 | OM_uint32 major, minor; | |
435 | gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"}; | |
436 | Gssctxt *intctx = NULL; | |
437 | ||
438 | if (ctx == NULL) | |
439 | ctx = &intctx; | |
440 | ||
441 | /* RFC 4462 says we MUST NOT do SPNEGO */ | |
442 | if (oid->length == spnego_oid.length && | |
443 | (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0)) | |
444 | return 0; /* false */ | |
445 | ||
70791e56 | 446 | ssh_gssapi_build_ctx(ctx); |
447 | ssh_gssapi_set_oid(*ctx, oid); | |
2e437378 | 448 | major = ssh_gssapi_import_name(*ctx, host); |
449 | if (!GSS_ERROR(major)) { | |
450 | major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, | |
451 | NULL); | |
452 | gss_release_buffer(&minor, &token); | |
453 | if ((*ctx)->context != GSS_C_NO_CONTEXT) | |
454 | gss_delete_sec_context(&minor, &(*ctx)->context, | |
455 | GSS_C_NO_BUFFER); | |
456 | } | |
457 | ||
458 | if (GSS_ERROR(major) || intctx != NULL) | |
459 | ssh_gssapi_delete_ctx(ctx); | |
ff2d7a98 | 460 | |
34fee935 | 461 | return (!GSS_ERROR(major)); |
ff2d7a98 | 462 | } |
1c14df9e | 463 | |
b9a54c29 | 464 | #endif /* GSSAPI */ |