15e01956fSGlenn Barry /*
25e01956fSGlenn Barry  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
35e01956fSGlenn Barry  */
4ab9b2e15Sgtb /*
5ab9b2e15Sgtb  * lib/gssapi/krb5/export_name.c
6ab9b2e15Sgtb  *
7ab9b2e15Sgtb  * Copyright 1997 by the Massachusetts Institute of Technology.
8ab9b2e15Sgtb  * All Rights Reserved.
9ab9b2e15Sgtb  *
10ab9b2e15Sgtb  * Export of this software from the United States of America may
11ab9b2e15Sgtb  *   require a specific license from the United States Government.
12ab9b2e15Sgtb  *   It is the responsibility of any person or organization contemplating
13ab9b2e15Sgtb  *   export to obtain such a license before exporting.
14ab9b2e15Sgtb  *
15ab9b2e15Sgtb  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
16ab9b2e15Sgtb  * distribute this software and its documentation for any purpose and
17ab9b2e15Sgtb  * without fee is hereby granted, provided that the above copyright
18ab9b2e15Sgtb  * notice appear in all copies and that both that copyright notice and
19ab9b2e15Sgtb  * this permission notice appear in supporting documentation, and that
20ab9b2e15Sgtb  * the name of M.I.T. not be used in advertising or publicity pertaining
21ab9b2e15Sgtb  * to distribution of the software without specific, written prior
22ab9b2e15Sgtb  * permission.  Furthermore if you modify this software you must label
23ab9b2e15Sgtb  * your software as modified software and not distribute it in such a
24ab9b2e15Sgtb  * fashion that it might be confused with the original M.I.T. software.
25ab9b2e15Sgtb  * M.I.T. makes no representations about the suitability of
26ab9b2e15Sgtb  * this software for any purpose.  It is provided "as is" without express
27ab9b2e15Sgtb  * or implied warranty.
28ab9b2e15Sgtb  *
29ab9b2e15Sgtb  */
30ab9b2e15Sgtb 
31ab9b2e15Sgtb #include "gssapiP_krb5.h"
32ab9b2e15Sgtb 
krb5_gss_export_name(OM_uint32 * minor_status,const gss_name_t input_name,gss_buffer_t exported_name)33ab9b2e15Sgtb OM_uint32 krb5_gss_export_name(OM_uint32  *minor_status,
34ab9b2e15Sgtb 			       const gss_name_t input_name,
35ab9b2e15Sgtb 			       gss_buffer_t exported_name)
36ab9b2e15Sgtb {
37ab9b2e15Sgtb 	krb5_context context;
38ab9b2e15Sgtb 	krb5_error_code code;
39ab9b2e15Sgtb 	size_t length;
40ab9b2e15Sgtb 	char *str, *cp;
41ab9b2e15Sgtb 
42ab9b2e15Sgtb 	if (minor_status)
43ab9b2e15Sgtb 		*minor_status = 0;
44ab9b2e15Sgtb 
45ab9b2e15Sgtb 	code = krb5_gss_init_context(&context);
46ab9b2e15Sgtb 	if (code) {
47ab9b2e15Sgtb 	    if (minor_status)
48ab9b2e15Sgtb 		*minor_status = code;
49ab9b2e15Sgtb 	    return GSS_S_FAILURE;
50ab9b2e15Sgtb 	}
51ab9b2e15Sgtb 
52ab9b2e15Sgtb 	exported_name->length = 0;
53ab9b2e15Sgtb 	exported_name->value = NULL;
54*55fea89dSDan Cross 
55ab9b2e15Sgtb 	if (! kg_validate_name(input_name)) {
565e01956fSGlenn Barry 	    /* Solaris Kerberos: spruce-up the err msg */
575e01956fSGlenn Barry 	    krb5_principal princ = (krb5_principal) input_name;
585e01956fSGlenn Barry 	    char *s_name = NULL;
595e01956fSGlenn Barry 	    int kret = krb5_unparse_name(context, princ, &s_name);
605e01956fSGlenn Barry 	    if (minor_status)
615e01956fSGlenn Barry 	        *minor_status = (OM_uint32) G_VALIDATE_FAILED;
625e01956fSGlenn Barry 	    if (minor_status && kret == 0) {
635e01956fSGlenn Barry 	        krb5_set_error_message(context, *minor_status,
645e01956fSGlenn Barry   "Input name principal '%s' is invalid (kg_validate_name()) for export_name",
655e01956fSGlenn Barry 				    s_name);
665e01956fSGlenn Barry 		save_error_info(*minor_status, context);
675e01956fSGlenn Barry 		krb5_free_unparsed_name(context, s_name);
685e01956fSGlenn Barry 	    }
695e01956fSGlenn Barry 	    krb5_free_context(context);
705e01956fSGlenn Barry 	    return(GSS_S_CALL_BAD_STRUCTURE|GSS_S_BAD_NAME);
71ab9b2e15Sgtb 	}
72ab9b2e15Sgtb 
73*55fea89dSDan Cross 	if ((code = krb5_unparse_name(context, (krb5_principal) input_name,
74ab9b2e15Sgtb 				      &str))) {
75ab9b2e15Sgtb 		if (minor_status)
76ab9b2e15Sgtb 			*minor_status = code;
775e01956fSGlenn Barry 		save_error_info((OM_uint32)code, context);
78ab9b2e15Sgtb 		krb5_free_context(context);
79ab9b2e15Sgtb 		return(GSS_S_FAILURE);
80ab9b2e15Sgtb 	}
81ab9b2e15Sgtb 
82ab9b2e15Sgtb 	krb5_free_context(context);
83ab9b2e15Sgtb 	length = strlen(str);
84ab9b2e15Sgtb 	exported_name->length = 10 + length + gss_mech_krb5->length;
85ab9b2e15Sgtb 	exported_name->value = malloc(exported_name->length);
86ab9b2e15Sgtb 	if (!exported_name->value) {
87ab9b2e15Sgtb 		free(str);
88ab9b2e15Sgtb 		if (minor_status)
89ab9b2e15Sgtb 			*minor_status = ENOMEM;
90ab9b2e15Sgtb 		return(GSS_S_FAILURE);
91ab9b2e15Sgtb 	}
92ab9b2e15Sgtb 	cp = exported_name->value;
93ab9b2e15Sgtb 
94ab9b2e15Sgtb 	/* Note: we assume the OID will be less than 128 bytes... */
95ab9b2e15Sgtb 	*cp++ = 0x04; *cp++ = 0x01;
96ab9b2e15Sgtb 	*cp++ = (gss_mech_krb5->length+2) >> 8;
97ab9b2e15Sgtb 	*cp++ = (gss_mech_krb5->length+2) & 0xFF;
98ab9b2e15Sgtb 	*cp++ = 0x06;
99ab9b2e15Sgtb 	*cp++ = (gss_mech_krb5->length) & 0xFF;
100ab9b2e15Sgtb 	memcpy(cp, gss_mech_krb5->elements, gss_mech_krb5->length);
101ab9b2e15Sgtb 	cp += gss_mech_krb5->length;
102ab9b2e15Sgtb 	*cp++ = length >> 24;
103ab9b2e15Sgtb 	*cp++ = length >> 16;
104ab9b2e15Sgtb 	*cp++ = length >> 8;
105ab9b2e15Sgtb 	*cp++ = length & 0xFF;
106ab9b2e15Sgtb 	memcpy(cp, str, length);
107ab9b2e15Sgtb 
108ab9b2e15Sgtb 	free(str);
109ab9b2e15Sgtb 
110ab9b2e15Sgtb 	return(GSS_S_COMPLETE);
111ab9b2e15Sgtb }
112