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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
#include <k5-int.h>
#include "cleanup.h"
#include "auth_con.h"
#include <stddef.h> /* NULL */
#include <stdlib.h> /* malloc */
#include <errno.h> /* ENOMEM */
/*-------------------- decrypt_credencdata --------------------*/
/*
* decrypt the enc_part of a krb5_cred
*/
static krb5_error_code
decrypt_credencdata(context, pcred, pkeyblock, pcredenc)
krb5_context context;
krb5_cred * pcred;
krb5_keyblock * pkeyblock;
krb5_cred_enc_part * pcredenc;
{
krb5_cred_enc_part * ppart;
krb5_encrypt_block eblock;
krb5_error_code retval;
krb5_data scratch;
scratch.length = pcred->enc_part.ciphertext.length;
if (!(scratch.data = (char *)malloc(scratch.length)))
return ENOMEM;
if (pkeyblock != NULL) {
if (!valid_enctype(pcred->enc_part.enctype)) {
free(scratch.data);
return KRB5_PROG_ETYPE_NOSUPP;
}
/* put together an eblock for this decryption */
krb5_use_enctype(context, &eblock, pcred->enc_part.enctype);
/* do any necessary key pre-processing */
if ((retval = krb5_process_key(context, &eblock, pkeyblock)))
goto cleanup;
/* call the decryption routine */
if ((retval = krb5_decrypt(context,
(krb5_pointer) pcred->enc_part.ciphertext.data,
(krb5_pointer) scratch.data,
scratch.length, &eblock, 0))) {
(void)krb5_finish_key(context, &eblock);
goto cleanup;
}
if ((retval = krb5_finish_key(context, &eblock)))
goto cleanup;
} else {
memcpy(scratch.data, pcred->enc_part.ciphertext.data, scratch.length);
}
/* now decode the decrypted stuff */
if ((retval = decode_krb5_enc_cred_part(&scratch, &ppart)))
goto cleanup_encpart;
*pcredenc = *ppart;
retval = 0;
cleanup_encpart:
memset(ppart, 0, sizeof(*ppart));
krb5_xfree(ppart);
cleanup:
memset(scratch.data, 0, scratch.length);
krb5_xfree(scratch.data);
return retval;
}
/*----------------------- krb5_rd_cred_basic -----------------------*/
static krb5_error_code
krb5_rd_cred_basic(context, pcreddata, pkeyblock, local_addr, remote_addr,
replaydata, pppcreds)
krb5_context context;
krb5_data * pcreddata;
krb5_keyblock * pkeyblock;
krb5_address * local_addr;
krb5_address * remote_addr;
krb5_replay_data * replaydata;
krb5_creds *** pppcreds;
{
krb5_error_code retval;
krb5_cred * pcred;
krb5_int32 ncreds;
krb5_int32 i = 0;
krb5_cred_enc_part encpart;
/* decode cred message */
if ((retval = decode_krb5_cred(pcreddata, &pcred)))
return retval;
memset(&encpart, sizeof(encpart), 0);
if ((retval = decrypt_credencdata(context, pcred, pkeyblock, &encpart)))
goto cleanup_cred;
/*
* Only check the remote address if the KRB_CRED message was
* protected by encryption. If it came in the checksum field of
* an init_sec_context message, skip over this check.
*/
if (remote_addr && encpart.s_address && pkeyblock != NULL) {
if (!krb5_address_compare(context, remote_addr, encpart.s_address)) {
retval = KRB5KRB_AP_ERR_BADADDR;
goto cleanup_cred;
}
}
if (encpart.r_address) {
if (local_addr) {
if (!krb5_address_compare(context, local_addr, encpart.r_address)) {
retval = KRB5KRB_AP_ERR_BADADDR;
goto cleanup_cred;
}
} else {
krb5_address **our_addrs;
if ((retval = krb5_os_localaddr(context, &our_addrs))) {
goto cleanup_cred;
}
if (!krb5_address_search(context, encpart.r_address, our_addrs)) {
krb5_free_addresses(context, our_addrs);
retval = KRB5KRB_AP_ERR_BADADDR;
goto cleanup_cred;
}
krb5_free_addresses(context, our_addrs);
}
}
replaydata->timestamp = encpart.timestamp;
replaydata->usec = encpart.usec;
replaydata->seq = encpart.nonce;
/*
* Allocate the list of creds. The memory is allocated so that
* krb5_free_tgt_creds can be used to free the list.
*/
for (ncreds = 0; pcred->tickets[ncreds]; ncreds++);
if ((*pppcreds =
(krb5_creds **)malloc((size_t)(sizeof(krb5_creds *) *
(ncreds + 1)))) == NULL) {
retval = ENOMEM;
goto cleanup_cred;
}
(*pppcreds)[0] = NULL;
/*
* For each credential, create a strcture in the list of
* credentials and copy the information.
*/
while (i < ncreds) {
krb5_cred_info * pinfo;
krb5_creds * pcur;
krb5_data * pdata;
if ((pcur = (krb5_creds *)malloc(sizeof(krb5_creds))) == NULL) {
retval = ENOMEM;
goto cleanup;
}
(*pppcreds)[i] = pcur;
(*pppcreds)[i+1] = 0;
pinfo = encpart.ticket_info[i++];
memset(pcur, 0, sizeof(krb5_creds));
if ((retval = krb5_copy_principal(context, pinfo->client,
&pcur->client)))
goto cleanup;
if ((retval = krb5_copy_principal(context, pinfo->server,
&pcur->server)))
goto cleanup;
if ((retval = krb5_copy_keyblock_contents(context, pinfo->session,
&pcur->keyblock)))
goto cleanup;
if ((retval = krb5_copy_addresses(context, pinfo->caddrs,
&pcur->addresses)))
goto cleanup;
if ((retval = encode_krb5_ticket(pcred->tickets[i - 1], &pdata)))
goto cleanup;
pcur->ticket = *pdata;
krb5_xfree(pdata);
pcur->is_skey = FALSE;
pcur->magic = KV5M_CREDS;
pcur->times = pinfo->times;
pcur->ticket_flags = pinfo->flags;
pcur->authdata = NULL; /* not used */
memset(&pcur->second_ticket, 0, sizeof(pcur->second_ticket));
}
/*
* NULL terminate the list
*/
(*pppcreds)[i] = NULL;
cleanup:
if (retval)
krb5_free_tgt_creds(context, *pppcreds);
cleanup_cred:
krb5_free_cred(context, pcred);
krb5_free_cred_enc_part(context, &encpart);
return retval;
}
/*----------------------- krb5_rd_cred -----------------------*/
#define in_clock_skew(date) (labs((date)-currenttime) < context->clockskew)
/*
* This functions takes as input an KRB_CRED message, validates it, and
* outputs the nonce and an array of the forwarded credentials.
*/
KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
krb5_rd_cred(context, auth_context, pcreddata, pppcreds, outdata)
krb5_context context;
krb5_auth_context auth_context;
krb5_data FAR * pcreddata;
krb5_creds FAR * FAR * FAR * pppcreds;
krb5_replay_data FAR * outdata;
{
krb5_error_code retval;
krb5_keyblock * keyblock;
krb5_replay_data replaydata;
/* Get keyblock */
if ((keyblock = auth_context->local_subkey) == NULL)
if ((keyblock = auth_context->remote_subkey) == NULL)
keyblock = auth_context->keyblock;
if (((auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_RET_TIME) ||
(auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_RET_SEQUENCE)) &&
(outdata == NULL))
/* Need a better error */
return KRB5_RC_REQUIRED;
if ((auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_DO_TIME) &&
(auth_context->rcache == NULL))
return KRB5_RC_REQUIRED;
{
krb5_address * premote_fulladdr = NULL;
krb5_address * plocal_fulladdr = NULL;
krb5_address remote_fulladdr;
krb5_address local_fulladdr;
CLEANUP_INIT(2);
if (auth_context->local_addr) {
if (auth_context->local_port) {
if (!(retval = krb5_make_fulladdr(context,auth_context->local_addr,
auth_context->local_port,
&local_fulladdr))){
CLEANUP_PUSH(local_fulladdr.contents, free);
plocal_fulladdr = &local_fulladdr;
} else {
return retval;
}
} else {
plocal_fulladdr = auth_context->local_addr;
}
}
if (auth_context->remote_addr) {
if (auth_context->remote_port) {
if (!(retval = krb5_make_fulladdr(context,auth_context->remote_addr,
auth_context->remote_port,
&remote_fulladdr))){
CLEANUP_PUSH(remote_fulladdr.contents, free);
premote_fulladdr = &remote_fulladdr;
} else {
return retval;
}
} else {
premote_fulladdr = auth_context->remote_addr;
}
}
if ((retval = krb5_rd_cred_basic(context, pcreddata, keyblock,
plocal_fulladdr, premote_fulladdr,
&replaydata, pppcreds))) {
CLEANUP_DONE();
return retval;
}
CLEANUP_DONE();
}
if (auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_DO_TIME) {
krb5_donot_replay replay;
krb5_timestamp currenttime;
if ((retval = krb5_timeofday(context, ¤ttime)))
goto error;
if (!in_clock_skew(replaydata.timestamp)) {
retval = KRB5KRB_AP_ERR_SKEW;
goto error;
}
if ((retval = krb5_gen_replay_name(context, auth_context->remote_addr,
"_forw", &replay.client)))
goto error;
replay.server = ""; /* XXX */
replay.cusec = replaydata.usec;
replay.ctime = replaydata.timestamp;
if ((retval = krb5_rc_store(context, auth_context->rcache, &replay))) {
krb5_xfree(replay.client);
goto error;
}
krb5_xfree(replay.client);
}
if (auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {
if (auth_context->remote_seq_number != replaydata.seq) {
retval = KRB5KRB_AP_ERR_BADORDER;
goto error;
}
auth_context->remote_seq_number++;
}
if ((auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_RET_TIME) ||
(auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_RET_SEQUENCE)) {
outdata->timestamp = replaydata.timestamp;
outdata->usec = replaydata.usec;
outdata->seq = replaydata.seq;
}
error:;
if (retval)
krb5_xfree(*pppcreds);
return retval;
}
|