Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / mk_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: mk_safe.c,v 1.25.2.1 2000/10/10 13:19:25 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 /* from rd_safe.c */
52 extern int dqc_type;
53 void fixup_quad_cksum(void*, size_t, des_cblock*, void*, void*, int);
54
55 /*
56  * krb_mk_safe() constructs an AUTH_MSG_SAFE message.  It takes some
57  * user data "in" of "length" bytes and creates a packet in "out"
58  * consisting of the user data, a timestamp, and the sender's network
59  * address, followed by a checksum computed on the above, using the
60  * given "key".  The length of the resulting packet is returned.
61  *
62  * The "out" packet consists of:
63  *
64  * Size                 Variable                Field
65  * ----                 --------                -----
66  *
67  * 1 byte               KRB_PROT_VERSION        protocol version number
68  * 1 byte               AUTH_MSG_SAFE |         message type plus local
69  *                      HOST_BYTE_ORDER         byte order in low bit
70  *
71  * ===================== begin checksum ================================
72  * 
73  * 4 bytes              length                  length of user data
74  * length               in                      user data
75  * 1 byte               msg_time_5ms            timestamp milliseconds
76  * 4 bytes              sender->sin.addr.s_addr sender's IP address
77  *
78  * 4 bytes              msg_time_sec or         timestamp seconds with
79  *                      -msg_time_sec           direction in sign bit
80  *
81  * ======================= end checksum ================================
82  *
83  * 16 bytes             big_cksum               quadratic checksum of
84  *                                              above using "key"
85  */
86
87 int32_t
88 krb_mk_safe(void *in, void *out, u_int32_t length, des_cblock *key, 
89             struct sockaddr_in *sender, struct sockaddr_in *receiver)
90 {
91     unsigned char * p = (unsigned char*)out;
92     struct timeval tv;
93     unsigned char *start;
94     u_int32_t src_addr;
95
96     p += krb_put_int(KRB_PROT_VERSION, p, 1, 1);
97     p += krb_put_int(AUTH_MSG_SAFE, p, 1, 1);
98     
99     start = p;
100
101     p += krb_put_int(length, p, 4, 4);
102
103     memcpy(p, in, length);
104     p += length;
105     
106     krb_kdctimeofday(&tv);
107
108     *p++ = tv.tv_usec/5000; /* 5ms */
109     
110     src_addr = sender->sin_addr.s_addr;
111     p += krb_put_address(src_addr, p, 4);
112
113     p += krb_put_int(lsb_time(tv.tv_sec, sender, receiver), p, 4, 4);
114
115     {
116         /* We are faking big endian mode, so we need to fix the
117          * checksum (that is byte order dependent). We always send a
118          * checksum of the new type, unless we know that we are
119          * talking to an old client (this requires a call to
120          * krb_rd_safe first).  
121          */
122         unsigned char new_checksum[16];
123         unsigned char old_checksum[16];
124         fixup_quad_cksum(start, p - start, key, new_checksum, old_checksum, 0);
125         
126         if((dqc_type == DES_QUAD_GUESS && DES_QUAD_DEFAULT == DES_QUAD_OLD) || 
127            dqc_type == DES_QUAD_OLD)
128             memcpy(p, old_checksum, 16);
129         else
130             memcpy(p, new_checksum, 16);
131     }
132     p += 16;
133
134     return p - (unsigned char*)out;
135 }