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