xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/krb/encode_kdc.c (revision 505d05c73a6e56769f263d4803b22eddd168ee24)
17c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * lib/krb5/krb/encode_kdc.c
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
67c478bd9Sstevel@tonic-gate  * All Rights Reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
97c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
107c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
117c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
147c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
157c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
167c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
177c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
187c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
197c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
207c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
217c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
227c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
237c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
247c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
257c478bd9Sstevel@tonic-gate  * or implied warranty.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * krb5_encode_kdc_rep() function.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <k5-int.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  Takes KDC rep parts in *rep and *encpart, and formats it into *enc_rep,
357c478bd9Sstevel@tonic-gate  using message type type and encryption key client_key and encryption type
367c478bd9Sstevel@tonic-gate  etype.
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate  The string *enc_rep will be allocated before formatting; the caller should
397c478bd9Sstevel@tonic-gate  free when finished.
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate  returns system errors
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate  dec_rep->enc_part.ciphertext is allocated and filled in.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate /* due to argument promotion rules, we need to use the DECLARG/OLDDECLARG
467c478bd9Sstevel@tonic-gate    stuff... */
477c478bd9Sstevel@tonic-gate krb5_error_code
48*505d05c7Sgtb krb5_encode_kdc_rep(krb5_context context, krb5_msgtype type,
49*505d05c7Sgtb 		    const krb5_enc_kdc_rep_part *encpart,
50*505d05c7Sgtb 		    int using_subkey, const krb5_keyblock *client_key,
51*505d05c7Sgtb 		    krb5_kdc_rep *dec_rep, krb5_data **enc_rep)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate     krb5_data *scratch;
547c478bd9Sstevel@tonic-gate     krb5_error_code retval;
557c478bd9Sstevel@tonic-gate     krb5_enc_kdc_rep_part tmp_encpart;
567c478bd9Sstevel@tonic-gate     krb5_keyusage usage;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate     if (!valid_enctype(dec_rep->enc_part.enctype))
597c478bd9Sstevel@tonic-gate 	return KRB5_PROG_ETYPE_NOSUPP;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate     switch (type) {
627c478bd9Sstevel@tonic-gate     case KRB5_AS_REP:
637c478bd9Sstevel@tonic-gate 	usage = KRB5_KEYUSAGE_AS_REP_ENCPART;
647c478bd9Sstevel@tonic-gate 	break;
657c478bd9Sstevel@tonic-gate     case KRB5_TGS_REP:
667c478bd9Sstevel@tonic-gate 	if (using_subkey)
677c478bd9Sstevel@tonic-gate 	    usage = KRB5_KEYUSAGE_TGS_REP_ENCPART_SUBKEY;
687c478bd9Sstevel@tonic-gate 	else
697c478bd9Sstevel@tonic-gate 	    usage = KRB5_KEYUSAGE_TGS_REP_ENCPART_SESSKEY;
707c478bd9Sstevel@tonic-gate 	break;
717c478bd9Sstevel@tonic-gate     default:
727c478bd9Sstevel@tonic-gate 	return KRB5_BADMSGTYPE;
737c478bd9Sstevel@tonic-gate     }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate     /*
767c478bd9Sstevel@tonic-gate      * We don't want to modify encpart, but we need to be able to pass
777c478bd9Sstevel@tonic-gate      * in the message type to the encoder, so it can set the ASN.1
787c478bd9Sstevel@tonic-gate      * type correct.
797c478bd9Sstevel@tonic-gate      *
807c478bd9Sstevel@tonic-gate      * Although note that it may be doing nothing with the message
817c478bd9Sstevel@tonic-gate      * type, to be compatible with old versions of Kerberos that always
827c478bd9Sstevel@tonic-gate      * encode this as a TGS_REP regardly of what it really should be;
837c478bd9Sstevel@tonic-gate      * also note that the reason why we are passing it in a structure
847c478bd9Sstevel@tonic-gate      * instead of as an argument to encode_krb5_enc_kdc_rep_part (the
857c478bd9Sstevel@tonic-gate      * way we should) is for compatibility with the ISODE version of
867c478bd9Sstevel@tonic-gate      * this fuction.  Ah, compatibility....
877c478bd9Sstevel@tonic-gate      */
887c478bd9Sstevel@tonic-gate     tmp_encpart = *encpart;
897c478bd9Sstevel@tonic-gate     tmp_encpart.msg_type = type;
907c478bd9Sstevel@tonic-gate     retval = encode_krb5_enc_kdc_rep_part(&tmp_encpart, &scratch);
917c478bd9Sstevel@tonic-gate     if (retval) {
927c478bd9Sstevel@tonic-gate 	return retval;
937c478bd9Sstevel@tonic-gate     }
947c478bd9Sstevel@tonic-gate     memset(&tmp_encpart, 0, sizeof(tmp_encpart));
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define cleanup_scratch() { (void) memset(scratch->data, 0, scratch->length); \
977c478bd9Sstevel@tonic-gate krb5_free_data(context, scratch); }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate     retval = krb5_encrypt_helper(context, client_key, usage, scratch,
1007c478bd9Sstevel@tonic-gate 				 &dec_rep->enc_part);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define cleanup_encpart() { \
1037c478bd9Sstevel@tonic-gate (void) memset(dec_rep->enc_part.ciphertext.data, 0, \
1047c478bd9Sstevel@tonic-gate 	     dec_rep->enc_part.ciphertext.length); \
1057c478bd9Sstevel@tonic-gate free(dec_rep->enc_part.ciphertext.data); \
1067c478bd9Sstevel@tonic-gate dec_rep->enc_part.ciphertext.length = 0; \
1077c478bd9Sstevel@tonic-gate dec_rep->enc_part.ciphertext.data = 0;}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate     cleanup_scratch();
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate     if (retval)
1127c478bd9Sstevel@tonic-gate 	return(retval);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate     /* now it's ready to be encoded for the wire! */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate     switch (type) {
1177c478bd9Sstevel@tonic-gate     case KRB5_AS_REP:
1187c478bd9Sstevel@tonic-gate 	retval = encode_krb5_as_rep(dec_rep, enc_rep);
1197c478bd9Sstevel@tonic-gate 	break;
1207c478bd9Sstevel@tonic-gate     case KRB5_TGS_REP:
1217c478bd9Sstevel@tonic-gate 	retval = encode_krb5_tgs_rep(dec_rep, enc_rep);
1227c478bd9Sstevel@tonic-gate 	break;
1237c478bd9Sstevel@tonic-gate     }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate     if (retval)
1267c478bd9Sstevel@tonic-gate 	cleanup_encpart();
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate     return retval;
1297c478bd9Sstevel@tonic-gate }
130