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