1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/kdb/db2/kdb_xdr.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright 1995 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All Rights Reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert * require a specific license from the United States Government.
9*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
24*7f2fe78bSCy Schubert * or implied warranty.
25*7f2fe78bSCy Schubert */
26*7f2fe78bSCy Schubert
27*7f2fe78bSCy Schubert #include "k5-int.h"
28*7f2fe78bSCy Schubert #include <string.h>
29*7f2fe78bSCy Schubert #include <stdio.h>
30*7f2fe78bSCy Schubert #include <errno.h>
31*7f2fe78bSCy Schubert #include "kdb_xdr.h"
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert krb5_error_code
krb5_encode_princ_dbkey(krb5_context context,krb5_data * key,krb5_const_principal principal)34*7f2fe78bSCy Schubert krb5_encode_princ_dbkey(krb5_context context, krb5_data *key,
35*7f2fe78bSCy Schubert krb5_const_principal principal)
36*7f2fe78bSCy Schubert {
37*7f2fe78bSCy Schubert char *princ_name;
38*7f2fe78bSCy Schubert krb5_error_code retval;
39*7f2fe78bSCy Schubert
40*7f2fe78bSCy Schubert if (!(retval = krb5_unparse_name(context, principal, &princ_name))) {
41*7f2fe78bSCy Schubert /* need to store the NULL for decoding */
42*7f2fe78bSCy Schubert key->length = strlen(princ_name)+1;
43*7f2fe78bSCy Schubert key->data = princ_name;
44*7f2fe78bSCy Schubert }
45*7f2fe78bSCy Schubert return(retval);
46*7f2fe78bSCy Schubert }
47*7f2fe78bSCy Schubert
48*7f2fe78bSCy Schubert krb5_error_code
krb5_encode_princ_entry(krb5_context context,krb5_data * content,krb5_db_entry * entry)49*7f2fe78bSCy Schubert krb5_encode_princ_entry(krb5_context context, krb5_data *content,
50*7f2fe78bSCy Schubert krb5_db_entry *entry)
51*7f2fe78bSCy Schubert {
52*7f2fe78bSCy Schubert int i, j;
53*7f2fe78bSCy Schubert unsigned int unparse_princ_size;
54*7f2fe78bSCy Schubert char * unparse_princ;
55*7f2fe78bSCy Schubert unsigned char * nextloc;
56*7f2fe78bSCy Schubert krb5_tl_data * tl_data;
57*7f2fe78bSCy Schubert krb5_error_code retval;
58*7f2fe78bSCy Schubert krb5_int16 psize16;
59*7f2fe78bSCy Schubert
60*7f2fe78bSCy Schubert /*
61*7f2fe78bSCy Schubert * Generate one lump of data from the krb5_db_entry.
62*7f2fe78bSCy Schubert * This data must be independent of byte order of the machine,
63*7f2fe78bSCy Schubert * compact and extensible.
64*7f2fe78bSCy Schubert */
65*7f2fe78bSCy Schubert
66*7f2fe78bSCy Schubert /*
67*7f2fe78bSCy Schubert * First allocate enough space for all the data.
68*7f2fe78bSCy Schubert * Need 2 bytes for the length of the base structure
69*7f2fe78bSCy Schubert * then 36 [ 8 * 4 + 2 * 2] bytes for the base information
70*7f2fe78bSCy Schubert * [ attributes, max_life, max_renewable_life, expiration,
71*7f2fe78bSCy Schubert * pw_expiration, last_success, last_failed, fail_auth_count ]
72*7f2fe78bSCy Schubert * [ n_key_data, n_tl_data ]
73*7f2fe78bSCy Schubert * then XX bytes [ e_length ] for the extra data [ e_data ]
74*7f2fe78bSCy Schubert * then XX bytes [ 2 for length + length for string ] for the principal,
75*7f2fe78bSCy Schubert * then (4 [type + length] + tl_data_length) bytes per tl_data
76*7f2fe78bSCy Schubert * then (4 + (4 + key_data_length) per key_data_contents) bytes per key_data
77*7f2fe78bSCy Schubert */
78*7f2fe78bSCy Schubert content->length = entry->len + entry->e_length;
79*7f2fe78bSCy Schubert
80*7f2fe78bSCy Schubert if ((retval = krb5_unparse_name(context, entry->princ, &unparse_princ)))
81*7f2fe78bSCy Schubert return(retval);
82*7f2fe78bSCy Schubert
83*7f2fe78bSCy Schubert unparse_princ_size = strlen(unparse_princ) + 1;
84*7f2fe78bSCy Schubert content->length += unparse_princ_size;
85*7f2fe78bSCy Schubert content->length += 2;
86*7f2fe78bSCy Schubert
87*7f2fe78bSCy Schubert i = 0;
88*7f2fe78bSCy Schubert /* tl_data is a linked list */
89*7f2fe78bSCy Schubert for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
90*7f2fe78bSCy Schubert content->length += tl_data->tl_data_length;
91*7f2fe78bSCy Schubert content->length += 4; /* type, length */
92*7f2fe78bSCy Schubert i++;
93*7f2fe78bSCy Schubert }
94*7f2fe78bSCy Schubert
95*7f2fe78bSCy Schubert if (i != entry->n_tl_data) {
96*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
97*7f2fe78bSCy Schubert goto epc_error;
98*7f2fe78bSCy Schubert }
99*7f2fe78bSCy Schubert
100*7f2fe78bSCy Schubert /* key_data is an array */
101*7f2fe78bSCy Schubert for (i = 0; i < entry->n_key_data; i++) {
102*7f2fe78bSCy Schubert content->length += 4; /* Version, KVNO */
103*7f2fe78bSCy Schubert for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
104*7f2fe78bSCy Schubert content->length += entry->key_data[i].key_data_length[j];
105*7f2fe78bSCy Schubert content->length += 4; /* type + length */
106*7f2fe78bSCy Schubert }
107*7f2fe78bSCy Schubert }
108*7f2fe78bSCy Schubert
109*7f2fe78bSCy Schubert if ((content->data = malloc(content->length)) == NULL) {
110*7f2fe78bSCy Schubert retval = ENOMEM;
111*7f2fe78bSCy Schubert goto epc_error;
112*7f2fe78bSCy Schubert }
113*7f2fe78bSCy Schubert
114*7f2fe78bSCy Schubert /*
115*7f2fe78bSCy Schubert * Now we go through entry again, this time copying data
116*7f2fe78bSCy Schubert * These first entries are always saved regardless of version
117*7f2fe78bSCy Schubert */
118*7f2fe78bSCy Schubert nextloc = (unsigned char *)content->data;
119*7f2fe78bSCy Schubert
120*7f2fe78bSCy Schubert /* Base Length */
121*7f2fe78bSCy Schubert krb5_kdb_encode_int16(entry->len, nextloc);
122*7f2fe78bSCy Schubert nextloc += 2;
123*7f2fe78bSCy Schubert
124*7f2fe78bSCy Schubert /* Attributes */
125*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->attributes, nextloc);
126*7f2fe78bSCy Schubert nextloc += 4;
127*7f2fe78bSCy Schubert
128*7f2fe78bSCy Schubert /* Max Life */
129*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->max_life, nextloc);
130*7f2fe78bSCy Schubert nextloc += 4;
131*7f2fe78bSCy Schubert
132*7f2fe78bSCy Schubert /* Max Renewable Life */
133*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->max_renewable_life, nextloc);
134*7f2fe78bSCy Schubert nextloc += 4;
135*7f2fe78bSCy Schubert
136*7f2fe78bSCy Schubert /* When the client expires */
137*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->expiration, nextloc);
138*7f2fe78bSCy Schubert nextloc += 4;
139*7f2fe78bSCy Schubert
140*7f2fe78bSCy Schubert /* When its passwd expires */
141*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->pw_expiration, nextloc);
142*7f2fe78bSCy Schubert nextloc += 4;
143*7f2fe78bSCy Schubert
144*7f2fe78bSCy Schubert /* Last successful passwd */
145*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->last_success, nextloc);
146*7f2fe78bSCy Schubert nextloc += 4;
147*7f2fe78bSCy Schubert
148*7f2fe78bSCy Schubert /* Last failed passwd attempt */
149*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->last_failed, nextloc);
150*7f2fe78bSCy Schubert nextloc += 4;
151*7f2fe78bSCy Schubert
152*7f2fe78bSCy Schubert /* # of failed passwd attempt */
153*7f2fe78bSCy Schubert krb5_kdb_encode_int32(entry->fail_auth_count, nextloc);
154*7f2fe78bSCy Schubert nextloc += 4;
155*7f2fe78bSCy Schubert
156*7f2fe78bSCy Schubert /* # tl_data structures */
157*7f2fe78bSCy Schubert krb5_kdb_encode_int16(entry->n_tl_data, nextloc);
158*7f2fe78bSCy Schubert nextloc += 2;
159*7f2fe78bSCy Schubert
160*7f2fe78bSCy Schubert /* # key_data structures */
161*7f2fe78bSCy Schubert krb5_kdb_encode_int16(entry->n_key_data, nextloc);
162*7f2fe78bSCy Schubert nextloc += 2;
163*7f2fe78bSCy Schubert
164*7f2fe78bSCy Schubert /* Put extended fields here */
165*7f2fe78bSCy Schubert if (entry->len != KRB5_KDB_V1_BASE_LENGTH)
166*7f2fe78bSCy Schubert abort();
167*7f2fe78bSCy Schubert
168*7f2fe78bSCy Schubert /* Any extra data that this version doesn't understand. */
169*7f2fe78bSCy Schubert if (entry->e_length) {
170*7f2fe78bSCy Schubert memcpy(nextloc, entry->e_data, entry->e_length);
171*7f2fe78bSCy Schubert nextloc += entry->e_length;
172*7f2fe78bSCy Schubert }
173*7f2fe78bSCy Schubert
174*7f2fe78bSCy Schubert /*
175*7f2fe78bSCy Schubert * Now we get to the principal.
176*7f2fe78bSCy Schubert * To squeze a few extra bytes out it is always assumed to come
177*7f2fe78bSCy Schubert * after the base type.
178*7f2fe78bSCy Schubert */
179*7f2fe78bSCy Schubert psize16 = (krb5_int16) unparse_princ_size;
180*7f2fe78bSCy Schubert krb5_kdb_encode_int16(psize16, nextloc);
181*7f2fe78bSCy Schubert nextloc += 2;
182*7f2fe78bSCy Schubert (void) memcpy(nextloc, unparse_princ, unparse_princ_size);
183*7f2fe78bSCy Schubert nextloc += unparse_princ_size;
184*7f2fe78bSCy Schubert
185*7f2fe78bSCy Schubert /* tl_data is a linked list, of type, length, contents */
186*7f2fe78bSCy Schubert for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
187*7f2fe78bSCy Schubert krb5_kdb_encode_int16(tl_data->tl_data_type, nextloc);
188*7f2fe78bSCy Schubert nextloc += 2;
189*7f2fe78bSCy Schubert krb5_kdb_encode_int16(tl_data->tl_data_length, nextloc);
190*7f2fe78bSCy Schubert nextloc += 2;
191*7f2fe78bSCy Schubert
192*7f2fe78bSCy Schubert memcpy(nextloc, tl_data->tl_data_contents, tl_data->tl_data_length);
193*7f2fe78bSCy Schubert nextloc += tl_data->tl_data_length;
194*7f2fe78bSCy Schubert }
195*7f2fe78bSCy Schubert
196*7f2fe78bSCy Schubert /* key_data is an array */
197*7f2fe78bSCy Schubert for (i = 0; i < entry->n_key_data; i++) {
198*7f2fe78bSCy Schubert krb5_kdb_encode_int16(entry->key_data[i].key_data_ver, nextloc);
199*7f2fe78bSCy Schubert nextloc += 2;
200*7f2fe78bSCy Schubert krb5_kdb_encode_int16(entry->key_data[i].key_data_kvno, nextloc);
201*7f2fe78bSCy Schubert nextloc += 2;
202*7f2fe78bSCy Schubert
203*7f2fe78bSCy Schubert for (j = 0; j < entry->key_data[i].key_data_ver; j++) {
204*7f2fe78bSCy Schubert krb5_int16 type = entry->key_data[i].key_data_type[j];
205*7f2fe78bSCy Schubert krb5_ui_2 length = entry->key_data[i].key_data_length[j];
206*7f2fe78bSCy Schubert
207*7f2fe78bSCy Schubert krb5_kdb_encode_int16(type, nextloc);
208*7f2fe78bSCy Schubert nextloc += 2;
209*7f2fe78bSCy Schubert krb5_kdb_encode_int16(length, nextloc);
210*7f2fe78bSCy Schubert nextloc += 2;
211*7f2fe78bSCy Schubert
212*7f2fe78bSCy Schubert if (length) {
213*7f2fe78bSCy Schubert memcpy(nextloc, entry->key_data[i].key_data_contents[j],length);
214*7f2fe78bSCy Schubert nextloc += length;
215*7f2fe78bSCy Schubert }
216*7f2fe78bSCy Schubert }
217*7f2fe78bSCy Schubert }
218*7f2fe78bSCy Schubert
219*7f2fe78bSCy Schubert epc_error:;
220*7f2fe78bSCy Schubert free(unparse_princ);
221*7f2fe78bSCy Schubert return retval;
222*7f2fe78bSCy Schubert }
223*7f2fe78bSCy Schubert
224*7f2fe78bSCy Schubert krb5_error_code
krb5_decode_princ_entry(krb5_context context,krb5_data * content,krb5_db_entry ** entry_ptr)225*7f2fe78bSCy Schubert krb5_decode_princ_entry(krb5_context context, krb5_data *content,
226*7f2fe78bSCy Schubert krb5_db_entry **entry_ptr)
227*7f2fe78bSCy Schubert {
228*7f2fe78bSCy Schubert int sizeleft, i;
229*7f2fe78bSCy Schubert unsigned char * nextloc;
230*7f2fe78bSCy Schubert krb5_tl_data ** tl_data;
231*7f2fe78bSCy Schubert krb5_int16 i16;
232*7f2fe78bSCy Schubert krb5_db_entry * entry;
233*7f2fe78bSCy Schubert krb5_error_code retval;
234*7f2fe78bSCy Schubert
235*7f2fe78bSCy Schubert *entry_ptr = NULL;
236*7f2fe78bSCy Schubert
237*7f2fe78bSCy Schubert entry = k5alloc(sizeof(*entry), &retval);
238*7f2fe78bSCy Schubert if (entry == NULL)
239*7f2fe78bSCy Schubert return retval;
240*7f2fe78bSCy Schubert
241*7f2fe78bSCy Schubert /*
242*7f2fe78bSCy Schubert * Reverse the encoding of encode_princ_entry.
243*7f2fe78bSCy Schubert *
244*7f2fe78bSCy Schubert * The first part is decoding the base type. If the base type is
245*7f2fe78bSCy Schubert * bigger than the original base type then the additional fields
246*7f2fe78bSCy Schubert * need to be filled in. If the base type is larger than any
247*7f2fe78bSCy Schubert * known base type the additional data goes in e_data.
248*7f2fe78bSCy Schubert */
249*7f2fe78bSCy Schubert
250*7f2fe78bSCy Schubert /* First do the easy stuff */
251*7f2fe78bSCy Schubert nextloc = (unsigned char *)content->data;
252*7f2fe78bSCy Schubert sizeleft = content->length;
253*7f2fe78bSCy Schubert if (sizeleft < KRB5_KDB_V1_BASE_LENGTH) {
254*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
255*7f2fe78bSCy Schubert goto error_out;
256*7f2fe78bSCy Schubert }
257*7f2fe78bSCy Schubert sizeleft -= KRB5_KDB_V1_BASE_LENGTH;
258*7f2fe78bSCy Schubert
259*7f2fe78bSCy Schubert /* Base Length */
260*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, entry->len);
261*7f2fe78bSCy Schubert nextloc += 2;
262*7f2fe78bSCy Schubert
263*7f2fe78bSCy Schubert /* Attributes */
264*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->attributes);
265*7f2fe78bSCy Schubert nextloc += 4;
266*7f2fe78bSCy Schubert
267*7f2fe78bSCy Schubert /* Max Life */
268*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->max_life);
269*7f2fe78bSCy Schubert nextloc += 4;
270*7f2fe78bSCy Schubert
271*7f2fe78bSCy Schubert /* Max Renewable Life */
272*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->max_renewable_life);
273*7f2fe78bSCy Schubert nextloc += 4;
274*7f2fe78bSCy Schubert
275*7f2fe78bSCy Schubert /* When the client expires */
276*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->expiration);
277*7f2fe78bSCy Schubert nextloc += 4;
278*7f2fe78bSCy Schubert
279*7f2fe78bSCy Schubert /* When its passwd expires */
280*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->pw_expiration);
281*7f2fe78bSCy Schubert nextloc += 4;
282*7f2fe78bSCy Schubert
283*7f2fe78bSCy Schubert /* Last successful passwd */
284*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->last_success);
285*7f2fe78bSCy Schubert nextloc += 4;
286*7f2fe78bSCy Schubert
287*7f2fe78bSCy Schubert /* Last failed passwd attempt */
288*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->last_failed);
289*7f2fe78bSCy Schubert nextloc += 4;
290*7f2fe78bSCy Schubert
291*7f2fe78bSCy Schubert /* # of failed passwd attempt */
292*7f2fe78bSCy Schubert krb5_kdb_decode_int32(nextloc, entry->fail_auth_count);
293*7f2fe78bSCy Schubert nextloc += 4;
294*7f2fe78bSCy Schubert
295*7f2fe78bSCy Schubert /* # tl_data structures */
296*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, entry->n_tl_data);
297*7f2fe78bSCy Schubert nextloc += 2;
298*7f2fe78bSCy Schubert
299*7f2fe78bSCy Schubert if (entry->n_tl_data < 0) {
300*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
301*7f2fe78bSCy Schubert goto error_out;
302*7f2fe78bSCy Schubert }
303*7f2fe78bSCy Schubert
304*7f2fe78bSCy Schubert /* # key_data structures */
305*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, entry->n_key_data);
306*7f2fe78bSCy Schubert nextloc += 2;
307*7f2fe78bSCy Schubert
308*7f2fe78bSCy Schubert if (entry->n_key_data < 0) {
309*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
310*7f2fe78bSCy Schubert goto error_out;
311*7f2fe78bSCy Schubert }
312*7f2fe78bSCy Schubert
313*7f2fe78bSCy Schubert /* Check for extra data */
314*7f2fe78bSCy Schubert if (entry->len > KRB5_KDB_V1_BASE_LENGTH) {
315*7f2fe78bSCy Schubert entry->e_length = entry->len - KRB5_KDB_V1_BASE_LENGTH;
316*7f2fe78bSCy Schubert entry->e_data = k5memdup(nextloc, entry->e_length, &retval);
317*7f2fe78bSCy Schubert if (entry->e_data == NULL)
318*7f2fe78bSCy Schubert goto error_out;
319*7f2fe78bSCy Schubert nextloc += entry->e_length;
320*7f2fe78bSCy Schubert }
321*7f2fe78bSCy Schubert
322*7f2fe78bSCy Schubert /*
323*7f2fe78bSCy Schubert * Get the principal name for the entry
324*7f2fe78bSCy Schubert * (stored as a string which gets unparsed.)
325*7f2fe78bSCy Schubert */
326*7f2fe78bSCy Schubert if (sizeleft < 2) {
327*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
328*7f2fe78bSCy Schubert goto error_out;
329*7f2fe78bSCy Schubert }
330*7f2fe78bSCy Schubert sizeleft -= 2;
331*7f2fe78bSCy Schubert
332*7f2fe78bSCy Schubert i = 0;
333*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, i16);
334*7f2fe78bSCy Schubert i = (int) i16;
335*7f2fe78bSCy Schubert nextloc += 2;
336*7f2fe78bSCy Schubert if (i <= 0 || i > sizeleft || nextloc[i - 1] != '\0' ||
337*7f2fe78bSCy Schubert memchr((char *)nextloc, '\0', i - 1) != NULL) {
338*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
339*7f2fe78bSCy Schubert goto error_out;
340*7f2fe78bSCy Schubert }
341*7f2fe78bSCy Schubert
342*7f2fe78bSCy Schubert if ((retval = krb5_parse_name(context, (char *)nextloc, &(entry->princ))))
343*7f2fe78bSCy Schubert goto error_out;
344*7f2fe78bSCy Schubert sizeleft -= i;
345*7f2fe78bSCy Schubert nextloc += i;
346*7f2fe78bSCy Schubert
347*7f2fe78bSCy Schubert /* tl_data is a linked list */
348*7f2fe78bSCy Schubert tl_data = &entry->tl_data;
349*7f2fe78bSCy Schubert for (i = 0; i < entry->n_tl_data; i++) {
350*7f2fe78bSCy Schubert if (sizeleft < 4) {
351*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
352*7f2fe78bSCy Schubert goto error_out;
353*7f2fe78bSCy Schubert }
354*7f2fe78bSCy Schubert sizeleft -= 4;
355*7f2fe78bSCy Schubert if ((*tl_data = (krb5_tl_data *)
356*7f2fe78bSCy Schubert malloc(sizeof(krb5_tl_data))) == NULL) {
357*7f2fe78bSCy Schubert retval = ENOMEM;
358*7f2fe78bSCy Schubert goto error_out;
359*7f2fe78bSCy Schubert }
360*7f2fe78bSCy Schubert (*tl_data)->tl_data_next = NULL;
361*7f2fe78bSCy Schubert (*tl_data)->tl_data_contents = NULL;
362*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_type);
363*7f2fe78bSCy Schubert nextloc += 2;
364*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, (*tl_data)->tl_data_length);
365*7f2fe78bSCy Schubert nextloc += 2;
366*7f2fe78bSCy Schubert
367*7f2fe78bSCy Schubert if ((*tl_data)->tl_data_length > sizeleft) {
368*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
369*7f2fe78bSCy Schubert goto error_out;
370*7f2fe78bSCy Schubert }
371*7f2fe78bSCy Schubert sizeleft -= (*tl_data)->tl_data_length;
372*7f2fe78bSCy Schubert (*tl_data)->tl_data_contents =
373*7f2fe78bSCy Schubert k5memdup(nextloc, (*tl_data)->tl_data_length, &retval);
374*7f2fe78bSCy Schubert if ((*tl_data)->tl_data_contents == NULL)
375*7f2fe78bSCy Schubert goto error_out;
376*7f2fe78bSCy Schubert nextloc += (*tl_data)->tl_data_length;
377*7f2fe78bSCy Schubert tl_data = &((*tl_data)->tl_data_next);
378*7f2fe78bSCy Schubert }
379*7f2fe78bSCy Schubert
380*7f2fe78bSCy Schubert /* key_data is an array */
381*7f2fe78bSCy Schubert if (entry->n_key_data && ((entry->key_data = (krb5_key_data *)
382*7f2fe78bSCy Schubert malloc(sizeof(krb5_key_data) * entry->n_key_data)) == NULL)) {
383*7f2fe78bSCy Schubert retval = ENOMEM;
384*7f2fe78bSCy Schubert goto error_out;
385*7f2fe78bSCy Schubert }
386*7f2fe78bSCy Schubert for (i = 0; i < entry->n_key_data; i++) {
387*7f2fe78bSCy Schubert krb5_key_data * key_data;
388*7f2fe78bSCy Schubert int j;
389*7f2fe78bSCy Schubert
390*7f2fe78bSCy Schubert if (sizeleft < 4) {
391*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
392*7f2fe78bSCy Schubert goto error_out;
393*7f2fe78bSCy Schubert }
394*7f2fe78bSCy Schubert sizeleft -= 4;
395*7f2fe78bSCy Schubert key_data = entry->key_data + i;
396*7f2fe78bSCy Schubert memset(key_data, 0, sizeof(krb5_key_data));
397*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, key_data->key_data_ver);
398*7f2fe78bSCy Schubert nextloc += 2;
399*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, key_data->key_data_kvno);
400*7f2fe78bSCy Schubert nextloc += 2;
401*7f2fe78bSCy Schubert
402*7f2fe78bSCy Schubert /* key_data_ver determines number of elements and how to unparse
403*7f2fe78bSCy Schubert * them. */
404*7f2fe78bSCy Schubert if (key_data->key_data_ver >= 0 &&
405*7f2fe78bSCy Schubert key_data->key_data_ver <= KRB5_KDB_V1_KEY_DATA_ARRAY) {
406*7f2fe78bSCy Schubert for (j = 0; j < key_data->key_data_ver; j++) {
407*7f2fe78bSCy Schubert if (sizeleft < 4) {
408*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
409*7f2fe78bSCy Schubert goto error_out;
410*7f2fe78bSCy Schubert }
411*7f2fe78bSCy Schubert sizeleft -= 4;
412*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, key_data->key_data_type[j]);
413*7f2fe78bSCy Schubert nextloc += 2;
414*7f2fe78bSCy Schubert krb5_kdb_decode_int16(nextloc, key_data->key_data_length[j]);
415*7f2fe78bSCy Schubert nextloc += 2;
416*7f2fe78bSCy Schubert
417*7f2fe78bSCy Schubert if (key_data->key_data_length[j] > sizeleft) {
418*7f2fe78bSCy Schubert retval = KRB5_KDB_TRUNCATED_RECORD;
419*7f2fe78bSCy Schubert goto error_out;
420*7f2fe78bSCy Schubert }
421*7f2fe78bSCy Schubert sizeleft -= key_data->key_data_length[j];
422*7f2fe78bSCy Schubert if (key_data->key_data_length[j]) {
423*7f2fe78bSCy Schubert key_data->key_data_contents[j] =
424*7f2fe78bSCy Schubert k5memdup(nextloc, key_data->key_data_length[j],
425*7f2fe78bSCy Schubert &retval);
426*7f2fe78bSCy Schubert if (key_data->key_data_contents[j] == NULL)
427*7f2fe78bSCy Schubert goto error_out;
428*7f2fe78bSCy Schubert nextloc += key_data->key_data_length[j];
429*7f2fe78bSCy Schubert }
430*7f2fe78bSCy Schubert }
431*7f2fe78bSCy Schubert } else {
432*7f2fe78bSCy Schubert retval = KRB5_KDB_BAD_VERSION;
433*7f2fe78bSCy Schubert goto error_out;
434*7f2fe78bSCy Schubert }
435*7f2fe78bSCy Schubert }
436*7f2fe78bSCy Schubert *entry_ptr = entry;
437*7f2fe78bSCy Schubert return 0;
438*7f2fe78bSCy Schubert
439*7f2fe78bSCy Schubert error_out:
440*7f2fe78bSCy Schubert krb5_db_free_principal(context, entry);
441*7f2fe78bSCy Schubert return retval;
442*7f2fe78bSCy Schubert }
443