1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * lib/krb5/krb/kdc_rep_dc.c
10  *
11  * Copyright 1990 by the Massachusetts Institute of Technology.
12  * All Rights Reserved.
13  *
14  * Export of this software from the United States of America may
15  *   require a specific license from the United States Government.
16  *   It is the responsibility of any person or organization contemplating
17  *   export to obtain such a license before exporting.
18  *
19  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20  * distribute this software and its documentation for any purpose and
21  * without fee is hereby granted, provided that the above copyright
22  * notice appear in all copies and that both that copyright notice and
23  * this permission notice appear in supporting documentation, and that
24  * the name of M.I.T. not be used in advertising or publicity pertaining
25  * to distribution of the software without specific, written prior
26  * permission.  Furthermore if you modify this software you must label
27  * your software as modified software and not distribute it in such a
28  * fashion that it might be confused with the original M.I.T. software.
29  * M.I.T. makes no representations about the suitability of
30  * this software for any purpose.  It is provided "as is" without express
31  * or implied warranty.
32  *
33  *
34  * krb5_kdc_rep_decrypt_proc()
35  */
36 
37 #include <k5-int.h>
38 
39 /*
40  * Decrypt the encrypted portion of the KDC_REP message, using the key
41  * passed.
42  *
43  */
44 
45 /*ARGSUSED*/
46 krb5_error_code
47 krb5_kdc_rep_decrypt_proc(krb5_context context, const krb5_keyblock *key, krb5_const_pointer decryptarg, krb5_kdc_rep *dec_rep)
48 {
49     krb5_error_code retval;
50     krb5_data scratch;
51     krb5_enc_kdc_rep_part *local_encpart;
52     krb5_keyusage usage;
53 
54     if (decryptarg) {
55 	usage = *(const krb5_keyusage *) decryptarg;
56     } else {
57 	usage = KRB5_KEYUSAGE_AS_REP_ENCPART;
58     }
59 
60     /* set up scratch decrypt/decode area */
61 
62     scratch.length = dec_rep->enc_part.ciphertext.length;
63     if (!(scratch.data = malloc(dec_rep->enc_part.ciphertext.length))) {
64 	return(ENOMEM);
65     }
66 
67     /*(void) (dec_rep->enc_part.enctype);*/
68 
69     retval = krb5_c_decrypt(context, key, usage, 0, &dec_rep->enc_part,
70 				 &scratch);
71     if (retval) {
72 	free(scratch.data);
73 	return(retval);
74     }
75 
76 #define clean_scratch() {memset(scratch.data, 0, scratch.length); \
77 free(scratch.data);}
78 
79     /* and do the decode */
80     retval = decode_krb5_enc_kdc_rep_part(&scratch, &local_encpart);
81     clean_scratch();
82     if (retval)
83 	return retval;
84 
85     dec_rep->enc_part2 = local_encpart;
86 
87     return 0;
88 }
89