1 /*
2  * Copyright 2004 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/mk_rep.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_mk_rep()
35  */
36 
37 #include <k5-int.h>
38 #include <auth_con.h>
39 
40 /*
41  Formats a KRB_AP_REP message into outbuf.
42 
43  The outbuf buffer storage is allocated, and should be freed by the
44  caller when finished.
45 
46  returns system errors
47 */
48 
49 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
50 krb5_mk_rep(context, auth_context, outbuf)
51     krb5_context 	  context;
52     krb5_auth_context	  auth_context;
53     krb5_data 		FAR * outbuf;
54 {
55     krb5_error_code 	  retval;
56     krb5_ap_rep_enc_part  repl;
57     krb5_ap_rep 	  reply;
58     krb5_data 		* scratch;
59     krb5_data 		* toutbuf;
60 
61     /* Make the reply */
62     if (((auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) ||
63 	(auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_RET_SEQUENCE)) &&
64 	(auth_context->local_seq_number == 0)) {
65 	if ((retval = krb5_generate_seq_number(context, auth_context->keyblock,
66 					       &auth_context->local_seq_number)))
67             return(retval);
68     }
69 
70     repl.ctime = auth_context->authentp->ctime;
71     repl.cusec = auth_context->authentp->cusec;
72     if (auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_USE_SUBKEY) {
73 	retval = krb5int_generate_and_save_subkey (context, auth_context,
74                                                    auth_context->keyblock);
75 	if (retval)
76             return retval;
77 	repl.subkey = auth_context->send_subkey;
78     } else
79         repl.subkey = auth_context->authentp->subkey;
80 
81     repl.seq_number = auth_context->local_seq_number;
82 
83     /* encode it before encrypting */
84     retval = encode_krb5_ap_rep_enc_part(&repl, &scratch);
85     if (retval)
86 	return retval;
87 
88     retval = krb5_encrypt_helper(context, auth_context->keyblock,
89 				KRB5_KEYUSAGE_AP_REP_ENCPART,
90 				scratch, &reply.enc_part);
91     if (retval)
92 	goto cleanup_scratch;
93 
94     retval = encode_krb5_ap_rep(&reply, &toutbuf);
95     if (!retval) {
96 	*outbuf = *toutbuf;
97 	krb5_xfree(toutbuf);
98     }
99 
100     memset(reply.enc_part.ciphertext.data, 0, reply.enc_part.ciphertext.length);
101     free(reply.enc_part.ciphertext.data);
102     reply.enc_part.ciphertext.length = 0;
103     reply.enc_part.ciphertext.data = 0;
104 
105 cleanup_scratch:
106     memset(scratch->data, 0, scratch->length);
107     krb5_free_data(context, scratch);
108 
109     return retval;
110 }
111