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