17c478bd9Sstevel@tonic-gate /*
2*5e01956fSGlenn Barry  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate /*
57c478bd9Sstevel@tonic-gate  * lib/gssapi/krb5/import_sec_context.c
67c478bd9Sstevel@tonic-gate  *
7ab9b2e15Sgtb  * Copyright 1995,2004 by the Massachusetts Institute of Technology.
87c478bd9Sstevel@tonic-gate  * All Rights Reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
117c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
127c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
137c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
167c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
177c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
187c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
197c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
207c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
217c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
227c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
237c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
247c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
257c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
267c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
277c478bd9Sstevel@tonic-gate  * or implied warranty.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * import_sec_context.c	- Internalize the security context.
337c478bd9Sstevel@tonic-gate  */
34ab9b2e15Sgtb #include "gssapiP_krb5.h"
35ab9b2e15Sgtb /* for serialization initialization functions */
36ab9b2e15Sgtb #include "k5-int.h"
37ab9b2e15Sgtb #include "mglueP.h"  /* SUNW15resync - for KGSS_ macros */
38ab9b2e15Sgtb 
39ab9b2e15Sgtb #ifdef	 _KERNEL
40ab9b2e15Sgtb extern OM_uint32 kgss_release_oid(OM_uint32 *, gss_OID *);
41ab9b2e15Sgtb #endif
42ab9b2e15Sgtb 
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Fix up the OID of the mechanism so that uses the static version of
467c478bd9Sstevel@tonic-gate  * the OID if possible.
477c478bd9Sstevel@tonic-gate  */
krb5_gss_convert_static_mech_oid(oid)48ab9b2e15Sgtb gss_OID krb5_gss_convert_static_mech_oid(oid)
49ab9b2e15Sgtb      gss_OID	oid;
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	const gss_OID_desc 	*p;
527c478bd9Sstevel@tonic-gate 	OM_uint32		minor_status;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	for (p = krb5_gss_oid_array; p->length; p++) {
557c478bd9Sstevel@tonic-gate 		if ((oid->length == p->length) &&
567c478bd9Sstevel@tonic-gate 		    (memcmp(oid->elements, p->elements, p->length) == 0)) {
57ab9b2e15Sgtb 		        (void) KGSS_RELEASE_OID(&minor_status, &oid);
58ab9b2e15Sgtb 			return (gss_OID) p;
597c478bd9Sstevel@tonic-gate 		}
607c478bd9Sstevel@tonic-gate 	}
61ab9b2e15Sgtb 	return oid;
627c478bd9Sstevel@tonic-gate }
63ab9b2e15Sgtb 
64ab9b2e15Sgtb krb5_error_code
krb5_gss_ser_init(krb5_context context)65ab9b2e15Sgtb krb5_gss_ser_init (krb5_context context)
66ab9b2e15Sgtb {
67ab9b2e15Sgtb     krb5_error_code code;
68ab9b2e15Sgtb     static krb5_error_code (KRB5_CALLCONV *const fns[])(krb5_context) = {
69ab9b2e15Sgtb 	krb5_ser_auth_context_init,
70ab9b2e15Sgtb #ifndef _KERNEL
71ab9b2e15Sgtb 	krb5_ser_context_init,
72ab9b2e15Sgtb 	krb5_ser_ccache_init, krb5_ser_rcache_init, krb5_ser_keytab_init,
737c478bd9Sstevel@tonic-gate #endif
74ab9b2e15Sgtb     };
75ab9b2e15Sgtb     int i;
76ab9b2e15Sgtb 
77ab9b2e15Sgtb     for (i = 0; i < sizeof(fns)/sizeof(fns[0]); i++)
78ab9b2e15Sgtb 	if ((code = (fns[i])(context)) != 0)
79ab9b2e15Sgtb 	    return code;
80ab9b2e15Sgtb     return 0;
81ab9b2e15Sgtb }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate OM_uint32
krb5_gss_import_sec_context(minor_status,interprocess_token,context_handle)84ab9b2e15Sgtb krb5_gss_import_sec_context(minor_status, interprocess_token, context_handle)
857c478bd9Sstevel@tonic-gate     OM_uint32		*minor_status;
867c478bd9Sstevel@tonic-gate     gss_buffer_t	interprocess_token;
877c478bd9Sstevel@tonic-gate     gss_ctx_id_t	*context_handle;
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate     krb5_context	context;
907c478bd9Sstevel@tonic-gate     krb5_error_code	kret = 0;
917c478bd9Sstevel@tonic-gate     size_t		blen;
927c478bd9Sstevel@tonic-gate     krb5_gss_ctx_id_t	ctx;
937c478bd9Sstevel@tonic-gate     krb5_octet		*ibp;
947c478bd9Sstevel@tonic-gate 
95ab9b2e15Sgtb     /* This is a bit screwy.  We create a krb5 context because we need
96ab9b2e15Sgtb        one when calling the serialization code.  However, one of the
97ab9b2e15Sgtb        objects we're unpacking is a krb5 context, so when we finish,
98ab9b2e15Sgtb        we can throw this one away.  */
99ab9b2e15Sgtb     kret = KGSS_INIT_CONTEXT(&context);
100ab9b2e15Sgtb     if (kret) {
101ab9b2e15Sgtb 	*minor_status = kret;
102ab9b2e15Sgtb 	return GSS_S_FAILURE;
103ab9b2e15Sgtb     }
1047c478bd9Sstevel@tonic-gate 
105ab9b2e15Sgtb     kret = krb5_gss_ser_init(context);
106ab9b2e15Sgtb     if (kret) {
107ab9b2e15Sgtb 	*minor_status = kret;
108*5e01956fSGlenn Barry 	save_error_info(*minor_status, context);
109*5e01956fSGlenn Barry 	krb5_free_context(context);
110ab9b2e15Sgtb 	return GSS_S_FAILURE;
111ab9b2e15Sgtb     }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate     /* Assume a tragic failure */
1147c478bd9Sstevel@tonic-gate     ctx = (krb5_gss_ctx_id_t) NULL;
1157c478bd9Sstevel@tonic-gate     *minor_status = 0;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate     /* Internalize the context */
1187c478bd9Sstevel@tonic-gate     ibp = (krb5_octet *) interprocess_token->value;
1197c478bd9Sstevel@tonic-gate     blen = (size_t) interprocess_token->length;
120ab9b2e15Sgtb     kret = kg_ctx_internalize(context, (krb5_pointer *) &ctx, &ibp, &blen);
121ab9b2e15Sgtb     /*
122ab9b2e15Sgtb      * SUNW15resync
123ab9b2e15Sgtb      *
124ab9b2e15Sgtb      *    krb5_free_context(context);
125ab9b2e15Sgtb      * Previous versions of MIT(1.2ish)/Solaris did not serialize the
126ab9b2e15Sgtb      * k5_context but MIT 1.5 does.  But we don't need all the userspace
127ab9b2e15Sgtb      * junk in the kernel so we continue to not serialize it.
128ab9b2e15Sgtb      * So we keep this context live here (see it's use in kg_ctx_internalize)
129ab9b2e15Sgtb      * and it will get freed by delete_sec_context.
130ab9b2e15Sgtb      */
131ab9b2e15Sgtb     if (kret) {
1327c478bd9Sstevel@tonic-gate        *minor_status = (OM_uint32) kret;
133*5e01956fSGlenn Barry        save_error_info(*minor_status, context);
134*5e01956fSGlenn Barry        krb5_free_context(context);
135ab9b2e15Sgtb        return(GSS_S_FAILURE);
1367c478bd9Sstevel@tonic-gate     }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate     /* intern the context handle */
1397c478bd9Sstevel@tonic-gate     if (! kg_save_ctx_id((gss_ctx_id_t) ctx)) {
140ab9b2e15Sgtb        (void)krb5_gss_delete_sec_context(minor_status,
1417c478bd9Sstevel@tonic-gate 					 (gss_ctx_id_t *) &ctx, NULL
1427c478bd9Sstevel@tonic-gate #ifdef _KERNEL
143ab9b2e15Sgtb  					,0  /* gssd_ctx_verifier */
1447c478bd9Sstevel@tonic-gate #endif
1457c478bd9Sstevel@tonic-gate 					);
1467c478bd9Sstevel@tonic-gate        *minor_status = (OM_uint32) G_VALIDATE_FAILED;
1477c478bd9Sstevel@tonic-gate        return(GSS_S_FAILURE);
1487c478bd9Sstevel@tonic-gate     }
149ab9b2e15Sgtb     ctx->mech_used = krb5_gss_convert_static_mech_oid(ctx->mech_used);
150ab9b2e15Sgtb 
1517c478bd9Sstevel@tonic-gate     *context_handle = (gss_ctx_id_t) ctx;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate     *minor_status = 0;
1547c478bd9Sstevel@tonic-gate     return (GSS_S_COMPLETE);
1557c478bd9Sstevel@tonic-gate }
156