2 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
13 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
16 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <openssl/evp.h>
40 #include "monitor_wrap.h"
50 } ssh_gss_kex_mapping;
52 static ssh_gss_kex_mapping *gss_enc2oid;
54 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
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.
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.
66 ssh_gssapi_client_mechanisms(char *host) {
67 gss_OID_set supported;
74 char digest[EVP_MAX_MD_SIZE];
76 const EVP_MD *evp_md = EVP_md5();
80 if (datafellows & SSH_OLD_GSSAPI) return NULL;
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));
87 gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping)
88 *(supported->count+1));
94 for (i=0;i<supported->count;i++) {
96 gss_enc2oid[oidpos].encoded=NULL;
98 if (supported->elements[i].length<128 &&
99 ssh_gssapi_check_mechanism(&(supported->elements[i]),host)) {
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
107 if (datafellows & SSH_BUG_GSSAPI_BER) {
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"};
112 if (supported->elements[i].length==krb5oid.length &&
113 memcmp(supported->elements[i].elements,
114 krb5oid.elements, krb5oid.length)==0) {
115 bodge="Se3H81ismmOC3OE+FwYCiQ==";
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==";
126 buffer_put_char(&buf,',');
129 buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1);
130 buffer_append(&buf, bodge, strlen(bodge));
132 gss_enc2oid[oidpos].oid=&(supported->elements[i]);
133 gss_enc2oid[oidpos].encoded=bodge;
139 /* Add the required DER encoding octets and MD5 hash */
140 deroid[0]=0x06; /* Object Identifier */
141 deroid[1]=supported->elements[i].length;
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);
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);
155 buffer_put_char(&buf,',');
157 buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1);
158 buffer_append(&buf, encoded, enclen);
160 debug("Mechanism encoded as %s",encoded);
162 gss_enc2oid[oidpos].oid=&(supported->elements[i]);
163 gss_enc2oid[oidpos].encoded=encoded;
167 gss_enc2oid[oidpos].oid=NULL;
168 gss_enc2oid[oidpos].encoded=NULL;
170 buffer_put_char(&buf,'\0');
172 mechs=xmalloc(buffer_len(&buf));
173 buffer_get(&buf,mechs,buffer_len(&buf));
175 if (strlen(mechs)==0)
182 ssh_gssapi_client_id_kex(Gssctxt *ctx, char *name) {
185 if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
189 name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the ID string */
191 while (gss_enc2oid[i].encoded!=NULL &&
192 strcmp(name,gss_enc2oid[i].encoded)!=0) {
196 if (gss_enc2oid[i].oid!=NULL) {
197 ssh_gssapi_set_oid(ctx,gss_enc2oid[i].oid);
200 return gss_enc2oid[i].oid;
203 /* Check that the OID in a data stream matches that in the context */
204 int ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len) {
206 return (ctx!=NULL && ctx->oid != GSS_C_NO_OID &&
207 ctx->oid->length == len &&
208 memcmp(ctx->oid->elements,data,len)==0);
211 /* Set the contexts OID from a data stream */
212 void ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) {
213 if (ctx->oid != GSS_C_NO_OID) {
214 xfree(ctx->oid->elements);
217 ctx->oid=xmalloc(sizeof(gss_OID_desc));
218 ctx->oid->length=len;
219 ctx->oid->elements=xmalloc(len);
220 memcpy(ctx->oid->elements,data,len);
223 /* Set the contexts OID */
224 void ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) {
225 ssh_gssapi_set_oid_data(ctx,oid->elements,oid->length);
228 /* All this effort to report an error ... */
231 ssh_gssapi_error(Gssctxt *ctxt) {
233 debug(ssh_gssapi_last_error(ctxt,NULL,NULL));
237 ssh_gssapi_last_error(Gssctxt *ctxt,
238 OM_uint32 *major_status, OM_uint32 *minor_status) {
247 if (major_status!=NULL) *major_status=ctxt->major;
248 if (minor_status!=NULL) *minor_status=ctxt->minor;
251 /* The GSSAPI error */
253 gss_display_status(&lmin, ctxt->major,
254 GSS_C_GSS_CODE, ctxt->oid,
257 buffer_append(&b,msg.value,msg.length);
258 buffer_put_char(&b,'\n');
260 gss_release_buffer(&lmin, &msg);
263 /* The mechanism specific error */
265 gss_display_status(&lmin, ctxt->minor,
266 GSS_C_MECH_CODE, ctxt->oid,
269 buffer_append(&b,msg.value,msg.length);
270 buffer_put_char(&b,'\n');
272 gss_release_buffer(&lmin, &msg);
275 buffer_put_char(&b,'\0');
276 ret=xmalloc(buffer_len(&b));
277 buffer_get(&b,ret,buffer_len(&b));
282 /* Initialise our GSSAPI context. We use this opaque structure to contain all
283 * of the data which both the client and server need to persist across
284 * {accept,init}_sec_context calls, so that when we do it from the userauth
285 * stuff life is a little easier
288 ssh_gssapi_build_ctx(Gssctxt **ctx)
290 *ctx=xmalloc(sizeof (Gssctxt));
293 (*ctx)->context=GSS_C_NO_CONTEXT;
294 (*ctx)->name=GSS_C_NO_NAME;
295 (*ctx)->oid=GSS_C_NO_OID;
296 (*ctx)->creds=GSS_C_NO_CREDENTIAL;
297 (*ctx)->client=GSS_C_NO_NAME;
298 (*ctx)->client_creds=GSS_C_NO_CREDENTIAL;
301 /* Delete our context, providing it has been built correctly */
303 ssh_gssapi_delete_ctx(Gssctxt **ctx)
305 #if !defined(MECHGLUE)
309 /* Return if there's no context */
313 #if !defined(MECHGLUE) /* mechglue has some memory management issues */
314 if ((*ctx)->context != GSS_C_NO_CONTEXT)
315 gss_delete_sec_context(&ms,&(*ctx)->context,GSS_C_NO_BUFFER);
316 if ((*ctx)->name != GSS_C_NO_NAME)
317 gss_release_name(&ms,&(*ctx)->name);
318 if ((*ctx)->oid != GSS_C_NO_OID) {
319 xfree((*ctx)->oid->elements);
321 (*ctx)->oid = GSS_C_NO_OID;
323 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
324 gss_release_cred(&ms,&(*ctx)->creds);
325 if ((*ctx)->client != GSS_C_NO_NAME)
326 gss_release_name(&ms,&(*ctx)->client);
327 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
328 gss_release_cred(&ms,&(*ctx)->client_creds);
335 /* Wrapper to init_sec_context
336 * Requires that the context contains:
338 * server name (from ssh_gssapi_import_name)
341 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
342 gss_buffer_desc* send_tok, OM_uint32 *flags)
347 deleg_flag=GSS_C_DELEG_FLAG;
348 debug("Delegating credentials");
351 ctx->major=gss_init_sec_context(&ctx->minor,
352 GSS_C_NO_CREDENTIAL, /* def. cred */
359 0, /* default lifetime */
360 NULL, /* no channel bindings */
366 if (GSS_ERROR(ctx->major)) {
367 ssh_gssapi_error(ctx);
372 /* Create a service name for the given host */
374 ssh_gssapi_import_name(Gssctxt *ctx, const char *host) {
375 gss_buffer_desc gssbuf;
378 /* Make a copy of the host name, in case it was returned by a
379 * previous call to gethostbyname(). */
380 xhost = xstrdup(host);
382 /* Make sure we have the FQDN. Some GSSAPI implementations don't do
383 * this for us themselves */
384 resolve_localhost(&xhost);
386 gssbuf.length = sizeof("host@")+strlen(xhost);
388 gssbuf.value = xmalloc(gssbuf.length);
389 if (gssbuf.value == NULL) {
393 snprintf(gssbuf.value,gssbuf.length,"host@%s",xhost);
394 if ((ctx->major=gss_import_name(&ctx->minor,
396 GSS_C_NT_HOSTBASED_SERVICE,
398 ssh_gssapi_error(ctx);
406 /* Acquire credentials for a server running on the current host.
407 * Requires that the context structure contains a valid OID
410 /* Returns a GSSAPI error code */
412 ssh_gssapi_acquire_cred(Gssctxt *ctx) {
414 char lname[MAXHOSTNAMELEN];
417 gss_create_empty_oid_set(&status,&oidset);
418 gss_add_oid_set_member(&status,ctx->oid,&oidset);
420 if (gethostname(lname, MAXHOSTNAMELEN)) {
424 if (GSS_ERROR(ssh_gssapi_import_name(ctx,lname))) {
428 if ((ctx->major=gss_acquire_cred(&ctx->minor,
436 ssh_gssapi_error(ctx);
439 gss_release_oid_set(&status, &oidset);
444 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
446 /* ssh1 needs to exchange the hash of the keys */
447 /* will us this hash to return it */
449 if ((ctx->major=gss_wrap(&ctx->minor,ctx->context,
455 ssh_gssapi_error(ctx);
459 if ((ctx->major=gss_get_mic(&ctx->minor,ctx->context,
460 GSS_C_QOP_DEFAULT, buffer, hash))) {
461 ssh_gssapi_error(ctx);
468 ssh_gssapi_server_ctx(Gssctxt **ctx,gss_OID oid) {
469 if (*ctx) ssh_gssapi_delete_ctx(ctx);
470 ssh_gssapi_build_ctx(ctx);
471 ssh_gssapi_set_oid(*ctx,oid);
472 return(ssh_gssapi_acquire_cred(*ctx));
476 ssh_gssapi_check_mechanism(gss_OID oid, char *host) {
477 Gssctxt * ctx = NULL;
478 gss_buffer_desc token;
479 OM_uint32 major,minor;
481 ssh_gssapi_build_ctx(&ctx);
482 ssh_gssapi_set_oid(ctx,oid);
483 ssh_gssapi_import_name(ctx,host);
484 major=ssh_gssapi_init_ctx(ctx,0, GSS_C_NO_BUFFER, &token, NULL);
485 gss_release_buffer(&minor,&token);
486 ssh_gssapi_delete_ctx(&ctx);
487 return(!GSS_ERROR(major));