s3:locking: ensure all share mode removal functions go through a common lease refcoun...
[Samba.git] / auth / kerberos / kerberos_pac.c
blob8f55c8ff6a4ec2d68b582f85bc242ba4da1088db
1 /*
2 Unix SMB/CIFS implementation.
3 kerberos authorization data (PAC) utility library
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Luke Howard 2002-2003
8 Copyright (C) Stefan Metzmacher 2004-2005
9 Copyright (C) Guenther Deschner 2005,2007,2008
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.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #ifdef HAVE_KRB5
28 #include "librpc/gen_ndr/ndr_krb5pac.h"
29 #include "auth/kerberos/pac_utils.h"
31 krb5_error_code check_pac_checksum(DATA_BLOB pac_data,
32 struct PAC_SIGNATURE_DATA *sig,
33 krb5_context context,
34 const krb5_keyblock *keyblock)
36 krb5_error_code ret;
37 krb5_checksum cksum;
38 krb5_keyusage usage = 0;
39 krb5_boolean checksum_valid = false;
40 krb5_data input;
42 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM /* Heimdal */
43 cksum.cksumtype = (krb5_cksumtype)sig->type;
44 cksum.checksum.length = sig->signature.length;
45 cksum.checksum.data = sig->signature.data;
46 #else /* MIT */
47 cksum.checksum_type = (krb5_cksumtype)sig->type;
48 cksum.length = sig->signature.length;
49 cksum.contents = sig->signature.data;
50 #endif
52 #ifdef HAVE_KRB5_KU_OTHER_CKSUM /* Heimdal */
53 usage = KRB5_KU_OTHER_CKSUM;
54 #elif defined(HAVE_KRB5_KEYUSAGE_APP_DATA_CKSUM) /* MIT */
55 usage = KRB5_KEYUSAGE_APP_DATA_CKSUM;
56 #else
57 #error UNKNOWN_KRB5_KEYUSAGE
58 #endif
60 input.data = (char *)pac_data.data;
61 input.length = pac_data.length;
63 ret = krb5_c_verify_checksum(context,
64 keyblock,
65 usage,
66 &input,
67 &cksum,
68 &checksum_valid);
69 if (!checksum_valid) {
70 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
72 if (ret){
73 DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
74 error_message(ret), ret));
75 return ret;
78 return ret;
81 /**
82 * @brief Decode a blob containing a NDR encoded PAC structure
84 * @param mem_ctx - The memory context
85 * @param pac_data_blob - The data blob containing the NDR encoded data
86 * @param context - The Kerberos Context
87 * @param service_keyblock - The Service Key used to verify the checksum
88 * @param client_principal - The client principal
89 * @param tgs_authtime - The ticket timestamp
90 * @param pac_data_out - [out] The decoded PAC
92 * @return - A NTSTATUS error code
94 NTSTATUS kerberos_decode_pac(TALLOC_CTX *mem_ctx,
95 DATA_BLOB pac_data_blob,
96 krb5_context context,
97 const krb5_keyblock *krbtgt_keyblock,
98 const krb5_keyblock *service_keyblock,
99 krb5_const_principal client_principal,
100 time_t tgs_authtime,
101 struct PAC_DATA **pac_data_out)
103 NTSTATUS status;
104 enum ndr_err_code ndr_err;
105 krb5_error_code ret;
106 DATA_BLOB modified_pac_blob;
108 NTTIME tgs_authtime_nttime;
109 krb5_principal client_principal_pac = NULL;
110 int i;
112 struct PAC_SIGNATURE_DATA *srv_sig_ptr = NULL;
113 struct PAC_SIGNATURE_DATA *kdc_sig_ptr = NULL;
114 struct PAC_SIGNATURE_DATA *srv_sig_wipe = NULL;
115 struct PAC_SIGNATURE_DATA *kdc_sig_wipe = NULL;
116 struct PAC_LOGON_NAME *logon_name = NULL;
117 struct PAC_LOGON_INFO *logon_info = NULL;
118 struct PAC_DATA *pac_data = NULL;
119 struct PAC_DATA_RAW *pac_data_raw = NULL;
121 DATA_BLOB *srv_sig_blob = NULL;
122 DATA_BLOB *kdc_sig_blob = NULL;
124 bool bool_ret;
126 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
127 if (!tmp_ctx) {
128 return NT_STATUS_NO_MEMORY;
131 if (pac_data_out) {
132 *pac_data_out = NULL;
135 pac_data = talloc(tmp_ctx, struct PAC_DATA);
136 pac_data_raw = talloc(tmp_ctx, struct PAC_DATA_RAW);
137 kdc_sig_wipe = talloc(tmp_ctx, struct PAC_SIGNATURE_DATA);
138 srv_sig_wipe = talloc(tmp_ctx, struct PAC_SIGNATURE_DATA);
139 if (!pac_data_raw || !pac_data || !kdc_sig_wipe || !srv_sig_wipe) {
140 talloc_free(tmp_ctx);
141 return NT_STATUS_NO_MEMORY;
144 ndr_err = ndr_pull_struct_blob(&pac_data_blob, pac_data, pac_data,
145 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
146 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
147 status = ndr_map_error2ntstatus(ndr_err);
148 DEBUG(0,("can't parse the PAC: %s\n",
149 nt_errstr(status)));
150 talloc_free(tmp_ctx);
151 return status;
154 if (pac_data->num_buffers < 4) {
155 /* we need logon_ingo, service_key and kdc_key */
156 DEBUG(0,("less than 4 PAC buffers\n"));
157 talloc_free(tmp_ctx);
158 return NT_STATUS_INVALID_PARAMETER;
161 ndr_err = ndr_pull_struct_blob(
162 &pac_data_blob, pac_data_raw, pac_data_raw,
163 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA_RAW);
164 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
165 status = ndr_map_error2ntstatus(ndr_err);
166 DEBUG(0,("can't parse the PAC: %s\n",
167 nt_errstr(status)));
168 talloc_free(tmp_ctx);
169 return status;
172 if (pac_data_raw->num_buffers < 4) {
173 /* we need logon_ingo, service_key and kdc_key */
174 DEBUG(0,("less than 4 PAC buffers\n"));
175 talloc_free(tmp_ctx);
176 return NT_STATUS_INVALID_PARAMETER;
179 if (pac_data->num_buffers != pac_data_raw->num_buffers) {
180 /* we need logon_ingo, service_key and kdc_key */
181 DEBUG(0, ("misparse! PAC_DATA has %d buffers while "
182 "PAC_DATA_RAW has %d\n", pac_data->num_buffers,
183 pac_data_raw->num_buffers));
184 talloc_free(tmp_ctx);
185 return NT_STATUS_INVALID_PARAMETER;
188 for (i=0; i < pac_data->num_buffers; i++) {
189 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
190 struct PAC_BUFFER_RAW *raw_buf = &pac_data_raw->buffers[i];
192 if (data_buf->type != raw_buf->type) {
193 DEBUG(0, ("misparse! PAC_DATA buffer %d has type "
194 "%d while PAC_DATA_RAW has %d\n", i,
195 data_buf->type, raw_buf->type));
196 talloc_free(tmp_ctx);
197 return NT_STATUS_INVALID_PARAMETER;
199 switch (data_buf->type) {
200 case PAC_TYPE_LOGON_INFO:
201 if (!data_buf->info) {
202 break;
204 logon_info = data_buf->info->logon_info.info;
205 break;
206 case PAC_TYPE_SRV_CHECKSUM:
207 if (!data_buf->info) {
208 break;
210 srv_sig_ptr = &data_buf->info->srv_cksum;
211 srv_sig_blob = &raw_buf->info->remaining;
212 break;
213 case PAC_TYPE_KDC_CHECKSUM:
214 if (!data_buf->info) {
215 break;
217 kdc_sig_ptr = &data_buf->info->kdc_cksum;
218 kdc_sig_blob = &raw_buf->info->remaining;
219 break;
220 case PAC_TYPE_LOGON_NAME:
221 logon_name = &data_buf->info->logon_name;
222 break;
223 default:
224 break;
228 if (!logon_info) {
229 DEBUG(0,("PAC no logon_info\n"));
230 talloc_free(tmp_ctx);
231 return NT_STATUS_INVALID_PARAMETER;
234 if (!logon_name) {
235 DEBUG(0,("PAC no logon_name\n"));
236 talloc_free(tmp_ctx);
237 return NT_STATUS_INVALID_PARAMETER;
240 if (!srv_sig_ptr || !srv_sig_blob) {
241 DEBUG(0,("PAC no srv_key\n"));
242 talloc_free(tmp_ctx);
243 return NT_STATUS_INVALID_PARAMETER;
246 if (!kdc_sig_ptr || !kdc_sig_blob) {
247 DEBUG(0,("PAC no kdc_key\n"));
248 talloc_free(tmp_ctx);
249 return NT_STATUS_INVALID_PARAMETER;
252 /* Find and zero out the signatures,
253 * as required by the signing algorithm */
255 /* We find the data blobs above,
256 * now we parse them to get at the exact portion we should zero */
257 ndr_err = ndr_pull_struct_blob(
258 kdc_sig_blob, kdc_sig_wipe, kdc_sig_wipe,
259 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
260 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
261 status = ndr_map_error2ntstatus(ndr_err);
262 DEBUG(0,("can't parse the KDC signature: %s\n",
263 nt_errstr(status)));
264 talloc_free(tmp_ctx);
265 return status;
268 ndr_err = ndr_pull_struct_blob(
269 srv_sig_blob, srv_sig_wipe, srv_sig_wipe,
270 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
271 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
272 status = ndr_map_error2ntstatus(ndr_err);
273 DEBUG(0,("can't parse the SRV signature: %s\n",
274 nt_errstr(status)));
275 talloc_free(tmp_ctx);
276 return status;
279 /* Now zero the decoded structure */
280 memset(kdc_sig_wipe->signature.data,
281 '\0', kdc_sig_wipe->signature.length);
282 memset(srv_sig_wipe->signature.data,
283 '\0', srv_sig_wipe->signature.length);
285 /* and reencode, back into the same place it came from */
286 ndr_err = ndr_push_struct_blob(
287 kdc_sig_blob, pac_data_raw, kdc_sig_wipe,
288 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
289 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
290 status = ndr_map_error2ntstatus(ndr_err);
291 DEBUG(0,("can't repack the KDC signature: %s\n",
292 nt_errstr(status)));
293 talloc_free(tmp_ctx);
294 return status;
296 ndr_err = ndr_push_struct_blob(
297 srv_sig_blob, pac_data_raw, srv_sig_wipe,
298 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
299 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
300 status = ndr_map_error2ntstatus(ndr_err);
301 DEBUG(0,("can't repack the SRV signature: %s\n",
302 nt_errstr(status)));
303 talloc_free(tmp_ctx);
304 return status;
307 /* push out the whole structure, but now with zero'ed signatures */
308 ndr_err = ndr_push_struct_blob(
309 &modified_pac_blob, pac_data_raw, pac_data_raw,
310 (ndr_push_flags_fn_t)ndr_push_PAC_DATA_RAW);
311 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
312 status = ndr_map_error2ntstatus(ndr_err);
313 DEBUG(0,("can't repack the RAW PAC: %s\n",
314 nt_errstr(status)));
315 talloc_free(tmp_ctx);
316 return status;
319 if (service_keyblock) {
320 /* verify by service_key */
321 ret = check_pac_checksum(modified_pac_blob, srv_sig_ptr,
322 context,
323 service_keyblock);
324 if (ret) {
325 DEBUG(5, ("PAC Decode: Failed to verify the service "
326 "signature: %s\n", error_message(ret)));
327 return NT_STATUS_ACCESS_DENIED;
330 if (krbtgt_keyblock) {
331 /* verify the service key checksum by krbtgt_key */
332 ret = check_pac_checksum(srv_sig_ptr->signature, kdc_sig_ptr,
333 context, krbtgt_keyblock);
334 if (ret) {
335 DEBUG(1, ("PAC Decode: Failed to verify the KDC signature: %s\n",
336 smb_get_krb5_error_message(context, ret, tmp_ctx)));
337 talloc_free(tmp_ctx);
338 return NT_STATUS_ACCESS_DENIED;
343 if (tgs_authtime) {
344 /* Convert to NT time, so as not to loose accuracy in comparison */
345 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
347 if (tgs_authtime_nttime != logon_name->logon_time) {
348 DEBUG(2, ("PAC Decode: "
349 "Logon time mismatch between ticket and PAC!\n"));
350 DEBUG(2, ("PAC Decode: PAC: %s\n",
351 nt_time_string(tmp_ctx, logon_name->logon_time)));
352 DEBUG(2, ("PAC Decode: Ticket: %s\n",
353 nt_time_string(tmp_ctx, tgs_authtime_nttime)));
354 talloc_free(tmp_ctx);
355 return NT_STATUS_ACCESS_DENIED;
359 if (client_principal) {
360 ret = smb_krb5_parse_name_norealm(context,
361 logon_name->account_name,
362 &client_principal_pac);
363 if (ret) {
364 DEBUG(2, ("Could not parse name from PAC: [%s]:%s\n",
365 logon_name->account_name, error_message(ret)));
366 talloc_free(tmp_ctx);
367 return NT_STATUS_INVALID_PARAMETER;
370 bool_ret = smb_krb5_principal_compare_any_realm(context,
371 client_principal,
372 client_principal_pac);
374 krb5_free_principal(context, client_principal_pac);
376 if (!bool_ret) {
377 DEBUG(2, ("Name in PAC [%s] does not match principal name "
378 "in ticket\n", logon_name->account_name));
379 talloc_free(tmp_ctx);
380 return NT_STATUS_ACCESS_DENIED;
384 DEBUG(3,("Found account name from PAC: %s [%s]\n",
385 logon_info->info3.base.account_name.string,
386 logon_info->info3.base.full_name.string));
388 DEBUG(10,("Successfully validated Kerberos PAC\n"));
390 if (DEBUGLEVEL >= 10) {
391 const char *s;
392 s = NDR_PRINT_STRUCT_STRING(tmp_ctx, PAC_DATA, pac_data);
393 if (s) {
394 DEBUGADD(10,("%s\n", s));
398 if (pac_data_out) {
399 *pac_data_out = talloc_steal(mem_ctx, pac_data);
402 return NT_STATUS_OK;
405 NTSTATUS kerberos_pac_logon_info(TALLOC_CTX *mem_ctx,
406 DATA_BLOB blob,
407 krb5_context context,
408 const krb5_keyblock *krbtgt_keyblock,
409 const krb5_keyblock *service_keyblock,
410 krb5_const_principal client_principal,
411 time_t tgs_authtime,
412 struct PAC_LOGON_INFO **logon_info)
414 NTSTATUS nt_status;
415 struct PAC_DATA *pac_data;
416 int i;
417 nt_status = kerberos_decode_pac(mem_ctx,
418 blob,
419 context,
420 krbtgt_keyblock,
421 service_keyblock,
422 client_principal,
423 tgs_authtime,
424 &pac_data);
425 if (!NT_STATUS_IS_OK(nt_status)) {
426 return nt_status;
429 *logon_info = NULL;
430 for (i=0; i < pac_data->num_buffers; i++) {
431 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
432 continue;
434 *logon_info = pac_data->buffers[i].info->logon_info.info;
436 if (!*logon_info) {
437 return NT_STATUS_INVALID_PARAMETER;
439 return NT_STATUS_OK;
442 #endif