17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * lib/krb5/krb/mk_rep.c
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate  * All Rights Reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
87c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
97c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
107c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
11*55fea89dSDan Cross  *
127c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
147c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
157c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
167c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
177c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
197c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
207c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
217c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
227c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
237c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
247c478bd9Sstevel@tonic-gate  * or implied warranty.
25*55fea89dSDan Cross  *
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * krb5_mk_rep()
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30159d09a2SMark Phalan #include "k5-int.h"
31159d09a2SMark Phalan #include "auth_con.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  Formats a KRB_AP_REP message into outbuf.
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate  The outbuf buffer storage is allocated, and should be freed by the
377c478bd9Sstevel@tonic-gate  caller when finished.
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate  returns system errors
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate 
42505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_mk_rep(krb5_context context,krb5_auth_context auth_context,krb5_data * outbuf)43505d05c7Sgtb krb5_mk_rep(krb5_context context, krb5_auth_context auth_context, krb5_data *outbuf)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate     krb5_error_code 	  retval;
467c478bd9Sstevel@tonic-gate     krb5_ap_rep_enc_part  repl;
477c478bd9Sstevel@tonic-gate     krb5_ap_rep 	  reply;
487c478bd9Sstevel@tonic-gate     krb5_data 		* scratch;
497c478bd9Sstevel@tonic-gate     krb5_data 		* toutbuf;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate     /* Make the reply */
527c478bd9Sstevel@tonic-gate     if (((auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) ||
537c478bd9Sstevel@tonic-gate 	(auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_RET_SEQUENCE)) &&
547c478bd9Sstevel@tonic-gate 	(auth_context->local_seq_number == 0)) {
557c478bd9Sstevel@tonic-gate 	if ((retval = krb5_generate_seq_number(context, auth_context->keyblock,
567c478bd9Sstevel@tonic-gate 					       &auth_context->local_seq_number)))
577c478bd9Sstevel@tonic-gate             return(retval);
587c478bd9Sstevel@tonic-gate     }
597c478bd9Sstevel@tonic-gate 
60*55fea89dSDan Cross     repl.ctime = auth_context->authentp->ctime;
61*55fea89dSDan Cross     repl.cusec = auth_context->authentp->cusec;
627c478bd9Sstevel@tonic-gate     if (auth_context->auth_context_flags & KRB5_AUTH_CONTEXT_USE_SUBKEY) {
637c478bd9Sstevel@tonic-gate 	retval = krb5int_generate_and_save_subkey (context, auth_context,
64159d09a2SMark Phalan 						   auth_context->keyblock);
657c478bd9Sstevel@tonic-gate 	if (retval)
66159d09a2SMark Phalan 	    return retval;
677c478bd9Sstevel@tonic-gate 	repl.subkey = auth_context->send_subkey;
687c478bd9Sstevel@tonic-gate     } else
69159d09a2SMark Phalan 	repl.subkey = auth_context->authentp->subkey;
707c478bd9Sstevel@tonic-gate     repl.seq_number = auth_context->local_seq_number;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate     /* encode it before encrypting */
73159d09a2SMark Phalan     if ((retval = encode_krb5_ap_rep_enc_part(&repl, &scratch)))
747c478bd9Sstevel@tonic-gate 	return retval;
757c478bd9Sstevel@tonic-gate 
76159d09a2SMark Phalan     if ((retval = krb5_encrypt_helper(context, auth_context->keyblock,
77159d09a2SMark Phalan 				      KRB5_KEYUSAGE_AP_REP_ENCPART,
78159d09a2SMark Phalan 				      scratch, &reply.enc_part)))
797c478bd9Sstevel@tonic-gate 	goto cleanup_scratch;
807c478bd9Sstevel@tonic-gate 
81159d09a2SMark Phalan     if (!(retval = encode_krb5_ap_rep(&reply, &toutbuf))) {
827c478bd9Sstevel@tonic-gate 	*outbuf = *toutbuf;
837c478bd9Sstevel@tonic-gate 	krb5_xfree(toutbuf);
847c478bd9Sstevel@tonic-gate     }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate     memset(reply.enc_part.ciphertext.data, 0, reply.enc_part.ciphertext.length);
87*55fea89dSDan Cross     free(reply.enc_part.ciphertext.data);
88*55fea89dSDan Cross     reply.enc_part.ciphertext.length = 0;
897c478bd9Sstevel@tonic-gate     reply.enc_part.ciphertext.data = 0;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate cleanup_scratch:
92*55fea89dSDan Cross     memset(scratch->data, 0, scratch->length);
937c478bd9Sstevel@tonic-gate     krb5_free_data(context, scratch);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate     return retval;
967c478bd9Sstevel@tonic-gate }
97