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/credentials/credentials.h"
34 #include "auth/kerberos/kerberos_util.h"
35 #include "auth/kerberos/pac_utils.h"
37 krb5_error_code kerberos_encode_pac(TALLOC_CTX *mem_ctx,
38 struct PAC_DATA *pac_data,
40 const krb5_keyblock *krbtgt_keyblock,
41 const krb5_keyblock *service_keyblock,
46 enum ndr_err_code ndr_err;
47 DATA_BLOB zero_blob = data_blob(NULL, 0);
48 DATA_BLOB tmp_blob = data_blob(NULL, 0);
49 struct PAC_SIGNATURE_DATA *kdc_checksum = NULL;
50 struct PAC_SIGNATURE_DATA *srv_checksum = NULL;
53 /* First, just get the keytypes filled in (and lengths right, eventually) */
54 for (i=0; i < pac_data->num_buffers; i++) {
55 if (pac_data->buffers[i].type != PAC_TYPE_KDC_CHECKSUM) {
58 kdc_checksum = &pac_data->buffers[i].info->kdc_cksum,
59 ret = smb_krb5_make_pac_checksum(mem_ctx,
64 &kdc_checksum->signature);
66 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
67 smb_get_krb5_error_message(context, ret, mem_ctx)));
68 talloc_free(pac_data);
73 for (i=0; i < pac_data->num_buffers; i++) {
74 if (pac_data->buffers[i].type != PAC_TYPE_SRV_CHECKSUM) {
77 srv_checksum = &pac_data->buffers[i].info->srv_cksum;
78 ret = smb_krb5_make_pac_checksum(mem_ctx,
83 &srv_checksum->signature);
85 DEBUG(2, ("making service PAC checksum failed: %s\n",
86 smb_get_krb5_error_message(context, ret, mem_ctx)));
87 talloc_free(pac_data);
93 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!"));
97 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!"));
101 /* But wipe out the actual signatures */
102 memset(kdc_checksum->signature.data, '\0', kdc_checksum->signature.length);
103 memset(srv_checksum->signature.data, '\0', srv_checksum->signature.length);
105 ndr_err = ndr_push_struct_blob(&tmp_blob, mem_ctx,
107 (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
108 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
109 nt_status = ndr_map_error2ntstatus(ndr_err);
110 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status)));
111 talloc_free(pac_data);
115 /* Then sign the result of the previous push, where the sig was zero'ed out */
116 ret = smb_krb5_make_pac_checksum(mem_ctx,
121 &srv_checksum->signature);
124 DBG_WARNING("making krbtgt PAC srv_checksum failed: %s\n",
125 smb_get_krb5_error_message(context, ret, mem_ctx));
126 talloc_free(pac_data);
130 /* Then sign Server checksum */
131 ret = smb_krb5_make_pac_checksum(mem_ctx,
132 &srv_checksum->signature,
136 &kdc_checksum->signature);
138 DBG_WARNING("making krbtgt PAC kdc_checksum failed: %s\n",
139 smb_get_krb5_error_message(context, ret, mem_ctx));
140 talloc_free(pac_data);
144 /* And push it out again, this time to the world. This relies on determanistic pointer values */
145 ndr_err = ndr_push_struct_blob(&tmp_blob, mem_ctx,
147 (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
148 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
149 nt_status = ndr_map_error2ntstatus(ndr_err);
150 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status)));
151 talloc_free(pac_data);
161 krb5_error_code kerberos_create_pac(TALLOC_CTX *mem_ctx,
162 struct auth_user_info_dc *user_info_dc,
163 krb5_context context,
164 const krb5_keyblock *krbtgt_keyblock,
165 const krb5_keyblock *service_keyblock,
166 krb5_principal client_principal,
172 struct PAC_DATA *pac_data = talloc(mem_ctx, struct PAC_DATA);
173 struct netr_SamInfo3 *sam3;
174 union PAC_INFO *u_LOGON_INFO;
175 struct PAC_LOGON_INFO *LOGON_INFO;
176 union PAC_INFO *u_LOGON_NAME;
177 struct PAC_LOGON_NAME *LOGON_NAME;
178 union PAC_INFO *u_KDC_CHECKSUM;
179 union PAC_INFO *u_SRV_CHECKSUM;
184 PAC_BUF_LOGON_INFO = 0,
185 PAC_BUF_LOGON_NAME = 1,
186 PAC_BUF_SRV_CHECKSUM = 2,
187 PAC_BUF_KDC_CHECKSUM = 3,
188 PAC_BUF_NUM_BUFFERS = 4
195 pac_data->num_buffers = PAC_BUF_NUM_BUFFERS;
196 pac_data->version = 0;
198 pac_data->buffers = talloc_array(pac_data,
200 pac_data->num_buffers);
201 if (!pac_data->buffers) {
202 talloc_free(pac_data);
207 u_LOGON_INFO = talloc_zero(pac_data->buffers, union PAC_INFO);
209 talloc_free(pac_data);
212 pac_data->buffers[PAC_BUF_LOGON_INFO].type = PAC_TYPE_LOGON_INFO;
213 pac_data->buffers[PAC_BUF_LOGON_INFO].info = u_LOGON_INFO;
216 u_LOGON_NAME = talloc_zero(pac_data->buffers, union PAC_INFO);
218 talloc_free(pac_data);
221 pac_data->buffers[PAC_BUF_LOGON_NAME].type = PAC_TYPE_LOGON_NAME;
222 pac_data->buffers[PAC_BUF_LOGON_NAME].info = u_LOGON_NAME;
223 LOGON_NAME = &u_LOGON_NAME->logon_name;
226 u_SRV_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
227 if (!u_SRV_CHECKSUM) {
228 talloc_free(pac_data);
231 pac_data->buffers[PAC_BUF_SRV_CHECKSUM].type = PAC_TYPE_SRV_CHECKSUM;
232 pac_data->buffers[PAC_BUF_SRV_CHECKSUM].info = u_SRV_CHECKSUM;
235 u_KDC_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
236 if (!u_KDC_CHECKSUM) {
237 talloc_free(pac_data);
240 pac_data->buffers[PAC_BUF_KDC_CHECKSUM].type = PAC_TYPE_KDC_CHECKSUM;
241 pac_data->buffers[PAC_BUF_KDC_CHECKSUM].info = u_KDC_CHECKSUM;
243 /* now the real work begins... */
245 LOGON_INFO = talloc_zero(u_LOGON_INFO, struct PAC_LOGON_INFO);
247 talloc_free(pac_data);
250 nt_status = auth_convert_user_info_dc_saminfo3(LOGON_INFO, user_info_dc, &sam3);
251 if (!NT_STATUS_IS_OK(nt_status)) {
252 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status)));
253 talloc_free(pac_data);
257 u_LOGON_INFO->logon_info.info = LOGON_INFO;
258 LOGON_INFO->info3 = *sam3;
260 ret = krb5_unparse_name_flags(context, client_principal,
261 KRB5_PRINCIPAL_UNPARSE_NO_REALM |
262 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
267 LOGON_NAME->account_name = talloc_strdup(LOGON_NAME, name);
270 this logon_time field is absolutely critical. This is what
271 caused all our PAC troubles :-)
273 unix_to_nt_time(&LOGON_NAME->logon_time, tgs_authtime);
275 ret = kerberos_encode_pac(mem_ctx,
281 talloc_free(pac_data);
285 static krb5_error_code kerberos_pac_buffer_present(krb5_context context,
286 const krb5_const_pac pac,
289 #ifdef SAMBA4_USES_HEIMDAL
290 return krb5_pac_get_buffer(context, pac, type, NULL);
296 * MIT won't let us pass NULL for the data parameter, so we are forced
297 * to allocate a new buffer and then immediately free it.
299 ret = krb5_pac_get_buffer(context, pac, type, &data);
301 krb5_free_data_contents(context, &data);
304 #endif /* SAMBA4_USES_HEIMDAL */
307 krb5_error_code kerberos_pac_to_user_info_dc(TALLOC_CTX *mem_ctx,
309 krb5_context context,
310 struct auth_user_info_dc **user_info_dc,
311 struct PAC_SIGNATURE_DATA *pac_srv_sig,
312 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
315 enum ndr_err_code ndr_err;
318 DATA_BLOB pac_logon_info_in, pac_srv_checksum_in, pac_kdc_checksum_in;
319 krb5_data k5pac_logon_info_in, k5pac_srv_checksum_in, k5pac_kdc_checksum_in;
320 DATA_BLOB pac_upn_dns_info_in;
321 krb5_data k5pac_upn_dns_info_in;
324 union PAC_INFO _upn_dns_info;
325 struct PAC_UPN_DNS_INFO *upn_dns_info = NULL;
326 struct auth_user_info_dc *user_info_dc_out;
328 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
334 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_LOGON_INFO, &k5pac_logon_info_in);
336 talloc_free(tmp_ctx);
340 pac_logon_info_in = data_blob_const(k5pac_logon_info_in.data, k5pac_logon_info_in.length);
342 ndr_err = ndr_pull_union_blob(&pac_logon_info_in, tmp_ctx, &info,
344 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
345 smb_krb5_free_data_contents(context, &k5pac_logon_info_in);
346 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
347 nt_status = ndr_map_error2ntstatus(ndr_err);
348 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
349 talloc_free(tmp_ctx);
352 if (info.logon_info.info == NULL) {
353 DEBUG(0,("can't parse the PAC LOGON_INFO: missing info pointer\n"));
354 talloc_free(tmp_ctx);
358 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_UPN_DNS_INFO,
359 &k5pac_upn_dns_info_in);
361 ZERO_STRUCT(k5pac_upn_dns_info_in);
365 talloc_free(tmp_ctx);
369 pac_upn_dns_info_in = data_blob_const(k5pac_upn_dns_info_in.data,
370 k5pac_upn_dns_info_in.length);
372 if (pac_upn_dns_info_in.length != 0) {
373 ndr_err = ndr_pull_union_blob(&pac_upn_dns_info_in, tmp_ctx,
375 PAC_TYPE_UPN_DNS_INFO,
376 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
377 smb_krb5_free_data_contents(context, &k5pac_upn_dns_info_in);
378 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
379 nt_status = ndr_map_error2ntstatus(ndr_err);
380 DEBUG(0,("can't parse the PAC UPN_DNS_INFO: %s\n",
381 nt_errstr(nt_status)));
382 talloc_free(tmp_ctx);
385 upn_dns_info = &_upn_dns_info.upn_dns_info;
388 /* Pull this right into the normal auth system structures */
389 nt_status = make_user_info_dc_pac(mem_ctx,
390 info.logon_info.info,
393 if (!NT_STATUS_IS_OK(nt_status)) {
394 DBG_ERR("make_user_info_dc_pac() failed -%s\n",
395 nt_errstr(nt_status));
396 NDR_PRINT_DEBUG(PAC_LOGON_INFO, info.logon_info.info);
397 if (upn_dns_info != NULL) {
398 NDR_PRINT_DEBUG(PAC_UPN_DNS_INFO, upn_dns_info);
400 talloc_free(tmp_ctx);
405 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_SRV_CHECKSUM, &k5pac_srv_checksum_in);
407 talloc_free(tmp_ctx);
411 pac_srv_checksum_in = data_blob_const(k5pac_srv_checksum_in.data, k5pac_srv_checksum_in.length);
413 ndr_err = ndr_pull_struct_blob(&pac_srv_checksum_in, pac_srv_sig,
415 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
416 smb_krb5_free_data_contents(context, &k5pac_srv_checksum_in);
417 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
418 nt_status = ndr_map_error2ntstatus(ndr_err);
419 DEBUG(0,("can't parse the server signature: %s\n",
420 nt_errstr(nt_status)));
426 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_KDC_CHECKSUM, &k5pac_kdc_checksum_in);
428 talloc_free(tmp_ctx);
432 pac_kdc_checksum_in = data_blob_const(k5pac_kdc_checksum_in.data, k5pac_kdc_checksum_in.length);
434 ndr_err = ndr_pull_struct_blob(&pac_kdc_checksum_in, pac_kdc_sig,
436 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
437 smb_krb5_free_data_contents(context, &k5pac_kdc_checksum_in);
438 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
439 nt_status = ndr_map_error2ntstatus(ndr_err);
440 DEBUG(0,("can't parse the KDC signature: %s\n",
441 nt_errstr(nt_status)));
447 * Based on the presence of a REQUESTER_SID PAC buffer, ascertain
448 * whether the ticket is a TGT. This helps the KDC and kpasswd service
449 * ensure they do not accept tickets meant for the other.
451 * This heuristic will fail for older Samba versions and Windows prior
452 * to Nov. 2021 updates, which lack support for the REQUESTER_SID PAC
455 ret = kerberos_pac_buffer_present(context, pac, PAC_TYPE_REQUESTER_SID);
457 /* This probably isn't a TGT. */
458 user_info_dc_out->ticket_type = TICKET_TYPE_NON_TGT;
459 } else if (ret != 0) {
460 talloc_free(tmp_ctx);
463 /* This probably is a TGT. */
464 user_info_dc_out->ticket_type = TICKET_TYPE_TGT;
467 *user_info_dc = user_info_dc_out;
473 NTSTATUS kerberos_pac_blob_to_user_info_dc(TALLOC_CTX *mem_ctx,
475 krb5_context context,
476 struct auth_user_info_dc **user_info_dc,
477 struct PAC_SIGNATURE_DATA *pac_srv_sig,
478 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
482 ret = krb5_pac_parse(context,
483 pac_blob.data, pac_blob.length,
486 return map_nt_error_from_unix_common(ret);
490 ret = kerberos_pac_to_user_info_dc(mem_ctx, pac, context, user_info_dc, pac_srv_sig, pac_kdc_sig);
491 krb5_pac_free(context, pac);
493 return map_nt_error_from_unix_common(ret);