2 * Copyright (c) 1995, 1996, 1997, 1998 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_req.c,v 1.27.2.2 2000/06/23 04:00:20 assar Exp $");
38 static struct timeval t_local = { 0, 0 };
41 * Keep the following information around for subsequent calls
42 * to this routine by the same server using the same key.
45 static des_key_schedule serv_key; /* Key sched to decrypt ticket */
46 static des_cblock ky; /* Initialization vector */
47 static int st_kvno; /* version number for this key */
48 static char st_rlm[REALM_SZ]; /* server's realm */
49 static char st_nam[ANAME_SZ]; /* service name */
50 static char st_inst[INST_SZ]; /* server's instance */
53 * This file contains two functions. krb_set_key() takes a DES
54 * key or password string and returns a DES key (either the original
55 * key, or the password converted into a DES key) and a key schedule
58 * krb_rd_req() reads an authentication request and returns information
59 * about the identity of the requestor, or an indication that the
60 * identity information was not authentic.
64 * krb_set_key() takes as its first argument either a DES key or a
65 * password string. The "cvt" argument indicates how the first
66 * argument "key" is to be interpreted: if "cvt" is null, "key" is
67 * taken to be a DES key; if "cvt" is non-null, "key" is taken to
68 * be a password string, and is converted into a DES key using
69 * string_to_key(). In either case, the resulting key is returned
70 * in the external static variable "ky". A key schedule is
71 * generated for "ky" and returned in the external static variable
74 * This routine returns the return value of des_key_sched.
76 * krb_set_key() needs to be in the same .o file as krb_rd_req() so that
77 * the key set by krb_set_key() is available in private storage for
82 krb_set_key(void *key, int cvt)
85 memset(ky, 0, sizeof(ky));
89 des_string_to_key((char*)key, &ky);
91 memcpy((char*)ky, key, 8);
92 return(des_key_sched(&ky, serv_key));
93 #endif /* NOENCRYPTION */
98 * krb_rd_req() takes an AUTH_MSG_APPL_REQUEST or
99 * AUTH_MSG_APPL_REQUEST_MUTUAL message created by krb_mk_req(),
100 * checks its integrity and returns a judgement as to the requestor's
103 * The "authent" argument is a pointer to the received message.
104 * The "service" and "instance" arguments name the receiving server,
105 * and are used to get the service's ticket to decrypt the ticket
106 * in the message, and to compare against the server name inside the
107 * ticket. "from_addr" is the network address of the host from which
108 * the message was received; this is checked against the network
109 * address in the ticket. If "from_addr" is zero, the check is not
110 * performed. "ad" is an AUTH_DAT structure which is
111 * filled in with information about the sender's identity according
112 * to the authenticator and ticket sent in the message. Finally,
113 * "fn" contains the name of the file containing the server's key.
114 * (If "fn" is NULL, the server's key is assumed to have been set
115 * by krb_set_key(). If "fn" is the null string ("") the default
116 * file KEYFILE, defined in "krb.h", is used.)
118 * krb_rd_req() returns RD_AP_OK if the authentication information
119 * was genuine, or one of the following error codes (defined in
122 * RD_AP_VERSION - wrong protocol version number
123 * RD_AP_MSG_TYPE - wrong message type
124 * RD_AP_UNDEC - couldn't decipher the message
125 * RD_AP_INCON - inconsistencies found
126 * RD_AP_BADD - wrong network address
127 * RD_AP_TIME - client time (in authenticator)
128 * too far off server time
129 * RD_AP_NYV - Kerberos time (in ticket) too
130 * far off server time
131 * RD_AP_EXP - ticket expired
133 * For the message format, see krb_mk_req().
135 * Mutual authentication is not implemented.
139 krb_rd_req(KTEXT authent, /* The received message */
140 char *service, /* Service name */
141 char *instance, /* Service instance */
142 int32_t from_addr, /* Net address of originating host */
143 AUTH_DAT *ad, /* Structure to be filled in */
144 char *a_fn) /* Filename to get keys from */
146 static KTEXT_ST ticket; /* Temp storage for ticket */
147 static KTEXT tkt = &ticket;
148 static KTEXT_ST req_id_st; /* Temp storage for authenticator */
149 KTEXT req_id = &req_id_st;
151 char realm[REALM_SZ]; /* Realm of issuing kerberos */
153 unsigned char skey[KKEY_SZ]; /* Session key from ticket */
154 char sname[SNAME_SZ]; /* Service name from ticket */
155 char iname[INST_SZ]; /* Instance name from ticket */
156 char r_aname[ANAME_SZ]; /* Client name from authenticator */
157 char r_inst[INST_SZ]; /* Client instance from authenticator */
158 char r_realm[REALM_SZ]; /* Client realm from authenticator */
159 u_int32_t r_time_sec; /* Coarse time from authenticator */
160 unsigned long delta_t; /* Time in authenticator - local time */
161 long tkt_age; /* Age of ticket */
162 static unsigned char s_kvno;/* Version number of the server's key
163 * Kerberos used to encrypt ticket */
172 const char *fn = a_fn;
176 if (authent->length <= 0)
177 return(RD_AP_MODIFIED);
181 /* get msg version, type and byte order, and server key version */
185 if(pvno != KRB_PROT_VERSION)
186 return RD_AP_VERSION;
190 little_endian = type & 1;
193 if(type != AUTH_MSG_APPL_REQUEST && type != AUTH_MSG_APPL_REQUEST_MUTUAL)
194 return RD_AP_MSG_TYPE;
198 p += krb_get_string(p, realm, sizeof(realm));
201 * If "fn" is NULL, key info should already be set; don't
202 * bother with ticket file. Otherwise, check to see if we
203 * already have key info for the given server and key version
204 * (saved in the static st_* variables). If not, go get it
205 * from the ticket file. If "fn" is the null string, use the
206 * default ticket file.
208 if (fn && (strcmp(st_nam,service) || strcmp(st_inst,instance) ||
209 strcmp(st_rlm,realm) || (st_kvno != s_kvno))) {
210 if (*fn == 0) fn = (char *)KEYFILE;
212 if (read_service_key(service, instance, realm, s_kvno,
215 if ((status = krb_set_key((char*)skey, 0)))
217 strlcpy (st_rlm, realm, REALM_SZ);
218 strlcpy (st_nam, service, SNAME_SZ);
219 strlcpy (st_inst, instance, INST_SZ);
224 req_id->length = *p++;
226 if(tkt->length + (p - authent->dat) > authent->length)
227 return RD_AP_MODIFIED;
229 memcpy(tkt->dat, p, tkt->length);
232 if (krb_ap_req_debug)
233 krb_log("ticket->length: %d",tkt->length);
235 /* Decrypt and take apart ticket */
236 if (decomp_ticket(tkt, &ad->k_flags, ad->pname, ad->pinst, ad->prealm,
237 &ad->address, ad->session, &ad->life,
238 &ad->time_sec, sname, iname, &ky, serv_key))
241 if (krb_ap_req_debug) {
242 krb_log("Ticket Contents.");
243 krb_log(" Aname: %s.%s",ad->pname, ad->prealm);
244 krb_log(" Service: %s", krb_unparse_name_long(sname, iname, NULL));
247 /* Extract the authenticator */
249 if(req_id->length + (p - authent->dat) > authent->length)
250 return RD_AP_MODIFIED;
252 memcpy(req_id->dat, p, req_id->length);
256 /* And decrypt it with the session key from the ticket */
257 if (krb_ap_req_debug) krb_log("About to decrypt authenticator");
259 encrypt_ktext(req_id, &ad->session, DES_DECRYPT);
261 if (krb_ap_req_debug) krb_log("Done.");
262 #endif /* NOENCRYPTION */
264 /* cast req_id->length to int? */
265 #define check_ptr() if ((ptr - (char *) req_id->dat) > req_id->length) return(RD_AP_MODIFIED);
268 r_aname, sizeof(r_aname),
269 r_inst, sizeof(r_inst),
270 r_realm, sizeof(r_realm));
272 p += krb_get_int(p, &ad->checksum, 4, little_endian);
274 p++; /* time_5ms is not used */
276 p += krb_get_int(p, &r_time_sec, 4, little_endian);
278 /* Check for authenticity of the request */
279 if (krb_ap_req_debug)
280 krb_log("Principal: %s.%s@%s / %s.%s@%s",ad->pname,ad->pinst, ad->prealm,
281 r_aname, r_inst, r_realm);
282 if (strcmp(ad->pname, r_aname) != 0 ||
283 strcmp(ad->pinst, r_inst) != 0 ||
284 strcmp(ad->prealm, r_realm) != 0)
287 if (krb_ap_req_debug)
288 krb_log("Address: %x %x", ad->address, from_addr);
290 if (from_addr && (!krb_equiv(ad->address, from_addr)))
293 gettimeofday(&tv, NULL);
294 delta_t = abs((int)(tv.tv_sec - r_time_sec));
295 if (delta_t > CLOCK_SKEW) {
296 if (krb_ap_req_debug)
297 krb_log("Time out of range: %lu - %lu = %lu",
298 (unsigned long)t_local.tv_sec,
299 (unsigned long)r_time_sec,
300 (unsigned long)delta_t);
304 /* Now check for expiration of ticket */
306 tkt_age = tv.tv_sec - ad->time_sec;
307 if (krb_ap_req_debug)
308 krb_log("Time: %ld Issue Date: %lu Diff: %ld Life %x",
310 (unsigned long)ad->time_sec,
314 if ((tkt_age < 0) && (-tkt_age > CLOCK_SKEW))
317 if (tv.tv_sec > krb_life_to_time(ad->time_sec, ad->life))
321 ad->reply.length = 0;