1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/asn.1/ktest.c */
3 /*
4  * Copyright (C) 1994 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
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.
11  *
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.
25  */
26 
27 #include "ktest.h"
28 #include "utility.h"
29 #include <stdlib.h>
30 
31 char *sample_principal_name = "hftsai/extra@ATHENA.MIT.EDU";
32 
33 void
ktest_make_sample_authenticator(krb5_authenticator * a)34 ktest_make_sample_authenticator(krb5_authenticator *a)
35 {
36     ktest_make_sample_principal(&a->client);
37     a->checksum = ealloc(sizeof(krb5_checksum));
38     ktest_make_sample_checksum(a->checksum);
39     a->cusec = SAMPLE_USEC;
40     a->ctime = SAMPLE_TIME;
41     a->subkey = ealloc(sizeof(krb5_keyblock));
42     ktest_make_sample_keyblock(a->subkey);
43     a->seq_number = SAMPLE_SEQ_NUMBER;
44     ktest_make_sample_authorization_data(&a->authorization_data);
45 }
46 
47 void
ktest_make_sample_principal(krb5_principal * p)48 ktest_make_sample_principal(krb5_principal *p)
49 {
50     if (krb5_parse_name(test_context, sample_principal_name, p))
51         abort();
52 }
53 
54 void
ktest_make_sample_checksum(krb5_checksum * cs)55 ktest_make_sample_checksum(krb5_checksum *cs)
56 {
57     cs->checksum_type = 1;
58     cs->length = 4;
59     cs->contents = ealloc(4);
60     memcpy(cs->contents,"1234",4);
61 }
62 
63 void
ktest_make_sample_keyblock(krb5_keyblock * kb)64 ktest_make_sample_keyblock(krb5_keyblock *kb)
65 {
66     kb->magic = KV5M_KEYBLOCK;
67     kb->enctype = 1;
68     kb->length = 8;
69     kb->contents = ealloc(8);
70     memcpy(kb->contents,"12345678",8);
71 }
72 
73 void
ktest_make_sample_ticket(krb5_ticket * tkt)74 ktest_make_sample_ticket(krb5_ticket *tkt)
75 {
76     ktest_make_sample_principal(&tkt->server);
77     ktest_make_sample_enc_data(&tkt->enc_part);
78     tkt->enc_part2 = NULL;
79 }
80 
81 void
ktest_make_sample_enc_data(krb5_enc_data * ed)82 ktest_make_sample_enc_data(krb5_enc_data *ed)
83 {
84     ed->kvno = 5;
85     ed->enctype = 0;
86     krb5_data_parse(&ed->ciphertext, "krbASN.1 test message");
87 }
88 
89 void
ktest_make_sample_enc_tkt_part(krb5_enc_tkt_part * etp)90 ktest_make_sample_enc_tkt_part(krb5_enc_tkt_part *etp)
91 {
92     etp->flags = SAMPLE_FLAGS;
93     etp->session = ealloc(sizeof(krb5_keyblock));
94     ktest_make_sample_keyblock(etp->session);
95     ktest_make_sample_principal(&etp->client);
96     ktest_make_sample_transited(&etp->transited);
97     ktest_make_sample_ticket_times(&etp->times);
98     ktest_make_sample_addresses(&etp->caddrs);
99     ktest_make_sample_authorization_data(&etp->authorization_data);
100 }
101 
102 void
ktest_make_sample_addresses(krb5_address *** caddrs)103 ktest_make_sample_addresses(krb5_address ***caddrs)
104 {
105     int i;
106 
107     *caddrs = ealloc(3 * sizeof(krb5_address *));
108     for (i = 0; i < 2; i++) {
109         (*caddrs)[i] = ealloc(sizeof(krb5_address));
110         ktest_make_sample_address((*caddrs)[i]);
111     }
112     (*caddrs)[2] = NULL;
113 }
114 
115 void
ktest_make_sample_authorization_data(krb5_authdata *** ad)116 ktest_make_sample_authorization_data(krb5_authdata ***ad)
117 {
118     int i;
119 
120     *ad = ealloc(3 * sizeof(krb5_authdata *));
121     for (i = 0; i <= 1; i++) {
122         (*ad)[i] = ealloc(sizeof(krb5_authdata));
123         ktest_make_sample_authdata((*ad)[i]);
124     }
125     (*ad)[2] = NULL;
126 }
127 
128 void
ktest_make_sample_transited(krb5_transited * t)129 ktest_make_sample_transited(krb5_transited *t)
130 {
131     t->tr_type = 1;
132     krb5_data_parse(&t->tr_contents, "EDU,MIT.,ATHENA.,WASHINGTON.EDU,CS.");
133 }
134 
135 void
ktest_make_sample_ticket_times(krb5_ticket_times * tt)136 ktest_make_sample_ticket_times(krb5_ticket_times *tt)
137 {
138     tt->authtime = SAMPLE_TIME;
139     tt->starttime = SAMPLE_TIME;
140     tt->endtime = SAMPLE_TIME;
141     tt->renew_till = SAMPLE_TIME;
142 }
143 
144 void
ktest_make_sample_address(krb5_address * a)145 ktest_make_sample_address(krb5_address *a)
146 {
147     a->addrtype = ADDRTYPE_INET;
148     a->length = 4;
149     a->contents = ealloc(4 * sizeof(krb5_octet));
150     a->contents[0] = 18;
151     a->contents[1] = 208;
152     a->contents[2] = 0;
153     a->contents[3] = 35;
154 }
155 
156 void
ktest_make_sample_authdata(krb5_authdata * ad)157 ktest_make_sample_authdata(krb5_authdata *ad)
158 {
159     ad->ad_type = 1;
160     ad->length = 6;
161     ad->contents = ealloc(6 * sizeof(krb5_octet));
162     memcpy(ad->contents, "foobar", 6);
163 }
164 
165 void
ktest_make_sample_enc_kdc_rep_part(krb5_enc_kdc_rep_part * ekr)166 ktest_make_sample_enc_kdc_rep_part(krb5_enc_kdc_rep_part *ekr)
167 {
168     ekr->session = ealloc(sizeof(krb5_keyblock));
169     ktest_make_sample_keyblock(ekr->session);
170     ktest_make_sample_last_req(&ekr->last_req);
171     ekr->nonce = SAMPLE_NONCE;
172     ekr->key_exp = SAMPLE_TIME;
173     ekr->flags = SAMPLE_FLAGS;
174     ekr->times.authtime = SAMPLE_TIME;
175     ekr->times.starttime = SAMPLE_TIME;
176     ekr->times.endtime = SAMPLE_TIME;
177     ekr->times.renew_till = SAMPLE_TIME;
178     ktest_make_sample_principal(&ekr->server);
179     ktest_make_sample_addresses(&ekr->caddrs);
180 }
181 
182 void
ktest_make_sample_last_req(krb5_last_req_entry *** lr)183 ktest_make_sample_last_req(krb5_last_req_entry ***lr)
184 {
185     int i;
186 
187     *lr = ealloc(3 * sizeof(krb5_last_req_entry *));
188     for (i = 0; i <= 1; i++)
189         ktest_make_sample_last_req_entry(&(*lr)[i]);
190     (*lr)[2] = NULL;
191 }
192 
193 void
ktest_make_sample_last_req_entry(krb5_last_req_entry ** lre)194 ktest_make_sample_last_req_entry(krb5_last_req_entry **lre)
195 {
196     *lre = ealloc(sizeof(krb5_last_req_entry));
197     (*lre)->lr_type = -5;
198     (*lre)->value = SAMPLE_TIME;
199 }
200 
201 void
ktest_make_sample_kdc_rep(krb5_kdc_rep * kdcr)202 ktest_make_sample_kdc_rep(krb5_kdc_rep *kdcr)
203 {
204     ktest_make_sample_pa_data_array(&kdcr->padata);
205     ktest_make_sample_principal(&kdcr->client);
206     kdcr->ticket = ealloc(sizeof(krb5_ticket));
207     ktest_make_sample_ticket(kdcr->ticket);
208     ktest_make_sample_enc_data(&kdcr->enc_part);
209     kdcr->enc_part2 = NULL;
210 }
211 
212 void
ktest_make_sample_pa_data_array(krb5_pa_data *** pad)213 ktest_make_sample_pa_data_array(krb5_pa_data ***pad)
214 {
215     int i;
216 
217     *pad = ealloc(3 * sizeof(krb5_pa_data *));
218     for (i = 0; i <= 1; i++) {
219         (*pad)[i] = ealloc(sizeof(krb5_pa_data));
220         ktest_make_sample_pa_data((*pad)[i]);
221     }
222     (*pad)[2] = NULL;
223 }
224 
225 void
ktest_make_sample_empty_pa_data_array(krb5_pa_data *** pad)226 ktest_make_sample_empty_pa_data_array(krb5_pa_data ***pad)
227 {
228     *pad = ealloc(sizeof(krb5_pa_data *));
229     (*pad)[0] = NULL;
230 }
231 
232 void
ktest_make_sample_pa_data(krb5_pa_data * pad)233 ktest_make_sample_pa_data(krb5_pa_data *pad)
234 {
235     pad->pa_type = 13;
236     pad->length = 7;
237     pad->contents = ealloc(7);
238     memcpy(pad->contents, "pa-data", 7);
239 }
240 
241 void
ktest_make_sample_ap_req(krb5_ap_req * ar)242 ktest_make_sample_ap_req(krb5_ap_req *ar)
243 {
244     ar->ap_options = SAMPLE_FLAGS;
245     ar->ticket = ealloc(sizeof(krb5_ticket));
246     ktest_make_sample_ticket(ar->ticket);
247     ktest_make_sample_enc_data(&(ar->authenticator));
248 }
249 
250 void
ktest_make_sample_ap_rep(krb5_ap_rep * ar)251 ktest_make_sample_ap_rep(krb5_ap_rep *ar)
252 {
253     ktest_make_sample_enc_data(&ar->enc_part);
254 }
255 
256 void
ktest_make_sample_ap_rep_enc_part(krb5_ap_rep_enc_part * arep)257 ktest_make_sample_ap_rep_enc_part(krb5_ap_rep_enc_part *arep)
258 {
259     arep->ctime = SAMPLE_TIME;
260     arep->cusec = SAMPLE_USEC;
261     arep->subkey = ealloc(sizeof(krb5_keyblock));
262     ktest_make_sample_keyblock(arep->subkey);
263     arep->seq_number = SAMPLE_SEQ_NUMBER;
264 }
265 
266 void
ktest_make_sample_kdc_req(krb5_kdc_req * kr)267 ktest_make_sample_kdc_req(krb5_kdc_req *kr)
268 {
269     /* msg_type is left up to the calling procedure */
270     ktest_make_sample_pa_data_array(&kr->padata);
271     kr->kdc_options = SAMPLE_FLAGS;
272     ktest_make_sample_principal(&(kr->client));
273     ktest_make_sample_principal(&(kr->server));
274     kr->from = SAMPLE_TIME;
275     kr->till = SAMPLE_TIME;
276     kr->rtime = SAMPLE_TIME;
277     kr->nonce = SAMPLE_NONCE;
278     kr->nktypes = 2;
279     kr->ktype = ealloc(2 * sizeof(krb5_enctype));
280     kr->ktype[0] = 0;
281     kr->ktype[1] = 1;
282     ktest_make_sample_addresses(&kr->addresses);
283     ktest_make_sample_enc_data(&kr->authorization_data);
284     ktest_make_sample_authorization_data(&kr->unenc_authdata);
285     ktest_make_sample_sequence_of_ticket(&kr->second_ticket);
286 }
287 
288 void
ktest_make_sample_kdc_req_body(krb5_kdc_req * krb)289 ktest_make_sample_kdc_req_body(krb5_kdc_req *krb)
290 {
291     krb->kdc_options = SAMPLE_FLAGS;
292     ktest_make_sample_principal(&krb->client);
293     ktest_make_sample_principal(&krb->server);
294     krb->from = SAMPLE_TIME;
295     krb->till = SAMPLE_TIME;
296     krb->rtime = SAMPLE_TIME;
297     krb->nonce = SAMPLE_NONCE;
298     krb->nktypes = 2;
299     krb->ktype = (krb5_enctype*)calloc(2,sizeof(krb5_enctype));
300     krb->ktype[0] = 0;
301     krb->ktype[1] = 1;
302     ktest_make_sample_addresses(&krb->addresses);
303     ktest_make_sample_enc_data(&krb->authorization_data);
304     ktest_make_sample_authorization_data(&krb->unenc_authdata);
305     ktest_make_sample_sequence_of_ticket(&krb->second_ticket);
306 }
307 
308 void
ktest_make_sample_safe(krb5_safe * s)309 ktest_make_sample_safe(krb5_safe *s)
310 {
311     ktest_make_sample_data(&s->user_data);
312     s->timestamp = SAMPLE_TIME;
313     s->usec = SAMPLE_USEC;
314     s->seq_number = SAMPLE_SEQ_NUMBER;
315     s->s_address = ealloc(sizeof(krb5_address));
316     ktest_make_sample_address(s->s_address);
317     s->r_address = ealloc(sizeof(krb5_address));
318     ktest_make_sample_address(s->r_address);
319     s->checksum = ealloc(sizeof(krb5_checksum));
320     ktest_make_sample_checksum(s->checksum);
321 }
322 
323 void
ktest_make_sample_priv(krb5_priv * p)324 ktest_make_sample_priv(krb5_priv *p)
325 {
326     ktest_make_sample_enc_data(&p->enc_part);
327 }
328 
329 void
ktest_make_sample_priv_enc_part(krb5_priv_enc_part * pep)330 ktest_make_sample_priv_enc_part(krb5_priv_enc_part *pep)
331 {
332     ktest_make_sample_data(&(pep->user_data));
333     pep->timestamp = SAMPLE_TIME;
334     pep->usec = SAMPLE_USEC;
335     pep->seq_number = SAMPLE_SEQ_NUMBER;
336     pep->s_address = ealloc(sizeof(krb5_address));
337     ktest_make_sample_address(pep->s_address);
338     pep->r_address = ealloc(sizeof(krb5_address));
339     ktest_make_sample_address(pep->r_address);
340 }
341 
342 void
ktest_make_sample_cred(krb5_cred * c)343 ktest_make_sample_cred(krb5_cred *c)
344 {
345     ktest_make_sample_sequence_of_ticket(&c->tickets);
346     ktest_make_sample_enc_data(&c->enc_part);
347 }
348 
349 void
ktest_make_sample_sequence_of_ticket(krb5_ticket *** sot)350 ktest_make_sample_sequence_of_ticket(krb5_ticket ***sot)
351 {
352     int i;
353 
354     *sot = ealloc(3 * sizeof(krb5_ticket *));
355     for (i = 0; i < 2; i++) {
356         (*sot)[i] = ealloc(sizeof(krb5_ticket));
357         ktest_make_sample_ticket((*sot)[i]);
358     }
359     (*sot)[2] = NULL;
360 }
361 
362 void
ktest_make_sample_cred_enc_part(krb5_cred_enc_part * cep)363 ktest_make_sample_cred_enc_part(krb5_cred_enc_part *cep)
364 {
365     cep->nonce = SAMPLE_NONCE;
366     cep->timestamp = SAMPLE_TIME;
367     cep->usec = SAMPLE_USEC;
368     cep->s_address = ealloc(sizeof(krb5_address));
369     ktest_make_sample_address(cep->s_address);
370     cep->r_address = ealloc(sizeof(krb5_address));
371     ktest_make_sample_address(cep->r_address);
372     ktest_make_sequence_of_cred_info(&cep->ticket_info);
373 }
374 
375 void
ktest_make_sequence_of_cred_info(krb5_cred_info *** soci)376 ktest_make_sequence_of_cred_info(krb5_cred_info ***soci)
377 {
378     int i;
379 
380     *soci = ealloc(3 * sizeof(krb5_cred_info *));
381     for (i = 0; i < 2; i++) {
382         (*soci)[i] = ealloc(sizeof(krb5_cred_info));
383         ktest_make_sample_cred_info((*soci)[i]);
384     }
385     (*soci)[2] = NULL;
386 }
387 
388 void
ktest_make_sample_cred_info(krb5_cred_info * ci)389 ktest_make_sample_cred_info(krb5_cred_info *ci)
390 {
391     ci->session = ealloc(sizeof(krb5_keyblock));
392     ktest_make_sample_keyblock(ci->session);
393     ktest_make_sample_principal(&ci->client);
394     ktest_make_sample_principal(&ci->server);
395     ci->flags = SAMPLE_FLAGS;
396     ci->times.authtime = SAMPLE_TIME;
397     ci->times.starttime = SAMPLE_TIME;
398     ci->times.endtime = SAMPLE_TIME;
399     ci->times.renew_till = SAMPLE_TIME;
400     ktest_make_sample_addresses(&ci->caddrs);
401 }
402 
403 void
ktest_make_sample_error(krb5_error * kerr)404 ktest_make_sample_error(krb5_error *kerr)
405 {
406     kerr->ctime = SAMPLE_TIME;
407     kerr->cusec = SAMPLE_USEC;
408     kerr->susec = SAMPLE_USEC;
409     kerr->stime = SAMPLE_TIME;
410     kerr->error = SAMPLE_ERROR;
411     ktest_make_sample_principal(&kerr->client);
412     ktest_make_sample_principal(&kerr->server);
413     ktest_make_sample_data(&kerr->text);
414     ktest_make_sample_data(&kerr->e_data);
415 }
416 
417 void
ktest_make_sample_data(krb5_data * d)418 ktest_make_sample_data(krb5_data *d)
419 {
420     krb5_data_parse(d, "krb5data");
421 }
422 
423 void
ktest_make_sample_etype_info(krb5_etype_info_entry *** p)424 ktest_make_sample_etype_info(krb5_etype_info_entry ***p)
425 {
426     krb5_etype_info_entry **info;
427     int i, len;
428     char *str;
429 
430     info = ealloc(4 * sizeof(krb5_etype_info_entry *));
431     for (i = 0; i < 3; i++) {
432         info[i] = ealloc(sizeof(krb5_etype_info_entry));
433         info[i]->etype = i;
434         len = asprintf(&str, "Morton's #%d", i);
435         if (len < 0)
436             abort();
437         info[i]->salt = (krb5_octet *)str;
438         info[i]->length = len;
439         info[i]->s2kparams.data = NULL;
440         info[i]->s2kparams.length = 0;
441         info[i]->magic = KV5M_ETYPE_INFO_ENTRY;
442     }
443     free(info[1]->salt);
444     info[1]->length = KRB5_ETYPE_NO_SALT;
445     info[1]->salt = 0;
446     *p = info;
447 }
448 
449 
450 void
ktest_make_sample_etype_info2(krb5_etype_info_entry *** p)451 ktest_make_sample_etype_info2(krb5_etype_info_entry ***p)
452 {
453     krb5_etype_info_entry **info;
454     int i, len;
455     char *str;
456 
457     info = ealloc(4 * sizeof(krb5_etype_info_entry *));
458     for (i = 0; i < 3; i++) {
459         info[i] = ealloc(sizeof(krb5_etype_info_entry));
460         info[i]->etype = i;
461         len = asprintf(&str, "Morton's #%d", i);
462         if (len < 0)
463             abort();
464         info[i]->salt = (krb5_octet *)str;
465         info[i]->length = (unsigned int)len;
466         len = asprintf(&info[i]->s2kparams.data, "s2k: %d", i);
467         if (len < 0)
468             abort();
469         info[i]->s2kparams.length = (unsigned int) len;
470         info[i]->magic = KV5M_ETYPE_INFO_ENTRY;
471     }
472     free(info[1]->salt);
473     info[1]->length = KRB5_ETYPE_NO_SALT;
474     info[1]->salt = 0;
475     *p = info;
476 }
477 
478 
479 void
ktest_make_sample_pa_enc_ts(krb5_pa_enc_ts * pa_enc)480 ktest_make_sample_pa_enc_ts(krb5_pa_enc_ts *pa_enc)
481 {
482     pa_enc->patimestamp = SAMPLE_TIME;
483     pa_enc->pausec = SAMPLE_USEC;
484 }
485 
486 void
ktest_make_sample_sam_challenge_2(krb5_sam_challenge_2 * p)487 ktest_make_sample_sam_challenge_2(krb5_sam_challenge_2 *p)
488 {
489     /* Need a valid DER sequence encoding here; this one contains the OCTET
490      * STRING "challenge". */
491     krb5_data_parse(&p->sam_challenge_2_body, "\x30\x0B\x04\x09" "challenge");
492     p->sam_cksum = ealloc(2 * sizeof(krb5_checksum *));
493     p->sam_cksum[0] = ealloc(sizeof(krb5_checksum));
494     ktest_make_sample_checksum(p->sam_cksum[0]);
495     p->sam_cksum[1] = NULL;
496 }
497 
498 void
ktest_make_sample_sam_challenge_2_body(krb5_sam_challenge_2_body * p)499 ktest_make_sample_sam_challenge_2_body(krb5_sam_challenge_2_body *p)
500 {
501     p->sam_type = 42;
502     p->sam_flags = KRB5_SAM_USE_SAD_AS_KEY;
503     krb5_data_parse(&p->sam_type_name, "type name");
504     p->sam_track_id = empty_data();
505     krb5_data_parse(&p->sam_challenge_label, "challenge label");
506     krb5_data_parse(&p->sam_challenge, "challenge ipse");
507     krb5_data_parse(&p->sam_response_prompt, "response_prompt ipse");
508     p->sam_pk_for_sad = empty_data();
509     p->sam_nonce = 0x543210;
510     p->sam_etype = ENCTYPE_AES256_CTS_HMAC_SHA384_192;
511 }
512 
513 void
ktest_make_sample_sam_response_2(krb5_sam_response_2 * p)514 ktest_make_sample_sam_response_2(krb5_sam_response_2 *p)
515 {
516     p->magic = KV5M_SAM_RESPONSE;
517     p->sam_type = 43; /* information */
518     p->sam_flags = KRB5_SAM_USE_SAD_AS_KEY; /* KRB5_SAM_* values */
519     krb5_data_parse(&p->sam_track_id, "track data");
520     krb5_data_parse(&p->sam_enc_nonce_or_sad.ciphertext, "nonce or sad");
521     p->sam_enc_nonce_or_sad.enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192;
522     p->sam_enc_nonce_or_sad.kvno = 3382;
523     p->sam_nonce = 0x543210;
524 }
525 
526 void
ktest_make_sample_enc_sam_response_enc_2(krb5_enc_sam_response_enc_2 * p)527 ktest_make_sample_enc_sam_response_enc_2(krb5_enc_sam_response_enc_2 *p)
528 {
529     p->magic = 83;
530     p->sam_nonce = 88;
531     krb5_data_parse(&p->sam_sad, "enc_sam_response_enc_2");
532 }
533 
534 void
ktest_make_sample_pa_for_user(krb5_pa_for_user * p)535 ktest_make_sample_pa_for_user(krb5_pa_for_user *p)
536 {
537     ktest_make_sample_principal(&p->user);
538     ktest_make_sample_checksum(&p->cksum);
539     ktest_make_sample_data(&p->auth_package);
540 }
541 
542 void
ktest_make_sample_pa_s4u_x509_user(krb5_pa_s4u_x509_user * p)543 ktest_make_sample_pa_s4u_x509_user(krb5_pa_s4u_x509_user *p)
544 {
545     krb5_s4u_userid *u = &p->user_id;
546 
547     u->nonce = 13243546;
548     ktest_make_sample_principal(&u->user);
549     krb5_data_parse(&u->subject_cert, "pa_s4u_x509_user");
550     u->options = 0x80000000;
551     ktest_make_sample_checksum(&p->cksum);
552 }
553 
554 void
ktest_make_sample_ad_kdcissued(krb5_ad_kdcissued * p)555 ktest_make_sample_ad_kdcissued(krb5_ad_kdcissued *p)
556 {
557     ktest_make_sample_checksum(&p->ad_checksum);
558     ktest_make_sample_principal(&p->i_principal);
559     ktest_make_sample_authorization_data(&p->elements);
560 }
561 
562 void
ktest_make_sample_ad_signedpath_data(krb5_ad_signedpath_data * p)563 ktest_make_sample_ad_signedpath_data(krb5_ad_signedpath_data *p)
564 {
565     ktest_make_sample_principal(&p->client);
566     p->authtime = SAMPLE_TIME;
567     p->delegated = ealloc(2 * sizeof(krb5_principal));
568     ktest_make_sample_principal(&p->delegated[0]);
569     p->delegated[1] = NULL;
570     ktest_make_sample_authorization_data(&p->authorization_data);
571     ktest_make_sample_pa_data_array(&p->method_data);
572 }
573 
574 void
ktest_make_sample_ad_signedpath(krb5_ad_signedpath * p)575 ktest_make_sample_ad_signedpath(krb5_ad_signedpath *p)
576 {
577     p->enctype = 1;
578     ktest_make_sample_checksum(&p->checksum);
579     p->delegated = ealloc(2 * sizeof(krb5_principal));
580     p->delegated[1] = NULL;
581     ktest_make_sample_pa_data_array(&p->method_data);
582 }
583 
584 void
ktest_make_sample_iakerb_header(krb5_iakerb_header * ih)585 ktest_make_sample_iakerb_header(krb5_iakerb_header *ih)
586 {
587     ktest_make_sample_data(&(ih->target_realm));
588     ih->cookie = ealloc(sizeof(krb5_data));
589     ktest_make_sample_data(ih->cookie);
590 }
591 
592 void
ktest_make_sample_iakerb_finished(krb5_iakerb_finished * ih)593 ktest_make_sample_iakerb_finished(krb5_iakerb_finished *ih)
594 {
595     ktest_make_sample_checksum(&ih->checksum);
596 }
597 
598 static void
ktest_make_sample_fast_finished(krb5_fast_finished * p)599 ktest_make_sample_fast_finished(krb5_fast_finished *p)
600 {
601     p->timestamp = SAMPLE_TIME;
602     p->usec = SAMPLE_USEC;
603     ktest_make_sample_principal(&p->client);
604     ktest_make_sample_checksum(&p->ticket_checksum);
605 }
606 
607 void
ktest_make_sample_fast_response(krb5_fast_response * p)608 ktest_make_sample_fast_response(krb5_fast_response *p)
609 {
610     ktest_make_sample_pa_data_array(&p->padata);
611     p->strengthen_key = ealloc(sizeof(krb5_keyblock));
612     ktest_make_sample_keyblock(p->strengthen_key);
613     p->finished = ealloc(sizeof(krb5_fast_finished));
614     ktest_make_sample_fast_finished(p->finished);
615     p->nonce = SAMPLE_NONCE;
616 }
617 
618 void
ktest_make_sha256_alg(krb5_algorithm_identifier * p)619 ktest_make_sha256_alg(krb5_algorithm_identifier *p)
620 {
621     /* { 2 16 840 1 101 3 4 2 1 } */
622     krb5_data_parse(&p->algorithm, "\x60\x86\x48\x01\x65\x03\x04\x02\x01");
623     p->parameters = empty_data();
624 }
625 
626 void
ktest_make_sha1_alg(krb5_algorithm_identifier * p)627 ktest_make_sha1_alg(krb5_algorithm_identifier *p)
628 {
629     /* { 1 3 14 3 2 26 } */
630     krb5_data_parse(&p->algorithm, "\x2b\x0e\x03\x02\x1a");
631     p->parameters = empty_data();
632 }
633 
634 void
ktest_make_minimal_otp_tokeninfo(krb5_otp_tokeninfo * p)635 ktest_make_minimal_otp_tokeninfo(krb5_otp_tokeninfo *p)
636 {
637     memset(p, 0, sizeof(*p));
638     p->length = p->format = p->iteration_count = -1;
639 }
640 
641 void
ktest_make_maximal_otp_tokeninfo(krb5_otp_tokeninfo * p)642 ktest_make_maximal_otp_tokeninfo(krb5_otp_tokeninfo *p)
643 {
644     p->flags = KRB5_OTP_FLAG_NEXTOTP | KRB5_OTP_FLAG_COMBINE |
645         KRB5_OTP_FLAG_COLLECT_PIN | KRB5_OTP_FLAG_ENCRYPT_NONCE |
646         KRB5_OTP_FLAG_SEPARATE_PIN | KRB5_OTP_FLAG_CHECK_DIGIT;
647     krb5_data_parse(&p->vendor, "Examplecorp");
648     krb5_data_parse(&p->challenge, "hark!");
649     p->length = 10;
650     p->format = 2;
651     krb5_data_parse(&p->token_id, "yourtoken");
652     krb5_data_parse(&p->alg_id, "urn:ietf:params:xml:ns:keyprov:pskc:hotp");
653     p->supported_hash_alg = ealloc(3 * sizeof(*p->supported_hash_alg));
654     p->supported_hash_alg[0] = ealloc(sizeof(*p->supported_hash_alg[0]));
655     ktest_make_sha256_alg(p->supported_hash_alg[0]);
656     p->supported_hash_alg[1] = ealloc(sizeof(*p->supported_hash_alg[1]));
657     ktest_make_sha1_alg(p->supported_hash_alg[1]);
658     p->supported_hash_alg[2] = NULL;
659     p->iteration_count = 1000;
660 }
661 
662 void
ktest_make_minimal_pa_otp_challenge(krb5_pa_otp_challenge * p)663 ktest_make_minimal_pa_otp_challenge(krb5_pa_otp_challenge *p)
664 {
665     memset(p, 0, sizeof(*p));
666     krb5_data_parse(&p->nonce, "minnonce");
667     p->tokeninfo = ealloc(2 * sizeof(*p->tokeninfo));
668     p->tokeninfo[0] = ealloc(sizeof(*p->tokeninfo[0]));
669     ktest_make_minimal_otp_tokeninfo(p->tokeninfo[0]);
670     p->tokeninfo[1] = NULL;
671 }
672 
673 void
ktest_make_maximal_pa_otp_challenge(krb5_pa_otp_challenge * p)674 ktest_make_maximal_pa_otp_challenge(krb5_pa_otp_challenge *p)
675 {
676     krb5_data_parse(&p->nonce, "maxnonce");
677     krb5_data_parse(&p->service, "testservice");
678     p->tokeninfo = ealloc(3 * sizeof(*p->tokeninfo));
679     p->tokeninfo[0] = ealloc(sizeof(*p->tokeninfo[0]));
680     ktest_make_minimal_otp_tokeninfo(p->tokeninfo[0]);
681     p->tokeninfo[1] = ealloc(sizeof(*p->tokeninfo[1]));
682     ktest_make_maximal_otp_tokeninfo(p->tokeninfo[1]);
683     p->tokeninfo[2] = NULL;
684     krb5_data_parse(&p->salt, "keysalt");
685     krb5_data_parse(&p->s2kparams, "1234");
686 }
687 
688 void
ktest_make_minimal_pa_otp_req(krb5_pa_otp_req * p)689 ktest_make_minimal_pa_otp_req(krb5_pa_otp_req *p)
690 {
691     memset(p, 0, sizeof(*p));
692     p->iteration_count = -1;
693     p->format = -1;
694     ktest_make_sample_enc_data(&p->enc_data);
695 }
696 
697 void
ktest_make_maximal_pa_otp_req(krb5_pa_otp_req * p)698 ktest_make_maximal_pa_otp_req(krb5_pa_otp_req *p)
699 {
700     p->flags = KRB5_OTP_FLAG_NEXTOTP | KRB5_OTP_FLAG_COMBINE;
701     krb5_data_parse(&p->nonce, "nonce");
702     ktest_make_sample_enc_data(&p->enc_data);
703     p->hash_alg = ealloc(sizeof(*p->hash_alg));
704     ktest_make_sha256_alg(p->hash_alg);
705     p->iteration_count = 1000;
706     krb5_data_parse(&p->otp_value, "frogs");
707     krb5_data_parse(&p->pin, "myfirstpin");
708     krb5_data_parse(&p->challenge, "hark!");
709     p->time = SAMPLE_TIME;
710     krb5_data_parse(&p->counter, "346");
711     p->format = 2;
712     krb5_data_parse(&p->token_id, "yourtoken");
713     krb5_data_parse(&p->alg_id, "urn:ietf:params:xml:ns:keyprov:pskc:hotp");
714     krb5_data_parse(&p->vendor, "Examplecorp");
715 }
716 
717 #ifndef DISABLE_PKINIT
718 
719 static void
ktest_make_sample_pk_authenticator(krb5_pk_authenticator * p)720 ktest_make_sample_pk_authenticator(krb5_pk_authenticator *p)
721 {
722     p->cusec = SAMPLE_USEC;
723     p->ctime = SAMPLE_TIME;
724     p->nonce = SAMPLE_NONCE;
725     ktest_make_sample_checksum(&p->paChecksum);
726     /* We don't encode the checksum type, only the contents. */
727     p->paChecksum.checksum_type = 0;
728     p->freshnessToken = ealloc(sizeof(krb5_data));
729     ktest_make_sample_data(p->freshnessToken);
730 }
731 
732 static void
ktest_make_sample_oid(krb5_data * p)733 ktest_make_sample_oid(krb5_data *p)
734 {
735     krb5_data_parse(p, "\052\206\110\206\367\022\001\002\002");
736 }
737 
738 static void
ktest_make_sample_algorithm_identifier(krb5_algorithm_identifier * p)739 ktest_make_sample_algorithm_identifier(krb5_algorithm_identifier *p)
740 {
741     ktest_make_sample_oid(&p->algorithm);
742     /* Need a valid DER encoding here; this is the OCTET STRING "params". */
743     krb5_data_parse(&p->parameters, "\x04\x06" "params");
744 }
745 
746 static void
ktest_make_sample_algorithm_identifier_no_params(krb5_algorithm_identifier * p)747 ktest_make_sample_algorithm_identifier_no_params(krb5_algorithm_identifier *p)
748 {
749     ktest_make_sample_oid(&p->algorithm);
750     p->parameters = empty_data();
751 }
752 
753 static void
ktest_make_sample_subject_pk_info(krb5_subject_pk_info * p)754 ktest_make_sample_subject_pk_info(krb5_subject_pk_info *p)
755 {
756     ktest_make_sample_algorithm_identifier(&p->algorithm);
757     ktest_make_sample_data(&p->subjectPublicKey);
758 }
759 
760 static void
ktest_make_sample_external_principal_identifier(krb5_external_principal_identifier * p)761 ktest_make_sample_external_principal_identifier(
762     krb5_external_principal_identifier *p)
763 {
764     ktest_make_sample_data(&p->subjectName);
765     ktest_make_sample_data(&p->issuerAndSerialNumber);
766     ktest_make_sample_data(&p->subjectKeyIdentifier);
767 }
768 
769 void
ktest_make_sample_pa_pk_as_req(krb5_pa_pk_as_req * p)770 ktest_make_sample_pa_pk_as_req(krb5_pa_pk_as_req *p)
771 {
772     ktest_make_sample_data(&p->signedAuthPack);
773     p->trustedCertifiers =
774         ealloc(2 * sizeof(krb5_external_principal_identifier *));
775     p->trustedCertifiers[0] =
776         ealloc(sizeof(krb5_external_principal_identifier));
777     ktest_make_sample_external_principal_identifier(p->trustedCertifiers[0]);
778     p->trustedCertifiers[1] = NULL;
779     ktest_make_sample_data(&p->kdcPkId);
780 }
781 
782 static void
ktest_make_sample_dh_rep_info(krb5_dh_rep_info * p)783 ktest_make_sample_dh_rep_info(krb5_dh_rep_info *p)
784 {
785     ktest_make_sample_data(&p->dhSignedData);
786     ktest_make_sample_data(&p->serverDHNonce);
787     p->kdfID = ealloc(sizeof(krb5_data));
788     ktest_make_sample_data(p->kdfID);
789 }
790 
791 void
ktest_make_sample_pa_pk_as_rep_dhInfo(krb5_pa_pk_as_rep * p)792 ktest_make_sample_pa_pk_as_rep_dhInfo(krb5_pa_pk_as_rep *p)
793 {
794     p->choice = choice_pa_pk_as_rep_dhInfo;
795     ktest_make_sample_dh_rep_info(&p->u.dh_Info);
796 }
797 
798 void
ktest_make_sample_pa_pk_as_rep_encKeyPack(krb5_pa_pk_as_rep * p)799 ktest_make_sample_pa_pk_as_rep_encKeyPack(krb5_pa_pk_as_rep *p)
800 {
801     p->choice = choice_pa_pk_as_rep_encKeyPack;
802     ktest_make_sample_data(&p->u.encKeyPack);
803 }
804 
805 void
ktest_make_sample_auth_pack(krb5_auth_pack * p)806 ktest_make_sample_auth_pack(krb5_auth_pack *p)
807 {
808     ktest_make_sample_pk_authenticator(&p->pkAuthenticator);
809     p->clientPublicValue = ealloc(sizeof(krb5_subject_pk_info));
810     ktest_make_sample_subject_pk_info(p->clientPublicValue);
811     p->supportedCMSTypes = ealloc(3 * sizeof(krb5_algorithm_identifier *));
812     p->supportedCMSTypes[0] = ealloc(sizeof(krb5_algorithm_identifier));
813     ktest_make_sample_algorithm_identifier(p->supportedCMSTypes[0]);
814     p->supportedCMSTypes[1] = ealloc(sizeof(krb5_algorithm_identifier));
815     ktest_make_sample_algorithm_identifier_no_params(p->supportedCMSTypes[1]);
816     p->supportedCMSTypes[2] = NULL;
817     ktest_make_sample_data(&p->clientDHNonce);
818     p->supportedKDFs = ealloc(2 * sizeof(krb5_data *));
819     p->supportedKDFs[0] = ealloc(sizeof(krb5_data));
820     ktest_make_sample_data(p->supportedKDFs[0]);
821     p->supportedKDFs[1] = NULL;
822 }
823 
824 void
ktest_make_sample_kdc_dh_key_info(krb5_kdc_dh_key_info * p)825 ktest_make_sample_kdc_dh_key_info(krb5_kdc_dh_key_info *p)
826 {
827     ktest_make_sample_data(&p->subjectPublicKey);
828     p->nonce = SAMPLE_NONCE;
829     p->dhKeyExpiration = SAMPLE_TIME;
830 }
831 
832 void
ktest_make_sample_reply_key_pack(krb5_reply_key_pack * p)833 ktest_make_sample_reply_key_pack(krb5_reply_key_pack *p)
834 {
835     ktest_make_sample_keyblock(&p->replyKey);
836     ktest_make_sample_checksum(&p->asChecksum);
837 }
838 
839 void
ktest_make_sample_sp80056a_other_info(krb5_sp80056a_other_info * p)840 ktest_make_sample_sp80056a_other_info(krb5_sp80056a_other_info *p)
841 {
842     ktest_make_sample_algorithm_identifier_no_params(&p->algorithm_identifier);
843     ktest_make_sample_principal(&p->party_u_info);
844     ktest_make_sample_principal(&p->party_v_info);
845     ktest_make_sample_data(&p->supp_pub_info);
846 }
847 
848 void
ktest_make_sample_pkinit_supp_pub_info(krb5_pkinit_supp_pub_info * p)849 ktest_make_sample_pkinit_supp_pub_info(krb5_pkinit_supp_pub_info *p)
850 {
851     p->enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192;
852     ktest_make_sample_data(&p->as_req);
853     ktest_make_sample_data(&p->pk_as_rep);
854 }
855 
856 #endif /* not DISABLE_PKINIT */
857 
858 #ifdef ENABLE_LDAP
859 static void
ktest_make_sample_key_data(krb5_key_data * p,int i)860 ktest_make_sample_key_data(krb5_key_data *p, int i)
861 {
862     char *str;
863     int len;
864 
865     len = asprintf(&str, "key%d", i);
866     if (len < 0)
867         abort();
868     p->key_data_ver = 2;
869     p->key_data_type[0] = 2;
870     p->key_data_length[0] = (unsigned int) len;
871     p->key_data_contents[0] = (krb5_octet *)str;
872     len = asprintf(&str, "salt%d", i);
873     if (len < 0)
874         abort();
875     p->key_data_type[1] = i;
876     p->key_data_length[1] = (unsigned int) len;
877     p->key_data_contents[1] = (krb5_octet *)str;
878 }
879 
880 void
ktest_make_sample_ldap_seqof_key_data(ldap_seqof_key_data * p)881 ktest_make_sample_ldap_seqof_key_data(ldap_seqof_key_data *p)
882 {
883     int i;
884 
885     p->mkvno = 14;
886     p->n_key_data = 3;
887     p->key_data = calloc(3,sizeof(krb5_key_data));
888     p->kvno = 42;
889     for (i = 0; i < 3; i++)
890         ktest_make_sample_key_data(&p->key_data[i], i);
891 }
892 #endif
893 
894 void
ktest_make_sample_kkdcp_message(krb5_kkdcp_message * p)895 ktest_make_sample_kkdcp_message(krb5_kkdcp_message *p)
896 {
897     krb5_kdc_req req;
898     krb5_data *message;
899 
900     ktest_make_sample_kdc_req(&req);
901     req.msg_type = KRB5_AS_REQ;
902     encode_krb5_as_req(&req, &message);
903     p->kerb_message = *message;
904     free(message);
905     ktest_empty_kdc_req(&req);
906     ktest_make_sample_data(&(p->target_domain));
907     p->dclocator_hint = 0;
908 }
909 
910 static krb5_authdata *
make_ad_element(krb5_authdatatype ad_type,const char * str)911 make_ad_element(krb5_authdatatype ad_type, const char *str)
912 {
913     krb5_authdata *ad;
914 
915     ad = ealloc(sizeof(*ad));
916     ad->ad_type = ad_type;
917     ad->length = strlen(str);
918     ad->contents = ealloc(ad->length);
919     memcpy(ad->contents, str, ad->length);
920     return ad;
921 }
922 
923 static krb5_verifier_mac *
make_vmac(krb5_boolean include_princ,krb5_kvno kvno,krb5_enctype enctype,const char * cksumstr)924 make_vmac(krb5_boolean include_princ, krb5_kvno kvno, krb5_enctype enctype,
925           const char *cksumstr)
926 {
927     krb5_verifier_mac *vmac;
928 
929     vmac = ealloc(sizeof(*vmac));
930     if (include_princ) {
931         ktest_make_sample_principal(&vmac->princ);
932         (void)krb5_set_principal_realm(NULL, vmac->princ, "");
933     } else {
934         vmac->princ = NULL;
935     }
936     vmac->kvno = kvno;
937     vmac->enctype = enctype;
938     vmac->checksum.checksum_type = 1;
939     vmac->checksum.length = strlen(cksumstr);
940     vmac->checksum.contents = ealloc(vmac->checksum.length);
941     memcpy(vmac->checksum.contents, cksumstr, vmac->checksum.length);
942     return vmac;
943 }
944 
945 void
ktest_make_minimal_cammac(krb5_cammac * p)946 ktest_make_minimal_cammac(krb5_cammac *p)
947 {
948     memset(p, 0, sizeof(*p));
949     p->elements = ealloc(2 * sizeof(*p->elements));
950     p->elements[0] = make_ad_element(1, "ad1");
951     p->elements[1] = NULL;
952 }
953 
954 void
ktest_make_maximal_cammac(krb5_cammac * p)955 ktest_make_maximal_cammac(krb5_cammac *p)
956 {
957     p->elements = ealloc(3 * sizeof(*p->elements));
958     p->elements[0] = make_ad_element(1, "ad1");
959     p->elements[1] = make_ad_element(2, "ad2");
960     p->elements[2] = NULL;
961     p->kdc_verifier = make_vmac(TRUE, 5, 16, "cksumkdc");
962     p->svc_verifier = make_vmac(TRUE, 5, 16, "cksumsvc");
963     p->other_verifiers = ealloc(3 * sizeof(*p->other_verifiers));
964     p->other_verifiers[0] = make_vmac(FALSE, 0, 0, "cksum1");
965     p->other_verifiers[1] = make_vmac(TRUE, 5, 16, "cksum2");
966     p->other_verifiers[2] = NULL;
967 }
968 
969 void
ktest_make_sample_secure_cookie(krb5_secure_cookie * p)970 ktest_make_sample_secure_cookie(krb5_secure_cookie *p)
971 {
972     ktest_make_sample_pa_data_array(&p->data);
973     p->time = SAMPLE_TIME;
974 }
975 
976 void
ktest_make_minimal_spake_factor(krb5_spake_factor * p)977 ktest_make_minimal_spake_factor(krb5_spake_factor *p)
978 {
979     p->type = 1;
980     p->data = NULL;
981 }
982 
983 void
ktest_make_maximal_spake_factor(krb5_spake_factor * p)984 ktest_make_maximal_spake_factor(krb5_spake_factor *p)
985 {
986     p->type = 2;
987     p->data = ealloc(sizeof(*p->data));
988     krb5_data_parse(p->data, "fdata");
989 }
990 
991 void
ktest_make_support_pa_spake(krb5_pa_spake * p)992 ktest_make_support_pa_spake(krb5_pa_spake *p)
993 {
994     krb5_spake_support *s = &p->u.support;
995 
996     s->ngroups = 2;
997     s->groups = ealloc(s->ngroups * sizeof(*s->groups));
998     s->groups[0] = 1;
999     s->groups[1] = 2;
1000     p->choice = SPAKE_MSGTYPE_SUPPORT;
1001 }
1002 
1003 void
ktest_make_challenge_pa_spake(krb5_pa_spake * p)1004 ktest_make_challenge_pa_spake(krb5_pa_spake *p)
1005 {
1006     krb5_spake_challenge *c = &p->u.challenge;
1007 
1008     c->group = 1;
1009     krb5_data_parse(&c->pubkey, "T value");
1010     c->factors = ealloc(3 * sizeof(*c->factors));
1011     c->factors[0] = ealloc(sizeof(*c->factors[0]));
1012     ktest_make_minimal_spake_factor(c->factors[0]);
1013     c->factors[1] = ealloc(sizeof(*c->factors[1]));
1014     ktest_make_maximal_spake_factor(c->factors[1]);
1015     c->factors[2] = NULL;
1016     p->choice = SPAKE_MSGTYPE_CHALLENGE;
1017 }
1018 
1019 void
ktest_make_response_pa_spake(krb5_pa_spake * p)1020 ktest_make_response_pa_spake(krb5_pa_spake *p)
1021 {
1022     krb5_spake_response *r = &p->u.response;
1023 
1024     krb5_data_parse(&r->pubkey, "S value");
1025     ktest_make_sample_enc_data(&r->factor);
1026     p->choice = SPAKE_MSGTYPE_RESPONSE;
1027 }
1028 
1029 void
ktest_make_encdata_pa_spake(krb5_pa_spake * p)1030 ktest_make_encdata_pa_spake(krb5_pa_spake *p)
1031 {
1032     ktest_make_sample_enc_data(&p->u.encdata);
1033     p->choice = SPAKE_MSGTYPE_ENCDATA;
1034 }
1035 
1036 /****************************************************************/
1037 /* destructors */
1038 
1039 void
ktest_destroy_data(krb5_data ** d)1040 ktest_destroy_data(krb5_data **d)
1041 {
1042     if (*d != NULL) {
1043         free((*d)->data);
1044         free(*d);
1045         *d = NULL;
1046     }
1047 }
1048 
1049 void
ktest_empty_data(krb5_data * d)1050 ktest_empty_data(krb5_data *d)
1051 {
1052     if (d->data != NULL) {
1053         free(d->data);
1054         d->data = NULL;
1055         d->length = 0;
1056     }
1057 }
1058 
1059 static void
ktest_empty_checksum(krb5_checksum * cs)1060 ktest_empty_checksum(krb5_checksum *cs)
1061 {
1062     free(cs->contents);
1063     cs->contents = NULL;
1064 }
1065 
1066 void
ktest_destroy_checksum(krb5_checksum ** cs)1067 ktest_destroy_checksum(krb5_checksum **cs)
1068 {
1069     if (*cs != NULL) {
1070         free((*cs)->contents);
1071         free(*cs);
1072         *cs = NULL;
1073     }
1074 }
1075 
1076 void
ktest_empty_keyblock(krb5_keyblock * kb)1077 ktest_empty_keyblock(krb5_keyblock *kb)
1078 {
1079     if (kb != NULL) {
1080         if (kb->contents) {
1081             free(kb->contents);
1082             kb->contents = NULL;
1083         }
1084     }
1085 }
1086 
1087 void
ktest_destroy_keyblock(krb5_keyblock ** kb)1088 ktest_destroy_keyblock(krb5_keyblock **kb)
1089 {
1090     if (*kb != NULL) {
1091         free((*kb)->contents);
1092         free(*kb);
1093         *kb = NULL;
1094     }
1095 }
1096 
1097 void
ktest_empty_authorization_data(krb5_authdata ** ad)1098 ktest_empty_authorization_data(krb5_authdata **ad)
1099 {
1100     int i;
1101 
1102     if (*ad != NULL) {
1103         for (i=0; ad[i] != NULL; i++)
1104             ktest_destroy_authdata(&ad[i]);
1105     }
1106 }
1107 
1108 void
ktest_destroy_authorization_data(krb5_authdata *** ad)1109 ktest_destroy_authorization_data(krb5_authdata ***ad)
1110 {
1111     ktest_empty_authorization_data(*ad);
1112     free(*ad);
1113     *ad = NULL;
1114 }
1115 
1116 void
ktest_destroy_authdata(krb5_authdata ** ad)1117 ktest_destroy_authdata(krb5_authdata **ad)
1118 {
1119     if (*ad != NULL) {
1120         free((*ad)->contents);
1121         free(*ad);
1122         *ad = NULL;
1123     }
1124 }
1125 
1126 void
ktest_empty_pa_data_array(krb5_pa_data ** pad)1127 ktest_empty_pa_data_array(krb5_pa_data **pad)
1128 {
1129     int i;
1130 
1131     for (i=0; pad[i] != NULL; i++)
1132         ktest_destroy_pa_data(&pad[i]);
1133 }
1134 
1135 void
ktest_destroy_pa_data_array(krb5_pa_data *** pad)1136 ktest_destroy_pa_data_array(krb5_pa_data ***pad)
1137 {
1138     ktest_empty_pa_data_array(*pad);
1139     free(*pad);
1140     *pad = NULL;
1141 }
1142 
1143 void
ktest_destroy_pa_data(krb5_pa_data ** pad)1144 ktest_destroy_pa_data(krb5_pa_data **pad)
1145 {
1146     if (*pad != NULL) {
1147         free((*pad)->contents);
1148         free(*pad);
1149         *pad = NULL;
1150     }
1151 }
1152 
1153 void
ktest_destroy_address(krb5_address ** a)1154 ktest_destroy_address(krb5_address **a)
1155 {
1156     if (*a != NULL) {
1157         free((*a)->contents);
1158         free(*a);
1159         *a = NULL;
1160     }
1161 }
1162 
1163 void
ktest_empty_addresses(krb5_address ** a)1164 ktest_empty_addresses(krb5_address **a)
1165 {
1166     int i;
1167 
1168     for (i=0; a[i] != NULL; i++)
1169         ktest_destroy_address(&a[i]);
1170 }
1171 
1172 void
ktest_destroy_addresses(krb5_address *** a)1173 ktest_destroy_addresses(krb5_address ***a)
1174 {
1175     ktest_empty_addresses(*a);
1176     free(*a);
1177     *a = NULL;
1178 }
1179 
1180 void
ktest_destroy_principal(krb5_principal * p)1181 ktest_destroy_principal(krb5_principal *p)
1182 {
1183     int i;
1184 
1185     if (*p == NULL)
1186         return;
1187     for (i=0; i<(*p)->length; i++)
1188         ktest_empty_data(&(*p)->data[i]);
1189     ktest_empty_data(&(*p)->realm);
1190     free((*p)->data);
1191     free(*p);
1192     *p = NULL;
1193 }
1194 
1195 void
ktest_destroy_sequence_of_integer(long ** soi)1196 ktest_destroy_sequence_of_integer(long **soi)
1197 {
1198     free(*soi);
1199     *soi = NULL;
1200 }
1201 
1202 void
ktest_destroy_sequence_of_ticket(krb5_ticket *** sot)1203 ktest_destroy_sequence_of_ticket(krb5_ticket ***sot)
1204 {
1205     int i;
1206 
1207     for (i=0; (*sot)[i] != NULL; i++)
1208         ktest_destroy_ticket(&(*sot)[i]);
1209     free(*sot);
1210     *sot = NULL;
1211 }
1212 
1213 void
ktest_destroy_ticket(krb5_ticket ** tkt)1214 ktest_destroy_ticket(krb5_ticket **tkt)
1215 {
1216     ktest_destroy_principal(&(*tkt)->server);
1217     ktest_destroy_enc_data(&(*tkt)->enc_part);
1218     /*  ktest_empty_enc_tkt_part(((*tkt)->enc_part2));*/
1219     free(*tkt);
1220     *tkt = NULL;
1221 }
1222 
1223 void
ktest_empty_ticket(krb5_ticket * tkt)1224 ktest_empty_ticket(krb5_ticket *tkt)
1225 {
1226     if (tkt->server)
1227         ktest_destroy_principal(&tkt->server);
1228     ktest_destroy_enc_data(&tkt->enc_part);
1229     if (tkt->enc_part2)
1230         ktest_destroy_enc_tkt_part(&tkt->enc_part2);
1231 }
1232 
1233 void
ktest_destroy_enc_data(krb5_enc_data * ed)1234 ktest_destroy_enc_data(krb5_enc_data *ed)
1235 {
1236     ktest_empty_data(&ed->ciphertext);
1237     ed->kvno = 0;
1238 }
1239 
1240 void
ktest_destroy_etype_info_entry(krb5_etype_info_entry * i)1241 ktest_destroy_etype_info_entry(krb5_etype_info_entry *i)
1242 {
1243     if (i->salt)
1244         free(i->salt);
1245     ktest_empty_data(&i->s2kparams);
1246     free(i);
1247 }
1248 
1249 void
ktest_destroy_etype_info(krb5_etype_info_entry ** info)1250 ktest_destroy_etype_info(krb5_etype_info_entry **info)
1251 {
1252     int i;
1253 
1254     for (i = 0; info[i] != NULL; i++)
1255         ktest_destroy_etype_info_entry(info[i]);
1256     free(info);
1257 }
1258 
1259 void
ktest_empty_kdc_req(krb5_kdc_req * kr)1260 ktest_empty_kdc_req(krb5_kdc_req *kr)
1261 {
1262     if (kr->padata)
1263         ktest_destroy_pa_data_array(&kr->padata);
1264 
1265     if (kr->client)
1266         ktest_destroy_principal(&kr->client);
1267 
1268     if (kr->server)
1269         ktest_destroy_principal(&kr->server);
1270     free(kr->ktype);
1271     if (kr->addresses)
1272         ktest_destroy_addresses(&kr->addresses);
1273     ktest_destroy_enc_data(&kr->authorization_data);
1274     if (kr->unenc_authdata)
1275         ktest_destroy_authorization_data(&kr->unenc_authdata);
1276     if (kr->second_ticket)
1277         ktest_destroy_sequence_of_ticket(&kr->second_ticket);
1278 
1279 }
1280 
1281 void
ktest_empty_kdc_rep(krb5_kdc_rep * kr)1282 ktest_empty_kdc_rep(krb5_kdc_rep *kr)
1283 {
1284     if (kr->padata)
1285         ktest_destroy_pa_data_array(&kr->padata);
1286 
1287     if (kr->client)
1288         ktest_destroy_principal(&kr->client);
1289 
1290     if (kr->ticket)
1291         ktest_destroy_ticket(&kr->ticket);
1292 
1293     ktest_destroy_enc_data(&kr->enc_part);
1294 
1295     if (kr->enc_part2) {
1296         ktest_empty_enc_kdc_rep_part(kr->enc_part2);
1297         free(kr->enc_part2);
1298         kr->enc_part2 = NULL;
1299     }
1300 }
1301 
1302 void
ktest_empty_authenticator(krb5_authenticator * a)1303 ktest_empty_authenticator(krb5_authenticator *a)
1304 {
1305     if (a->client)
1306         ktest_destroy_principal(&a->client);
1307     if (a->checksum)
1308         ktest_destroy_checksum(&a->checksum);
1309     if (a->subkey)
1310         ktest_destroy_keyblock(&a->subkey);
1311     if (a->authorization_data)
1312         ktest_destroy_authorization_data(&a->authorization_data);
1313 }
1314 
1315 void
ktest_empty_enc_tkt_part(krb5_enc_tkt_part * etp)1316 ktest_empty_enc_tkt_part(krb5_enc_tkt_part *etp)
1317 {
1318     if (etp->session)
1319         ktest_destroy_keyblock(&etp->session);
1320     if (etp->client)
1321         ktest_destroy_principal(&etp->client);
1322     if (etp->caddrs)
1323         ktest_destroy_addresses(&etp->caddrs);
1324     if (etp->authorization_data)
1325         ktest_destroy_authorization_data(&etp->authorization_data);
1326     ktest_destroy_transited(&etp->transited);
1327 }
1328 
1329 void
ktest_destroy_enc_tkt_part(krb5_enc_tkt_part ** etp)1330 ktest_destroy_enc_tkt_part(krb5_enc_tkt_part **etp)
1331 {
1332     if (*etp) {
1333         ktest_empty_enc_tkt_part(*etp);
1334         free(*etp);
1335         *etp = NULL;
1336     }
1337 }
1338 
1339 void
ktest_empty_enc_kdc_rep_part(krb5_enc_kdc_rep_part * ekr)1340 ktest_empty_enc_kdc_rep_part(krb5_enc_kdc_rep_part *ekr)
1341 {
1342     if (ekr->session)
1343         ktest_destroy_keyblock(&ekr->session);
1344 
1345     if (ekr->server)
1346         ktest_destroy_principal(&ekr->server);
1347 
1348     if (ekr->caddrs)
1349         ktest_destroy_addresses(&ekr->caddrs);
1350     ktest_destroy_last_req(&ekr->last_req);
1351 }
1352 
1353 void
ktest_destroy_transited(krb5_transited * t)1354 ktest_destroy_transited(krb5_transited *t)
1355 {
1356     if (t->tr_contents.data)
1357         ktest_empty_data(&t->tr_contents);
1358 }
1359 
1360 void
ktest_empty_ap_rep(krb5_ap_rep * ar)1361 ktest_empty_ap_rep(krb5_ap_rep *ar)
1362 {
1363     ktest_destroy_enc_data(&ar->enc_part);
1364 }
1365 
1366 void
ktest_empty_ap_req(krb5_ap_req * ar)1367 ktest_empty_ap_req(krb5_ap_req *ar)
1368 {
1369     if (ar->ticket)
1370         ktest_destroy_ticket(&ar->ticket);
1371     ktest_destroy_enc_data(&ar->authenticator);
1372 }
1373 
1374 void
ktest_empty_cred_enc_part(krb5_cred_enc_part * cep)1375 ktest_empty_cred_enc_part(krb5_cred_enc_part *cep)
1376 {
1377     if (cep->s_address)
1378         ktest_destroy_address(&cep->s_address);
1379     if (cep->r_address)
1380         ktest_destroy_address(&cep->r_address);
1381     if (cep->ticket_info)
1382         ktest_destroy_sequence_of_cred_info(&cep->ticket_info);
1383 }
1384 
1385 void
ktest_destroy_cred_info(krb5_cred_info ** ci)1386 ktest_destroy_cred_info(krb5_cred_info **ci)
1387 {
1388     if ((*ci)->session)
1389         ktest_destroy_keyblock(&(*ci)->session);
1390     if ((*ci)->client)
1391         ktest_destroy_principal(&(*ci)->client);
1392     if ((*ci)->server)
1393         ktest_destroy_principal(&(*ci)->server);
1394     if ((*ci)->caddrs)
1395         ktest_destroy_addresses(&(*ci)->caddrs);
1396     free(*ci);
1397     *ci = NULL;
1398 }
1399 
1400 void
ktest_destroy_sequence_of_cred_info(krb5_cred_info *** soci)1401 ktest_destroy_sequence_of_cred_info(krb5_cred_info ***soci)
1402 {
1403     int i;
1404 
1405     for (i = 0; (*soci)[i] != NULL; i++)
1406         ktest_destroy_cred_info(&(*soci)[i]);
1407     free(*soci);
1408     *soci = NULL;
1409 }
1410 
1411 void
ktest_empty_safe(krb5_safe * s)1412 ktest_empty_safe(krb5_safe *s)
1413 {
1414     ktest_empty_data(&s->user_data);
1415     ktest_destroy_address(&s->s_address);
1416     ktest_destroy_address(&s->r_address);
1417     ktest_destroy_checksum(&s->checksum);
1418 }
1419 
1420 void
ktest_empty_priv_enc_part(krb5_priv_enc_part * pep)1421 ktest_empty_priv_enc_part(krb5_priv_enc_part *pep)
1422 {
1423     ktest_empty_data(&pep->user_data);
1424     ktest_destroy_address(&pep->s_address);
1425     ktest_destroy_address(&pep->r_address);
1426 }
1427 
1428 void
ktest_empty_priv(krb5_priv * p)1429 ktest_empty_priv(krb5_priv *p)
1430 {
1431     ktest_destroy_enc_data(&p->enc_part);
1432 }
1433 
1434 void
ktest_empty_cred(krb5_cred * c)1435 ktest_empty_cred(krb5_cred *c)
1436 {
1437     ktest_destroy_sequence_of_ticket(&c->tickets);
1438     ktest_destroy_enc_data(&c->enc_part);
1439     /* enc_part2 */
1440 }
1441 
1442 void
ktest_destroy_last_req(krb5_last_req_entry *** lr)1443 ktest_destroy_last_req(krb5_last_req_entry ***lr)
1444 {
1445     int i;
1446 
1447     if (*lr) {
1448         for (i=0; (*lr)[i] != NULL; i++)
1449             free((*lr)[i]);
1450 
1451         free(*lr);
1452     }
1453 }
1454 
1455 void
ktest_empty_error(krb5_error * kerr)1456 ktest_empty_error(krb5_error *kerr)
1457 {
1458     if (kerr->client)
1459         ktest_destroy_principal(&kerr->client);
1460     if (kerr->server)
1461         ktest_destroy_principal(&kerr->server);
1462     ktest_empty_data(&kerr->text);
1463     ktest_empty_data(&kerr->e_data);
1464 }
1465 
1466 void
ktest_empty_ap_rep_enc_part(krb5_ap_rep_enc_part * arep)1467 ktest_empty_ap_rep_enc_part(krb5_ap_rep_enc_part *arep)
1468 {
1469     ktest_destroy_keyblock(&(arep)->subkey);
1470 }
1471 
1472 void
ktest_empty_sam_challenge_2(krb5_sam_challenge_2 * p)1473 ktest_empty_sam_challenge_2(krb5_sam_challenge_2 *p)
1474 {
1475     krb5_checksum **ck;
1476 
1477     ktest_empty_data(&p->sam_challenge_2_body);
1478     if (p->sam_cksum != NULL) {
1479         for (ck = p->sam_cksum; *ck != NULL; ck++)
1480             ktest_destroy_checksum(ck);
1481         free(p->sam_cksum);
1482         p->sam_cksum = NULL;
1483     }
1484 }
1485 
1486 void
ktest_empty_sam_challenge_2_body(krb5_sam_challenge_2_body * p)1487 ktest_empty_sam_challenge_2_body(krb5_sam_challenge_2_body *p)
1488 {
1489     ktest_empty_data(&p->sam_type_name);
1490     ktest_empty_data(&p->sam_track_id);
1491     ktest_empty_data(&p->sam_challenge_label);
1492     ktest_empty_data(&p->sam_challenge);
1493     ktest_empty_data(&p->sam_response_prompt);
1494     ktest_empty_data(&p->sam_pk_for_sad);
1495 }
1496 
1497 void
ktest_empty_sam_response_2(krb5_sam_response_2 * p)1498 ktest_empty_sam_response_2(krb5_sam_response_2 *p)
1499 {
1500     ktest_empty_data(&p->sam_track_id);
1501     ktest_empty_data(&p->sam_enc_nonce_or_sad.ciphertext);
1502 }
1503 
1504 void
ktest_empty_enc_sam_response_enc_2(krb5_enc_sam_response_enc_2 * p)1505 ktest_empty_enc_sam_response_enc_2(krb5_enc_sam_response_enc_2 *p)
1506 {
1507     ktest_empty_data(&p->sam_sad);
1508 }
1509 
1510 void
ktest_empty_pa_for_user(krb5_pa_for_user * p)1511 ktest_empty_pa_for_user(krb5_pa_for_user *p)
1512 {
1513     ktest_destroy_principal(&p->user);
1514     ktest_empty_checksum(&p->cksum);
1515     ktest_empty_data(&p->auth_package);
1516 }
1517 
1518 void
ktest_empty_pa_s4u_x509_user(krb5_pa_s4u_x509_user * p)1519 ktest_empty_pa_s4u_x509_user(krb5_pa_s4u_x509_user *p)
1520 {
1521     ktest_destroy_principal(&p->user_id.user);
1522     ktest_empty_data(&p->user_id.subject_cert);
1523     free(p->cksum.contents);
1524 }
1525 
1526 void
ktest_empty_ad_kdcissued(krb5_ad_kdcissued * p)1527 ktest_empty_ad_kdcissued(krb5_ad_kdcissued *p)
1528 {
1529     free(p->ad_checksum.contents);
1530     ktest_destroy_principal(&p->i_principal);
1531     ktest_destroy_authorization_data(&p->elements);
1532 }
1533 
1534 void
ktest_empty_ad_signedpath_data(krb5_ad_signedpath_data * p)1535 ktest_empty_ad_signedpath_data(krb5_ad_signedpath_data *p)
1536 {
1537     int i;
1538 
1539     ktest_destroy_principal(&p->client);
1540     if (p->delegated != NULL) {
1541         for (i = 0; p->delegated[i] != NULL; i++) {
1542             krb5_principal princ = p->delegated[i];
1543             ktest_destroy_principal(&princ);
1544         }
1545         free(p->delegated);
1546     }
1547     ktest_destroy_pa_data_array(&p->method_data);
1548     ktest_destroy_authorization_data(&p->authorization_data);
1549 }
1550 
1551 void
ktest_empty_ad_signedpath(krb5_ad_signedpath * p)1552 ktest_empty_ad_signedpath(krb5_ad_signedpath *p)
1553 {
1554     int i;
1555 
1556     free(p->checksum.contents);
1557     if (p->delegated != NULL) {
1558         for (i = 0; p->delegated[i] != NULL; i++) {
1559             krb5_principal princ = p->delegated[i];
1560             ktest_destroy_principal(&princ);
1561         }
1562         free(p->delegated);
1563     }
1564     ktest_destroy_pa_data_array(&p->method_data);
1565 }
1566 
1567 void
ktest_empty_iakerb_header(krb5_iakerb_header * p)1568 ktest_empty_iakerb_header(krb5_iakerb_header *p)
1569 {
1570     krb5_free_data_contents(NULL, &p->target_realm);
1571     krb5_free_data(NULL, p->cookie);
1572 }
1573 
1574 void
ktest_empty_iakerb_finished(krb5_iakerb_finished * p)1575 ktest_empty_iakerb_finished(krb5_iakerb_finished *p)
1576 {
1577     krb5_free_checksum_contents(NULL, &p->checksum);
1578 }
1579 
1580 static void
ktest_empty_fast_finished(krb5_fast_finished * p)1581 ktest_empty_fast_finished(krb5_fast_finished *p)
1582 {
1583     ktest_destroy_principal(&p->client);
1584     ktest_empty_checksum(&p->ticket_checksum);
1585 }
1586 
1587 void
ktest_empty_fast_response(krb5_fast_response * p)1588 ktest_empty_fast_response(krb5_fast_response *p)
1589 {
1590     ktest_destroy_pa_data_array(&p->padata);
1591     ktest_destroy_keyblock(&p->strengthen_key);
1592     if (p->finished != NULL) {
1593         ktest_empty_fast_finished(p->finished);
1594         free(p->finished);
1595         p->finished = NULL;
1596     }
1597 }
1598 
1599 static void
ktest_empty_algorithm_identifier(krb5_algorithm_identifier * p)1600 ktest_empty_algorithm_identifier(krb5_algorithm_identifier *p)
1601 {
1602     ktest_empty_data(&p->algorithm);
1603     ktest_empty_data(&p->parameters);
1604 }
1605 
1606 void
ktest_empty_otp_tokeninfo(krb5_otp_tokeninfo * p)1607 ktest_empty_otp_tokeninfo(krb5_otp_tokeninfo *p)
1608 {
1609     krb5_algorithm_identifier **alg;
1610 
1611     p->flags = 0;
1612     krb5_free_data_contents(NULL, &p->vendor);
1613     krb5_free_data_contents(NULL, &p->challenge);
1614     krb5_free_data_contents(NULL, &p->token_id);
1615     krb5_free_data_contents(NULL, &p->alg_id);
1616     for (alg = p->supported_hash_alg; alg != NULL && *alg != NULL; alg++) {
1617         ktest_empty_algorithm_identifier(*alg);
1618         free(*alg);
1619     }
1620     free(p->supported_hash_alg);
1621     p->supported_hash_alg = NULL;
1622     p->length = p->format = p->iteration_count = -1;
1623 }
1624 
1625 void
ktest_empty_pa_otp_challenge(krb5_pa_otp_challenge * p)1626 ktest_empty_pa_otp_challenge(krb5_pa_otp_challenge *p)
1627 {
1628     krb5_otp_tokeninfo **ti;
1629 
1630     krb5_free_data_contents(NULL, &p->nonce);
1631     krb5_free_data_contents(NULL, &p->service);
1632     for (ti = p->tokeninfo; *ti != NULL; ti++) {
1633         ktest_empty_otp_tokeninfo(*ti);
1634         free(*ti);
1635     }
1636     free(p->tokeninfo);
1637     p->tokeninfo = NULL;
1638     krb5_free_data_contents(NULL, &p->salt);
1639     krb5_free_data_contents(NULL, &p->s2kparams);
1640 }
1641 
1642 void
ktest_empty_pa_otp_req(krb5_pa_otp_req * p)1643 ktest_empty_pa_otp_req(krb5_pa_otp_req *p)
1644 {
1645     p->flags = 0;
1646     krb5_free_data_contents(NULL, &p->nonce);
1647     ktest_destroy_enc_data(&p->enc_data);
1648     if (p->hash_alg != NULL)
1649         ktest_empty_algorithm_identifier(p->hash_alg);
1650     free(p->hash_alg);
1651     p->hash_alg = NULL;
1652     p->iteration_count = -1;
1653     krb5_free_data_contents(NULL, &p->otp_value);
1654     krb5_free_data_contents(NULL, &p->pin);
1655     krb5_free_data_contents(NULL, &p->challenge);
1656     p->time = 0;
1657     krb5_free_data_contents(NULL, &p->counter);
1658     p->format = -1;
1659     krb5_free_data_contents(NULL, &p->token_id);
1660     krb5_free_data_contents(NULL, &p->alg_id);
1661     krb5_free_data_contents(NULL, &p->vendor);
1662 }
1663 
1664 #ifndef DISABLE_PKINIT
1665 
1666 static void
ktest_empty_pk_authenticator(krb5_pk_authenticator * p)1667 ktest_empty_pk_authenticator(krb5_pk_authenticator *p)
1668 {
1669     ktest_empty_checksum(&p->paChecksum);
1670     p->paChecksum.contents = NULL;
1671     krb5_free_data(NULL, p->freshnessToken);
1672     p->freshnessToken = NULL;
1673 }
1674 
1675 static void
ktest_empty_subject_pk_info(krb5_subject_pk_info * p)1676 ktest_empty_subject_pk_info(krb5_subject_pk_info *p)
1677 {
1678     ktest_empty_algorithm_identifier(&p->algorithm);
1679     ktest_empty_data(&p->subjectPublicKey);
1680 }
1681 
1682 static void
ktest_empty_external_principal_identifier(krb5_external_principal_identifier * p)1683 ktest_empty_external_principal_identifier(
1684     krb5_external_principal_identifier *p)
1685 {
1686     ktest_empty_data(&p->subjectName);
1687     ktest_empty_data(&p->issuerAndSerialNumber);
1688     ktest_empty_data(&p->subjectKeyIdentifier);
1689 }
1690 
1691 void
ktest_empty_pa_pk_as_req(krb5_pa_pk_as_req * p)1692 ktest_empty_pa_pk_as_req(krb5_pa_pk_as_req *p)
1693 {
1694     krb5_external_principal_identifier **pi;
1695 
1696     ktest_empty_data(&p->signedAuthPack);
1697     for (pi = p->trustedCertifiers; *pi != NULL; pi++) {
1698         ktest_empty_external_principal_identifier(*pi);
1699         free(*pi);
1700     }
1701     free(p->trustedCertifiers);
1702     p->trustedCertifiers = NULL;
1703     ktest_empty_data(&p->kdcPkId);
1704 }
1705 
1706 static void
ktest_empty_dh_rep_info(krb5_dh_rep_info * p)1707 ktest_empty_dh_rep_info(krb5_dh_rep_info *p)
1708 {
1709     ktest_empty_data(&p->dhSignedData);
1710     ktest_empty_data(&p->serverDHNonce);
1711     ktest_destroy_data(&p->kdfID);
1712 }
1713 
1714 void
ktest_empty_pa_pk_as_rep(krb5_pa_pk_as_rep * p)1715 ktest_empty_pa_pk_as_rep(krb5_pa_pk_as_rep *p)
1716 {
1717     if (p->choice == choice_pa_pk_as_rep_dhInfo)
1718         ktest_empty_dh_rep_info(&p->u.dh_Info);
1719     else if (p->choice == choice_pa_pk_as_rep_encKeyPack)
1720         ktest_empty_data(&p->u.encKeyPack);
1721     p->choice = choice_pa_pk_as_rep_UNKNOWN;
1722 }
1723 
1724 void
ktest_empty_auth_pack(krb5_auth_pack * p)1725 ktest_empty_auth_pack(krb5_auth_pack *p)
1726 {
1727     krb5_algorithm_identifier **ai;
1728     krb5_data **d;
1729 
1730     ktest_empty_pk_authenticator(&p->pkAuthenticator);
1731     if (p->clientPublicValue != NULL) {
1732         ktest_empty_subject_pk_info(p->clientPublicValue);
1733         free(p->clientPublicValue);
1734         p->clientPublicValue = NULL;
1735     }
1736     if (p->supportedCMSTypes != NULL) {
1737         for (ai = p->supportedCMSTypes; *ai != NULL; ai++) {
1738             ktest_empty_algorithm_identifier(*ai);
1739             free(*ai);
1740         }
1741         free(p->supportedCMSTypes);
1742         p->supportedCMSTypes = NULL;
1743     }
1744     ktest_empty_data(&p->clientDHNonce);
1745     if (p->supportedKDFs != NULL) {
1746         for (d = p->supportedKDFs; *d != NULL; d++) {
1747             ktest_empty_data(*d);
1748             free(*d);
1749         }
1750         free(p->supportedKDFs);
1751         p->supportedKDFs = NULL;
1752     }
1753 }
1754 
1755 void
ktest_empty_kdc_dh_key_info(krb5_kdc_dh_key_info * p)1756 ktest_empty_kdc_dh_key_info(krb5_kdc_dh_key_info *p)
1757 {
1758     ktest_empty_data(&p->subjectPublicKey);
1759 }
1760 
1761 void
ktest_empty_reply_key_pack(krb5_reply_key_pack * p)1762 ktest_empty_reply_key_pack(krb5_reply_key_pack *p)
1763 {
1764     ktest_empty_keyblock(&p->replyKey);
1765     ktest_empty_checksum(&p->asChecksum);
1766 }
1767 
ktest_empty_sp80056a_other_info(krb5_sp80056a_other_info * p)1768 void ktest_empty_sp80056a_other_info(krb5_sp80056a_other_info *p)
1769 {
1770     ktest_empty_algorithm_identifier(&p->algorithm_identifier);
1771     ktest_destroy_principal(&p->party_u_info);
1772     ktest_destroy_principal(&p->party_v_info);
1773     ktest_empty_data(&p->supp_pub_info);
1774 }
1775 
ktest_empty_pkinit_supp_pub_info(krb5_pkinit_supp_pub_info * p)1776 void ktest_empty_pkinit_supp_pub_info(krb5_pkinit_supp_pub_info *p)
1777 {
1778     ktest_empty_data(&p->as_req);
1779     ktest_empty_data(&p->pk_as_rep);
1780 }
1781 
1782 #endif /* not DISABLE_PKINIT */
1783 
1784 #ifdef ENABLE_LDAP
1785 void
ktest_empty_ldap_seqof_key_data(krb5_context ctx,ldap_seqof_key_data * p)1786 ktest_empty_ldap_seqof_key_data(krb5_context ctx, ldap_seqof_key_data *p)
1787 {
1788     int i;
1789 
1790     for (i = 0; i < p->n_key_data; i++) {
1791         free(p->key_data[i].key_data_contents[0]);
1792         free(p->key_data[i].key_data_contents[1]);
1793     }
1794     free(p->key_data);
1795 }
1796 #endif
1797 
1798 void
ktest_empty_kkdcp_message(krb5_kkdcp_message * p)1799 ktest_empty_kkdcp_message(krb5_kkdcp_message *p)
1800 {
1801     ktest_empty_data(&p->kerb_message);
1802     ktest_empty_data(&p->target_domain);
1803     p->dclocator_hint = -1;
1804 }
1805 
1806 static void
destroy_verifier_mac(krb5_verifier_mac ** vmac)1807 destroy_verifier_mac(krb5_verifier_mac **vmac)
1808 {
1809     if (*vmac == NULL)
1810         return;
1811     ktest_destroy_principal(&(*vmac)->princ);
1812     ktest_empty_checksum(&(*vmac)->checksum);
1813     free(*vmac);
1814     *vmac = NULL;
1815 }
1816 
1817 void
ktest_empty_cammac(krb5_cammac * p)1818 ktest_empty_cammac(krb5_cammac *p)
1819 {
1820     krb5_verifier_mac **vmacp;
1821 
1822     ktest_destroy_authorization_data(&p->elements);
1823     destroy_verifier_mac(&p->kdc_verifier);
1824     destroy_verifier_mac(&p->svc_verifier);
1825     for (vmacp = p->other_verifiers; vmacp != NULL && *vmacp != NULL; vmacp++)
1826         destroy_verifier_mac(vmacp);
1827     free(p->other_verifiers);
1828     p->other_verifiers = NULL;
1829 }
1830 
1831 void
ktest_empty_secure_cookie(krb5_secure_cookie * p)1832 ktest_empty_secure_cookie(krb5_secure_cookie *p)
1833 {
1834     ktest_empty_pa_data_array(p->data);
1835 }
1836 
1837 void
ktest_empty_spake_factor(krb5_spake_factor * p)1838 ktest_empty_spake_factor(krb5_spake_factor *p)
1839 {
1840     krb5_free_data(NULL, p->data);
1841     p->data = NULL;
1842 }
1843 
1844 void
ktest_empty_pa_spake(krb5_pa_spake * p)1845 ktest_empty_pa_spake(krb5_pa_spake *p)
1846 {
1847     krb5_spake_factor **f;
1848 
1849     switch (p->choice) {
1850     case SPAKE_MSGTYPE_SUPPORT:
1851         free(p->u.support.groups);
1852         break;
1853     case SPAKE_MSGTYPE_CHALLENGE:
1854         ktest_empty_data(&p->u.challenge.pubkey);
1855         for (f = p->u.challenge.factors; *f != NULL; f++) {
1856             ktest_empty_spake_factor(*f);
1857             free(*f);
1858         }
1859         free(p->u.challenge.factors);
1860         break;
1861     case SPAKE_MSGTYPE_RESPONSE:
1862         ktest_empty_data(&p->u.response.pubkey);
1863         ktest_destroy_enc_data(&p->u.response.factor);
1864         break;
1865     case SPAKE_MSGTYPE_ENCDATA:
1866         ktest_destroy_enc_data(&p->u.encdata);
1867         break;
1868     default:
1869         break;
1870     }
1871     p->choice = SPAKE_MSGTYPE_UNKNOWN;
1872 }
1873