]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/kerberosIV/lib/krb/rd_req.c
This commit was generated by cvs2svn to compensate for changes in r56889,
[FreeBSD/FreeBSD.git] / crypto / kerberosIV / lib / krb / rd_req.c
1 /*
2  * Copyright (c) 1995, 1996, 1997, 1998 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_req.c,v 1.27.2.1 1999/12/06 22:04:36 assar Exp $");
37
38 static struct timeval t_local = { 0, 0 };
39
40 /*
41  * Keep the following information around for subsequent calls
42  * to this routine by the same server using the same key.
43  */
44
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 */
51
52 /*
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
56  * for it.
57  *
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.
61  */
62
63 /*
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
72  * "serv_key".
73  *
74  * This routine returns the return value of des_key_sched.
75  *
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
78  * krb_rd_req().
79  */
80
81 int
82 krb_set_key(void *key, int cvt)
83 {
84 #ifdef NOENCRYPTION
85     memset(ky, 0, sizeof(ky));
86     return KSUCCESS;
87 #else /* Encrypt */
88     if (cvt)
89         des_string_to_key((char*)key, &ky);
90     else
91         memcpy((char*)ky, key, 8);
92     return(des_key_sched(&ky, serv_key));
93 #endif /* NOENCRYPTION */
94 }
95
96
97 /*
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
101  * identity.
102  *
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.)
117  *
118  * krb_rd_req() returns RD_AP_OK if the authentication information
119  * was genuine, or one of the following error codes (defined in
120  * "krb.h"):
121  *
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
132  *
133  * For the message format, see krb_mk_req().
134  *
135  * Mutual authentication is not implemented.
136  */
137
138 int
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 *fn)            /* Filename to get keys from */
145 {
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;
150
151     char realm[REALM_SZ];       /* Realm of issuing kerberos */
152
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 */
164
165     struct timeval tv;
166     int status;
167
168     int pvno;
169     int type;
170     int little_endian;
171
172     unsigned char *p;
173
174     if (authent->length <= 0)
175         return(RD_AP_MODIFIED);
176
177     p = authent->dat;
178
179     /* get msg version, type and byte order, and server key version */
180
181     pvno = *p++;
182
183     if(pvno != KRB_PROT_VERSION)
184         return RD_AP_VERSION;
185     
186     type = *p++;
187     
188     little_endian = type & 1;
189     type &= ~1;
190     
191     if(type != AUTH_MSG_APPL_REQUEST && type != AUTH_MSG_APPL_REQUEST_MUTUAL)
192         return RD_AP_MSG_TYPE;
193
194     s_kvno = *p++;
195
196     p += krb_get_string(p, realm, sizeof(realm));
197
198     /*
199      * If "fn" is NULL, key info should already be set; don't
200      * bother with ticket file.  Otherwise, check to see if we
201      * already have key info for the given server and key version
202      * (saved in the static st_* variables).  If not, go get it
203      * from the ticket file.  If "fn" is the null string, use the
204      * default ticket file.
205      */
206     if (fn && (strcmp(st_nam,service) || strcmp(st_inst,instance) ||
207                strcmp(st_rlm,realm) || (st_kvno != s_kvno))) {
208         if (*fn == 0) fn = (char *)KEYFILE;
209         st_kvno = s_kvno;
210         if (read_service_key(service, instance, realm, s_kvno,
211                              fn, (char *)skey))
212             return(RD_AP_UNDEC);
213         if ((status = krb_set_key((char*)skey, 0)))
214             return(status);
215         strlcpy (st_rlm, realm, REALM_SZ);
216         strlcpy (st_nam, service, SNAME_SZ);
217         strlcpy (st_inst, instance, INST_SZ);
218     }
219
220     tkt->length = *p++;
221
222     req_id->length = *p++;
223
224     if(tkt->length + (p - authent->dat) > authent->length)
225         return RD_AP_MODIFIED;
226
227     memcpy(tkt->dat, p, tkt->length);
228     p += tkt->length;
229
230     if (krb_ap_req_debug)
231         krb_log("ticket->length: %d",tkt->length);
232
233     /* Decrypt and take apart ticket */
234     if (decomp_ticket(tkt, &ad->k_flags, ad->pname, ad->pinst, ad->prealm,
235                       &ad->address, ad->session, &ad->life,
236                       &ad->time_sec, sname, iname, &ky, serv_key))
237         return RD_AP_UNDEC;
238     
239     if (krb_ap_req_debug) {
240         krb_log("Ticket Contents.");
241         krb_log(" Aname:   %s.%s",ad->pname, ad->prealm);
242         krb_log(" Service: %s", krb_unparse_name_long(sname, iname, NULL));
243     }
244
245     /* Extract the authenticator */
246     
247     if(req_id->length + (p - authent->dat) > authent->length)
248         return RD_AP_MODIFIED;
249
250     memcpy(req_id->dat, p, req_id->length);
251     p = req_id->dat;
252     
253 #ifndef NOENCRYPTION
254     /* And decrypt it with the session key from the ticket */
255     if (krb_ap_req_debug) krb_log("About to decrypt authenticator");
256
257     encrypt_ktext(req_id, &ad->session, DES_DECRYPT);
258
259     if (krb_ap_req_debug) krb_log("Done.");
260 #endif /* NOENCRYPTION */
261
262     /* cast req_id->length to int? */
263 #define check_ptr() if ((ptr - (char *) req_id->dat) > req_id->length) return(RD_AP_MODIFIED);
264
265     p += krb_get_nir(p, r_aname, r_inst, r_realm); /* XXX no rangecheck */
266
267     p += krb_get_int(p, &ad->checksum, 4, little_endian);
268
269     p++; /* time_5ms is not used */
270
271     p += krb_get_int(p, &r_time_sec, 4, little_endian);
272
273     /* Check for authenticity of the request */
274     if (krb_ap_req_debug)
275         krb_log("Principal: %s.%s@%s / %s.%s@%s",ad->pname,ad->pinst, ad->prealm, 
276               r_aname, r_inst, r_realm);
277     if (strcmp(ad->pname, r_aname) != 0 ||
278         strcmp(ad->pinst, r_inst) != 0 ||
279         strcmp(ad->prealm, r_realm) != 0)
280         return RD_AP_INCON;
281     
282     if (krb_ap_req_debug)
283         krb_log("Address: %x %x", ad->address, from_addr);
284
285     if (from_addr && (!krb_equiv(ad->address, from_addr)))
286         return RD_AP_BADD;
287
288     gettimeofday(&tv, NULL);
289     delta_t = abs((int)(tv.tv_sec - r_time_sec));
290     if (delta_t > CLOCK_SKEW) {
291         if (krb_ap_req_debug)
292             krb_log("Time out of range: %lu - %lu = %lu",
293                     (unsigned long)t_local.tv_sec,
294                     (unsigned long)r_time_sec,
295                     (unsigned long)delta_t);
296         return RD_AP_TIME;
297     }
298
299     /* Now check for expiration of ticket */
300
301     tkt_age = tv.tv_sec - ad->time_sec;
302     if (krb_ap_req_debug)
303         krb_log("Time: %ld Issue Date: %lu Diff: %ld Life %x",
304                 (long)tv.tv_sec,
305                 (unsigned long)ad->time_sec,
306                 tkt_age,
307                 ad->life);
308     
309     if ((tkt_age < 0) && (-tkt_age > CLOCK_SKEW))
310         return RD_AP_NYV;
311
312     if (tv.tv_sec > krb_life_to_time(ad->time_sec, ad->life))
313         return RD_AP_EXP;
314
315     /* All seems OK */
316     ad->reply.length = 0;
317
318     return(RD_AP_OK);
319 }