Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / kdc_reply.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: kdc_reply.c,v 1.12.2.2 2000/12/04 14:34:28 assar Exp $");
37
38 static int little_endian; /* XXX ugly */
39
40 int
41 kdc_reply_cred(KTEXT cip, CREDENTIALS *cred)
42 {
43     unsigned char *p = cip->dat;
44     
45     memcpy(cred->session, p, 8);
46     p += 8;
47     
48     if(p + strlen((char*)p) > cip->dat + cip->length)
49         return INTK_BADPW;
50     p += krb_get_string(p, cred->service, sizeof(cred->service));
51     
52     if(p + strlen((char*)p) > cip->dat + cip->length)
53         return INTK_BADPW;
54     p += krb_get_string(p, cred->instance, sizeof(cred->instance));
55     
56     if(p + strlen((char*)p) > cip->dat + cip->length)
57         return INTK_BADPW;
58     p += krb_get_string(p, cred->realm, sizeof(cred->realm));
59     
60     if(p + 3 > cip->dat + cip->length)
61         return INTK_BADPW;
62     cred->lifetime = *p++;
63     cred->kvno = *p++;
64     cred->ticket_st.length = *p++;
65     
66     if(p + cred->ticket_st.length + 4 > cip->dat + cip->length)
67         return INTK_BADPW;
68     memcpy(cred->ticket_st.dat, p, cred->ticket_st.length);
69     p += cred->ticket_st.length;
70     
71     p += krb_get_int(p, (u_int32_t *)&cred->issue_date, 4, little_endian);
72     
73     return KSUCCESS;
74 }
75
76 int
77 kdc_reply_cipher(KTEXT reply, KTEXT cip)
78 {
79     unsigned char *p;
80     unsigned char pvno;
81     unsigned char type;
82
83     char aname[ANAME_SZ];
84     char inst[INST_SZ];
85     char realm[REALM_SZ];
86     
87     u_int32_t kdc_time;
88     u_int32_t exp_date;
89     u_int32_t clen;
90
91     p = reply->dat;
92
93     pvno = *p++;
94
95     if (pvno != KRB_PROT_VERSION )
96         return INTK_PROT;
97     
98     type = *p++;
99     little_endian = type & 1;
100     
101     type &= ~1;
102
103     if(type == AUTH_MSG_ERR_REPLY){
104         u_int32_t code;
105         /* skip these fields */
106         p += strlen((char*)p) + 1; /* name */
107         p += strlen((char*)p) + 1; /* instance */
108         p += strlen((char*)p) + 1; /* realm */
109         p += 4; /* time */
110         p += krb_get_int(p, &code, 4, little_endian);
111         if(code == 0)
112             code = KFAILURE; /* things will go bad otherwise */
113         return code;
114     }
115     if(type != AUTH_MSG_KDC_REPLY)
116         return INTK_PROT;
117
118     p += krb_get_nir(p,
119                      aname, sizeof(aname),
120                      inst, sizeof(inst),
121                      realm, sizeof(realm));
122     p += krb_get_int(p, &kdc_time, 4, little_endian);
123     p++; /* number of tickets */
124     p += krb_get_int(p, &exp_date, 4, little_endian);
125     p++; /* master key version number */
126     p += krb_get_int(p, &clen, 2, little_endian);
127     if (reply->length - (p - reply->dat) < clen)
128         return INTK_PROT;
129
130     cip->length = clen;
131     memcpy(cip->dat, p, clen);
132     p += clen;
133     
134     return KSUCCESS;
135 }