auth4: avoid map_user_info() in auth_check_password_send()
[Samba.git] / auth / kerberos / kerberos_pac.c
blob7b6efdc8db019d54789a75a5415c844bd33d6c82
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 switch (sig->type) {
43 case CKSUMTYPE_HMAC_MD5:
44 /* ignores the key type */
45 break;
46 case CKSUMTYPE_HMAC_SHA1_96_AES_256:
47 if (KRB5_KEY_TYPE(keyblock) != ENCTYPE_AES256_CTS_HMAC_SHA1_96) {
48 return EINVAL;
50 /* ok */
51 break;
52 case CKSUMTYPE_HMAC_SHA1_96_AES_128:
53 if (KRB5_KEY_TYPE(keyblock) != ENCTYPE_AES128_CTS_HMAC_SHA1_96) {
54 return EINVAL;
56 /* ok */
57 break;
58 default:
59 DEBUG(2,("check_pac_checksum: Checksum Type %d is not supported\n",
60 (int)sig->type));
61 return EINVAL;
64 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM /* Heimdal */
65 cksum.cksumtype = (krb5_cksumtype)sig->type;
66 cksum.checksum.length = sig->signature.length;
67 cksum.checksum.data = sig->signature.data;
68 #else /* MIT */
69 cksum.checksum_type = (krb5_cksumtype)sig->type;
70 cksum.length = sig->signature.length;
71 cksum.contents = sig->signature.data;
72 #endif
74 #ifdef HAVE_KRB5_KU_OTHER_CKSUM /* Heimdal */
75 usage = KRB5_KU_OTHER_CKSUM;
76 #elif defined(HAVE_KRB5_KEYUSAGE_APP_DATA_CKSUM) /* MIT */
77 usage = KRB5_KEYUSAGE_APP_DATA_CKSUM;
78 #else
79 #error UNKNOWN_KRB5_KEYUSAGE
80 #endif
82 input.data = (char *)pac_data.data;
83 input.length = pac_data.length;
85 ret = krb5_c_verify_checksum(context,
86 keyblock,
87 usage,
88 &input,
89 &cksum,
90 &checksum_valid);
91 if (!checksum_valid) {
92 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
94 if (ret){
95 DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
96 error_message(ret), ret));
97 return ret;
100 return ret;
104 * @brief Decode a blob containing a NDR encoded PAC structure
106 * @param mem_ctx - The memory context
107 * @param pac_data_blob - The data blob containing the NDR encoded data
108 * @param context - The Kerberos Context
109 * @param service_keyblock - The Service Key used to verify the checksum
110 * @param client_principal - The client principal
111 * @param tgs_authtime - The ticket timestamp
112 * @param pac_data_out - [out] The decoded PAC
114 * @return - A NTSTATUS error code
116 NTSTATUS kerberos_decode_pac(TALLOC_CTX *mem_ctx,
117 DATA_BLOB pac_data_blob,
118 krb5_context context,
119 const krb5_keyblock *krbtgt_keyblock,
120 const krb5_keyblock *service_keyblock,
121 krb5_const_principal client_principal,
122 time_t tgs_authtime,
123 struct PAC_DATA **pac_data_out)
125 NTSTATUS status;
126 enum ndr_err_code ndr_err;
127 krb5_error_code ret;
128 DATA_BLOB modified_pac_blob;
130 NTTIME tgs_authtime_nttime;
131 int i;
133 struct PAC_SIGNATURE_DATA *srv_sig_ptr = NULL;
134 struct PAC_SIGNATURE_DATA *kdc_sig_ptr = NULL;
135 struct PAC_SIGNATURE_DATA *srv_sig_wipe = NULL;
136 struct PAC_SIGNATURE_DATA *kdc_sig_wipe = NULL;
137 struct PAC_LOGON_NAME *logon_name = NULL;
138 struct PAC_LOGON_INFO *logon_info = NULL;
139 struct PAC_DATA *pac_data = NULL;
140 struct PAC_DATA_RAW *pac_data_raw = NULL;
142 DATA_BLOB *srv_sig_blob = NULL;
143 DATA_BLOB *kdc_sig_blob = NULL;
145 bool bool_ret;
147 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
148 if (!tmp_ctx) {
149 return NT_STATUS_NO_MEMORY;
152 if (pac_data_out) {
153 *pac_data_out = NULL;
156 pac_data = talloc(tmp_ctx, struct PAC_DATA);
157 pac_data_raw = talloc(tmp_ctx, struct PAC_DATA_RAW);
158 kdc_sig_wipe = talloc(tmp_ctx, struct PAC_SIGNATURE_DATA);
159 srv_sig_wipe = talloc(tmp_ctx, struct PAC_SIGNATURE_DATA);
160 if (!pac_data_raw || !pac_data || !kdc_sig_wipe || !srv_sig_wipe) {
161 talloc_free(tmp_ctx);
162 return NT_STATUS_NO_MEMORY;
165 ndr_err = ndr_pull_struct_blob(&pac_data_blob, pac_data, pac_data,
166 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
167 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
168 status = ndr_map_error2ntstatus(ndr_err);
169 DEBUG(0,("can't parse the PAC: %s\n",
170 nt_errstr(status)));
171 talloc_free(tmp_ctx);
172 return status;
175 if (pac_data->num_buffers < 4) {
176 /* we need logon_ingo, service_key and kdc_key */
177 DEBUG(0,("less than 4 PAC buffers\n"));
178 talloc_free(tmp_ctx);
179 return NT_STATUS_INVALID_PARAMETER;
182 ndr_err = ndr_pull_struct_blob(
183 &pac_data_blob, pac_data_raw, pac_data_raw,
184 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA_RAW);
185 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
186 status = ndr_map_error2ntstatus(ndr_err);
187 DEBUG(0,("can't parse the PAC: %s\n",
188 nt_errstr(status)));
189 talloc_free(tmp_ctx);
190 return status;
193 if (pac_data_raw->num_buffers < 4) {
194 /* we need logon_ingo, service_key and kdc_key */
195 DEBUG(0,("less than 4 PAC buffers\n"));
196 talloc_free(tmp_ctx);
197 return NT_STATUS_INVALID_PARAMETER;
200 if (pac_data->num_buffers != pac_data_raw->num_buffers) {
201 /* we need logon_ingo, service_key and kdc_key */
202 DEBUG(0, ("misparse! PAC_DATA has %d buffers while "
203 "PAC_DATA_RAW has %d\n", pac_data->num_buffers,
204 pac_data_raw->num_buffers));
205 talloc_free(tmp_ctx);
206 return NT_STATUS_INVALID_PARAMETER;
209 for (i=0; i < pac_data->num_buffers; i++) {
210 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
211 struct PAC_BUFFER_RAW *raw_buf = &pac_data_raw->buffers[i];
213 if (data_buf->type != raw_buf->type) {
214 DEBUG(0, ("misparse! PAC_DATA buffer %d has type "
215 "%d while PAC_DATA_RAW has %d\n", i,
216 data_buf->type, raw_buf->type));
217 talloc_free(tmp_ctx);
218 return NT_STATUS_INVALID_PARAMETER;
220 switch (data_buf->type) {
221 case PAC_TYPE_LOGON_INFO:
222 if (!data_buf->info) {
223 break;
225 logon_info = data_buf->info->logon_info.info;
226 break;
227 case PAC_TYPE_SRV_CHECKSUM:
228 if (!data_buf->info) {
229 break;
231 srv_sig_ptr = &data_buf->info->srv_cksum;
232 srv_sig_blob = &raw_buf->info->remaining;
233 break;
234 case PAC_TYPE_KDC_CHECKSUM:
235 if (!data_buf->info) {
236 break;
238 kdc_sig_ptr = &data_buf->info->kdc_cksum;
239 kdc_sig_blob = &raw_buf->info->remaining;
240 break;
241 case PAC_TYPE_LOGON_NAME:
242 logon_name = &data_buf->info->logon_name;
243 break;
244 default:
245 break;
249 if (!logon_info) {
250 DEBUG(0,("PAC no logon_info\n"));
251 talloc_free(tmp_ctx);
252 return NT_STATUS_INVALID_PARAMETER;
255 if (!logon_name) {
256 DEBUG(0,("PAC no logon_name\n"));
257 talloc_free(tmp_ctx);
258 return NT_STATUS_INVALID_PARAMETER;
261 if (!srv_sig_ptr || !srv_sig_blob) {
262 DEBUG(0,("PAC no srv_key\n"));
263 talloc_free(tmp_ctx);
264 return NT_STATUS_INVALID_PARAMETER;
267 if (!kdc_sig_ptr || !kdc_sig_blob) {
268 DEBUG(0,("PAC no kdc_key\n"));
269 talloc_free(tmp_ctx);
270 return NT_STATUS_INVALID_PARAMETER;
273 /* Find and zero out the signatures,
274 * as required by the signing algorithm */
276 /* We find the data blobs above,
277 * now we parse them to get at the exact portion we should zero */
278 ndr_err = ndr_pull_struct_blob(
279 kdc_sig_blob, kdc_sig_wipe, kdc_sig_wipe,
280 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
281 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
282 status = ndr_map_error2ntstatus(ndr_err);
283 DEBUG(0,("can't parse the KDC signature: %s\n",
284 nt_errstr(status)));
285 talloc_free(tmp_ctx);
286 return status;
289 ndr_err = ndr_pull_struct_blob(
290 srv_sig_blob, srv_sig_wipe, srv_sig_wipe,
291 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
292 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
293 status = ndr_map_error2ntstatus(ndr_err);
294 DEBUG(0,("can't parse the SRV signature: %s\n",
295 nt_errstr(status)));
296 talloc_free(tmp_ctx);
297 return status;
300 /* Now zero the decoded structure */
301 memset(kdc_sig_wipe->signature.data,
302 '\0', kdc_sig_wipe->signature.length);
303 memset(srv_sig_wipe->signature.data,
304 '\0', srv_sig_wipe->signature.length);
306 /* and reencode, back into the same place it came from */
307 ndr_err = ndr_push_struct_blob(
308 kdc_sig_blob, pac_data_raw, kdc_sig_wipe,
309 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
310 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
311 status = ndr_map_error2ntstatus(ndr_err);
312 DEBUG(0,("can't repack the KDC signature: %s\n",
313 nt_errstr(status)));
314 talloc_free(tmp_ctx);
315 return status;
317 ndr_err = ndr_push_struct_blob(
318 srv_sig_blob, pac_data_raw, srv_sig_wipe,
319 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
320 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
321 status = ndr_map_error2ntstatus(ndr_err);
322 DEBUG(0,("can't repack the SRV signature: %s\n",
323 nt_errstr(status)));
324 talloc_free(tmp_ctx);
325 return status;
328 /* push out the whole structure, but now with zero'ed signatures */
329 ndr_err = ndr_push_struct_blob(
330 &modified_pac_blob, pac_data_raw, pac_data_raw,
331 (ndr_push_flags_fn_t)ndr_push_PAC_DATA_RAW);
332 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
333 status = ndr_map_error2ntstatus(ndr_err);
334 DEBUG(0,("can't repack the RAW PAC: %s\n",
335 nt_errstr(status)));
336 talloc_free(tmp_ctx);
337 return status;
340 if (service_keyblock) {
341 /* verify by service_key */
342 ret = check_pac_checksum(modified_pac_blob, srv_sig_ptr,
343 context,
344 service_keyblock);
345 if (ret) {
346 DEBUG(5, ("PAC Decode: Failed to verify the service "
347 "signature: %s\n", error_message(ret)));
348 return NT_STATUS_ACCESS_DENIED;
351 if (krbtgt_keyblock) {
352 /* verify the service key checksum by krbtgt_key */
353 ret = check_pac_checksum(srv_sig_ptr->signature, kdc_sig_ptr,
354 context, krbtgt_keyblock);
355 if (ret) {
356 DEBUG(1, ("PAC Decode: Failed to verify the KDC signature: %s\n",
357 smb_get_krb5_error_message(context, ret, tmp_ctx)));
358 talloc_free(tmp_ctx);
359 return NT_STATUS_ACCESS_DENIED;
364 if (tgs_authtime) {
365 /* Convert to NT time, so as not to loose accuracy in comparison */
366 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
368 if (tgs_authtime_nttime != logon_name->logon_time) {
369 DEBUG(2, ("PAC Decode: "
370 "Logon time mismatch between ticket and PAC!\n"));
371 DEBUG(2, ("PAC Decode: PAC: %s\n",
372 nt_time_string(tmp_ctx, logon_name->logon_time)));
373 DEBUG(2, ("PAC Decode: Ticket: %s\n",
374 nt_time_string(tmp_ctx, tgs_authtime_nttime)));
375 talloc_free(tmp_ctx);
376 return NT_STATUS_ACCESS_DENIED;
380 if (client_principal) {
381 char *client_principal_string;
382 ret = krb5_unparse_name_flags(context, client_principal,
383 KRB5_PRINCIPAL_UNPARSE_NO_REALM|KRB5_PRINCIPAL_UNPARSE_DISPLAY,
384 &client_principal_string);
385 if (ret) {
386 DEBUG(2, ("Could not unparse name from ticket to match with name from PAC: [%s]:%s\n",
387 logon_name->account_name, error_message(ret)));
388 talloc_free(tmp_ctx);
389 return NT_STATUS_INVALID_PARAMETER;
392 bool_ret = strcmp(client_principal_string, logon_name->account_name) == 0;
394 if (!bool_ret) {
395 DEBUG(2, ("Name in PAC [%s] does not match principal name "
396 "in ticket [%s]\n",
397 logon_name->account_name,
398 client_principal_string));
399 SAFE_FREE(client_principal_string);
400 talloc_free(tmp_ctx);
401 return NT_STATUS_ACCESS_DENIED;
403 SAFE_FREE(client_principal_string);
407 DEBUG(3,("Found account name from PAC: %s [%s]\n",
408 logon_info->info3.base.account_name.string,
409 logon_info->info3.base.full_name.string));
411 DEBUG(10,("Successfully validated Kerberos PAC\n"));
413 if (DEBUGLEVEL >= 10) {
414 const char *s;
415 s = NDR_PRINT_STRUCT_STRING(tmp_ctx, PAC_DATA, pac_data);
416 if (s) {
417 DEBUGADD(10,("%s\n", s));
421 if (pac_data_out) {
422 *pac_data_out = talloc_steal(mem_ctx, pac_data);
425 return NT_STATUS_OK;
428 NTSTATUS kerberos_pac_logon_info(TALLOC_CTX *mem_ctx,
429 DATA_BLOB blob,
430 krb5_context context,
431 const krb5_keyblock *krbtgt_keyblock,
432 const krb5_keyblock *service_keyblock,
433 krb5_const_principal client_principal,
434 time_t tgs_authtime,
435 struct PAC_LOGON_INFO **logon_info)
437 NTSTATUS nt_status;
438 struct PAC_DATA *pac_data;
439 int i;
440 nt_status = kerberos_decode_pac(mem_ctx,
441 blob,
442 context,
443 krbtgt_keyblock,
444 service_keyblock,
445 client_principal,
446 tgs_authtime,
447 &pac_data);
448 if (!NT_STATUS_IS_OK(nt_status)) {
449 return nt_status;
452 *logon_info = NULL;
453 for (i=0; i < pac_data->num_buffers; i++) {
454 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
455 continue;
457 *logon_info = pac_data->buffers[i].info->logon_info.info;
459 if (!*logon_info) {
460 return NT_STATUS_INVALID_PARAMETER;
462 return NT_STATUS_OK;
465 #endif