1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include "krb5_locl.h"
35b528cefcSMark Murray
36*ae771770SStanislav Sedov #ifndef HEIMDAL_SMALLER
37b528cefcSMark Murray
38b528cefcSMark Murray static krb5_error_code
make_pa_enc_timestamp(krb5_context context,PA_DATA * pa,krb5_enctype etype,krb5_keyblock * key)39b528cefcSMark Murray make_pa_enc_timestamp(krb5_context context, PA_DATA *pa,
40b528cefcSMark Murray krb5_enctype etype, krb5_keyblock *key)
41b528cefcSMark Murray {
42b528cefcSMark Murray PA_ENC_TS_ENC p;
430cadf2f4SJacques Vidrine unsigned char *buf;
440cadf2f4SJacques Vidrine size_t buf_size;
45*ae771770SStanislav Sedov size_t len = 0;
46b528cefcSMark Murray EncryptedData encdata;
47b528cefcSMark Murray krb5_error_code ret;
48c19800e8SDoug Rabson int32_t usec;
49b528cefcSMark Murray int usec2;
50b528cefcSMark Murray krb5_crypto crypto;
51b528cefcSMark Murray
52c19800e8SDoug Rabson krb5_us_timeofday (context, &p.patimestamp, &usec);
53b528cefcSMark Murray usec2 = usec;
54b528cefcSMark Murray p.pausec = &usec2;
55b528cefcSMark Murray
560cadf2f4SJacques Vidrine ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
57b528cefcSMark Murray if (ret)
58b528cefcSMark Murray return ret;
590cadf2f4SJacques Vidrine if(buf_size != len)
600cadf2f4SJacques Vidrine krb5_abortx(context, "internal error in ASN.1 encoder");
615e9cd1aeSAssar Westerlund ret = krb5_crypto_init(context, key, 0, &crypto);
620cadf2f4SJacques Vidrine if (ret) {
630cadf2f4SJacques Vidrine free(buf);
645e9cd1aeSAssar Westerlund return ret;
650cadf2f4SJacques Vidrine }
66b528cefcSMark Murray ret = krb5_encrypt_EncryptedData(context,
67b528cefcSMark Murray crypto,
68b528cefcSMark Murray KRB5_KU_PA_ENC_TIMESTAMP,
690cadf2f4SJacques Vidrine buf,
70b528cefcSMark Murray len,
71b528cefcSMark Murray 0,
72b528cefcSMark Murray &encdata);
730cadf2f4SJacques Vidrine free(buf);
74b528cefcSMark Murray krb5_crypto_destroy(context, crypto);
75b528cefcSMark Murray if (ret)
76b528cefcSMark Murray return ret;
77b528cefcSMark Murray
780cadf2f4SJacques Vidrine ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
79b528cefcSMark Murray free_EncryptedData(&encdata);
80b528cefcSMark Murray if (ret)
81b528cefcSMark Murray return ret;
820cadf2f4SJacques Vidrine if(buf_size != len)
830cadf2f4SJacques Vidrine krb5_abortx(context, "internal error in ASN.1 encoder");
845e9cd1aeSAssar Westerlund pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
850cadf2f4SJacques Vidrine pa->padata_value.length = len;
860cadf2f4SJacques Vidrine pa->padata_value.data = buf;
87b528cefcSMark Murray return 0;
88b528cefcSMark Murray }
89b528cefcSMark Murray
90b528cefcSMark Murray static krb5_error_code
add_padata(krb5_context context,METHOD_DATA * md,krb5_principal client,krb5_key_proc key_proc,krb5_const_pointer keyseed,krb5_enctype * enctypes,unsigned netypes,krb5_salt * salt)91b528cefcSMark Murray add_padata(krb5_context context,
92b528cefcSMark Murray METHOD_DATA *md,
93b528cefcSMark Murray krb5_principal client,
94b528cefcSMark Murray krb5_key_proc key_proc,
95b528cefcSMark Murray krb5_const_pointer keyseed,
964137ff4cSJacques Vidrine krb5_enctype *enctypes,
97b528cefcSMark Murray unsigned netypes,
98b528cefcSMark Murray krb5_salt *salt)
99b528cefcSMark Murray {
100b528cefcSMark Murray krb5_error_code ret;
101b528cefcSMark Murray PA_DATA *pa2;
102b528cefcSMark Murray krb5_salt salt2;
1034137ff4cSJacques Vidrine krb5_enctype *ep;
104*ae771770SStanislav Sedov size_t i;
105b528cefcSMark Murray
106b528cefcSMark Murray if(salt == NULL) {
107b528cefcSMark Murray /* default to standard salt */
108b528cefcSMark Murray ret = krb5_get_pw_salt (context, client, &salt2);
109*ae771770SStanislav Sedov if (ret)
110*ae771770SStanislav Sedov return ret;
111b528cefcSMark Murray salt = &salt2;
112b528cefcSMark Murray }
113b528cefcSMark Murray if (!enctypes) {
1144137ff4cSJacques Vidrine enctypes = context->etypes;
115b528cefcSMark Murray netypes = 0;
116b528cefcSMark Murray for (ep = enctypes; *ep != ETYPE_NULL; ep++)
117b528cefcSMark Murray netypes++;
118b528cefcSMark Murray }
119b528cefcSMark Murray pa2 = realloc (md->val, (md->len + netypes) * sizeof(*md->val));
120adb0ddaeSAssar Westerlund if (pa2 == NULL) {
121*ae771770SStanislav Sedov krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
122b528cefcSMark Murray return ENOMEM;
123adb0ddaeSAssar Westerlund }
124b528cefcSMark Murray md->val = pa2;
125b528cefcSMark Murray
126b528cefcSMark Murray for (i = 0; i < netypes; ++i) {
127b528cefcSMark Murray krb5_keyblock *key;
128b528cefcSMark Murray
129b528cefcSMark Murray ret = (*key_proc)(context, enctypes[i], *salt, keyseed, &key);
130b528cefcSMark Murray if (ret)
131b528cefcSMark Murray continue;
132b528cefcSMark Murray ret = make_pa_enc_timestamp (context, &md->val[md->len],
133b528cefcSMark Murray enctypes[i], key);
134b528cefcSMark Murray krb5_free_keyblock (context, key);
135b528cefcSMark Murray if (ret)
136b528cefcSMark Murray return ret;
137b528cefcSMark Murray ++md->len;
138b528cefcSMark Murray }
139b528cefcSMark Murray if(salt == &salt2)
140b528cefcSMark Murray krb5_free_salt(context, salt2);
141b528cefcSMark Murray return 0;
142b528cefcSMark Murray }
143b528cefcSMark Murray
144b528cefcSMark Murray static krb5_error_code
init_as_req(krb5_context context,KDCOptions opts,krb5_creds * creds,const krb5_addresses * addrs,const krb5_enctype * etypes,const krb5_preauthtype * ptypes,const krb5_preauthdata * preauth,krb5_key_proc key_proc,krb5_const_pointer keyseed,unsigned nonce,AS_REQ * a)145b528cefcSMark Murray init_as_req (krb5_context context,
146c19800e8SDoug Rabson KDCOptions opts,
147b528cefcSMark Murray krb5_creds *creds,
148b528cefcSMark Murray const krb5_addresses *addrs,
149b528cefcSMark Murray const krb5_enctype *etypes,
150b528cefcSMark Murray const krb5_preauthtype *ptypes,
151b528cefcSMark Murray const krb5_preauthdata *preauth,
152b528cefcSMark Murray krb5_key_proc key_proc,
153b528cefcSMark Murray krb5_const_pointer keyseed,
154b528cefcSMark Murray unsigned nonce,
155b528cefcSMark Murray AS_REQ *a)
156b528cefcSMark Murray {
157b528cefcSMark Murray krb5_error_code ret;
158b528cefcSMark Murray krb5_salt salt;
159b528cefcSMark Murray
160b528cefcSMark Murray memset(a, 0, sizeof(*a));
161b528cefcSMark Murray
162b528cefcSMark Murray a->pvno = 5;
163b528cefcSMark Murray a->msg_type = krb_as_req;
164c19800e8SDoug Rabson a->req_body.kdc_options = opts;
165b528cefcSMark Murray a->req_body.cname = malloc(sizeof(*a->req_body.cname));
166b528cefcSMark Murray if (a->req_body.cname == NULL) {
167b528cefcSMark Murray ret = ENOMEM;
168*ae771770SStanislav Sedov krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
169b528cefcSMark Murray goto fail;
170b528cefcSMark Murray }
171b528cefcSMark Murray a->req_body.sname = malloc(sizeof(*a->req_body.sname));
172b528cefcSMark Murray if (a->req_body.sname == NULL) {
173b528cefcSMark Murray ret = ENOMEM;
174*ae771770SStanislav Sedov krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
175b528cefcSMark Murray goto fail;
176b528cefcSMark Murray }
177c19800e8SDoug Rabson ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
178b528cefcSMark Murray if (ret)
179b528cefcSMark Murray goto fail;
180c19800e8SDoug Rabson ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
181b528cefcSMark Murray if (ret)
182b528cefcSMark Murray goto fail;
183b528cefcSMark Murray ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
184b528cefcSMark Murray if (ret)
185b528cefcSMark Murray goto fail;
186b528cefcSMark Murray
187b528cefcSMark Murray if(creds->times.starttime) {
188b528cefcSMark Murray a->req_body.from = malloc(sizeof(*a->req_body.from));
189b528cefcSMark Murray if (a->req_body.from == NULL) {
190b528cefcSMark Murray ret = ENOMEM;
191*ae771770SStanislav Sedov krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
192b528cefcSMark Murray goto fail;
193b528cefcSMark Murray }
194b528cefcSMark Murray *a->req_body.from = creds->times.starttime;
195b528cefcSMark Murray }
196b528cefcSMark Murray if(creds->times.endtime){
197b528cefcSMark Murray ALLOC(a->req_body.till, 1);
198b528cefcSMark Murray *a->req_body.till = creds->times.endtime;
199b528cefcSMark Murray }
200b528cefcSMark Murray if(creds->times.renew_till){
201b528cefcSMark Murray a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
202b528cefcSMark Murray if (a->req_body.rtime == NULL) {
203b528cefcSMark Murray ret = ENOMEM;
204*ae771770SStanislav Sedov krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
205b528cefcSMark Murray goto fail;
206b528cefcSMark Murray }
207b528cefcSMark Murray *a->req_body.rtime = creds->times.renew_till;
208b528cefcSMark Murray }
209b528cefcSMark Murray a->req_body.nonce = nonce;
210*ae771770SStanislav Sedov ret = _krb5_init_etype(context,
211*ae771770SStanislav Sedov KRB5_PDU_AS_REQUEST,
212b528cefcSMark Murray &a->req_body.etype.len,
213b528cefcSMark Murray &a->req_body.etype.val,
214b528cefcSMark Murray etypes);
215b528cefcSMark Murray if (ret)
216b528cefcSMark Murray goto fail;
217b528cefcSMark Murray
218b528cefcSMark Murray /*
219b528cefcSMark Murray * This means no addresses
220b528cefcSMark Murray */
221b528cefcSMark Murray
222b528cefcSMark Murray if (addrs && addrs->len == 0) {
223b528cefcSMark Murray a->req_body.addresses = NULL;
224b528cefcSMark Murray } else {
225b528cefcSMark Murray a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
226b528cefcSMark Murray if (a->req_body.addresses == NULL) {
227b528cefcSMark Murray ret = ENOMEM;
228*ae771770SStanislav Sedov krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
229b528cefcSMark Murray goto fail;
230b528cefcSMark Murray }
231b528cefcSMark Murray
232b528cefcSMark Murray if (addrs)
233b528cefcSMark Murray ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
2344137ff4cSJacques Vidrine else {
235b528cefcSMark Murray ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
2364137ff4cSJacques Vidrine if(ret == 0 && a->req_body.addresses->len == 0) {
2374137ff4cSJacques Vidrine free(a->req_body.addresses);
2384137ff4cSJacques Vidrine a->req_body.addresses = NULL;
2394137ff4cSJacques Vidrine }
2404137ff4cSJacques Vidrine }
241b528cefcSMark Murray if (ret)
242b528cefcSMark Murray return ret;
243b528cefcSMark Murray }
244b528cefcSMark Murray
245b528cefcSMark Murray a->req_body.enc_authorization_data = NULL;
246b528cefcSMark Murray a->req_body.additional_tickets = NULL;
247b528cefcSMark Murray
248b528cefcSMark Murray if(preauth != NULL) {
249*ae771770SStanislav Sedov size_t i;
250b528cefcSMark Murray ALLOC(a->padata, 1);
251b528cefcSMark Murray if(a->padata == NULL) {
252b528cefcSMark Murray ret = ENOMEM;
253*ae771770SStanislav Sedov krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
254b528cefcSMark Murray goto fail;
255b528cefcSMark Murray }
256c19800e8SDoug Rabson a->padata->val = NULL;
257c19800e8SDoug Rabson a->padata->len = 0;
258b528cefcSMark Murray for(i = 0; i < preauth->len; i++) {
259b528cefcSMark Murray if(preauth->val[i].type == KRB5_PADATA_ENC_TIMESTAMP){
260*ae771770SStanislav Sedov size_t j;
261c19800e8SDoug Rabson
262b528cefcSMark Murray for(j = 0; j < preauth->val[i].info.len; j++) {
263b528cefcSMark Murray krb5_salt *sp = &salt;
264b528cefcSMark Murray if(preauth->val[i].info.val[j].salttype)
265b528cefcSMark Murray salt.salttype = *preauth->val[i].info.val[j].salttype;
266b528cefcSMark Murray else
267b528cefcSMark Murray salt.salttype = KRB5_PW_SALT;
268b528cefcSMark Murray if(preauth->val[i].info.val[j].salt)
269b528cefcSMark Murray salt.saltvalue = *preauth->val[i].info.val[j].salt;
270b528cefcSMark Murray else
271b528cefcSMark Murray if(salt.salttype == KRB5_PW_SALT)
272b528cefcSMark Murray sp = NULL;
273b528cefcSMark Murray else
274b528cefcSMark Murray krb5_data_zero(&salt.saltvalue);
275bbd80c28SJacques Vidrine ret = add_padata(context, a->padata, creds->client,
276b528cefcSMark Murray key_proc, keyseed,
277b528cefcSMark Murray &preauth->val[i].info.val[j].etype, 1,
278b528cefcSMark Murray sp);
279bbd80c28SJacques Vidrine if (ret == 0)
280bbd80c28SJacques Vidrine break;
281b528cefcSMark Murray }
282b528cefcSMark Murray }
283b528cefcSMark Murray }
284b528cefcSMark Murray } else
285b528cefcSMark Murray /* not sure this is the way to use `ptypes' */
286b528cefcSMark Murray if (ptypes == NULL || *ptypes == KRB5_PADATA_NONE)
287b528cefcSMark Murray a->padata = NULL;
288b528cefcSMark Murray else if (*ptypes == KRB5_PADATA_ENC_TIMESTAMP) {
289b528cefcSMark Murray ALLOC(a->padata, 1);
290b528cefcSMark Murray if (a->padata == NULL) {
291b528cefcSMark Murray ret = ENOMEM;
292*ae771770SStanislav Sedov krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
293b528cefcSMark Murray goto fail;
294b528cefcSMark Murray }
295b528cefcSMark Murray a->padata->len = 0;
296b528cefcSMark Murray a->padata->val = NULL;
297b528cefcSMark Murray
298b528cefcSMark Murray /* make a v5 salted pa-data */
299b528cefcSMark Murray add_padata(context, a->padata, creds->client,
300b528cefcSMark Murray key_proc, keyseed, a->req_body.etype.val,
301b528cefcSMark Murray a->req_body.etype.len, NULL);
302b528cefcSMark Murray
303b528cefcSMark Murray /* make a v4 salted pa-data */
304b528cefcSMark Murray salt.salttype = KRB5_PW_SALT;
305b528cefcSMark Murray krb5_data_zero(&salt.saltvalue);
306b528cefcSMark Murray add_padata(context, a->padata, creds->client,
307b528cefcSMark Murray key_proc, keyseed, a->req_body.etype.val,
308b528cefcSMark Murray a->req_body.etype.len, &salt);
309b528cefcSMark Murray } else {
310b528cefcSMark Murray ret = KRB5_PREAUTH_BAD_TYPE;
311*ae771770SStanislav Sedov krb5_set_error_message (context, ret,
312*ae771770SStanislav Sedov N_("pre-auth type %d not supported", ""),
313*ae771770SStanislav Sedov *ptypes);
314b528cefcSMark Murray goto fail;
315b528cefcSMark Murray }
316b528cefcSMark Murray return 0;
317b528cefcSMark Murray fail:
318b528cefcSMark Murray free_AS_REQ(a);
319b528cefcSMark Murray return ret;
320b528cefcSMark Murray }
321b528cefcSMark Murray
322b528cefcSMark Murray static int
set_ptypes(krb5_context context,KRB_ERROR * error,const krb5_preauthtype ** ptypes,krb5_preauthdata ** preauth)323b528cefcSMark Murray set_ptypes(krb5_context context,
324b528cefcSMark Murray KRB_ERROR *error,
325c19800e8SDoug Rabson const krb5_preauthtype **ptypes,
326b528cefcSMark Murray krb5_preauthdata **preauth)
327b528cefcSMark Murray {
328b528cefcSMark Murray static krb5_preauthdata preauth2;
329b528cefcSMark Murray static krb5_preauthtype ptypes2[] = { KRB5_PADATA_ENC_TIMESTAMP, KRB5_PADATA_NONE };
330b528cefcSMark Murray
331b528cefcSMark Murray if(error->e_data) {
332b528cefcSMark Murray METHOD_DATA md;
333*ae771770SStanislav Sedov size_t i;
334b528cefcSMark Murray decode_METHOD_DATA(error->e_data->data,
335b528cefcSMark Murray error->e_data->length,
336b528cefcSMark Murray &md,
337b528cefcSMark Murray NULL);
338b528cefcSMark Murray for(i = 0; i < md.len; i++){
339b528cefcSMark Murray switch(md.val[i].padata_type){
3405e9cd1aeSAssar Westerlund case KRB5_PADATA_ENC_TIMESTAMP:
341b528cefcSMark Murray *ptypes = ptypes2;
342b528cefcSMark Murray break;
3435e9cd1aeSAssar Westerlund case KRB5_PADATA_ETYPE_INFO:
344b528cefcSMark Murray *preauth = &preauth2;
345b528cefcSMark Murray ALLOC_SEQ(*preauth, 1);
346b528cefcSMark Murray (*preauth)->val[0].type = KRB5_PADATA_ENC_TIMESTAMP;
347*ae771770SStanislav Sedov decode_ETYPE_INFO(md.val[i].padata_value.data,
348b528cefcSMark Murray md.val[i].padata_value.length,
349b528cefcSMark Murray &(*preauth)->val[0].info,
350b528cefcSMark Murray NULL);
351b528cefcSMark Murray break;
3525e9cd1aeSAssar Westerlund default:
3535e9cd1aeSAssar Westerlund break;
354b528cefcSMark Murray }
355b528cefcSMark Murray }
356b528cefcSMark Murray free_METHOD_DATA(&md);
357b528cefcSMark Murray } else {
358b528cefcSMark Murray *ptypes = ptypes2;
359b528cefcSMark Murray }
360b528cefcSMark Murray return(1);
361b528cefcSMark Murray }
362b528cefcSMark Murray
363*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_in_cred(krb5_context context,krb5_flags options,const krb5_addresses * addrs,const krb5_enctype * etypes,const krb5_preauthtype * ptypes,const krb5_preauthdata * preauth,krb5_key_proc key_proc,krb5_const_pointer keyseed,krb5_decrypt_proc decrypt_proc,krb5_const_pointer decryptarg,krb5_creds * creds,krb5_kdc_rep * ret_as_reply)364b528cefcSMark Murray krb5_get_in_cred(krb5_context context,
365b528cefcSMark Murray krb5_flags options,
366b528cefcSMark Murray const krb5_addresses *addrs,
367b528cefcSMark Murray const krb5_enctype *etypes,
368b528cefcSMark Murray const krb5_preauthtype *ptypes,
369b528cefcSMark Murray const krb5_preauthdata *preauth,
370b528cefcSMark Murray krb5_key_proc key_proc,
371b528cefcSMark Murray krb5_const_pointer keyseed,
372b528cefcSMark Murray krb5_decrypt_proc decrypt_proc,
373b528cefcSMark Murray krb5_const_pointer decryptarg,
374b528cefcSMark Murray krb5_creds *creds,
375b528cefcSMark Murray krb5_kdc_rep *ret_as_reply)
376*ae771770SStanislav Sedov KRB5_DEPRECATED_FUNCTION("Use X instead")
377b528cefcSMark Murray {
378b528cefcSMark Murray krb5_error_code ret;
379b528cefcSMark Murray AS_REQ a;
380b528cefcSMark Murray krb5_kdc_rep rep;
381b528cefcSMark Murray krb5_data req, resp;
382*ae771770SStanislav Sedov size_t len = 0;
383b528cefcSMark Murray krb5_salt salt;
384b528cefcSMark Murray krb5_keyblock *key;
385b528cefcSMark Murray size_t size;
386c19800e8SDoug Rabson KDCOptions opts;
387b528cefcSMark Murray PA_DATA *pa;
388b528cefcSMark Murray krb5_enctype etype;
389b528cefcSMark Murray krb5_preauthdata *my_preauth = NULL;
390b528cefcSMark Murray unsigned nonce;
391b528cefcSMark Murray int done;
392b528cefcSMark Murray
393c19800e8SDoug Rabson opts = int2KDCOptions(options);
394b528cefcSMark Murray
395b528cefcSMark Murray krb5_generate_random_block (&nonce, sizeof(nonce));
396b528cefcSMark Murray nonce &= 0xffffffff;
397b528cefcSMark Murray
398b528cefcSMark Murray do {
399b528cefcSMark Murray done = 1;
400b528cefcSMark Murray ret = init_as_req (context,
401b528cefcSMark Murray opts,
402b528cefcSMark Murray creds,
403b528cefcSMark Murray addrs,
404b528cefcSMark Murray etypes,
405b528cefcSMark Murray ptypes,
406b528cefcSMark Murray preauth,
407b528cefcSMark Murray key_proc,
408b528cefcSMark Murray keyseed,
409b528cefcSMark Murray nonce,
410b528cefcSMark Murray &a);
411b528cefcSMark Murray if (my_preauth) {
412b528cefcSMark Murray free_ETYPE_INFO(&my_preauth->val[0].info);
413b528cefcSMark Murray free (my_preauth->val);
414c19800e8SDoug Rabson my_preauth = NULL;
415b528cefcSMark Murray }
416b528cefcSMark Murray if (ret)
417b528cefcSMark Murray return ret;
418b528cefcSMark Murray
4190cadf2f4SJacques Vidrine ASN1_MALLOC_ENCODE(AS_REQ, req.data, req.length, &a, &len, ret);
420b528cefcSMark Murray free_AS_REQ(&a);
421b528cefcSMark Murray if (ret)
422b528cefcSMark Murray return ret;
4230cadf2f4SJacques Vidrine if(len != req.length)
4240cadf2f4SJacques Vidrine krb5_abortx(context, "internal error in ASN.1 encoder");
425b528cefcSMark Murray
426b528cefcSMark Murray ret = krb5_sendto_kdc (context, &req, &creds->client->realm, &resp);
4270cadf2f4SJacques Vidrine krb5_data_free(&req);
428b528cefcSMark Murray if (ret)
429b528cefcSMark Murray return ret;
430b528cefcSMark Murray
431b528cefcSMark Murray memset (&rep, 0, sizeof(rep));
432b528cefcSMark Murray ret = decode_AS_REP(resp.data, resp.length, &rep.kdc_rep, &size);
433b528cefcSMark Murray if(ret) {
434b528cefcSMark Murray /* let's try to parse it as a KRB-ERROR */
435b528cefcSMark Murray KRB_ERROR error;
436b528cefcSMark Murray int ret2;
437b528cefcSMark Murray
438b528cefcSMark Murray ret2 = krb5_rd_error(context, &resp, &error);
439b528cefcSMark Murray if(ret2 && resp.data && ((char*)resp.data)[0] == 4)
440b528cefcSMark Murray ret = KRB5KRB_AP_ERR_V4_REPLY;
441b528cefcSMark Murray krb5_data_free(&resp);
442b528cefcSMark Murray if (ret2 == 0) {
443adb0ddaeSAssar Westerlund ret = krb5_error_from_rd_error(context, &error, creds);
444b528cefcSMark Murray /* if no preauth was set and KDC requires it, give it
445b528cefcSMark Murray one more try */
446b528cefcSMark Murray if (!ptypes && !preauth
447b528cefcSMark Murray && ret == KRB5KDC_ERR_PREAUTH_REQUIRED
448b528cefcSMark Murray #if 0
449b528cefcSMark Murray || ret == KRB5KDC_ERR_BADOPTION
450b528cefcSMark Murray #endif
451b528cefcSMark Murray && set_ptypes(context, &error, &ptypes, &my_preauth)) {
452b528cefcSMark Murray done = 0;
453b528cefcSMark Murray preauth = my_preauth;
454adb0ddaeSAssar Westerlund krb5_free_error_contents(context, &error);
455*ae771770SStanislav Sedov krb5_clear_error_message(context);
456b528cefcSMark Murray continue;
457b528cefcSMark Murray }
458b528cefcSMark Murray if(ret_as_reply)
459b528cefcSMark Murray ret_as_reply->error = error;
460b528cefcSMark Murray else
461b528cefcSMark Murray free_KRB_ERROR (&error);
462b528cefcSMark Murray return ret;
463b528cefcSMark Murray }
464b528cefcSMark Murray return ret;
465b528cefcSMark Murray }
466b528cefcSMark Murray krb5_data_free(&resp);
467b528cefcSMark Murray } while(!done);
468b528cefcSMark Murray
469b528cefcSMark Murray pa = NULL;
470b528cefcSMark Murray etype = rep.kdc_rep.enc_part.etype;
471b528cefcSMark Murray if(rep.kdc_rep.padata){
472c19800e8SDoug Rabson int i = 0;
473b528cefcSMark Murray pa = krb5_find_padata(rep.kdc_rep.padata->val, rep.kdc_rep.padata->len,
474c19800e8SDoug Rabson KRB5_PADATA_PW_SALT, &i);
475b528cefcSMark Murray if(pa == NULL) {
476c19800e8SDoug Rabson i = 0;
477b528cefcSMark Murray pa = krb5_find_padata(rep.kdc_rep.padata->val,
478b528cefcSMark Murray rep.kdc_rep.padata->len,
479c19800e8SDoug Rabson KRB5_PADATA_AFS3_SALT, &i);
480b528cefcSMark Murray }
481b528cefcSMark Murray }
482b528cefcSMark Murray if(pa) {
483*ae771770SStanislav Sedov salt.salttype = (krb5_salttype)pa->padata_type;
484b528cefcSMark Murray salt.saltvalue = pa->padata_value;
485b528cefcSMark Murray
486b528cefcSMark Murray ret = (*key_proc)(context, etype, salt, keyseed, &key);
487b528cefcSMark Murray } else {
488b528cefcSMark Murray /* make a v5 salted pa-data */
489b528cefcSMark Murray ret = krb5_get_pw_salt (context, creds->client, &salt);
490b528cefcSMark Murray
491b528cefcSMark Murray if (ret)
492b528cefcSMark Murray goto out;
493b528cefcSMark Murray ret = (*key_proc)(context, etype, salt, keyseed, &key);
494b528cefcSMark Murray krb5_free_salt(context, salt);
495b528cefcSMark Murray }
496b528cefcSMark Murray if (ret)
497b528cefcSMark Murray goto out;
498b528cefcSMark Murray
499c19800e8SDoug Rabson {
500*ae771770SStanislav Sedov unsigned flags = EXTRACT_TICKET_TIMESYNC;
501c19800e8SDoug Rabson if (opts.request_anonymous)
502c19800e8SDoug Rabson flags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
503c19800e8SDoug Rabson
504b528cefcSMark Murray ret = _krb5_extract_ticket(context,
505b528cefcSMark Murray &rep,
506b528cefcSMark Murray creds,
507b528cefcSMark Murray key,
508b528cefcSMark Murray keyseed,
509b528cefcSMark Murray KRB5_KU_AS_REP_ENC_PART,
510b528cefcSMark Murray NULL,
511b528cefcSMark Murray nonce,
512c19800e8SDoug Rabson flags,
513b528cefcSMark Murray decrypt_proc,
514b528cefcSMark Murray decryptarg);
515c19800e8SDoug Rabson }
516b528cefcSMark Murray memset (key->keyvalue.data, 0, key->keyvalue.length);
517b528cefcSMark Murray krb5_free_keyblock_contents (context, key);
518b528cefcSMark Murray free (key);
519b528cefcSMark Murray
520b528cefcSMark Murray out:
521b528cefcSMark Murray if (ret == 0 && ret_as_reply)
522b528cefcSMark Murray *ret_as_reply = rep;
523b528cefcSMark Murray else
524b528cefcSMark Murray krb5_free_kdc_rep (context, &rep);
525b528cefcSMark Murray return ret;
526b528cefcSMark Murray }
527b528cefcSMark Murray
528*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_in_tkt(krb5_context context,krb5_flags options,const krb5_addresses * addrs,const krb5_enctype * etypes,const krb5_preauthtype * ptypes,krb5_key_proc key_proc,krb5_const_pointer keyseed,krb5_decrypt_proc decrypt_proc,krb5_const_pointer decryptarg,krb5_creds * creds,krb5_ccache ccache,krb5_kdc_rep * ret_as_reply)529b528cefcSMark Murray krb5_get_in_tkt(krb5_context context,
530b528cefcSMark Murray krb5_flags options,
531b528cefcSMark Murray const krb5_addresses *addrs,
532b528cefcSMark Murray const krb5_enctype *etypes,
533b528cefcSMark Murray const krb5_preauthtype *ptypes,
534b528cefcSMark Murray krb5_key_proc key_proc,
535b528cefcSMark Murray krb5_const_pointer keyseed,
536b528cefcSMark Murray krb5_decrypt_proc decrypt_proc,
537b528cefcSMark Murray krb5_const_pointer decryptarg,
538b528cefcSMark Murray krb5_creds *creds,
539b528cefcSMark Murray krb5_ccache ccache,
540b528cefcSMark Murray krb5_kdc_rep *ret_as_reply)
541*ae771770SStanislav Sedov KRB5_DEPRECATED_FUNCTION("Use X instead")
542b528cefcSMark Murray {
543b528cefcSMark Murray krb5_error_code ret;
544b528cefcSMark Murray
545b528cefcSMark Murray ret = krb5_get_in_cred (context,
546c19800e8SDoug Rabson options,
547b528cefcSMark Murray addrs,
548b528cefcSMark Murray etypes,
549b528cefcSMark Murray ptypes,
550b528cefcSMark Murray NULL,
551b528cefcSMark Murray key_proc,
552b528cefcSMark Murray keyseed,
553b528cefcSMark Murray decrypt_proc,
554b528cefcSMark Murray decryptarg,
555b528cefcSMark Murray creds,
556b528cefcSMark Murray ret_as_reply);
557b528cefcSMark Murray if(ret)
558b528cefcSMark Murray return ret;
5591c43270aSJacques Vidrine if (ccache)
560b528cefcSMark Murray ret = krb5_cc_store_cred (context, ccache, creds);
561b528cefcSMark Murray return ret;
562b528cefcSMark Murray }
563*ae771770SStanislav Sedov
564*ae771770SStanislav Sedov #endif /* HEIMDAL_SMALLER */
565