Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / rd_safe.c
1 /*
2  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
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.
16  * 
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.
20  * 
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.
32  */
33
34 #include "krb_locl.h"
35
36 RCSID("$Id: rd_safe.c,v 1.26.2.1 2000/10/10 13:20:36 assar Exp $");
37
38 /* application include files */
39 #include "krb-archaeology.h"
40
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
46
47 #define DES_QUAD_DEFAULT DES_QUAD_GUESS
48
49 #endif /* DES_QUAD_GUESS */
50
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.
54  */
55
56 /* des_quad_chsum-type; 0 == unknown, 1 == new PL10++, 2 == old */
57 int dqc_type = DES_QUAD_DEFAULT;
58
59 void
60 fixup_quad_cksum(void *start, size_t len, des_cblock *key, 
61                  void *new_checksum, void *old_checksum, int little)
62 {
63     des_quad_cksum((des_cblock*)start, (des_cblock*)new_checksum, len, 2, key);
64     if(HOST_BYTE_ORDER){
65         if(little){
66             memcpy(old_checksum, new_checksum, 16);
67         }else{
68             u_int32_t *tmp = (u_int32_t*)new_checksum;
69             memcpy(old_checksum, new_checksum, 16);
70             swap_u_16(old_checksum);
71             swap_u_long(tmp[0]);
72             swap_u_long(tmp[1]);
73             swap_u_long(tmp[2]);
74             swap_u_long(tmp[3]);
75         }
76     }else{
77         if(little){
78             u_int32_t *tmp = (u_int32_t*)new_checksum;
79             swap_u_long(tmp[0]);
80             swap_u_long(tmp[1]);
81             swap_u_long(tmp[2]);
82             swap_u_long(tmp[3]);
83             memcpy(old_checksum, new_checksum, 16);
84         }else{
85             u_int32_t tmp[4];
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);
91         }
92     }
93 }
94
95 /*
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
101  * some error code.
102  *
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".
106  *
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".
110  */
111
112 int32_t
113 krb_rd_safe(void *in, u_int32_t in_length, des_cblock *key, 
114             struct sockaddr_in *sender, struct sockaddr_in *receiver, 
115             MSG_DAT *m_data)
116 {
117     unsigned char *p = (unsigned char*)in, *start;
118
119     unsigned char pvno, type;
120     int little_endian;
121     struct timeval tv;
122     u_int32_t src_addr;
123     int delta_t;
124     
125
126     pvno = *p++;
127     if(pvno != KRB_PROT_VERSION)
128         return RD_AP_VERSION;
129     
130     type = *p++;
131     little_endian = type & 1;
132     type &= ~1;
133     if(type != AUTH_MSG_SAFE)
134         return RD_AP_MSG_TYPE;
135
136     start = p;
137     
138     p += krb_get_int(p, &m_data->app_length, 4, little_endian);
139     
140     if(m_data->app_length + 31 > in_length)
141         return RD_AP_MODIFIED;
142     
143     m_data->app_data = p;
144
145     p += m_data->app_length;
146
147     m_data->time_5ms = *p++;
148
149     p += krb_get_address(p, &src_addr);
150
151     if (!krb_equiv(src_addr, sender->sin_addr.s_addr))
152         return RD_AP_BADD;
153
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);
156     
157     gettimeofday(&tv, NULL);
158
159     delta_t = abs((int)((long) tv.tv_sec - m_data->time_sec));
160     if (delta_t > CLOCK_SKEW) return RD_AP_TIME;
161
162     /*
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.
166      */
167
168     {
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;
179         else
180             return RD_AP_MODIFIED;
181     }
182     return KSUCCESS;
183 }