2 Unix SMB/CIFS implementation.
4 Create and parse the krb5 PAC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005,2008
7 Copyright (C) Andrew Tridgell 2001
8 Copyright (C) Luke Howard 2002-2003
9 Copyright (C) Stefan Metzmacher 2004-2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "system/kerberos.h"
28 #include "auth/auth.h"
29 #include "auth/kerberos/kerberos.h"
30 #include "librpc/gen_ndr/ndr_krb5pac.h"
32 #include "auth/auth_sam_reply.h"
34 _PUBLIC_ NTSTATUS
kerberos_pac_logon_info(TALLOC_CTX
*mem_ctx
,
37 const krb5_keyblock
*krbtgt_keyblock
,
38 const krb5_keyblock
*service_keyblock
,
39 krb5_const_principal client_principal
,
41 struct PAC_LOGON_INFO
**logon_info
)
44 struct PAC_DATA
*pac_data
;
46 nt_status
= kerberos_decode_pac(mem_ctx
,
54 if (!NT_STATUS_IS_OK(nt_status
)) {
59 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
60 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_LOGON_INFO
) {
63 *logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
66 return NT_STATUS_INVALID_PARAMETER
;
71 static krb5_error_code
make_pac_checksum(TALLOC_CTX
*mem_ctx
,
73 struct PAC_SIGNATURE_DATA
*sig
,
75 const krb5_keyblock
*keyblock
)
82 ret
= krb5_crypto_init(context
,
87 DEBUG(0,("krb5_crypto_init() failed: %s\n",
88 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
91 ret
= krb5_create_checksum(context
,
99 DEBUG(2, ("PAC Verification failed: %s\n",
100 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
103 krb5_crypto_destroy(context
, crypto
);
109 sig
->type
= cksum
.cksumtype
;
110 sig
->signature
= data_blob_talloc(mem_ctx
, cksum
.checksum
.data
, cksum
.checksum
.length
);
111 free_Checksum(&cksum
);
116 krb5_error_code
kerberos_encode_pac(TALLOC_CTX
*mem_ctx
,
117 struct PAC_DATA
*pac_data
,
118 krb5_context context
,
119 const krb5_keyblock
*krbtgt_keyblock
,
120 const krb5_keyblock
*service_keyblock
,
125 enum ndr_err_code ndr_err
;
126 DATA_BLOB zero_blob
= data_blob(NULL
, 0);
127 DATA_BLOB tmp_blob
= data_blob(NULL
, 0);
128 struct PAC_SIGNATURE_DATA
*kdc_checksum
= NULL
;
129 struct PAC_SIGNATURE_DATA
*srv_checksum
= NULL
;
132 /* First, just get the keytypes filled in (and lengths right, eventually) */
133 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
134 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_KDC_CHECKSUM
) {
137 kdc_checksum
= &pac_data
->buffers
[i
].info
->kdc_cksum
,
138 ret
= make_pac_checksum(mem_ctx
, &zero_blob
,
140 context
, krbtgt_keyblock
);
142 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
143 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
144 talloc_free(pac_data
);
149 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
150 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_SRV_CHECKSUM
) {
153 srv_checksum
= &pac_data
->buffers
[i
].info
->srv_cksum
;
154 ret
= make_pac_checksum(mem_ctx
, &zero_blob
,
156 context
, service_keyblock
);
158 DEBUG(2, ("making service PAC checksum failed: %s\n",
159 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
160 talloc_free(pac_data
);
166 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!"));
170 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!"));
174 /* But wipe out the actual signatures */
175 memset(kdc_checksum
->signature
.data
, '\0', kdc_checksum
->signature
.length
);
176 memset(srv_checksum
->signature
.data
, '\0', srv_checksum
->signature
.length
);
178 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
180 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
181 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
182 nt_status
= ndr_map_error2ntstatus(ndr_err
);
183 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status
)));
184 talloc_free(pac_data
);
188 /* Then sign the result of the previous push, where the sig was zero'ed out */
189 ret
= make_pac_checksum(mem_ctx
, &tmp_blob
, srv_checksum
,
190 context
, service_keyblock
);
192 /* Then sign Server checksum */
193 ret
= make_pac_checksum(mem_ctx
, &srv_checksum
->signature
, kdc_checksum
, context
, krbtgt_keyblock
);
195 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
196 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
197 talloc_free(pac_data
);
201 /* And push it out again, this time to the world. This relies on determanistic pointer values */
202 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
204 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
205 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
206 nt_status
= ndr_map_error2ntstatus(ndr_err
);
207 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status
)));
208 talloc_free(pac_data
);
218 krb5_error_code
kerberos_create_pac(TALLOC_CTX
*mem_ctx
,
219 struct auth_user_info_dc
*user_info_dc
,
220 krb5_context context
,
221 const krb5_keyblock
*krbtgt_keyblock
,
222 const krb5_keyblock
*service_keyblock
,
223 krb5_principal client_principal
,
229 struct PAC_DATA
*pac_data
= talloc(mem_ctx
, struct PAC_DATA
);
230 struct netr_SamInfo3
*sam3
;
231 union PAC_INFO
*u_LOGON_INFO
;
232 struct PAC_LOGON_INFO
*LOGON_INFO
;
233 union PAC_INFO
*u_LOGON_NAME
;
234 struct PAC_LOGON_NAME
*LOGON_NAME
;
235 union PAC_INFO
*u_KDC_CHECKSUM
;
236 union PAC_INFO
*u_SRV_CHECKSUM
;
241 PAC_BUF_LOGON_INFO
= 0,
242 PAC_BUF_LOGON_NAME
= 1,
243 PAC_BUF_SRV_CHECKSUM
= 2,
244 PAC_BUF_KDC_CHECKSUM
= 3,
245 PAC_BUF_NUM_BUFFERS
= 4
252 pac_data
->num_buffers
= PAC_BUF_NUM_BUFFERS
;
253 pac_data
->version
= 0;
255 pac_data
->buffers
= talloc_array(pac_data
,
257 pac_data
->num_buffers
);
258 if (!pac_data
->buffers
) {
259 talloc_free(pac_data
);
264 u_LOGON_INFO
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
266 talloc_free(pac_data
);
269 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].type
= PAC_TYPE_LOGON_INFO
;
270 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].info
= u_LOGON_INFO
;
273 u_LOGON_NAME
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
275 talloc_free(pac_data
);
278 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].type
= PAC_TYPE_LOGON_NAME
;
279 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].info
= u_LOGON_NAME
;
280 LOGON_NAME
= &u_LOGON_NAME
->logon_name
;
283 u_SRV_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
284 if (!u_SRV_CHECKSUM
) {
285 talloc_free(pac_data
);
288 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].type
= PAC_TYPE_SRV_CHECKSUM
;
289 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].info
= u_SRV_CHECKSUM
;
292 u_KDC_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
293 if (!u_KDC_CHECKSUM
) {
294 talloc_free(pac_data
);
297 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].type
= PAC_TYPE_KDC_CHECKSUM
;
298 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].info
= u_KDC_CHECKSUM
;
300 /* now the real work begins... */
302 LOGON_INFO
= talloc_zero(u_LOGON_INFO
, struct PAC_LOGON_INFO
);
304 talloc_free(pac_data
);
307 nt_status
= auth_convert_user_info_dc_saminfo3(LOGON_INFO
, user_info_dc
, &sam3
);
308 if (!NT_STATUS_IS_OK(nt_status
)) {
309 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status
)));
310 talloc_free(pac_data
);
314 u_LOGON_INFO
->logon_info
.info
= LOGON_INFO
;
315 LOGON_INFO
->info3
= *sam3
;
317 ret
= krb5_unparse_name_flags(context
, client_principal
,
318 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
322 LOGON_NAME
->account_name
= talloc_strdup(LOGON_NAME
, name
);
325 this logon_time field is absolutely critical. This is what
326 caused all our PAC troubles :-)
328 unix_to_nt_time(&LOGON_NAME
->logon_time
, tgs_authtime
);
330 ret
= kerberos_encode_pac(mem_ctx
,
336 talloc_free(pac_data
);
340 krb5_error_code
kerberos_pac_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
342 krb5_context context
,
343 struct auth_user_info_dc
**user_info_dc
,
344 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
345 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
)
348 enum ndr_err_code ndr_err
;
351 DATA_BLOB pac_logon_info_in
, pac_srv_checksum_in
, pac_kdc_checksum_in
;
352 krb5_data k5pac_logon_info_in
, k5pac_srv_checksum_in
, k5pac_kdc_checksum_in
;
355 struct auth_user_info_dc
*user_info_dc_out
;
357 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
363 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_LOGON_INFO
, &k5pac_logon_info_in
);
365 talloc_free(tmp_ctx
);
369 pac_logon_info_in
= data_blob_const(k5pac_logon_info_in
.data
, k5pac_logon_info_in
.length
);
371 ndr_err
= ndr_pull_union_blob(&pac_logon_info_in
, tmp_ctx
, &info
,
373 (ndr_pull_flags_fn_t
)ndr_pull_PAC_INFO
);
374 krb5_data_free(&k5pac_logon_info_in
);
375 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
) || !info
.logon_info
.info
) {
376 nt_status
= ndr_map_error2ntstatus(ndr_err
);
377 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status
)));
378 talloc_free(tmp_ctx
);
382 /* Pull this right into the normal auth sysstem structures */
383 nt_status
= make_user_info_dc_pac(mem_ctx
,
384 info
.logon_info
.info
,
386 if (!NT_STATUS_IS_OK(nt_status
)) {
387 talloc_free(tmp_ctx
);
392 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_SRV_CHECKSUM
, &k5pac_srv_checksum_in
);
394 talloc_free(tmp_ctx
);
398 pac_srv_checksum_in
= data_blob_const(k5pac_srv_checksum_in
.data
, k5pac_srv_checksum_in
.length
);
400 ndr_err
= ndr_pull_struct_blob(&pac_srv_checksum_in
, pac_srv_sig
,
402 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
403 krb5_data_free(&k5pac_srv_checksum_in
);
404 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
405 nt_status
= ndr_map_error2ntstatus(ndr_err
);
406 DEBUG(0,("can't parse the KDC signature: %s\n",
407 nt_errstr(nt_status
)));
413 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_KDC_CHECKSUM
, &k5pac_kdc_checksum_in
);
415 talloc_free(tmp_ctx
);
419 pac_kdc_checksum_in
= data_blob_const(k5pac_kdc_checksum_in
.data
, k5pac_kdc_checksum_in
.length
);
421 ndr_err
= ndr_pull_struct_blob(&pac_kdc_checksum_in
, pac_kdc_sig
,
423 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
424 krb5_data_free(&k5pac_kdc_checksum_in
);
425 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
426 nt_status
= ndr_map_error2ntstatus(ndr_err
);
427 DEBUG(0,("can't parse the KDC signature: %s\n",
428 nt_errstr(nt_status
)));
432 *user_info_dc
= user_info_dc_out
;
438 NTSTATUS
kerberos_pac_blob_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
440 krb5_context context
,
441 struct auth_user_info_dc
**user_info_dc
,
442 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
443 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
)
447 ret
= krb5_pac_parse(context
,
448 pac_blob
.data
, pac_blob
.length
,
451 return map_nt_error_from_unix_common(ret
);
455 ret
= kerberos_pac_to_user_info_dc(mem_ctx
, pac
, context
, user_info_dc
, pac_srv_sig
, pac_kdc_sig
);
456 krb5_pac_free(context
, pac
);
458 return map_nt_error_from_unix_common(ret
);