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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/krb5/ccache/ccmarshal.c - Functions for serializing creds */
/*
* Copyright (C) 2014 by the Massachusetts Institute of Technology.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDER 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.
*/
/*
* This file implements marshalling and unmarshalling of krb5 credentials and
* principals in versions 1 through 4 of the FILE ccache format. Version 4 is
* also used for the KEYRING ccache type.
*
* The FILE credential cache format uses fixed 16-bit or 32-bit representations
* of integers. In versions 1 and 2 these are in host byte order; in later
* versions they are in big-endian byte order. Variable-length fields are
* represented with a 32-bit length followed by the field value. There is no
* type tagging; field representations are simply concatenated together.
*
* A psuedo-BNF grammar for the credential and principal formats is:
*
* credential ::=
* client (principal)
* server (principal)
* keyblock (keyblock)
* authtime (32 bits)
* starttime (32 bits)
* endtime (32 bits)
* renew_till (32 bits)
* is_skey (1 byte, 0 or 1)
* ticket_flags (32 bits)
* addresses (addresses)
* authdata (authdata)
* ticket (data)
* second_ticket (data)
*
* principal ::=
* name type (32 bits) [omitted in version 1]
* count of components (32 bits) [includes realm in version 1]
* realm (data)
* component1 (data)
* component2 (data)
* ...
*
* keyblock ::=
* enctype (16 bits) [repeated twice in version 3; see below]
* data
*
* addresses ::=
* count (32 bits)
* address1
* address2
* ...
*
* address ::=
* addrtype (16 bits)
* data
*
* authdata ::=
* count (32 bits)
* authdata1
* authdata2
* ...
*
* authdata ::=
* ad_type (16 bits)
* data
*
* data ::=
* length (32 bits)
* value (length bytes)
*
* When version 3 was current (before release 1.0), the keyblock had separate
* key type and enctype fields, and both were recorded. At present we record
* the enctype field twice when writing the version 3 format and ignore the
* second value when reading it.
*/
#include "k5-input.h"
#include "cc-int.h"
/* Read a 16-bit integer in host byte order for versions 1 and 2, or in
* big-endian byte order for later versions.*/
static uint16_t
get16(struct k5input *in, int version)
{
return (version < 3) ? k5_input_get_uint16_n(in) :
k5_input_get_uint16_be(in);
}
/* Read a 32-bit integer in host byte order for versions 1 and 2, or in
* big-endian byte order for later versions.*/
static uint32_t
get32(struct k5input *in, int version)
{
return (version < 3) ? k5_input_get_uint32_n(in) :
k5_input_get_uint32_be(in);
}
/* Read a 32-bit length and make a copy of that many bytes. Return NULL on
* error. */
static void *
get_len_bytes(struct k5input *in, int version, unsigned int *len_out)
{
krb5_error_code ret;
unsigned int len = get32(in, version);
const void *bytes = k5_input_get_bytes(in, len);
void *copy;
*len_out = 0;
if (bytes == NULL)
return NULL;
copy = k5memdup0(bytes, len, &ret);
if (copy == NULL) {
k5_input_set_status(in, ret);
return NULL;
}
*len_out = len;
return copy;
}
/* Like get_len_bytes, but put the result in data. */
static void
get_data(struct k5input *in, int version, krb5_data *data)
{
unsigned int len;
void *bytes = get_len_bytes(in, version, &len);
*data = (bytes == NULL) ? empty_data() : make_data(bytes, len);
}
static krb5_principal
unmarshal_princ(struct k5input *in, int version)
{
krb5_error_code ret;
krb5_principal princ;
uint32_t i, ncomps;
princ = k5alloc(sizeof(krb5_principal_data), &ret);
if (princ == NULL) {
k5_input_set_status(in, ret);
return NULL;
}
princ->magic = KV5M_PRINCIPAL;
/* Version 1 does not store the principal name type, and counts the realm
* in the number of components. */
princ->type = (version == 1) ? KRB5_NT_UNKNOWN : get32(in, version);
ncomps = get32(in, version);
if (version == 1)
ncomps--;
if (ncomps > in->len) { /* Sanity check to avoid large allocations */
ret = EINVAL;
goto error;
}
if (ncomps != 0) {
princ->data = k5calloc(ncomps, sizeof(krb5_data), &ret);
if (princ->data == NULL)
goto error;
princ->length = ncomps;
}
get_data(in, version, &princ->realm);
for (i = 0; i < ncomps; i++)
get_data(in, version, &princ->data[i]);
return princ;
error:
k5_input_set_status(in, ret);
krb5_free_principal(NULL, princ);
return NULL;
}
static void
unmarshal_keyblock(struct k5input *in, int version, krb5_keyblock *kb)
{
memset(kb, 0, sizeof(*kb));
kb->magic = KV5M_KEYBLOCK;
kb->enctype = get16(in, version);
/* Version 3 stores the enctype twice. */
if (version == 3)
(void)get16(in, version);
kb->contents = get_len_bytes(in, version, &kb->length);
}
static krb5_address *
unmarshal_addr(struct k5input *in, int version)
{
krb5_address *addr;
addr = calloc(1, sizeof(*addr));
if (addr == NULL) {
k5_input_set_status(in, ENOMEM);
return NULL;
}
addr->magic = KV5M_ADDRESS;
addr->addrtype = get16(in, version);
addr->contents = get_len_bytes(in, version, &addr->length);
return addr;
}
static krb5_address **
unmarshal_addrs(struct k5input *in, int version)
{
krb5_address **addrs;
size_t i, count;
count = get32(in, version);
if (count > in->len) { /* Sanity check to avoid large allocations */
k5_input_set_status(in, EINVAL);
return NULL;
}
addrs = calloc(count + 1, sizeof(*addrs));
if (addrs == NULL) {
k5_input_set_status(in, ENOMEM);
return NULL;
}
for (i = 0; i < count; i++)
addrs[i] = unmarshal_addr(in, version);
return addrs;
}
static krb5_authdata *
unmarshal_authdatum(struct k5input *in, int version)
{
krb5_authdata *ad;
ad = calloc(1, sizeof(*ad));
if (ad == NULL) {
k5_input_set_status(in, ENOMEM);
return NULL;
}
ad->magic = KV5M_ADDRESS;
/* Authdata types can be negative, so sign-extend the get16 result. */
ad->ad_type = (int16_t)get16(in, version);
ad->contents = get_len_bytes(in, version, &ad->length);
return ad;
}
static krb5_authdata **
unmarshal_authdata(struct k5input *in, int version)
{
krb5_authdata **authdata;
size_t i, count;
count = get32(in, version);
if (count > in->len) { /* Sanity check to avoid large allocations */
k5_input_set_status(in, EINVAL);
return NULL;
}
authdata = calloc(count + 1, sizeof(*authdata));
if (authdata == NULL) {
k5_input_set_status(in, ENOMEM);
return NULL;
}
for (i = 0; i < count; i++)
authdata[i] = unmarshal_authdatum(in, version);
return authdata;
}
/* Unmarshal a credential using the specified file ccache version (expressed as
* an integer from 1 to 4). Does not check for trailing garbage. */
krb5_error_code
k5_unmarshal_cred(const unsigned char *data, size_t len, int version,
krb5_creds *creds)
{
struct k5input in;
k5_input_init(&in, data, len);
creds->client = unmarshal_princ(&in, version);
creds->server = unmarshal_princ(&in, version);
unmarshal_keyblock(&in, version, &creds->keyblock);
creds->times.authtime = get32(&in, version);
creds->times.starttime = get32(&in, version);
creds->times.endtime = get32(&in, version);
creds->times.renew_till = get32(&in, version);
creds->is_skey = k5_input_get_byte(&in);
creds->ticket_flags = get32(&in, version);
creds->addresses = unmarshal_addrs(&in, version);
creds->authdata = unmarshal_authdata(&in, version);
get_data(&in, version, &creds->ticket);
get_data(&in, version, &creds->second_ticket);
if (in.status) {
krb5_free_cred_contents(NULL, creds);
memset(creds, 0, sizeof(*creds));
}
return (in.status == EINVAL) ? KRB5_CC_FORMAT : in.status;
}
/* Unmarshal a principal using the specified file ccache version (expressed as
* an integer from 1 to 4). Does not check for trailing garbage. */
krb5_error_code
k5_unmarshal_princ(const unsigned char *data, size_t len, int version,
krb5_principal *princ_out)
{
struct k5input in;
krb5_principal princ;
*princ_out = NULL;
k5_input_init(&in, data, len);
princ = unmarshal_princ(&in, version);
if (in.status)
krb5_free_principal(NULL, princ);
else
*princ_out = princ;
return (in.status == EINVAL) ? KRB5_CC_FORMAT : in.status;
}
/* Store a 16-bit integer in host byte order for versions 1 and 2, or in
* big-endian byte order for later versions.*/
static void
put16(struct k5buf *buf, int version, uint16_t num)
{
char n[2];
if (version < 3)
store_16_n(num, n);
else
store_16_be(num, n);
k5_buf_add_len(buf, n, 2);
}
/* Store a 32-bit integer in host byte order for versions 1 and 2, or in
* big-endian byte order for later versions.*/
static void
put32(struct k5buf *buf, int version, uint32_t num)
{
char n[4];
if (version < 3)
store_32_n(num, n);
else
store_32_be(num, n);
k5_buf_add_len(buf, n, 4);
}
static void
put_len_bytes(struct k5buf *buf, int version, const void *bytes,
unsigned int len)
{
put32(buf, version, len);
k5_buf_add_len(buf, bytes, len);
}
static void
put_data(struct k5buf *buf, int version, krb5_data *data)
{
put_len_bytes(buf, version, data->data, data->length);
}
static void
marshal_princ(struct k5buf *buf, int version, krb5_principal princ)
{
int32_t i, ncomps;
/* Version 1 does not store the principal name type, and counts the realm
* in the number of components. */
if (version != 1)
put32(buf, version, princ->type);
ncomps = princ->length + ((version == 1) ? 1 : 0);
put32(buf, version, ncomps);
put_data(buf, version, &princ->realm);
for (i = 0; i < princ->length; i++)
put_data(buf, version, &princ->data[i]);
}
static void
marshal_keyblock(struct k5buf *buf, int version, krb5_keyblock *kb)
{
put16(buf, version, kb->enctype);
/* Version 3 stores the enctype twice. */
if (version == 3)
put16(buf, version, kb->enctype);
put_len_bytes(buf, version, kb->contents, kb->length);
}
static void
marshal_addrs(struct k5buf *buf, int version, krb5_address **addrs)
{
size_t i, count;
for (count = 0; addrs != NULL && addrs[count] != NULL; count++);
put32(buf, version, count);
for (i = 0; i < count; i++) {
put16(buf, version, addrs[i]->addrtype);
put_len_bytes(buf, version, addrs[i]->contents, addrs[i]->length);
}
}
static void
marshal_authdata(struct k5buf *buf, int version, krb5_authdata **authdata)
{
size_t i, count;
for (count = 0; authdata != NULL && authdata[count] != NULL; count++);
put32(buf, version, count);
for (i = 0; i < count; i++) {
put16(buf, version, authdata[i]->ad_type);
put_len_bytes(buf, version, authdata[i]->contents,
authdata[i]->length);
}
}
/* Marshal a credential using the specified file ccache version (expressed as
* an integer from 1 to 4). */
krb5_error_code
k5_marshal_cred(krb5_creds *creds, int version, unsigned char **bytes_out,
size_t *len_out)
{
struct k5buf buf;
char is_skey;
*bytes_out = NULL;
*len_out = 0;
k5_buf_init_dynamic(&buf);
marshal_princ(&buf, version, creds->client);
marshal_princ(&buf, version, creds->server);
marshal_keyblock(&buf, version, &creds->keyblock);
put32(&buf, version, creds->times.authtime);
put32(&buf, version, creds->times.starttime);
put32(&buf, version, creds->times.endtime);
put32(&buf, version, creds->times.renew_till);
is_skey = creds->is_skey;
k5_buf_add_len(&buf, &is_skey, 1);
put32(&buf, version, creds->ticket_flags);
marshal_addrs(&buf, version, creds->addresses);
marshal_authdata(&buf, version, creds->authdata);
put_data(&buf, version, &creds->ticket);
put_data(&buf, version, &creds->second_ticket);
if (k5_buf_data(&buf) == NULL)
return ENOMEM;
*bytes_out = (unsigned char *)k5_buf_data(&buf);
*len_out = k5_buf_len(&buf);
return 0;
}
/* Marshal a principal using the specified file ccache version (expressed as an
* integer from 1 to 4). */
krb5_error_code
k5_marshal_princ(krb5_principal princ, int version, unsigned char **bytes_out,
size_t *len_out)
{
struct k5buf buf;
*bytes_out = NULL;
*len_out = 0;
k5_buf_init_dynamic(&buf);
marshal_princ(&buf, version, princ);
if (k5_buf_data(&buf) == NULL)
return ENOMEM;
*bytes_out = (unsigned char *)k5_buf_data(&buf);
*len_out = k5_buf_len(&buf);
return 0;
}
|