xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/krb/rd_rep.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * lib/krb5/krb/rd_rep.c
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
12*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
15*7c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
16*7c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
17*7c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
21*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
22*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
23*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
24*7c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
25*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
26*7c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
27*7c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
28*7c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
29*7c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
30*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
31*7c478bd9Sstevel@tonic-gate  * or implied warranty.
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * krb5_rd_rep()
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <k5-int.h>
38*7c478bd9Sstevel@tonic-gate #include <auth_con.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  *  Parses a KRB_AP_REP message, returning its contents.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  *  repl is filled in with with a pointer to allocated memory containing
44*7c478bd9Sstevel@tonic-gate  * the fields from the encrypted response.
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  *  the key in kblock is used to decrypt the message.
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  *  returns system errors, encryption errors, replay errors
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate krb5_error_code KRB5_CALLCONV
52*7c478bd9Sstevel@tonic-gate krb5_rd_rep(
53*7c478bd9Sstevel@tonic-gate     krb5_context 	  context,
54*7c478bd9Sstevel@tonic-gate     krb5_auth_context	  auth_context,
55*7c478bd9Sstevel@tonic-gate     const krb5_data 	* inbuf,
56*7c478bd9Sstevel@tonic-gate     krb5_ap_rep_enc_part * *repl)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate     krb5_error_code 	  retval;
59*7c478bd9Sstevel@tonic-gate     krb5_ap_rep 	* reply;
60*7c478bd9Sstevel@tonic-gate     krb5_data 	 	  scratch;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate     if (!krb5_is_ap_rep(inbuf))
63*7c478bd9Sstevel@tonic-gate 	return KRB5KRB_AP_ERR_MSG_TYPE;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate     /* decode it */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate     if ((retval = decode_krb5_ap_rep(inbuf, &reply)))
68*7c478bd9Sstevel@tonic-gate 	return retval;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate     /* put together an eblock for this encryption */
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate     scratch.length = reply->enc_part.ciphertext.length;
73*7c478bd9Sstevel@tonic-gate     if (!(scratch.data = malloc(scratch.length))) {
74*7c478bd9Sstevel@tonic-gate 	krb5_free_ap_rep(context, reply);
75*7c478bd9Sstevel@tonic-gate 	return(ENOMEM);
76*7c478bd9Sstevel@tonic-gate     }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate     retval = krb5_c_decrypt(context, auth_context->keyblock,
79*7c478bd9Sstevel@tonic-gate 				 KRB5_KEYUSAGE_AP_REP_ENCPART, 0,
80*7c478bd9Sstevel@tonic-gate 				 &reply->enc_part, &scratch);
81*7c478bd9Sstevel@tonic-gate     if (retval)
82*7c478bd9Sstevel@tonic-gate 	goto clean_scratch;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate     /* now decode the decrypted stuff */
85*7c478bd9Sstevel@tonic-gate     retval = decode_krb5_ap_rep_enc_part(&scratch, repl);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate     if (retval)
88*7c478bd9Sstevel@tonic-gate 	goto clean_scratch;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate     /* Check reply fields */
91*7c478bd9Sstevel@tonic-gate     if (((*repl)->ctime != auth_context->authentp->ctime) ||
92*7c478bd9Sstevel@tonic-gate       ((*repl)->cusec != auth_context->authentp->cusec)) {
93*7c478bd9Sstevel@tonic-gate 	retval = KRB5_MUTUAL_FAILED;
94*7c478bd9Sstevel@tonic-gate 	goto clean_scratch;
95*7c478bd9Sstevel@tonic-gate     }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate     /* Set auth subkey */
98*7c478bd9Sstevel@tonic-gate     if ((*repl)->subkey) {
99*7c478bd9Sstevel@tonic-gate 	if (auth_context->recv_subkey) {
100*7c478bd9Sstevel@tonic-gate 	    krb5_free_keyblock(context, auth_context->recv_subkey);
101*7c478bd9Sstevel@tonic-gate 	    auth_context->recv_subkey = NULL;
102*7c478bd9Sstevel@tonic-gate         }
103*7c478bd9Sstevel@tonic-gate 	retval = krb5_copy_keyblock(context, (*repl)->subkey,
104*7c478bd9Sstevel@tonic-gate 		&auth_context->recv_subkey);
105*7c478bd9Sstevel@tonic-gate 	if (retval)
106*7c478bd9Sstevel@tonic-gate 	    goto clean_scratch;
107*7c478bd9Sstevel@tonic-gate 	if (auth_context->send_subkey) {
108*7c478bd9Sstevel@tonic-gate 	    krb5_free_keyblock(context, auth_context->send_subkey);
109*7c478bd9Sstevel@tonic-gate 	    auth_context->send_subkey = NULL;
110*7c478bd9Sstevel@tonic-gate         }
111*7c478bd9Sstevel@tonic-gate 	retval = krb5_copy_keyblock(context, (*repl)->subkey,
112*7c478bd9Sstevel@tonic-gate 		&auth_context->send_subkey);
113*7c478bd9Sstevel@tonic-gate 	if (retval) {
114*7c478bd9Sstevel@tonic-gate 	    krb5_free_keyblock(context, auth_context->send_subkey);
115*7c478bd9Sstevel@tonic-gate 	    auth_context->send_subkey = NULL;
116*7c478bd9Sstevel@tonic-gate         }
117*7c478bd9Sstevel@tonic-gate     }
118*7c478bd9Sstevel@tonic-gate     /* Get remote sequence number */
119*7c478bd9Sstevel@tonic-gate     auth_context->remote_seq_number = (*repl)->seq_number;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate clean_scratch:
122*7c478bd9Sstevel@tonic-gate     memset(scratch.data, 0, scratch.length);
123*7c478bd9Sstevel@tonic-gate errout:
124*7c478bd9Sstevel@tonic-gate     krb5_free_ap_rep(context, reply);
125*7c478bd9Sstevel@tonic-gate     free(scratch.data);
126*7c478bd9Sstevel@tonic-gate     return retval;
127*7c478bd9Sstevel@tonic-gate }
128