1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/encode_kdc.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. 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 "k5-int.h"
28
29 /*
30 Takes KDC rep parts in *rep and *encpart, and formats it into *enc_rep,
31 using message type type and encryption key client_key and encryption type
32 etype.
33
34 The string *enc_rep will be allocated before formatting; the caller should
35 free when finished.
36
37 returns system errors
38
39 dec_rep->enc_part.ciphertext is allocated and filled in.
40 */
41 /* due to argument promotion rules, we need to use the DECLARG/OLDDECLARG
42 stuff... */
43 krb5_error_code
krb5_encode_kdc_rep(krb5_context context,krb5_msgtype type,const krb5_enc_kdc_rep_part * encpart,int using_subkey,const krb5_keyblock * client_key,krb5_kdc_rep * dec_rep,krb5_data ** enc_rep)44 krb5_encode_kdc_rep(krb5_context context, krb5_msgtype type,
45 const krb5_enc_kdc_rep_part *encpart,
46 int using_subkey, const krb5_keyblock *client_key,
47 krb5_kdc_rep *dec_rep, krb5_data **enc_rep)
48 {
49 krb5_data *scratch;
50 krb5_error_code retval;
51 krb5_enc_kdc_rep_part tmp_encpart;
52 krb5_keyusage usage;
53
54 if (!krb5_c_valid_enctype(dec_rep->enc_part.enctype))
55 return KRB5_PROG_ETYPE_NOSUPP;
56
57 switch (type) {
58 case KRB5_AS_REP:
59 usage = KRB5_KEYUSAGE_AS_REP_ENCPART;
60 break;
61 case KRB5_TGS_REP:
62 if (using_subkey)
63 usage = KRB5_KEYUSAGE_TGS_REP_ENCPART_SUBKEY;
64 else
65 usage = KRB5_KEYUSAGE_TGS_REP_ENCPART_SESSKEY;
66 break;
67 default:
68 return KRB5_BADMSGTYPE;
69 }
70
71 /*
72 * We don't want to modify encpart, but we need to be able to pass
73 * in the message type to the encoder, so it can set the ASN.1
74 * type correct.
75 *
76 * Although note that it may be doing nothing with the message
77 * type, to be compatible with old versions of Kerberos that always
78 * encode this as a TGS_REP regardly of what it really should be;
79 * also note that the reason why we are passing it in a structure
80 * instead of as an argument to encode_krb5_enc_kdc_rep_part (the
81 * way we should) is for compatibility with the ISODE version of
82 * this function. Ah, compatibility....
83 */
84 tmp_encpart = *encpart;
85 tmp_encpart.msg_type = type;
86 retval = encode_krb5_enc_kdc_rep_part(&tmp_encpart, &scratch);
87 if (retval) {
88 return retval;
89 }
90 memset(&tmp_encpart, 0, sizeof(tmp_encpart));
91
92 #define cleanup_scratch() { (void) memset(scratch->data, 0, scratch->length); \
93 krb5_free_data(context, scratch); }
94
95 retval = krb5_encrypt_helper(context, client_key, usage, scratch,
96 &dec_rep->enc_part);
97
98 #define cleanup_encpart() { \
99 (void) memset(dec_rep->enc_part.ciphertext.data, 0, \
100 dec_rep->enc_part.ciphertext.length); \
101 free(dec_rep->enc_part.ciphertext.data); \
102 dec_rep->enc_part.ciphertext.length = 0; \
103 dec_rep->enc_part.ciphertext.data = 0;}
104
105 cleanup_scratch();
106
107 if (retval)
108 return(retval);
109
110 /* now it's ready to be encoded for the wire! */
111
112 switch (type) {
113 case KRB5_AS_REP:
114 retval = encode_krb5_as_rep(dec_rep, enc_rep);
115 break;
116 case KRB5_TGS_REP:
117 retval = encode_krb5_tgs_rep(dec_rep, enc_rep);
118 break;
119 }
120
121 if (retval)
122 cleanup_encpart();
123
124 return retval;
125 }
126