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"
33 #include "auth/kerberos/kerberos_util.h"
34 #include "auth/kerberos/pac_utils.h"
36 krb5_error_code
kerberos_encode_pac(TALLOC_CTX
*mem_ctx
,
37 struct PAC_DATA
*pac_data
,
39 const krb5_keyblock
*krbtgt_keyblock
,
40 const krb5_keyblock
*service_keyblock
,
45 enum ndr_err_code ndr_err
;
46 DATA_BLOB zero_blob
= data_blob(NULL
, 0);
47 DATA_BLOB tmp_blob
= data_blob(NULL
, 0);
48 struct PAC_SIGNATURE_DATA
*kdc_checksum
= NULL
;
49 struct PAC_SIGNATURE_DATA
*srv_checksum
= NULL
;
52 /* First, just get the keytypes filled in (and lengths right, eventually) */
53 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
54 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_KDC_CHECKSUM
) {
57 kdc_checksum
= &pac_data
->buffers
[i
].info
->kdc_cksum
,
58 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
63 &kdc_checksum
->signature
);
65 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
66 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
67 talloc_free(pac_data
);
72 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
73 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_SRV_CHECKSUM
) {
76 srv_checksum
= &pac_data
->buffers
[i
].info
->srv_cksum
;
77 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
82 &srv_checksum
->signature
);
84 DEBUG(2, ("making service PAC checksum failed: %s\n",
85 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
86 talloc_free(pac_data
);
92 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!"));
96 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!"));
100 /* But wipe out the actual signatures */
101 memset(kdc_checksum
->signature
.data
, '\0', kdc_checksum
->signature
.length
);
102 memset(srv_checksum
->signature
.data
, '\0', srv_checksum
->signature
.length
);
104 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
106 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
107 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
108 nt_status
= ndr_map_error2ntstatus(ndr_err
);
109 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status
)));
110 talloc_free(pac_data
);
114 /* Then sign the result of the previous push, where the sig was zero'ed out */
115 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
120 &srv_checksum
->signature
);
122 /* Then sign Server checksum */
123 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
124 &srv_checksum
->signature
,
128 &kdc_checksum
->signature
);
130 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
131 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
132 talloc_free(pac_data
);
136 /* And push it out again, this time to the world. This relies on determanistic pointer values */
137 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
139 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
140 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
141 nt_status
= ndr_map_error2ntstatus(ndr_err
);
142 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status
)));
143 talloc_free(pac_data
);
153 krb5_error_code
kerberos_create_pac(TALLOC_CTX
*mem_ctx
,
154 struct auth_user_info_dc
*user_info_dc
,
155 krb5_context context
,
156 const krb5_keyblock
*krbtgt_keyblock
,
157 const krb5_keyblock
*service_keyblock
,
158 krb5_principal client_principal
,
164 struct PAC_DATA
*pac_data
= talloc(mem_ctx
, struct PAC_DATA
);
165 struct netr_SamInfo3
*sam3
;
166 union PAC_INFO
*u_LOGON_INFO
;
167 struct PAC_LOGON_INFO
*LOGON_INFO
;
168 union PAC_INFO
*u_LOGON_NAME
;
169 struct PAC_LOGON_NAME
*LOGON_NAME
;
170 union PAC_INFO
*u_KDC_CHECKSUM
;
171 union PAC_INFO
*u_SRV_CHECKSUM
;
176 PAC_BUF_LOGON_INFO
= 0,
177 PAC_BUF_LOGON_NAME
= 1,
178 PAC_BUF_SRV_CHECKSUM
= 2,
179 PAC_BUF_KDC_CHECKSUM
= 3,
180 PAC_BUF_NUM_BUFFERS
= 4
187 pac_data
->num_buffers
= PAC_BUF_NUM_BUFFERS
;
188 pac_data
->version
= 0;
190 pac_data
->buffers
= talloc_array(pac_data
,
192 pac_data
->num_buffers
);
193 if (!pac_data
->buffers
) {
194 talloc_free(pac_data
);
199 u_LOGON_INFO
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
201 talloc_free(pac_data
);
204 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].type
= PAC_TYPE_LOGON_INFO
;
205 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].info
= u_LOGON_INFO
;
208 u_LOGON_NAME
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
210 talloc_free(pac_data
);
213 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].type
= PAC_TYPE_LOGON_NAME
;
214 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].info
= u_LOGON_NAME
;
215 LOGON_NAME
= &u_LOGON_NAME
->logon_name
;
218 u_SRV_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
219 if (!u_SRV_CHECKSUM
) {
220 talloc_free(pac_data
);
223 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].type
= PAC_TYPE_SRV_CHECKSUM
;
224 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].info
= u_SRV_CHECKSUM
;
227 u_KDC_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
228 if (!u_KDC_CHECKSUM
) {
229 talloc_free(pac_data
);
232 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].type
= PAC_TYPE_KDC_CHECKSUM
;
233 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].info
= u_KDC_CHECKSUM
;
235 /* now the real work begins... */
237 LOGON_INFO
= talloc_zero(u_LOGON_INFO
, struct PAC_LOGON_INFO
);
239 talloc_free(pac_data
);
242 nt_status
= auth_convert_user_info_dc_saminfo3(LOGON_INFO
, user_info_dc
, &sam3
);
243 if (!NT_STATUS_IS_OK(nt_status
)) {
244 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status
)));
245 talloc_free(pac_data
);
249 u_LOGON_INFO
->logon_info
.info
= LOGON_INFO
;
250 LOGON_INFO
->info3
= *sam3
;
252 ret
= krb5_unparse_name_flags(context
, client_principal
,
253 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
257 LOGON_NAME
->account_name
= talloc_strdup(LOGON_NAME
, name
);
260 this logon_time field is absolutely critical. This is what
261 caused all our PAC troubles :-)
263 unix_to_nt_time(&LOGON_NAME
->logon_time
, tgs_authtime
);
265 ret
= kerberos_encode_pac(mem_ctx
,
271 talloc_free(pac_data
);
275 krb5_error_code
kerberos_pac_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
277 krb5_context context
,
278 struct auth_user_info_dc
**user_info_dc
,
279 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
280 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
)
283 enum ndr_err_code ndr_err
;
286 DATA_BLOB pac_logon_info_in
, pac_srv_checksum_in
, pac_kdc_checksum_in
;
287 krb5_data k5pac_logon_info_in
, k5pac_srv_checksum_in
, k5pac_kdc_checksum_in
;
290 struct auth_user_info_dc
*user_info_dc_out
;
292 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
298 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_LOGON_INFO
, &k5pac_logon_info_in
);
300 talloc_free(tmp_ctx
);
304 pac_logon_info_in
= data_blob_const(k5pac_logon_info_in
.data
, k5pac_logon_info_in
.length
);
306 ndr_err
= ndr_pull_union_blob(&pac_logon_info_in
, tmp_ctx
, &info
,
308 (ndr_pull_flags_fn_t
)ndr_pull_PAC_INFO
);
309 kerberos_free_data_contents(context
, &k5pac_logon_info_in
);
310 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
) || !info
.logon_info
.info
) {
311 nt_status
= ndr_map_error2ntstatus(ndr_err
);
312 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status
)));
313 talloc_free(tmp_ctx
);
317 /* Pull this right into the normal auth sysstem structures */
318 nt_status
= make_user_info_dc_pac(mem_ctx
,
319 info
.logon_info
.info
,
321 if (!NT_STATUS_IS_OK(nt_status
)) {
322 talloc_free(tmp_ctx
);
327 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_SRV_CHECKSUM
, &k5pac_srv_checksum_in
);
329 talloc_free(tmp_ctx
);
333 pac_srv_checksum_in
= data_blob_const(k5pac_srv_checksum_in
.data
, k5pac_srv_checksum_in
.length
);
335 ndr_err
= ndr_pull_struct_blob(&pac_srv_checksum_in
, pac_srv_sig
,
337 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
338 kerberos_free_data_contents(context
, &k5pac_srv_checksum_in
);
339 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
340 nt_status
= ndr_map_error2ntstatus(ndr_err
);
341 DEBUG(0,("can't parse the KDC signature: %s\n",
342 nt_errstr(nt_status
)));
348 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_KDC_CHECKSUM
, &k5pac_kdc_checksum_in
);
350 talloc_free(tmp_ctx
);
354 pac_kdc_checksum_in
= data_blob_const(k5pac_kdc_checksum_in
.data
, k5pac_kdc_checksum_in
.length
);
356 ndr_err
= ndr_pull_struct_blob(&pac_kdc_checksum_in
, pac_kdc_sig
,
358 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
359 kerberos_free_data_contents(context
, &k5pac_kdc_checksum_in
);
360 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
361 nt_status
= ndr_map_error2ntstatus(ndr_err
);
362 DEBUG(0,("can't parse the KDC signature: %s\n",
363 nt_errstr(nt_status
)));
367 *user_info_dc
= user_info_dc_out
;
373 NTSTATUS
kerberos_pac_blob_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
375 krb5_context context
,
376 struct auth_user_info_dc
**user_info_dc
,
377 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
378 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
)
382 ret
= krb5_pac_parse(context
,
383 pac_blob
.data
, pac_blob
.length
,
386 return map_nt_error_from_unix_common(ret
);
390 ret
= kerberos_pac_to_user_info_dc(mem_ctx
, pac
, context
, user_info_dc
, pac_srv_sig
, pac_kdc_sig
);
391 krb5_pac_free(context
, pac
);
393 return map_nt_error_from_unix_common(ret
);