Change __signed to signed.
[dragonfly.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.2 2000/06/23 04:00:20 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 *a_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     const char *fn = a_fn;
173
174     unsigned char *p;
175
176     if (authent->length <= 0)
177         return(RD_AP_MODIFIED);
178
179     p = authent->dat;
180
181     /* get msg version, type and byte order, and server key version */
182
183     pvno = *p++;
184
185     if(pvno != KRB_PROT_VERSION)
186         return RD_AP_VERSION;
187     
188     type = *p++;
189     
190     little_endian = type & 1;
191     type &= ~1;
192     
193     if(type != AUTH_MSG_APPL_REQUEST && type != AUTH_MSG_APPL_REQUEST_MUTUAL)
194         return RD_AP_MSG_TYPE;
195
196     s_kvno = *p++;
197
198     p += krb_get_string(p, realm, sizeof(realm));
199
200     /*
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.
207      */
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;
211         st_kvno = s_kvno;
212         if (read_service_key(service, instance, realm, s_kvno,
213                              fn, (char *)skey))
214             return(RD_AP_UNDEC);
215         if ((status = krb_set_key((char*)skey, 0)))
216             return(status);
217         strlcpy (st_rlm, realm, REALM_SZ);
218         strlcpy (st_nam, service, SNAME_SZ);
219         strlcpy (st_inst, instance, INST_SZ);
220     }
221
222     tkt->length = *p++;
223
224     req_id->length = *p++;
225
226     if(tkt->length + (p - authent->dat) > authent->length)
227         return RD_AP_MODIFIED;
228
229     memcpy(tkt->dat, p, tkt->length);
230     p += tkt->length;
231
232     if (krb_ap_req_debug)
233         krb_log("ticket->length: %d",tkt->length);
234
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))
239         return RD_AP_UNDEC;
240     
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));
245     }
246
247     /* Extract the authenticator */
248     
249     if(req_id->length + (p - authent->dat) > authent->length)
250         return RD_AP_MODIFIED;
251
252     memcpy(req_id->dat, p, req_id->length);
253     p = req_id->dat;
254     
255 #ifndef NOENCRYPTION
256     /* And decrypt it with the session key from the ticket */
257     if (krb_ap_req_debug) krb_log("About to decrypt authenticator");
258
259     encrypt_ktext(req_id, &ad->session, DES_DECRYPT);
260
261     if (krb_ap_req_debug) krb_log("Done.");
262 #endif /* NOENCRYPTION */
263
264     /* cast req_id->length to int? */
265 #define check_ptr() if ((ptr - (char *) req_id->dat) > req_id->length) return(RD_AP_MODIFIED);
266
267     p += krb_get_nir(p,
268                      r_aname, sizeof(r_aname),
269                      r_inst, sizeof(r_inst),
270                      r_realm, sizeof(r_realm));
271
272     p += krb_get_int(p, &ad->checksum, 4, little_endian);
273
274     p++; /* time_5ms is not used */
275
276     p += krb_get_int(p, &r_time_sec, 4, little_endian);
277
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)
285         return RD_AP_INCON;
286     
287     if (krb_ap_req_debug)
288         krb_log("Address: %x %x", ad->address, from_addr);
289
290     if (from_addr && (!krb_equiv(ad->address, from_addr)))
291         return RD_AP_BADD;
292
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);
301         return RD_AP_TIME;
302     }
303
304     /* Now check for expiration of ticket */
305
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",
309                 (long)tv.tv_sec,
310                 (unsigned long)ad->time_sec,
311                 tkt_age,
312                 ad->life);
313     
314     if ((tkt_age < 0) && (-tkt_age > CLOCK_SKEW))
315         return RD_AP_NYV;
316
317     if (tv.tv_sec > krb_life_to_time(ad->time_sec, ad->life))
318         return RD_AP_EXP;
319
320     /* All seems OK */
321     ad->reply.length = 0;
322
323     return(RD_AP_OK);
324 }