c2cdf12281228db1431a7458710c7851e7fc9c5b
[krb5.git] / src / lib / krb5 / krb / kdc_rep_dc.c
1 /*
2  * lib/krb5/krb/kdc_rep_dc.c
3  *
4  * Copyright 1990 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.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * krb5_kdc_rep_decrypt_proc()
25  */
26
27 #include "k5-int.h"
28
29 /*
30  * Decrypt the encrypted portion of the KDC_REP message, using the key
31  * passed.
32  *
33  */
34
35 /*ARGSUSED*/
36 krb5_error_code INTERFACE
37 krb5_kdc_rep_decrypt_proc(context, key, decryptarg, dec_rep)
38     krb5_context context;
39     const krb5_keyblock * key;
40     krb5_const_pointer decryptarg;
41     krb5_kdc_rep * dec_rep;
42 {
43     krb5_error_code retval;
44     krb5_encrypt_block eblock;
45     krb5_data scratch;
46     krb5_enc_kdc_rep_part *local_encpart;
47
48     if (!valid_etype(dec_rep->enc_part.etype))
49         return KRB5_PROG_ETYPE_NOSUPP;
50
51     /* set up scratch decrypt/decode area */
52
53     scratch.length = dec_rep->enc_part.ciphertext.length;
54     if (!(scratch.data = malloc(dec_rep->enc_part.ciphertext.length))) {
55         return(ENOMEM);
56     }
57
58     /* put together an eblock for this encryption */
59
60     krb5_use_cstype(context, &eblock, dec_rep->enc_part.etype);
61
62     /* do any necessary key pre-processing */
63     if (retval = krb5_process_key(context, &eblock, key)) {
64         free(scratch.data);
65         return(retval);
66     }
67
68     /* call the decryption routine */
69     if (retval = krb5_decrypt(context, (krb5_pointer) dec_rep->enc_part.ciphertext.data,
70                               (krb5_pointer) scratch.data,
71                               scratch.length, &eblock, 0)) {
72         (void) krb5_finish_key(context, &eblock);
73         free(scratch.data);
74         return retval;
75     }
76 #define clean_scratch() {memset(scratch.data, 0, scratch.length); \
77 free(scratch.data);}
78     if (retval = krb5_finish_key(context, &eblock)) {
79         clean_scratch();
80         return retval;
81     }
82
83     /* and do the decode */
84     retval = decode_krb5_enc_kdc_rep_part(&scratch, &local_encpart);
85     clean_scratch();
86     if (retval)
87         return retval;
88
89     dec_rep->enc_part2 = local_encpart;
90
91     return 0;
92 }