xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/rd_priv.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: rd_priv.c,v 1.1.1.1 2011/04/13 18:15:37 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #include "krb5_locl.h"
37*ebfedea0SLionel Sambuc 
38*ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_rd_priv(krb5_context context,krb5_auth_context auth_context,const krb5_data * inbuf,krb5_data * outbuf,krb5_replay_data * outdata)39*ebfedea0SLionel Sambuc krb5_rd_priv(krb5_context context,
40*ebfedea0SLionel Sambuc 	     krb5_auth_context auth_context,
41*ebfedea0SLionel Sambuc 	     const krb5_data *inbuf,
42*ebfedea0SLionel Sambuc 	     krb5_data *outbuf,
43*ebfedea0SLionel Sambuc 	     krb5_replay_data *outdata)
44*ebfedea0SLionel Sambuc {
45*ebfedea0SLionel Sambuc     krb5_error_code ret;
46*ebfedea0SLionel Sambuc     KRB_PRIV priv;
47*ebfedea0SLionel Sambuc     EncKrbPrivPart part;
48*ebfedea0SLionel Sambuc     size_t len;
49*ebfedea0SLionel Sambuc     krb5_data plain;
50*ebfedea0SLionel Sambuc     krb5_keyblock *key;
51*ebfedea0SLionel Sambuc     krb5_crypto crypto;
52*ebfedea0SLionel Sambuc 
53*ebfedea0SLionel Sambuc     krb5_data_zero(outbuf);
54*ebfedea0SLionel Sambuc 
55*ebfedea0SLionel Sambuc     if ((auth_context->flags &
56*ebfedea0SLionel Sambuc 	 (KRB5_AUTH_CONTEXT_RET_TIME | KRB5_AUTH_CONTEXT_RET_SEQUENCE)))
57*ebfedea0SLionel Sambuc     {
58*ebfedea0SLionel Sambuc 	if (outdata == NULL) {
59*ebfedea0SLionel Sambuc 	    krb5_clear_error_message (context);
60*ebfedea0SLionel Sambuc 	    return KRB5_RC_REQUIRED; /* XXX better error, MIT returns this */
61*ebfedea0SLionel Sambuc 	}
62*ebfedea0SLionel Sambuc 	/* if these fields are not present in the priv-part, silently
63*ebfedea0SLionel Sambuc            return zero */
64*ebfedea0SLionel Sambuc 	memset(outdata, 0, sizeof(*outdata));
65*ebfedea0SLionel Sambuc     }
66*ebfedea0SLionel Sambuc 
67*ebfedea0SLionel Sambuc     memset(&priv, 0, sizeof(priv));
68*ebfedea0SLionel Sambuc     ret = decode_KRB_PRIV (inbuf->data, inbuf->length, &priv, &len);
69*ebfedea0SLionel Sambuc     if (ret) {
70*ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
71*ebfedea0SLionel Sambuc 	goto failure;
72*ebfedea0SLionel Sambuc     }
73*ebfedea0SLionel Sambuc     if (priv.pvno != 5) {
74*ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
75*ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_BADVERSION;
76*ebfedea0SLionel Sambuc 	goto failure;
77*ebfedea0SLionel Sambuc     }
78*ebfedea0SLionel Sambuc     if (priv.msg_type != krb_priv) {
79*ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
80*ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_MSG_TYPE;
81*ebfedea0SLionel Sambuc 	goto failure;
82*ebfedea0SLionel Sambuc     }
83*ebfedea0SLionel Sambuc 
84*ebfedea0SLionel Sambuc     if (auth_context->remote_subkey)
85*ebfedea0SLionel Sambuc 	key = auth_context->remote_subkey;
86*ebfedea0SLionel Sambuc     else if (auth_context->local_subkey)
87*ebfedea0SLionel Sambuc 	key = auth_context->local_subkey;
88*ebfedea0SLionel Sambuc     else
89*ebfedea0SLionel Sambuc 	key = auth_context->keyblock;
90*ebfedea0SLionel Sambuc 
91*ebfedea0SLionel Sambuc     ret = krb5_crypto_init(context, key, 0, &crypto);
92*ebfedea0SLionel Sambuc     if (ret)
93*ebfedea0SLionel Sambuc 	goto failure;
94*ebfedea0SLionel Sambuc     ret = krb5_decrypt_EncryptedData(context,
95*ebfedea0SLionel Sambuc 				     crypto,
96*ebfedea0SLionel Sambuc 				     KRB5_KU_KRB_PRIV,
97*ebfedea0SLionel Sambuc 				     &priv.enc_part,
98*ebfedea0SLionel Sambuc 				     &plain);
99*ebfedea0SLionel Sambuc     krb5_crypto_destroy(context, crypto);
100*ebfedea0SLionel Sambuc     if (ret)
101*ebfedea0SLionel Sambuc 	goto failure;
102*ebfedea0SLionel Sambuc 
103*ebfedea0SLionel Sambuc     ret = decode_EncKrbPrivPart (plain.data, plain.length, &part, &len);
104*ebfedea0SLionel Sambuc     krb5_data_free (&plain);
105*ebfedea0SLionel Sambuc     if (ret) {
106*ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
107*ebfedea0SLionel Sambuc 	goto failure;
108*ebfedea0SLionel Sambuc     }
109*ebfedea0SLionel Sambuc 
110*ebfedea0SLionel Sambuc     /* check sender address */
111*ebfedea0SLionel Sambuc 
112*ebfedea0SLionel Sambuc     if (part.s_address
113*ebfedea0SLionel Sambuc 	&& auth_context->remote_address
114*ebfedea0SLionel Sambuc 	&& !krb5_address_compare (context,
115*ebfedea0SLionel Sambuc 				  auth_context->remote_address,
116*ebfedea0SLionel Sambuc 				  part.s_address)) {
117*ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
118*ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_BADADDR;
119*ebfedea0SLionel Sambuc 	goto failure_part;
120*ebfedea0SLionel Sambuc     }
121*ebfedea0SLionel Sambuc 
122*ebfedea0SLionel Sambuc     /* check receiver address */
123*ebfedea0SLionel Sambuc 
124*ebfedea0SLionel Sambuc     if (part.r_address
125*ebfedea0SLionel Sambuc 	&& auth_context->local_address
126*ebfedea0SLionel Sambuc 	&& !krb5_address_compare (context,
127*ebfedea0SLionel Sambuc 				  auth_context->local_address,
128*ebfedea0SLionel Sambuc 				  part.r_address)) {
129*ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
130*ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_BADADDR;
131*ebfedea0SLionel Sambuc 	goto failure_part;
132*ebfedea0SLionel Sambuc     }
133*ebfedea0SLionel Sambuc 
134*ebfedea0SLionel Sambuc     /* check timestamp */
135*ebfedea0SLionel Sambuc     if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
136*ebfedea0SLionel Sambuc 	krb5_timestamp sec;
137*ebfedea0SLionel Sambuc 
138*ebfedea0SLionel Sambuc 	krb5_timeofday (context, &sec);
139*ebfedea0SLionel Sambuc 	if (part.timestamp == NULL ||
140*ebfedea0SLionel Sambuc 	    part.usec      == NULL ||
141*ebfedea0SLionel Sambuc 	    abs(*part.timestamp - sec) > context->max_skew) {
142*ebfedea0SLionel Sambuc 	    krb5_clear_error_message (context);
143*ebfedea0SLionel Sambuc 	    ret = KRB5KRB_AP_ERR_SKEW;
144*ebfedea0SLionel Sambuc 	    goto failure_part;
145*ebfedea0SLionel Sambuc 	}
146*ebfedea0SLionel Sambuc     }
147*ebfedea0SLionel Sambuc 
148*ebfedea0SLionel Sambuc     /* XXX - check replay cache */
149*ebfedea0SLionel Sambuc 
150*ebfedea0SLionel Sambuc     /* check sequence number. since MIT krb5 cannot generate a sequence
151*ebfedea0SLionel Sambuc        number of zero but instead generates no sequence number, we accept that
152*ebfedea0SLionel Sambuc     */
153*ebfedea0SLionel Sambuc 
154*ebfedea0SLionel Sambuc     if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {
155*ebfedea0SLionel Sambuc 	if ((part.seq_number == NULL
156*ebfedea0SLionel Sambuc 	     && auth_context->remote_seqnumber != 0)
157*ebfedea0SLionel Sambuc 	    || (part.seq_number != NULL
158*ebfedea0SLionel Sambuc 		&& *part.seq_number != auth_context->remote_seqnumber)) {
159*ebfedea0SLionel Sambuc 	    krb5_clear_error_message (context);
160*ebfedea0SLionel Sambuc 	    ret = KRB5KRB_AP_ERR_BADORDER;
161*ebfedea0SLionel Sambuc 	    goto failure_part;
162*ebfedea0SLionel Sambuc 	}
163*ebfedea0SLionel Sambuc 	auth_context->remote_seqnumber++;
164*ebfedea0SLionel Sambuc     }
165*ebfedea0SLionel Sambuc 
166*ebfedea0SLionel Sambuc     ret = krb5_data_copy (outbuf, part.user_data.data, part.user_data.length);
167*ebfedea0SLionel Sambuc     if (ret)
168*ebfedea0SLionel Sambuc 	goto failure_part;
169*ebfedea0SLionel Sambuc 
170*ebfedea0SLionel Sambuc     if ((auth_context->flags &
171*ebfedea0SLionel Sambuc 	 (KRB5_AUTH_CONTEXT_RET_TIME | KRB5_AUTH_CONTEXT_RET_SEQUENCE))) {
172*ebfedea0SLionel Sambuc 	if(part.timestamp)
173*ebfedea0SLionel Sambuc 	    outdata->timestamp = *part.timestamp;
174*ebfedea0SLionel Sambuc 	if(part.usec)
175*ebfedea0SLionel Sambuc 	    outdata->usec = *part.usec;
176*ebfedea0SLionel Sambuc 	if(part.seq_number)
177*ebfedea0SLionel Sambuc 	    outdata->seq = *part.seq_number;
178*ebfedea0SLionel Sambuc     }
179*ebfedea0SLionel Sambuc 
180*ebfedea0SLionel Sambuc   failure_part:
181*ebfedea0SLionel Sambuc     free_EncKrbPrivPart (&part);
182*ebfedea0SLionel Sambuc 
183*ebfedea0SLionel Sambuc   failure:
184*ebfedea0SLionel Sambuc     free_KRB_PRIV (&priv);
185*ebfedea0SLionel Sambuc     return ret;
186*ebfedea0SLionel Sambuc }
187