Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / crypto / kerberosIV / lib / krb / rd_priv.c
1 /*
2  * Copyright (c) 1995, 1996, 1997 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 /* $FreeBSD: src/crypto/kerberosIV/lib/krb/rd_priv.c,v 1.1.1.3.2.1 2003/02/13 21:34:36 nectar Exp $ */
34 /* $DragonFly: src/crypto/kerberosIV/lib/krb/Attic/rd_priv.c,v 1.2 2003/06/17 04:24:36 dillon Exp $ */
35
36 #include "krb_locl.h"
37
38 RCSID("$Id: rd_priv.c,v 1.27 1999/12/02 16:58:43 joda Exp $");
39
40 /* application include files */
41 #include "krb-archaeology.h"
42
43 /*
44  * krb_rd_priv() decrypts and checks the integrity of an
45  * AUTH_MSG_PRIVATE message.  Given the message received, "in",
46  * the length of that message, "in_length", the key "schedule"
47  * and "key", and the network addresses of the
48  * "sender" and "receiver" of the message, krb_rd_safe() returns
49  * RD_AP_OK if the message is okay, otherwise some error code.
50  *
51  * The message data retrieved from "in" are returned in the structure
52  * "m_data".  The pointer to the application data
53  * (m_data->app_data) refers back to the appropriate place in "in".
54  *
55  * See the file "mk_priv.c" for the format of the AUTH_MSG_PRIVATE
56  * message.  The structure containing the extracted message
57  * information, MSG_DAT, is defined in "krb.h".
58  */
59
60 int32_t
61 krb_rd_priv(void *in, u_int32_t in_length, 
62             des_key_schedule schedule, des_cblock *key, 
63             struct sockaddr_in *sender, struct sockaddr_in *receiver, 
64             MSG_DAT *m_data)
65 {
66     unsigned char *p = (unsigned char*)in;
67     int little_endian;
68     u_int32_t clen;
69     struct timeval tv;
70     u_int32_t src_addr;
71     int delta_t;
72
73     unsigned char pvno, type;
74
75     pvno = *p++;
76     if(pvno != KRB_PROT_VERSION)
77         return RD_AP_VERSION;
78     
79     type = *p++;
80     little_endian = type & 1;
81     type &= ~1;
82
83     p += krb_get_int(p, &clen, 4, little_endian);
84     
85     if(clen + 2 > in_length)
86         return RD_AP_MODIFIED;
87
88     des_pcbc_encrypt((des_cblock*)p, (des_cblock*)p, clen, 
89                      schedule, key, DES_DECRYPT);
90     
91     p += krb_get_int(p, &m_data->app_length, 4, little_endian);
92     if(m_data->app_length + 17 > in_length)
93         return RD_AP_MODIFIED;
94
95     m_data->app_data = p;
96     p += m_data->app_length;
97     
98     m_data->time_5ms = *p++;
99
100     p += krb_get_address(p, &src_addr);
101
102     if (!krb_equiv(src_addr, sender->sin_addr.s_addr))
103         return RD_AP_BADD;
104
105     p += krb_get_int(p, (u_int32_t *)&m_data->time_sec, 4, little_endian);
106
107     m_data->time_sec = lsb_time(m_data->time_sec, sender, receiver);
108     
109     gettimeofday(&tv, NULL);
110
111     /* check the time integrity of the msg */
112     delta_t = abs((int)((long) tv.tv_sec - m_data->time_sec));
113     if (delta_t > CLOCK_SKEW)
114         return RD_AP_TIME;
115     if (krb_debug)
116         krb_warning("delta_t = %d\n", (int) delta_t);
117
118     /*
119      * caller must check timestamps for proper order and
120      * replays, since server might have multiple clients
121      * each with its own timestamps and we don't assume
122      * tightly synchronized clocks.
123      */
124
125     return KSUCCESS;
126 }