2 Unix SMB/CIFS implementation.
4 Create and parse the krb5 PAC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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 2 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, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "system/kerberos.h"
29 #include "system/time.h"
30 #include "system/network.h"
31 #include "auth/auth.h"
32 #include "auth/kerberos/kerberos.h"
33 #include "librpc/gen_ndr/ndr_krb5pac.h"
34 #include "auth/auth.h"
36 static krb5_error_code check_pac_checksum(TALLOC_CTX *mem_ctx,
38 struct PAC_SIGNATURE_DATA *sig,
40 krb5_keyblock *keyblock)
46 cksum.cksumtype = (CKSUMTYPE)sig->type;
47 cksum.checksum.length = sig->signature.length;
48 cksum.checksum.data = sig->signature.data;
50 ret = krb5_crypto_init(context,
55 DEBUG(0,("krb5_crypto_init() failed: %s\n",
56 smb_get_krb5_error_message(context, ret, mem_ctx)));
59 ret = krb5_verify_checksum(context,
65 krb5_crypto_destroy(context, crypto);
70 NTSTATUS kerberos_decode_pac(TALLOC_CTX *mem_ctx,
71 struct PAC_DATA **pac_data_out,
74 krb5_keyblock *krbtgt_keyblock,
75 krb5_keyblock *service_keyblock,
76 krb5_const_principal client_principal,
81 struct PAC_SIGNATURE_DATA *srv_sig_ptr = NULL;
82 struct PAC_SIGNATURE_DATA *kdc_sig_ptr = NULL;
83 struct PAC_SIGNATURE_DATA *srv_sig_wipe = NULL;
84 struct PAC_SIGNATURE_DATA *kdc_sig_wipe = NULL;
85 struct PAC_LOGON_INFO *logon_info = NULL;
86 struct PAC_LOGON_NAME *logon_name = NULL;
87 struct PAC_DATA *pac_data;
88 struct PAC_DATA_RAW *pac_data_raw;
90 DATA_BLOB *srv_sig_blob;
91 DATA_BLOB *kdc_sig_blob;
93 DATA_BLOB modified_pac_blob;
94 NTTIME tgs_authtime_nttime;
95 krb5_principal client_principal_pac;
98 pac_data = talloc(mem_ctx, struct PAC_DATA);
99 pac_data_raw = talloc(mem_ctx, struct PAC_DATA_RAW);
100 kdc_sig_wipe = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
101 srv_sig_wipe = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
102 if (!pac_data_raw || !pac_data || !kdc_sig_wipe || !srv_sig_wipe) {
103 return NT_STATUS_NO_MEMORY;
106 status = ndr_pull_struct_blob(&blob, pac_data, pac_data,
107 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
108 if (!NT_STATUS_IS_OK(status)) {
109 DEBUG(0,("can't parse the PAC\n"));
113 if (pac_data->num_buffers < 4) {
114 /* we need logon_ingo, service_key and kdc_key */
115 DEBUG(0,("less than 4 PAC buffers\n"));
116 return NT_STATUS_INVALID_PARAMETER;
119 status = ndr_pull_struct_blob(&blob, pac_data_raw, pac_data_raw,
120 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA_RAW);
121 if (!NT_STATUS_IS_OK(status)) {
122 DEBUG(0,("can't parse the PAC\n"));
126 if (pac_data_raw->num_buffers < 4) {
127 /* we need logon_ingo, service_key and kdc_key */
128 DEBUG(0,("less than 4 PAC buffers\n"));
129 return NT_STATUS_INVALID_PARAMETER;
132 if (pac_data->num_buffers != pac_data_raw->num_buffers) {
133 /* we need logon_ingo, service_key and kdc_key */
134 DEBUG(0,("misparse! PAC_DATA has %d buffers while PAC_DATA_RAW has %d\n",
135 pac_data->num_buffers, pac_data_raw->num_buffers));
136 return NT_STATUS_INVALID_PARAMETER;
139 for (i=0; i < pac_data->num_buffers; i++) {
140 if (pac_data->buffers[i].type != pac_data_raw->buffers[i].type) {
141 DEBUG(0,("misparse! PAC_DATA buffer %d has type %d while PAC_DATA_RAW has %d\n",
142 i, pac_data->buffers[i].type, pac_data->buffers[i].type));
143 return NT_STATUS_INVALID_PARAMETER;
145 switch (pac_data->buffers[i].type) {
146 case PAC_TYPE_LOGON_INFO:
147 if (!pac_data->buffers[i].info) {
150 logon_info = pac_data->buffers[i].info->logon_info.info;
152 case PAC_TYPE_SRV_CHECKSUM:
153 if (!pac_data->buffers[i].info) {
156 srv_sig_ptr = &pac_data->buffers[i].info->srv_cksum;
157 srv_sig_blob = &pac_data_raw->buffers[i].info->remaining;
159 case PAC_TYPE_KDC_CHECKSUM:
160 if (!pac_data->buffers[i].info) {
163 kdc_sig_ptr = &pac_data->buffers[i].info->kdc_cksum;
164 kdc_sig_blob = &pac_data_raw->buffers[i].info->remaining;
166 case PAC_TYPE_LOGON_NAME:
167 logon_name = &pac_data->buffers[i].info->logon_name;
175 DEBUG(0,("PAC no logon_info\n"));
176 return NT_STATUS_INVALID_PARAMETER;
180 DEBUG(0,("PAC no logon_name\n"));
181 return NT_STATUS_INVALID_PARAMETER;
184 if (!srv_sig_ptr || !srv_sig_blob) {
185 DEBUG(0,("PAC no srv_key\n"));
186 return NT_STATUS_INVALID_PARAMETER;
189 if (!kdc_sig_ptr || !kdc_sig_blob) {
190 DEBUG(0,("PAC no kdc_key\n"));
191 return NT_STATUS_INVALID_PARAMETER;
194 /* Find and zero out the signatures, as required by the signing algorithm */
196 /* We find the data blobs above, now we parse them to get at the exact portion we should zero */
197 status = ndr_pull_struct_blob(kdc_sig_blob, kdc_sig_wipe, kdc_sig_wipe,
198 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(0,("can't parse the KDC signature\n"));
204 status = ndr_pull_struct_blob(srv_sig_blob, srv_sig_wipe, srv_sig_wipe,
205 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(0,("can't parse the SRV signature\n"));
211 /* Now zero the decoded structure */
212 memset(kdc_sig_wipe->signature.data, '\0', kdc_sig_wipe->signature.length);
213 memset(srv_sig_wipe->signature.data, '\0', srv_sig_wipe->signature.length);
215 /* and reencode, back into the same place it came from */
216 status = ndr_push_struct_blob(kdc_sig_blob, pac_data_raw, kdc_sig_wipe,
217 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
218 if (!NT_STATUS_IS_OK(status)) {
219 DEBUG(0,("can't repack the KDC signature\n"));
222 status = ndr_push_struct_blob(srv_sig_blob, pac_data_raw, srv_sig_wipe,
223 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
224 if (!NT_STATUS_IS_OK(status)) {
225 DEBUG(0,("can't repack the SRV signature\n"));
229 /* push out the whole structure, but now with zero'ed signatures */
230 status = ndr_push_struct_blob(&modified_pac_blob, pac_data_raw, pac_data_raw,
231 (ndr_push_flags_fn_t)ndr_push_PAC_DATA_RAW);
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(0,("can't repack the RAW PAC\n"));
237 /* verify by service_key */
238 ret = check_pac_checksum(mem_ctx,
239 modified_pac_blob, srv_sig_ptr,
243 DEBUG(1, ("PAC Decode: Failed to verify the service signature: %s\n",
244 smb_get_krb5_error_message(context, ret, mem_ctx)));
245 return NT_STATUS_ACCESS_DENIED;
248 if (krbtgt_keyblock) {
249 ret = check_pac_checksum(mem_ctx,
250 srv_sig_ptr->signature, kdc_sig_ptr,
251 context, krbtgt_keyblock);
253 DEBUG(1, ("PAC Decode: Failed to verify the KDC signature: %s\n",
254 smb_get_krb5_error_message(context, ret, mem_ctx)));
255 return NT_STATUS_ACCESS_DENIED;
259 /* Convert to NT time, so as not to loose accuracy in comparison */
260 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
262 if (tgs_authtime_nttime != logon_name->logon_time) {
263 DEBUG(2, ("PAC Decode: Logon time mismatch between ticket and PAC!\n"));
264 DEBUG(2, ("PAC Decode: PAC: %s\n", nt_time_string(mem_ctx, logon_name->logon_time)));
265 DEBUG(2, ("PAC Decode: Ticket: %s\n", nt_time_string(mem_ctx, tgs_authtime_nttime)));
266 return NT_STATUS_ACCESS_DENIED;
269 ret = krb5_parse_name_norealm(context, logon_name->account_name, &client_principal_pac);
271 DEBUG(2, ("Could not parse name from incoming PAC: [%s]: %s\n",
272 logon_name->account_name,
273 smb_get_krb5_error_message(context, ret, mem_ctx)));
274 return NT_STATUS_INVALID_PARAMETER;
277 if (!krb5_principal_compare_any_realm(context, client_principal, client_principal_pac)) {
278 DEBUG(2, ("Name in PAC [%s] does not match principal name in ticket\n",
279 logon_name->account_name));
280 return NT_STATUS_ACCESS_DENIED;
284 if (strcasecmp(logon_info->info3.base.account_name.string,
285 "Administrator")== 0) {
286 file_save("tmp_pac_data-admin.dat",blob.data,blob.length);
290 DEBUG(0,("account_name: %s [%s]\n",
291 logon_info->info3.base.account_name.string,
292 logon_info->info3.base.full_name.string));
293 *pac_data_out = pac_data;
298 NTSTATUS kerberos_pac_logon_info(TALLOC_CTX *mem_ctx,
299 struct PAC_LOGON_INFO **logon_info,
301 krb5_context context,
302 krb5_keyblock *krbtgt_keyblock,
303 krb5_keyblock *service_keyblock,
304 krb5_const_principal client_principal,
308 struct PAC_DATA *pac_data;
311 nt_status = kerberos_decode_pac(mem_ctx, &pac_data,
318 if (!NT_STATUS_IS_OK(nt_status)) {
323 for (i=0; i < pac_data->num_buffers; i++) {
324 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
327 *logon_info = pac_data->buffers[i].info->logon_info.info;
330 return NT_STATUS_INVALID_PARAMETER;
335 static krb5_error_code make_pac_checksum(TALLOC_CTX *mem_ctx,
337 struct PAC_SIGNATURE_DATA *sig,
338 krb5_context context,
339 krb5_keyblock *keyblock)
346 ret = krb5_crypto_init(context,
351 DEBUG(0,("krb5_crypto_init() failed: %s\n",
352 smb_get_krb5_error_message(context, ret, mem_ctx)));
355 ret = krb5_create_checksum(context,
363 DEBUG(2, ("PAC Verification failed: %s\n",
364 smb_get_krb5_error_message(context, ret, mem_ctx)));
367 krb5_crypto_destroy(context, crypto);
373 sig->type = cksum.cksumtype;
374 sig->signature = data_blob_talloc(mem_ctx, cksum.checksum.data, cksum.checksum.length);
375 free_Checksum(&cksum);
380 krb5_error_code kerberos_encode_pac(TALLOC_CTX *mem_ctx,
381 struct PAC_DATA *pac_data,
382 krb5_context context,
383 krb5_keyblock *krbtgt_keyblock,
384 krb5_keyblock *service_keyblock,
389 DATA_BLOB zero_blob = data_blob(NULL, 0);
390 DATA_BLOB tmp_blob = data_blob(NULL, 0);
391 struct PAC_SIGNATURE_DATA *kdc_checksum = NULL;
392 struct PAC_SIGNATURE_DATA *srv_checksum = NULL;
395 /* First, just get the keytypes filled in (and lengths right, eventually) */
396 for (i=0; i < pac_data->num_buffers; i++) {
397 if (pac_data->buffers[i].type != PAC_TYPE_KDC_CHECKSUM) {
400 kdc_checksum = &pac_data->buffers[i].info->kdc_cksum,
401 ret = make_pac_checksum(mem_ctx, &zero_blob,
403 context, krbtgt_keyblock);
405 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
406 smb_get_krb5_error_message(context, ret, mem_ctx)));
407 talloc_free(pac_data);
412 for (i=0; i < pac_data->num_buffers; i++) {
413 if (pac_data->buffers[i].type != PAC_TYPE_SRV_CHECKSUM) {
416 srv_checksum = &pac_data->buffers[i].info->srv_cksum;
417 ret = make_pac_checksum(mem_ctx, &zero_blob,
419 context, service_keyblock);
421 DEBUG(2, ("making service PAC checksum failed: %s\n",
422 smb_get_krb5_error_message(context, ret, mem_ctx)));
423 talloc_free(pac_data);
429 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!"));
433 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!"));
437 /* But wipe out the actual signatures */
438 memset(kdc_checksum->signature.data, '\0', kdc_checksum->signature.length);
439 memset(srv_checksum->signature.data, '\0', srv_checksum->signature.length);
441 nt_status = ndr_push_struct_blob(&tmp_blob, mem_ctx, pac_data,
442 (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
443 if (!NT_STATUS_IS_OK(nt_status)) {
444 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status)));
445 talloc_free(pac_data);
449 /* Then sign the result of the previous push, where the sig was zero'ed out */
450 ret = make_pac_checksum(mem_ctx, &tmp_blob, srv_checksum,
451 context, service_keyblock);
453 /* Then sign Server checksum */
454 ret = make_pac_checksum(mem_ctx, &srv_checksum->signature, kdc_checksum, context, krbtgt_keyblock);
456 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
457 smb_get_krb5_error_message(context, ret, mem_ctx)));
458 talloc_free(pac_data);
462 /* And push it out again, this time to the world. This relies on determanistic pointer values */
463 nt_status = ndr_push_struct_blob(&tmp_blob, mem_ctx, pac_data,
464 (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
465 if (!NT_STATUS_IS_OK(nt_status)) {
466 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status)));
467 talloc_free(pac_data);
477 krb5_error_code kerberos_create_pac(TALLOC_CTX *mem_ctx,
478 struct auth_serversupplied_info *server_info,
479 krb5_context context,
480 krb5_keyblock *krbtgt_keyblock,
481 krb5_keyblock *service_keyblock,
482 krb5_principal client_principal,
488 struct PAC_DATA *pac_data = talloc(mem_ctx, struct PAC_DATA);
489 struct netr_SamInfo3 *sam3;
490 union PAC_INFO *u_LOGON_INFO;
491 struct PAC_LOGON_INFO *LOGON_INFO;
492 union PAC_INFO *u_LOGON_NAME;
493 struct PAC_LOGON_NAME *LOGON_NAME;
494 union PAC_INFO *u_KDC_CHECKSUM;
495 union PAC_INFO *u_SRV_CHECKSUM;
500 PAC_BUF_LOGON_INFO = 0,
501 PAC_BUF_LOGON_NAME = 1,
502 PAC_BUF_SRV_CHECKSUM = 2,
503 PAC_BUF_KDC_CHECKSUM = 3,
504 PAC_BUF_NUM_BUFFERS = 4
511 pac_data->num_buffers = PAC_BUF_NUM_BUFFERS;
512 pac_data->version = 0;
514 pac_data->buffers = talloc_array(pac_data,
516 pac_data->num_buffers);
517 if (!pac_data->buffers) {
518 talloc_free(pac_data);
523 u_LOGON_INFO = talloc_zero(pac_data->buffers, union PAC_INFO);
525 talloc_free(pac_data);
528 pac_data->buffers[PAC_BUF_LOGON_INFO].type = PAC_TYPE_LOGON_INFO;
529 pac_data->buffers[PAC_BUF_LOGON_INFO].info = u_LOGON_INFO;
532 u_LOGON_NAME = talloc_zero(pac_data->buffers, union PAC_INFO);
534 talloc_free(pac_data);
537 pac_data->buffers[PAC_BUF_LOGON_NAME].type = PAC_TYPE_LOGON_NAME;
538 pac_data->buffers[PAC_BUF_LOGON_NAME].info = u_LOGON_NAME;
539 LOGON_NAME = &u_LOGON_NAME->logon_name;
542 u_SRV_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
543 if (!u_SRV_CHECKSUM) {
544 talloc_free(pac_data);
547 pac_data->buffers[PAC_BUF_SRV_CHECKSUM].type = PAC_TYPE_SRV_CHECKSUM;
548 pac_data->buffers[PAC_BUF_SRV_CHECKSUM].info = u_SRV_CHECKSUM;
551 u_KDC_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
552 if (!u_KDC_CHECKSUM) {
553 talloc_free(pac_data);
556 pac_data->buffers[PAC_BUF_KDC_CHECKSUM].type = PAC_TYPE_KDC_CHECKSUM;
557 pac_data->buffers[PAC_BUF_KDC_CHECKSUM].info = u_KDC_CHECKSUM;
559 /* now the real work begins... */
561 LOGON_INFO = talloc_zero(u_LOGON_INFO, struct PAC_LOGON_INFO);
563 talloc_free(pac_data);
566 nt_status = auth_convert_server_info_saminfo3(LOGON_INFO, server_info, &sam3);
567 if (!NT_STATUS_IS_OK(nt_status)) {
568 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status)));
569 talloc_free(pac_data);
573 u_LOGON_INFO->logon_info.info = LOGON_INFO;
574 LOGON_INFO->info3 = *sam3;
576 ret = krb5_unparse_name_norealm(context, client_principal, &name);
580 LOGON_NAME->account_name = talloc_strdup(LOGON_NAME, name);
583 this logon_time field is absolutely critical. This is what
584 caused all our PAC troubles :-)
586 unix_to_nt_time(&LOGON_NAME->logon_time, tgs_authtime);
588 ret = kerberos_encode_pac(mem_ctx,
594 talloc_free(pac_data);