A couple bug reports/patches from Ed Phillips (flaregun@udel.edu)
[krb5.git] / src / lib / krb5 / krb / auth_con.c
1
2 #include "k5-int.h"
3 #include "auth_con.h"
4
5 krb5_error_code
6 krb5_auth_con_init(context, auth_context)
7     krb5_context          context;
8     krb5_auth_context  * auth_context;
9 {
10     *auth_context =
11             (krb5_auth_context)malloc(sizeof(struct _krb5_auth_context));
12     if (!*auth_context)
13             return ENOMEM;
14     
15     memset(*auth_context, 0, sizeof(struct _krb5_auth_context));
16
17     /* Default flags, do time not seq */
18     (*auth_context)->auth_context_flags = 
19             KRB5_AUTH_CONTEXT_DO_TIME |  KRB5_AUTH_CONN_INITIALIZED;
20
21     (*auth_context)->cksumtype = CKSUMTYPE_RSA_MD4_DES;
22     /* (*auth_context)->cksumtype = CKSUMTYPE_CRC32; */
23     return 0;
24 }
25
26 krb5_error_code
27 krb5_auth_con_free(context, auth_context)
28     krb5_context          context;
29     krb5_auth_context     auth_context;
30 {
31     if (auth_context->local_addr) 
32         free(auth_context->local_addr);
33     if (auth_context->remote_addr) 
34         free(auth_context->remote_addr);
35     if (auth_context->local_port) 
36         free(auth_context->local_port);
37     if (auth_context->remote_port) 
38         free(auth_context->remote_port);
39     if (auth_context->authentp) 
40         krb5_free_authenticator(context, auth_context->authentp);
41     if (auth_context->keyblock) 
42         krb5_free_keyblock(context, auth_context->keyblock);
43     if (auth_context->local_subkey) 
44         krb5_free_keyblock(context, auth_context->local_subkey);
45     if (auth_context->remote_subkey) 
46         krb5_free_keyblock(context, auth_context->remote_subkey);
47     if (auth_context->rcache)
48         krb5_rc_close(context, auth_context->rcache);
49     free(auth_context);
50     return 0;
51 }
52
53 krb5_error_code
54 krb5_auth_con_setaddrs(context, auth_context, local_addr, remote_addr)
55     krb5_context          context;
56     krb5_auth_context     auth_context;
57     krb5_address        * local_addr;
58     krb5_address        * remote_addr;
59 {
60     /* Free old addresses */
61     if (auth_context->local_addr) 
62         free(auth_context->local_addr);
63     if (auth_context->remote_addr) 
64         free(auth_context->remote_addr);
65
66     if (local_addr) {
67         if ((auth_context->local_addr = (krb5_address *)
68                 malloc(sizeof(krb5_address) + local_addr->length)) == NULL) {
69             return ENOMEM;
70         }
71         auth_context->local_addr->addrtype = local_addr->addrtype;
72         auth_context->local_addr->length = local_addr->length;
73         auth_context->local_addr->contents = (krb5_octet *)
74           auth_context->local_addr + sizeof(krb5_address);
75         memcpy(auth_context->local_addr->contents,
76                local_addr->contents, local_addr->length);
77     } else {
78         auth_context->local_addr = NULL;
79     }
80
81     if (remote_addr) {
82         if ((auth_context->remote_addr = (krb5_address *)
83                 malloc(sizeof(krb5_address) + remote_addr->length)) == NULL) {
84             if (auth_context->local_addr)
85                 free(auth_context->local_addr);
86             return ENOMEM;
87         }
88         auth_context->remote_addr->addrtype = remote_addr->addrtype;
89         auth_context->remote_addr->length = remote_addr->length;
90         auth_context->remote_addr->contents = (krb5_octet *)
91           auth_context->remote_addr + sizeof(krb5_address);
92         memcpy(auth_context->remote_addr->contents,
93                remote_addr->contents, remote_addr->length);
94     } else {
95         auth_context->remote_addr = NULL;
96     }
97     return 0;
98 }
99
100 krb5_error_code
101 krb5_auth_con_getaddrs(context, auth_context, local_addr, remote_addr)
102     krb5_context          context;
103     krb5_auth_context     auth_context;
104     krb5_address       ** local_addr;
105     krb5_address       ** remote_addr;
106 {
107     krb5_address        * tmp_addr;
108
109     if (local_addr && auth_context->local_addr) {
110         if (!(tmp_addr = (krb5_address *)malloc(sizeof(krb5_address))))
111             return ENOMEM;
112         if ((tmp_addr->contents = malloc(auth_context->local_addr->length))) {
113             memcpy(tmp_addr->contents, auth_context->local_addr->contents,
114                    auth_context->local_addr->length);
115             tmp_addr->addrtype = auth_context->local_addr->addrtype;
116             tmp_addr->length = auth_context->local_addr->length;
117             *local_addr = tmp_addr;
118         } else {
119             free(tmp_addr);
120             return ENOMEM;
121         }
122     }
123     if ((remote_addr) && auth_context->remote_addr) {
124         if ((tmp_addr = (krb5_address *)malloc(sizeof(krb5_address))) == NULL) {
125             if (local_addr && auth_context->local_addr) {
126                 krb5_free_address(context, *local_addr);
127             }
128             return ENOMEM;
129         }
130         if ((tmp_addr->contents = malloc(auth_context->remote_addr->length))) {
131             memcpy(tmp_addr->contents, auth_context->remote_addr->contents,
132                    auth_context->remote_addr->length);
133             tmp_addr->addrtype = auth_context->remote_addr->addrtype;
134             tmp_addr->length = auth_context->remote_addr->length;
135             *remote_addr = tmp_addr;
136         } else {
137             if (local_addr && auth_context->local_addr) {
138                 krb5_free_address(context, *local_addr);
139             }
140             free(tmp_addr);
141             return ENOMEM;
142         }
143     }
144     return 0 ;
145 }
146
147 krb5_error_code
148 krb5_auth_con_setports(context, auth_context, local_port, remote_port)
149     krb5_context          context;
150     krb5_auth_context     auth_context;
151     krb5_address        * local_port;
152     krb5_address        * remote_port;
153 {
154     /* Free old addresses */
155     if (auth_context->local_port) 
156         free(auth_context->local_port);
157     if (auth_context->remote_port) 
158         free(auth_context->remote_port);
159
160     if (local_port) {
161         if (((auth_context->local_port = (krb5_address *)
162                 malloc(sizeof(krb5_address) + local_port->length)) == NULL)) {
163             return ENOMEM;
164         }
165         auth_context->local_port->addrtype = local_port->addrtype;
166         auth_context->local_port->length = local_port->length;
167         auth_context->local_port->contents = (krb5_octet *)
168           auth_context->local_port + sizeof(krb5_address);
169         memcpy(auth_context->local_port->contents,
170                local_port->contents, local_port->length);
171     } else {
172         auth_context->local_port = NULL;
173     }
174
175     if (remote_port) {
176         if ((auth_context->remote_port = (krb5_address *)
177                 malloc(sizeof(krb5_address) + remote_port->length)) == NULL) {
178             if (auth_context->local_port)
179                 free(auth_context->local_port);
180             return ENOMEM;
181         }
182         auth_context->remote_port->addrtype = remote_port->addrtype;
183         auth_context->remote_port->length = remote_port->length;
184         auth_context->remote_port->contents = (krb5_octet *)
185           auth_context->remote_port + sizeof(krb5_address);
186         memcpy(auth_context->remote_port->contents,
187                remote_port->contents, remote_port->length);
188     } else {
189         auth_context->remote_port = NULL;
190     }
191     return 0;
192 }
193
194
195 /*
196  * This function overloads the keyblock field. It is only useful prior to
197  * a krb5_rd_req_decode() call for user to user authentication where the
198  * server has the key and needs to use it to decrypt the incoming request.
199  * Once decrypted this key is no longer necessary and is then overwritten
200  * with the session key sent by the client.
201  */
202 krb5_error_code
203 krb5_auth_con_setuseruserkey(context, auth_context, keyblock)
204     krb5_context          context;
205     krb5_auth_context     auth_context;
206     krb5_keyblock       * keyblock;             
207 {
208     if (auth_context->keyblock)
209         krb5_free_keyblock(context, auth_context->keyblock);
210     return(krb5_copy_keyblock(context, keyblock, &(auth_context->keyblock)));
211 }
212
213 krb5_error_code
214 krb5_auth_con_getkey(context, auth_context, keyblock)
215     krb5_context          context;
216     krb5_auth_context     auth_context;
217     krb5_keyblock      ** keyblock;             
218 {
219     if (auth_context->keyblock)
220         return krb5_copy_keyblock(context, auth_context->keyblock, keyblock);
221     *keyblock = NULL;
222     return 0;
223 }
224
225 krb5_error_code
226 krb5_auth_con_getlocalsubkey(context, auth_context, keyblock)
227     krb5_context          context;
228     krb5_auth_context     auth_context;
229     krb5_keyblock      ** keyblock;             
230 {
231     if (auth_context->local_subkey)
232         return krb5_copy_keyblock(context,auth_context->local_subkey,keyblock);
233     *keyblock = NULL;
234     return 0;
235 }
236
237 krb5_error_code
238 krb5_auth_con_getremotesubkey(context, auth_context, keyblock)
239     krb5_context          context;
240     krb5_auth_context     auth_context;
241     krb5_keyblock      ** keyblock;             
242 {
243     if (auth_context->remote_subkey)
244         return krb5_copy_keyblock(context,auth_context->remote_subkey,keyblock);
245     *keyblock = NULL;
246     return 0;
247 }
248
249 krb5_error_code
250 krb5_auth_con_setcksumtype(context, auth_context, cksumtype)
251     krb5_context          context;
252     krb5_auth_context     auth_context;
253     krb5_cksumtype        cksumtype;            
254 {
255     auth_context->cksumtype = cksumtype;
256     return 0;
257 }
258
259 krb5_error_code
260 krb5_auth_con_getlocalseqnumber(context, auth_context, seqnumber)
261     krb5_context          context;
262     krb5_auth_context     auth_context;
263     krb5_int32          * seqnumber;            
264 {
265     *seqnumber = auth_context->local_seq_number;
266     return 0;
267 }
268
269 krb5_error_code
270 krb5_auth_con_getauthenticator(context, auth_context, authenticator)
271     krb5_context          context;
272     krb5_auth_context     auth_context;
273     krb5_authenticator ** authenticator;                
274 {
275     return (krb5_copy_authenticator(context, auth_context->authentp,
276                                     authenticator));
277 }
278
279 krb5_error_code
280 krb5_auth_con_getremoteseqnumber(context, auth_context, seqnumber)
281     krb5_context          context;
282     krb5_auth_context     auth_context;
283     krb5_int32          * seqnumber;            
284 {
285     *seqnumber = auth_context->remote_seq_number;
286     return 0;
287 }
288
289 krb5_error_code
290 krb5_auth_con_initivector(context, auth_context)
291     krb5_context          context;
292     krb5_auth_context     auth_context;
293 {
294     if (auth_context->keyblock) {
295         int size = krb5_keytype_array[auth_context->keyblock->keytype]->
296                       system->block_length;
297
298         if ((auth_context->i_vector = (krb5_pointer)malloc(size))) {
299             memset(auth_context->i_vector, 0, size);
300             return 0;
301         }
302         return ENOMEM;
303     }
304     return EINVAL; /* XXX need an error for no keyblock */
305 }
306
307 krb5_error_code
308 krb5_auth_con_setivector(context, auth_context, ivector)
309     krb5_context          context;
310     krb5_auth_context     auth_context;
311     krb5_pointer          ivector;
312 {
313     auth_context->i_vector = ivector;
314     return 0;
315 }
316
317 krb5_error_code
318 krb5_auth_con_getivector(context, auth_context, ivector)
319     krb5_context          context;
320     krb5_auth_context     auth_context;
321     krb5_pointer        * ivector;
322 {
323     *ivector = auth_context->i_vector;
324     return 0;
325 }
326
327 krb5_error_code
328 krb5_auth_con_setflags(context, auth_context, flags)
329     krb5_context          context;
330     krb5_auth_context     auth_context;
331     krb5_int32            flags;
332 {
333     auth_context->auth_context_flags = flags;
334     return 0;
335 }
336
337 krb5_error_code
338 krb5_auth_con_getflags(context, auth_context, flags)
339     krb5_context          context;
340     krb5_auth_context     auth_context;
341     krb5_int32          * flags;
342 {
343     *flags = auth_context->auth_context_flags;
344     return 0;
345 }
346
347 krb5_error_code
348 krb5_auth_con_setrcache(context, auth_context, rcache)
349     krb5_context          context;
350     krb5_auth_context     auth_context;
351     krb5_rcache           rcache;
352 {
353     auth_context->rcache = rcache;
354     return 0;
355 }
356     
357 krb5_error_code
358 krb5_auth_con_getrcache(context, auth_context, rcache)
359     krb5_context          context;
360     krb5_auth_context     auth_context;
361     krb5_rcache         * rcache;
362 {
363     *rcache = auth_context->rcache;
364     return 0;
365 }
366