convert_creds.c revision 178825
138494Sobrien/*
238494Sobrien * Copyright (c) 1997 - 2004 Kungliga Tekniska H�gskolan
338494Sobrien * (Royal Institute of Technology, Stockholm, Sweden).
438494Sobrien * All rights reserved.
538494Sobrien *
638494Sobrien * Redistribution and use in source and binary forms, with or without
738494Sobrien * modification, are permitted provided that the following conditions
838494Sobrien * are met:
938494Sobrien *
1038494Sobrien * 1. Redistributions of source code must retain the above copyright
1138494Sobrien *    notice, this list of conditions and the following disclaimer.
1238494Sobrien *
1338494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1438494Sobrien *    notice, this list of conditions and the following disclaimer in the
1538494Sobrien *    documentation and/or other materials provided with the distribution.
1638494Sobrien *
1738494Sobrien * 3. Neither the name of the Institute nor the names of its contributors
1838494Sobrien *    may be used to endorse or promote products derived from this software
1938494Sobrien *    without specific prior written permission.
2042629Sobrien *
2138494Sobrien * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2238494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2338494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2438494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2538494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2638494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2738494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2838494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2938494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3038494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3138494Sobrien * SUCH DAMAGE.
3238494Sobrien */
3338494Sobrien
3438494Sobrien#include "krb5_locl.h"
3538494SobrienRCSID("$Id: convert_creds.c 22050 2007-11-11 11:20:46Z lha $");
3638494Sobrien
3738494Sobrien#include "krb5-v4compat.h"
3838494Sobrien
3938494Sobrienstatic krb5_error_code
4038494Sobriencheck_ticket_flags(TicketFlags f)
4142629Sobrien{
4238494Sobrien    return 0; /* maybe add some more tests here? */
4338494Sobrien}
4438494Sobrien
4538494Sobrien/**
4638494Sobrien * Convert the v5 credentials in in_cred to v4-dito in v4creds.  This
4738494Sobrien * is done by sending them to the 524 function in the KDC.  If
4838494Sobrien * `in_cred' doesn't contain a DES session key, then a new one is
4938494Sobrien * gotten from the KDC and stored in the cred cache `ccache'.
5038494Sobrien *
5138494Sobrien * @param context Kerberos 5 context.
5238494Sobrien * @param in_cred the credential to convert
5338494Sobrien * @param v4creds the converted credential
5438494Sobrien *
5538494Sobrien * @return Returns 0 to indicate success. Otherwise an kerberos et
5638494Sobrien * error code is returned, see krb5_get_error_message().
5738494Sobrien *
5838494Sobrien * @ingroup krb5_v4compat
5938494Sobrien */
60
61krb5_error_code KRB5_LIB_FUNCTION
62krb524_convert_creds_kdc(krb5_context context,
63			 krb5_creds *in_cred,
64			 struct credentials *v4creds)
65{
66    krb5_error_code ret;
67    krb5_data reply;
68    krb5_storage *sp;
69    int32_t tmp;
70    krb5_data ticket;
71    char realm[REALM_SZ];
72    krb5_creds *v5_creds = in_cred;
73
74    ret = check_ticket_flags(v5_creds->flags.b);
75    if(ret)
76	goto out2;
77
78    {
79	krb5_krbhst_handle handle;
80
81	ret = krb5_krbhst_init(context,
82			       krb5_principal_get_realm(context,
83							v5_creds->server),
84			       KRB5_KRBHST_KRB524,
85			       &handle);
86	if (ret)
87	    goto out2;
88
89	ret = krb5_sendto (context,
90			   &v5_creds->ticket,
91			   handle,
92			   &reply);
93	krb5_krbhst_free(context, handle);
94	if (ret)
95	    goto out2;
96    }
97    sp = krb5_storage_from_mem(reply.data, reply.length);
98    if(sp == NULL) {
99	ret = ENOMEM;
100	krb5_set_error_string (context, "malloc: out of memory");
101	goto out2;
102    }
103    krb5_ret_int32(sp, &tmp);
104    ret = tmp;
105    if(ret == 0) {
106	memset(v4creds, 0, sizeof(*v4creds));
107	ret = krb5_ret_int32(sp, &tmp);
108	if(ret)
109	    goto out;
110	v4creds->kvno = tmp;
111	ret = krb5_ret_data(sp, &ticket);
112	if(ret)
113	    goto out;
114	v4creds->ticket_st.length = ticket.length;
115	memcpy(v4creds->ticket_st.dat, ticket.data, ticket.length);
116	krb5_data_free(&ticket);
117	ret = krb5_524_conv_principal(context,
118				      v5_creds->server,
119				      v4creds->service,
120				      v4creds->instance,
121				      v4creds->realm);
122	if(ret)
123	    goto out;
124	v4creds->issue_date = v5_creds->times.starttime;
125	v4creds->lifetime = _krb5_krb_time_to_life(v4creds->issue_date,
126						   v5_creds->times.endtime);
127	ret = krb5_524_conv_principal(context, v5_creds->client,
128				      v4creds->pname,
129				      v4creds->pinst,
130				      realm);
131	if(ret)
132	    goto out;
133	memcpy(v4creds->session, v5_creds->session.keyvalue.data, 8);
134    } else {
135	krb5_set_error_string(context, "converting credentials: %s",
136			      krb5_get_err_text(context, ret));
137    }
138out:
139    krb5_storage_free(sp);
140    krb5_data_free(&reply);
141out2:
142    if (v5_creds != in_cred)
143	krb5_free_creds (context, v5_creds);
144    return ret;
145}
146
147/**
148 * Convert the v5 credentials in in_cred to v4-dito in v4creds,
149 * check the credential cache ccache before checking with the KDC.
150 *
151 * @param context Kerberos 5 context.
152 * @param ccache credential cache used to check for des-ticket.
153 * @param in_cred the credential to convert
154 * @param v4creds the converted credential
155 *
156 * @return Returns 0 to indicate success. Otherwise an kerberos et
157 * error code is returned, see krb5_get_error_message().
158 *
159 * @ingroup krb5_v4compat
160 */
161
162krb5_error_code KRB5_LIB_FUNCTION
163krb524_convert_creds_kdc_ccache(krb5_context context,
164				krb5_ccache ccache,
165				krb5_creds *in_cred,
166				struct credentials *v4creds)
167{
168    krb5_error_code ret;
169    krb5_creds *v5_creds = in_cred;
170    krb5_keytype keytype;
171
172    keytype = v5_creds->session.keytype;
173
174    if (keytype != ENCTYPE_DES_CBC_CRC) {
175	/* MIT krb524d doesn't like nothing but des-cbc-crc tickets,
176           so go get one */
177	krb5_creds template;
178
179	memset (&template, 0, sizeof(template));
180	template.session.keytype = ENCTYPE_DES_CBC_CRC;
181	ret = krb5_copy_principal (context, in_cred->client, &template.client);
182	if (ret) {
183	    krb5_free_cred_contents (context, &template);
184	    return ret;
185	}
186	ret = krb5_copy_principal (context, in_cred->server, &template.server);
187	if (ret) {
188	    krb5_free_cred_contents (context, &template);
189	    return ret;
190	}
191
192	ret = krb5_get_credentials (context, 0, ccache,
193				    &template, &v5_creds);
194	krb5_free_cred_contents (context, &template);
195	if (ret)
196	    return ret;
197    }
198
199    ret = krb524_convert_creds_kdc(context, v5_creds, v4creds);
200
201    if (v5_creds != in_cred)
202	krb5_free_creds (context, v5_creds);
203    return ret;
204}
205