auth/kerberos Move all the PAC handling functions to auth/kerberos
[samba.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 "libcli/auth/krb5_wrap.h"
30
31 krb5_error_code check_pac_checksum(TALLOC_CTX *mem_ctx,
32                                           DATA_BLOB pac_data,
33                                           struct PAC_SIGNATURE_DATA *sig,
34                                           krb5_context context,
35                                           const krb5_keyblock *keyblock)
36 {
37         krb5_error_code ret;
38         krb5_checksum cksum;
39         krb5_keyusage usage = 0;
40
41         smb_krb5_checksum_from_pac_sig(&cksum, sig);
42
43 #ifdef HAVE_KRB5_KU_OTHER_CKSUM /* Heimdal */
44         usage = KRB5_KU_OTHER_CKSUM;
45 #elif defined(HAVE_KRB5_KEYUSAGE_APP_DATA_CKSUM) /* MIT */
46         usage = KRB5_KEYUSAGE_APP_DATA_CKSUM;
47 #else
48 #error UNKNOWN_KRB5_KEYUSAGE
49 #endif
50
51         ret = smb_krb5_verify_checksum(context,
52                                        keyblock,
53                                        usage,
54                                        &cksum,
55                                        pac_data.data,
56                                        pac_data.length);
57
58         if (ret) {
59                 DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
60                         error_message(ret), ret));
61                 return ret;
62         }
63
64         return ret;
65 }
66
67 /**
68 * @brief Decode a blob containing a NDR envoded PAC structure
69 *
70 * @param mem_ctx          - The memory context
71 * @param pac_data_blob    - The data blob containing the NDR encoded data
72 * @param context          - The Kerberos Context
73 * @param service_keyblock - The Service Key used to verify the checksum
74 * @param client_principal - The client principal
75 * @param tgs_authtime     - The ticket timestamp
76 * @param pac_data_out     - [out] The decoded PAC
77 *
78 * @return - A NTSTATUS error code
79 */
80 NTSTATUS kerberos_decode_pac(TALLOC_CTX *mem_ctx,
81                              DATA_BLOB pac_data_blob,
82                              krb5_context context,
83                              const krb5_keyblock *krbtgt_keyblock,
84                              const krb5_keyblock *service_keyblock,
85                              krb5_const_principal client_principal,
86                              time_t tgs_authtime,
87                              struct PAC_DATA **pac_data_out)
88 {
89         NTSTATUS status;
90         enum ndr_err_code ndr_err;
91         krb5_error_code ret;
92         DATA_BLOB modified_pac_blob;
93
94         NTTIME tgs_authtime_nttime;
95         krb5_principal client_principal_pac = NULL;
96         int i;
97
98         struct PAC_SIGNATURE_DATA *srv_sig_ptr = NULL;
99         struct PAC_SIGNATURE_DATA *kdc_sig_ptr = NULL;
100         struct PAC_SIGNATURE_DATA *srv_sig_wipe = NULL;
101         struct PAC_SIGNATURE_DATA *kdc_sig_wipe = NULL;
102         struct PAC_LOGON_NAME *logon_name = NULL;
103         struct PAC_LOGON_INFO *logon_info = NULL;
104         struct PAC_DATA *pac_data = NULL;
105         struct PAC_DATA_RAW *pac_data_raw = NULL;
106
107         DATA_BLOB *srv_sig_blob = NULL;
108         DATA_BLOB *kdc_sig_blob = NULL;
109
110         bool bool_ret;
111
112         *pac_data_out = NULL;
113
114         pac_data = talloc(mem_ctx, struct PAC_DATA);
115         pac_data_raw = talloc(mem_ctx, struct PAC_DATA_RAW);
116         kdc_sig_wipe = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
117         srv_sig_wipe = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
118         if (!pac_data_raw || !pac_data || !kdc_sig_wipe || !srv_sig_wipe) {
119                 return NT_STATUS_NO_MEMORY;
120         }
121
122         ndr_err = ndr_pull_struct_blob(&pac_data_blob, pac_data, pac_data,
123                        (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
124         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
125                 status = ndr_map_error2ntstatus(ndr_err);
126                 DEBUG(0,("can't parse the PAC: %s\n",
127                         nt_errstr(status)));
128                 return status;
129         }
130
131         if (pac_data->num_buffers < 4) {
132                 /* we need logon_ingo, service_key and kdc_key */
133                 DEBUG(0,("less than 4 PAC buffers\n"));
134                 return NT_STATUS_INVALID_PARAMETER;
135         }
136
137         ndr_err = ndr_pull_struct_blob(
138                                 &pac_data_blob, pac_data_raw, pac_data_raw,
139                                 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA_RAW);
140         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
141                 status = ndr_map_error2ntstatus(ndr_err);
142                 DEBUG(0,("can't parse the PAC: %s\n",
143                         nt_errstr(status)));
144                 return status;
145         }
146
147         if (pac_data_raw->num_buffers < 4) {
148                 /* we need logon_ingo, service_key and kdc_key */
149                 DEBUG(0,("less than 4 PAC buffers\n"));
150                 return NT_STATUS_INVALID_PARAMETER;
151         }
152
153         if (pac_data->num_buffers != pac_data_raw->num_buffers) {
154                 /* we need logon_ingo, service_key and kdc_key */
155                 DEBUG(0, ("misparse! PAC_DATA has %d buffers while "
156                           "PAC_DATA_RAW has %d\n", pac_data->num_buffers,
157                           pac_data_raw->num_buffers));
158                 return NT_STATUS_INVALID_PARAMETER;
159         }
160
161         for (i=0; i < pac_data->num_buffers; i++) {
162                 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
163                 struct PAC_BUFFER_RAW *raw_buf = &pac_data_raw->buffers[i];
164
165                 if (data_buf->type != raw_buf->type) {
166                         DEBUG(0, ("misparse! PAC_DATA buffer %d has type "
167                                   "%d while PAC_DATA_RAW has %d\n", i,
168                                   data_buf->type, raw_buf->type));
169                         return NT_STATUS_INVALID_PARAMETER;
170                 }
171                 switch (data_buf->type) {
172                 case PAC_TYPE_LOGON_INFO:
173                         if (!data_buf->info) {
174                                 break;
175                         }
176                         logon_info = data_buf->info->logon_info.info;
177                         break;
178                 case PAC_TYPE_SRV_CHECKSUM:
179                         if (!data_buf->info) {
180                                 break;
181                         }
182                         srv_sig_ptr = &data_buf->info->srv_cksum;
183                         srv_sig_blob = &raw_buf->info->remaining;
184                         break;
185                 case PAC_TYPE_KDC_CHECKSUM:
186                         if (!data_buf->info) {
187                                 break;
188                         }
189                         kdc_sig_ptr = &data_buf->info->kdc_cksum;
190                         kdc_sig_blob = &raw_buf->info->remaining;
191                         break;
192                 case PAC_TYPE_LOGON_NAME:
193                         logon_name = &data_buf->info->logon_name;
194                         break;
195                 default:
196                         break;
197                 }
198         }
199
200         if (!logon_info) {
201                 DEBUG(0,("PAC no logon_info\n"));
202                 return NT_STATUS_INVALID_PARAMETER;
203         }
204
205         if (!logon_name) {
206                 DEBUG(0,("PAC no logon_name\n"));
207                 return NT_STATUS_INVALID_PARAMETER;
208         }
209
210         if (!srv_sig_ptr || !srv_sig_blob) {
211                 DEBUG(0,("PAC no srv_key\n"));
212                 return NT_STATUS_INVALID_PARAMETER;
213         }
214
215         if (!kdc_sig_ptr || !kdc_sig_blob) {
216                 DEBUG(0,("PAC no kdc_key\n"));
217                 return NT_STATUS_INVALID_PARAMETER;
218         }
219
220         /* Find and zero out the signatures,
221          * as required by the signing algorithm */
222
223         /* We find the data blobs above,
224          * now we parse them to get at the exact portion we should zero */
225         ndr_err = ndr_pull_struct_blob(
226                         kdc_sig_blob, kdc_sig_wipe, kdc_sig_wipe,
227                         (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
228         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
229                 status = ndr_map_error2ntstatus(ndr_err);
230                 DEBUG(0,("can't parse the KDC signature: %s\n",
231                         nt_errstr(status)));
232                 return status;
233         }
234
235         ndr_err = ndr_pull_struct_blob(
236                         srv_sig_blob, srv_sig_wipe, srv_sig_wipe,
237                         (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
238         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
239                 status = ndr_map_error2ntstatus(ndr_err);
240                 DEBUG(0,("can't parse the SRV signature: %s\n",
241                         nt_errstr(status)));
242                 return status;
243         }
244
245         /* Now zero the decoded structure */
246         memset(kdc_sig_wipe->signature.data,
247                 '\0', kdc_sig_wipe->signature.length);
248         memset(srv_sig_wipe->signature.data,
249                 '\0', srv_sig_wipe->signature.length);
250
251         /* and reencode, back into the same place it came from */
252         ndr_err = ndr_push_struct_blob(
253                         kdc_sig_blob, pac_data_raw, kdc_sig_wipe,
254                         (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
255         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
256                 status = ndr_map_error2ntstatus(ndr_err);
257                 DEBUG(0,("can't repack the KDC signature: %s\n",
258                         nt_errstr(status)));
259                 return status;
260         }
261         ndr_err = ndr_push_struct_blob(
262                         srv_sig_blob, pac_data_raw, srv_sig_wipe,
263                         (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
264         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
265                 status = ndr_map_error2ntstatus(ndr_err);
266                 DEBUG(0,("can't repack the SRV signature: %s\n",
267                         nt_errstr(status)));
268                 return status;
269         }
270
271         /* push out the whole structure, but now with zero'ed signatures */
272         ndr_err = ndr_push_struct_blob(
273                         &modified_pac_blob, pac_data_raw, pac_data_raw,
274                         (ndr_push_flags_fn_t)ndr_push_PAC_DATA_RAW);
275         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
276                 status = ndr_map_error2ntstatus(ndr_err);
277                 DEBUG(0,("can't repack the RAW PAC: %s\n",
278                         nt_errstr(status)));
279                 return status;
280         }
281
282         if (service_keyblock) {
283                 /* verify by service_key */
284                 ret = check_pac_checksum(mem_ctx,
285                                          modified_pac_blob, srv_sig_ptr,
286                                          context,
287                                          service_keyblock);
288                 if (ret) {
289                         DEBUG(1, ("PAC Decode: Failed to verify the service "
290                                   "signature: %s\n", error_message(ret)));
291                         return NT_STATUS_ACCESS_DENIED;
292                 }
293
294                 if (krbtgt_keyblock) {
295                         /* verify the service key checksum by krbtgt_key */
296                         ret = check_pac_checksum(mem_ctx,
297                                                  srv_sig_ptr->signature, kdc_sig_ptr,
298                                                  context, krbtgt_keyblock);
299                         if (ret) {
300                                 DEBUG(1, ("PAC Decode: Failed to verify the KDC signature: %s\n",
301                                           smb_get_krb5_error_message(context, ret, mem_ctx)));
302                                 return NT_STATUS_ACCESS_DENIED;
303                         }
304                 }
305         }
306
307         if (tgs_authtime) {
308                 /* Convert to NT time, so as not to loose accuracy in comparison */
309                 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
310
311                 if (tgs_authtime_nttime != logon_name->logon_time) {
312                         DEBUG(2, ("PAC Decode: "
313                                   "Logon time mismatch between ticket and PAC!\n"));
314                         DEBUG(2, ("PAC Decode: PAC: %s\n",
315                                   nt_time_string(mem_ctx, logon_name->logon_time)));
316                         DEBUG(2, ("PAC Decode: Ticket: %s\n",
317                                   nt_time_string(mem_ctx, tgs_authtime_nttime)));
318                         return NT_STATUS_ACCESS_DENIED;
319                 }
320         }
321
322         if (client_principal) {
323                 ret = smb_krb5_parse_name_norealm(context,
324                                                   logon_name->account_name,
325                                                   &client_principal_pac);
326                 if (ret) {
327                         DEBUG(2, ("Could not parse name from PAC: [%s]:%s\n",
328                                   logon_name->account_name, error_message(ret)));
329                         return NT_STATUS_INVALID_PARAMETER;
330                 }
331
332                 bool_ret = smb_krb5_principal_compare_any_realm(context,
333                                                                 client_principal,
334                                                                 client_principal_pac);
335
336                 krb5_free_principal(context, client_principal_pac);
337
338                 if (!bool_ret) {
339                         DEBUG(2, ("Name in PAC [%s] does not match principal name "
340                                   "in ticket\n", logon_name->account_name));
341                         return NT_STATUS_ACCESS_DENIED;
342                 }
343         }
344
345         DEBUG(3,("Found account name from PAC: %s [%s]\n",
346                  logon_info->info3.base.account_name.string,
347                  logon_info->info3.base.full_name.string));
348
349         DEBUG(10,("Successfully validated Kerberos PAC\n"));
350
351         if (DEBUGLEVEL >= 10) {
352                 const char *s;
353                 s = NDR_PRINT_STRUCT_STRING(mem_ctx, PAC_DATA, pac_data);
354                 if (s) {
355                         DEBUGADD(10,("%s\n", s));
356                 }
357         }
358
359         *pac_data_out = pac_data;
360
361         return NT_STATUS_OK;
362 }
363
364 #endif