Debian Codegraph @

source4/torture/krb5/kdc-heimdal.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 Validate the krb5 pac generation routines
5
6 Copyright (C) Andrew Bartlett <[email protected]> 2005-2015
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "system/kerberos.h"
25 #include "torture/smbtorture.h"
26 #include "torture/winbind/proto.h"
27 #include "torture/krb5/proto.h"
28 #include "auth/credentials/credentials.h"
29 #include "lib/cmdline/cmdline.h"
30 #include "source4/auth/kerberos/kerberos.h"
31 #include "source4/auth/kerberos/kerberos_util.h"
32 #include "lib/util/util_net.h"
33 #include "param/param.h"
34
35 #define krb5_is_app_tag(dat,tag) \
36 ((dat != NULL) && (dat)->length && \
37 (((((char *)(dat)->data)[0] & ~0x20) == ((tag) | 0x40))))
38
39 #define krb5_is_krb_error(dat) krb5_is_app_tag(dat, 30)
40
41 enum torture_krb5_test {
42 TORTURE_KRB5_TEST_PLAIN,
43 TORTURE_KRB5_TEST_PAC_REQUEST,
44 TORTURE_KRB5_TEST_BREAK_PW,
45 TORTURE_KRB5_TEST_CLOCK_SKEW,
46 TORTURE_KRB5_TEST_AES,
47 TORTURE_KRB5_TEST_RC4,
48 TORTURE_KRB5_TEST_AES_RC4,
49
50 /*
51 * This is in and out of the client.
52 * Out refers to requests, in refers to replies
53 */
54 TORTURE_KRB5_TEST_CHANGE_SERVER_OUT,
55 TORTURE_KRB5_TEST_CHANGE_SERVER_IN,
56 TORTURE_KRB5_TEST_CHANGE_SERVER_BOTH,
57 };
58
59 struct torture_krb5_context {
60 struct torture_context *tctx;
61 struct addrinfo *server;
62 enum torture_krb5_test test;
63 int packet_count;
64 AS_REQ as_req;
65 AS_REP as_rep;
66 const char *krb5_service;
67 const char *krb5_hostname;
68 };
69
70 /*
71 * Confirm that the outgoing packet meets certain expectations. This
72 * should be extended to further assert the correct and expected
73 * behaviour of the krb5 libs, so we know what we are sending to the
74 * server.
75 *
76 */
77
78 static bool torture_krb5_pre_send_test(struct torture_krb5_context *test_context, krb5_data *send_buf)
79 {
80 size_t used;
81 switch (test_context->test)
82 {
83 case TORTURE_KRB5_TEST_PLAIN:
84 case TORTURE_KRB5_TEST_PAC_REQUEST:
85 case TORTURE_KRB5_TEST_BREAK_PW:
86 case TORTURE_KRB5_TEST_CLOCK_SKEW:
87 case TORTURE_KRB5_TEST_AES:
88 case TORTURE_KRB5_TEST_RC4:
89 case TORTURE_KRB5_TEST_AES_RC4:
90 case TORTURE_KRB5_TEST_CHANGE_SERVER_IN:
91 torture_assert_int_equal(test_context->tctx,
92 decode_AS_REQ(send_buf->data, send_buf->length, &test_context->as_req, &used), 0,
93 "decode_AS_REQ failed");
94 torture_assert_int_equal(test_context->tctx, used, send_buf->length, "length mismatch");
95 torture_assert_int_equal(test_context->tctx, test_context->as_req.pvno, 5, "Got wrong as_req->pvno");
96 break;
97 case TORTURE_KRB5_TEST_CHANGE_SERVER_OUT:
98 case TORTURE_KRB5_TEST_CHANGE_SERVER_BOTH:
99 {
100 AS_REQ mod_as_req;
101 krb5_error_code k5ret;
102 krb5_data modified_send_buf;
103 torture_assert_int_equal(test_context->tctx,
104 decode_AS_REQ(send_buf->data, send_buf->length, &test_context->as_req, &used), 0,
105 "decode_AS_REQ failed");
106 torture_assert_int_equal(test_context->tctx, used, send_buf->length, "length mismatch");
107 torture_assert_int_equal(test_context->tctx, test_context->as_req.pvno, 5, "Got wrong as_req->pvno");
108
109 /* Only change it if configured with --option=torture:krb5-hostname= */
110 if (test_context->krb5_hostname[0] == '\0') {
111 break;
112 }
113
114 mod_as_req = test_context->as_req;
115
116 torture_assert_int_equal(test_context->tctx,
117 mod_as_req.req_body.sname->name_string.len, 2,
118 "Sending wrong mod_as_req.req_body->sname.name_string.len");
119 free(mod_as_req.req_body.sname->name_string.val[0]);
120 free(mod_as_req.req_body.sname->name_string.val[1]);
121 mod_as_req.req_body.sname->name_string.val[0] = strdup(test_context->krb5_service);
122 mod_as_req.req_body.sname->name_string.val[1] = strdup(test_context->krb5_hostname);
123
124 ASN1_MALLOC_ENCODE(AS_REQ, modified_send_buf.data, modified_send_buf.length,
125 &mod_as_req, &used, k5ret);
126 torture_assert_int_equal(test_context->tctx,
127 k5ret, 0,
128 "encode_AS_REQ failed");
129
130 *send_buf = modified_send_buf;
131 break;
132 }
133 }
134 return true;
135 }
136
137 static bool torture_check_krb5_error(struct torture_krb5_context *test_context,
138 const krb5_data *reply,
139 krb5_error_code expected_error,
140 bool check_pa_data)
141 {
142 KRB_ERROR error = { 0 };
143 size_t used = 0;
144 int rc;
145
146 rc = decode_KRB_ERROR(reply->data, reply->length, &error, &used);
147 torture_assert_int_equal(test_context->tctx,
148 rc, 0,
149 "decode_KRB_ERROR failed");
150
151 torture_assert_int_equal(test_context->tctx,
152 used, reply->length,
153 "length mismatch");
154 torture_assert_int_equal(test_context->tctx,
155 error.pvno, 5,
156 "Got wrong error.pvno");
157 torture_assert_int_equal(test_context->tctx,
158 error.error_code, expected_error - KRB5KDC_ERR_NONE,
159 "Got wrong error.error_code");
160
161 if (check_pa_data) {
162 METHOD_DATA m;
163 size_t len;
164 int i;
165 bool found_enc_ts = false;
166 bool found_etype_info2 = false;
167 torture_assert(test_context->tctx,
168 error.e_data != NULL,
169 "No e-data returned");
170
171 rc = decode_METHOD_DATA(error.e_data->data,
172 error.e_data->length,
173 &m,
174 &len);
175 torture_assert_int_equal(test_context->tctx,
176 rc, 0,
177 "Got invalid method data");
178
179 torture_assert(test_context->tctx,
180 m.len > 0,
181 "No PA_DATA given");
182 for (i = 0; i < m.len; i++) {
183 if (m.val[i].padata_type == KRB5_PADATA_ENC_TIMESTAMP) {
184 found_enc_ts = true;
185 }
186 else if (m.val[i].padata_type == KRB5_PADATA_ETYPE_INFO2) {
187 found_etype_info2 = true;
188 }
189 }
190 torture_assert(test_context->tctx,
191 found_etype_info2,
192 "PADATA_ETYPE_INFO2 not found");
193 if (expected_error != KRB5KDC_ERR_PREAUTH_FAILED)
194 torture_assert(test_context->tctx,
195 found_enc_ts,
196 "Encrypted timestamp not found");
197 }
198
199 free_KRB_ERROR(&error);
200
201 return true;
202 }
203
204 static bool torture_check_krb5_as_rep_enctype(struct torture_krb5_context *test_context,
205 const krb5_data *reply,
206 const krb5_enctype* allowed_enctypes)
207 {
208 ENCTYPE reply_enctype = { 0 };
209 size_t used = 0;
210 int rc;
211 int expected_enctype = ETYPE_NULL;
212
213 rc = decode_AS_REP(reply->data,
214 reply->length,
215 &test_context->as_rep,
216 &used);
217 torture_assert_int_equal(test_context->tctx,
218 rc, 0,
219 "decode_AS_REP failed");
220 torture_assert_int_equal(test_context->tctx,
221 used, reply->length,
222 "length mismatch");
223 torture_assert_int_equal(test_context->tctx,
224 test_context->as_rep.pvno, 5,
225 "Got wrong as_rep->pvno");
226 torture_assert_int_equal(test_context->tctx,
227 test_context->as_rep.ticket.tkt_vno, 5,
228 "Got wrong as_rep->ticket.tkt_vno");
229 torture_assert(test_context->tctx,
230 test_context->as_rep.ticket.enc_part.kvno,
231 "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");
232
233 if (test_context->as_req.padata) {
234 /*
235 * If the AS-REQ contains a PA-ENC-TIMESTAMP, then
236 * that encryption type is used to determine the reply
237 * enctype.
238 */
239 int i = 0;
240 const PA_DATA *pa = krb5_find_padata(test_context->as_req.padata->val,
241 test_context->as_req.padata->len,
242 KRB5_PADATA_ENC_TIMESTAMP,
243 &i);
244 if (pa) {
245 EncryptedData ed;
246 size_t len;
247 krb5_error_code ret = decode_EncryptedData(pa->padata_value.data,
248 pa->padata_value.length,
249 &ed, &len);
250 torture_assert_int_equal(test_context->tctx,
251 ret,
252 0,
253 "decode_EncryptedData failed");
254 expected_enctype = ed.etype;
255 free_EncryptedData(&ed);
256 }
257 }
258 if (expected_enctype == ETYPE_NULL) {
259 /*
260 * Otherwise, find the strongest enctype contained in
261 * the AS-REQ supported enctypes list.
262 */
263 const krb5_enctype *p = NULL;
264
265 for (p = krb5_kerberos_enctypes(NULL); *p != (krb5_enctype)ETYPE_NULL; ++p) {
266 int j;
267
268 if ((*p == (krb5_enctype)ETYPE_AES256_CTS_HMAC_SHA1_96 ||
269 *p == (krb5_enctype)ETYPE_AES128_CTS_HMAC_SHA1_96) &&
270 !test_context->as_req.req_body.kdc_options.canonicalize)
271 {
272 /*
273 * AES encryption types are only used here when
274 * we set the canonicalize flag, as the salt
275 * needs to match.
276 */
277 continue;
278 }
279
280 for (j = 0; j < test_context->as_req.req_body.etype.len; ++j) {
281 krb5_enctype etype = test_context->as_req.req_body.etype.val[j];
282 if (*p == etype) {
283 expected_enctype = etype;
284 break;
285 }
286 }
287
288 if (expected_enctype != (krb5_enctype)ETYPE_NULL) {
289 break;
290 }
291 }
292 }
293
294 {
295 /* Ensure the enctype to check against is an expected type. */
296 const krb5_enctype *p = NULL;
297 bool found = false;
298 for (p = allowed_enctypes; *p != (krb5_enctype)ETYPE_NULL; ++p) {
299 if (*p == expected_enctype) {
300 found = true;
301 break;
302 }
303 }
304
305 torture_assert(test_context->tctx,
306 found,
307 "Calculated enctype not in allowed list");
308 }
309
310 reply_enctype = test_context->as_rep.enc_part.etype;
311 torture_assert_int_equal(test_context->tctx,
312 reply_enctype, expected_enctype,
313 "Ticket encrypted with invalid algorithm");
314
315 return true;
316 }
317
318 /*
319 * Confirm that the incoming packet from the KDC meets certain
320 * expectations. This uses a switch and the packet count to work out
321 * what test we are in, and where in the test we are, so we can assert
322 * on the expected reply packets from the KDC.
323 *
324 */
325
326 static bool torture_krb5_post_recv_test(struct torture_krb5_context *test_context, krb5_data *recv_buf)
327 {
328 KRB_ERROR error;
329 size_t used;
330 bool ok;
331 bool require_canon = lpcfg_kdc_require_canonicalization(
332 test_context->tctx->lp_ctx);
333
334 if (require_canon) {
335 torture_comment(test_context->tctx, "require_canon\n");
336 ok = torture_check_krb5_error(test_context,
337 recv_buf,
338 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN,
339 false);
340 torture_assert(test_context->tctx,
341 ok,
342 "canonicalization required");
343 return true;
344 }
345
346 switch (test_context->test)
347 {
348 case TORTURE_KRB5_TEST_CHANGE_SERVER_OUT:
349 case TORTURE_KRB5_TEST_PLAIN:
350 if (test_context->packet_count == 0) {
351 ok = torture_check_krb5_error(test_context,
352 recv_buf,
353 KRB5KDC_ERR_PREAUTH_REQUIRED,
354 false);
355 torture_assert(test_context->tctx,
356 ok,
357 "torture_check_krb5_error failed");
358 } else if ((decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0)
359 && (test_context->packet_count == 1)) {
360 torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
361 torture_assert_int_equal(test_context->tctx, error.pvno, 5, "Got wrong error.pvno");
362 torture_assert_int_equal(test_context->tctx, error.error_code, KRB5KRB_ERR_RESPONSE_TOO_BIG - KRB5KDC_ERR_NONE,
363 "Got wrong error.error_code");
364 free_KRB_ERROR(&error);
365 } else {
366 torture_assert_int_equal(test_context->tctx,
367 decode_AS_REP(recv_buf->data, recv_buf->length, &test_context->as_rep, &used), 0,
368 "decode_AS_REP failed");
369 torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
370 torture_assert_int_equal(test_context->tctx,
371 test_context->as_rep.pvno, 5,
372 "Got wrong as_rep->pvno");
373 torture_assert_int_equal(test_context->tctx,
374 test_context->as_rep.ticket.tkt_vno, 5,
375 "Got wrong as_rep->ticket.tkt_vno");
376 torture_assert(test_context->tctx,
377 test_context->as_rep.ticket.enc_part.kvno,
378 "Did not get a KVNO in test_context->as_rep.ticket.enc_part.kvno");
379 if (test_context->test == TORTURE_KRB5_TEST_PLAIN) {
380 if (torture_setting_bool(test_context->tctx, "expect_cached_at_rodc", false)) {
381 torture_assert_int_not_equal(test_context->tctx,
382 *test_context->as_rep.ticket.enc_part.kvno & 0xFFFF0000,
383 0, "Did not get a RODC number in the KVNO");
384 } else {
385 torture_assert_int_equal(test_context->tctx,
386 *test_context->as_rep.ticket.enc_part.kvno & 0xFFFF0000,
387 0, "Unexpecedly got a RODC number in the KVNO");
388 }
389 }
390 free_AS_REP(&test_context->as_rep);
391 }
392 torture_assert(test_context->tctx, test_context->packet_count < 3, "too many packets");
393 free_AS_REQ(&test_context->as_req);
394 break;
395
396 /*
397 * Confirm correct error codes when we ask for the PAC. This behaviour is rather odd...
398 */
399 case TORTURE_KRB5_TEST_PAC_REQUEST:
400 if (test_context->packet_count == 0) {
401 ok = torture_check_krb5_error(test_context,
402 recv_buf,
403 KRB5KDC_ERR_PREAUTH_REQUIRED,
404 false);
405 torture_assert(test_context->tctx,
406 ok,
407 "torture_check_krb5_error failed");
408 } else if ((decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0)
409 && (test_context->packet_count == 1)) {
410 torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
411 torture_assert_int_equal(test_context->tctx, error.pvno, 5, "Got wrong error.pvno");
412 torture_assert_int_equal(test_context->tctx, error.error_code, KRB5KRB_ERR_RESPONSE_TOO_BIG - KRB5KDC_ERR_NONE,
413 "Got wrong error.error_code");
414 free_KRB_ERROR(&error);
415 } else {
416 torture_assert_int_equal(test_context->tctx,
417 decode_AS_REP(recv_buf->data, recv_buf->length, &test_context->as_rep, &used), 0,
418 "decode_AS_REP failed");
419 torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
420 torture_assert_int_equal(test_context->tctx, test_context->as_rep.pvno, 5, "Got wrong as_rep->pvno");
421 free_AS_REP(&test_context->as_rep);
422 }
423 torture_assert(test_context->tctx, test_context->packet_count < 2, "too many packets");
424 free_AS_REQ(&test_context->as_req);
425 break;
426
427 /*
428 * Confirm correct error codes when we deliberately send the wrong password
429 */
430 case TORTURE_KRB5_TEST_BREAK_PW:
431 if (test_context->packet_count == 0) {
432 ok = torture_check_krb5_error(test_context,
433 recv_buf,
434 KRB5KDC_ERR_PREAUTH_REQUIRED,
435 false);
436 torture_assert(test_context->tctx,
437 ok,
438 "torture_check_krb5_error failed");
439 } else if (test_context->packet_count == 1) {
440 ok = torture_check_krb5_error(test_context,
441 recv_buf,
442 KRB5KDC_ERR_PREAUTH_FAILED,
443 true);
444 torture_assert(test_context->tctx,
445 ok,
446 "torture_check_krb5_error failed");
447 }
448 torture_assert(test_context->tctx, test_context->packet_count < 2, "too many packets");
449 free_AS_REQ(&test_context->as_req);
450 break;
451
452 /*
453 * Confirm correct error codes when we deliberately skew the client clock
454 */
455 case TORTURE_KRB5_TEST_CLOCK_SKEW:
456 if (test_context->packet_count == 0) {
457 ok = torture_check_krb5_error(test_context,
458 recv_buf,
459 KRB5KDC_ERR_PREAUTH_REQUIRED,
460 false);
461 torture_assert(test_context->tctx,
462 ok,
463 "torture_check_krb5_error failed");
464 } else if (test_context->packet_count == 1) {
465 ok = torture_check_krb5_error(test_context,
466 recv_buf,
467 KRB5KRB_AP_ERR_SKEW,
468 false);
469 torture_assert(test_context->tctx,
470 ok,
471 "torture_check_krb5_error failed");
472 }
473 torture_assert(test_context->tctx, test_context->packet_count < 2, "too many packets");
474 free_AS_REQ(&test_context->as_req);
475 break;
476 case TORTURE_KRB5_TEST_AES:
477 torture_comment(test_context->tctx, "TORTURE_KRB5_TEST_AES\n");
478
479 if (test_context->packet_count == 0) {
480 ok = torture_check_krb5_error(test_context,
481 recv_buf,
482 KRB5KDC_ERR_PREAUTH_REQUIRED,
483 false);
484 torture_assert(test_context->tctx,
485 ok,
486 "torture_check_krb5_error failed");
487 } else if (krb5_is_krb_error(recv_buf)) {
488 ok = torture_check_krb5_error(test_context,
489 recv_buf,
490 KRB5KRB_ERR_RESPONSE_TOO_BIG,
491 false);
492 torture_assert(test_context->tctx,
493 ok,
494 "torture_check_krb5_error failed");
495 } else {
496 const krb5_enctype allowed_enctypes[] = {
497 KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96,
498 ETYPE_NULL
499 };
500 ok = torture_check_krb5_as_rep_enctype(test_context,
501 recv_buf,
502 allowed_enctypes);
503 torture_assert(test_context->tctx,
504 ok,
505 "torture_check_krb5_as_rep_enctype failed");
506 }
507
508 torture_assert(test_context->tctx,
509 test_context->packet_count < 3,
510 "Too many packets");
511 break;
512 case TORTURE_KRB5_TEST_RC4:
513 torture_comment(test_context->tctx, "TORTURE_KRB5_TEST_RC4\n");
514
515 if (test_context->packet_count == 0) {
516 ok = torture_check_krb5_error(test_context,
517 recv_buf,
518 KRB5KDC_ERR_PREAUTH_REQUIRED,
519 false);
520 torture_assert(test_context->tctx,
521 ok,
522 "torture_check_krb5_error failed");
523 } else if (krb5_is_krb_error(recv_buf)) {
524 ok = torture_check_krb5_error(test_context,
525 recv_buf,
526 KRB5KRB_ERR_RESPONSE_TOO_BIG,
527 false);
528 torture_assert(test_context->tctx,
529 ok,
530 "torture_check_krb5_error failed");
531 } else {
532 const krb5_enctype allowed_enctypes[] = {
533 KRB5_ENCTYPE_ARCFOUR_HMAC_MD5,
534 ETYPE_NULL
535 };
536 ok = torture_check_krb5_as_rep_enctype(test_context,
537 recv_buf,
538 allowed_enctypes);
539 torture_assert(test_context->tctx,
540 ok,
541 "torture_check_krb5_as_rep_enctype failed");
542 }
543
544 torture_assert(test_context->tctx,
545 test_context->packet_count < 3,
546 "Too many packets");
547 break;
548 case TORTURE_KRB5_TEST_AES_RC4:
549 torture_comment(test_context->tctx, "TORTURE_KRB5_TEST_AES_RC4\n");
550
551 if (test_context->packet_count == 0) {
552 ok = torture_check_krb5_error(test_context,
553 recv_buf,
554 KRB5KDC_ERR_PREAUTH_REQUIRED,
555 false);
556 torture_assert(test_context->tctx,
557 ok,
558 "torture_check_krb5_error failed");
559 } else if (krb5_is_krb_error(recv_buf)) {
560 ok = torture_check_krb5_error(test_context,
561 recv_buf,
562 KRB5KRB_ERR_RESPONSE_TOO_BIG,
563 false);
564 torture_assert(test_context->tctx,
565 ok,
566 "torture_check_krb5_error failed");
567 } else {
568 const krb5_enctype allowed_enctypes[] = {
569 KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96,
570 KRB5_ENCTYPE_ARCFOUR_HMAC_MD5,
571 ETYPE_NULL
572 };
573 ok = torture_check_krb5_as_rep_enctype(test_context,
574 recv_buf,
575 allowed_enctypes);
576 torture_assert(test_context->tctx,
577 ok,
578 "torture_check_krb5_as_rep_enctype failed");
579 }
580
581 torture_assert(test_context->tctx,
582 test_context->packet_count < 3,
583 "Too many packets");
584 break;
585 case TORTURE_KRB5_TEST_CHANGE_SERVER_IN:
586 case TORTURE_KRB5_TEST_CHANGE_SERVER_BOTH:
587 {
588 AS_REP mod_as_rep;
589 krb5_error_code k5ret;
590 krb5_data modified_recv_buf;
591 if (test_context->packet_count == 0) {
592 ok = torture_check_krb5_error(test_context,
593 recv_buf,
594 KRB5KDC_ERR_PREAUTH_REQUIRED,
595 false);
596 torture_assert(test_context->tctx,
597 ok,
598 "torture_check_krb5_error failed");
599 } else if ((decode_KRB_ERROR(recv_buf->data, recv_buf->length, &error, &used) == 0)
600 && (test_context->packet_count == 1)) {
601 torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
602 torture_assert_int_equal(test_context->tctx, error.pvno, 5, "Got wrong error.pvno");
603 torture_assert_int_equal(test_context->tctx, error.error_code, KRB5KRB_ERR_RESPONSE_TOO_BIG - KRB5KDC_ERR_NONE,
604 "Got wrong error.error_code");
605 free_KRB_ERROR(&error);
606 } else {
607 torture_assert_int_equal(test_context->tctx,
608 decode_AS_REP(recv_buf->data, recv_buf->length, &test_context->as_rep, &used), 0,
609 "decode_AS_REP failed");
610 torture_assert_int_equal(test_context->tctx, used, recv_buf->length, "length mismatch");
611 torture_assert_int_equal(test_context->tctx,
612 test_context->as_rep.pvno, 5,
613 "Got wrong as_rep->pvno");
614 torture_assert_int_equal(test_context->tctx,
615 test_context->as_rep.ticket.tkt_vno, 5,
616 "Got wrong as_rep->ticket.tkt_vno");
617 torture_assert_int_equal(test_context->tctx,
618 test_context->as_rep.ticket.sname.name_string.len, 2,
619 "Got wrong as_rep->ticket.sname.name_string.len");
620 free(test_context->as_rep.ticket.sname.name_string.val[0]);
621 free(test_context->as_rep.ticket.sname.name_string.val[1]);
622 test_context->as_rep.ticket.sname.name_string.val[0] = strdup("bad");
623 test_context->as_rep.ticket.sname.name_string.val[1] = strdup("mallory");
624
625 mod_as_rep = test_context->as_rep;
626
627 ASN1_MALLOC_ENCODE(AS_REP, modified_recv_buf.data, modified_recv_buf.length,
628 &mod_as_rep, &used, k5ret);
629 torture_assert_int_equal(test_context->tctx,
630 k5ret, 0,
631 "encode_AS_REQ failed");
632 krb5_data_free(recv_buf);
633
634 *recv_buf = modified_recv_buf;
635 free_AS_REQ(&test_context->as_req);
636 }
637 torture_assert(test_context->tctx, test_context->packet_count < 3, "too many packets");
638
639 break;
640 }
641 }
642
643
644 return true;
645 }
646
647
648 /*
649 * This function is set in torture_krb5_init_context as krb5
650 * send_and_recv function. This allows us to override what server the
651 * test is aimed at, and to inspect the packets just before they are
652 * sent to the network, and before they are processed on the recv
653 * side.
654 *
655 * The torture_krb5_pre_send_test() and torture_krb5_post_recv_test()
656 * functions are implement the actual tests.
657 *
658 * When this asserts, the caller will get a spurious 'cannot contact
659 * any KDC' message.
660 *
661 */
662 static krb5_error_code test_krb5_send_to_realm_override(
663 struct smb_krb5_context *smb_krb5_context,
664 void *data, /* struct torture_krb5_context */
665 krb5_const_realm realm,
666 time_t timeout,
667 const krb5_data *send_buf,
668 krb5_data *recv_buf)
669 {
670 krb5_error_code k5ret;
671 bool ok;
672 krb5_data modified_send_buf = *send_buf;
673
674 struct torture_krb5_context *test_context
675 = talloc_get_type_abort(data, struct torture_krb5_context);
676
677 ok = torture_krb5_pre_send_test(test_context, &modified_send_buf);
678 if (ok == false) {
679 return EINVAL;
680 }
681
682 k5ret = smb_krb5_send_and_recv_func_forced_tcp(smb_krb5_context,
683 test_context->server,
684 timeout,
685 &modified_send_buf,
686 recv_buf);
687 if (k5ret != 0) {
688 return k5ret;
689 }
690 ok = torture_krb5_post_recv_test(test_context, recv_buf);
691 if (ok == false) {
692 return EINVAL;
693 }
694
695 test_context->packet_count++;
696
697 return k5ret;
698 }
699
700 static int test_context_destructor(struct torture_krb5_context *test_context)
701 {
702 freeaddrinfo(test_context->server);
703 return 0;
704 }
705
706
707 static bool torture_krb5_init_context(struct torture_context *tctx,
708 enum torture_krb5_test test,
709 struct smb_krb5_context **smb_krb5_context)
710 {
711 const char *host = torture_setting_string(tctx, "host", NULL);
712 krb5_error_code k5ret;
713 bool ok;
714
715 struct torture_krb5_context *test_context = talloc_zero(tctx, struct torture_krb5_context);
716 torture_assert(tctx, test_context != NULL, "Failed to allocate");
717
718 test_context->test = test;
719 test_context->tctx = tctx;
720
721 test_context->krb5_service = torture_setting_string(tctx, "krb5-service", "host");
722 test_context->krb5_hostname = torture_setting_string(tctx, "krb5-hostname", "");
723
724 k5ret = smb_krb5_init_context(tctx, tctx->lp_ctx, smb_krb5_context);
725 torture_assert_int_equal(tctx, k5ret, 0, "smb_krb5_init_context failed");
726
727 ok = interpret_string_addr_internal(&test_context->server, host, AI_NUMERICHOST);
728 torture_assert(tctx, ok, "Failed to parse target server");
729
730 talloc_set_destructor(test_context, test_context_destructor);
731
732 set_sockaddr_port(test_context->server->ai_addr, 88);
733
734 k5ret = smb_krb5_set_send_to_kdc_func((*smb_krb5_context),
735 test_krb5_send_to_realm_override,
736 NULL, /* send_to_kdc */
737 test_context);
738 torture_assert_int_equal(tctx, k5ret, 0, "krb5_set_send_to_kdc_func failed");
739 return true;
740 }
741
742 static bool torture_krb5_as_req_creds(struct torture_context *tctx,
743 struct cli_credentials *credentials,
744 enum torture_krb5_test test)
745 {
746 krb5_error_code k5ret;
747 bool ok;
748 krb5_creds my_creds;
749 krb5_principal principal;
750 struct smb_krb5_context *smb_krb5_context;
751 krb5_context k5_context;
752 enum credentials_obtained obtained;
753 const char *error_string;
754 const char *password = cli_credentials_get_password(credentials);
755 const char *expected_principal_string;
756 krb5_get_init_creds_opt *krb_options = NULL;
757 const char *realm;
758 const char *krb5_hostname = torture_setting_string(tctx, "krb5-hostname", "");
759 bool require_canon = lpcfg_kdc_require_canonicalization(tctx->lp_ctx);
760
761 ok = torture_krb5_init_context(tctx, test, &smb_krb5_context);
762 torture_assert(tctx, ok, "torture_krb5_init_context failed");
763 k5_context = smb_krb5_context->krb5_context;
764
765 expected_principal_string
766 = cli_credentials_get_principal(credentials,
767 tctx);
768
769 realm = strupper_talloc(tctx, cli_credentials_get_realm(credentials));
770 k5ret = principal_from_credentials(tctx, credentials, smb_krb5_context,
771 &principal, &obtained, &error_string);
772 torture_assert_int_equal(tctx, k5ret, 0, error_string);
773
774 switch (test)
775 {
776 case TORTURE_KRB5_TEST_PLAIN:
777 case TORTURE_KRB5_TEST_CHANGE_SERVER_OUT:
778 case TORTURE_KRB5_TEST_CHANGE_SERVER_IN:
779 case TORTURE_KRB5_TEST_CHANGE_SERVER_BOTH:
780 break;
781
782 case TORTURE_KRB5_TEST_PAC_REQUEST:
783 torture_assert_int_equal(tctx,
784 krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context, &krb_options),
785 0, "krb5_get_init_creds_opt_alloc failed");
786
787 torture_assert_int_equal(tctx,
788 krb5_get_init_creds_opt_set_pac_request(smb_krb5_context->krb5_context, krb_options, true),
789 0, "krb5_get_init_creds_opt_set_pac_request failed");
790 break;
791
792 case TORTURE_KRB5_TEST_BREAK_PW:
793 password = "NOT the password";
794 break;
795
796 case TORTURE_KRB5_TEST_CLOCK_SKEW:
797 torture_assert_int_equal(tctx,
798 krb5_set_real_time(smb_krb5_context->krb5_context, time(NULL) + 3600, 0),
799 0, "krb5_set_real_time failed");
800 break;
801
802 case TORTURE_KRB5_TEST_AES: {
803 static krb5_enctype etype_list[] = { KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96 };
804
805 k5ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context,
806 &krb_options);
807 torture_assert_int_equal(tctx,
808 k5ret, 0,
809 "krb5_get_init_creds_opt_alloc failed");
810
811 krb5_get_init_creds_opt_set_etype_list(krb_options,
812 etype_list,
813 1);
814 break;
815 }
816 case TORTURE_KRB5_TEST_RC4: {
817 static krb5_enctype etype_list[] = { KRB5_ENCTYPE_ARCFOUR_HMAC_MD5 };
818
819 k5ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context,
820 &krb_options);
821 torture_assert_int_equal(tctx,
822 k5ret, 0,
823 "krb5_get_init_creds_opt_alloc failed");
824
825 krb5_get_init_creds_opt_set_etype_list(krb_options,
826 etype_list,
827 1);
828 break;
829 }
830 case TORTURE_KRB5_TEST_AES_RC4: {
831 static krb5_enctype etype_list[] = { KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96,
832 KRB5_ENCTYPE_ARCFOUR_HMAC_MD5 };
833
834 k5ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context,
835 &krb_options);
836 torture_assert_int_equal(tctx,
837 k5ret, 0,
838 "krb5_get_init_creds_opt_alloc failed");
839
840 krb5_get_init_creds_opt_set_etype_list(krb_options,
841 etype_list,
842 2);
843 break;
844 }
845
846 } /* end switch */
847
848 k5ret = krb5_get_init_creds_password(smb_krb5_context->krb5_context, &my_creds, principal,
849 password, NULL, NULL, 0,
850 NULL, krb_options);
851
852 if (require_canon) {
853 /*
854 * The clients in these requests are not using canonicalization, so
855 * in all cases where the server has 'require canonicalization = yes',
856 * the error code will be the same.
857 */
858 torture_assert_int_equal(tctx,
859 k5ret,
860 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN,
861 "server requires client to ask for canonicalization");
862 return true;
863 }
864
865 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
866
867 switch (test)
868 {
869 case TORTURE_KRB5_TEST_PLAIN:
870 case TORTURE_KRB5_TEST_CHANGE_SERVER_IN:
871 case TORTURE_KRB5_TEST_PAC_REQUEST:
872 case TORTURE_KRB5_TEST_AES:
873 case TORTURE_KRB5_TEST_RC4:
874 case TORTURE_KRB5_TEST_AES_RC4:
875 {
876 char *got_principal_string;
877 char *assertion_message;
878 torture_assert_int_equal(tctx, k5ret, 0, "krb5_get_init_creds_password failed");
879
880 torture_assert_int_equal(tctx,
881 krb5_principal_get_type(k5_context,
882 my_creds.client),
883 KRB5_NT_PRINCIPAL,
884 "smb_krb5_init_context gave incorrect client->name.name_type");
885
886 torture_assert_int_equal(tctx,
887 krb5_unparse_name(k5_context,
888 my_creds.client,
889 &got_principal_string), 0,
890 "krb5_unparse_name failed");
891
892 assertion_message = talloc_asprintf(tctx,
893 "krb5_get_init_creds_password returned a different principal %s to what was expected %s",
894 got_principal_string, expected_principal_string);
895 krb5_xfree(got_principal_string);
896
897 torture_assert(tctx, krb5_principal_compare(k5_context,
898 my_creds.client,
899 principal),
900 assertion_message);
901
902
903 torture_assert_str_equal(tctx,
904 my_creds.server->name.name_string.val[0],
905 "krbtgt",
906 "Mismatch in name between AS_REP and expected response, expected krbtgt");
907 torture_assert_str_equal(tctx,
908 my_creds.server->name.name_string.val[1],
909 realm,
910 "Mismatch in realm part of krbtgt/ in AS_REP, expected krbtgt/REALM@REALM");
911
912 torture_assert_str_equal(tctx,
913 my_creds.server->realm,
914 realm,
915 "Mismatch in server realm in AS_REP, expected krbtgt/REALM@REALM");
916
917 break;
918 }
919 case TORTURE_KRB5_TEST_BREAK_PW:
920 torture_assert_int_equal(tctx, k5ret, KRB5KDC_ERR_PREAUTH_FAILED, "krb5_get_init_creds_password should have failed");
921 return true;
922
923 case TORTURE_KRB5_TEST_CLOCK_SKEW:
924 torture_assert_int_equal(tctx, k5ret, KRB5KRB_AP_ERR_SKEW, "krb5_get_init_creds_password should have failed");
925 return true;
926
927 case TORTURE_KRB5_TEST_CHANGE_SERVER_OUT:
928 case TORTURE_KRB5_TEST_CHANGE_SERVER_BOTH:
929 {
930 char *got_principal_string;
931 char *assertion_message;
932
933 if (krb5_hostname[0] != '\0') {
934 torture_assert_int_equal(tctx, k5ret, KRB5KRB_AP_ERR_BAD_INTEGRITY, "krb5_get_init_creds_password should have failed");
935 return true;
936 }
937
938 torture_assert_int_equal(tctx, k5ret, 0, "krb5_get_init_creds_password failed");
939
940 torture_assert_int_equal(tctx,
941 krb5_principal_get_type(k5_context,
942 my_creds.client),
943 KRB5_NT_PRINCIPAL,
944 "smb_krb5_init_context gave incorrect client->name.name_type");
945
946 torture_assert_int_equal(tctx,
947 krb5_unparse_name(k5_context,
948 my_creds.client,
949 &got_principal_string), 0,
950 "krb5_unparse_name failed");
951
952 assertion_message = talloc_asprintf(tctx,
953 "krb5_get_init_creds_password returned a different principal %s to what was expected %s",
954 got_principal_string, expected_principal_string);
955 krb5_xfree(got_principal_string);
956
957 torture_assert(tctx, krb5_principal_compare(k5_context,
958 my_creds.client,
959 principal),
960 assertion_message);
961
962 break;
963 }
964 }
965
966 k5ret = krb5_free_cred_contents(smb_krb5_context->krb5_context, &my_creds);
967 torture_assert_int_equal(tctx, k5ret, 0, "krb5_free_creds failed");
968
969 return true;
970 }
971
972 static bool torture_krb5_as_req_cmdline(struct torture_context *tctx)
973 {
974 return torture_krb5_as_req_creds(tctx, samba_cmdline_get_creds(),
975 TORTURE_KRB5_TEST_PLAIN);
976 }
977
978 static bool torture_krb5_as_req_pac_request(struct torture_context *tctx)
979 {
980 if (torture_setting_bool(tctx, "expect_rodc", false)) {
981 torture_skip(tctx, "This test needs further investigation in the RODC case against a Windows DC, in particular with non-cached users");
982 }
983 return torture_krb5_as_req_creds(tctx, samba_cmdline_get_creds(),
984 TORTURE_KRB5_TEST_PAC_REQUEST);
985 }
986
987 static bool torture_krb5_as_req_break_pw(struct torture_context *tctx)
988 {
989 return torture_krb5_as_req_creds(tctx, samba_cmdline_get_creds(),
990 TORTURE_KRB5_TEST_BREAK_PW);
991 }
992
993 static bool torture_krb5_as_req_clock_skew(struct torture_context *tctx)
994 {
995 return torture_krb5_as_req_creds(tctx, samba_cmdline_get_creds(),
996 TORTURE_KRB5_TEST_CLOCK_SKEW);
997 }
998
999 static bool torture_krb5_as_req_aes(struct torture_context *tctx)
1000 {
1001 return torture_krb5_as_req_creds(tctx,
1002 samba_cmdline_get_creds(),
1003 TORTURE_KRB5_TEST_AES);
1004 }
1005
1006 static bool torture_krb5_as_req_rc4(struct torture_context *tctx)
1007 {
1008 return torture_krb5_as_req_creds(tctx,
1009 samba_cmdline_get_creds(),
1010 TORTURE_KRB5_TEST_RC4);
1011 }
1012
1013 static bool torture_krb5_as_req_aes_rc4(struct torture_context *tctx)
1014 {
1015 return torture_krb5_as_req_creds(tctx,
1016 samba_cmdline_get_creds(),
1017 TORTURE_KRB5_TEST_AES_RC4);
1018 }
1019
1020 /* Checking for the "Orpheus' Lyre" attack */
1021 static bool torture_krb5_as_req_change_server_out(struct torture_context *tctx)
1022 {
1023 return torture_krb5_as_req_creds(tctx,
1024 samba_cmdline_get_creds(),
1025 TORTURE_KRB5_TEST_CHANGE_SERVER_OUT);
1026 }
1027
1028 static bool torture_krb5_as_req_change_server_in(struct torture_context *tctx)
1029 {
1030 return torture_krb5_as_req_creds(tctx,
1031 samba_cmdline_get_creds(),
1032 TORTURE_KRB5_TEST_CHANGE_SERVER_IN);
1033 }
1034
1035 static bool torture_krb5_as_req_change_server_both(struct torture_context *tctx)
1036 {
1037 return torture_krb5_as_req_creds(tctx,
1038 samba_cmdline_get_creds(),
1039 TORTURE_KRB5_TEST_CHANGE_SERVER_BOTH);
1040 }
1041
1042 NTSTATUS torture_krb5_init(TALLOC_CTX *ctx)
1043 {
1044 struct torture_suite *suite = torture_suite_create(ctx, "krb5");
1045 struct torture_suite *kdc_suite = torture_suite_create(suite, "kdc");
1046 suite->description = talloc_strdup(suite, "Kerberos tests");
1047 kdc_suite->description = talloc_strdup(kdc_suite, "Kerberos KDC tests");
1048
1049 torture_suite_add_simple_test(kdc_suite, "as-req-cmdline",
1050 torture_krb5_as_req_cmdline);
1051
1052 torture_suite_add_simple_test(kdc_suite, "as-req-pac-request",
1053 torture_krb5_as_req_pac_request);
1054
1055 torture_suite_add_simple_test(kdc_suite, "as-req-break-pw",
1056 torture_krb5_as_req_break_pw);
1057
1058 torture_suite_add_simple_test(kdc_suite, "as-req-clock-skew",
1059 torture_krb5_as_req_clock_skew);
1060
1061 torture_suite_add_simple_test(kdc_suite,
1062 "as-req-aes",
1063 torture_krb5_as_req_aes);
1064
1065 torture_suite_add_simple_test(kdc_suite,
1066 "as-req-rc4",
1067 torture_krb5_as_req_rc4);
1068
1069 torture_suite_add_simple_test(kdc_suite,
1070 "as-req-aes-rc4",
1071 torture_krb5_as_req_aes_rc4);
1072
1073 /*
1074 * This is in and out of the client.
1075 * Out refers to requests, in refers to replies
1076 */
1077 torture_suite_add_simple_test(kdc_suite,
1078 "as-req-change-server-in",
1079 torture_krb5_as_req_change_server_in);
1080
1081 torture_suite_add_simple_test(kdc_suite,
1082 "as-req-change-server-out",
1083 torture_krb5_as_req_change_server_out);
1084
1085 torture_suite_add_simple_test(kdc_suite,
1086 "as-req-change-server-both",
1087 torture_krb5_as_req_change_server_both);
1088
1089 torture_suite_add_suite(kdc_suite, torture_krb5_canon(kdc_suite));
1090 torture_suite_add_suite(suite, kdc_suite);
1091
1092 torture_register_suite(ctx, suite);
1093 return NT_STATUS_OK;
1094 }