Debian Codegraph @

tests/client/torture_auth.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2010 by Aris Adamantiadis
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "torture.h"
27 #include "libssh/libssh.h"
28 #include "libssh/priv.h"
29 #include "libssh/session.h"
30
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <pwd.h>
34
35 #include "torture_auth_common.c"
36
37 #ifdef WITH_FIDO2
38 #include "torture_sk.h"
39 #endif
40
41 static int sshd_setup(void **state)
42 {
43 torture_setup_sshd_server(state, true);
44
45 return 0;
46 }
47
48 static int sshd_teardown(void **state) {
49 torture_teardown_sshd_server(state);
50
51 return 0;
52 }
53
54 static int session_setup(void **state)
55 {
56 struct torture_state *s = *state;
57 int verbosity = torture_libssh_verbosity();
58 const char *all_keytypes = NULL;
59 struct passwd *pwd;
60 bool b = false;
61 int rc;
62
63 pwd = getpwnam("bob");
64 assert_non_null(pwd);
65
66 rc = setuid(pwd->pw_uid);
67 assert_return_code(rc, errno);
68
69 s->ssh.session = ssh_new();
70 assert_non_null(s->ssh.session);
71
72 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
73 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
74 /* Make sure no other configuration options from system will get used */
75 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b);
76 assert_ssh_return_code(s->ssh.session, rc);
77
78 /* Enable all hostkeys */
79 all_keytypes = ssh_get_supported_methods(SSH_HOSTKEYS);
80 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES, all_keytypes);
81 assert_ssh_return_code(s->ssh.session, rc);
82
83 return 0;
84 }
85
86 static int session_teardown(void **state)
87 {
88 struct torture_state *s = *state;
89
90 ssh_disconnect(s->ssh.session);
91 ssh_free(s->ssh.session);
92
93 return 0;
94 }
95
96 static int pubkey_setup(void **state)
97 {
98 int rc;
99
100 rc = session_setup(state);
101 if (rc != 0) {
102 return rc;
103 }
104
105 /* Make sure we do not interfere with another ssh-agent */
106 unsetenv("SSH_AUTH_SOCK");
107 unsetenv("SSH_AGENT_PID");
108
109 return 0;
110 }
111
112 static int agent_setup(void **state)
113 {
114 struct torture_state *s = *state;
115 struct passwd *pwd;
116 char ssh_key_path[1024];
117 int rc;
118
119 rc = pubkey_setup(state);
120 if (rc != 0) {
121 return rc;
122 }
123
124 pwd = getpwnam("bob");
125 assert_non_null(pwd);
126
127 /* Use the common function to set up the SSH agent with Bob's key */
128 snprintf(ssh_key_path, sizeof(ssh_key_path), "%s/.ssh/id_rsa", pwd->pw_dir);
129 rc = torture_setup_ssh_agent(s, ssh_key_path);
130 if (rc != 0) {
131 return rc;
132 }
133
134 return 0;
135 }
136
137 static int agent_teardown(void **state)
138 {
139 int rc;
140
141 rc = session_teardown(state);
142 if (rc != 0) {
143 return rc;
144 }
145
146 /* Use the common function to clean up the SSH agent */
147 rc = torture_cleanup_ssh_agent();
148 if (rc != 0) {
149 return rc;
150 }
151
152 return 0;
153 }
154
155 static void torture_auth_none(void **state) {
156 struct torture_state *s = *state;
157 ssh_session session = s->ssh.session;
158 int rc;
159
160 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
161 assert_int_equal(rc, SSH_OK);
162
163 rc = ssh_connect(session);
164 assert_int_equal(rc, SSH_OK);
165
166 rc = ssh_userauth_none(session,NULL);
167 assert_int_equal(rc, SSH_AUTH_DENIED);
168
169 /* This request should return a SSH_REQUEST_DENIED error */
170 if (rc == SSH_ERROR) {
171 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
172 }
173 }
174
175 static void torture_auth_none_nonblocking(void **state) {
176 struct torture_state *s = *state;
177 ssh_session session = s->ssh.session;
178 int rc;
179
180 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
181 assert_int_equal(rc, SSH_OK);
182
183 rc = ssh_connect(session);
184 assert_int_equal(rc, SSH_OK);
185
186 /* This request should return a SSH_REQUEST_DENIED error */
187 if (rc == SSH_ERROR) {
188 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
189 }
190
191 ssh_set_blocking(session,0);
192
193 do {
194 rc = ssh_userauth_none(session,NULL);
195 } while (rc == SSH_AUTH_AGAIN);
196 assert_int_equal(rc, SSH_AUTH_DENIED);
197 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
198
199 }
200
201 /* Setting MaxAuthTries 0 makes libssh hang. The option is not practical,
202 * but simulates setting low value and requiring multiple authentication
203 * methods to succeed (T233)
204 */
205 static void torture_auth_none_max_tries(void **state) {
206 struct torture_state *s = *state;
207 ssh_session session = s->ssh.session;
208 int rc;
209 const char *sshd_config = "MaxAuthTries 0";
210
211 torture_update_sshd_config(state, sshd_config);
212
213 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
214 assert_int_equal(rc, SSH_OK);
215
216 rc = ssh_connect(session);
217 assert_int_equal(rc, SSH_OK);
218
219 rc = ssh_userauth_none(session,NULL);
220 assert_int_equal(rc, SSH_AUTH_DENIED);
221
222 /* This request should return a SSH_REQUEST_DENIED error */
223 if (rc == SSH_ERROR) {
224 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
225 }
226
227 /* Reset config back to defaults */
228 torture_update_sshd_config(state, "");
229 }
230
231
232 static void torture_auth_pubkey(void **state) {
233 struct torture_state *s = *state;
234 ssh_session session = s->ssh.session;
235 char bob_ssh_key[1024];
236 ssh_key privkey = NULL;
237 struct passwd *pwd = NULL;
238 int rc;
239
240 pwd = getpwnam("bob");
241 assert_non_null(pwd);
242
243 snprintf(bob_ssh_key,
244 sizeof(bob_ssh_key),
245 "%s/.ssh/id_rsa",
246 pwd->pw_dir);
247
248 /* Authenticate as alice with bob his pubkey */
249 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
250 assert_int_equal(rc, SSH_OK);
251
252 rc = ssh_connect(session);
253 assert_int_equal(rc, SSH_OK);
254
255 rc = ssh_userauth_none(session, NULL);
256 /* This request should return a SSH_REQUEST_DENIED error */
257 if (rc == SSH_ERROR) {
258 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
259 }
260
261 rc = ssh_userauth_list(session, NULL);
262 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
263
264 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
265 assert_int_equal(rc, SSH_OK);
266
267 /* negative tests */
268 rc = ssh_userauth_try_publickey(NULL, NULL, privkey);
269 assert_int_equal(rc, SSH_AUTH_ERROR);
270 rc = ssh_userauth_try_publickey(session, NULL, NULL);
271 assert_int_equal(rc, SSH_AUTH_ERROR);
272
273 rc = ssh_userauth_try_publickey(session, NULL, privkey);
274 assert_int_equal(rc, SSH_AUTH_SUCCESS);
275
276 /* negative tests */
277 rc = ssh_userauth_publickey(NULL, NULL, privkey);
278 assert_int_equal(rc, SSH_AUTH_ERROR);
279 rc = ssh_userauth_publickey(session, NULL, NULL);
280 assert_int_equal(rc, SSH_AUTH_ERROR);
281
282 rc = ssh_userauth_publickey(session, NULL, privkey);
283 assert_int_equal(rc, SSH_AUTH_SUCCESS);
284
285 SSH_KEY_FREE(privkey);
286 }
287
288 static void torture_auth_pubkey_nonblocking(void **state) {
289 struct torture_state *s = *state;
290 ssh_session session = s->ssh.session;
291 char bob_ssh_key[1024];
292 ssh_key privkey = NULL;
293 struct passwd *pwd = NULL;
294 int rc;
295
296 pwd = getpwnam("bob");
297 assert_non_null(pwd);
298
299 snprintf(bob_ssh_key,
300 sizeof(bob_ssh_key),
301 "%s/.ssh/id_rsa",
302 pwd->pw_dir);
303
304 /* Authenticate as alice with bob his pubkey */
305 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
306 assert_int_equal(rc, SSH_OK);
307
308 rc = ssh_connect(session);
309 assert_int_equal(rc, SSH_OK);
310
311 ssh_set_blocking(session, 0);
312
313 do {
314 rc = ssh_userauth_none(session,NULL);
315 } while (rc == SSH_AUTH_AGAIN);
316 assert_int_equal(rc, SSH_AUTH_DENIED);
317 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
318
319 rc = ssh_userauth_list(session, NULL);
320 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
321
322 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
323 assert_int_equal(rc, SSH_OK);
324
325 do {
326 rc = ssh_userauth_try_publickey(session, NULL, privkey);
327 } while (rc == SSH_AUTH_AGAIN);
328 assert_int_equal(rc, SSH_AUTH_SUCCESS);
329
330 do {
331 rc = ssh_userauth_publickey(session, NULL, privkey);
332 } while (rc == SSH_AUTH_AGAIN);
333 assert_int_equal(rc, SSH_AUTH_SUCCESS);
334
335 SSH_KEY_FREE(privkey);
336 }
337
338 static void torture_auth_autopubkey(void **state) {
339 struct torture_state *s = *state;
340 ssh_session session = s->ssh.session;
341 int rc;
342
343 /* Authenticate as alice with bob his pubkey */
344 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
345 assert_int_equal(rc, SSH_OK);
346
347 rc = ssh_connect(session);
348 assert_int_equal(rc, SSH_OK);
349
350 rc = ssh_userauth_none(session,NULL);
351 /* This request should return a SSH_REQUEST_DENIED error */
352 if (rc == SSH_ERROR) {
353 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
354 }
355 rc = ssh_userauth_list(session, NULL);
356 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
357
358 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
359 assert_int_equal(rc, SSH_AUTH_SUCCESS);
360 }
361
362 struct torture_auth_autopubkey_protected_data {
363 ssh_session session;
364 int n_calls;
365 };
366
367 static int
368 torture_auth_autopubkey_protected_auth_function (const char *prompt, char *buf, size_t len,
369 int echo, int verify, void *userdata)
370 {
371 int rc;
372 char *id, *expected_id;
373 struct torture_auth_autopubkey_protected_data *data = userdata;
374
375 assert_true(prompt != NULL);
376 assert_int_equal(echo, 0);
377 assert_int_equal(verify, 0);
378
379 expected_id = ssh_path_expand_escape(data->session, "%d/.ssh/id_rsa_protected");
380 assert_true(expected_id != NULL);
381
382 rc = ssh_userauth_publickey_auto_get_current_identity(data->session, &id);
383 assert_int_equal(rc, SSH_OK);
384
385 assert_string_equal(expected_id, id);
386
387 ssh_string_free_char(id);
388 ssh_string_free_char(expected_id);
389
390 data->n_calls += 1;
391 strncpy(buf, "secret", len);
392 return 0;
393 }
394
395 static void torture_auth_autopubkey_protected(void **state) {
396 struct torture_state *s = *state;
397 ssh_session session = s->ssh.session;
398 char *id;
399 int rc;
400
401 struct torture_auth_autopubkey_protected_data data = {
402 .session = session,
403 .n_calls = 0
404 };
405
406 struct ssh_callbacks_struct callbacks = {
407 .userdata = &data,
408 .auth_function = torture_auth_autopubkey_protected_auth_function
409 };
410
411 /* no session pointer */
412 rc = ssh_userauth_publickey_auto_get_current_identity(NULL, &id);
413 assert_int_equal(rc, SSH_ERROR);
414
415 /* no result pointer */
416 rc = ssh_userauth_publickey_auto_get_current_identity(session, NULL);
417 assert_int_equal(rc, SSH_ERROR);
418
419 /* no auto auth going on */
420 rc = ssh_userauth_publickey_auto_get_current_identity(session, &id);
421 assert_int_equal(rc, SSH_ERROR);
422
423 ssh_callbacks_init(&callbacks);
424 ssh_set_callbacks(session, &callbacks);
425
426 /* Authenticate as alice with bob his pubkey */
427 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
428 assert_int_equal(rc, SSH_OK);
429
430 /* Try id_rsa_protected first.
431 */
432 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "%d/.ssh/id_rsa_protected");
433 assert_int_equal(rc, SSH_OK);
434
435 rc = ssh_connect(session);
436 assert_int_equal(rc, SSH_OK);
437
438 rc = ssh_userauth_none(session,NULL);
439 /* This request should return a SSH_REQUEST_DENIED error */
440 if (rc == SSH_ERROR) {
441 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
442 }
443 rc = ssh_userauth_list(session, NULL);
444 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
445
446 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
447 assert_int_equal(rc, SSH_AUTH_SUCCESS);
448
449 assert_int_equal (data.n_calls, 1);
450 }
451
452 static void torture_auth_autopubkey_nonblocking(void **state) {
453 struct torture_state *s = *state;
454 ssh_session session = s->ssh.session;
455 int rc;
456
457 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
458 assert_int_equal(rc, SSH_OK);
459
460 rc = ssh_connect(session);
461 assert_int_equal(rc, SSH_OK);
462
463 ssh_set_blocking(session,0);
464 do {
465 rc = ssh_userauth_none(session, NULL);
466 } while (rc == SSH_AUTH_AGAIN);
467
468 /* This request should return a SSH_REQUEST_DENIED error */
469 if (rc == SSH_ERROR) {
470 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
471 }
472
473 rc = ssh_userauth_list(session, NULL);
474 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
475
476 do {
477 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
478 } while (rc == SSH_AUTH_AGAIN);
479 assert_int_equal(rc, SSH_AUTH_SUCCESS);
480 }
481
482 static void
483 torture_auth_kbdint(void **state,
484 const char *password,
485 enum ssh_auth_e res)
486 {
487 struct torture_state *s = *state;
488 ssh_session session = s->ssh.session;
489 int rc;
490
491 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
492 assert_int_equal(rc, SSH_OK);
493
494 rc = ssh_connect(session);
495 assert_int_equal(rc, SSH_OK);
496
497 rc = ssh_userauth_none(session,NULL);
498 /* This request should return a SSH_REQUEST_DENIED error */
499 if (rc == SSH_ERROR) {
500 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
501 }
502 rc = ssh_userauth_list(session, NULL);
503 assert_true(rc & SSH_AUTH_METHOD_INTERACTIVE);
504
505 rc = ssh_userauth_kbdint(session, NULL, NULL);
506 assert_int_equal(rc, SSH_AUTH_INFO);
507 assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 1);
508
509 rc = ssh_userauth_kbdint_setanswer(session, 0, password);
510 assert_false(rc < 0);
511
512 rc = ssh_userauth_kbdint(session, NULL, NULL);
513 /* Sometimes, SSH server send an empty query at the end of exchange */
514 if (rc == SSH_AUTH_INFO) {
515 assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 0);
516 rc = ssh_userauth_kbdint(session, NULL, NULL);
517 }
518 assert_int_equal(rc, res);
519 }
520
521 static void
522 torture_auth_kbdint_good(void **state)
523 {
524 torture_auth_kbdint(state, TORTURE_SSH_USER_BOB_PASSWORD, SSH_AUTH_SUCCESS);
525 }
526
527 static void
528 torture_auth_kbdint_bad(void **state)
529 {
530 torture_auth_kbdint(state, "bad password stample", SSH_AUTH_DENIED);
531 }
532
533 static void
534 torture_auth_kbdint_nonblocking(void **state,
535 const char *password,
536 enum ssh_auth_e res)
537 {
538 struct torture_state *s = *state;
539 ssh_session session = s->ssh.session;
540 int rc;
541
542 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
543 assert_int_equal(rc, SSH_OK);
544
545 rc = ssh_connect(session);
546 assert_int_equal(rc, SSH_OK);
547
548 ssh_set_blocking(session, 0);
549 do {
550 rc = ssh_userauth_none(session, NULL);
551 } while (rc == SSH_AUTH_AGAIN);
552
553 /* This request should return a SSH_REQUEST_DENIED error */
554 if (rc == SSH_ERROR) {
555 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
556 }
557 rc = ssh_userauth_list(session, NULL);
558 assert_true(rc & SSH_AUTH_METHOD_INTERACTIVE);
559
560 do {
561 rc = ssh_userauth_kbdint(session, NULL, NULL);
562 } while (rc == SSH_AUTH_AGAIN);
563 assert_int_equal(rc, SSH_AUTH_INFO);
564 assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 1);
565 rc = ssh_userauth_kbdint_setanswer(session, 0, password);
566 assert_false(rc < 0);
567
568 do {
569 rc = ssh_userauth_kbdint(session, NULL, NULL);
570 } while (rc == SSH_AUTH_AGAIN);
571 /* Sometimes, SSH server send an empty query at the end of exchange */
572 if (rc == SSH_AUTH_INFO) {
573 assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 0);
574 do {
575 rc = ssh_userauth_kbdint(session, NULL, NULL);
576 } while (rc == SSH_AUTH_AGAIN);
577 }
578 assert_int_equal(rc, res);
579 }
580
581 static void
582 torture_auth_kbdint_nonblocking_good(void **state)
583 {
584 torture_auth_kbdint_nonblocking(state,
585 TORTURE_SSH_USER_BOB_PASSWORD,
586 SSH_AUTH_SUCCESS);
587 }
588
589 static void
590 torture_auth_kbdint_nonblocking_bad(void **state)
591 {
592 torture_auth_kbdint_nonblocking(state,
593 "bad password stample",
594 SSH_AUTH_DENIED);
595 }
596
597 static void
598 torture_auth_password(void **state, const char *password, enum ssh_auth_e res)
599 {
600 struct torture_state *s = *state;
601 ssh_session session = s->ssh.session;
602 int rc;
603
604 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
605 assert_int_equal(rc, SSH_OK);
606
607 rc = ssh_connect(session);
608 assert_int_equal(rc, SSH_OK);
609
610 rc = ssh_userauth_none(session, NULL);
611 /* This request should return a SSH_REQUEST_DENIED error */
612 if (rc == SSH_AUTH_ERROR) {
613 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
614 }
615 rc = ssh_userauth_list(session, NULL);
616 assert_true(rc & SSH_AUTH_METHOD_PASSWORD);
617
618 rc = ssh_userauth_password(session, NULL, password);
619 assert_int_equal(rc, res);
620 }
621
622 static void
623 torture_auth_password_good(void **state)
624 {
625 torture_auth_password(state,
626 TORTURE_SSH_USER_BOB_PASSWORD,
627 SSH_AUTH_SUCCESS);
628 }
629
630 static void
631 torture_auth_password_bad(void **state)
632 {
633 torture_auth_password(state, "bad password stample", SSH_AUTH_DENIED);
634 }
635
636 static void
637 torture_auth_password_nonblocking(void **state,
638 const char *password,
639 enum ssh_auth_e res)
640 {
641 struct torture_state *s = *state;
642 ssh_session session = s->ssh.session;
643 int rc;
644
645 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
646 assert_int_equal(rc, SSH_OK);
647
648 rc = ssh_connect(session);
649 assert_int_equal(rc, SSH_OK);
650
651 ssh_set_blocking(session,0);
652 do {
653 rc = ssh_userauth_none(session, NULL);
654 } while (rc == SSH_AUTH_AGAIN);
655
656 /* This request should return a SSH_REQUEST_DENIED error */
657 if (rc == SSH_AUTH_ERROR) {
658 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
659 }
660
661 rc = ssh_userauth_list(session, NULL);
662 assert_true(rc & SSH_AUTH_METHOD_PASSWORD);
663
664 do {
665 rc = ssh_userauth_password(session, NULL, password);
666 } while (rc == SSH_AUTH_AGAIN);
667
668 assert_int_equal(rc, res);
669 }
670
671 static void
672 torture_auth_password_nonblocking_good(void **state)
673 {
674 torture_auth_password_nonblocking(state,
675 TORTURE_SSH_USER_BOB_PASSWORD,
676 SSH_AUTH_SUCCESS);
677 }
678
679 static void
680 torture_auth_password_nonblocking_bad(void **state)
681 {
682 torture_auth_password_nonblocking(state,
683 "bad password stample",
684 SSH_AUTH_DENIED);
685 }
686
687 /* TODO cover the case:
688 * * when there is accompanying certificate (identities only + agent)
689 * * export private key to public key during _auto() authentication.
690 * this needs to be a encrypted private key in PEM format without
691 * accompanying public key.
692 */
693 static void torture_auth_agent_identities_only(void **state)
694 {
695 struct torture_state *s = *state;
696 ssh_session session = s->ssh.session;
697 char bob_ssh_key[1024];
698 struct passwd *pwd = NULL;
699 int rc;
700 bool identities_only = true;
701 char *id = NULL;
702
703 pwd = getpwnam("bob");
704 assert_non_null(pwd);
705
706 snprintf(bob_ssh_key,
707 sizeof(bob_ssh_key),
708 "%s/.ssh/id_rsa",
709 pwd->pw_dir);
710
711 if (!ssh_agent_is_running(session)){
712 print_message("*** Agent not running. Test ignored\n");
713 return;
714 }
715 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
716 assert_int_equal(rc, SSH_OK);
717
718 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
719 assert_int_equal(rc, SSH_OK);
720
721 /* Remove the default identities */
722 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) != NULL) {
723 SAFE_FREE(id);
724 }
725
726 rc = ssh_connect(session);
727 assert_int_equal(rc, SSH_OK);
728
729 rc = ssh_userauth_none(session, NULL);
730 /* This request should return a SSH_REQUEST_DENIED error */
731 if (rc == SSH_ERROR) {
732 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
733 }
734 rc = ssh_userauth_list(session, NULL);
735 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
736
737 /* Should fail as key is not in config */
738 rc = ssh_userauth_agent(session, NULL);
739 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
740
741 /* Re-add a key */
742 rc = ssh_list_append(session->opts.identity, strdup(bob_ssh_key));
743 assert_int_equal(rc, SSH_OK);
744
745 /* Should succeed as key now in config/options */
746 rc = ssh_userauth_agent(session, NULL);
747 assert_ssh_return_code(session, rc);
748 }
749
750 static void torture_auth_agent_identities_only_protected(void **state)
751 {
752 struct torture_state *s = *state;
753 ssh_session session = s->ssh.session;
754 char bob_ssh_key[1024];
755 struct passwd *pwd;
756 int rc;
757 bool identities_only = true;
758 char *id = NULL;
759
760 pwd = getpwnam("bob");
761 assert_non_null(pwd);
762
763 snprintf(bob_ssh_key,
764 sizeof(bob_ssh_key),
765 "%s/.ssh/id_rsa_protected",
766 pwd->pw_dir);
767
768 if (!ssh_agent_is_running(session)){
769 print_message("*** Agent not running. Test ignored\n");
770 return;
771 }
772 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
773 assert_int_equal(rc, SSH_OK);
774
775 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
776 assert_int_equal(rc, SSH_OK);
777
778 /* Remove the default identities */
779 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) != NULL) {
780 SAFE_FREE(id);
781 }
782
783 rc = ssh_connect(session);
784 assert_int_equal(rc, SSH_OK);
785
786 rc = ssh_userauth_none(session, NULL);
787 /* This request should return a SSH_REQUEST_DENIED error */
788 if (rc == SSH_ERROR) {
789 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
790 }
791 rc = ssh_userauth_list(session, NULL);
792 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
793
794 /* Should fail as key is not in config */
795 rc = ssh_userauth_agent(session, NULL);
796 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
797
798 /* Re-add a key */
799 rc = ssh_list_append(session->opts.identity, strdup(bob_ssh_key));
800 assert_int_equal(rc, SSH_OK);
801
802 /* Should succeed as key now in config */
803 rc = ssh_userauth_agent(session, NULL);
804 assert_ssh_return_code(session, rc);
805 }
806
807 static void torture_auth_pubkey_types(void **state)
808 {
809 struct torture_state *s = *state;
810 ssh_session session = s->ssh.session;
811 int rc;
812
813 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
814 assert_ssh_return_code(session, rc);
815
816 rc = ssh_connect(session);
817 assert_ssh_return_code(session, rc);
818
819 rc = ssh_userauth_none(session, NULL);
820 /* This request should return a SSH_REQUEST_DENIED error */
821 if (rc == SSH_ERROR) {
822 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
823 }
824 rc = ssh_userauth_list(session, NULL);
825 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
826
827 /* Disable RSA key types for authentication */
828 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
829 "ecdsa-sha2-nistp384");
830 assert_ssh_return_code(session, rc);
831
832 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
833 assert_int_equal(rc, SSH_AUTH_DENIED);
834
835 /* Now enable it and retry */
836 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
837 "rsa-sha2-512,ssh-rsa");
838 assert_ssh_return_code(session, rc);
839
840 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
841 assert_int_equal(rc, SSH_AUTH_SUCCESS);
842 }
843
844 static void torture_auth_pubkey_types_ecdsa(void **state)
845 {
846 struct torture_state *s = *state;
847 ssh_session session = s->ssh.session;
848 int rc;
849
850 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
851 assert_ssh_return_code(session, rc);
852
853 rc = ssh_connect(session);
854 assert_ssh_return_code(session, rc);
855
856 rc = ssh_userauth_none(session, NULL);
857 /* This request should return a SSH_REQUEST_DENIED error */
858 if (rc == SSH_ERROR) {
859 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
860 }
861 rc = ssh_userauth_list(session, NULL);
862 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
863
864 /* We have only the 256b key -- allowlisting only larger should fail */
865 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
866 "ecdsa-sha2-nistp384");
867 assert_ssh_return_code(session, rc);
868
869 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
870 assert_int_equal(rc, SSH_AUTH_DENIED);
871
872 /* Verify we can use also ECDSA keys with their various names */
873 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
874 "ecdsa-sha2-nistp256");
875 assert_ssh_return_code(session, rc);
876
877 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
878 assert_int_equal(rc, SSH_AUTH_SUCCESS);
879
880 }
881
882 static void torture_auth_pubkey_types_ed25519(void **state)
883 {
884 struct torture_state *s = *state;
885 ssh_session session = s->ssh.session;
886 char bob_ssh_key[1024];
887 ssh_key privkey = NULL;
888 struct passwd *pwd;
889 int rc;
890
891 if (ssh_fips_mode()) {
892 skip();
893 }
894
895 pwd = getpwnam("bob");
896 assert_non_null(pwd);
897
898 snprintf(bob_ssh_key,
899 sizeof(bob_ssh_key),
900 "%s/.ssh/id_ed25519",
901 pwd->pw_dir);
902
903 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
904 assert_ssh_return_code(session, rc);
905
906 rc = ssh_connect(session);
907 assert_ssh_return_code(session, rc);
908
909 rc = ssh_userauth_none(session, NULL);
910 /* This request should return a SSH_REQUEST_DENIED error */
911 if (rc == SSH_ERROR) {
912 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
913 }
914 rc = ssh_userauth_list(session, NULL);
915 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
916
917 /* Import the ED25519 private key */
918 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
919 assert_int_equal(rc, SSH_OK);
920
921 /* Enable only RSA keys -- authentication should fail */
922 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
923 "ssh-rsa");
924 assert_ssh_return_code(session, rc);
925
926 rc = ssh_userauth_publickey(session, NULL, privkey);
927 assert_int_equal(rc, SSH_AUTH_DENIED);
928
929 /* Verify we can use also ed25519 keys */
930 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
931 "ssh-ed25519");
932 assert_ssh_return_code(session, rc);
933
934 rc = ssh_userauth_publickey(session, NULL, privkey);
935 assert_int_equal(rc, SSH_AUTH_SUCCESS);
936
937 SSH_KEY_FREE(privkey);
938 }
939
940 static void torture_auth_pubkey_types_nonblocking(void **state)
941 {
942 struct torture_state *s = *state;
943 ssh_session session = s->ssh.session;
944 int rc;
945
946 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
947 assert_ssh_return_code(session, rc);
948
949 rc = ssh_connect(session);
950 assert_ssh_return_code(session, rc);
951
952 ssh_set_blocking(session, 0);
953 do {
954 rc = ssh_userauth_none(session, NULL);
955 } while (rc == SSH_AUTH_AGAIN);
956
957 /* This request should return a SSH_REQUEST_DENIED error */
958 if (rc == SSH_ERROR) {
959 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
960 }
961
962 rc = ssh_userauth_list(session, NULL);
963 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
964
965 /* Disable RSA key types for authentication */
966 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
967 "ecdsa-sha2-nistp521");
968 assert_ssh_return_code(session, rc);
969
970 do {
971 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
972 } while (rc == SSH_AUTH_AGAIN);
973 assert_int_equal(rc, SSH_AUTH_DENIED);
974
975 /* Now enable it and retry */
976 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
977 "rsa-sha2-512,ssh-rsa");
978 assert_ssh_return_code(session, rc);
979
980 do {
981 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
982 } while (rc == SSH_AUTH_AGAIN);
983 assert_int_equal(rc, SSH_AUTH_SUCCESS);
984
985 }
986
987 static void torture_auth_pubkey_types_ecdsa_nonblocking(void **state)
988 {
989 struct torture_state *s = *state;
990 ssh_session session = s->ssh.session;
991 int rc;
992
993 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
994 assert_ssh_return_code(session, rc);
995
996 rc = ssh_connect(session);
997 assert_ssh_return_code(session, rc);
998
999 ssh_set_blocking(session, 0);
1000 do {
1001 rc = ssh_userauth_none(session, NULL);
1002 } while (rc == SSH_AUTH_AGAIN);
1003
1004 /* This request should return a SSH_REQUEST_DENIED error */
1005 if (rc == SSH_ERROR) {
1006 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
1007 }
1008
1009 rc = ssh_userauth_list(session, NULL);
1010 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
1011
1012 /* We have only the 256b key -- allowlisting only larger should fail */
1013 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
1014 "ecdsa-sha2-nistp384");
1015 assert_ssh_return_code(session, rc);
1016
1017 do {
1018 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
1019 } while (rc == SSH_AUTH_AGAIN);
1020 assert_int_equal(rc, SSH_AUTH_DENIED);
1021
1022 /* Verify we can use also ECDSA key to authenticate */
1023 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
1024 "ecdsa-sha2-nistp256");
1025 assert_ssh_return_code(session, rc);
1026
1027 do {
1028 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
1029 } while (rc == SSH_AUTH_AGAIN);
1030 assert_int_equal(rc, SSH_AUTH_SUCCESS);
1031
1032 }
1033
1034 static void torture_auth_pubkey_types_ed25519_nonblocking(void **state)
1035 {
1036 struct torture_state *s = *state;
1037 ssh_session session = s->ssh.session;
1038 char bob_ssh_key[1024];
1039 ssh_key privkey = NULL;
1040 struct passwd *pwd;
1041 int rc;
1042
1043 if (ssh_fips_mode()) {
1044 skip();
1045 }
1046
1047 pwd = getpwnam("bob");
1048 assert_non_null(pwd);
1049
1050 snprintf(bob_ssh_key,
1051 sizeof(bob_ssh_key),
1052 "%s/.ssh/id_ed25519",
1053 pwd->pw_dir);
1054
1055 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
1056 assert_ssh_return_code(session, rc);
1057
1058 rc = ssh_connect(session);
1059 assert_ssh_return_code(session, rc);
1060
1061 ssh_set_blocking(session, 0);
1062 do {
1063 rc = ssh_userauth_none(session, NULL);
1064 } while (rc == SSH_AUTH_AGAIN);
1065
1066 /* This request should return a SSH_REQUEST_DENIED error */
1067 if (rc == SSH_ERROR) {
1068 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
1069 }
1070
1071 rc = ssh_userauth_list(session, NULL);
1072 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
1073
1074 /* Import the ED25519 private key */
1075 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
1076 assert_int_equal(rc, SSH_OK);
1077
1078 /* Enable only RSA keys -- authentication should fail */
1079 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
1080 "ssh-rsa");
1081 assert_ssh_return_code(session, rc);
1082
1083 do {
1084 rc = ssh_userauth_publickey(session, NULL, privkey);
1085 } while (rc == SSH_AUTH_AGAIN);
1086 assert_int_equal(rc, SSH_AUTH_DENIED);
1087
1088 /* Verify we can use also ED25519 key to authenticate */
1089 rc = ssh_options_set(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
1090 "ssh-ed25519");
1091 assert_ssh_return_code(session, rc);
1092
1093 do {
1094 rc = ssh_userauth_publickey(session, NULL, privkey);
1095 } while (rc == SSH_AUTH_AGAIN);
1096 assert_int_equal(rc, SSH_AUTH_SUCCESS);
1097
1098 SSH_KEY_FREE(privkey);
1099 }
1100
1101 #ifdef WITH_FIDO2
1102
1103 static void torture_auth_pubkey_types_sk_key(void **state,
1104 enum ssh_keytypes_e key_type)
1105 {
1106 struct torture_state *s = *state;
1107 ssh_session session = s->ssh.session;
1108 const char *privkey_file_name = NULL;
1109 const char *key_type_str = NULL;
1110 char bob_ssh_key[1024];
1111 ssh_key privkey = NULL;
1112 struct passwd *pwd = NULL;
1113 const struct ssh_sk_callbacks_struct *sk_dummy_callbacks = NULL;
1114 ssh_pki_ctx pki_context = NULL;
1115 int rc;
1116
1117 /* Conditions to skip the test */
1118 sk_dummy_callbacks = torture_get_sk_dummy_callbacks();
1119 if (sk_dummy_callbacks == NULL) {
1120 skip();
1121 }
1122
1123 if (key_type == SSH_KEYTYPE_SK_ED25519 && ssh_fips_mode()) {
1124 skip();
1125 }
1126
1127 /* Key type specific setup */
1128 switch (key_type) {
1129 case SSH_KEYTYPE_SK_ECDSA:
1130 privkey_file_name = "id_ecdsa_sk";
1131 key_type_str = "[email protected]";
1132 break;
1133
1134 case SSH_KEYTYPE_SK_ED25519:
1135 privkey_file_name = "id_ed25519_sk";
1136 key_type_str = "[email protected]";
1137 break;
1138
1139 default:
1140 /* should never reach here */
1141 assert_true(0);
1142 }
1143
1144 pwd = getpwnam("bob");
1145 assert_non_null(pwd);
1146
1147 snprintf(bob_ssh_key,
1148 sizeof(bob_ssh_key),
1149 "%s/.ssh/%s",
1150 pwd->pw_dir,
1151 privkey_file_name);
1152
1153 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
1154 assert_ssh_return_code(session, rc);
1155
1156 pki_context = ssh_pki_ctx_new();
1157 assert_non_null(pki_context);
1158
1159 rc = ssh_pki_ctx_options_set(pki_context,
1160 SSH_PKI_OPTION_SK_CALLBACKS,
1161 sk_dummy_callbacks);
1162 assert_int_equal(rc, SSH_OK);
1163
1164 rc = ssh_options_set(session, SSH_OPTIONS_PKI_CONTEXT, pki_context);
1165 assert_int_equal(rc, SSH_OK);
1166
1167 rc = ssh_connect(session);
1168 assert_ssh_return_code(session, rc);
1169
1170 rc = ssh_userauth_none(session, NULL);
1171
1172 /* This request should return a SSH_REQUEST_DENIED error */
1173 if (rc == SSH_ERROR) {
1174 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
1175 }
1176
1177 rc = ssh_userauth_list(session, NULL);
1178 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
1179
1180 /* Import the private key */
1181 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
1182 assert_int_equal(rc, SSH_OK);
1183
1184 /* Enable only RSA keys -- authentication should fail */
1185 rc = ssh_options_set(session,
1186 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
1187 "ssh-rsa");
1188 assert_ssh_return_code(session, rc);
1189
1190 rc = ssh_userauth_publickey(session, NULL, privkey);
1191 assert_int_equal(rc, SSH_AUTH_DENIED);
1192
1193 /* Verify we can use the SK key */
1194 rc = ssh_options_set(session,
1195 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
1196 key_type_str);
1197 assert_ssh_return_code(session, rc);
1198
1199 rc = ssh_userauth_publickey(session, NULL, privkey);
1200 assert_int_equal(rc, SSH_AUTH_SUCCESS);
1201
1202 SSH_KEY_FREE(privkey);
1203 SSH_PKI_CTX_FREE(pki_context);
1204 }
1205
1206 static void torture_auth_pubkey_types_sk_ecdsa(void **state)
1207 {
1208 torture_auth_pubkey_types_sk_key(state, SSH_KEYTYPE_SK_ECDSA);
1209 }
1210
1211 static void torture_auth_pubkey_types_sk_ed25519(void **state)
1212 {
1213 torture_auth_pubkey_types_sk_key(state, SSH_KEYTYPE_SK_ED25519);
1214 }
1215
1216 #endif /* WITH_FIDO2 */
1217
1218 static void torture_auth_pubkey_rsa_key_size(void **state)
1219 {
1220 struct torture_state *s = *state;
1221 ssh_session session = s->ssh.session;
1222 char bob_ssh_key[1024];
1223 ssh_key privkey = NULL;
1224 struct passwd *pwd;
1225 int rc;
1226 unsigned int limit = 4096;
1227
1228 pwd = getpwnam("bob");
1229 assert_non_null(pwd);
1230
1231 snprintf(bob_ssh_key,
1232 sizeof(bob_ssh_key),
1233 "%s/.ssh/id_rsa",
1234 pwd->pw_dir);
1235
1236 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
1237 assert_ssh_return_code(session, rc);
1238
1239 rc = ssh_connect(session);
1240 assert_ssh_return_code(session, rc);
1241
1242 rc = ssh_userauth_none(session, NULL);
1243 /* This request should return a SSH_REQUEST_DENIED error */
1244 if (rc == SSH_ERROR) {
1245 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
1246 }
1247 rc = ssh_userauth_list(session, NULL);
1248 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
1249
1250 /* set unreasonable large minimum key size to trigger the condition */
1251 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &limit); /* larger than the test key */
1252 assert_ssh_return_code(session, rc);
1253
1254 /* Import the RSA private key */
1255 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
1256 assert_int_equal(rc, SSH_OK);
1257
1258 rc = ssh_userauth_publickey(session, NULL, privkey);
1259 assert_int_equal(rc, SSH_AUTH_DENIED);
1260
1261 /* revert to default values which should work also in FIPS mode */
1262 limit = 0;
1263 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &limit);
1264 assert_ssh_return_code(session, rc);
1265
1266 rc = ssh_userauth_publickey(session, NULL, privkey);
1267 assert_int_equal(rc, SSH_AUTH_SUCCESS);
1268
1269 SSH_KEY_FREE(privkey);
1270 }
1271
1272 static void torture_auth_pubkey_rsa_key_size_nonblocking(void **state)
1273 {
1274 struct torture_state *s = *state;
1275 ssh_session session = s->ssh.session;
1276 char bob_ssh_key[1024];
1277 ssh_key privkey = NULL;
1278 struct passwd *pwd;
1279 int rc;
1280 unsigned int limit = 4096;
1281
1282 pwd = getpwnam("bob");
1283 assert_non_null(pwd);
1284
1285 snprintf(bob_ssh_key,
1286 sizeof(bob_ssh_key),
1287 "%s/.ssh/id_rsa",
1288 pwd->pw_dir);
1289
1290 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
1291 assert_ssh_return_code(session, rc);
1292
1293 rc = ssh_connect(session);
1294 assert_ssh_return_code(session, rc);
1295
1296 ssh_set_blocking(session, 0);
1297 do {
1298 rc = ssh_userauth_none(session, NULL);
1299 } while (rc == SSH_AUTH_AGAIN);
1300
1301 /* This request should return a SSH_REQUEST_DENIED error */
1302 if (rc == SSH_ERROR) {
1303 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
1304 }
1305
1306 rc = ssh_userauth_list(session, NULL);
1307 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
1308
1309 /* set unreasonable large minimum key size to trigger the condition */
1310 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &limit); /* larger than the test key */
1311 assert_ssh_return_code(session, rc);
1312
1313 /* Import the RSA private key */
1314 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
1315 assert_int_equal(rc, SSH_OK);
1316
1317 do {
1318 rc = ssh_userauth_publickey(session, NULL, privkey);
1319 } while (rc == SSH_AUTH_AGAIN);
1320 assert_int_equal(rc, SSH_AUTH_DENIED);
1321
1322 /* revert to default values which should work also in FIPS mode */
1323 limit = 0;
1324 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &limit);
1325 assert_ssh_return_code(session, rc);
1326
1327 do {
1328 rc = ssh_userauth_publickey(session, NULL, privkey);
1329 } while (rc == SSH_AUTH_AGAIN);
1330 assert_int_equal(rc, SSH_AUTH_SUCCESS);
1331
1332 SSH_KEY_FREE(privkey);
1333 }
1334
1335 static void torture_auth_pubkey_skip_none(void **state)
1336 {
1337 struct torture_state *s = *state;
1338 ssh_session session = s->ssh.session;
1339 char bob_ssh_key[1024];
1340 ssh_key privkey = NULL;
1341 struct passwd *pwd = NULL;
1342 int rc;
1343
1344 pwd = getpwnam("bob");
1345 assert_non_null(pwd);
1346
1347 snprintf(bob_ssh_key, sizeof(bob_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
1348
1349 /* Authenticate as alice with bob his pubkey */
1350 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
1351 assert_int_equal(rc, SSH_OK);
1352
1353 rc = ssh_connect(session);
1354 assert_int_equal(rc, SSH_OK);
1355
1356 /* Skip the ssh_userauth_none() here */
1357
1358 rc = ssh_pki_import_privkey_file(bob_ssh_key, NULL, NULL, NULL, &privkey);
1359 assert_int_equal(rc, SSH_OK);
1360
1361 rc = ssh_userauth_publickey(session, NULL, privkey);
1362 assert_int_equal(rc, SSH_AUTH_SUCCESS);
1363
1364 SSH_KEY_FREE(privkey);
1365 }
1366
1367 int torture_run_tests(void) {
1368 int rc;
1369 struct CMUnitTest tests[] = {
1370 cmocka_unit_test_setup_teardown(torture_auth_none,
1371 session_setup,
1372 session_teardown),
1373 cmocka_unit_test_setup_teardown(torture_auth_none_nonblocking,
1374 session_setup,
1375 session_teardown),
1376 cmocka_unit_test_setup_teardown(torture_auth_none_max_tries,
1377 session_setup,
1378 session_teardown),
1379 cmocka_unit_test_setup_teardown(torture_auth_password_good,
1380 session_setup,
1381 session_teardown),
1382 cmocka_unit_test_setup_teardown(torture_auth_password_nonblocking_good,
1383 session_setup,
1384 session_teardown),
1385 cmocka_unit_test_setup_teardown(torture_auth_password_bad,
1386 session_setup,
1387 session_teardown),
1388 cmocka_unit_test_setup_teardown(torture_auth_password_nonblocking_bad,
1389 session_setup,
1390 session_teardown),
1391 cmocka_unit_test_setup_teardown(torture_auth_kbdint_good,
1392 session_setup,
1393 session_teardown),
1394 cmocka_unit_test_setup_teardown(torture_auth_kbdint_nonblocking_good,
1395 session_setup,
1396 session_teardown),
1397 cmocka_unit_test_setup_teardown(torture_auth_kbdint_bad,
1398 session_setup,
1399 session_teardown),
1400 cmocka_unit_test_setup_teardown(torture_auth_kbdint_nonblocking_bad,
1401 session_setup,
1402 session_teardown),
1403 cmocka_unit_test_setup_teardown(torture_auth_pubkey,
1404 pubkey_setup,
1405 session_teardown),
1406 cmocka_unit_test_setup_teardown(torture_auth_pubkey_nonblocking,
1407 pubkey_setup,
1408 session_teardown),
1409 cmocka_unit_test_setup_teardown(torture_auth_autopubkey,
1410 pubkey_setup,
1411 session_teardown),
1412 cmocka_unit_test_setup_teardown(torture_auth_autopubkey_protected,
1413 pubkey_setup,
1414 session_teardown),
1415 cmocka_unit_test_setup_teardown(torture_auth_autopubkey_nonblocking,
1416 pubkey_setup,
1417 session_teardown),
1418 cmocka_unit_test_setup_teardown(torture_auth_agent,
1419 agent_setup,
1420 agent_teardown),
1421 cmocka_unit_test_setup_teardown(torture_auth_agent_nonblocking,
1422 agent_setup,
1423 agent_teardown),
1424 cmocka_unit_test_setup_teardown(torture_auth_agent_identities_only,
1425 agent_setup,
1426 agent_teardown),
1427 cmocka_unit_test_setup_teardown(torture_auth_agent_identities_only_protected,
1428 agent_setup,
1429 agent_teardown),
1430 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types,
1431 pubkey_setup,
1432 session_teardown),
1433 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types_nonblocking,
1434 pubkey_setup,
1435 session_teardown),
1436 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types_ecdsa,
1437 pubkey_setup,
1438 session_teardown),
1439 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types_ecdsa_nonblocking,
1440 pubkey_setup,
1441 session_teardown),
1442 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types_ed25519,
1443 pubkey_setup,
1444 session_teardown),
1445 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types_ed25519_nonblocking,
1446 pubkey_setup,
1447 session_teardown),
1448 #ifdef WITH_FIDO2
1449 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types_sk_ecdsa,
1450 pubkey_setup,
1451 session_teardown),
1452 cmocka_unit_test_setup_teardown(torture_auth_pubkey_types_sk_ed25519,
1453 pubkey_setup,
1454 session_teardown),
1455 #endif /* WITH_FIDO2 */
1456 cmocka_unit_test_setup_teardown(torture_auth_pubkey_rsa_key_size,
1457 pubkey_setup,
1458 session_teardown),
1459 cmocka_unit_test_setup_teardown(torture_auth_pubkey_rsa_key_size_nonblocking,
1460 pubkey_setup,
1461 session_teardown),
1462 cmocka_unit_test_setup_teardown(torture_auth_pubkey_skip_none,
1463 pubkey_setup,
1464 session_teardown),
1465 };
1466
1467 ssh_init();
1468 torture_filter_tests(tests);
1469 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
1470 ssh_finalize();
1471
1472 return rc;
1473 }