Debian Codegraph @

tests/client/torture_auth_pkcs11.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 /* agent_is_running */
36 #include "agent.c"
37
38 #define LIBSSH_RSA_TESTKEY "id_pkcs11_rsa"
39 #define LIBSSH_ECDSA_256_TESTKEY "id_pkcs11_ecdsa_256"
40 #define LIBSSH_ECDSA_384_TESTKEY "id_pkcs11_ecdsa_384"
41 #define LIBSSH_ECDSA_521_TESTKEY "id_pkcs11_ecdsa_521"
42 #define LIBSSH_ED25519_TESTKEY "id_pkcs11_ed25519"
43
44 const char template[] = "/tmp/temp_dir_XXXXXX";
45
46 struct pki_st {
47 char *temp_dir;
48 char *orig_dir;
49 char *keys_dir;
50 };
51
52 static int setup_tokens(void **state, const char *type, const char *obj_name)
53 {
54 struct torture_state *s = *state;
55 struct pki_st *test_state = s->private_data;
56 char priv_filename[1024];
57 char *cwd = NULL;
58
59 cwd = test_state->temp_dir;
60 assert_non_null(cwd);
61
62 snprintf(priv_filename,
63 sizeof(priv_filename),
64 "%s%s",
65 test_state->keys_dir,
66 type);
67
68 torture_setup_tokens(cwd, priv_filename, obj_name, "1");
69
70 return 0;
71 }
72
73 static int session_setup(void **state)
74 {
75 int verbosity = torture_libssh_verbosity();
76 struct torture_state *s = *state;
77 struct passwd *pwd = NULL;
78 bool b = false;
79 int rc;
80
81 pwd = getpwnam("bob");
82 assert_non_null(pwd);
83
84 rc = setuid(pwd->pw_uid);
85 assert_return_code(rc, errno);
86
87 s->ssh.session = ssh_new();
88 assert_non_null(s->ssh.session);
89
90 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
91 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
92 /* Make sure no other configuration options from system will get used */
93 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b);
94 assert_ssh_return_code(s->ssh.session, rc);
95
96 /* Make sure we do not interfere with another ssh-agent */
97 unsetenv("SSH_AUTH_SOCK");
98 unsetenv("SSH_AGENT_PID");
99
100 return 0;
101 }
102
103 static int session_teardown(void **state)
104 {
105 struct torture_state *s = *state;
106
107 ssh_disconnect(s->ssh.session);
108 ssh_free(s->ssh.session);
109
110 return 0;
111 }
112
113 static int setup_pkcs11(void **state)
114 {
115 struct torture_state *s = *state;
116 struct pki_st *test_state = NULL;
117 int rc;
118 char keys_dir[1024] = {0};
119 char *temp_dir = NULL;
120
121 test_state = malloc(sizeof(struct pki_st));
122 assert_non_null(test_state);
123
124 s->private_data = test_state;
125
126 test_state->orig_dir = torture_get_current_working_dir();
127 assert_non_null(test_state->orig_dir);
128
129 temp_dir = torture_make_temp_dir(template);
130 assert_non_null(temp_dir);
131
132 rc = torture_change_dir(temp_dir);
133 assert_int_equal(rc, 0);
134
135 test_state->temp_dir = torture_get_current_working_dir();
136 assert_non_null(test_state->temp_dir);
137
138 snprintf(keys_dir, sizeof(keys_dir), "%s/tests/keys/pkcs11/", SOURCEDIR);
139
140 test_state->keys_dir = strdup(keys_dir);
141
142 setup_tokens(state, LIBSSH_RSA_TESTKEY, "rsa");
143 setup_tokens(state, LIBSSH_ECDSA_256_TESTKEY, "ecdsa256");
144 setup_tokens(state, LIBSSH_ECDSA_384_TESTKEY, "ecdsa384");
145 setup_tokens(state, LIBSSH_ECDSA_521_TESTKEY, "ecdsa521");
146 if (!ssh_fips_mode()) {
147 setup_tokens(state, LIBSSH_ED25519_TESTKEY, "ed25519");
148 }
149
150 return 0;
151 }
152
153 static int sshd_setup(void **state)
154 {
155
156 torture_setup_sshd_server(state, true);
157 setup_pkcs11(state);
158
159 return 0;
160 }
161
162 static int sshd_teardown(void **state)
163 {
164 struct torture_state *s = *state;
165 struct pki_st *test_state = s->private_data;
166 int rc;
167
168 if (test_state != NULL) {
169 torture_cleanup_tokens(test_state->temp_dir);
170
171 rc = torture_change_dir(test_state->orig_dir);
172 assert_int_equal(rc, 0);
173
174 rc = torture_rmdirs(test_state->temp_dir);
175 assert_int_equal(rc, 0);
176
177 SAFE_FREE(test_state->temp_dir);
178 SAFE_FREE(test_state->orig_dir);
179 SAFE_FREE(test_state->keys_dir);
180 SAFE_FREE(test_state);
181 }
182
183 torture_teardown_sshd_server(state);
184
185 return 0;
186 }
187
188 static void
189 torture_auth_autopubkey(void **state, const char *obj_name, const char *pin)
190 {
191 struct torture_state *s = *state;
192 ssh_session session = s->ssh.session;
193 int rc;
194 char priv_uri[1042];
195
196 /* Authenticate as charlie with bob his pubkey */
197 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_CHARLIE);
198 assert_int_equal(rc, SSH_OK);
199
200 snprintf(priv_uri,
201 sizeof(priv_uri),
202 "pkcs11:token=%s;object=%s;type=private?pin-value=%s",
203 obj_name,
204 obj_name,
205 pin);
206
207 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, priv_uri);
208 assert_int_equal(rc, SSH_OK);
209 assert_string_equal(session->opts.identity_non_exp->root->data, priv_uri);
210
211 rc = ssh_connect(session);
212 assert_int_equal(rc, SSH_OK);
213 rc = ssh_userauth_none(session,NULL);
214 /* This request should return a SSH_REQUEST_DENIED error */
215 if (rc == SSH_ERROR) {
216 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
217 }
218 rc = ssh_userauth_list(session, NULL);
219 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
220
221 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
222 assert_int_equal(rc, SSH_AUTH_SUCCESS);
223 }
224
225 static void torture_auth_autopubkey_rsa(void **state)
226 {
227 torture_auth_autopubkey(state, "rsa", "1234");
228 }
229
230 static void torture_auth_autopubkey_ecdsa_key_256(void **state)
231 {
232 torture_auth_autopubkey(state, "ecdsa256", "1234");
233 }
234
235 static void torture_auth_autopubkey_ecdsa_key_384(void **state)
236 {
237 torture_auth_autopubkey(state, "ecdsa384", "1234");
238 }
239
240 static void torture_auth_autopubkey_ecdsa_key_521(void **state)
241 {
242 torture_auth_autopubkey(state, "ecdsa521", "1234");
243 }
244
245 #ifdef WITH_PKCS11_PROVIDER
246 static void torture_auth_autopubkey_ed25519(void **state)
247 {
248 /* The Ed25519 keys are not supported in FIPS mode */
249 if (ssh_fips_mode()) {
250 skip();
251 }
252
253 torture_auth_autopubkey(state, "ed25519", "1234");
254 }
255 #endif /* WITH_PKCS11_PROVIDER */
256
257 int torture_run_tests(void)
258 {
259 int rc;
260 struct CMUnitTest tests[] = {
261 cmocka_unit_test_setup_teardown(torture_auth_autopubkey_rsa,
262 session_setup,
263 session_teardown),
264 cmocka_unit_test_setup_teardown(torture_auth_autopubkey_ecdsa_key_256,
265 session_setup,
266 session_teardown),
267 cmocka_unit_test_setup_teardown(torture_auth_autopubkey_ecdsa_key_384,
268 session_setup,
269 session_teardown),
270 cmocka_unit_test_setup_teardown(torture_auth_autopubkey_ecdsa_key_521,
271 session_setup,
272 session_teardown),
273 #ifdef WITH_PKCS11_PROVIDER
274 cmocka_unit_test_setup_teardown(torture_auth_autopubkey_ed25519,
275 session_setup,
276 session_teardown),
277 #endif /* WITH_PKCS11_PROVIDER */
278 };
279
280 /* Do not use system openssl.cnf for the pkcs11 uri tests.
281 * It can load a pkcs11 provider too early before we will set up environment
282 * variables that are needed for the pkcs11 provider to access correct
283 * tokens, causing unexpected failures.
284 * Make sure this comes before ssh_init(), which initializes OpenSSL!
285 */
286 setenv("OPENSSL_CONF", SOURCEDIR "/tests/etc/openssl.cnf", 1);
287
288 ssh_init();
289 torture_filter_tests(tests);
290 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
291 ssh_finalize();
292
293 return rc;
294 }