Debian Codegraph @

tests/client/torture_auth_cert.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2010 by Aris Adamantiadis
5 * Copyright (c) 2023 by Jakub Jelen
6 *
7 * The SSH Library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at your
10 * option) any later version.
11 *
12 * The SSH Library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the SSH Library; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA.
21 */
22
23 #include "config.h"
24
25 #define LIBSSH_STATIC
26
27 #include "libssh/libssh.h"
28 #include "libssh/priv.h"
29 #include "libssh/session.h"
30 #include "torture.h"
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <pwd.h>
35 #include <sys/types.h>
36
37 #include "torture_auth_common.c"
38
39 static int sshd_setup(void **state)
40 {
41 torture_setup_sshd_server(state, true);
42
43 return 0;
44 }
45
46 static int sshd_teardown(void **state)
47 {
48 torture_teardown_sshd_server(state);
49
50 return 0;
51 }
52
53 static int session_setup(void **state)
54 {
55 struct torture_state *s = *state;
56 int verbosity = torture_libssh_verbosity();
57 const char *all_keytypes = NULL;
58 struct passwd *pwd = NULL;
59 bool b = false;
60 int rc;
61
62 pwd = getpwnam("doe");
63 assert_non_null(pwd);
64
65 rc = setuid(pwd->pw_uid);
66 assert_return_code(rc, errno);
67
68 s->ssh.session = ssh_new();
69 assert_non_null(s->ssh.session);
70
71 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
72 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
73 /* Make sure no other configuration options from system will get used */
74 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b);
75 assert_ssh_return_code(s->ssh.session, rc);
76
77 /* Enable all hostkeys */
78 all_keytypes = ssh_get_supported_methods(SSH_HOSTKEYS);
79 rc = ssh_options_set(s->ssh.session,
80 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
81 all_keytypes);
82 assert_ssh_return_code(s->ssh.session, rc);
83
84 /* certs have been signed for login as alice */
85 rc = ssh_options_set(s->ssh.session,
86 SSH_OPTIONS_USER,
87 TORTURE_SSH_USER_ALICE);
88 assert_int_equal(rc, SSH_OK);
89
90 /* Make sure we do not interfere with another ssh-agent */
91 unsetenv("SSH_AUTH_SOCK");
92 unsetenv("SSH_AGENT_PID");
93
94 return 0;
95 }
96
97 /* This sets up the ssh session in the directory without the default
98 * certificates that are used for authentication, requiring them to be provided
99 * as configuration options or from agent explicitly. */
100 static int session_setup_ssh_dir(void **state)
101 {
102 struct torture_state *s = *state;
103
104 session_setup(state);
105
106 s->ssh.session->opts.homedir = strdup("~/.no_ssh");
107
108 return 0;
109 }
110
111 static int session_teardown(void **state)
112 {
113 struct torture_state *s = *state;
114
115 ssh_disconnect(s->ssh.session);
116 ssh_free(s->ssh.session);
117
118 return 0;
119 }
120
121 static int agent_setup(void **state)
122 {
123 struct torture_state *s = *state;
124 struct passwd *pwd;
125 char key_path[1024];
126 int rc;
127
128 rc = session_setup(state);
129 if (rc != 0) {
130 return rc;
131 }
132
133 pwd = getpwnam("doe");
134 assert_non_null(pwd);
135
136 snprintf(key_path, sizeof(key_path), "%s/.ssh/id_rsa", pwd->pw_dir);
137
138 /* run ssh-agent and add the key */
139 rc = torture_setup_ssh_agent(s, key_path);
140 assert_int_equal(rc, 0);
141
142 return 0;
143 }
144
145 static int agent_cert_setup(void **state)
146 {
147 char ssh_key_cmd[1024];
148 struct passwd *pwd;
149 int rc;
150
151 rc = agent_setup(state);
152 if (rc != 0) {
153 return rc;
154 }
155
156 pwd = getpwnam("doe");
157 assert_non_null(pwd);
158
159 /* remove all keys, load alternative key + cert */
160 snprintf(ssh_key_cmd,
161 sizeof(ssh_key_cmd),
162 "ssh-add -D && ssh-add %s/.ssh/id_rsa",
163 pwd->pw_dir);
164
165 rc = system(ssh_key_cmd);
166 assert_return_code(rc, errno);
167
168 return 0;
169 }
170
171 static int agent_cert_setup_explicit(void **state)
172 {
173 char orig_doe_ssh_key[1024];
174 char doe_ssh_key[1024];
175 char keydata[2048];
176 struct passwd *pwd = NULL;
177 int fd;
178 int rc;
179
180 rc = agent_cert_setup(state);
181 if (rc != 0) {
182 return rc;
183 }
184
185 pwd = getpwnam("doe");
186 assert_non_null(pwd);
187
188 snprintf(orig_doe_ssh_key,
189 sizeof(orig_doe_ssh_key),
190 "%s/.ssh/id_rsa",
191 pwd->pw_dir);
192
193 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/my_rsa", pwd->pw_dir);
194
195 /* move the private key away from the default location the certificate can
196 * not be loaded automatically */
197 fd = open(orig_doe_ssh_key, O_RDONLY);
198 assert_true(fd > 0);
199 rc = read(fd, keydata, sizeof(keydata));
200 assert_true(rc > 0);
201 keydata[rc] = '\0';
202 close(fd);
203 torture_write_file(doe_ssh_key, keydata);
204
205 return 0;
206 }
207
208 static int agent_teardown(void **state)
209 {
210 int rc;
211
212 rc = session_teardown(state);
213 if (rc != 0) {
214 return rc;
215 }
216
217 rc = torture_cleanup_ssh_agent();
218 assert_int_equal(rc, 0);
219
220 return 0;
221 }
222
223 static void torture_auth_cert(void **state)
224 {
225 struct torture_state *s = *state;
226 ssh_session session = s->ssh.session;
227 ssh_key privkey = NULL;
228 ssh_key cert = NULL;
229 char doe_ssh_key[1024];
230 char doe_ssh_cert[2048];
231 struct passwd *pwd;
232 int rc;
233
234 pwd = getpwnam("doe");
235 assert_non_null(pwd);
236
237 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
238 snprintf(doe_ssh_cert, sizeof(doe_ssh_cert), "%s-cert.pub", doe_ssh_key);
239
240 rc = ssh_connect(session);
241 assert_int_equal(rc, SSH_OK);
242
243 rc = ssh_pki_import_privkey_file(doe_ssh_key, NULL, NULL, NULL, &privkey);
244 assert_int_equal(rc, SSH_OK);
245
246 rc = ssh_pki_import_cert_file(doe_ssh_cert, &cert);
247 assert_int_equal(rc, SSH_OK);
248
249 rc = ssh_pki_copy_cert_to_privkey(cert, privkey);
250 assert_int_equal(rc, SSH_OK);
251
252 rc = ssh_userauth_try_publickey(session, NULL, cert);
253 assert_ssh_return_code(session, rc);
254
255 rc = ssh_userauth_publickey(session, NULL, privkey);
256 assert_int_equal(rc, SSH_AUTH_SUCCESS);
257
258 SSH_KEY_FREE(privkey);
259 SSH_KEY_FREE(cert);
260 }
261
262 static void torture_auth_cert_nonblocking(void **state)
263 {
264 struct torture_state *s = *state;
265 ssh_session session = s->ssh.session;
266 ssh_key privkey = NULL;
267 ssh_key cert = NULL;
268 char doe_ssh_key[1024];
269 char doe_ssh_cert[2048];
270 struct passwd *pwd;
271 int rc;
272
273 pwd = getpwnam("doe");
274 assert_non_null(pwd);
275
276 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
277 snprintf(doe_ssh_cert, sizeof(doe_ssh_cert), "%s-cert.pub", doe_ssh_key);
278
279 rc = ssh_connect(session);
280 assert_int_equal(rc, SSH_OK);
281
282 ssh_set_blocking(session, 0);
283
284 rc = ssh_pki_import_privkey_file(doe_ssh_key, NULL, NULL, NULL, &privkey);
285 assert_int_equal(rc, SSH_OK);
286
287 rc = ssh_pki_import_cert_file(doe_ssh_cert, &cert);
288 assert_int_equal(rc, SSH_OK);
289
290 rc = ssh_pki_copy_cert_to_privkey(cert, privkey);
291 assert_int_equal(rc, SSH_OK);
292
293 do {
294 rc = ssh_userauth_try_publickey(session, NULL, cert);
295 } while (rc == SSH_AUTH_AGAIN);
296 assert_ssh_return_code(session, rc);
297
298 do {
299 rc = ssh_userauth_publickey(session, NULL, privkey);
300 } while (rc == SSH_AUTH_AGAIN);
301
302 assert_int_equal(rc, SSH_AUTH_SUCCESS);
303
304 SSH_KEY_FREE(privkey);
305 SSH_KEY_FREE(cert);
306 }
307
308 /* Same as torture_auth_cert, but without explicitly loading certificate to the
309 * private key file, keeping libssh to use default cert path when done with
310 * _auto(). */
311 static void torture_auth_cert_default_non_explicit(void **state)
312 {
313 struct torture_state *s = *state;
314 ssh_session session = s->ssh.session;
315 int rc;
316
317 /* the cert is in the default location (~/.ssh/id_rsa-cert.pub) */
318
319 rc = ssh_connect(session);
320 assert_int_equal(rc, SSH_OK);
321
322 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
323 assert_int_equal(rc, SSH_AUTH_SUCCESS);
324 }
325
326 /* Same as torture_auth_cert_nonblocking, but without explicitly loading
327 * certificate to the private key file, keeping libssh to use default cert path
328 * when done with _auto().
329 * Non-blocking version */
330 static void torture_auth_cert_default_non_explicit_nonblocking(void **state)
331 {
332 struct torture_state *s = *state;
333 ssh_session session = s->ssh.session;
334 int rc;
335
336 /* the cert is in the default location (~/.ssh/id_rsa-cert.pub) */
337
338 rc = ssh_connect(session);
339 assert_int_equal(rc, SSH_OK);
340
341 ssh_set_blocking(session, 0);
342
343 do {
344 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
345 } while (rc == SSH_AUTH_AGAIN);
346 assert_int_equal(rc, SSH_AUTH_SUCCESS);
347 }
348
349 /* Sanity test that there are no default identities available and the automatic
350 * pubkey authentication fails without any explicit identities */
351 static void torture_auth_auto_fail(void **state)
352 {
353 struct torture_state *s = *state;
354 ssh_session session = s->ssh.session;
355 int rc;
356
357 rc = ssh_connect(session);
358 assert_int_equal(rc, SSH_OK);
359
360 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
361 assert_int_equal(rc, SSH_AUTH_DENIED);
362 }
363
364 /* Sanity test that there are no default identities available and the automatic
365 * pubkey authentication fails without any explicit identities
366 * Non-blocking version */
367 static void torture_auth_auto_fail_nonblocking(void **state)
368 {
369 struct torture_state *s = *state;
370 ssh_session session = s->ssh.session;
371 int rc;
372
373 rc = ssh_connect(session);
374 assert_int_equal(rc, SSH_OK);
375
376 ssh_set_blocking(session, 0);
377
378 do {
379 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
380 } while (rc == SSH_AUTH_AGAIN);
381 assert_int_equal(rc, SSH_AUTH_DENIED);
382 }
383
384 /* Same as torture_auth_cert, but the home SSH dir does not have any default
385 * identities and they are loaded through the options, only through the private
386 * key path. */
387 static void torture_auth_cert_options_private(void **state)
388 {
389 struct torture_state *s = *state;
390 ssh_session session = s->ssh.session;
391 char doe_ssh_key[1024];
392 struct passwd *pwd;
393 int rc;
394
395 pwd = getpwnam("doe");
396 assert_non_null(pwd);
397
398 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
399
400 /* the cert has default naming relative to the private key (*-cert.pub) */
401 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, doe_ssh_key);
402 assert_int_equal(rc, SSH_OK);
403
404 rc = ssh_connect(session);
405 assert_int_equal(rc, SSH_OK);
406
407 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
408 assert_int_equal(rc, SSH_AUTH_SUCCESS);
409 }
410
411 /* Same as torture_auth_cert, but the home SSH dir does not have any default
412 * identities and they are loaded through the options, only through the private
413 * key path.
414 * Non-blocking version */
415 static void torture_auth_cert_options_private_nonblocking(void **state)
416 {
417 struct torture_state *s = *state;
418 ssh_session session = s->ssh.session;
419 char doe_ssh_key[1024];
420 struct passwd *pwd;
421 int rc;
422
423 pwd = getpwnam("doe");
424 assert_non_null(pwd);
425
426 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
427
428 /* the cert has default naming relative to the private key (*-cert.pub) */
429 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, doe_ssh_key);
430 assert_int_equal(rc, SSH_OK);
431
432 rc = ssh_connect(session);
433 assert_int_equal(rc, SSH_OK);
434
435 ssh_set_blocking(session, 0);
436
437 do {
438 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
439 } while (rc == SSH_AUTH_AGAIN);
440 assert_int_equal(rc, SSH_AUTH_SUCCESS);
441 }
442
443 /* Same as torture_auth_cert, but the home SSH dir does not have any default
444 * identities and they are loaded through the options, also the certificate file
445 */
446 static void torture_auth_cert_options_cert(void **state)
447 {
448 struct torture_state *s = *state;
449 ssh_session session = s->ssh.session;
450 char doe_ssh_key[1024];
451 char doe_ssh_cert[2048];
452 struct passwd *pwd;
453 int rc;
454
455 pwd = getpwnam("doe");
456 assert_non_null(pwd);
457
458 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
459 snprintf(doe_ssh_cert, sizeof(doe_ssh_cert), "%s-cert.pub", doe_ssh_key);
460
461 /* Explicit private key and cert */
462 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, doe_ssh_key);
463 assert_int_equal(rc, SSH_OK);
464 rc = ssh_options_set(session, SSH_OPTIONS_CERTIFICATE, doe_ssh_cert);
465 assert_int_equal(rc, SSH_OK);
466
467 rc = ssh_connect(session);
468 assert_int_equal(rc, SSH_OK);
469
470 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
471 assert_int_equal(rc, SSH_AUTH_SUCCESS);
472 }
473
474 /* Same as torture_auth_cert, but the home SSH dir does not have any default
475 * identities and they are loaded through the options, only through the private
476 * key path.
477 * Non-blocking version */
478 static void torture_auth_cert_options_cert_nonblocking(void **state)
479 {
480 struct torture_state *s = *state;
481 ssh_session session = s->ssh.session;
482 char doe_ssh_key[1024];
483 char doe_ssh_cert[2048];
484 struct passwd *pwd;
485 int rc;
486
487 pwd = getpwnam("doe");
488 assert_non_null(pwd);
489
490 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
491 snprintf(doe_ssh_cert, sizeof(doe_ssh_cert), "%s-cert.pub", doe_ssh_key);
492
493 /* Explicit private key and cert */
494 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, doe_ssh_key);
495 assert_int_equal(rc, SSH_OK);
496 rc = ssh_options_set(session, SSH_OPTIONS_CERTIFICATE, doe_ssh_cert);
497 assert_int_equal(rc, SSH_OK);
498
499 rc = ssh_connect(session);
500 assert_int_equal(rc, SSH_OK);
501
502 ssh_set_blocking(session, 0);
503
504 do {
505 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
506 } while (rc == SSH_AUTH_AGAIN);
507 assert_int_equal(rc, SSH_AUTH_SUCCESS);
508 }
509
510 static void workaround_old_openssh_bug(void **state)
511 {
512 #if OPENSSH_VERSION_MAJOR < 8 || \
513 (OPENSSH_VERSION_MAJOR == 8 && OPENSSH_VERSION_MINOR == 0)
514 struct torture_state *s = *state;
515 ssh_session session = s->ssh.session;
516 int rc;
517
518 /* Skip this test if in FIPS mode.
519 *
520 * OpenSSH agent has a bug which makes it to not use SHA2 in signatures when
521 * using certificates. It always uses SHA1.
522 *
523 * This should be removed as soon as OpenSSH agent bug is fixed.
524 * (see https://gitlab.com/libssh/libssh-mirror/merge_requests/34) */
525 if (ssh_fips_mode()) {
526 skip();
527 } else {
528 /* After the bug is solved, this also should be removed */
529 rc = ssh_options_set(session,
530 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
531 "[email protected]");
532 assert_int_equal(rc, SSH_OK);
533 }
534 #else
535 (void)state;
536 #endif /* OPENSSH_VERSION_MAJOR < 8.1 */
537 }
538
539 static void torture_auth_agent_cert(void **state)
540 {
541 workaround_old_openssh_bug(state);
542
543 /* Setup loads a different key, tests are exactly the same. */
544 torture_auth_agent(state);
545 }
546
547 static void torture_auth_agent_cert_nonblocking(void **state)
548 {
549 workaround_old_openssh_bug(state);
550
551 torture_auth_agent_nonblocking(state);
552 }
553
554 static void torture_auth_agent_cert_identities_only(void **state)
555 {
556 struct torture_state *s = *state;
557 ssh_session session = s->ssh.session;
558 char doe_ssh_key[1024];
559 struct passwd *pwd = NULL;
560 bool identities_only = true;
561 char *id = NULL;
562 int rc;
563
564 workaround_old_openssh_bug(state);
565
566 pwd = getpwnam("doe");
567 assert_non_null(pwd);
568
569 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
570
571 if (!ssh_agent_is_running(session)) {
572 print_message("*** Agent not running. Test ignored\n");
573 return;
574 }
575
576 rc =
577 ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
578 assert_int_equal(rc, SSH_OK);
579
580 /* Remove the default identities */
581 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) !=
582 NULL) {
583 SAFE_FREE(id);
584 }
585
586 rc = ssh_connect(session);
587 assert_int_equal(rc, SSH_OK);
588
589 rc = ssh_userauth_none(session, NULL);
590 /* This request should return a SSH_REQUEST_DENIED error */
591 if (rc == SSH_ERROR) {
592 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
593 }
594 rc = ssh_userauth_list(session, NULL);
595 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
596
597 /* Should fail as key is not in config */
598 rc = ssh_userauth_agent(session, NULL);
599 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
600
601 /* Re-add a key -- the cert in default location should be loaded
602 * automatically */
603 rc = ssh_list_append(session->opts.identity, strdup(doe_ssh_key));
604 assert_int_equal(rc, SSH_OK);
605
606 /* Should succeed as key now in config/options */
607 rc = ssh_userauth_agent(session, NULL);
608 assert_ssh_return_code(session, rc);
609 }
610
611 static void torture_auth_agent_cert_identities_only_nonblocking(void **state)
612 {
613 struct torture_state *s = *state;
614 ssh_session session = s->ssh.session;
615 char doe_ssh_key[1024];
616 struct passwd *pwd = NULL;
617 bool identities_only = true;
618 char *id = NULL;
619 int rc;
620
621 workaround_old_openssh_bug(state);
622
623 pwd = getpwnam("doe");
624 assert_non_null(pwd);
625
626 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/id_rsa", pwd->pw_dir);
627
628 if (!ssh_agent_is_running(session)) {
629 print_message("*** Agent not running. Test ignored\n");
630 return;
631 }
632
633 rc =
634 ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
635 assert_int_equal(rc, SSH_OK);
636
637 /* Remove the default identities */
638 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) !=
639 NULL) {
640 SAFE_FREE(id);
641 }
642
643 rc = ssh_connect(session);
644 assert_int_equal(rc, SSH_OK);
645
646 ssh_set_blocking(session, 0);
647 do {
648 rc = ssh_userauth_none(session, NULL);
649 } while (rc == SSH_AUTH_AGAIN);
650 /* This request should return a SSH_REQUEST_DENIED error */
651 if (rc == SSH_ERROR) {
652 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
653 }
654 rc = ssh_userauth_list(session, NULL);
655 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
656
657 /* Should fail as key is not in config */
658 do {
659 rc = ssh_userauth_agent(session, NULL);
660 } while (rc == SSH_AUTH_AGAIN);
661 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
662
663 /* Re-add a key -- the cert in default location should be loaded
664 * automatically */
665 rc = ssh_list_append(session->opts.identity, strdup(doe_ssh_key));
666 assert_int_equal(rc, SSH_OK);
667
668 /* Should succeed as key now in config/options */
669 do {
670 rc = ssh_userauth_agent(session, NULL);
671 } while (rc == SSH_AUTH_AGAIN);
672 assert_ssh_return_code(session, rc);
673 }
674
675 static void torture_auth_agent_cert_identities_only_explicit(void **state)
676 {
677 struct torture_state *s = *state;
678 ssh_session session = s->ssh.session;
679 char doe_ssh_key[1024];
680 char doe_ssh_cert[1024];
681 struct passwd *pwd = NULL;
682 bool identities_only = true;
683 char *id = NULL;
684 int rc;
685
686 workaround_old_openssh_bug(state);
687
688 pwd = getpwnam("doe");
689 assert_non_null(pwd);
690
691 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/my_rsa", pwd->pw_dir);
692 snprintf(doe_ssh_cert,
693 sizeof(doe_ssh_cert),
694 "%s/.ssh/id_rsa-cert.pub",
695 pwd->pw_dir);
696
697 if (!ssh_agent_is_running(session)) {
698 print_message("*** Agent not running. Test ignored\n");
699 skip();
700 }
701
702 rc =
703 ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
704 assert_int_equal(rc, SSH_OK);
705
706 /* Remove the default identities */
707 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) !=
708 NULL) {
709 SAFE_FREE(id);
710 }
711
712 rc = ssh_connect(session);
713 assert_int_equal(rc, SSH_OK);
714
715 rc = ssh_userauth_none(session, NULL);
716 /* This request should return a SSH_REQUEST_DENIED error */
717 if (rc == SSH_ERROR) {
718 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
719 }
720 rc = ssh_userauth_list(session, NULL);
721 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
722
723 /* Should fail as key is not in config */
724 rc = ssh_userauth_agent(session, NULL);
725 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
726
727 /* Re-add a key and cert */
728 rc = ssh_list_append(session->opts.identity, strdup(doe_ssh_key));
729 assert_int_equal(rc, SSH_OK);
730 rc = ssh_list_append(session->opts.certificate, strdup(doe_ssh_cert));
731 assert_int_equal(rc, SSH_OK);
732
733 /* Should succeed as key now in config/options */
734 rc = ssh_userauth_agent(session, NULL);
735 assert_ssh_return_code(session, rc);
736 }
737
738 static void
739 torture_auth_agent_cert_identities_only_nonblocking_explicit(void **state)
740 {
741 struct torture_state *s = *state;
742 ssh_session session = s->ssh.session;
743 char doe_ssh_key[1024];
744 char doe_ssh_cert[1024];
745 struct passwd *pwd = NULL;
746 bool identities_only = true;
747 char *id = NULL;
748 int rc;
749
750 workaround_old_openssh_bug(state);
751
752 pwd = getpwnam("doe");
753 assert_non_null(pwd);
754
755 snprintf(doe_ssh_key, sizeof(doe_ssh_key), "%s/.ssh/my_rsa", pwd->pw_dir);
756 snprintf(doe_ssh_cert,
757 sizeof(doe_ssh_cert),
758 "%s/.ssh/id_rsa-cert.pub",
759 pwd->pw_dir);
760
761 if (!ssh_agent_is_running(session)) {
762 print_message("*** Agent not running. Test ignored\n");
763 skip();
764 }
765
766 rc =
767 ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
768 assert_int_equal(rc, SSH_OK);
769
770 /* Remove the default identities */
771 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) !=
772 NULL) {
773 SAFE_FREE(id);
774 }
775
776 rc = ssh_connect(session);
777 assert_int_equal(rc, SSH_OK);
778
779 ssh_set_blocking(session, 0);
780
781 do {
782 rc = ssh_userauth_none(session, NULL);
783 } while (rc == SSH_AUTH_AGAIN);
784 /* This request should return a SSH_REQUEST_DENIED error */
785 if (rc == SSH_ERROR) {
786 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
787 }
788 rc = ssh_userauth_list(session, NULL);
789 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
790
791 /* Should fail as key is not in config */
792 do {
793 rc = ssh_userauth_agent(session, NULL);
794 } while (rc == SSH_AUTH_AGAIN);
795 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
796
797 /* Re-add a key and cert */
798 rc = ssh_list_append(session->opts.identity, strdup(doe_ssh_key));
799 assert_int_equal(rc, SSH_OK);
800 rc = ssh_list_append(session->opts.certificate, strdup(doe_ssh_cert));
801 assert_int_equal(rc, SSH_OK);
802
803 /* Should succeed as key now in config/options */
804 do {
805 rc = ssh_userauth_agent(session, NULL);
806 } while (rc == SSH_AUTH_AGAIN);
807 assert_ssh_return_code(session, rc);
808 }
809
810 static void torture_auth_agent_cert_only_identities_only(void **state)
811 {
812 struct torture_state *s = *state;
813 ssh_session session = s->ssh.session;
814 char doe_ssh_cert[1024];
815 struct passwd *pwd = NULL;
816 bool identities_only = true;
817 char *id = NULL;
818 int rc;
819
820 workaround_old_openssh_bug(state);
821
822 pwd = getpwnam("doe");
823 assert_non_null(pwd);
824
825 snprintf(doe_ssh_cert,
826 sizeof(doe_ssh_cert),
827 "%s/.ssh/id_rsa-cert.pub",
828 pwd->pw_dir);
829
830 if (!ssh_agent_is_running(session)) {
831 print_message("*** Agent not running. Test ignored\n");
832 skip();
833 }
834
835 rc =
836 ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
837 assert_int_equal(rc, SSH_OK);
838
839 /* Remove the default identities */
840 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) !=
841 NULL) {
842 SAFE_FREE(id);
843 }
844
845 rc = ssh_connect(session);
846 assert_int_equal(rc, SSH_OK);
847
848 rc = ssh_userauth_none(session, NULL);
849 /* This request should return a SSH_REQUEST_DENIED error */
850 if (rc == SSH_ERROR) {
851 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
852 }
853 rc = ssh_userauth_list(session, NULL);
854 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
855
856 /* Should fail as key is not in config */
857 rc = ssh_userauth_agent(session, NULL);
858 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
859
860 /* Re-add a cert: key is in the agent */
861 rc = ssh_list_append(session->opts.certificate, strdup(doe_ssh_cert));
862 assert_int_equal(rc, SSH_OK);
863
864 /* Should succeed as key now in config/options */
865 rc = ssh_userauth_agent(session, NULL);
866 assert_ssh_return_code(session, rc);
867 }
868
869 static void
870 torture_auth_agent_cert_only_identities_only_nonblocking(void **state)
871 {
872 struct torture_state *s = *state;
873 ssh_session session = s->ssh.session;
874 char doe_ssh_cert[1024];
875 struct passwd *pwd = NULL;
876 bool identities_only = true;
877 char *id = NULL;
878 int rc;
879
880 workaround_old_openssh_bug(state);
881
882 pwd = getpwnam("doe");
883 assert_non_null(pwd);
884
885 snprintf(doe_ssh_cert,
886 sizeof(doe_ssh_cert),
887 "%s/.ssh/id_rsa-cert.pub",
888 pwd->pw_dir);
889
890 if (!ssh_agent_is_running(session)) {
891 print_message("*** Agent not running. Test ignored\n");
892 skip();
893 }
894
895 rc =
896 ssh_options_set(session, SSH_OPTIONS_IDENTITIES_ONLY, &identities_only);
897 assert_int_equal(rc, SSH_OK);
898
899 /* Remove the default identities */
900 while ((id = ssh_list_pop_head(char *, session->opts.identity_non_exp)) !=
901 NULL) {
902 SAFE_FREE(id);
903 }
904
905 rc = ssh_connect(session);
906 assert_int_equal(rc, SSH_OK);
907
908 ssh_set_blocking(session, 0);
909
910 do {
911 rc = ssh_userauth_none(session, NULL);
912 } while (rc == SSH_AUTH_AGAIN);
913 /* This request should return a SSH_REQUEST_DENIED error */
914 if (rc == SSH_ERROR) {
915 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
916 }
917 rc = ssh_userauth_list(session, NULL);
918 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
919
920 /* Should fail as key is not in config */
921 do {
922 rc = ssh_userauth_agent(session, NULL);
923 } while (rc == SSH_AUTH_AGAIN);
924 assert_ssh_return_code_equal(session, rc, SSH_AUTH_DENIED);
925
926 /* Re-add a cert: key is in the agent */
927 rc = ssh_list_append(session->opts.certificate, strdup(doe_ssh_cert));
928 assert_int_equal(rc, SSH_OK);
929
930 /* Should succeed as key now in config/options */
931 do {
932 rc = ssh_userauth_agent(session, NULL);
933 } while (rc == SSH_AUTH_AGAIN);
934 assert_ssh_return_code(session, rc);
935 }
936
937 int torture_run_tests(void)
938 {
939 int rc;
940 struct CMUnitTest tests[] = {
941 cmocka_unit_test_setup_teardown(torture_auth_cert,
942 session_setup,
943 session_teardown),
944 cmocka_unit_test_setup_teardown(torture_auth_cert_nonblocking,
945 session_setup,
946 session_teardown),
947 cmocka_unit_test_setup_teardown(torture_auth_cert_default_non_explicit,
948 session_setup,
949 session_teardown),
950 cmocka_unit_test_setup_teardown(
951 torture_auth_cert_default_non_explicit_nonblocking,
952 session_setup,
953 session_teardown),
954 cmocka_unit_test_setup_teardown(torture_auth_auto_fail,
955 session_setup_ssh_dir,
956 session_teardown),
957 cmocka_unit_test_setup_teardown(torture_auth_auto_fail_nonblocking,
958 session_setup_ssh_dir,
959 session_teardown),
960 cmocka_unit_test_setup_teardown(torture_auth_cert_options_private,
961 session_setup_ssh_dir,
962 session_teardown),
963 cmocka_unit_test_setup_teardown(
964 torture_auth_cert_options_private_nonblocking,
965 session_setup_ssh_dir,
966 session_teardown),
967 cmocka_unit_test_setup_teardown(torture_auth_cert_options_cert,
968 session_setup_ssh_dir,
969 session_teardown),
970 cmocka_unit_test_setup_teardown(
971 torture_auth_cert_options_cert_nonblocking,
972 session_setup_ssh_dir,
973 session_teardown),
974 cmocka_unit_test_setup_teardown(torture_auth_agent_cert,
975 agent_cert_setup,
976 agent_teardown),
977 cmocka_unit_test_setup_teardown(torture_auth_agent_cert_nonblocking,
978 agent_cert_setup,
979 agent_teardown),
980 cmocka_unit_test_setup_teardown(torture_auth_agent_cert_identities_only,
981 agent_cert_setup,
982 agent_teardown),
983 cmocka_unit_test_setup_teardown(
984 torture_auth_agent_cert_identities_only_nonblocking,
985 agent_cert_setup,
986 agent_teardown),
987 cmocka_unit_test_setup_teardown(
988 torture_auth_agent_cert_identities_only_explicit,
989 agent_cert_setup_explicit,
990 agent_teardown),
991 cmocka_unit_test_setup_teardown(
992 torture_auth_agent_cert_identities_only_nonblocking_explicit,
993 agent_cert_setup_explicit,
994 agent_teardown),
995 cmocka_unit_test_setup_teardown(
996 torture_auth_agent_cert_only_identities_only,
997 agent_cert_setup,
998 agent_teardown),
999 cmocka_unit_test_setup_teardown(
1000 torture_auth_agent_cert_only_identities_only_nonblocking,
1001 agent_cert_setup,
1002 agent_teardown),
1003 };
1004
1005 ssh_init();
1006 torture_filter_tests(tests);
1007 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
1008 ssh_finalize();
1009
1010 return rc;
1011 }