Debian Codegraph @

tests/client/torture_hostkey.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 by Red Hat, Inc.
5 *
6 * Author: Jakub Jelen <[email protected]>
7 *
8 * The SSH Library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or (at your
11 * option) any later version.
12 *
13 * The SSH Library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the SSH Library; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24 #include "config.h"
25
26 #define LIBSSH_STATIC
27
28 #include "torture.h"
29 #include <libssh/libssh.h>
30 #ifdef HAVE_SYS_TIME_H
31 #include <sys/time.h>
32 #endif /* HAVE_SYS_TIME_H */
33 #include <errno.h>
34 #include <pwd.h>
35
36 static int sshd_setup(void **state)
37 {
38 torture_setup_sshd_server(state, false);
39
40 return 0;
41 }
42
43 static int sshd_teardown(void **state) {
44 torture_teardown_sshd_server(state);
45
46 return 0;
47 }
48
49 static int session_setup(void **state)
50 {
51 struct torture_state *s = *state;
52 int verbosity = torture_libssh_verbosity();
53 struct passwd *pwd;
54 int rc;
55
56 pwd = getpwnam("bob");
57 assert_non_null(pwd);
58
59 rc = setuid(pwd->pw_uid);
60 assert_return_code(rc, errno);
61
62 s->ssh.session = ssh_new();
63 assert_non_null(s->ssh.session);
64
65 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
66 assert_ssh_return_code(s->ssh.session, rc);
67
68 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
69 assert_ssh_return_code(s->ssh.session, rc);
70
71 return 0;
72 }
73
74 static int session_teardown(void **state)
75 {
76 struct torture_state *s = *state;
77
78 ssh_disconnect(s->ssh.session);
79 ssh_free(s->ssh.session);
80
81 return 0;
82 }
83
84 static void torture_hostkey_rsa(void **state) {
85 struct torture_state *s = *state;
86 ssh_session session = s->ssh.session;
87 char rsa[] = "ssh-rsa";
88
89 int rc;
90
91 if (ssh_fips_mode()) {
92 skip();
93 }
94
95 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, &rsa);
96 assert_ssh_return_code(session, rc);
97
98 rc = ssh_connect(session);
99 assert_ssh_return_code(session, rc);
100
101 ssh_disconnect(session);
102
103 rc = ssh_connect(session);
104 assert_ssh_return_code(session, rc);
105 }
106
107 static void torture_hostkey_ed25519(void **state) {
108 struct torture_state *s = *state;
109 ssh_session session = s->ssh.session;
110 char ed[] = "ssh-ed25519";
111
112 int rc;
113
114 if (ssh_fips_mode()) {
115 skip();
116 }
117
118 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, &ed);
119 assert_ssh_return_code(session, rc);
120
121 rc = ssh_connect(session);
122 assert_ssh_return_code(session, rc);
123
124 ssh_disconnect(session);
125
126 rc = ssh_connect(session);
127 assert_ssh_return_code(session, rc);
128 }
129
130 #ifdef HAVE_ECC
131 static void torture_hostkey_ecdsa(void **state) {
132 struct torture_state *s = *state;
133 ssh_session session = s->ssh.session;
134 char ecdsa[] = "ecdsa-sha2-nistp521";
135
136 int rc;
137
138 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, &ecdsa);
139 assert_ssh_return_code(session, rc);
140
141 rc = ssh_connect(session);
142 assert_ssh_return_code(session, rc);
143
144 ssh_disconnect(session);
145
146 rc = ssh_connect(session);
147 assert_ssh_return_code(session, rc);
148 }
149 #endif
150
151 static void torture_hostkey_rsa_sha256(void **state) {
152 struct torture_state *s = *state;
153 ssh_session session = s->ssh.session;
154 char rsa[] = "rsa-sha2-256";
155
156 int rc;
157
158 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, &rsa);
159 assert_ssh_return_code(session, rc);
160
161 rc = ssh_connect(session);
162 assert_ssh_return_code(session, rc);
163
164 ssh_disconnect(session);
165
166 rc = ssh_connect(session);
167 assert_ssh_return_code(session, rc);
168 }
169
170 static void torture_hostkey_rsa_sha512(void **state) {
171 struct torture_state *s = *state;
172 ssh_session session = s->ssh.session;
173 char rsa[] = "rsa-sha2-512";
174
175 int rc;
176
177 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, &rsa);
178 assert_ssh_return_code(session, rc);
179
180 rc = ssh_connect(session);
181 assert_ssh_return_code(session, rc);
182
183 ssh_disconnect(session);
184
185 rc = ssh_connect(session);
186 assert_ssh_return_code(session, rc);
187 }
188
189 int torture_run_tests(void) {
190 int rc;
191 struct CMUnitTest tests[] = {
192 cmocka_unit_test_setup_teardown(torture_hostkey_rsa, session_setup,
193 session_teardown),
194 cmocka_unit_test_setup_teardown(torture_hostkey_ed25519, session_setup,
195 session_teardown),
196 #ifdef HAVE_ECC
197 cmocka_unit_test_setup_teardown(torture_hostkey_ecdsa, session_setup,
198 session_teardown),
199 #endif
200 /* the client is able to handle SHA2 extension (if negotiated) */
201 cmocka_unit_test_setup_teardown(torture_hostkey_rsa_sha256,
202 session_setup, session_teardown),
203 cmocka_unit_test_setup_teardown(torture_hostkey_rsa_sha512,
204 session_setup, session_teardown),
205 };
206
207 ssh_init();
208
209 torture_filter_tests(tests);
210 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
211
212 ssh_finalize();
213 return rc;
214 }