[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / krb5 / rd_cred.c
blobf41edfa2b51dad58f60128878540443291a84807
1 /*
2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <krb5_locl.h>
36 static krb5_error_code
37 compare_addrs(krb5_context context,
38 krb5_address *a,
39 krb5_address *b,
40 const char *message)
42 char a_str[64], b_str[64];
43 size_t len;
45 if(krb5_address_compare (context, a, b))
46 return 0;
48 krb5_print_address (a, a_str, sizeof(a_str), &len);
49 krb5_print_address (b, b_str, sizeof(b_str), &len);
50 krb5_set_error_message(context, KRB5KRB_AP_ERR_BADADDR,
51 "%s: %s != %s", message, b_str, a_str);
52 return KRB5KRB_AP_ERR_BADADDR;
55 krb5_error_code KRB5_LIB_FUNCTION
56 krb5_rd_cred(krb5_context context,
57 krb5_auth_context auth_context,
58 krb5_data *in_data,
59 krb5_creds ***ret_creds,
60 krb5_replay_data *outdata)
62 krb5_error_code ret;
63 size_t len;
64 KRB_CRED cred;
65 EncKrbCredPart enc_krb_cred_part;
66 krb5_data enc_krb_cred_part_data;
67 krb5_crypto crypto;
68 int i;
70 memset(&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
72 if ((auth_context->flags &
73 (KRB5_AUTH_CONTEXT_RET_TIME | KRB5_AUTH_CONTEXT_RET_SEQUENCE)) &&
74 outdata == NULL)
75 return KRB5_RC_REQUIRED; /* XXX better error, MIT returns this */
77 *ret_creds = NULL;
79 ret = decode_KRB_CRED(in_data->data, in_data->length,
80 &cred, &len);
81 if(ret) {
82 krb5_clear_error_message(context);
83 return ret;
86 if (cred.pvno != 5) {
87 ret = KRB5KRB_AP_ERR_BADVERSION;
88 krb5_clear_error_message (context);
89 goto out;
92 if (cred.msg_type != krb_cred) {
93 ret = KRB5KRB_AP_ERR_MSG_TYPE;
94 krb5_clear_error_message (context);
95 goto out;
98 if (cred.enc_part.etype == ETYPE_NULL) {
99 /* DK: MIT GSS-API Compatibility */
100 enc_krb_cred_part_data.length = cred.enc_part.cipher.length;
101 enc_krb_cred_part_data.data = cred.enc_part.cipher.data;
102 } else {
103 /* Try both subkey and session key.
105 * RFC4120 claims we should use the session key, but Heimdal
106 * before 0.8 used the remote subkey if it was send in the
107 * auth_context.
110 if (auth_context->remote_subkey) {
111 ret = krb5_crypto_init(context, auth_context->remote_subkey,
112 0, &crypto);
113 if (ret)
114 goto out;
116 ret = krb5_decrypt_EncryptedData(context,
117 crypto,
118 KRB5_KU_KRB_CRED,
119 &cred.enc_part,
120 &enc_krb_cred_part_data);
122 krb5_crypto_destroy(context, crypto);
126 * If there was not subkey, or we failed using subkey,
127 * retry using the session key
129 if (auth_context->remote_subkey == NULL || ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
132 ret = krb5_crypto_init(context, auth_context->keyblock,
133 0, &crypto);
135 if (ret)
136 goto out;
138 ret = krb5_decrypt_EncryptedData(context,
139 crypto,
140 KRB5_KU_KRB_CRED,
141 &cred.enc_part,
142 &enc_krb_cred_part_data);
144 krb5_crypto_destroy(context, crypto);
146 if (ret)
147 goto out;
150 ret = decode_EncKrbCredPart(enc_krb_cred_part_data.data,
151 enc_krb_cred_part_data.length,
152 &enc_krb_cred_part,
153 &len);
154 if (enc_krb_cred_part_data.data != cred.enc_part.cipher.data)
155 krb5_data_free(&enc_krb_cred_part_data);
156 if (ret) {
157 krb5_set_error_message(context, ret,
158 N_("Failed to decode "
159 "encrypte credential part", ""));
160 goto out;
163 /* check sender address */
165 if (enc_krb_cred_part.s_address
166 && auth_context->remote_address
167 && auth_context->remote_port) {
168 krb5_address *a;
170 ret = krb5_make_addrport (context, &a,
171 auth_context->remote_address,
172 auth_context->remote_port);
173 if (ret)
174 goto out;
177 ret = compare_addrs(context, a, enc_krb_cred_part.s_address,
178 N_("sender address is wrong "
179 "in received creds", ""));
180 krb5_free_address(context, a);
181 free(a);
182 if(ret)
183 goto out;
186 /* check receiver address */
188 if (enc_krb_cred_part.r_address
189 && auth_context->local_address) {
190 if(auth_context->local_port &&
191 enc_krb_cred_part.r_address->addr_type == KRB5_ADDRESS_ADDRPORT) {
192 krb5_address *a;
193 ret = krb5_make_addrport (context, &a,
194 auth_context->local_address,
195 auth_context->local_port);
196 if (ret)
197 goto out;
199 ret = compare_addrs(context, a, enc_krb_cred_part.r_address,
200 N_("receiver address is wrong "
201 "in received creds", ""));
202 krb5_free_address(context, a);
203 free(a);
204 if(ret)
205 goto out;
206 } else {
207 ret = compare_addrs(context, auth_context->local_address,
208 enc_krb_cred_part.r_address,
209 N_("receiver address is wrong "
210 "in received creds", ""));
211 if(ret)
212 goto out;
216 /* check timestamp */
217 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
218 krb5_timestamp sec;
220 krb5_timeofday (context, &sec);
222 if (enc_krb_cred_part.timestamp == NULL ||
223 enc_krb_cred_part.usec == NULL ||
224 abs(*enc_krb_cred_part.timestamp - sec)
225 > context->max_skew) {
226 krb5_clear_error_message (context);
227 ret = KRB5KRB_AP_ERR_SKEW;
228 goto out;
232 if ((auth_context->flags &
233 (KRB5_AUTH_CONTEXT_RET_TIME | KRB5_AUTH_CONTEXT_RET_SEQUENCE))) {
234 /* if these fields are not present in the cred-part, silently
235 return zero */
236 memset(outdata, 0, sizeof(*outdata));
237 if(enc_krb_cred_part.timestamp)
238 outdata->timestamp = *enc_krb_cred_part.timestamp;
239 if(enc_krb_cred_part.usec)
240 outdata->usec = *enc_krb_cred_part.usec;
241 if(enc_krb_cred_part.nonce)
242 outdata->seq = *enc_krb_cred_part.nonce;
245 /* Convert to NULL terminated list of creds */
247 *ret_creds = calloc(enc_krb_cred_part.ticket_info.len + 1,
248 sizeof(**ret_creds));
250 if (*ret_creds == NULL) {
251 ret = ENOMEM;
252 krb5_set_error_message(context, ret,
253 N_("malloc: out of memory", ""));
254 goto out;
257 for (i = 0; i < enc_krb_cred_part.ticket_info.len; ++i) {
258 KrbCredInfo *kci = &enc_krb_cred_part.ticket_info.val[i];
259 krb5_creds *creds;
261 creds = calloc(1, sizeof(*creds));
262 if(creds == NULL) {
263 ret = ENOMEM;
264 krb5_set_error_message(context, ret,
265 N_("malloc: out of memory", ""));
266 goto out;
269 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
270 &cred.tickets.val[i], &len, ret);
271 if (ret) {
272 free(creds);
273 goto out;
275 if(creds->ticket.length != len)
276 krb5_abortx(context, "internal error in ASN.1 encoder");
277 copy_EncryptionKey (&kci->key, &creds->session);
278 if (kci->prealm && kci->pname)
279 _krb5_principalname2krb5_principal (context,
280 &creds->client,
281 *kci->pname,
282 *kci->prealm);
283 if (kci->flags)
284 creds->flags.b = *kci->flags;
285 if (kci->authtime)
286 creds->times.authtime = *kci->authtime;
287 if (kci->starttime)
288 creds->times.starttime = *kci->starttime;
289 if (kci->endtime)
290 creds->times.endtime = *kci->endtime;
291 if (kci->renew_till)
292 creds->times.renew_till = *kci->renew_till;
293 if (kci->srealm && kci->sname)
294 _krb5_principalname2krb5_principal (context,
295 &creds->server,
296 *kci->sname,
297 *kci->srealm);
298 if (kci->caddr)
299 krb5_copy_addresses (context,
300 kci->caddr,
301 &creds->addresses);
303 (*ret_creds)[i] = creds;
306 (*ret_creds)[i] = NULL;
308 free_KRB_CRED (&cred);
309 free_EncKrbCredPart(&enc_krb_cred_part);
311 return 0;
313 out:
314 free_EncKrbCredPart(&enc_krb_cred_part);
315 free_KRB_CRED (&cred);
316 if(*ret_creds) {
317 for(i = 0; (*ret_creds)[i]; i++)
318 krb5_free_creds(context, (*ret_creds)[i]);
319 free(*ret_creds);
320 *ret_creds = NULL;
322 return ret;
325 krb5_error_code KRB5_LIB_FUNCTION
326 krb5_rd_cred2 (krb5_context context,
327 krb5_auth_context auth_context,
328 krb5_ccache ccache,
329 krb5_data *in_data)
331 krb5_error_code ret;
332 krb5_creds **creds;
333 int i;
335 ret = krb5_rd_cred(context, auth_context, in_data, &creds, NULL);
336 if(ret)
337 return ret;
339 /* Store the creds in the ccache */
341 for(i = 0; creds && creds[i]; i++) {
342 krb5_cc_store_cred(context, ccache, creds[i]);
343 krb5_free_creds(context, creds[i]);
345 free(creds);
346 return 0;