summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/asn.1/krb5_decode.c
blob: 4172c882bf9429679af6c9609868ca7e06293c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
 * src/lib/krb5/asn.1/krb5_decode.c
 * 
 * Copyright 1994 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "krb5.h"
#include "krbasn1.h"
#include "asn1_k_decode.h"
#include "asn1_decode.h"
#include "asn1_get.h"

/* setup *********************************************************/
/* set up variables */
/* the setup* macros can return, but are always used at function start
   and thus need no malloc cleanup */
#define setup_buf_only()\
asn1_error_code retval;\
asn1buf buf;\
\
retval = asn1buf_wrap_data(&buf,code);\
if(retval) return retval

#define setup_no_tagnum()\
asn1_class asn1class;\
asn1_construction construction;\
setup_buf_only()

#define setup_no_length()\
asn1_tagnum tagnum;\
setup_no_tagnum()

#define setup()\
unsigned int length;\
setup_no_length()

/* helper macros for cleanup */
#define clean_return(val) { retval = val; goto error_out; }

/* alloc_field is the first thing to allocate storage that may need cleanup */
#define alloc_field(var,type)\
var = (type*)calloc(1,sizeof(type));\
if((var) == NULL) clean_return(ENOMEM)

/* process encoding header ***************************************/
/* decode tag and check that it == [APPLICATION tagnum] */
#define check_apptag(tagexpect)						\
{									\
    taginfo t1;								\
    retval = asn1_get_tag_2(&buf, &t1);					\
    if (retval) clean_return (retval);					\
    if (t1.asn1class != APPLICATION || t1.construction != CONSTRUCTED)	\
	clean_return(ASN1_BAD_ID);					\
    if (t1.tagnum != (tagexpect)) clean_return(KRB5_BADMSGTYPE);	\
    asn1class = t1.asn1class;						\
    construction = t1.construction;					\
    tagnum = t1.tagnum;							\
}



/* process a structure *******************************************/

/* decode an explicit tag and place the number in tagnum */
#define next_tag()				\
{ taginfo t2;					\
  retval = asn1_get_tag_2(&subbuf, &t2);	\
  if(retval) clean_return(retval);		\
  asn1class = t2.asn1class;			\
  construction = t2.construction;		\
  tagnum = t2.tagnum;				\
  indef = t2.indef;				\
}

#define get_eoc()						\
{								\
    taginfo t3;							\
    retval = asn1_get_tag_2(&subbuf, &t3);			\
    if (retval) return retval;					\
    if (t3.asn1class != UNIVERSAL || t3.tagnum || t3.indef)	\
        return ASN1_MISSING_EOC;				\
    asn1class = t3.asn1class;					\
    construction = t3.construction;				\
    tagnum = t3.tagnum;						\
    indef = t3.indef;						\
}

/* decode sequence header and initialize tagnum with the first field */
#define begin_structure()\
asn1buf subbuf;\
int seqindef;\
int indef;\
retval = asn1_get_sequence(&buf,&length,&seqindef);\
if(retval) clean_return(retval);\
retval = asn1buf_imbed(&subbuf,&buf,length,seqindef);\
if(retval) clean_return(retval);\
next_tag()

#define end_structure()\
retval = asn1buf_sync(&buf,&subbuf,asn1class,tagnum,length,indef,seqindef);\
if (retval) clean_return(retval)

/* process fields *******************************************/
/* normal fields ************************/
#define get_field_body(var,decoder)\
retval = decoder(&subbuf,&(var));\
if(retval) clean_return(retval);\
if (indef) { get_eoc(); }\
next_tag()

/* decode a field (<[UNIVERSAL id]> <length> <contents>)
    check that the id number == tagexpect then
    decode into var
    get the next tag */
#define get_field(var,tagexpect,decoder)\
if(tagnum > (tagexpect)) clean_return(ASN1_MISSING_FIELD);\
if(tagnum < (tagexpect)) clean_return(ASN1_MISPLACED_FIELD);\
if(asn1class != CONTEXT_SPECIFIC || construction != CONSTRUCTED)\
  clean_return(ASN1_BAD_ID);\
get_field_body(var,decoder)

/* decode (or skip, if not present) an optional field */
#define opt_field(var,tagexpect,decoder)				\
  if (asn1buf_remains(&subbuf, seqindef)) {				\
    if (asn1class != CONTEXT_SPECIFIC || construction != CONSTRUCTED)	\
      clean_return(ASN1_BAD_ID);					\
    if (tagnum == (tagexpect)) {					\
      get_field_body(var,decoder);					\
    }									\
  }

/* field w/ accompanying length *********/
#define get_lenfield_body(len,var,decoder)\
retval = decoder(&subbuf,&(len),&(var));\
if(retval) clean_return(retval);\
if (indef) { get_eoc(); }\
next_tag()

/* decode a field w/ its length (for string types) */
#define get_lenfield(len,var,tagexpect,decoder)\
if(tagnum > (tagexpect)) clean_return(ASN1_MISSING_FIELD);\
if(tagnum < (tagexpect)) clean_return(ASN1_MISPLACED_FIELD);\
if(asn1class != CONTEXT_SPECIFIC || construction != CONSTRUCTED)\
  clean_return(ASN1_BAD_ID);\
get_lenfield_body(len,var,decoder)

/* decode an optional field w/ length */
#define opt_lenfield(len,var,tagexpect,decoder)				\
  if (asn1buf_remains(&subbuf, seqindef)) {				\
    if (asn1class != CONTEXT_SPECIFIC || construction != CONSTRUCTED)	\
      clean_return(ASN1_BAD_ID);					\
    if (tagnum == (tagexpect)) {					\
      get_lenfield_body(len,var,decoder);				\
    }									\
  }
  

/* clean up ******************************************************/
/* finish up */
/* to make things less painful, assume the cleanup is passed rep */
#define cleanup(cleanup_routine)\
   return 0; \
error_out: \
   if (rep && *rep) \
	cleanup_routine(*rep); \
   return retval;

#define cleanup_none()\
   return 0; \
error_out: \
   return retval;
	
#define cleanup_manual()\
   return 0;

#define free_field(rep,f) if ((rep)->f) free((rep)->f)
#define clear_field(rep,f) (*(rep))->f = 0

krb5_error_code decode_krb5_authenticator(const krb5_data *code, krb5_authenticator **rep)
{
  setup();
  alloc_field(*rep,krb5_authenticator);
  clear_field(rep,subkey);
  clear_field(rep,checksum);
  clear_field(rep,client);

  check_apptag(2);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) clean_return(KRB5KDC_ERR_BAD_PVNO); }
    alloc_field((*rep)->client,krb5_principal_data);
    get_field((*rep)->client,1,asn1_decode_realm);
    get_field((*rep)->client,2,asn1_decode_principal_name);
    if(tagnum == 3){
      alloc_field((*rep)->checksum,krb5_checksum);
      get_field(*((*rep)->checksum),3,asn1_decode_checksum); }
    get_field((*rep)->cusec,4,asn1_decode_int32);
    get_field((*rep)->ctime,5,asn1_decode_kerberos_time);
    if(tagnum == 6){ alloc_field((*rep)->subkey,krb5_keyblock); }
    opt_field(*((*rep)->subkey),6,asn1_decode_encryption_key);
    opt_field((*rep)->seq_number,7,asn1_decode_seqnum);
    opt_field((*rep)->authorization_data,8,asn1_decode_authorization_data);
    (*rep)->magic = KV5M_AUTHENTICATOR;
    end_structure();
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,subkey);
      free_field(*rep,checksum);
      free_field(*rep,client);
      free(*rep);
  }
  return retval;
}

krb5_error_code
KRB5_CALLCONV
krb5_decode_ticket(const krb5_data *code, krb5_ticket **rep)
{
    return decode_krb5_ticket(code, rep);
}

krb5_error_code decode_krb5_ticket(const krb5_data *code, krb5_ticket **rep)
{
  setup();
  alloc_field(*rep,krb5_ticket);
  clear_field(rep,server);
  
  check_apptag(1);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) return KRB5KDC_ERR_BAD_PVNO;
    }
    alloc_field((*rep)->server,krb5_principal_data);
    get_field((*rep)->server,1,asn1_decode_realm);
    get_field((*rep)->server,2,asn1_decode_principal_name);
    get_field((*rep)->enc_part,3,asn1_decode_encrypted_data);
    (*rep)->magic = KV5M_TICKET;
    end_structure();
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,server);
      free(*rep);
  }
  return retval;
}

krb5_error_code decode_krb5_encryption_key(const krb5_data *code, krb5_keyblock **rep)
{
  setup();
  alloc_field(*rep,krb5_keyblock);

  { begin_structure();
    get_field((*rep)->enctype,0,asn1_decode_enctype);
    get_lenfield((*rep)->length,(*rep)->contents,1,asn1_decode_octetstring);
    end_structure();
    (*rep)->magic = KV5M_KEYBLOCK;
  }
  cleanup(free);
}

krb5_error_code decode_krb5_enc_tkt_part(const krb5_data *code, krb5_enc_tkt_part **rep)
{
  setup();
  alloc_field(*rep,krb5_enc_tkt_part);
  clear_field(rep,session);
  clear_field(rep,client);

  check_apptag(3);
  { begin_structure();
    get_field((*rep)->flags,0,asn1_decode_ticket_flags);
    alloc_field((*rep)->session,krb5_keyblock);
    get_field(*((*rep)->session),1,asn1_decode_encryption_key);
    alloc_field((*rep)->client,krb5_principal_data);
    get_field((*rep)->client,2,asn1_decode_realm);
    get_field((*rep)->client,3,asn1_decode_principal_name);
    get_field((*rep)->transited,4,asn1_decode_transited_encoding);
    get_field((*rep)->times.authtime,5,asn1_decode_kerberos_time);
    if (tagnum == 6)
      { get_field((*rep)->times.starttime,6,asn1_decode_kerberos_time); }
    else
      (*rep)->times.starttime=(*rep)->times.authtime;
    get_field((*rep)->times.endtime,7,asn1_decode_kerberos_time);
    opt_field((*rep)->times.renew_till,8,asn1_decode_kerberos_time);
    opt_field((*rep)->caddrs,9,asn1_decode_host_addresses);
    opt_field((*rep)->authorization_data,10,asn1_decode_authorization_data);
    (*rep)->magic = KV5M_ENC_TKT_PART;
    end_structure();
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,session);
      free_field(*rep,client);
      free(*rep);
  }
  return retval;
}

krb5_error_code decode_krb5_enc_kdc_rep_part(const krb5_data *code, krb5_enc_kdc_rep_part **rep)
{
  taginfo t4;
  setup_buf_only();
  alloc_field(*rep,krb5_enc_kdc_rep_part);

  retval = asn1_get_tag_2(&buf, &t4);
  if (retval) clean_return(retval);
  if (t4.asn1class != APPLICATION || t4.construction != CONSTRUCTED) clean_return(ASN1_BAD_ID);
  if (t4.tagnum == 25) (*rep)->msg_type = KRB5_AS_REP;
  else if(t4.tagnum == 26) (*rep)->msg_type = KRB5_TGS_REP;
  else clean_return(KRB5_BADMSGTYPE);

  retval = asn1_decode_enc_kdc_rep_part(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_as_rep(const krb5_data *code, krb5_kdc_rep **rep)
{
  setup_no_length();
  alloc_field(*rep,krb5_kdc_rep);

  check_apptag(11);
  retval = asn1_decode_kdc_rep(&buf,*rep);
  if(retval) clean_return(retval);
#ifdef KRB5_MSGTYPE_STRICT
  if((*rep)->msg_type != KRB5_AS_REP)
    clean_return(KRB5_BADMSGTYPE);
#endif

  cleanup(free);
}

krb5_error_code decode_krb5_tgs_rep(const krb5_data *code, krb5_kdc_rep **rep)
{
  setup_no_length();
  alloc_field(*rep,krb5_kdc_rep);

  check_apptag(13);
  retval = asn1_decode_kdc_rep(&buf,*rep);
  if(retval) clean_return(retval);
#ifdef KRB5_MSGTYPE_STRICT
  if((*rep)->msg_type != KRB5_TGS_REP) clean_return(KRB5_BADMSGTYPE);
#endif

  cleanup(free);
}

krb5_error_code decode_krb5_ap_req(const krb5_data *code, krb5_ap_req **rep)
{
  setup();
  alloc_field(*rep,krb5_ap_req);
  clear_field(rep,ticket);

  check_apptag(14);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) clean_return(KRB5KDC_ERR_BAD_PVNO); }
    { krb5_msgtype msg_type;
      get_field(msg_type,1,asn1_decode_msgtype);
#ifdef KRB5_MSGTYPE_STRICT
      if(msg_type != KRB5_AP_REQ) clean_return(KRB5_BADMSGTYPE);
#endif
    }
    get_field((*rep)->ap_options,2,asn1_decode_ap_options);
    alloc_field((*rep)->ticket,krb5_ticket);
    get_field(*((*rep)->ticket),3,asn1_decode_ticket);
    get_field((*rep)->authenticator,4,asn1_decode_encrypted_data);
    end_structure();
    (*rep)->magic = KV5M_AP_REQ;
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,ticket);
      free(*rep);
  }
  return retval;
}

krb5_error_code decode_krb5_ap_rep(const krb5_data *code, krb5_ap_rep **rep)
{
  setup();
  alloc_field(*rep,krb5_ap_rep);

  check_apptag(15);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) clean_return(KRB5KDC_ERR_BAD_PVNO); }
    { krb5_msgtype msg_type;
      get_field(msg_type,1,asn1_decode_msgtype);
#ifdef KRB5_MSGTYPE_STRICT
      if(msg_type != KRB5_AP_REP) clean_return(KRB5_BADMSGTYPE);
#endif
    }
    get_field((*rep)->enc_part,2,asn1_decode_encrypted_data);
    end_structure();
    (*rep)->magic = KV5M_AP_REP;
  }
  cleanup(free);
}

krb5_error_code decode_krb5_ap_rep_enc_part(const krb5_data *code, krb5_ap_rep_enc_part **rep)
{
  setup();
  alloc_field(*rep,krb5_ap_rep_enc_part);
  clear_field(rep,subkey);

  check_apptag(27);
  { begin_structure();
    get_field((*rep)->ctime,0,asn1_decode_kerberos_time);
    get_field((*rep)->cusec,1,asn1_decode_int32);
    if(tagnum == 2){ alloc_field((*rep)->subkey,krb5_keyblock); }
    opt_field(*((*rep)->subkey),2,asn1_decode_encryption_key);
    opt_field((*rep)->seq_number,3,asn1_decode_seqnum);
    end_structure();
    (*rep)->magic = KV5M_AP_REP_ENC_PART;
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,subkey);
      free(*rep);
  }
  return retval;
}

krb5_error_code decode_krb5_as_req(const krb5_data *code, krb5_kdc_req **rep)
{
  setup_no_length();
  alloc_field(*rep,krb5_kdc_req);

  check_apptag(10);
  retval = asn1_decode_kdc_req(&buf,*rep);
  if(retval) clean_return(retval);
#ifdef KRB5_MSGTYPE_STRICT
  if((*rep)->msg_type != KRB5_AS_REQ) clean_return(KRB5_BADMSGTYPE);
#endif
  
  cleanup(free);
}

krb5_error_code decode_krb5_tgs_req(const krb5_data *code, krb5_kdc_req **rep)
{
  setup_no_length();
  alloc_field(*rep,krb5_kdc_req);

  check_apptag(12);
  retval = asn1_decode_kdc_req(&buf,*rep);
  if(retval) clean_return(retval);
#ifdef KRB5_MSGTYPE_STRICT
  if((*rep)->msg_type != KRB5_TGS_REQ) clean_return(KRB5_BADMSGTYPE);
#endif
  
  cleanup(free);
}

krb5_error_code decode_krb5_kdc_req_body(const krb5_data *code, krb5_kdc_req **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_kdc_req);

  retval = asn1_decode_kdc_req_body(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_safe(const krb5_data *code, krb5_safe **rep)
{
  setup();
  alloc_field(*rep,krb5_safe);
  clear_field(rep,checksum);

  check_apptag(20);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) clean_return(KRB5KDC_ERR_BAD_PVNO); }
    { krb5_msgtype msg_type;
      get_field(msg_type,1,asn1_decode_msgtype);
#ifdef KRB5_MSGTYPE_STRICT
      if(msg_type != KRB5_SAFE) clean_return(KRB5_BADMSGTYPE);
#endif
    }
    get_field(**rep,2,asn1_decode_krb_safe_body);
    alloc_field((*rep)->checksum,krb5_checksum);
    get_field(*((*rep)->checksum),3,asn1_decode_checksum);
  (*rep)->magic = KV5M_SAFE;
    end_structure();
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,checksum);
      free(*rep);
  }
  return retval;
}

krb5_error_code decode_krb5_priv(const krb5_data *code, krb5_priv **rep)
{
  setup();
  alloc_field(*rep,krb5_priv);

  check_apptag(21);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) clean_return(KRB5KDC_ERR_BAD_PVNO); }
    { krb5_msgtype msg_type;
      get_field(msg_type,1,asn1_decode_msgtype);
#ifdef KRB5_MSGTYPE_STRICT
      if(msg_type != KRB5_PRIV) clean_return(KRB5_BADMSGTYPE);
#endif
    }
    get_field((*rep)->enc_part,3,asn1_decode_encrypted_data);
    (*rep)->magic = KV5M_PRIV;
    end_structure();
  }
  cleanup(free);
}

krb5_error_code decode_krb5_enc_priv_part(const krb5_data *code, krb5_priv_enc_part **rep)
{
  setup();
  alloc_field(*rep,krb5_priv_enc_part);
  clear_field(rep,r_address);
  clear_field(rep,s_address);

  check_apptag(28);
  { begin_structure();
    get_lenfield((*rep)->user_data.length,(*rep)->user_data.data,0,asn1_decode_charstring);
    opt_field((*rep)->timestamp,1,asn1_decode_kerberos_time);
    opt_field((*rep)->usec,2,asn1_decode_int32);
    opt_field((*rep)->seq_number,3,asn1_decode_seqnum);
    alloc_field((*rep)->s_address,krb5_address);
    get_field(*((*rep)->s_address),4,asn1_decode_host_address);
    if(tagnum == 5){ alloc_field((*rep)->r_address,krb5_address); }
    opt_field(*((*rep)->r_address),5,asn1_decode_host_address);
    (*rep)->magic = KV5M_PRIV_ENC_PART;
    end_structure();
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,r_address);
      free_field(*rep,s_address);
      free(*rep);
  }
  return retval;
}

krb5_error_code decode_krb5_cred(const krb5_data *code, krb5_cred **rep)
{
  setup();
  alloc_field(*rep,krb5_cred);

  check_apptag(22);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) clean_return(KRB5KDC_ERR_BAD_PVNO); }
    { krb5_msgtype msg_type;
      get_field(msg_type,1,asn1_decode_msgtype);
#ifdef KRB5_MSGTYPE_STRICT
      if(msg_type != KRB5_CRED) clean_return(KRB5_BADMSGTYPE);
#endif
    }
    get_field((*rep)->tickets,2,asn1_decode_sequence_of_ticket);
    get_field((*rep)->enc_part,3,asn1_decode_encrypted_data);
    (*rep)->magic = KV5M_CRED;
    end_structure();
  }
  cleanup(free);
}

krb5_error_code decode_krb5_enc_cred_part(const krb5_data *code, krb5_cred_enc_part **rep)
{
  setup();
  alloc_field(*rep,krb5_cred_enc_part);
  clear_field(rep,r_address);
  clear_field(rep,s_address);

  check_apptag(29);
  { begin_structure();
    get_field((*rep)->ticket_info,0,asn1_decode_sequence_of_krb_cred_info);
    opt_field((*rep)->nonce,1,asn1_decode_int32);
    opt_field((*rep)->timestamp,2,asn1_decode_kerberos_time);
    opt_field((*rep)->usec,3,asn1_decode_int32);
    if(tagnum == 4){ alloc_field((*rep)->s_address,krb5_address); }
    opt_field(*((*rep)->s_address),4,asn1_decode_host_address);
    if(tagnum == 5){ alloc_field((*rep)->r_address,krb5_address); }
    opt_field(*((*rep)->r_address),5,asn1_decode_host_address);
    (*rep)->magic = KV5M_CRED_ENC_PART;
    end_structure();
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,r_address);
      free_field(*rep,s_address);
      free(*rep);
  }
  return retval;
}


krb5_error_code decode_krb5_error(const krb5_data *code, krb5_error **rep)
{
  setup();
  alloc_field(*rep,krb5_error);
  clear_field(rep,server);
  clear_field(rep,client);
  
  check_apptag(30);
  { begin_structure();
    { krb5_kvno kvno;
      get_field(kvno,0,asn1_decode_kvno);
      if(kvno != KVNO) clean_return(KRB5KDC_ERR_BAD_PVNO); }
    { krb5_msgtype msg_type;
      get_field(msg_type,1,asn1_decode_msgtype);
#ifdef KRB5_MSGTYPE_STRICT
      if(msg_type != KRB5_ERROR) clean_return(KRB5_BADMSGTYPE);
#endif
    }
    opt_field((*rep)->ctime,2,asn1_decode_kerberos_time);
    opt_field((*rep)->cusec,3,asn1_decode_int32);
    get_field((*rep)->stime,4,asn1_decode_kerberos_time);
    get_field((*rep)->susec,5,asn1_decode_int32);
    get_field((*rep)->error,6,asn1_decode_ui_4);
    if(tagnum == 7){ alloc_field((*rep)->client,krb5_principal_data); }
    opt_field((*rep)->client,7,asn1_decode_realm);
    opt_field((*rep)->client,8,asn1_decode_principal_name);
    alloc_field((*rep)->server,krb5_principal_data);
    get_field((*rep)->server,9,asn1_decode_realm);
    get_field((*rep)->server,10,asn1_decode_principal_name);
    opt_lenfield((*rep)->text.length,(*rep)->text.data,11,asn1_decode_generalstring);
    opt_lenfield((*rep)->e_data.length,(*rep)->e_data.data,12,asn1_decode_charstring);
    (*rep)->magic = KV5M_ERROR;
    end_structure();
  }
  cleanup_manual();
error_out:
  if (rep && *rep) {
      free_field(*rep,server);
      free_field(*rep,client);
      free(*rep);
  }
  return retval;
}

krb5_error_code decode_krb5_authdata(const krb5_data *code, krb5_authdata ***rep)
{
  setup_buf_only();
  *rep = 0;
  retval = asn1_decode_authorization_data(&buf,rep);
  if(retval) clean_return(retval);
  cleanup_none();		/* we're not allocating anything here... */
}

krb5_error_code decode_krb5_pwd_sequence(const krb5_data *code, passwd_phrase_element **rep)
{
  setup_buf_only();
  alloc_field(*rep,passwd_phrase_element);
  retval = asn1_decode_passwdsequence(&buf,*rep);
  if(retval) clean_return(retval);
  cleanup(free);
}

krb5_error_code decode_krb5_pwd_data(const krb5_data *code, krb5_pwd_data **rep)
{
  setup();
  alloc_field(*rep,krb5_pwd_data);
  { begin_structure();
    get_field((*rep)->sequence_count,0,asn1_decode_int);
    get_field((*rep)->element,1,asn1_decode_sequence_of_passwdsequence);
    (*rep)->magic = KV5M_PWD_DATA;
    end_structure (); }
  cleanup(free);
}

krb5_error_code decode_krb5_padata_sequence(const krb5_data *code, krb5_pa_data ***rep)
{
  setup_buf_only();
  *rep = 0;
  retval = asn1_decode_sequence_of_pa_data(&buf,rep);
  if(retval) clean_return(retval);
  cleanup_none();		/* we're not allocating anything here */
}

krb5_error_code decode_krb5_alt_method(const krb5_data *code, krb5_alt_method **rep)
{
  setup();
  alloc_field(*rep,krb5_alt_method);
  { begin_structure();
    get_field((*rep)->method,0,asn1_decode_int32);
    if (tagnum == 1) {
	get_lenfield((*rep)->length,(*rep)->data,1,asn1_decode_octetstring);
    } else {
	(*rep)->length = 0;
	(*rep)->data = 0;
    }
    (*rep)->magic = KV5M_ALT_METHOD;
    end_structure();
  }
  cleanup(free);
}

krb5_error_code decode_krb5_etype_info(const krb5_data *code, krb5_etype_info_entry ***rep)
{
  setup_buf_only();
  *rep = 0;
  retval = asn1_decode_etype_info(&buf,rep);
  if(retval) clean_return(retval);
  cleanup_none();		/* we're not allocating anything here */
}

krb5_error_code decode_krb5_etype_info2(const krb5_data *code, krb5_etype_info_entry ***rep)
{
    setup_buf_only();
    *rep = 0;
    retval = asn1_decode_etype_info2(&buf,rep, 0);
    if (retval == ASN1_BAD_ID) {
	retval = asn1buf_wrap_data(&buf,code);
	if(retval) clean_return(retval);
	retval = asn1_decode_etype_info2(&buf, rep, 1);
    }
    if(retval) clean_return(retval);
    cleanup_none();		/* we're not allocating anything here */
}


krb5_error_code decode_krb5_enc_data(const krb5_data *code, krb5_enc_data **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_enc_data);

  retval = asn1_decode_encrypted_data(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_pa_enc_ts(const krb5_data *code, krb5_pa_enc_ts **rep)
{
  setup();
  alloc_field(*rep,krb5_pa_enc_ts);
  { begin_structure();
    get_field((*rep)->patimestamp,0,asn1_decode_kerberos_time);
    if (tagnum == 1) {
	get_field((*rep)->pausec,1,asn1_decode_int32);
    } else
	(*rep)->pausec = 0;
    end_structure (); }
  cleanup(free);
}

krb5_error_code decode_krb5_sam_challenge(const krb5_data *code, krb5_sam_challenge **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_sam_challenge);

  retval = asn1_decode_sam_challenge(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_sam_challenge_2(const krb5_data *code, krb5_sam_challenge_2 **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_sam_challenge_2);

  retval = asn1_decode_sam_challenge_2(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_sam_challenge_2_body(const krb5_data *code, krb5_sam_challenge_2_body **rep)
{
  setup_buf_only();
  alloc_field(*rep, krb5_sam_challenge_2_body);

  retval = asn1_decode_sam_challenge_2_body(&buf, *rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_enc_sam_key(const krb5_data *code, krb5_sam_key **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_sam_key);

  retval = asn1_decode_enc_sam_key(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_enc_sam_response_enc(const krb5_data *code, krb5_enc_sam_response_enc **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_enc_sam_response_enc);

  retval = asn1_decode_enc_sam_response_enc(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_enc_sam_response_enc_2(const krb5_data *code, krb5_enc_sam_response_enc_2 **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_enc_sam_response_enc_2);

  retval = asn1_decode_enc_sam_response_enc_2(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_sam_response(const krb5_data *code, krb5_sam_response **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_sam_response);

  retval = asn1_decode_sam_response(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_sam_response_2(const krb5_data *code, krb5_sam_response_2 **rep)
{
  setup_buf_only();
  alloc_field(*rep,krb5_sam_response_2);

  retval = asn1_decode_sam_response_2(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}

krb5_error_code decode_krb5_predicted_sam_response(const krb5_data *code, krb5_predicted_sam_response **rep)
{
  setup_buf_only();		/* preallocated */
  alloc_field(*rep,krb5_predicted_sam_response);

  retval = asn1_decode_predicted_sam_response(&buf,*rep);
  if(retval) clean_return(retval);

  cleanup(free);
}