2 * Copyright (c) 1995 - 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
36 RCSID("$Id: rd_safe.c,v 1.26.2.1 2000/10/10 13:20:36 assar Exp $");
38 /* application include files */
39 #include "krb-archaeology.h"
41 #ifndef DES_QUAD_GUESS
42 /* Temporary fixes for krb_{rd,mk}_safe */
43 #define DES_QUAD_GUESS 0
44 #define DES_QUAD_NEW 1
45 #define DES_QUAD_OLD 2
47 #define DES_QUAD_DEFAULT DES_QUAD_GUESS
49 #endif /* DES_QUAD_GUESS */
51 /* Generate two checksums in the given byteorder of the data, one
52 * new-form and one old-form. It has to be done this way to be
53 * compatible with the old version of des_quad_cksum.
56 /* des_quad_chsum-type; 0 == unknown, 1 == new PL10++, 2 == old */
57 int dqc_type = DES_QUAD_DEFAULT;
60 fixup_quad_cksum(void *start, size_t len, des_cblock *key,
61 void *new_checksum, void *old_checksum, int little)
63 des_quad_cksum((des_cblock*)start, (des_cblock*)new_checksum, len, 2, key);
66 memcpy(old_checksum, new_checksum, 16);
68 u_int32_t *tmp = (u_int32_t*)new_checksum;
69 memcpy(old_checksum, new_checksum, 16);
70 swap_u_16(old_checksum);
78 u_int32_t *tmp = (u_int32_t*)new_checksum;
83 memcpy(old_checksum, new_checksum, 16);
86 tmp[0] = ((u_int32_t*)new_checksum)[3];
87 tmp[1] = ((u_int32_t*)new_checksum)[2];
88 tmp[2] = ((u_int32_t*)new_checksum)[1];
89 tmp[3] = ((u_int32_t*)new_checksum)[0];
90 memcpy(old_checksum, tmp, 16);
96 * krb_rd_safe() checks the integrity of an AUTH_MSG_SAFE message.
97 * Given the message received, "in", the length of that message,
98 * "in_length", the "key" to compute the checksum with, and the
99 * network addresses of the "sender" and "receiver" of the message,
100 * krb_rd_safe() returns RD_AP_OK if message is okay, otherwise
103 * The message data retrieved from "in" is returned in the structure
104 * "m_data". The pointer to the application data (m_data->app_data)
105 * refers back to the appropriate place in "in".
107 * See the file "mk_safe.c" for the format of the AUTH_MSG_SAFE
108 * message. The structure containing the extracted message
109 * information, MSG_DAT, is defined in "krb.h".
113 krb_rd_safe(void *in, u_int32_t in_length, des_cblock *key,
114 struct sockaddr_in *sender, struct sockaddr_in *receiver,
117 unsigned char *p = (unsigned char*)in, *start;
119 unsigned char pvno, type;
127 if(pvno != KRB_PROT_VERSION)
128 return RD_AP_VERSION;
131 little_endian = type & 1;
133 if(type != AUTH_MSG_SAFE)
134 return RD_AP_MSG_TYPE;
138 p += krb_get_int(p, &m_data->app_length, 4, little_endian);
140 if(m_data->app_length + 31 > in_length)
141 return RD_AP_MODIFIED;
143 m_data->app_data = p;
145 p += m_data->app_length;
147 m_data->time_5ms = *p++;
149 p += krb_get_address(p, &src_addr);
151 if (!krb_equiv(src_addr, sender->sin_addr.s_addr))
154 p += krb_get_int(p, (u_int32_t *)&m_data->time_sec, 4, little_endian);
155 m_data->time_sec = lsb_time(m_data->time_sec, sender, receiver);
157 gettimeofday(&tv, NULL);
159 delta_t = abs((int)((long) tv.tv_sec - m_data->time_sec));
160 if (delta_t > CLOCK_SKEW) return RD_AP_TIME;
163 * caller must check timestamps for proper order and replays, since
164 * server might have multiple clients each with its own timestamps
165 * and we don't assume tightly synchronized clocks.
169 unsigned char new_checksum[16];
170 unsigned char old_checksum[16];
171 fixup_quad_cksum(start, p - start, key,
172 new_checksum, old_checksum, little_endian);
173 if((dqc_type == DES_QUAD_GUESS || dqc_type == DES_QUAD_NEW) &&
174 memcmp(new_checksum, p, 16) == 0)
175 dqc_type = DES_QUAD_NEW;
176 else if((dqc_type == DES_QUAD_GUESS || dqc_type == DES_QUAD_OLD) &&
177 memcmp(old_checksum, p, 16) == 0)
178 dqc_type = DES_QUAD_OLD;
180 return RD_AP_MODIFIED;