1/*
2 * Copyright (c) 2008-2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2008-2010 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36
37#include "heim.h"
38#include <string.h>
39#include <errno.h>
40#include <syslog.h>
41
42
43mit_krb5_error_code KRB5_CALLCONV
44krb5_decode_ticket(const mit_krb5_data *code,
45		   mit_krb5_ticket **rep)
46{
47    krb5_error_code ret;
48    Ticket t;
49
50    LOG_ENTRY();
51
52    ret = decode_Ticket((unsigned char *)code->data, code->length, &t, NULL);
53    if (ret)
54	return ret;
55
56    *rep = calloc(1, sizeof(**rep));
57
58    /* XXX */
59    (*rep)->enc_part.kvno = t.enc_part.kvno ? *t.enc_part.kvno : 0;
60
61    free_Ticket(&t);
62
63    return 0;
64}
65
66mit_krb5_error_code KRB5_CALLCONV
67krb5_get_credentials(mit_krb5_context context,
68		     mit_krb5_flags flags,
69		     mit_krb5_ccache id,
70		     mit_krb5_creds *mcreds,
71		     mit_krb5_creds **creds)
72{
73    krb5_error_code ret;
74    krb5_flags options = flags;
75    krb5_creds *hcreds = NULL, hmcreds;
76
77    LOG_ENTRY();
78
79    mshim_mcred2hcred(HC(context), mcreds, &hmcreds);
80
81    ret = heim_krb5_get_credentials(HC(context), options, (krb5_ccache)id, &hmcreds, &hcreds);
82
83    heim_krb5_free_cred_contents(HC(context), &hmcreds);
84    if (ret == 0) {
85	*creds = calloc(1, sizeof(**creds));
86	mshim_hcred2mcred(HC(context), hcreds, *creds);
87	heim_krb5_free_creds(HC(context), hcreds);
88    }
89
90    return ret;
91}
92
93mit_krb5_error_code KRB5_CALLCONV
94krb5_copy_creds(mit_krb5_context context,
95		const mit_krb5_creds *from,
96		mit_krb5_creds **to)
97{
98    mit_krb5_error_code ret;
99    mit_krb5_creds *c;
100
101    c = mshim_malloc(sizeof(*c));
102
103    c->magic = MIT_KV5M_CREDS;
104
105    ret = krb5_copy_principal(context, from->client, &c->client);
106    if (ret)
107	abort();
108    ret = krb5_copy_principal(context, from->server, &c->server);
109    if (ret)
110	abort();
111
112    ret = krb5_copy_keyblock_contents(context, &from->keyblock,
113				      &c->keyblock);
114    if (ret)
115	abort();
116
117    c->ticket.magic = MIT_KV5M_DATA;
118    c->ticket.length = from->ticket.length;
119    c->ticket.data = mshim_malloc(from->ticket.length);
120    memcpy(c->ticket.data, from->ticket.data, c->ticket.length);
121
122    c->times.authtime = from->times.authtime;
123    c->times.starttime = from->times.starttime;
124    c->times.endtime = from->times.endtime;
125    c->times.renew_till = from->times.renew_till;
126
127    c->ticket_flags = from->ticket_flags;
128
129    *to = c;
130
131    return 0;
132}
133