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