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