nwrap: catch error to add item to vector in nwrap_he_parse_line()
[obnox/samba/samba-obnox.git] / auth / kerberos / kerberos_pac.c
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
10
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.
15
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.
20
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/>.
23 */
24
25 #include "includes.h"
26 #ifdef HAVE_KRB5
27
28 #include "librpc/gen_ndr/ndr_krb5pac.h"
29 #include "auth/kerberos/pac_utils.h"
30
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)
35 {
36         krb5_error_code ret;
37         krb5_checksum cksum;
38         krb5_keyusage usage = 0;
39         krb5_boolean checksum_valid = false;
40         krb5_data input;
41
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
51
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
59
60         input.data = (char *)pac_data.data;
61         input.length = pac_data.length;
62
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;
71         }
72         if (ret){
73                 DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
74                         error_message(ret), ret));
75                 return ret;
76         }
77
78         return ret;
79 }
80
81 /**
82 * @brief Decode a blob containing a NDR encoded PAC structure
83 *
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
91 *
92 * @return - A NTSTATUS error code
93 */
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)
102 {
103         NTSTATUS status;
104         enum ndr_err_code ndr_err;
105         krb5_error_code ret;
106         DATA_BLOB modified_pac_blob;
107
108         NTTIME tgs_authtime_nttime;
109         int i;
110
111         struct PAC_SIGNATURE_DATA *srv_sig_ptr = NULL;
112         struct PAC_SIGNATURE_DATA *kdc_sig_ptr = NULL;
113         struct PAC_SIGNATURE_DATA *srv_sig_wipe = NULL;
114         struct PAC_SIGNATURE_DATA *kdc_sig_wipe = NULL;
115         struct PAC_LOGON_NAME *logon_name = NULL;
116         struct PAC_LOGON_INFO *logon_info = NULL;
117         struct PAC_DATA *pac_data = NULL;
118         struct PAC_DATA_RAW *pac_data_raw = NULL;
119
120         DATA_BLOB *srv_sig_blob = NULL;
121         DATA_BLOB *kdc_sig_blob = NULL;
122
123         bool bool_ret;
124
125         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
126         if (!tmp_ctx) {
127                 return NT_STATUS_NO_MEMORY;
128         }
129
130         if (pac_data_out) {
131                 *pac_data_out = NULL;
132         }
133
134         pac_data = talloc(tmp_ctx, struct PAC_DATA);
135         pac_data_raw = talloc(tmp_ctx, struct PAC_DATA_RAW);
136         kdc_sig_wipe = talloc(tmp_ctx, struct PAC_SIGNATURE_DATA);
137         srv_sig_wipe = talloc(tmp_ctx, struct PAC_SIGNATURE_DATA);
138         if (!pac_data_raw || !pac_data || !kdc_sig_wipe || !srv_sig_wipe) {
139                 talloc_free(tmp_ctx);
140                 return NT_STATUS_NO_MEMORY;
141         }
142
143         ndr_err = ndr_pull_struct_blob(&pac_data_blob, pac_data, pac_data,
144                        (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
145         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
146                 status = ndr_map_error2ntstatus(ndr_err);
147                 DEBUG(0,("can't parse the PAC: %s\n",
148                         nt_errstr(status)));
149                 talloc_free(tmp_ctx);
150                 return status;
151         }
152
153         if (pac_data->num_buffers < 4) {
154                 /* we need logon_ingo, service_key and kdc_key */
155                 DEBUG(0,("less than 4 PAC buffers\n"));
156                 talloc_free(tmp_ctx);
157                 return NT_STATUS_INVALID_PARAMETER;
158         }
159
160         ndr_err = ndr_pull_struct_blob(
161                                 &pac_data_blob, pac_data_raw, pac_data_raw,
162                                 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA_RAW);
163         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
164                 status = ndr_map_error2ntstatus(ndr_err);
165                 DEBUG(0,("can't parse the PAC: %s\n",
166                         nt_errstr(status)));
167                 talloc_free(tmp_ctx);
168                 return status;
169         }
170
171         if (pac_data_raw->num_buffers < 4) {
172                 /* we need logon_ingo, service_key and kdc_key */
173                 DEBUG(0,("less than 4 PAC buffers\n"));
174                 talloc_free(tmp_ctx);
175                 return NT_STATUS_INVALID_PARAMETER;
176         }
177
178         if (pac_data->num_buffers != pac_data_raw->num_buffers) {
179                 /* we need logon_ingo, service_key and kdc_key */
180                 DEBUG(0, ("misparse! PAC_DATA has %d buffers while "
181                           "PAC_DATA_RAW has %d\n", pac_data->num_buffers,
182                           pac_data_raw->num_buffers));
183                 talloc_free(tmp_ctx);
184                 return NT_STATUS_INVALID_PARAMETER;
185         }
186
187         for (i=0; i < pac_data->num_buffers; i++) {
188                 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
189                 struct PAC_BUFFER_RAW *raw_buf = &pac_data_raw->buffers[i];
190
191                 if (data_buf->type != raw_buf->type) {
192                         DEBUG(0, ("misparse! PAC_DATA buffer %d has type "
193                                   "%d while PAC_DATA_RAW has %d\n", i,
194                                   data_buf->type, raw_buf->type));
195                         talloc_free(tmp_ctx);
196                         return NT_STATUS_INVALID_PARAMETER;
197                 }
198                 switch (data_buf->type) {
199                 case PAC_TYPE_LOGON_INFO:
200                         if (!data_buf->info) {
201                                 break;
202                         }
203                         logon_info = data_buf->info->logon_info.info;
204                         break;
205                 case PAC_TYPE_SRV_CHECKSUM:
206                         if (!data_buf->info) {
207                                 break;
208                         }
209                         srv_sig_ptr = &data_buf->info->srv_cksum;
210                         srv_sig_blob = &raw_buf->info->remaining;
211                         break;
212                 case PAC_TYPE_KDC_CHECKSUM:
213                         if (!data_buf->info) {
214                                 break;
215                         }
216                         kdc_sig_ptr = &data_buf->info->kdc_cksum;
217                         kdc_sig_blob = &raw_buf->info->remaining;
218                         break;
219                 case PAC_TYPE_LOGON_NAME:
220                         logon_name = &data_buf->info->logon_name;
221                         break;
222                 default:
223                         break;
224                 }
225         }
226
227         if (!logon_info) {
228                 DEBUG(0,("PAC no logon_info\n"));
229                 talloc_free(tmp_ctx);
230                 return NT_STATUS_INVALID_PARAMETER;
231         }
232
233         if (!logon_name) {
234                 DEBUG(0,("PAC no logon_name\n"));
235                 talloc_free(tmp_ctx);
236                 return NT_STATUS_INVALID_PARAMETER;
237         }
238
239         if (!srv_sig_ptr || !srv_sig_blob) {
240                 DEBUG(0,("PAC no srv_key\n"));
241                 talloc_free(tmp_ctx);
242                 return NT_STATUS_INVALID_PARAMETER;
243         }
244
245         if (!kdc_sig_ptr || !kdc_sig_blob) {
246                 DEBUG(0,("PAC no kdc_key\n"));
247                 talloc_free(tmp_ctx);
248                 return NT_STATUS_INVALID_PARAMETER;
249         }
250
251         /* Find and zero out the signatures,
252          * as required by the signing algorithm */
253
254         /* We find the data blobs above,
255          * now we parse them to get at the exact portion we should zero */
256         ndr_err = ndr_pull_struct_blob(
257                         kdc_sig_blob, kdc_sig_wipe, kdc_sig_wipe,
258                         (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
259         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
260                 status = ndr_map_error2ntstatus(ndr_err);
261                 DEBUG(0,("can't parse the KDC signature: %s\n",
262                         nt_errstr(status)));
263                 talloc_free(tmp_ctx);
264                 return status;
265         }
266
267         ndr_err = ndr_pull_struct_blob(
268                         srv_sig_blob, srv_sig_wipe, srv_sig_wipe,
269                         (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
270         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
271                 status = ndr_map_error2ntstatus(ndr_err);
272                 DEBUG(0,("can't parse the SRV signature: %s\n",
273                         nt_errstr(status)));
274                 talloc_free(tmp_ctx);
275                 return status;
276         }
277
278         /* Now zero the decoded structure */
279         memset(kdc_sig_wipe->signature.data,
280                 '\0', kdc_sig_wipe->signature.length);
281         memset(srv_sig_wipe->signature.data,
282                 '\0', srv_sig_wipe->signature.length);
283
284         /* and reencode, back into the same place it came from */
285         ndr_err = ndr_push_struct_blob(
286                         kdc_sig_blob, pac_data_raw, kdc_sig_wipe,
287                         (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
288         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
289                 status = ndr_map_error2ntstatus(ndr_err);
290                 DEBUG(0,("can't repack the KDC signature: %s\n",
291                         nt_errstr(status)));
292                 talloc_free(tmp_ctx);
293                 return status;
294         }
295         ndr_err = ndr_push_struct_blob(
296                         srv_sig_blob, pac_data_raw, srv_sig_wipe,
297                         (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
298         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
299                 status = ndr_map_error2ntstatus(ndr_err);
300                 DEBUG(0,("can't repack the SRV signature: %s\n",
301                         nt_errstr(status)));
302                 talloc_free(tmp_ctx);
303                 return status;
304         }
305
306         /* push out the whole structure, but now with zero'ed signatures */
307         ndr_err = ndr_push_struct_blob(
308                         &modified_pac_blob, pac_data_raw, pac_data_raw,
309                         (ndr_push_flags_fn_t)ndr_push_PAC_DATA_RAW);
310         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
311                 status = ndr_map_error2ntstatus(ndr_err);
312                 DEBUG(0,("can't repack the RAW PAC: %s\n",
313                         nt_errstr(status)));
314                 talloc_free(tmp_ctx);
315                 return status;
316         }
317
318         if (service_keyblock) {
319                 /* verify by service_key */
320                 ret = check_pac_checksum(modified_pac_blob, srv_sig_ptr,
321                                          context,
322                                          service_keyblock);
323                 if (ret) {
324                         DEBUG(5, ("PAC Decode: Failed to verify the service "
325                                   "signature: %s\n", error_message(ret)));
326                         return NT_STATUS_ACCESS_DENIED;
327                 }
328
329                 if (krbtgt_keyblock) {
330                         /* verify the service key checksum by krbtgt_key */
331                         ret = check_pac_checksum(srv_sig_ptr->signature, kdc_sig_ptr,
332                                                  context, krbtgt_keyblock);
333                         if (ret) {
334                                 DEBUG(1, ("PAC Decode: Failed to verify the KDC signature: %s\n",
335                                           smb_get_krb5_error_message(context, ret, tmp_ctx)));
336                                 talloc_free(tmp_ctx);
337                                 return NT_STATUS_ACCESS_DENIED;
338                         }
339                 }
340         }
341
342         if (tgs_authtime) {
343                 /* Convert to NT time, so as not to loose accuracy in comparison */
344                 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
345
346                 if (tgs_authtime_nttime != logon_name->logon_time) {
347                         DEBUG(2, ("PAC Decode: "
348                                   "Logon time mismatch between ticket and PAC!\n"));
349                         DEBUG(2, ("PAC Decode: PAC: %s\n",
350                                   nt_time_string(tmp_ctx, logon_name->logon_time)));
351                         DEBUG(2, ("PAC Decode: Ticket: %s\n",
352                                   nt_time_string(tmp_ctx, tgs_authtime_nttime)));
353                         talloc_free(tmp_ctx);
354                         return NT_STATUS_ACCESS_DENIED;
355                 }
356         }
357
358         if (client_principal) {
359                 char *client_principal_string;
360                 ret = krb5_unparse_name_flags(context, client_principal,
361                                               KRB5_PRINCIPAL_UNPARSE_NO_REALM|KRB5_PRINCIPAL_UNPARSE_DISPLAY,
362                                               &client_principal_string);
363                 if (ret) {
364                         DEBUG(2, ("Could not unparse name from ticket to match with 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;
368                 }
369
370                 bool_ret = strcmp(client_principal_string, logon_name->account_name) == 0;
371
372                 if (!bool_ret) {
373                         DEBUG(2, ("Name in PAC [%s] does not match principal name "
374                                   "in ticket [%s]\n",
375                                   logon_name->account_name,
376                                   client_principal_string));
377                         SAFE_FREE(client_principal_string);
378                         talloc_free(tmp_ctx);
379                         return NT_STATUS_ACCESS_DENIED;
380                 }
381                 SAFE_FREE(client_principal_string);
382
383         }
384
385         DEBUG(3,("Found account name from PAC: %s [%s]\n",
386                  logon_info->info3.base.account_name.string,
387                  logon_info->info3.base.full_name.string));
388
389         DEBUG(10,("Successfully validated Kerberos PAC\n"));
390
391         if (DEBUGLEVEL >= 10) {
392                 const char *s;
393                 s = NDR_PRINT_STRUCT_STRING(tmp_ctx, PAC_DATA, pac_data);
394                 if (s) {
395                         DEBUGADD(10,("%s\n", s));
396                 }
397         }
398
399         if (pac_data_out) {
400                 *pac_data_out = talloc_steal(mem_ctx, pac_data);
401         }
402
403         return NT_STATUS_OK;
404 }
405
406 NTSTATUS kerberos_pac_logon_info(TALLOC_CTX *mem_ctx,
407                                  DATA_BLOB blob,
408                                  krb5_context context,
409                                  const krb5_keyblock *krbtgt_keyblock,
410                                  const krb5_keyblock *service_keyblock,
411                                  krb5_const_principal client_principal,
412                                  time_t tgs_authtime,
413                                  struct PAC_LOGON_INFO **logon_info)
414 {
415         NTSTATUS nt_status;
416         struct PAC_DATA *pac_data;
417         int i;
418         nt_status = kerberos_decode_pac(mem_ctx,
419                                         blob,
420                                         context,
421                                         krbtgt_keyblock,
422                                         service_keyblock,
423                                         client_principal,
424                                         tgs_authtime,
425                                         &pac_data);
426         if (!NT_STATUS_IS_OK(nt_status)) {
427                 return nt_status;
428         }
429
430         *logon_info = NULL;
431         for (i=0; i < pac_data->num_buffers; i++) {
432                 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
433                         continue;
434                 }
435                 *logon_info = pac_data->buffers[i].info->logon_info.info;
436         }
437         if (!*logon_info) {
438                 return NT_STATUS_INVALID_PARAMETER;
439         }
440         return NT_STATUS_OK;
441 }
442
443 #endif