s4-auth: Use smb_krb5_make_pac_checksum.
[Samba/gebeck_regimport.git] / source4 / auth / kerberos / kerberos_pac.c
blob85b62632bac3770a337da23c904d5bfb22604bee
1 /*
2 Unix SMB/CIFS implementation.
4 Create and parse the krb5 PAC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005,2008
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 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.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "system/kerberos.h"
28 #include "auth/auth.h"
29 #include "auth/kerberos/kerberos.h"
30 #include "librpc/gen_ndr/ndr_krb5pac.h"
31 #include <ldb.h>
32 #include "auth/auth_sam_reply.h"
33 #include "auth/kerberos/kerberos_util.h"
34 #include "auth/kerberos/pac_utils.h"
36 _PUBLIC_ NTSTATUS kerberos_pac_logon_info(TALLOC_CTX *mem_ctx,
37 DATA_BLOB blob,
38 krb5_context context,
39 const krb5_keyblock *krbtgt_keyblock,
40 const krb5_keyblock *service_keyblock,
41 krb5_const_principal client_principal,
42 time_t tgs_authtime,
43 struct PAC_LOGON_INFO **logon_info)
45 NTSTATUS nt_status;
46 struct PAC_DATA *pac_data;
47 int i;
48 nt_status = kerberos_decode_pac(mem_ctx,
49 blob,
50 context,
51 krbtgt_keyblock,
52 service_keyblock,
53 client_principal,
54 tgs_authtime,
55 &pac_data);
56 if (!NT_STATUS_IS_OK(nt_status)) {
57 return nt_status;
60 *logon_info = NULL;
61 for (i=0; i < pac_data->num_buffers; i++) {
62 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
63 continue;
65 *logon_info = pac_data->buffers[i].info->logon_info.info;
67 if (!*logon_info) {
68 return NT_STATUS_INVALID_PARAMETER;
70 return NT_STATUS_OK;
73 krb5_error_code kerberos_encode_pac(TALLOC_CTX *mem_ctx,
74 struct PAC_DATA *pac_data,
75 krb5_context context,
76 const krb5_keyblock *krbtgt_keyblock,
77 const krb5_keyblock *service_keyblock,
78 DATA_BLOB *pac)
80 NTSTATUS nt_status;
81 krb5_error_code ret;
82 enum ndr_err_code ndr_err;
83 DATA_BLOB zero_blob = data_blob(NULL, 0);
84 DATA_BLOB tmp_blob = data_blob(NULL, 0);
85 struct PAC_SIGNATURE_DATA *kdc_checksum = NULL;
86 struct PAC_SIGNATURE_DATA *srv_checksum = NULL;
87 int i;
89 /* First, just get the keytypes filled in (and lengths right, eventually) */
90 for (i=0; i < pac_data->num_buffers; i++) {
91 if (pac_data->buffers[i].type != PAC_TYPE_KDC_CHECKSUM) {
92 continue;
94 kdc_checksum = &pac_data->buffers[i].info->kdc_cksum,
95 ret = smb_krb5_make_pac_checksum(mem_ctx,
96 &zero_blob,
97 context,
98 krbtgt_keyblock,
99 &kdc_checksum->type,
100 &kdc_checksum->signature);
101 if (ret) {
102 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
103 smb_get_krb5_error_message(context, ret, mem_ctx)));
104 talloc_free(pac_data);
105 return ret;
109 for (i=0; i < pac_data->num_buffers; i++) {
110 if (pac_data->buffers[i].type != PAC_TYPE_SRV_CHECKSUM) {
111 continue;
113 srv_checksum = &pac_data->buffers[i].info->srv_cksum;
114 ret = smb_krb5_make_pac_checksum(mem_ctx,
115 &zero_blob,
116 context,
117 service_keyblock,
118 &srv_checksum->type,
119 &srv_checksum->signature);
120 if (ret) {
121 DEBUG(2, ("making service PAC checksum failed: %s\n",
122 smb_get_krb5_error_message(context, ret, mem_ctx)));
123 talloc_free(pac_data);
124 return ret;
128 if (!kdc_checksum) {
129 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!"));
130 return EINVAL;
132 if (!srv_checksum) {
133 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!"));
134 return EINVAL;
137 /* But wipe out the actual signatures */
138 memset(kdc_checksum->signature.data, '\0', kdc_checksum->signature.length);
139 memset(srv_checksum->signature.data, '\0', srv_checksum->signature.length);
141 ndr_err = ndr_push_struct_blob(&tmp_blob, mem_ctx,
142 pac_data,
143 (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
144 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
145 nt_status = ndr_map_error2ntstatus(ndr_err);
146 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status)));
147 talloc_free(pac_data);
148 return EINVAL;
151 /* Then sign the result of the previous push, where the sig was zero'ed out */
152 ret = smb_krb5_make_pac_checksum(mem_ctx,
153 &tmp_blob,
154 context,
155 service_keyblock,
156 &srv_checksum->type,
157 &srv_checksum->signature);
159 /* Then sign Server checksum */
160 ret = smb_krb5_make_pac_checksum(mem_ctx,
161 &srv_checksum->signature,
162 context,
163 krbtgt_keyblock,
164 &kdc_checksum->type,
165 &kdc_checksum->signature);
166 if (ret) {
167 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
168 smb_get_krb5_error_message(context, ret, mem_ctx)));
169 talloc_free(pac_data);
170 return ret;
173 /* And push it out again, this time to the world. This relies on determanistic pointer values */
174 ndr_err = ndr_push_struct_blob(&tmp_blob, mem_ctx,
175 pac_data,
176 (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
177 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
178 nt_status = ndr_map_error2ntstatus(ndr_err);
179 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status)));
180 talloc_free(pac_data);
181 return EINVAL;
184 *pac = tmp_blob;
186 return ret;
190 krb5_error_code kerberos_create_pac(TALLOC_CTX *mem_ctx,
191 struct auth_user_info_dc *user_info_dc,
192 krb5_context context,
193 const krb5_keyblock *krbtgt_keyblock,
194 const krb5_keyblock *service_keyblock,
195 krb5_principal client_principal,
196 time_t tgs_authtime,
197 DATA_BLOB *pac)
199 NTSTATUS nt_status;
200 krb5_error_code ret;
201 struct PAC_DATA *pac_data = talloc(mem_ctx, struct PAC_DATA);
202 struct netr_SamInfo3 *sam3;
203 union PAC_INFO *u_LOGON_INFO;
204 struct PAC_LOGON_INFO *LOGON_INFO;
205 union PAC_INFO *u_LOGON_NAME;
206 struct PAC_LOGON_NAME *LOGON_NAME;
207 union PAC_INFO *u_KDC_CHECKSUM;
208 union PAC_INFO *u_SRV_CHECKSUM;
210 char *name;
212 enum {
213 PAC_BUF_LOGON_INFO = 0,
214 PAC_BUF_LOGON_NAME = 1,
215 PAC_BUF_SRV_CHECKSUM = 2,
216 PAC_BUF_KDC_CHECKSUM = 3,
217 PAC_BUF_NUM_BUFFERS = 4
220 if (!pac_data) {
221 return ENOMEM;
224 pac_data->num_buffers = PAC_BUF_NUM_BUFFERS;
225 pac_data->version = 0;
227 pac_data->buffers = talloc_array(pac_data,
228 struct PAC_BUFFER,
229 pac_data->num_buffers);
230 if (!pac_data->buffers) {
231 talloc_free(pac_data);
232 return ENOMEM;
235 /* LOGON_INFO */
236 u_LOGON_INFO = talloc_zero(pac_data->buffers, union PAC_INFO);
237 if (!u_LOGON_INFO) {
238 talloc_free(pac_data);
239 return ENOMEM;
241 pac_data->buffers[PAC_BUF_LOGON_INFO].type = PAC_TYPE_LOGON_INFO;
242 pac_data->buffers[PAC_BUF_LOGON_INFO].info = u_LOGON_INFO;
244 /* LOGON_NAME */
245 u_LOGON_NAME = talloc_zero(pac_data->buffers, union PAC_INFO);
246 if (!u_LOGON_NAME) {
247 talloc_free(pac_data);
248 return ENOMEM;
250 pac_data->buffers[PAC_BUF_LOGON_NAME].type = PAC_TYPE_LOGON_NAME;
251 pac_data->buffers[PAC_BUF_LOGON_NAME].info = u_LOGON_NAME;
252 LOGON_NAME = &u_LOGON_NAME->logon_name;
254 /* SRV_CHECKSUM */
255 u_SRV_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
256 if (!u_SRV_CHECKSUM) {
257 talloc_free(pac_data);
258 return ENOMEM;
260 pac_data->buffers[PAC_BUF_SRV_CHECKSUM].type = PAC_TYPE_SRV_CHECKSUM;
261 pac_data->buffers[PAC_BUF_SRV_CHECKSUM].info = u_SRV_CHECKSUM;
263 /* KDC_CHECKSUM */
264 u_KDC_CHECKSUM = talloc_zero(pac_data->buffers, union PAC_INFO);
265 if (!u_KDC_CHECKSUM) {
266 talloc_free(pac_data);
267 return ENOMEM;
269 pac_data->buffers[PAC_BUF_KDC_CHECKSUM].type = PAC_TYPE_KDC_CHECKSUM;
270 pac_data->buffers[PAC_BUF_KDC_CHECKSUM].info = u_KDC_CHECKSUM;
272 /* now the real work begins... */
274 LOGON_INFO = talloc_zero(u_LOGON_INFO, struct PAC_LOGON_INFO);
275 if (!LOGON_INFO) {
276 talloc_free(pac_data);
277 return ENOMEM;
279 nt_status = auth_convert_user_info_dc_saminfo3(LOGON_INFO, user_info_dc, &sam3);
280 if (!NT_STATUS_IS_OK(nt_status)) {
281 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status)));
282 talloc_free(pac_data);
283 return EINVAL;
286 u_LOGON_INFO->logon_info.info = LOGON_INFO;
287 LOGON_INFO->info3 = *sam3;
289 ret = krb5_unparse_name_flags(context, client_principal,
290 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
291 if (ret) {
292 return ret;
294 LOGON_NAME->account_name = talloc_strdup(LOGON_NAME, name);
295 free(name);
297 this logon_time field is absolutely critical. This is what
298 caused all our PAC troubles :-)
300 unix_to_nt_time(&LOGON_NAME->logon_time, tgs_authtime);
302 ret = kerberos_encode_pac(mem_ctx,
303 pac_data,
304 context,
305 krbtgt_keyblock,
306 service_keyblock,
307 pac);
308 talloc_free(pac_data);
309 return ret;
312 krb5_error_code kerberos_pac_to_user_info_dc(TALLOC_CTX *mem_ctx,
313 krb5_pac pac,
314 krb5_context context,
315 struct auth_user_info_dc **user_info_dc,
316 struct PAC_SIGNATURE_DATA *pac_srv_sig,
317 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
319 NTSTATUS nt_status;
320 enum ndr_err_code ndr_err;
321 krb5_error_code ret;
323 DATA_BLOB pac_logon_info_in, pac_srv_checksum_in, pac_kdc_checksum_in;
324 krb5_data k5pac_logon_info_in, k5pac_srv_checksum_in, k5pac_kdc_checksum_in;
326 union PAC_INFO info;
327 struct auth_user_info_dc *user_info_dc_out;
329 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
331 if (!tmp_ctx) {
332 return ENOMEM;
335 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_LOGON_INFO, &k5pac_logon_info_in);
336 if (ret != 0) {
337 talloc_free(tmp_ctx);
338 return EINVAL;
341 pac_logon_info_in = data_blob_const(k5pac_logon_info_in.data, k5pac_logon_info_in.length);
343 ndr_err = ndr_pull_union_blob(&pac_logon_info_in, tmp_ctx, &info,
344 PAC_TYPE_LOGON_INFO,
345 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
346 kerberos_free_data_contents(context, &k5pac_logon_info_in);
347 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err) || !info.logon_info.info) {
348 nt_status = ndr_map_error2ntstatus(ndr_err);
349 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
350 talloc_free(tmp_ctx);
351 return EINVAL;
354 /* Pull this right into the normal auth sysstem structures */
355 nt_status = make_user_info_dc_pac(mem_ctx,
356 info.logon_info.info,
357 &user_info_dc_out);
358 if (!NT_STATUS_IS_OK(nt_status)) {
359 talloc_free(tmp_ctx);
360 return EINVAL;
363 if (pac_srv_sig) {
364 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_SRV_CHECKSUM, &k5pac_srv_checksum_in);
365 if (ret != 0) {
366 talloc_free(tmp_ctx);
367 return ret;
370 pac_srv_checksum_in = data_blob_const(k5pac_srv_checksum_in.data, k5pac_srv_checksum_in.length);
372 ndr_err = ndr_pull_struct_blob(&pac_srv_checksum_in, pac_srv_sig,
373 pac_srv_sig,
374 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
375 kerberos_free_data_contents(context, &k5pac_srv_checksum_in);
376 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
377 nt_status = ndr_map_error2ntstatus(ndr_err);
378 DEBUG(0,("can't parse the KDC signature: %s\n",
379 nt_errstr(nt_status)));
380 return EINVAL;
384 if (pac_kdc_sig) {
385 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_KDC_CHECKSUM, &k5pac_kdc_checksum_in);
386 if (ret != 0) {
387 talloc_free(tmp_ctx);
388 return ret;
391 pac_kdc_checksum_in = data_blob_const(k5pac_kdc_checksum_in.data, k5pac_kdc_checksum_in.length);
393 ndr_err = ndr_pull_struct_blob(&pac_kdc_checksum_in, pac_kdc_sig,
394 pac_kdc_sig,
395 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
396 kerberos_free_data_contents(context, &k5pac_kdc_checksum_in);
397 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
398 nt_status = ndr_map_error2ntstatus(ndr_err);
399 DEBUG(0,("can't parse the KDC signature: %s\n",
400 nt_errstr(nt_status)));
401 return EINVAL;
404 *user_info_dc = user_info_dc_out;
406 return 0;
410 NTSTATUS kerberos_pac_blob_to_user_info_dc(TALLOC_CTX *mem_ctx,
411 DATA_BLOB pac_blob,
412 krb5_context context,
413 struct auth_user_info_dc **user_info_dc,
414 struct PAC_SIGNATURE_DATA *pac_srv_sig,
415 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
417 krb5_error_code ret;
418 krb5_pac pac;
419 ret = krb5_pac_parse(context,
420 pac_blob.data, pac_blob.length,
421 &pac);
422 if (ret) {
423 return map_nt_error_from_unix_common(ret);
427 ret = kerberos_pac_to_user_info_dc(mem_ctx, pac, context, user_info_dc, pac_srv_sig, pac_kdc_sig);
428 krb5_pac_free(context, pac);
429 if (ret) {
430 return map_nt_error_from_unix_common(ret);
432 return NT_STATUS_OK;