-
Notifications
You must be signed in to change notification settings - Fork 4
/
rd_req.c
304 lines (257 loc) · 8.5 KB
/
rd_req.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (c) 2008-2010 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Portions Copyright (c) 2008-2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "heim.h"
#include "mit-krb5.h"
#include <string.h>
#include <errno.h>
static krb5_flags
mshim_mit_ap_rep_flags(mit_krb5_flags ap_req_options)
{
krb5_flags flags = 0;
if (ap_req_options & MIT_KRB5_RECVAUTH_SKIP_VERSION)
flags |= KRB5_RECVAUTH_IGNORE_VERSION;
return flags;
}
static void
mshim_hticket2mticket(krb5_context context, krb5_ticket *h, mit_krb5_ticket *m)
{
m->server = mshim_hprinc2mprinc(context, h->server);
m->enc_part2 = calloc(1, sizeof(*m->enc_part2));
m->enc_part2->flags = 0;
if (h->ticket.flags.forwardable)
m->enc_part2->flags |= MIT_TKT_FLG_FORWARDABLE;
if (h->ticket.flags.forwarded)
m->enc_part2->flags |= MIT_TKT_FLG_FORWARDED;
if (h->ticket.flags.proxiable)
m->enc_part2->flags |= MIT_TKT_FLG_PROXIABLE;
if (h->ticket.flags.proxy)
m->enc_part2->flags |= MIT_TKT_FLG_PROXY;
if (h->ticket.flags.may_postdate)
m->enc_part2->flags |= MIT_TKT_FLG_MAY_POSTDATE;
if (h->ticket.flags.postdated)
m->enc_part2->flags |= MIT_TKT_FLG_POSTDATED;
if (h->ticket.flags.invalid)
m->enc_part2->flags |= MIT_TKT_FLG_INVALID;
if (h->ticket.flags.renewable)
m->enc_part2->flags |= MIT_TKT_FLG_RENEWABLE;
if (h->ticket.flags.initial)
m->enc_part2->flags |= MIT_TKT_FLG_INITIAL;
if (h->ticket.flags.pre_authent)
m->enc_part2->flags |= MIT_TKT_FLG_PRE_AUTH;
if (h->ticket.flags.hw_authent)
m->enc_part2->flags |= MIT_TKT_FLG_HW_AUTH;
if (h->ticket.flags.transited_policy_checked)
m->enc_part2->flags |= MIT_TKT_FLG_TRANSIT_POLICY_CHECKED;
if (h->ticket.flags.ok_as_delegate)
m->enc_part2->flags |= MIT_TKT_FLG_OK_AS_DELEGATE;
if (h->ticket.flags.anonymous)
m->enc_part2->flags |= MIT_TKT_FLG_ANONYMOUS;
m->enc_part2->session = mshim_malloc(sizeof(*m->enc_part2->session));
mshim_hkeyblock2mkeyblock(&h->ticket.key, m->enc_part2->session);
m->enc_part2->client = mshim_hprinc2mprinc(HC(context), h->client);
m->enc_part2->transited.tr_type = 0;
m->enc_part2->transited.tr_contents.data = NULL;
m->enc_part2->transited.tr_contents.length = 0;
m->enc_part2->times.authtime = h->ticket.authtime;
m->enc_part2->times.starttime =
h->ticket.starttime ? *h->ticket.starttime : 0 ;
m->enc_part2->times.endtime = h->ticket.endtime;
m->enc_part2->times.renew_till =
h->ticket.renew_till ? *h->ticket.renew_till : 0 ;
m->enc_part2->caddrs = NULL;
m->enc_part2->authorization_data = NULL;
}
mit_krb5_error_code KRB5_CALLCONV
krb5_rd_req(mit_krb5_context context,
mit_krb5_auth_context *ac,
const mit_krb5_data *inbuf,
mit_krb5_const_principal server,
mit_krb5_keytab keytab,
mit_krb5_flags *ap_req_options,
mit_krb5_ticket **ticket)
{
krb5_data idata, *d = NULL;
struct comb_principal *p = (struct comb_principal *)server;
krb5_ticket *t;
krb5_flags flags;
krb5_error_code ret;
LOG_ENTRY();
if (inbuf) {
d = &idata;
idata.data = inbuf->data;
idata.length = inbuf->length;
}
ret = heim_krb5_rd_req(HC(context), (krb5_auth_context *)ac,
d, p ? p->heim : NULL, (krb5_keytab)keytab,
&flags, &t);
if (ret == 0) {
mit_krb5_ticket *mt;
mt = calloc(1, sizeof(*mt));
mshim_hticket2mticket(HC(context), t, mt);
heim_krb5_free_ticket(HC(context), t);
*ticket = mt;
}
return ret;
}
mit_krb5_error_code KRB5_CALLCONV
krb5_recvauth(mit_krb5_context context,
mit_krb5_auth_context *ac,
mit_krb5_pointer fd,
char *appl_version,
mit_krb5_principal server,
mit_krb5_int32 flags,
mit_krb5_keytab keytab,
mit_krb5_ticket **ticket)
{
struct comb_principal *s = (struct comb_principal *)server;
krb5_ticket *hticket = NULL;
mit_krb5_error_code ret;
LOG_ENTRY();
ret = heim_krb5_recvauth(HC(context),
(krb5_auth_context *)ac,
fd,
appl_version,
s ? s->heim : NULL,
mshim_mit_ap_rep_flags(flags),
(krb5_keytab)keytab,
&hticket);
if (ticket && hticket) {
*ticket = calloc(1, sizeof(**ticket));
mshim_hticket2mticket(HC(context), hticket, *ticket);
}
if (hticket)
heim_krb5_free_ticket(HC(context), hticket);
return ret;
}
static krb5_boolean
match_appl_version(const void *ptr, const char *str)
{
mit_krb5_data *version = (mit_krb5_data *)ptr;
version->magic = MIT_KV5M_DATA;
version->data = strdup(str);
version->length = strlen(str);
return 1;
}
mit_krb5_error_code KRB5_CALLCONV
krb5_recvauth_version(mit_krb5_context context,
mit_krb5_auth_context *ac,
mit_krb5_pointer fd,
mit_krb5_principal server,
mit_krb5_int32 flags,
mit_krb5_keytab keytab,
mit_krb5_ticket **ticket,
mit_krb5_data *version)
{
struct comb_principal *s = (struct comb_principal *)server;
krb5_ticket *hticket = NULL;
mit_krb5_error_code ret;
LOG_ENTRY();
ret = heim_krb5_recvauth_match_version(HC(context),
(krb5_auth_context *)ac,
fd,
match_appl_version,
version,
s ? s->heim : NULL,
mshim_mit_ap_rep_flags(flags),
(krb5_keytab)keytab,
&hticket);
if (ticket && hticket) {
*ticket = calloc(1, sizeof(**ticket));
mshim_hticket2mticket(HC(context), hticket, *ticket);
}
if (hticket)
heim_krb5_free_ticket(HC(context), hticket);
return ret;
}
mit_krb5_error_code KRB5_CALLCONV
krb5_rd_priv(mit_krb5_context context,
mit_krb5_auth_context auth_context,
const mit_krb5_data *inbuf,
mit_krb5_data *outbuf,
mit_krb5_replay_data *replay)
{
krb5_replay_data outdata;
mit_krb5_error_code ret;
LOG_ENTRY();
krb5_data in, out;
memset(outbuf, 0, sizeof(*outbuf));
memset(&outdata, 0, sizeof(outdata));
in.data = inbuf->data;
in.length = inbuf->length;
ret = heim_krb5_rd_priv(HC(context),
(krb5_auth_context)auth_context,
&in,
&out,
&outdata);
if (ret)
return ret;
mshim_hdata2mdata(&out, outbuf);
heim_krb5_data_free(&out);
if (replay) {
memset(replay, 0, sizeof(*replay));
mshim_hreplay2mreplay(&outdata, replay);
}
return 0;
}
mit_krb5_error_code KRB5_CALLCONV
krb5_rd_safe(mit_krb5_context context,
mit_krb5_auth_context auth_context,
const mit_krb5_data *inbuf,
mit_krb5_data *outbuf,
mit_krb5_replay_data *replay)
{
krb5_replay_data outdata;
mit_krb5_error_code ret;
LOG_ENTRY();
krb5_data in, out;
memset(outbuf, 0, sizeof(*outbuf));
memset(&outdata, 0, sizeof(outdata));
in.data = inbuf->data;
in.length = inbuf->length;
ret = heim_krb5_rd_safe(HC(context),
(krb5_auth_context)auth_context,
&in,
&out,
&outdata);
if (ret)
return ret;
mshim_hdata2mdata(&out, outbuf);
heim_krb5_data_free(&out);
if (replay) {
memset(replay, 0, sizeof(*replay));
mshim_hreplay2mreplay(&outdata, replay);
}
return 0;
}