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