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: mk_safe.c,v 1.25.2.1 2000/10/10 13:19:25 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 */
53 void fixup_quad_cksum(void*, size_t, des_cblock*, void*, void*, int);
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.
62 * The "out" packet consists of:
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
71 * ===================== begin checksum ================================
73 * 4 bytes length length of user data
75 * 1 byte msg_time_5ms timestamp milliseconds
76 * 4 bytes sender->sin.addr.s_addr sender's IP address
78 * 4 bytes msg_time_sec or timestamp seconds with
79 * -msg_time_sec direction in sign bit
81 * ======================= end checksum ================================
83 * 16 bytes big_cksum quadratic checksum of
88 krb_mk_safe(void *in, void *out, u_int32_t length, des_cblock *key,
89 struct sockaddr_in *sender, struct sockaddr_in *receiver)
91 unsigned char * p = (unsigned char*)out;
96 p += krb_put_int(KRB_PROT_VERSION, p, 1, 1);
97 p += krb_put_int(AUTH_MSG_SAFE, p, 1, 1);
101 p += krb_put_int(length, p, 4, 4);
103 memcpy(p, in, length);
106 krb_kdctimeofday(&tv);
108 *p++ = tv.tv_usec/5000; /* 5ms */
110 src_addr = sender->sin_addr.s_addr;
111 p += krb_put_address(src_addr, p, 4);
113 p += krb_put_int(lsb_time(tv.tv_sec, sender, receiver), p, 4, 4);
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).
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);
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);
130 memcpy(p, new_checksum, 16);
134 return p - (unsigned char*)out;