2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <krb5-v4compat.h>
39 * fetch the server from `t', returning the name in malloced memory in
40 * `spn' and the entry itself in `server'
43 static krb5_error_code
44 fetch_server (krb5_context context
,
45 krb5_kdc_configuration
*config
,
48 hdb_entry_ex
**server
,
52 krb5_principal sprinc
;
54 ret
= _krb5_principalname2krb5_principal(context
, &sprinc
,
57 kdc_log(context
, config
, 0, "_krb5_principalname2krb5_principal: %s",
58 krb5_get_err_text(context
, ret
));
61 ret
= krb5_unparse_name(context
, sprinc
, spn
);
63 krb5_free_principal(context
, sprinc
);
64 kdc_log(context
, config
, 0, "krb5_unparse_name: %s",
65 krb5_get_err_text(context
, ret
));
68 ret
= _kdc_db_fetch(context
, config
, sprinc
, HDB_F_GET_SERVER
,
70 krb5_free_principal(context
, sprinc
);
72 kdc_log(context
, config
, 0,
73 "Request to convert ticket from %s for unknown principal %s: %s",
74 from
, *spn
, krb5_get_err_text(context
, ret
));
75 if (ret
== HDB_ERR_NOENTRY
)
76 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
82 static krb5_error_code
83 log_524 (krb5_context context
,
84 krb5_kdc_configuration
*config
,
85 const EncTicketPart
*et
,
89 krb5_principal client
;
93 ret
= _krb5_principalname2krb5_principal(context
, &client
,
94 et
->cname
, et
->crealm
);
96 kdc_log(context
, config
, 0, "_krb5_principalname2krb5_principal: %s",
97 krb5_get_err_text (context
, ret
));
100 ret
= krb5_unparse_name(context
, client
, &cpn
);
102 krb5_free_principal(context
, client
);
103 kdc_log(context
, config
, 0, "krb5_unparse_name: %s",
104 krb5_get_err_text (context
, ret
));
107 kdc_log(context
, config
, 1, "524-REQ %s from %s for %s", cpn
, from
, spn
);
109 krb5_free_principal(context
, client
);
113 static krb5_error_code
114 verify_flags (krb5_context context
,
115 krb5_kdc_configuration
*config
,
116 const EncTicketPart
*et
,
119 if(et
->endtime
< kdc_time
){
120 kdc_log(context
, config
, 0, "Ticket expired (%s)", spn
);
121 return KRB5KRB_AP_ERR_TKT_EXPIRED
;
123 if(et
->flags
.invalid
){
124 kdc_log(context
, config
, 0, "Ticket not valid (%s)", spn
);
125 return KRB5KRB_AP_ERR_TKT_NYV
;
131 * set the `et->caddr' to the most appropriate address to use, where
132 * `addr' is the address the request was received from.
135 static krb5_error_code
136 set_address (krb5_context context
,
137 krb5_kdc_configuration
*config
,
139 struct sockaddr
*addr
,
143 krb5_address
*v4_addr
;
145 v4_addr
= malloc (sizeof(*v4_addr
));
149 ret
= krb5_sockaddr2address(context
, addr
, v4_addr
);
152 kdc_log(context
, config
, 0, "Failed to convert address (%s)", from
);
156 if (et
->caddr
&& !krb5_address_search (context
, v4_addr
, et
->caddr
)) {
157 kdc_log(context
, config
, 0, "Incorrect network address (%s)", from
);
158 krb5_free_address(context
, v4_addr
);
160 return KRB5KRB_AP_ERR_BADADDR
;
162 if(v4_addr
->addr_type
== KRB5_ADDRESS_INET
) {
163 /* we need to collapse the addresses in the ticket to a
164 single address; best guess is to use the address the
165 connection came from */
167 if (et
->caddr
!= NULL
) {
168 free_HostAddresses(et
->caddr
);
170 et
->caddr
= malloc (sizeof (*et
->caddr
));
171 if (et
->caddr
== NULL
) {
172 krb5_free_address(context
, v4_addr
);
177 et
->caddr
->val
= v4_addr
;
180 krb5_free_address(context
, v4_addr
);
187 static krb5_error_code
188 encrypt_v4_ticket(krb5_context context
,
189 krb5_kdc_configuration
*config
,
193 EncryptedData
*reply
)
197 ret
= krb5_crypto_init(context
, skey
, ETYPE_DES_PCBC_NONE
, &crypto
);
200 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s",
201 krb5_get_err_text(context
, ret
));
205 ret
= krb5_encrypt_EncryptedData(context
,
212 krb5_crypto_destroy(context
, crypto
);
214 kdc_log(context
, config
, 0, "Failed to encrypt data: %s",
215 krb5_get_err_text(context
, ret
));
221 static krb5_error_code
222 encode_524_response(krb5_context context
,
223 krb5_kdc_configuration
*config
,
224 const char *spn
, const EncTicketPart et
,
225 const Ticket
*t
, hdb_entry_ex
*server
,
226 EncryptedData
*ticket
, int *kvno
)
232 use_2b
= krb5_config_get_bool(context
, NULL
, "kdc", "use_2b", spn
, NULL
);
234 ASN1_MALLOC_ENCODE(EncryptedData
,
235 ticket
->cipher
.data
, ticket
->cipher
.length
,
236 &t
->enc_part
, &len
, ret
);
239 kdc_log(context
, config
, 0,
240 "Failed to encode v4 (2b) ticket (%s)", spn
);
246 *kvno
= 213; /* 2b's use this magic kvno */
248 unsigned char buf
[MAX_KTXT_LEN
+ 4 * 4];
251 if (!config
->enable_v4_cross_realm
&& strcmp (et
.crealm
, t
->realm
) != 0) {
252 kdc_log(context
, config
, 0, "524 cross-realm %s -> %s disabled", et
.crealm
,
254 return KRB5KDC_ERR_POLICY
;
257 ret
= _kdc_encode_v4_ticket(context
, config
,
258 buf
+ sizeof(buf
) - 1, sizeof(buf
),
259 &et
, &t
->sname
, &len
);
261 kdc_log(context
, config
, 0,
262 "Failed to encode v4 ticket (%s)", spn
);
265 ret
= _kdc_get_des_key(context
, server
, TRUE
, FALSE
, &skey
);
267 kdc_log(context
, config
, 0,
268 "no suitable DES key for server (%s)", spn
);
271 ret
= encrypt_v4_ticket(context
, config
, buf
+ sizeof(buf
) - len
, len
,
274 kdc_log(context
, config
, 0,
275 "Failed to encrypt v4 ticket (%s)", spn
);
278 *kvno
= server
->entry
.kvno
;
285 * process a 5->4 request, based on `t', and received `from, addr',
286 * returning the reply in `reply'
290 _kdc_do_524(krb5_context context
,
291 krb5_kdc_configuration
*config
,
292 const Ticket
*t
, krb5_data
*reply
,
293 const char *from
, struct sockaddr
*addr
)
295 krb5_error_code ret
= 0;
297 hdb_entry_ex
*server
= NULL
;
301 EncryptedData ticket
;
304 unsigned char buf
[MAX_KTXT_LEN
+ 4 * 4];
307 if(!config
->enable_524
) {
308 ret
= KRB5KDC_ERR_POLICY
;
309 kdc_log(context
, config
, 0,
310 "Rejected ticket conversion request from %s", from
);
314 ret
= fetch_server (context
, config
, t
, &spn
, &server
, from
);
319 ret
= hdb_enctype2key(context
, &server
->entry
, t
->enc_part
.etype
, &skey
);
321 kdc_log(context
, config
, 0,
322 "No suitable key found for server (%s) from %s", spn
, from
);
325 ret
= krb5_crypto_init(context
, &skey
->key
, 0, &crypto
);
327 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s",
328 krb5_get_err_text(context
, ret
));
331 ret
= krb5_decrypt_EncryptedData (context
,
336 krb5_crypto_destroy(context
, crypto
);
338 kdc_log(context
, config
, 0,
339 "Failed to decrypt ticket from %s for %s", from
, spn
);
342 ret
= decode_EncTicketPart(et_data
.data
, et_data
.length
, &et
, NULL
);
343 krb5_data_free(&et_data
);
345 kdc_log(context
, config
, 0,
346 "Failed to decode ticket from %s for %s", from
, spn
);
350 ret
= log_524 (context
, config
, &et
, from
, spn
);
352 free_EncTicketPart(&et
);
356 ret
= verify_flags (context
, config
, &et
, spn
);
358 free_EncTicketPart(&et
);
362 ret
= set_address (context
, config
, &et
, addr
, from
);
364 free_EncTicketPart(&et
);
368 ret
= encode_524_response(context
, config
, spn
, et
, t
,
369 server
, &ticket
, &kvno
);
370 free_EncTicketPart(&et
);
374 memset(buf
, 0, sizeof(buf
));
375 sp
= krb5_storage_from_mem(buf
, sizeof(buf
));
377 krb5_store_int32(sp
, ret
);
379 krb5_store_int32(sp
, kvno
);
380 krb5_store_data(sp
, ticket
.cipher
);
381 /* Aargh! This is coded as a KTEXT_ST. */
382 krb5_storage_seek(sp
, MAX_KTXT_LEN
- ticket
.cipher
.length
, SEEK_CUR
);
383 krb5_store_int32(sp
, 0); /* mbz */
384 free_EncryptedData(&ticket
);
386 ret
= krb5_storage_to_data(sp
, reply
);
387 reply
->length
= krb5_storage_seek(sp
, 0, SEEK_CUR
);
388 krb5_storage_free(sp
);
390 krb5_data_zero(reply
);
394 _kdc_free_ent (context
, server
);