Add PKINIT decoder tests
[krb5.git] / src / tests / asn.1 / ktest.c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
251 ktest_make_sample_ap_rep(krb5_ap_rep *ar)
252 {
253     ktest_make_sample_enc_data(&ar->enc_part);
254 }
255
256 void
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
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
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
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
324 ktest_make_sample_priv(krb5_priv *p)
325 {
326     ktest_make_sample_enc_data(&p->enc_part);
327 }
328
329 void
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
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
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
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
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
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
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
418 ktest_make_sample_data(krb5_data *d)
419 {
420     krb5_data_parse(d, "krb5data");
421 }
422
423 void
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
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
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
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
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_DES_CBC_CRC;
511 }
512
513 void
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_DES_CBC_CRC;
522     p->sam_enc_nonce_or_sad.kvno = 3382;
523     p->sam_nonce = 0x543210;
524 }
525
526 void
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
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
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
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
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
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
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
593 ktest_make_sample_iakerb_finished(krb5_iakerb_finished *ih)
594 {
595     ktest_make_sample_checksum(&ih->checksum);
596 }
597
598 static void
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
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 #ifndef DISABLE_PKINIT
619
620 static void
621 ktest_make_sample_pk_authenticator(krb5_pk_authenticator *p)
622 {
623     p->cusec = SAMPLE_USEC;
624     p->ctime = SAMPLE_TIME;
625     p->nonce = SAMPLE_NONCE;
626     ktest_make_sample_checksum(&p->paChecksum);
627     /* We don't encode the checksum type, only the contents. */
628     p->paChecksum.checksum_type = 0;
629 }
630
631 static void
632 ktest_make_sample_pk_authenticator_draft9(krb5_pk_authenticator_draft9 *p)
633 {
634     ktest_make_sample_principal(&p->kdcName);
635     p->cusec = SAMPLE_USEC;
636     p->ctime = SAMPLE_TIME;
637     p->nonce = SAMPLE_NONCE;
638 }
639
640 static void
641 ktest_make_sample_oid(krb5_data *p)
642 {
643     krb5_data_parse(p, "\052\206\110\206\367\022\001\002\002");
644 }
645
646 static void
647 ktest_make_sample_algorithm_identifier(krb5_algorithm_identifier *p)
648 {
649     ktest_make_sample_oid(&p->algorithm);
650     /* Need a valid DER encoding here; this is the OCTET STRING "params". */
651     krb5_data_parse(&p->parameters, "\x04\x06" "params");
652 }
653
654 static void
655 ktest_make_sample_algorithm_identifier_no_params(krb5_algorithm_identifier *p)
656 {
657     ktest_make_sample_oid(&p->algorithm);
658     p->parameters = empty_data();
659 }
660
661 static void
662 ktest_make_sample_subject_pk_info(krb5_subject_pk_info *p)
663 {
664     ktest_make_sample_algorithm_identifier(&p->algorithm);
665     ktest_make_sample_data(&p->subjectPublicKey);
666 }
667
668 static void
669 ktest_make_sample_external_principal_identifier(
670     krb5_external_principal_identifier *p)
671 {
672     ktest_make_sample_data(&p->subjectName);
673     ktest_make_sample_data(&p->issuerAndSerialNumber);
674     ktest_make_sample_data(&p->subjectKeyIdentifier);
675 }
676
677 static void
678 ktest_make_sample_trusted_ca_principalName(krb5_trusted_ca *p)
679 {
680     p->choice = choice_trusted_cas_principalName;
681     ktest_make_sample_principal(&p->u.principalName);
682 }
683
684 static void
685 ktest_make_sample_trusted_ca_caName(krb5_trusted_ca *p)
686 {
687     p->choice = choice_trusted_cas_caName;
688     ktest_make_sample_data(&p->u.caName);
689 }
690
691 static void
692 ktest_make_sample_trusted_ca_issuerAndSerial(krb5_trusted_ca *p)
693 {
694     p->choice = choice_trusted_cas_issuerAndSerial;
695     ktest_make_sample_data(&p->u.issuerAndSerial);
696 }
697
698 void
699 ktest_make_sample_pa_pk_as_req(krb5_pa_pk_as_req *p)
700 {
701     ktest_make_sample_data(&p->signedAuthPack);
702     p->trustedCertifiers =
703         ealloc(2 * sizeof(krb5_external_principal_identifier *));
704     p->trustedCertifiers[0] =
705         ealloc(sizeof(krb5_external_principal_identifier));
706     ktest_make_sample_external_principal_identifier(p->trustedCertifiers[0]);
707     p->trustedCertifiers[1] = NULL;
708     ktest_make_sample_data(&p->kdcPkId);
709 }
710
711 void
712 ktest_make_sample_pa_pk_as_req_draft9(krb5_pa_pk_as_req_draft9 *p)
713 {
714     int i;
715
716     ktest_make_sample_data(&p->signedAuthPack);
717     p->trustedCertifiers = ealloc(4 * sizeof(krb5_trusted_ca *));
718     for (i = 0; i < 3; i++)
719         p->trustedCertifiers[i] = ealloc(sizeof(krb5_trusted_ca));
720     ktest_make_sample_trusted_ca_principalName(p->trustedCertifiers[0]);
721     ktest_make_sample_trusted_ca_caName(p->trustedCertifiers[1]);
722     ktest_make_sample_trusted_ca_issuerAndSerial(p->trustedCertifiers[2]);
723     p->trustedCertifiers[3] = NULL;
724     ktest_make_sample_data(&p->kdcCert);
725     ktest_make_sample_data(&p->encryptionCert);
726 }
727
728 static void
729 ktest_make_sample_dh_rep_info(krb5_dh_rep_info *p)
730 {
731     ktest_make_sample_data(&p->dhSignedData);
732     ktest_make_sample_data(&p->serverDHNonce);
733     p->kdfID = ealloc(sizeof(krb5_data));
734     ktest_make_sample_data(p->kdfID);
735 }
736
737 void
738 ktest_make_sample_pa_pk_as_rep_dhInfo(krb5_pa_pk_as_rep *p)
739 {
740     p->choice = choice_pa_pk_as_rep_dhInfo;
741     ktest_make_sample_dh_rep_info(&p->u.dh_Info);
742 }
743
744 void
745 ktest_make_sample_pa_pk_as_rep_encKeyPack(krb5_pa_pk_as_rep *p)
746 {
747     p->choice = choice_pa_pk_as_rep_encKeyPack;
748     ktest_make_sample_data(&p->u.encKeyPack);
749 }
750
751 void
752 ktest_make_sample_pa_pk_as_rep_draft9_dhSignedData(krb5_pa_pk_as_rep_draft9 *p)
753 {
754     p->choice = choice_pa_pk_as_rep_draft9_dhSignedData;
755     ktest_make_sample_data(&p->u.dhSignedData);
756 }
757
758 void
759 ktest_make_sample_pa_pk_as_rep_draft9_encKeyPack(krb5_pa_pk_as_rep_draft9 *p)
760 {
761     p->choice = choice_pa_pk_as_rep_draft9_encKeyPack;
762     ktest_make_sample_data(&p->u.encKeyPack);
763 }
764
765 void
766 ktest_make_sample_auth_pack(krb5_auth_pack *p)
767 {
768     ktest_make_sample_pk_authenticator(&p->pkAuthenticator);
769     p->clientPublicValue = ealloc(sizeof(krb5_subject_pk_info));
770     ktest_make_sample_subject_pk_info(p->clientPublicValue);
771     p->supportedCMSTypes = ealloc(3 * sizeof(krb5_algorithm_identifier *));
772     p->supportedCMSTypes[0] = ealloc(sizeof(krb5_algorithm_identifier));
773     ktest_make_sample_algorithm_identifier(p->supportedCMSTypes[0]);
774     p->supportedCMSTypes[1] = ealloc(sizeof(krb5_algorithm_identifier));
775     ktest_make_sample_algorithm_identifier_no_params(p->supportedCMSTypes[1]);
776     p->supportedCMSTypes[2] = NULL;
777     ktest_make_sample_data(&p->clientDHNonce);
778     p->supportedKDFs = ealloc(2 * sizeof(krb5_data ));
779     p->supportedKDFs[0] = ealloc(sizeof(krb5_data));
780     ktest_make_sample_data(p->supportedKDFs[0]);
781     p->supportedKDFs[1] = NULL;
782 }
783
784 void
785 ktest_make_sample_auth_pack_draft9(krb5_auth_pack_draft9 *p)
786 {
787     ktest_make_sample_pk_authenticator_draft9(&p->pkAuthenticator);
788     p->clientPublicValue = ealloc(sizeof(krb5_subject_pk_info));
789     ktest_make_sample_subject_pk_info(p->clientPublicValue);
790 }
791
792 void
793 ktest_make_sample_kdc_dh_key_info(krb5_kdc_dh_key_info *p)
794 {
795     ktest_make_sample_data(&p->subjectPublicKey);
796     p->nonce = SAMPLE_NONCE;
797     p->dhKeyExpiration = SAMPLE_TIME;
798 }
799
800 void
801 ktest_make_sample_reply_key_pack(krb5_reply_key_pack *p)
802 {
803     ktest_make_sample_keyblock(&p->replyKey);
804     ktest_make_sample_checksum(&p->asChecksum);
805 }
806
807 void
808 ktest_make_sample_reply_key_pack_draft9(krb5_reply_key_pack_draft9 *p)
809 {
810     ktest_make_sample_keyblock(&p->replyKey);
811     p->nonce = SAMPLE_NONCE;
812 }
813
814 void
815 ktest_make_sample_sp80056a_other_info(krb5_sp80056a_other_info *p)
816 {
817     ktest_make_sample_algorithm_identifier_no_params(&p->algorithm_identifier);
818     ktest_make_sample_principal(&p->party_u_info);
819     ktest_make_sample_principal(&p->party_v_info);
820     ktest_make_sample_data(&p->supp_pub_info);
821 }
822
823 void
824 ktest_make_sample_pkinit_supp_pub_info(krb5_pkinit_supp_pub_info *p)
825 {
826     p->enctype = ENCTYPE_DES_CBC_CRC;
827     ktest_make_sample_data(&p->as_req);
828     ktest_make_sample_data(&p->pk_as_rep);
829 }
830
831 #endif /* not DISABLE_PKINIT */
832
833 #ifdef ENABLE_LDAP
834 static void
835 ktest_make_sample_key_data(krb5_key_data *p, int i)
836 {
837     char *str;
838     int len;
839
840     p->key_data_ver = 2;
841     p->key_data_kvno = 42;
842     len = asprintf(&str, "key%d", i);
843     if (len < 0)
844         abort();
845     p->key_data_type[0] = 2;
846     p->key_data_length[0] = (unsigned int) len;
847     p->key_data_contents[0] = (krb5_octet *)str;
848     len = asprintf(&str, "salt%d", i);
849     if (len < 0)
850         abort();
851     p->key_data_type[1] = i;
852     p->key_data_length[1] = (unsigned int) len;
853     p->key_data_contents[1] = (krb5_octet *)str;
854 }
855
856 void
857 ktest_make_sample_ldap_seqof_key_data(ldap_seqof_key_data *p)
858 {
859     int i;
860
861     p->mkvno = 14;
862     p->n_key_data = 3;
863     p->key_data = calloc(3,sizeof(krb5_key_data));
864     for (i = 0; i < 3; i++)
865         ktest_make_sample_key_data(&p->key_data[i], i);
866 }
867 #endif
868
869
870 /****************************************************************/
871 /* destructors */
872
873 void
874 ktest_destroy_data(krb5_data **d)
875 {
876     if (*d != NULL) {
877         free((*d)->data);
878         free(*d);
879         *d = NULL;
880     }
881 }
882
883 void
884 ktest_empty_data(krb5_data *d)
885 {
886     if (d->data != NULL) {
887         free(d->data);
888         d->data = NULL;
889         d->length = 0;
890     }
891 }
892
893 static void
894 ktest_empty_checksum(krb5_checksum *cs)
895 {
896     free(cs->contents);
897     cs->contents = NULL;
898 }
899
900 void
901 ktest_destroy_checksum(krb5_checksum **cs)
902 {
903     if (*cs != NULL) {
904         free((*cs)->contents);
905         free(*cs);
906         *cs = NULL;
907     }
908 }
909
910 void
911 ktest_empty_keyblock(krb5_keyblock *kb)
912 {
913     if (kb != NULL) {
914         if (kb->contents) {
915             free(kb->contents);
916             kb->contents = NULL;
917         }
918     }
919 }
920
921 void
922 ktest_destroy_keyblock(krb5_keyblock **kb)
923 {
924     if (*kb != NULL) {
925         free((*kb)->contents);
926         free(*kb);
927         *kb = NULL;
928     }
929 }
930
931 void
932 ktest_empty_authorization_data(krb5_authdata **ad)
933 {
934     int i;
935
936     if (*ad != NULL) {
937         for (i=0; ad[i] != NULL; i++)
938             ktest_destroy_authdata(&ad[i]);
939     }
940 }
941
942 void
943 ktest_destroy_authorization_data(krb5_authdata ***ad)
944 {
945     ktest_empty_authorization_data(*ad);
946     free(*ad);
947     *ad = NULL;
948 }
949
950 void
951 ktest_destroy_authdata(krb5_authdata **ad)
952 {
953     if (*ad != NULL) {
954         free((*ad)->contents);
955         free(*ad);
956         *ad = NULL;
957     }
958 }
959
960 void
961 ktest_empty_pa_data_array(krb5_pa_data **pad)
962 {
963     int i;
964
965     for (i=0; pad[i] != NULL; i++)
966         ktest_destroy_pa_data(&pad[i]);
967 }
968
969 void
970 ktest_destroy_pa_data_array(krb5_pa_data ***pad)
971 {
972     ktest_empty_pa_data_array(*pad);
973     free(*pad);
974     *pad = NULL;
975 }
976
977 void
978 ktest_destroy_pa_data(krb5_pa_data **pad)
979 {
980     if (*pad != NULL) {
981         free((*pad)->contents);
982         free(*pad);
983         *pad = NULL;
984     }
985 }
986
987 void
988 ktest_destroy_address(krb5_address **a)
989 {
990     if (*a != NULL) {
991         free((*a)->contents);
992         free(*a);
993         *a = NULL;
994     }
995 }
996
997 void
998 ktest_empty_addresses(krb5_address **a)
999 {
1000     int i;
1001
1002     for (i=0; a[i] != NULL; i++)
1003         ktest_destroy_address(&a[i]);
1004 }
1005
1006 void
1007 ktest_destroy_addresses(krb5_address ***a)
1008 {
1009     ktest_empty_addresses(*a);
1010     free(*a);
1011     *a = NULL;
1012 }
1013
1014 void
1015 ktest_destroy_principal(krb5_principal *p)
1016 {
1017     int i;
1018
1019     for (i=0; i<(*p)->length; i++)
1020         ktest_empty_data(&(*p)->data[i]);
1021     ktest_empty_data(&(*p)->realm);
1022     free((*p)->data);
1023     free(*p);
1024     *p = NULL;
1025 }
1026
1027 void
1028 ktest_destroy_sequence_of_integer(long **soi)
1029 {
1030     free(*soi);
1031     *soi = NULL;
1032 }
1033
1034 void
1035 ktest_destroy_sequence_of_ticket(krb5_ticket ***sot)
1036 {
1037     int i;
1038
1039     for (i=0; (*sot)[i] != NULL; i++)
1040         ktest_destroy_ticket(&(*sot)[i]);
1041     free(*sot);
1042     *sot = NULL;
1043 }
1044
1045 void
1046 ktest_destroy_ticket(krb5_ticket **tkt)
1047 {
1048     ktest_destroy_principal(&(*tkt)->server);
1049     ktest_destroy_enc_data(&(*tkt)->enc_part);
1050     /*  ktest_empty_enc_tkt_part(((*tkt)->enc_part2));*/
1051     free(*tkt);
1052     *tkt = NULL;
1053 }
1054
1055 void
1056 ktest_empty_ticket(krb5_ticket *tkt)
1057 {
1058     if (tkt->server)
1059         ktest_destroy_principal(&tkt->server);
1060     ktest_destroy_enc_data(&tkt->enc_part);
1061     if (tkt->enc_part2)
1062         ktest_destroy_enc_tkt_part(&tkt->enc_part2);
1063 }
1064
1065 void
1066 ktest_destroy_enc_data(krb5_enc_data *ed)
1067 {
1068     ktest_empty_data(&ed->ciphertext);
1069     ed->kvno = 0;
1070 }
1071
1072 void
1073 ktest_destroy_etype_info_entry(krb5_etype_info_entry *i)
1074 {
1075     if (i->salt)
1076         free(i->salt);
1077     ktest_empty_data(&i->s2kparams);
1078     free(i);
1079 }
1080
1081 void
1082 ktest_destroy_etype_info(krb5_etype_info_entry **info)
1083 {
1084     int i;
1085
1086     for (i = 0; info[i] != NULL; i++)
1087         ktest_destroy_etype_info_entry(info[i]);
1088     free(info);
1089 }
1090
1091 void
1092 ktest_empty_kdc_req(krb5_kdc_req *kr)
1093 {
1094     if (kr->padata)
1095         ktest_destroy_pa_data_array(&kr->padata);
1096
1097     if (kr->client)
1098         ktest_destroy_principal(&kr->client);
1099
1100     if (kr->server)
1101         ktest_destroy_principal(&kr->server);
1102     free(kr->ktype);
1103     if (kr->addresses)
1104         ktest_destroy_addresses(&kr->addresses);
1105     ktest_destroy_enc_data(&kr->authorization_data);
1106     if (kr->unenc_authdata)
1107         ktest_destroy_authorization_data(&kr->unenc_authdata);
1108     if (kr->second_ticket)
1109         ktest_destroy_sequence_of_ticket(&kr->second_ticket);
1110
1111 }
1112
1113 void
1114 ktest_empty_kdc_rep(krb5_kdc_rep *kr)
1115 {
1116     if (kr->padata)
1117         ktest_destroy_pa_data_array(&kr->padata);
1118
1119     if (kr->client)
1120         ktest_destroy_principal(&kr->client);
1121
1122     if (kr->ticket)
1123         ktest_destroy_ticket(&kr->ticket);
1124
1125     ktest_destroy_enc_data(&kr->enc_part);
1126
1127     if (kr->enc_part2) {
1128         ktest_empty_enc_kdc_rep_part(kr->enc_part2);
1129         free(kr->enc_part2);
1130         kr->enc_part2 = NULL;
1131     }
1132 }
1133
1134 void
1135 ktest_empty_authenticator(krb5_authenticator *a)
1136 {
1137     if (a->client)
1138         ktest_destroy_principal(&a->client);
1139     if (a->checksum)
1140         ktest_destroy_checksum(&a->checksum);
1141     if (a->subkey)
1142         ktest_destroy_keyblock(&a->subkey);
1143     if (a->authorization_data)
1144         ktest_destroy_authorization_data(&a->authorization_data);
1145 }
1146
1147 void
1148 ktest_empty_enc_tkt_part(krb5_enc_tkt_part *etp)
1149 {
1150     if (etp->session)
1151         ktest_destroy_keyblock(&etp->session);
1152     if (etp->client)
1153         ktest_destroy_principal(&etp->client);
1154     if (etp->caddrs)
1155         ktest_destroy_addresses(&etp->caddrs);
1156     if (etp->authorization_data)
1157         ktest_destroy_authorization_data(&etp->authorization_data);
1158     ktest_destroy_transited(&etp->transited);
1159 }
1160
1161 void
1162 ktest_destroy_enc_tkt_part(krb5_enc_tkt_part **etp)
1163 {
1164     if (*etp) {
1165         ktest_empty_enc_tkt_part(*etp);
1166         free(*etp);
1167         *etp = NULL;
1168     }
1169 }
1170
1171 void
1172 ktest_empty_enc_kdc_rep_part(krb5_enc_kdc_rep_part *ekr)
1173 {
1174     if (ekr->session)
1175         ktest_destroy_keyblock(&ekr->session);
1176
1177     if (ekr->server)
1178         ktest_destroy_principal(&ekr->server);
1179
1180     if (ekr->caddrs)
1181         ktest_destroy_addresses(&ekr->caddrs);
1182     ktest_destroy_last_req(&ekr->last_req);
1183 }
1184
1185 void
1186 ktest_destroy_transited(krb5_transited *t)
1187 {
1188     if (t->tr_contents.data)
1189         ktest_empty_data(&t->tr_contents);
1190 }
1191
1192 void
1193 ktest_empty_ap_rep(krb5_ap_rep *ar)
1194 {
1195     ktest_destroy_enc_data(&ar->enc_part);
1196 }
1197
1198 void
1199 ktest_empty_ap_req(krb5_ap_req *ar)
1200 {
1201     if (ar->ticket)
1202         ktest_destroy_ticket(&ar->ticket);
1203     ktest_destroy_enc_data(&ar->authenticator);
1204 }
1205
1206 void
1207 ktest_empty_cred_enc_part(krb5_cred_enc_part *cep)
1208 {
1209     if (cep->s_address)
1210         ktest_destroy_address(&cep->s_address);
1211     if (cep->r_address)
1212         ktest_destroy_address(&cep->r_address);
1213     if (cep->ticket_info)
1214         ktest_destroy_sequence_of_cred_info(&cep->ticket_info);
1215 }
1216
1217 void
1218 ktest_destroy_cred_info(krb5_cred_info **ci)
1219 {
1220     if ((*ci)->session)
1221         ktest_destroy_keyblock(&(*ci)->session);
1222     if ((*ci)->client)
1223         ktest_destroy_principal(&(*ci)->client);
1224     if ((*ci)->server)
1225         ktest_destroy_principal(&(*ci)->server);
1226     if ((*ci)->caddrs)
1227         ktest_destroy_addresses(&(*ci)->caddrs);
1228     free(*ci);
1229     *ci = NULL;
1230 }
1231
1232 void
1233 ktest_destroy_sequence_of_cred_info(krb5_cred_info ***soci)
1234 {
1235     int i;
1236
1237     for (i = 0; (*soci)[i] != NULL; i++)
1238         ktest_destroy_cred_info(&(*soci)[i]);
1239     free(*soci);
1240     *soci = NULL;
1241 }
1242
1243 void
1244 ktest_empty_safe(krb5_safe *s)
1245 {
1246     ktest_empty_data(&s->user_data);
1247     ktest_destroy_address(&s->s_address);
1248     ktest_destroy_address(&s->r_address);
1249     ktest_destroy_checksum(&s->checksum);
1250 }
1251
1252 void
1253 ktest_empty_priv_enc_part(krb5_priv_enc_part *pep)
1254 {
1255     ktest_empty_data(&pep->user_data);
1256     ktest_destroy_address(&pep->s_address);
1257     ktest_destroy_address(&pep->r_address);
1258 }
1259
1260 void
1261 ktest_empty_priv(krb5_priv *p)
1262 {
1263     ktest_destroy_enc_data(&p->enc_part);
1264 }
1265
1266 void
1267 ktest_empty_cred(krb5_cred *c)
1268 {
1269     ktest_destroy_sequence_of_ticket(&c->tickets);
1270     ktest_destroy_enc_data(&c->enc_part);
1271     /* enc_part2 */
1272 }
1273
1274 void
1275 ktest_destroy_last_req(krb5_last_req_entry ***lr)
1276 {
1277     int i;
1278
1279     if (*lr) {
1280         for (i=0; (*lr)[i] != NULL; i++)
1281             free((*lr)[i]);
1282
1283         free(*lr);
1284     }
1285 }
1286
1287 void
1288 ktest_empty_error(krb5_error *kerr)
1289 {
1290     if (kerr->client)
1291         ktest_destroy_principal(&kerr->client);
1292     if (kerr->server)
1293         ktest_destroy_principal(&kerr->server);
1294     ktest_empty_data(&kerr->text);
1295     ktest_empty_data(&kerr->e_data);
1296 }
1297
1298 void
1299 ktest_empty_ap_rep_enc_part(krb5_ap_rep_enc_part *arep)
1300 {
1301     ktest_destroy_keyblock(&(arep)->subkey);
1302 }
1303
1304 void
1305 ktest_empty_sam_challenge_2(krb5_sam_challenge_2 *p)
1306 {
1307     krb5_checksum **ck;
1308
1309     ktest_empty_data(&p->sam_challenge_2_body);
1310     if (p->sam_cksum != NULL) {
1311         for (ck = p->sam_cksum; *ck != NULL; ck++)
1312             ktest_destroy_checksum(ck);
1313         free(p->sam_cksum);
1314         p->sam_cksum = NULL;
1315     }
1316 }
1317
1318 void
1319 ktest_empty_sam_challenge_2_body(krb5_sam_challenge_2_body *p)
1320 {
1321     ktest_empty_data(&p->sam_type_name);
1322     ktest_empty_data(&p->sam_track_id);
1323     ktest_empty_data(&p->sam_challenge_label);
1324     ktest_empty_data(&p->sam_challenge);
1325     ktest_empty_data(&p->sam_response_prompt);
1326     ktest_empty_data(&p->sam_pk_for_sad);
1327 }
1328
1329 void
1330 ktest_empty_sam_response_2(krb5_sam_response_2 *p)
1331 {
1332     ktest_empty_data(&p->sam_track_id);
1333     ktest_empty_data(&p->sam_enc_nonce_or_sad.ciphertext);
1334 }
1335
1336 void
1337 ktest_empty_enc_sam_response_enc_2(krb5_enc_sam_response_enc_2 *p)
1338 {
1339     ktest_empty_data(&p->sam_sad);
1340 }
1341
1342 void
1343 ktest_empty_pa_for_user(krb5_pa_for_user *p)
1344 {
1345     ktest_destroy_principal(&p->user);
1346     ktest_empty_checksum(&p->cksum);
1347     ktest_empty_data(&p->auth_package);
1348 }
1349
1350 void
1351 ktest_empty_pa_s4u_x509_user(krb5_pa_s4u_x509_user *p)
1352 {
1353     ktest_destroy_principal(&p->user_id.user);
1354     ktest_empty_data(&p->user_id.subject_cert);
1355     free(p->cksum.contents);
1356 }
1357
1358 void
1359 ktest_empty_ad_kdcissued(krb5_ad_kdcissued *p)
1360 {
1361     free(p->ad_checksum.contents);
1362     ktest_destroy_principal(&p->i_principal);
1363     ktest_destroy_authorization_data(&p->elements);
1364 }
1365
1366 void
1367 ktest_empty_ad_signedpath_data(krb5_ad_signedpath_data *p)
1368 {
1369     int i;
1370
1371     ktest_destroy_principal(&p->client);
1372     if (p->delegated != NULL) {
1373         for (i = 0; p->delegated[i] != NULL; i++) {
1374             krb5_principal princ = p->delegated[i];
1375             ktest_destroy_principal(&princ);
1376         }
1377         free(p->delegated);
1378     }
1379     ktest_destroy_pa_data_array(&p->method_data);
1380     ktest_destroy_authorization_data(&p->authorization_data);
1381 }
1382
1383 void
1384 ktest_empty_ad_signedpath(krb5_ad_signedpath *p)
1385 {
1386     int i;
1387
1388     free(p->checksum.contents);
1389     if (p->delegated != NULL) {
1390         for (i = 0; p->delegated[i] != NULL; i++) {
1391             krb5_principal princ = p->delegated[i];
1392             ktest_destroy_principal(&princ);
1393         }
1394         free(p->delegated);
1395     }
1396     ktest_destroy_pa_data_array(&p->method_data);
1397 }
1398
1399 void
1400 ktest_empty_iakerb_header(krb5_iakerb_header *p)
1401 {
1402     krb5_free_data_contents(NULL, &p->target_realm);
1403     krb5_free_data(NULL, p->cookie);
1404 }
1405
1406 void
1407 ktest_empty_iakerb_finished(krb5_iakerb_finished *p)
1408 {
1409     krb5_free_checksum_contents(NULL, &p->checksum);
1410 }
1411
1412 static void
1413 ktest_empty_fast_finished(krb5_fast_finished *p)
1414 {
1415     ktest_destroy_principal(&p->client);
1416     ktest_empty_checksum(&p->ticket_checksum);
1417 }
1418
1419 void
1420 ktest_empty_fast_response(krb5_fast_response *p)
1421 {
1422     ktest_destroy_pa_data_array(&p->padata);
1423     ktest_destroy_keyblock(&p->strengthen_key);
1424     if (p->finished != NULL) {
1425         ktest_empty_fast_finished(p->finished);
1426         free(p->finished);
1427         p->finished = NULL;
1428     }
1429 }
1430
1431 #ifndef DISABLE_PKINIT
1432
1433 static void
1434 ktest_empty_pk_authenticator(krb5_pk_authenticator *p)
1435 {
1436     ktest_empty_checksum(&p->paChecksum);
1437     p->paChecksum.contents = NULL;
1438 }
1439
1440 static void
1441 ktest_empty_pk_authenticator_draft9(krb5_pk_authenticator_draft9 *p)
1442 {
1443     ktest_destroy_principal(&p->kdcName);
1444 }
1445
1446 static void
1447 ktest_empty_algorithm_identifier(krb5_algorithm_identifier *p)
1448 {
1449     ktest_empty_data(&p->algorithm);
1450     ktest_empty_data(&p->parameters);
1451 }
1452
1453 static void
1454 ktest_empty_subject_pk_info(krb5_subject_pk_info *p)
1455 {
1456     ktest_empty_algorithm_identifier(&p->algorithm);
1457     ktest_empty_data(&p->subjectPublicKey);
1458 }
1459
1460 static void
1461 ktest_empty_external_principal_identifier(
1462     krb5_external_principal_identifier *p)
1463 {
1464     ktest_empty_data(&p->subjectName);
1465     ktest_empty_data(&p->issuerAndSerialNumber);
1466     ktest_empty_data(&p->subjectKeyIdentifier);
1467 }
1468
1469 static void
1470 ktest_empty_trusted_ca(krb5_trusted_ca *p)
1471 {
1472     if (p->choice == choice_trusted_cas_principalName)
1473         ktest_destroy_principal(&p->u.principalName);
1474     else if (p->choice == choice_trusted_cas_caName)
1475         ktest_empty_data(&p->u.caName);
1476     else if (p->choice == choice_trusted_cas_issuerAndSerial)
1477         ktest_empty_data(&p->u.issuerAndSerial);
1478     p->choice = choice_trusted_cas_UNKNOWN;
1479 }
1480
1481 void
1482 ktest_empty_pa_pk_as_req(krb5_pa_pk_as_req *p)
1483 {
1484     krb5_external_principal_identifier **pi;
1485
1486     ktest_empty_data(&p->signedAuthPack);
1487     for (pi = p->trustedCertifiers; *pi != NULL; pi++) {
1488         ktest_empty_external_principal_identifier(*pi);
1489         free(*pi);
1490     }
1491     free(p->trustedCertifiers);
1492     p->trustedCertifiers = NULL;
1493     ktest_empty_data(&p->kdcPkId);
1494 }
1495
1496 void
1497 ktest_empty_pa_pk_as_req_draft9(krb5_pa_pk_as_req_draft9 *p)
1498 {
1499     krb5_trusted_ca **ca;
1500
1501     ktest_empty_data(&p->signedAuthPack);
1502     if (p->trustedCertifiers != NULL) {
1503         for (ca = p->trustedCertifiers; *ca != NULL; ca++) {
1504             ktest_empty_trusted_ca(*ca);
1505             free(*ca);
1506         }
1507         free(p->trustedCertifiers);
1508         p->trustedCertifiers = NULL;
1509     }
1510     ktest_empty_data(&p->kdcCert);
1511     ktest_empty_data(&p->encryptionCert);
1512 }
1513
1514 static void
1515 ktest_empty_dh_rep_info(krb5_dh_rep_info *p)
1516 {
1517     ktest_empty_data(&p->dhSignedData);
1518     ktest_empty_data(&p->serverDHNonce);
1519     ktest_destroy_data(&p->kdfID);
1520 }
1521
1522 void
1523 ktest_empty_pa_pk_as_rep(krb5_pa_pk_as_rep *p)
1524 {
1525     if (p->choice == choice_pa_pk_as_rep_dhInfo)
1526         ktest_empty_dh_rep_info(&p->u.dh_Info);
1527     else if (p->choice == choice_pa_pk_as_rep_encKeyPack)
1528         ktest_empty_data(&p->u.encKeyPack);
1529     p->choice = choice_pa_pk_as_rep_UNKNOWN;
1530 }
1531
1532 void
1533 ktest_empty_pa_pk_as_rep_draft9(krb5_pa_pk_as_rep_draft9 *p)
1534 {
1535     if (p->choice == choice_pa_pk_as_rep_draft9_dhSignedData)
1536         ktest_empty_data(&p->u.dhSignedData);
1537     else if (p->choice == choice_pa_pk_as_rep_draft9_encKeyPack)
1538         ktest_empty_data(&p->u.encKeyPack);
1539     p->choice = choice_pa_pk_as_rep_draft9_UNKNOWN;
1540 }
1541
1542 void
1543 ktest_empty_auth_pack(krb5_auth_pack *p)
1544 {
1545     krb5_algorithm_identifier **ai;
1546     krb5_data **d;
1547
1548     ktest_empty_pk_authenticator(&p->pkAuthenticator);
1549     if (p->clientPublicValue != NULL) {
1550         ktest_empty_subject_pk_info(p->clientPublicValue);
1551         free(p->clientPublicValue);
1552         p->clientPublicValue = NULL;
1553     }
1554     if (p->supportedCMSTypes != NULL) {
1555         for (ai = p->supportedCMSTypes; *ai != NULL; ai++) {
1556             ktest_empty_algorithm_identifier(*ai);
1557             free(*ai);
1558         }
1559         free(p->supportedCMSTypes);
1560         p->supportedCMSTypes = NULL;
1561     }
1562     ktest_empty_data(&p->clientDHNonce);
1563     if (p->supportedKDFs != NULL) {
1564         for (d = p->supportedKDFs; *d != NULL; d++) {
1565             ktest_empty_data(*d);
1566             free(*d);
1567         }
1568         free(p->supportedKDFs);
1569         p->supportedKDFs = NULL;
1570     }
1571 }
1572
1573 void
1574 ktest_empty_auth_pack_draft9(krb5_auth_pack_draft9 *p)
1575 {
1576     ktest_empty_pk_authenticator_draft9(&p->pkAuthenticator);
1577     if (p->clientPublicValue != NULL) {
1578         ktest_empty_subject_pk_info(p->clientPublicValue);
1579         free(p->clientPublicValue);
1580         p->clientPublicValue = NULL;
1581     }
1582 }
1583
1584 void
1585 ktest_empty_kdc_dh_key_info(krb5_kdc_dh_key_info *p)
1586 {
1587     ktest_empty_data(&p->subjectPublicKey);
1588 }
1589
1590 void
1591 ktest_empty_reply_key_pack(krb5_reply_key_pack *p)
1592 {
1593     ktest_empty_keyblock(&p->replyKey);
1594     ktest_empty_checksum(&p->asChecksum);
1595 }
1596
1597 void
1598 ktest_empty_reply_key_pack_draft9(krb5_reply_key_pack_draft9 *p)
1599 {
1600     ktest_empty_keyblock(&p->replyKey);
1601 }
1602
1603 void ktest_empty_sp80056a_other_info(krb5_sp80056a_other_info *p)
1604 {
1605     ktest_empty_algorithm_identifier(&p->algorithm_identifier);
1606     ktest_destroy_principal(&p->party_u_info);
1607     ktest_destroy_principal(&p->party_v_info);
1608     ktest_empty_data(&p->supp_pub_info);
1609 }
1610
1611 void ktest_empty_pkinit_supp_pub_info(krb5_pkinit_supp_pub_info *p)
1612 {
1613     ktest_empty_data(&p->as_req);
1614     ktest_empty_data(&p->pk_as_rep);
1615 }
1616
1617 #endif /* not DISABLE_PKINIT */
1618
1619 #ifdef ENABLE_LDAP
1620 void
1621 ktest_empty_ldap_seqof_key_data(krb5_context ctx, ldap_seqof_key_data *p)
1622 {
1623     int i;
1624
1625     for (i = 0; i < p->n_key_data; i++) {
1626         free(p->key_data[i].key_data_contents[0]);
1627         free(p->key_data[i].key_data_contents[1]);
1628     }
1629     free(p->key_data);
1630 }
1631 #endif