1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/t_ser.c - Test serialization */
4 * Copyright 1995, 2019 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
33 static const char stuff[]="You can't take a pointer to a function and convert \
34 it to a pointer to char; ANSI doesn't say it'll work, and in fact on the HPPA \
35 you can lose some bits of the function pointer, and get a pointer that you \
36 can't safely dereference. This test file used to make this mistake, often.";
39 check(krb5_error_code code)
42 com_err("t_ser", code, NULL);
50 void *ptr = calloc(1, size);
58 ser_context(krb5_context ctx)
60 uint8_t *erep, *erep2, *bp;
61 size_t elen = 0, elen2 = 0, blen;
64 check(k5_size_context(ctx, &elen));
69 check(k5_externalize_context(ctx, &bp, &blen));
70 assert(bp == erep + elen && blen == 0);
74 check(k5_internalize_context(&ctx2, &bp, &blen));
75 assert(bp == erep + elen && blen == 0);
77 check(k5_size_context(ctx2, &elen2));
78 assert(elen2 == elen);
79 erep2 = ealloc(elen2);
83 check(k5_externalize_context(ctx2, &bp, &blen));
84 assert(bp == erep2 + elen2 && blen == 0);
85 assert(memcmp(erep, erep2, elen) == 0);
89 krb5_free_context(ctx2);
93 ser_auth_context(krb5_auth_context actx)
95 uint8_t *erep, *erep2, *bp;
96 size_t elen = 0, elen2 = 0, blen;
97 krb5_auth_context actx2;
99 check(k5_size_auth_context(actx, &elen));
104 check(k5_externalize_auth_context(actx, &bp, &blen));
105 assert(bp == erep + elen && blen == 0);
109 check(k5_internalize_auth_context(&actx2, &bp, &blen));
110 assert(bp == erep + elen && blen == 0);
112 check(k5_size_auth_context(actx2, &elen2));
113 assert(elen2 == elen);
114 erep2 = ealloc(elen2);
118 check(k5_externalize_auth_context(actx2, &bp, &blen));
119 assert(bp == erep2 + elen2 && blen == 0);
120 assert(memcmp(erep, erep2, elen) == 0);
124 krb5_auth_con_free(NULL, actx2);
128 ser_principal(krb5_principal princ)
130 uint8_t *erep, *erep2, *bp;
131 size_t elen = 0, elen2 = 0, blen;
132 krb5_principal princ2;
134 check(k5_size_principal(princ, &elen));
139 check(k5_externalize_principal(princ, &bp, &blen));
140 assert(bp == erep + elen && blen == 0);
144 check(k5_internalize_principal(&princ2, &bp, &blen));
145 assert(bp == erep + elen && blen == 0);
147 check(k5_size_principal(princ2, &elen2));
148 assert(elen2 == elen);
149 erep2 = ealloc(elen2);
153 check(k5_externalize_principal(princ2, &bp, &blen));
154 assert(bp == erep2 + elen2 && blen == 0);
155 assert(memcmp(erep, erep2, elen) == 0);
159 krb5_free_principal(NULL, princ2);
163 ser_checksum(krb5_checksum *cksum)
165 uint8_t *erep, *erep2, *bp;
166 size_t elen = 0, elen2 = 0, blen;
167 krb5_checksum *cksum2;
169 check(k5_size_checksum(cksum, &elen));
174 check(k5_externalize_checksum(cksum, &bp, &blen));
175 assert(bp == erep + elen && blen == 0);
179 check(k5_internalize_checksum(&cksum2, &bp, &blen));
180 assert(bp == erep + elen && blen == 0);
182 check(k5_size_checksum(cksum2, &elen2));
183 assert(elen2 == elen);
184 erep2 = ealloc(elen2);
188 check(k5_externalize_checksum(cksum2, &bp, &blen));
189 assert(bp == erep2 + elen2 && blen == 0);
190 assert(memcmp(erep, erep2, elen) == 0);
194 krb5_free_checksum(NULL, cksum2);
200 krb5_context context;
203 check(krb5_init_context(&context));
205 sprofile = context->profile;
206 context->profile = NULL;
207 ser_context(context);
209 context->profile = sprofile;
210 ser_context(context);
212 check(krb5_set_default_realm(context, "this.is.a.test"));
213 ser_context(context);
215 krb5_free_context(context);
221 krb5_auth_context actx;
222 krb5_address local_address;
223 krb5_address remote_address;
224 krb5_octet laddr_bytes[16];
225 krb5_octet raddr_bytes[16];
226 krb5_keyblock ukeyblock;
227 krb5_octet keydata[8];
228 krb5_authenticator aent;
230 krb5_authdata *adatalist[3];
231 krb5_authdata adataent;
233 check(krb5_auth_con_init(NULL, &actx));
234 ser_auth_context(actx);
236 memset(&local_address, 0, sizeof(local_address));
237 memset(&remote_address, 0, sizeof(remote_address));
238 memset(laddr_bytes, 0, sizeof(laddr_bytes));
239 memset(raddr_bytes, 0, sizeof(raddr_bytes));
240 local_address.addrtype = ADDRTYPE_INET;
241 local_address.length = sizeof(laddr_bytes);
242 local_address.contents = laddr_bytes;
250 laddr_bytes[7] = 127;
251 remote_address.addrtype = ADDRTYPE_INET;
252 remote_address.length = sizeof(raddr_bytes);
253 remote_address.contents = raddr_bytes;
261 raddr_bytes[7] = 127;
262 check(krb5_auth_con_setaddrs(NULL, actx, &local_address, &remote_address));
263 check(krb5_auth_con_setports(NULL, actx, &local_address, &remote_address));
264 ser_auth_context(actx);
266 memset(&ukeyblock, 0, sizeof(ukeyblock));
267 memset(keydata, 0, sizeof(keydata));
268 ukeyblock.enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128;
269 ukeyblock.length = sizeof(keydata);
270 ukeyblock.contents = keydata;
279 check(krb5_auth_con_setuseruserkey(NULL, actx, &ukeyblock));
280 ser_auth_context(actx);
282 check(krb5_auth_con_initivector(NULL, actx));
283 ser_auth_context(actx);
285 memset(&aent, 0, sizeof(aent));
286 aent.magic = KV5M_AUTHENTICATOR;
287 snprintf(clname, sizeof(clname),
288 "help/me/%d@this.is.a.test", (int)getpid());
289 actx->authentp = &aent;
290 check(krb5_parse_name(NULL, clname, &aent.client));
291 ser_auth_context(actx);
293 adataent.magic = KV5M_AUTHDATA;
294 adataent.ad_type = 123;
295 adataent.length = 128;
296 adataent.contents = (uint8_t *)stuff;
297 adatalist[0] = &adataent;
298 adatalist[1] = &adataent;
300 aent.authorization_data = adatalist;
301 ser_auth_context(actx);
303 krb5_free_principal(NULL, aent.client);
304 actx->authentp = NULL;
305 krb5_auth_con_free(NULL, actx);
311 krb5_principal princ;
314 snprintf(pname, sizeof(pname),
315 "the/quick/brown/fox/jumped/over/the/lazy/dog/%d@this.is.a.test",
317 check(krb5_parse_name(NULL, pname, &princ));
318 ser_principal(princ);
319 krb5_free_principal(NULL, princ);
325 krb5_checksum checksum;
326 krb5_octet ckdata[24];
328 memset(&checksum, 0, sizeof(krb5_checksum));
329 checksum.magic = KV5M_CHECKSUM;
330 ser_checksum(&checksum);
332 checksum.checksum_type = 123;
333 checksum.length = sizeof(ckdata);
334 checksum.contents = ckdata;
335 memcpy(ckdata, &stuff, sizeof(ckdata));
336 ser_checksum(&checksum);
340 main(int argc, char **argv)