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