2 * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include <krb5_locl.h>
36 RCSID("$Id: rd_safe.c,v 1.19 2000/02/06 05:20:51 assar Exp $");
38 static krb5_error_code
39 verify_checksum(krb5_context context,
40 krb5_auth_context auth_context,
51 safe->cksum.cksumtype = 0;
52 safe->cksum.checksum.data = NULL;
53 safe->cksum.checksum.length = 0;
56 buf_size = length_KRB_SAFE(safe);
57 buf = malloc(buf_size);
64 ret = encode_KRB_SAFE (buf + buf_size - 1,
68 krb5_crypto_init(context, auth_context->keyblock, 0, &crypto);
69 ret = krb5_verify_checksum (context,
71 KRB5_KU_KRB_SAFE_CKSUM,
75 krb5_crypto_destroy(context, crypto);
83 krb5_rd_safe(krb5_context context,
84 krb5_auth_context auth_context,
85 const krb5_data *inbuf,
87 /*krb5_replay_data*/ void *outdata)
93 ret = decode_KRB_SAFE (inbuf->data, inbuf->length, &safe, &len);
97 ret = KRB5KRB_AP_ERR_BADVERSION;
100 if (safe.msg_type != krb_safe) {
101 ret = KRB5KRB_AP_ERR_MSG_TYPE;
104 if (!krb5_checksum_is_keyed(context, safe.cksum.cksumtype)
105 || !krb5_checksum_is_collision_proof(context, safe.cksum.cksumtype)) {
106 ret = KRB5KRB_AP_ERR_INAPP_CKSUM;
110 /* check sender address */
112 if (safe.safe_body.s_address
113 && auth_context->remote_address
114 && !krb5_address_compare (context,
115 auth_context->remote_address,
116 safe.safe_body.s_address)) {
117 ret = KRB5KRB_AP_ERR_BADADDR;
121 /* check receiver address */
123 if (safe.safe_body.r_address
124 && auth_context->local_address
125 && !krb5_address_compare (context,
126 auth_context->local_address,
127 safe.safe_body.r_address)) {
128 ret = KRB5KRB_AP_ERR_BADADDR;
132 /* check timestamp */
133 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
136 krb5_timeofday (context, &sec);
138 if (safe.safe_body.timestamp == NULL ||
139 safe.safe_body.usec == NULL ||
140 abs(*safe.safe_body.timestamp - sec) > context->max_skew) {
141 ret = KRB5KRB_AP_ERR_SKEW;
145 /* XXX - check replay cache */
147 /* check sequence number */
148 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {
149 if (safe.safe_body.seq_number == NULL ||
150 *safe.safe_body.seq_number != ++auth_context->remote_seqnumber) {
151 ret = KRB5KRB_AP_ERR_BADORDER;
156 ret = verify_checksum (context, auth_context, &safe);
160 outbuf->length = safe.safe_body.user_data.length;
161 outbuf->data = malloc(outbuf->length);
162 if (outbuf->data == NULL) {
166 memcpy (outbuf->data, safe.safe_body.user_data.data, outbuf->length);
167 free_KRB_SAFE (&safe);
170 free_KRB_SAFE (&safe);