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