Debian Codegraph @

tests/unittests/torture_options.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #ifndef _WIN32
6 #define _POSIX_PTHREAD_SEMANTICS
7 # include <pwd.h>
8 #endif
9 #include <sys/stat.h>
10
11 #include "torture.h"
12 #include "torture_key.h"
13 #include <errno.h>
14 #include <libssh/config.h>
15 #include <libssh/misc.h>
16 #include <libssh/options.h>
17 #include <libssh/pki.h>
18 #include <libssh/pki_priv.h>
19 #include <libssh/session.h>
20 #ifdef WITH_SERVER
21 #include <libssh/bind.h>
22 #define LIBSSH_CUSTOM_BIND_CONFIG_FILE "my_bind_config"
23 #endif
24 #define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa"
25 #define LIBSSH_ED25519_TESTKEY "libssh_testkey.id_ed25519"
26 #ifdef HAVE_ECC
27 #define LIBSSH_ECDSA_521_TESTKEY "libssh_testkey.id_ecdsa521"
28 #endif
29
30 static int setup(void **state)
31 {
32 ssh_session session;
33 int verbosity;
34
35 session = ssh_new();
36
37 verbosity = torture_libssh_verbosity();
38 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
39
40 session->client = 1;
41
42 *state = session;
43
44 return 0;
45 }
46
47 static int teardown(void **state)
48 {
49 ssh_free(*state);
50
51 return 0;
52 }
53
54 static void torture_options_set_host(void **state) {
55 ssh_session session = *state;
56 int rc;
57
58 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
59 assert_true(rc == 0);
60 assert_non_null(session->opts.host);
61 assert_string_equal(session->opts.host, "localhost");
62
63 /* IPv4 address */
64 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "127.1.1.1");
65 assert_true(rc == 0);
66 assert_non_null(session->opts.host);
67 assert_string_equal(session->opts.host, "127.1.1.1");
68 assert_null(session->opts.username);
69
70 /* IPv6 address */
71 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "::1");
72 assert_true(rc == 0);
73 assert_non_null(session->opts.host);
74 assert_string_equal(session->opts.host, "::1");
75 assert_null(session->opts.username);
76
77 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "guru@meditation");
78 assert_true(rc == 0);
79 assert_non_null(session->opts.host);
80 assert_string_equal(session->opts.host, "meditation");
81 assert_non_null(session->opts.username);
82 assert_string_equal(session->opts.username, "guru");
83
84 /* more @ in uri is OK -- it should go to the username */
85 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "at@login@hostname");
86 assert_true(rc == 0);
87 assert_non_null(session->opts.host);
88 assert_string_equal(session->opts.host, "hostname");
89 assert_non_null(session->opts.username);
90 assert_string_equal(session->opts.username, "at@login");
91
92 /* disallow metacharacters in the username */
93 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "shallN()tP4ss -@hostname");
94 assert_string_equal(ssh_get_error(session),
95 "Invalid argument in ssh_options_set");
96 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
97
98 /* IPv6 hostnames should work without square braces */
99 SAFE_FREE(session->opts.username);
100 rc = ssh_options_set(session,
101 SSH_OPTIONS_HOST,
102 "fd4d:5449:7400:111:626d:3cff:fedf:4d39");
103 assert_return_code(rc, errno);
104 assert_non_null(session->opts.host);
105 assert_string_equal(session->opts.host,
106 "fd4d:5449:7400:111:626d:3cff:fedf:4d39");
107 assert_null(session->opts.username);
108
109 /* IPv6 hostnames should work also with square braces */
110 rc = ssh_options_set(session,
111 SSH_OPTIONS_HOST,
112 "[fd4d:5449:7400:111:626d:3cff:fedf:4d39]");
113 assert_return_code(rc, errno);
114 assert_non_null(session->opts.host);
115 assert_string_equal(session->opts.host,
116 "fd4d:5449:7400:111:626d:3cff:fedf:4d39");
117 assert_null(session->opts.username);
118
119 /* IDN need to be in punycode format */
120 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "xn--bcher-kva.tld");
121 assert_return_code(rc, errno);
122 assert_non_null(session->opts.host);
123 assert_string_equal(session->opts.host, "xn--bcher-kva.tld");
124 assert_null(session->opts.username);
125
126 /* IDN in UTF8 won't work */
127 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "bücher.tld");
128 assert_string_equal(ssh_get_error(session),
129 "Invalid argument in ssh_options_set");
130 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
131 }
132
133 static void torture_options_set_ciphers(void **state)
134 {
135 ssh_session session = *state;
136 int rc;
137
138 /* Test known ciphers */
139 rc = ssh_options_set(session,
140 SSH_OPTIONS_CIPHERS_C_S,
141 "aes128-ctr,aes192-ctr,aes256-ctr");
142 assert_ssh_return_code(session, rc);
143 assert_non_null(session->opts.wanted_methods[SSH_CRYPT_C_S]);
144 if (ssh_fips_mode()) {
145 assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S],
146 "aes128-ctr,aes256-ctr");
147 } else {
148 assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S],
149 "aes128-ctr,aes192-ctr,aes256-ctr");
150 }
151
152 /* Test one unknown cipher */
153 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S,
154 "aes128-ctr,[email protected],aes256-ctr");
155 assert_ssh_return_code(session, rc);
156 assert_non_null(session->opts.wanted_methods[SSH_CRYPT_C_S]);
157 assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S],
158 "aes128-ctr,aes256-ctr");
159
160 /* Test all unknown ciphers */
161 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S,
162 "[email protected],[email protected]");
163 assert_false(rc == 0);
164 }
165
166 static void torture_options_get_ciphers(void **state)
167 {
168 ssh_session session = *state;
169 int rc;
170 char *value = NULL;
171
172 /* Test defaults returned */
173 rc = ssh_options_get(session, SSH_OPTIONS_CIPHERS_C_S, &value);
174 assert_ssh_return_code(session, rc);
175 assert_non_null(value);
176 if (ssh_fips_mode()) {
177 assert_string_equal(value,
178 "[email protected],"
179 "aes256-ctr,"
180 "aes256-cbc,"
181 "[email protected],"
182 "aes128-ctr,"
183 "aes128-cbc");
184 } else {
185 assert_string_equal(value,
186 "[email protected],"
187 "[email protected],"
188 "[email protected],"
189 "aes256-ctr,"
190 "aes192-ctr,"
191 "aes128-ctr");
192 }
193 ssh_string_free_char(value);
194
195 /* Test explicit ciphers */
196 rc = ssh_options_set(session,
197 SSH_OPTIONS_CIPHERS_C_S,
198 "aes128-ctr,aes192-ctr,aes256-ctr");
199 assert_ssh_return_code(session, rc);
200
201 value = NULL;
202 rc = ssh_options_get(session, SSH_OPTIONS_CIPHERS_C_S, &value);
203 assert_ssh_return_code(session, rc);
204 assert_non_null(value);
205 if (ssh_fips_mode()) {
206 assert_string_equal(value, "aes128-ctr,aes256-ctr");
207 } else {
208 assert_string_equal(value, "aes128-ctr,aes192-ctr,aes256-ctr");
209 }
210 ssh_string_free_char(value);
211 }
212
213 static void torture_options_set_key_exchange(void **state)
214 {
215 ssh_session session = *state;
216 int rc;
217
218 /* Test known kexes */
219 rc = ssh_options_set(session,
220 SSH_OPTIONS_KEY_EXCHANGE,
221 "sntrup761x25519-sha512,"
222 "[email protected],"
223 "curve25519-sha256,[email protected],"
224 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
225 "diffie-hellman-group18-sha512,"
226 "diffie-hellman-group14-sha256,"
227 "diffie-hellman-group14-sha1");
228 assert_ssh_return_code(session, rc);
229 assert_non_null(session->opts.wanted_methods[SSH_KEX]);
230 if (ssh_fips_mode()) {
231 assert_string_equal(session->opts.wanted_methods[SSH_KEX],
232 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
233 "diffie-hellman-group18-sha512,"
234 "diffie-hellman-group14-sha256");
235 } else {
236 assert_string_equal(session->opts.wanted_methods[SSH_KEX],
237 "sntrup761x25519-sha512,"
238 "[email protected],"
239 "curve25519-sha256,[email protected],"
240 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
241 "diffie-hellman-group18-sha512,"
242 "diffie-hellman-group14-sha256,"
243 "diffie-hellman-group14-sha1");
244 }
245
246 /* Test one unknown kex */
247 rc = ssh_options_set(session,
248 SSH_OPTIONS_KEY_EXCHANGE,
249 "diffie-hellman-group16-sha512,"
250 "[email protected],"
251 "diffie-hellman-group18-sha512");
252 assert_ssh_return_code(session, rc);
253 assert_non_null(session->opts.wanted_methods[SSH_KEX]);
254 assert_string_equal(session->opts.wanted_methods[SSH_KEX],
255 "diffie-hellman-group16-sha512,"
256 "diffie-hellman-group18-sha512");
257
258 /* Test all unknown kexes */
259 rc = ssh_options_set(session,
260 SSH_OPTIONS_KEY_EXCHANGE,
261 "[email protected],[email protected]");
262 assert_false(rc == 0);
263 }
264
265 static void torture_options_get_key_exchange(void **state)
266 {
267 ssh_session session = *state;
268 int rc;
269 char *value = NULL;
270
271 /* Test defaults returned */
272 rc = ssh_options_get(session, SSH_OPTIONS_KEY_EXCHANGE, &value);
273 assert_ssh_return_code(session, rc);
274 assert_non_null(value);
275 if (ssh_fips_mode()) {
276 assert_string_equal(value,
277 "ecdh-sha2-nistp256,"
278 "ecdh-sha2-nistp384,"
279 "ecdh-sha2-nistp521,"
280 "diffie-hellman-group-exchange-sha256,"
281 "diffie-hellman-group14-sha256,"
282 "diffie-hellman-group16-sha512,"
283 "diffie-hellman-group18-sha512");
284 } else {
285 #ifdef HAVE_MLKEM1024
286 assert_string_equal(value,
287 "mlkem768x25519-sha256,"
288 "mlkem768nistp256-sha256,"
289 "mlkem1024nistp384-sha384,"
290 "sntrup761x25519-sha512,[email protected],"
291 "curve25519-sha256,[email protected],"
292 "ecdh-sha2-nistp256,ecdh-sha2-nistp384,"
293 "ecdh-sha2-nistp521,diffie-hellman-group18-sha512,"
294 "diffie-hellman-group16-sha512,"
295 "diffie-hellman-group-exchange-sha256,"
296 "diffie-hellman-group14-sha256");
297 #else
298 assert_string_equal(value,
299 "mlkem768x25519-sha256,"
300 "mlkem768nistp256-sha256,"
301 "sntrup761x25519-sha512,"
302 "[email protected],"
303 "curve25519-sha256,[email protected],"
304 "ecdh-sha2-nistp256,ecdh-sha2-nistp384,"
305 "ecdh-sha2-nistp521,diffie-hellman-group18-sha512,"
306 "diffie-hellman-group16-sha512,"
307 "diffie-hellman-group-exchange-sha256,"
308 "diffie-hellman-group14-sha256");
309 #endif
310 }
311 ssh_string_free_char(value);
312
313 /* Test explicit kexes */
314 rc = ssh_options_set(session,
315 SSH_OPTIONS_KEY_EXCHANGE,
316 "curve25519-sha256,[email protected],"
317 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
318 "diffie-hellman-group18-sha512,"
319 "diffie-hellman-group14-sha256,"
320 "diffie-hellman-group14-sha1");
321 assert_ssh_return_code(session, rc);
322
323 value = NULL;
324 rc = ssh_options_get(session, SSH_OPTIONS_KEY_EXCHANGE, &value);
325 assert_ssh_return_code(session, rc);
326 assert_non_null(value);
327 if (ssh_fips_mode()) {
328 assert_string_equal(value,
329 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
330 "diffie-hellman-group18-sha512,"
331 "diffie-hellman-group14-sha256");
332 } else {
333 assert_string_equal(value,
334 "curve25519-sha256,[email protected],"
335 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
336 "diffie-hellman-group18-sha512,"
337 "diffie-hellman-group14-sha256,"
338 "diffie-hellman-group14-sha1");
339 }
340 ssh_string_free_char(value);
341 }
342
343 static void torture_options_set_hostkey(void **state)
344 {
345 ssh_session session = *state;
346 int rc;
347
348 /* Test known host keys */
349 rc = ssh_options_set(session,
350 SSH_OPTIONS_HOSTKEYS,
351 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
352 assert_ssh_return_code(session, rc);
353 assert_non_null(session->opts.wanted_methods[SSH_HOSTKEYS]);
354 if (ssh_fips_mode()) {
355 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
356 "ecdsa-sha2-nistp384");
357 } else {
358 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
359 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
360 }
361
362 /* Test one unknown host key */
363 rc = ssh_options_set(session,
364 SSH_OPTIONS_HOSTKEYS,
365 "ecdsa-sha2-nistp521,"
366 "[email protected],"
367 "rsa-sha2-256");
368 assert_ssh_return_code(session, rc);
369 assert_non_null(session->opts.wanted_methods[SSH_HOSTKEYS]);
370 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
371 "ecdsa-sha2-nistp521,"
372 "rsa-sha2-256");
373
374 /* Test all unknown host keys */
375 rc = ssh_options_set(session,
376 SSH_OPTIONS_HOSTKEYS,
377 "[email protected],[email protected]");
378 assert_false(rc == 0);
379 }
380
381 static void torture_options_get_hostkey(void **state)
382 {
383 ssh_session session = *state;
384 int rc;
385 char *value = NULL;
386
387 rc = ssh_options_get(session, SSH_OPTIONS_HOSTKEYS, &value);
388 assert_ssh_return_code(session, rc);
389 assert_non_null(value);
390 if (ssh_fips_mode()) {
391 assert_string_equal(value,
392 "[email protected],"
393 "[email protected],"
394 "[email protected],"
395 "[email protected],"
396 "[email protected],"
397 "ecdsa-sha2-nistp521,"
398 "ecdsa-sha2-nistp384,"
399 "ecdsa-sha2-nistp256,"
400 "rsa-sha2-512,"
401 "rsa-sha2-256");
402 } else {
403 assert_string_equal(value,
404 "[email protected],"
405 "[email protected],"
406 "[email protected],"
407 "[email protected],"
408 "[email protected],"
409 "[email protected],"
410 "[email protected],"
411 "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
412 "ecdsa-sha2-nistp256,[email protected],"
413 "[email protected],"
414 "rsa-sha2-512,rsa-sha2-256");
415 }
416 ssh_string_free_char(value);
417
418 /* Test explicit host keys */
419 rc = ssh_options_set(session,
420 SSH_OPTIONS_HOSTKEYS,
421 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
422 assert_ssh_return_code(session, rc);
423
424 value = NULL;
425 rc = ssh_options_get(session, SSH_OPTIONS_HOSTKEYS, &value);
426 assert_ssh_return_code(session, rc);
427 assert_non_null(value);
428 if (ssh_fips_mode()) {
429 assert_string_equal(value, "ecdsa-sha2-nistp384");
430 } else {
431 assert_string_equal(value, "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
432 }
433 ssh_string_free_char(value);
434 }
435
436 static void torture_options_set_pubkey_accepted_types(void **state)
437 {
438 ssh_session session = *state;
439 int rc;
440 enum ssh_digest_e type;
441
442 /* Test known public key algorithms */
443 rc = ssh_options_set(session,
444 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
445 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
446 assert_ssh_return_code(session, rc);
447 assert_non_null(session->opts.pubkey_accepted_types);
448 if (ssh_fips_mode()) {
449 assert_string_equal(session->opts.pubkey_accepted_types,
450 "ecdsa-sha2-nistp384");
451 } else {
452 assert_string_equal(session->opts.pubkey_accepted_types,
453 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
454 }
455
456 if (!ssh_fips_mode()) {
457 /* Test one unknown public key algorithms */
458 rc = ssh_options_set(session,
459 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
460 "ssh-ed25519,[email protected],ssh-rsa");
461 assert_ssh_return_code(session, rc);
462 assert_non_null(session->opts.pubkey_accepted_types);
463 assert_string_equal(session->opts.pubkey_accepted_types,
464 "ssh-ed25519,ssh-rsa");
465
466 /* Test all unknown public key algorithms */
467 rc = ssh_options_set(session,
468 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
469 "[email protected],[email protected]");
470 assert_false(rc == 0);
471
472 /* Test that the option affects the algorithm selection for RSA keys */
473 /* simulate the SHA2 extension was negotiated */
474 session->extensions = SSH_EXT_SIG_RSA_SHA256;
475
476 /* previous configuration did not list the SHA2 extension algorithms, so
477 * it should not be used */
478 type = ssh_key_type_to_hash(session, SSH_KEYTYPE_RSA);
479 assert_int_equal(type, SSH_DIGEST_SHA1);
480 }
481
482 /* now, lets allow the signature from SHA2 extension and expect
483 * it to be used */
484 rc = ssh_options_set(session,
485 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
486 "rsa-sha2-256,ssh-rsa");
487 assert_ssh_return_code(session, rc);
488 assert_non_null(session->opts.pubkey_accepted_types);
489 if (ssh_fips_mode()) {
490 assert_string_equal(session->opts.pubkey_accepted_types,
491 "rsa-sha2-256");
492 } else {
493 assert_string_equal(session->opts.pubkey_accepted_types,
494 "rsa-sha2-256,ssh-rsa");
495 }
496
497 /* Test that the option affects the algorithm selection for RSA keys */
498 /* simulate the SHA2 extension was negotiated */
499 session->extensions = SSH_EXT_SIG_RSA_SHA256;
500
501 type = ssh_key_type_to_hash(session, SSH_KEYTYPE_RSA);
502 assert_int_equal(type, SSH_DIGEST_SHA256);
503 }
504
505 static void torture_options_get_pubkey_accepted_types(void **state)
506 {
507 ssh_session session = *state;
508 int rc;
509 char *value = NULL;
510
511 /* Test known public key algorithms */
512 rc = ssh_options_set(session,
513 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
514 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
515 assert_ssh_return_code(session, rc);
516
517 rc = ssh_options_get(session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES, &value);
518 assert_ssh_return_code(session, rc);
519 assert_non_null(value);
520 if (ssh_fips_mode()) {
521 assert_string_equal(value, "ecdsa-sha2-nistp384");
522 } else {
523 assert_string_equal(value, "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
524 }
525 ssh_string_free_char(value);
526 }
527
528
529 static void torture_options_set_macs(void **state)
530 {
531 ssh_session session = *state;
532 int rc;
533
534 /* Test known MACs */
535 rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "hmac-sha1");
536 assert_ssh_return_code(session, rc);
537 assert_non_null(session->opts.wanted_methods[SSH_MAC_S_C]);
538 assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], "hmac-sha1");
539
540 /* Test multiple known MACs */
541 rc = ssh_options_set(session,
542 SSH_OPTIONS_HMAC_S_C,
543 "[email protected],"
544 "[email protected],"
545 "hmac-sha1,hmac-sha2-256");
546 assert_ssh_return_code(session, rc);
547 assert_non_null(session->opts.wanted_methods[SSH_MAC_S_C]);
548 assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C],
549 "[email protected],"
550 "[email protected],"
551 "hmac-sha1,hmac-sha2-256");
552
553 /* Test unknown MACs */
554 rc = ssh_options_set(session,
555 SSH_OPTIONS_HMAC_S_C,
556 "[email protected],[email protected],"
557 "[email protected]");
558 assert_ssh_return_code(session, rc);
559 assert_non_null(session->opts.wanted_methods[SSH_MAC_S_C]);
560 assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C],
561 "[email protected]");
562
563 /* Test all unknown MACs */
564 rc = ssh_options_set(session,
565 SSH_OPTIONS_HMAC_S_C,
566 "[email protected]");
567 assert_false(rc == 0);
568 }
569
570 static void torture_options_get_macs(void **state)
571 {
572 ssh_session session = *state;
573 int rc;
574 char *value = NULL;
575
576 /* test defaults returned */
577 rc = ssh_options_get(session, SSH_OPTIONS_HMAC_S_C, &value);
578 assert_ssh_return_code(session, rc);
579 assert_non_null(value);
580 if (ssh_fips_mode()) {
581 assert_string_equal(value,
582 "[email protected],"
583 "[email protected],"
584 "[email protected],"
585 "hmac-sha2-256,"
586 "hmac-sha1,"
587 "hmac-sha2-512");
588 } else {
589 assert_string_equal(value,
590 "[email protected],"
591 "[email protected],"
592 "hmac-sha2-256,"
593 "hmac-sha2-512");
594 }
595 ssh_string_free_char(value);
596
597 /* Test known MACs */
598 rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "hmac-sha1");
599 assert_ssh_return_code(session, rc);
600
601 value = NULL;
602 rc = ssh_options_get(session, SSH_OPTIONS_HMAC_S_C, &value);
603 assert_ssh_return_code(session, rc);
604 assert_non_null(value);
605 assert_string_equal(value, "hmac-sha1");
606 ssh_string_free_char(value);
607 }
608
609 static void torture_options_set_compression(void **state)
610 {
611 ssh_session session = *state;
612 int rc;
613 const char *known_value;
614 const char *multiple;
615
616 #ifdef WITH_ZLIB
617 if (ssh_fips_mode()) {
618 known_value = "none";
619 multiple = "none,squeeze";
620 } else {
621 known_value = "zlib";
622 multiple = "zlib,squeeze";
623 }
624 #else
625 known_value = "none";
626 multiple = "none,squeeze";
627 #endif
628
629 /* Test known compression */
630 rc = ssh_options_set(session, SSH_OPTIONS_COMPRESSION_S_C, known_value);
631 assert_ssh_return_code(session, rc);
632 assert_non_null(session->opts.wanted_methods[SSH_COMP_S_C]);
633 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
634 known_value);
635
636 /* Test multiple known compression */
637 if (!ssh_fips_mode()) {
638 rc = ssh_options_set(session,
639 SSH_OPTIONS_COMPRESSION_S_C,
640 "none,[email protected]");
641 assert_ssh_return_code(session, rc);
642 assert_non_null(session->opts.wanted_methods[SSH_COMP_S_C]);
643 #ifdef WITH_ZLIB
644 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
645 "none,[email protected]");
646 #else
647 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C], "none");
648 #endif
649 }
650
651 /* Test unknown compression */
652 rc = ssh_options_set(session, SSH_OPTIONS_COMPRESSION_S_C, multiple);
653 assert_ssh_return_code(session, rc);
654 assert_non_null(session->opts.wanted_methods[SSH_COMP_S_C]);
655 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
656 known_value);
657
658 /* Test all unknown compression */
659 rc = ssh_options_set(session, SSH_OPTIONS_COMPRESSION_S_C, "squeeze");
660 assert_false(rc == 0);
661 }
662
663 static void torture_options_get_compression(void **state)
664 {
665 ssh_session session = *state;
666 int rc;
667 char *value = NULL;
668 const char *test_value = NULL;
669
670 #ifdef WITH_ZLIB
671 if (ssh_fips_mode()) {
672 test_value = "none";
673 } else {
674 test_value = "[email protected]";
675 }
676 #else
677 test_value = "none";
678 #endif
679
680 /* test defaults returned */
681 rc = ssh_options_get(session, SSH_OPTIONS_COMPRESSION_S_C, &value);
682 assert_ssh_return_code(session, rc);
683 assert_non_null(value);
684 #ifdef WITH_ZLIB
685 assert_string_equal(value, "none,[email protected]");
686 #else
687 assert_string_equal(value, "none");
688 #endif
689 ssh_string_free_char(value);
690
691 /* Test known compression */
692 rc = ssh_options_set(session, SSH_OPTIONS_COMPRESSION_S_C, test_value);
693 assert_ssh_return_code(session, rc);
694
695 value = NULL;
696 rc = ssh_options_get(session, SSH_OPTIONS_COMPRESSION_S_C, &value);
697 assert_ssh_return_code(session, rc);
698 assert_non_null(value);
699 assert_string_equal(value, test_value);
700 ssh_string_free_char(value);
701 }
702
703 static void torture_options_get_host(void **state)
704 {
705 ssh_session session = *state;
706 int rc;
707 char* host = NULL;
708
709 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
710 assert_true(rc == 0);
711 assert_string_equal(session->opts.host, "localhost");
712
713 assert_false(ssh_options_get(session, SSH_OPTIONS_HOST, &host));
714
715 assert_string_equal(host, "localhost");
716 ssh_string_free_char(host);
717 }
718
719 static void torture_options_set_port(void **state)
720 {
721 ssh_session session = *state;
722 int rc;
723 unsigned int port = 42;
724
725 rc = ssh_options_set(session, SSH_OPTIONS_PORT, &port);
726 assert_true(rc == 0);
727 assert_true(session->opts.port == port);
728
729 rc = ssh_options_set(session, SSH_OPTIONS_PORT_STR, "23");
730 assert_true(rc == 0);
731 assert_true(session->opts.port == 23);
732
733 rc = ssh_options_set(session, SSH_OPTIONS_PORT_STR, "five");
734 assert_true(rc == -1);
735 assert_int_not_equal(session->opts.port, 0);
736
737 rc = ssh_options_set(session, SSH_OPTIONS_PORT, NULL);
738 assert_true(rc == -1);
739 }
740
741 static void torture_options_get_port(void **state)
742 {
743 ssh_session session = *state;
744 unsigned int given_port = 1234;
745 unsigned int port_container;
746 int rc;
747
748 rc = ssh_options_set(session, SSH_OPTIONS_PORT, &given_port);
749 assert_true(rc == 0);
750 rc = ssh_options_get_port(session, &port_container);
751 assert_true(rc == 0);
752 assert_int_equal(port_container, 1234);
753 }
754
755 static void torture_options_get_user(void **state)
756 {
757 ssh_session session = *state;
758 char *user = NULL;
759 int rc;
760
761 rc = ssh_options_set(session, SSH_OPTIONS_USER, "magicaltrevor");
762 assert_int_equal(rc, SSH_OK);
763 rc = ssh_options_get(session, SSH_OPTIONS_USER, &user);
764 assert_int_equal(rc, SSH_OK);
765 assert_non_null(user);
766 assert_string_equal(user, "magicaltrevor");
767 ssh_string_free_char(user);
768 }
769
770 static void torture_options_set_fd(void **state)
771 {
772 ssh_session session = *state;
773 socket_t fd = 42;
774 int rc;
775
776 rc = ssh_options_set(session, SSH_OPTIONS_FD, &fd);
777 assert_true(rc == 0);
778 assert_true(session->opts.fd == fd);
779
780 rc = ssh_options_set(session, SSH_OPTIONS_FD, NULL);
781 assert_true(rc == SSH_ERROR);
782 assert_true(session->opts.fd == SSH_INVALID_SOCKET);
783 }
784
785 static void torture_options_set_user(void **state)
786 {
787 ssh_session session = *state;
788 int rc;
789 #ifndef _WIN32
790 # ifndef NSS_BUFLEN_PASSWD
791 # define NSS_BUFLEN_PASSWD 4096
792 # endif /* NSS_BUFLEN_PASSWD */
793 struct passwd pwd;
794 struct passwd *pwdbuf;
795 char buf[NSS_BUFLEN_PASSWD];
796
797 /* get local username */
798 rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
799 assert_true(rc == 0);
800 #endif /* _WIN32 */
801
802 rc = ssh_options_set(session, SSH_OPTIONS_USER, "&shallN()tP4ss");
803 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
804
805 rc = ssh_options_set(session, SSH_OPTIONS_USER, "guru");
806 assert_true(rc == 0);
807 assert_string_equal(session->opts.username, "guru");
808
809
810 rc = ssh_options_set(session, SSH_OPTIONS_USER, NULL);
811 assert_true(rc == 0);
812
813 #ifndef _WIN32
814 assert_string_equal(session->opts.username, pwd.pw_name);
815 #endif
816 }
817
818 static void torture_options_set_identity(void **state)
819 {
820 ssh_session session = *state;
821 int rc;
822
823 rc = ssh_options_set(session, SSH_OPTIONS_ADD_IDENTITY, "identity1");
824 assert_true(rc == 0);
825 assert_string_equal(session->opts.identity_non_exp->root->data,
826 "identity1");
827
828 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "identity2");
829 assert_true(rc == 0);
830 assert_string_equal(session->opts.identity_non_exp->root->data,
831 "identity2");
832 assert_string_equal(session->opts.identity_non_exp->root->next->data,
833 "identity1");
834 }
835
836 static void torture_options_get_identity(void **state)
837 {
838 ssh_session session = *state;
839 char *identity = NULL;
840 int rc;
841
842 /* This adds an identity to the head of the list and returns */
843 rc = ssh_options_set(session, SSH_OPTIONS_ADD_IDENTITY, "identity1");
844 assert_true(rc == 0);
845 rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
846 assert_int_equal(rc, SSH_OK);
847 assert_non_null(identity);
848 assert_string_equal(identity, "identity1");
849 SAFE_FREE(identity);
850
851 rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "identity2");
852 assert_int_equal(rc, SSH_OK);
853 assert_string_equal(session->opts.identity_non_exp->root->data,
854 "identity2");
855 rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
856 assert_int_equal(rc, SSH_OK);
857 assert_non_null(identity);
858 assert_string_equal(identity, "identity2");
859 ssh_string_free_char(identity);
860
861 /* Iterate over all of the identities */
862 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
863 assert_int_equal(rc, SSH_OK);
864 assert_string_equal(identity, "identity2");
865 ssh_string_free_char(identity);
866
867 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
868 assert_int_equal(rc, SSH_OK);
869 assert_string_equal(identity, "identity1");
870 SAFE_FREE(identity);
871
872 /* here are the default identities */
873 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
874 assert_int_equal(rc, SSH_OK);
875 assert_string_equal(identity, "%d/.ssh/id_ed25519");
876 ssh_string_free_char(identity);
877
878 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
879 assert_int_equal(rc, SSH_OK);
880 assert_string_equal(identity, "%d/.ssh/id_ecdsa");
881 ssh_string_free_char(identity);
882
883 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
884 assert_int_equal(rc, SSH_OK);
885 assert_string_equal(identity, "%d/.ssh/id_rsa");
886 ssh_string_free_char(identity);
887
888 #ifdef WITH_FIDO2
889 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
890 assert_int_equal(rc, SSH_OK);
891 assert_string_equal(identity, "%d/.ssh/id_ed25519_sk");
892 ssh_string_free_char(identity);
893
894 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
895 assert_int_equal(rc, SSH_OK);
896 assert_string_equal(identity, "%d/.ssh/id_ecdsa_sk");
897 ssh_string_free_char(identity);
898 #endif /* WITH_FIDO2 */
899
900 rc = ssh_options_get(session, SSH_OPTIONS_NEXT_IDENTITY, &identity);
901 assert_int_equal(rc, SSH_EOF);
902 }
903
904 static void torture_options_set_global_knownhosts(void **state)
905 {
906 ssh_session session = *state;
907 int rc;
908
909 rc = ssh_options_set(session,
910 SSH_OPTIONS_GLOBAL_KNOWNHOSTS,
911 "/etc/libssh/known_hosts");
912 assert_ssh_return_code(session, rc);
913 assert_string_equal(session->opts.global_knownhosts,
914 "/etc/libssh/known_hosts");
915 }
916
917 static void torture_options_get_global_knownhosts(void **state)
918 {
919 ssh_session session = *state;
920 char *str = NULL;
921 int rc;
922
923 rc = ssh_options_set(session,
924 SSH_OPTIONS_GLOBAL_KNOWNHOSTS,
925 "/etc/libssh/known_hosts");
926 assert_ssh_return_code(session, rc);
927 assert_string_equal(session->opts.global_knownhosts,
928 "/etc/libssh/known_hosts");
929
930
931 rc = ssh_options_get(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, &str);
932 assert_ssh_return_code(session, rc);
933 assert_string_equal(session->opts.global_knownhosts,
934 "/etc/libssh/known_hosts");
935
936 SSH_STRING_FREE_CHAR(str);
937 }
938
939 static void torture_options_set_knownhosts(void **state)
940 {
941 ssh_session session = *state;
942 int rc;
943
944 rc = ssh_options_set(session,
945 SSH_OPTIONS_KNOWNHOSTS,
946 "/home/libssh/.ssh/known_hosts");
947 assert_ssh_return_code(session, rc);
948 assert_string_equal(session->opts.knownhosts,
949 "/home/libssh/.ssh/known_hosts");
950
951 /* The NULL value should not crash the libssh */
952 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, NULL);
953 assert_ssh_return_code(session, rc);
954 assert_null(session->opts.knownhosts);
955
956 /* ssh_options_apply() should set the path to correct value */
957 rc = ssh_options_apply(session);
958 assert_ssh_return_code(session, rc);
959 assert_non_null(session->opts.knownhosts);
960 }
961
962 static void torture_options_get_knownhosts(void **state)
963 {
964 ssh_session session = *state;
965 char *str = NULL;
966 int rc;
967
968 rc = ssh_options_set(session,
969 SSH_OPTIONS_KNOWNHOSTS,
970 "/home/libssh/.ssh/known_hosts");
971 assert_ssh_return_code(session, rc);
972 assert_string_equal(session->opts.knownhosts,
973 "/home/libssh/.ssh/known_hosts");
974
975
976 rc = ssh_options_get(session, SSH_OPTIONS_KNOWNHOSTS, &str);
977 assert_ssh_return_code(session, rc);
978 assert_string_equal(session->opts.knownhosts,
979 "/home/libssh/.ssh/known_hosts");
980
981 SSH_STRING_FREE_CHAR(str);
982 }
983
984 static void torture_options_proxycommand(void **state) {
985 ssh_session session = *state;
986 int rc;
987
988 /* Enable ProxyCommand */
989 rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, "ssh -q -A -X -W %h:%p JUMPHOST");
990 assert_int_equal(rc, 0);
991
992 assert_string_equal(session->opts.ProxyCommand, "ssh -q -A -X -W %h:%p JUMPHOST");
993
994 /* Disable ProxyCommand */
995 rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, "none");
996 assert_int_equal(rc, 0);
997
998 assert_null(session->opts.ProxyCommand);
999 }
1000
1001 static void torture_options_control_master (void **state)
1002 {
1003 ssh_session session = *state;
1004 int rc, val = SSH_CONTROL_MASTER_NO;
1005
1006 rc = ssh_options_set(session,
1007 SSH_OPTIONS_CONTROL_MASTER,
1008 &val);
1009 assert_int_equal(rc, SSH_OK);
1010 assert_int_equal(session->opts.control_master, SSH_CONTROL_MASTER_NO);
1011
1012 val = SSH_CONTROL_MASTER_AUTO;
1013 rc = ssh_options_set(session,
1014 SSH_OPTIONS_CONTROL_MASTER,
1015 &val);
1016 assert_int_equal(rc, SSH_OK);
1017 assert_int_equal(session->opts.control_master, SSH_CONTROL_MASTER_AUTO);
1018
1019 val = SSH_CONTROL_MASTER_YES;
1020 rc = ssh_options_set(session,
1021 SSH_OPTIONS_CONTROL_MASTER,
1022 &val);
1023 assert_int_equal(rc, SSH_OK);
1024 assert_int_equal(session->opts.control_master, SSH_CONTROL_MASTER_YES);
1025
1026 val = SSH_CONTROL_MASTER_ASK;
1027 rc = ssh_options_set(session,
1028 SSH_OPTIONS_CONTROL_MASTER,
1029 &val);
1030 assert_int_equal(rc, SSH_OK);
1031 assert_int_equal(session->opts.control_master, SSH_CONTROL_MASTER_ASK);
1032
1033 val = SSH_CONTROL_MASTER_AUTOASK;
1034 rc = ssh_options_set(session,
1035 SSH_OPTIONS_CONTROL_MASTER,
1036 &val);
1037 assert_int_equal(rc, SSH_OK);
1038 assert_int_equal(session->opts.control_master, SSH_CONTROL_MASTER_AUTOASK);
1039
1040 val = 255;
1041 rc = ssh_options_set(session,
1042 SSH_OPTIONS_CONTROL_MASTER,
1043 &val);
1044 assert_int_equal(rc, SSH_ERROR);
1045 }
1046
1047 static void torture_options_control_path(void **state)
1048 {
1049 ssh_session session = *state;
1050 char *str = NULL;
1051 int rc;
1052
1053 /* Set Control Path */
1054 rc = ssh_options_set(session,
1055 SSH_OPTIONS_CONTROL_PATH,
1056 "/tmp/ssh-%r@%h:%p");
1057 assert_int_equal(rc, 0);
1058
1059 assert_string_equal(session->opts.control_path, "/tmp/ssh-%r@%h:%p");
1060
1061 rc = ssh_options_get(session, SSH_OPTIONS_CONTROL_PATH, &str);
1062 assert_int_equal(rc, 0);
1063 assert_string_equal(str, "/tmp/ssh-%r@%h:%p");
1064
1065 /* Disable Multiplexing */
1066 rc = ssh_options_set(session, SSH_OPTIONS_CONTROL_PATH, "none");
1067 assert_int_equal(rc, 0);
1068
1069 assert_null(session->opts.control_path);
1070 SSH_STRING_FREE_CHAR(str);
1071 }
1072
1073 static void torture_options_config_host(void **state)
1074 {
1075 ssh_session session = *state;
1076 FILE *config = NULL;
1077
1078 /* create a new config file */
1079 config = fopen("test_config", "w");
1080 assert_non_null(config);
1081 fputs("Host testhost1\nPort 42\n"
1082 "Host testhost2,testhost3\nPort 43\n"
1083 "Host testhost4 testhost5\nPort 44\n",
1084 config);
1085 fclose(config);
1086
1087 ssh_options_set(session, SSH_OPTIONS_HOST, "testhost1");
1088 ssh_options_parse_config(session, "test_config");
1089
1090 assert_int_equal(session->opts.port, 42);
1091
1092 torture_reset_config(session);
1093 ssh_options_set(session, SSH_OPTIONS_HOST, "testhost2");
1094 ssh_options_parse_config(session, "test_config");
1095 assert_int_equal(session->opts.port, 43);
1096
1097 session->opts.port = 0;
1098
1099 torture_reset_config(session);
1100 ssh_options_set(session, SSH_OPTIONS_HOST, "testhost3");
1101 ssh_options_parse_config(session, "test_config");
1102 assert_int_equal(session->opts.port, 43);
1103
1104 torture_reset_config(session);
1105 ssh_options_set(session, SSH_OPTIONS_HOST, "testhost4");
1106 ssh_options_parse_config(session, "test_config");
1107 assert_int_equal(session->opts.port, 44);
1108
1109 session->opts.port = 0;
1110
1111 torture_reset_config(session);
1112 ssh_options_set(session, SSH_OPTIONS_HOST, "testhost5");
1113 ssh_options_parse_config(session, "test_config");
1114 assert_int_equal(session->opts.port, 44);
1115
1116 unlink("test_config");
1117 }
1118
1119 static void torture_options_config_match(void **state)
1120 {
1121 ssh_session session = *state;
1122 char *localuser = NULL;
1123 FILE *config = NULL;
1124 int rv;
1125
1126 /* Required for options_parse_config() */
1127 ssh_options_set(session, SSH_OPTIONS_HOST, "testhost1");
1128
1129 /* The Match keyword requires argument */
1130 config = fopen("test_config", "w");
1131 assert_non_null(config);
1132 fputs("Match\n",
1133 config);
1134 fclose(config);
1135
1136 rv = ssh_options_parse_config(session, "test_config");
1137 assert_ssh_return_code_equal(session, rv, SSH_OK);
1138
1139 /* The Match all keyword needs to be the only one (start) */
1140 torture_reset_config(session);
1141 config = fopen("test_config", "w");
1142 assert_non_null(config);
1143 fputs("Match all host local\n",
1144 config);
1145 fclose(config);
1146
1147 rv = ssh_options_parse_config(session, "test_config");
1148 assert_ssh_return_code_equal(session, rv, SSH_ERROR);
1149
1150 /* The Match all keyword needs to be the only one (end) */
1151 torture_reset_config(session);
1152 config = fopen("test_config", "w");
1153 assert_non_null(config);
1154 fputs("Match host local all\n",
1155 config);
1156 fclose(config);
1157
1158 rv = ssh_options_parse_config(session, "test_config");
1159 assert_ssh_return_code_equal(session, rv, SSH_ERROR);
1160
1161 /* The Match host keyword requires an argument */
1162 torture_reset_config(session);
1163 config = fopen("test_config", "w");
1164 assert_non_null(config);
1165 fputs("Match host\n",
1166 config);
1167 fclose(config);
1168
1169 rv = ssh_options_parse_config(session, "test_config");
1170 assert_ssh_return_code_equal(session, rv, SSH_ERROR);
1171
1172 /* The Match user keyword requires an argument */
1173 torture_reset_config(session);
1174 config = fopen("test_config", "w");
1175 assert_non_null(config);
1176 fputs("Match user\n",
1177 config);
1178 fclose(config);
1179
1180 rv = ssh_options_parse_config(session, "test_config");
1181 assert_ssh_return_code_equal(session, rv, SSH_ERROR);
1182
1183 /* The Match canonical keyword is the same as match all */
1184 torture_reset_config(session);
1185 config = fopen("test_config", "w");
1186 assert_non_null(config);
1187 fputs("Match canonical\n"
1188 "\tPort 33\n"
1189 "Match all\n"
1190 "\tPort 34\n",
1191 config);
1192 fclose(config);
1193
1194 rv = ssh_options_parse_config(session, "test_config");
1195 assert_ssh_return_code_equal(session, rv, SSH_OK);
1196 assert_int_equal(session->opts.port, 33);
1197
1198 session->opts.port = 0;
1199
1200 /* The Match originalhost keyword is ignored */
1201 torture_reset_config(session);
1202 config = fopen("test_config", "w");
1203 assert_non_null(config);
1204 fputs("Match originalhost origin\n"
1205 "\tPort 33\n"
1206 "Match all\n"
1207 "\tPort 34\n",
1208 config);
1209 fclose(config);
1210
1211 rv = ssh_options_parse_config(session, "test_config");
1212 assert_ssh_return_code(session, rv);
1213 assert_int_equal(session->opts.port, 34);
1214
1215 session->opts.port = 0;
1216
1217 /* The Match localuser keyword */
1218 torture_reset_config(session);
1219 config = fopen("test_config", "w");
1220 assert_non_null(config);
1221 fputs("Match localuser ", config);
1222 localuser = ssh_get_local_username();
1223 assert_non_null(localuser);
1224 fputs(localuser, config);
1225 ssh_string_free_char(localuser);
1226 fputs("\n"
1227 "\tPort 33\n"
1228 "Match all\n"
1229 "\tPort 34\n",
1230 config);
1231 fclose(config);
1232
1233 rv = ssh_options_parse_config(session, "test_config");
1234 assert_ssh_return_code(session, rv);
1235 assert_int_equal(session->opts.port, 33);
1236
1237 session->opts.port = 0;
1238
1239 /* The Match exec keyword */
1240 torture_reset_config(session);
1241 config = fopen("test_config", "w");
1242 assert_non_null(config);
1243 fputs("Match exec true\n"
1244 "\tPort 33\n"
1245 "Match all\n"
1246 "\tPort 34\n",
1247 config);
1248 fclose(config);
1249
1250 rv = ssh_options_parse_config(session, "test_config");
1251 assert_ssh_return_code(session, rv);
1252 #ifndef WITH_EXEC
1253 /* The match exec is not supported on windows at this moment */
1254 assert_int_equal(session->opts.port, 34);
1255 #else
1256 assert_int_equal(session->opts.port, 33);
1257 #endif
1258
1259 session->opts.port = 0;
1260
1261 /* Commands containing whitespace characters must be quoted. */
1262 torture_reset_config(session);
1263 config = fopen("test_config", "w");
1264 assert_non_null(config);
1265 fputs("Match exec \"true 1\"\n"
1266 "\tPort 33\n"
1267 "Match all\n"
1268 "\tPort 34\n",
1269 config);
1270 fclose(config);
1271
1272 rv = ssh_options_parse_config(session, "test_config");
1273 assert_ssh_return_code(session, rv);
1274 #ifndef WITH_EXEC
1275 /* The match exec is not supported on windows at this moment */
1276 assert_int_equal(session->opts.port, 34);
1277 #else
1278 assert_int_equal(session->opts.port, 33);
1279 #endif
1280
1281 session->opts.port = 0;
1282
1283 unlink("test_config");
1284 }
1285
1286 static void torture_options_config_match_multi(void **state)
1287 {
1288 ssh_session session = *state;
1289 FILE *config = NULL;
1290 struct stat sb;
1291 int rv;
1292
1293 /* Required for options_parse_config() */
1294 ssh_options_set(session, SSH_OPTIONS_HOST, "testhost1");
1295
1296 /* Exec is not executed when it can not be matched */
1297 torture_reset_config(session);
1298 config = fopen("test_config", "w");
1299 assert_non_null(config);
1300 fputs("Match host wronghost exec \"touch test_config_wrong\"\n"
1301 "\tPort 33\n"
1302 "Match all\n"
1303 "\tPort 34\n",
1304 config);
1305 fclose(config);
1306
1307 rv = ssh_options_parse_config(session, "test_config");
1308 assert_ssh_return_code(session, rv);
1309 assert_int_equal(session->opts.port, 34);
1310 assert_int_equal(stat("test_config_wrong", &sb), -1);
1311
1312 session->opts.port = 0;
1313
1314 /* After matching exec, other conditions can be used */
1315 torture_reset_config(session);
1316 config = fopen("test_config", "w");
1317 assert_non_null(config);
1318 fputs("Match exec true host testhost1\n"
1319 "\tPort 33\n"
1320 "Match all\n"
1321 "\tPort 34\n",
1322 config);
1323 fclose(config);
1324
1325 rv = ssh_options_parse_config(session, "test_config");
1326 assert_ssh_return_code(session, rv);
1327 #ifndef WITH_EXEC
1328 /* The match exec is not supported on windows at this moment */
1329 assert_int_equal(session->opts.port, 34);
1330 #else
1331 assert_int_equal(session->opts.port, 33);
1332 #endif
1333
1334 /* After matching exec, other conditions can be used */
1335 torture_reset_config(session);
1336 config = fopen("test_config", "w");
1337 assert_non_null(config);
1338 fputs("Match exec true host otherhost\n"
1339 "\tPort 33\n"
1340 "Match all\n"
1341 "\tPort 34\n",
1342 config);
1343 fclose(config);
1344
1345 rv = ssh_options_parse_config(session, "test_config");
1346 assert_ssh_return_code(session, rv);
1347 assert_int_equal(session->opts.port, 34);
1348
1349 unlink("test_config");
1350 }
1351
1352 static void torture_options_copy(void **state)
1353 {
1354 ssh_session session = *state, new = NULL;
1355 struct ssh_iterator *it = NULL, *it2 = NULL;
1356 FILE *config = NULL;
1357 int i, level = 9;
1358 int rv;
1359
1360 /* Required for options_parse_config() */
1361 ssh_options_set(session, SSH_OPTIONS_HOST, "example");
1362
1363 /* Impossible to set through the configuration */
1364 rv = ssh_options_set(session, SSH_OPTIONS_COMPRESSION_LEVEL, &level);
1365 assert_ssh_return_code(session, rv);
1366 level = 1;
1367 rv = ssh_options_set(session, SSH_OPTIONS_NODELAY, &level);
1368 assert_ssh_return_code(session, rv);
1369
1370 /* The Match keyword requires argument */
1371 config = fopen("test_config", "w");
1372 assert_non_null(config);
1373 fputs("IdentityFile ~/.ssh/id_ecdsa\n"
1374 "IdentityFile ~/.ssh/my_rsa\n"
1375 "CertificateFile ~/.ssh/my_rsa-cert.pub\n"
1376 "CertificateFile ~/.ssh/id_ecdsa-cert.pub\n"
1377 "User tester\n"
1378 "Hostname example.com\n"
1379 "BindAddress 127.0.0.2\n"
1380 "GlobalKnownHostsFile /etc/ssh/known_hosts2\n"
1381 "UserKnownHostsFile ~/.ssh/known_hosts2\n"
1382 "KexAlgorithms curve25519-sha256,[email protected],ecdh-sha2-nistp521\n"
1383 "Ciphers aes256-ctr\n"
1384 "MACs hmac-sha2-256\n"
1385 "HostKeyAlgorithms ssh-ed25519,ecdsa-sha2-nistp521\n"
1386 "Compression yes\n"
1387 "PubkeyAcceptedAlgorithms ssh-ed25519,ecdsa-sha2-nistp521\n"
1388 "ProxyCommand nc 127.0.0.10 22\n"
1389 "ControlMaster ask\n"
1390 "ControlPath /tmp/ssh-%r@%h:%p\n"
1391 /* ops.custombanner */
1392 "ConnectTimeout 42\n"
1393 "Port 222\n"
1394 "StrictHostKeyChecking no\n"
1395 "GSSAPIServerIdentity my.example.com\n"
1396 "GSSAPIClientIdentity home.sweet\n"
1397 "GSSAPIDelegateCredentials yes\n"
1398 "PubkeyAuthentication yes\n" /* sets flags */
1399 "GSSAPIAuthentication no\n" /* sets flags */
1400 "AddressFamily inet6\n"
1401 "",
1402 config);
1403 fclose(config);
1404
1405 rv = ssh_options_parse_config(session, "test_config");
1406 assert_ssh_return_code(session, rv);
1407
1408 rv = ssh_options_copy(session, &new);
1409 assert_ssh_return_code(session, rv);
1410 assert_non_null(new);
1411
1412 /* Check the identities match */
1413 it = ssh_list_get_iterator(session->opts.identity_non_exp);
1414 assert_non_null(it);
1415 it2 = ssh_list_get_iterator(new->opts.identity_non_exp);
1416 assert_non_null(it2);
1417 while (it != NULL && it2 != NULL) {
1418 assert_string_equal(it->data, it2->data);
1419 it = it->next;
1420 it2 = it2->next;
1421 }
1422 assert_null(it);
1423 assert_null(it2);
1424
1425 /* Check the certificates match */
1426 it = ssh_list_get_iterator(session->opts.certificate_non_exp);
1427 assert_non_null(it);
1428 it2 = ssh_list_get_iterator(new->opts.certificate_non_exp);
1429 assert_non_null(it2);
1430 while (it != NULL && it2 != NULL) {
1431 assert_string_equal(it->data, it2->data);
1432 it = it->next;
1433 it2 = it2->next;
1434 }
1435 assert_null(it);
1436 assert_null(it2);
1437
1438 assert_string_equal(session->opts.username, new->opts.username);
1439 assert_string_equal(session->opts.host, new->opts.host);
1440 assert_string_equal(session->opts.bindaddr, new->opts.bindaddr);
1441 assert_string_equal(session->opts.sshdir, new->opts.sshdir);
1442 assert_string_equal(session->opts.knownhosts, new->opts.knownhosts);
1443 assert_string_equal(session->opts.global_knownhosts,
1444 new->opts.global_knownhosts);
1445 for (i = 0; i < SSH_KEX_METHODS; i++) {
1446 if (session->opts.wanted_methods[i] == NULL) {
1447 assert_null(new->opts.wanted_methods[i]);
1448 } else {
1449 assert_string_equal(session->opts.wanted_methods[i],
1450 new->opts.wanted_methods[i]);
1451 }
1452 }
1453 assert_string_equal(session->opts.pubkey_accepted_types,
1454 new->opts.pubkey_accepted_types);
1455 assert_string_equal(session->opts.ProxyCommand, new->opts.ProxyCommand);
1456 assert_null(new->opts.control_path);
1457 /* TODO custombanner */
1458 assert_int_equal(session->opts.timeout, new->opts.timeout);
1459 assert_int_equal(session->opts.timeout_usec, new->opts.timeout_usec);
1460 assert_int_equal(session->opts.port, new->opts.port);
1461 assert_int_equal(session->opts.control_master, new->opts.control_master);
1462 assert_int_equal(session->opts.StrictHostKeyChecking,
1463 new->opts.StrictHostKeyChecking);
1464 assert_int_equal(session->opts.compressionlevel,
1465 new->opts.compressionlevel);
1466 assert_string_equal(session->opts.gss_server_identity,
1467 new->opts.gss_server_identity);
1468 assert_string_equal(session->opts.gss_client_identity,
1469 new->opts.gss_client_identity);
1470 assert_int_equal(session->opts.gss_delegate_creds,
1471 new->opts.gss_delegate_creds);
1472 assert_int_equal(session->opts.flags, new->opts.flags);
1473 assert_int_equal(session->opts.nodelay, new->opts.nodelay);
1474 assert_true(session->opts.config_processed == new->opts.config_processed);
1475 assert_memory_equal(session->opts.options_seen, new->opts.options_seen,
1476 sizeof(session->opts.options_seen));
1477 assert_int_equal(session->opts.address_family, new->opts.address_family);
1478
1479 ssh_free(new);
1480
1481 /* test if ssh_options_apply was called before ssh_options_copy
1482 * the opts.identity list gets copied (percent expanded list) */
1483 rv = ssh_options_apply(session);
1484 assert_ssh_return_code(session, rv);
1485
1486 rv = ssh_options_copy(session, &new);
1487 assert_ssh_return_code(session, rv);
1488 assert_non_null(new);
1489
1490 it = ssh_list_get_iterator(session->opts.identity_non_exp);
1491 assert_null(it);
1492 it2 = ssh_list_get_iterator(new->opts.identity_non_exp);
1493 assert_null(it2);
1494
1495 it = ssh_list_get_iterator(session->opts.identity);
1496 assert_non_null(it);
1497 it2 = ssh_list_get_iterator(new->opts.identity);
1498 assert_non_null(it2);
1499 while (it != NULL && it2 != NULL) {
1500 assert_string_equal(it->data, it2->data);
1501 it = it->next;
1502 it2 = it2->next;
1503 }
1504 assert_null(it);
1505 assert_null(it2);
1506
1507 ssh_free(new);
1508 }
1509
1510 #define EXECUTABLE_NAME "test-exec"
1511 static void torture_options_getopt(void **state)
1512 {
1513 ssh_session session = *state;
1514 int rc;
1515 int previous_level, new_level;
1516 const char *argv[] = {EXECUTABLE_NAME, "-l", "username", "-p", "222",
1517 "-vv", "-v", "-r", "-c", "aes128-ctr",
1518 "-i", "id_rsa", "-C", "-2", "-1", NULL};
1519 int argc = sizeof(argv)/sizeof(char *) - 1;
1520 previous_level = ssh_get_log_level();
1521
1522 /* Test with all the supported options */
1523 rc = ssh_options_getopt(session, &argc, (char **)argv);
1524 #ifdef _MSC_VER
1525 UNUSED_VAR(new_level);
1526
1527 /* Not supported in windows */
1528 assert_ssh_return_code_equal(session, rc, -1);
1529 #else
1530 assert_ssh_return_code(session, rc);
1531
1532 /* Restore the log level to previous value first */
1533 new_level = ssh_get_log_level();
1534 assert_int_equal(new_level, 3); /* 2 + 1 -v's */
1535 rc = ssh_set_log_level(previous_level);
1536 assert_int_equal(rc, SSH_OK);
1537
1538 assert_ssh_return_code(session, rc);
1539 assert_string_equal(session->opts.username, "username");
1540 assert_int_equal(session->opts.port, 222);
1541 /* The -r (usersa) is noop */
1542 assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S],
1543 "aes128-ctr");
1544 assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_S_C],
1545 "aes128-ctr");
1546 assert_string_equal(session->opts.identity_non_exp->root->data, "id_rsa");
1547 #ifdef WITH_ZLIB
1548 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
1549 "[email protected],none");
1550 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
1551 "[email protected],none");
1552 #else
1553 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
1554 "none");
1555 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
1556 "none");
1557 #endif
1558 /* -1 and -2 are noop */
1559
1560
1561 /* It should ignore unknown arguments */
1562 argv[1] = "-F";
1563 argv[2] = "config_file";
1564 argv[3] = NULL;
1565 argc = 3;
1566 rc = ssh_options_getopt(session, &argc, (char **)argv);
1567 assert_ssh_return_code(session, rc);
1568 assert_int_equal(argc, 3);
1569 assert_string_equal(argv[0], EXECUTABLE_NAME);
1570 assert_string_equal(argv[1], "-F");
1571 assert_string_equal(argv[2], "config_file");
1572
1573
1574 /* It should not mess with unknown arguments order */
1575 argv[1] = "-F";
1576 argv[2] = "config_file";
1577 argv[3] = "-M";
1578 argv[4] = "hmac-sha1";
1579 argv[5] = "-X";
1580 argv[6] = NULL;
1581 argc = 6;
1582 rc = ssh_options_getopt(session, &argc, (char **)argv);
1583 assert_ssh_return_code(session, rc);
1584 assert_int_equal(argc, 6);
1585 assert_string_equal(argv[0], EXECUTABLE_NAME);
1586 assert_string_equal(argv[1], "-F");
1587 assert_string_equal(argv[2], "config_file");
1588 assert_string_equal(argv[3], "-M");
1589 assert_string_equal(argv[4], "hmac-sha1");
1590 assert_string_equal(argv[5], "-X");
1591
1592
1593 /* Trailing arguments should be passed as they are */
1594 argv[1] = "-F";
1595 argv[2] = "config_file";
1596 argv[3] = "-M";
1597 argv[4] = "hmac-sha1";
1598 argv[5] = "example.com";
1599 argv[6] = NULL;
1600 argc = 6;
1601 rc = ssh_options_getopt(session, &argc, (char **)argv);
1602 assert_ssh_return_code(session, rc);
1603 assert_int_equal(argc, 6);
1604 assert_string_equal(argv[0], EXECUTABLE_NAME);
1605 assert_string_equal(argv[1], "-F");
1606 assert_string_equal(argv[2], "config_file");
1607 assert_string_equal(argv[3], "-M");
1608 assert_string_equal(argv[4], "hmac-sha1");
1609 assert_string_equal(argv[5], "example.com");
1610
1611 /* Corner case: only one argument */
1612 argv[1] = "-C";
1613 argv[2] = NULL;
1614 argc = 2;
1615 rc = ssh_options_set(session, SSH_OPTIONS_COMPRESSION, "no");
1616 assert_ssh_return_code(session, rc);
1617 #ifdef WITH_ZLIB
1618 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
1619 "none,[email protected]");
1620 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
1621 "none,[email protected]");
1622 #else
1623 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
1624 "none");
1625 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
1626 "none");
1627 #endif
1628
1629 rc = ssh_options_getopt(session, &argc, (char **)argv);
1630 assert_ssh_return_code(session, rc);
1631 assert_int_equal(argc, 1);
1632 assert_string_equal(argv[0], EXECUTABLE_NAME);
1633 #ifdef WITH_ZLIB
1634 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
1635 "[email protected],none");
1636 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
1637 "[email protected],none");
1638 #else
1639 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
1640 "none");
1641 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
1642 "none");
1643 #endif
1644
1645 /* Corner case: only hostname is not parsed */
1646 argv[1] = "example.com";
1647 argv[2] = NULL;
1648 argc = 2;
1649 rc = ssh_options_getopt(session, &argc, (char **)argv);
1650 assert_ssh_return_code(session, rc);
1651 assert_int_equal(argc, 2);
1652 assert_string_equal(argv[0], EXECUTABLE_NAME);
1653 assert_string_equal(argv[1], "example.com");
1654
1655 /* Corner case: no arguments */
1656 argv[1] = NULL;
1657 argc = 1;
1658 rc = ssh_options_getopt(session, &argc, (char **)argv);
1659 assert_ssh_return_code(session, rc);
1660 assert_int_equal(argc, 1);
1661 assert_string_equal(argv[0], EXECUTABLE_NAME);
1662
1663 #endif /* _NSC_VER */
1664 }
1665
1666 static void torture_options_getopt_o_option(void **state)
1667 {
1668 #ifndef _MSC_VER
1669 ssh_session session = *state;
1670 int rc;
1671 enum ssh_config_opcode_e opcode =
1672 ssh_config_get_opcode((char *)"compression");
1673 const char *argv[6] = {EXECUTABLE_NAME, "-o", "Compression nah", NULL};
1674 int argc = 3;
1675
1676 // Test: -o with invalid value (e.g., "-o Compression nah")
1677 rc = ssh_options_getopt(session, &argc, (char **)argv);
1678 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1679
1680 session->opts.options_seen[opcode] = 0;
1681
1682 // Test: -o with valid value (e.g., "-o Compression yes")
1683 argv[1] = "-o";
1684 argv[2] = "compression yes";
1685 argv[3] = NULL;
1686 argc = 3;
1687
1688 rc = ssh_options_getopt(session, &argc, (char **)argv);
1689 assert_ssh_return_code(session, rc);
1690 assert_int_equal(session->opts.options_seen[opcode], 1);
1691
1692 #ifdef WITH_ZLIB
1693 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
1694 "[email protected],none");
1695 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
1696 "[email protected],none");
1697 #else
1698 assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S], "none");
1699 assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C], "none");
1700 #endif
1701
1702 // Test: -o with missing value (e.g., "-o =")
1703 argv[1] = "-o";
1704 argv[2] = "=";
1705 argv[3] = NULL;
1706 argc = 3;
1707 rc = ssh_options_getopt(session, &argc, (char **)argv);
1708 assert_ssh_return_code(session, rc);
1709
1710 // Test: -o with only option name, no value (e.g., "-o Compression")
1711 session->opts.options_seen[opcode] = 0;
1712 argv[1] = "-o";
1713 argv[2] = "Compression";
1714 argv[3] = NULL;
1715 argc = 3;
1716 rc = ssh_options_getopt(session, &argc, (char **)argv);
1717 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1718
1719 // Test: -o with empty string (e.g., "-o ")
1720 argv[1] = "-o";
1721 argv[2] = "";
1722 argv[3] = NULL;
1723 argc = 3;
1724 rc = ssh_options_getopt(session, &argc, (char **)argv);
1725 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1726
1727 // Test: -o with unsupported option on the cli
1728 argv[1] = "-o";
1729 argv[2] = "match *";
1730 argv[3] = NULL;
1731 argc = 3;
1732
1733 rc = ssh_options_getopt(session, &argc, (char **)argv);
1734 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1735
1736 // Test: multiple -o options together, one invalid
1737 session->opts.options_seen[opcode] = 0;
1738 argv[1] = "-o";
1739 argv[2] = "compression yes";
1740 argv[3] = "-o";
1741 argv[4] = "enablesshkeysign yes";
1742 argv[5] = NULL;
1743 argc = 5;
1744
1745 rc = ssh_options_getopt(session, &argc, (char **)argv);
1746 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1747
1748 // Test: multiple -o options together, all valid
1749 session->opts.options_seen[opcode] = 0;
1750 argv[1] = "-o";
1751 argv[2] = "compression no";
1752 argv[3] = "-o";
1753 argv[4] = "rekeylimit 1G 1h";
1754 argv[5] = NULL;
1755 argc = 5;
1756
1757 rc = ssh_options_getopt(session, &argc, (char **)argv);
1758 assert_ssh_return_code(session, rc);
1759
1760 opcode = ssh_config_get_opcode((char *)"compression");
1761 assert_int_equal(session->opts.options_seen[opcode], 1);
1762
1763 opcode = ssh_config_get_opcode((char *)"rekeylimit");
1764 assert_int_equal(session->opts.options_seen[opcode], 1);
1765 #endif /* _MSC_VER */
1766 }
1767
1768 static void torture_options_plus_sign(void **state)
1769 {
1770 ssh_session session = *state;
1771 int rc;
1772 const char *def_host_alg, *alg, *algs;
1773 char *awaited;
1774 size_t alg_len, algs_len;
1775
1776 if (ssh_fips_mode()) {
1777 alg = ",[email protected]";
1778 algs = ",[email protected],[email protected],ecdsa-sha2-nistp521";
1779 def_host_alg = ssh_kex_get_fips_methods(SSH_HOSTKEYS);
1780 } else {
1781 alg = ",ssh-rsa";
1782 algs = ",ssh-rsa,[email protected]";
1783 def_host_alg = ssh_kex_get_default_methods(SSH_HOSTKEYS);
1784 }
1785 alg_len = strlen(alg);
1786 algs_len = strlen(algs);
1787
1788 /* in fips mode, the default list is the available list, which means
1789 * we can't append anything because everything enabled is already
1790 * included */
1791 if (ssh_fips_mode()) {
1792 awaited = strdup(def_host_alg);
1793 assert_non_null(awaited);
1794 } else {
1795 awaited = calloc(strlen(def_host_alg) + alg_len + 1, 1);
1796 assert_non_null(awaited);
1797
1798 memcpy(awaited, def_host_alg, strlen(def_host_alg));
1799 memcpy(awaited+strlen(def_host_alg), alg, alg_len);
1800 }
1801
1802 if (ssh_fips_mode()) {
1803 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "[email protected]");
1804 } else {
1805 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa");
1806 }
1807 assert_ssh_return_code(session, rc);
1808 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1809 awaited);
1810
1811 if (!ssh_fips_mode()) {
1812 /* different algorithm list is used here */
1813 free(awaited);
1814
1815 awaited = calloc(strlen(def_host_alg) + algs_len + 1, 1);
1816 assert_non_null(awaited);
1817 memcpy(awaited, def_host_alg, strlen(def_host_alg));
1818 memcpy(awaited+strlen(def_host_alg), algs, algs_len);
1819 }
1820
1821 if (ssh_fips_mode()) {
1822 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS,
1823 "[email protected],[email protected],ecdsa-sha2-nistp521");
1824 } else {
1825 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS,
1826 "+ssh-rsa,[email protected]");
1827 }
1828 assert_ssh_return_code(session, rc);
1829 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1830 awaited);
1831
1832 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+");
1833 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1834
1835 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+blablabla");
1836 assert_ssh_return_code(session, rc);
1837 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1838 def_host_alg);
1839
1840 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, NULL);
1841 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1842
1843 free(awaited);
1844 }
1845
1846 static void torture_options_minus_sign(void **state)
1847 {
1848 ssh_session session = *state;
1849 int rc;
1850 const char *def_host_alg, *alg, *algs;
1851 char *awaited, *p;
1852 size_t alg_len, algs_len;
1853
1854 if (ssh_fips_mode()) {
1855 alg = "[email protected],";
1856 algs = "[email protected],ecdsa-sha2-nistp521,";
1857 def_host_alg = ssh_kex_get_fips_methods(SSH_HOSTKEYS);
1858 } else {
1859 alg = "ssh-ed25519,";
1860 algs = "ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,";
1861 def_host_alg = ssh_kex_get_default_methods(SSH_HOSTKEYS);
1862 }
1863 alg_len = strlen(alg);
1864 algs_len = strlen(algs);
1865
1866 awaited = calloc(strlen(def_host_alg) + 1, 1);
1867 assert_non_null(awaited);
1868
1869 memcpy(awaited, def_host_alg, strlen(def_host_alg));
1870 p = strstr(awaited, alg);
1871 assert_non_null(p);
1872 memmove(p, p+alg_len, strlen(p + alg_len) + 1);
1873
1874 if (ssh_fips_mode()) {
1875 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "[email protected]");
1876 } else {
1877 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "-ssh-ed25519");
1878 }
1879 assert_ssh_return_code(session, rc);
1880 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1881 awaited);
1882
1883 p = strstr(awaited, algs);
1884 assert_non_null(p);
1885 memmove(p, p+algs_len, strlen(p + algs_len) + 1);
1886
1887 if (ssh_fips_mode()) {
1888 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "[email protected],[email protected],ecdsa-sha2-nistp521");
1889 } else {
1890 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "-ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384");
1891 }
1892 assert_ssh_return_code(session, rc);
1893 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1894 awaited);
1895
1896 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "-");
1897 assert_ssh_return_code(session, rc);
1898 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1899 def_host_alg);
1900
1901 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "-blablabla");
1902 assert_ssh_return_code(session, rc);
1903 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1904 def_host_alg);
1905
1906 free(awaited);
1907 }
1908
1909 static void torture_options_caret_sign(void **state)
1910 {
1911 ssh_session session = *state;
1912 int rc;
1913 const char *def_host_alg, *alg, *algs;
1914 size_t alg_len, algs_len;
1915 char *awaited, *p;
1916
1917 if (ssh_fips_mode()) {
1918 alg = "[email protected],";
1919 algs = "[email protected],[email protected],ecdsa-sha2-nistp521,";
1920 def_host_alg = ssh_kex_get_fips_methods(SSH_HOSTKEYS);
1921 } else {
1922 alg = "ssh-rsa,";
1923 algs = "ssh-rsa,[email protected],";
1924 def_host_alg = ssh_kex_get_default_methods(SSH_HOSTKEYS);
1925 }
1926 alg_len = strlen(alg);
1927 algs_len = strlen(algs);
1928
1929 awaited = calloc(strlen(def_host_alg) + alg_len + 1, 1);
1930 assert_non_null(awaited);
1931
1932 memcpy(awaited, alg, alg_len);
1933 memcpy(awaited+alg_len, def_host_alg, strlen(def_host_alg));
1934 if (ssh_fips_mode()) {
1935 p = strstr(awaited, alg);
1936 /* look for second occurrence */
1937 p = strstr(p+1, algs);
1938 memmove(p, p+alg_len, strlen(p + alg_len) + 1);
1939 }
1940
1941 if (ssh_fips_mode()) {
1942 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "^[email protected]");
1943 } else {
1944 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "^ssh-rsa");
1945 }
1946 assert_ssh_return_code(session, rc);
1947 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1948 awaited);
1949 /* different algorithm list is used here */
1950 free(awaited);
1951
1952 awaited = calloc(strlen(def_host_alg) + algs_len + 1, 1);
1953 assert_non_null(awaited);
1954 memcpy(awaited, algs, algs_len);
1955 memcpy(awaited+algs_len, def_host_alg, strlen(def_host_alg));
1956 if (ssh_fips_mode()) {
1957 p = strstr(awaited, algs);
1958 /* look for second occurrence */
1959 p = strstr(p+1, algs);
1960 memmove(p, p+algs_len, strlen(p + algs_len) + 1);
1961 }
1962
1963 if (ssh_fips_mode()) {
1964 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS,
1965 "^[email protected],[email protected],ecdsa-sha2-nistp521");
1966 } else {
1967 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS,
1968 "^ssh-rsa,[email protected]");
1969 }
1970 assert_ssh_return_code(session, rc);
1971 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1972 awaited);
1973
1974 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "^");
1975 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
1976
1977 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "^blablabla");
1978 assert_ssh_return_code(session, rc);
1979 assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
1980 def_host_alg);
1981
1982 free(awaited);
1983 }
1984
1985 static void torture_options_apply (void **state)
1986 {
1987 ssh_session session = *state;
1988 struct ssh_list *awaited_list = NULL;
1989 struct ssh_iterator *it1 = NULL, *it2 = NULL;
1990 char *id = NULL;
1991 int rc;
1992
1993 rc = ssh_options_set(session,
1994 SSH_OPTIONS_KNOWNHOSTS,
1995 "%%d/.ssh/known_hosts");
1996 assert_ssh_return_code(session, rc);
1997
1998 rc = ssh_options_set(session,
1999 SSH_OPTIONS_GLOBAL_KNOWNHOSTS,
2000 "/etc/%%u/libssh/known_hosts");
2001 assert_ssh_return_code(session, rc);
2002
2003 rc = ssh_options_set(session,
2004 SSH_OPTIONS_PROXYCOMMAND,
2005 "exec echo \"Hello libssh %%d!\"");
2006 assert_ssh_return_code(session, rc);
2007
2008 rc = ssh_options_set(session,
2009 SSH_OPTIONS_ADD_IDENTITY,
2010 "%%d/do_not_expand");
2011 assert_ssh_return_code(session, rc);
2012
2013 rc = ssh_options_apply(session);
2014 assert_ssh_return_code(session, rc);
2015
2016 /* check that the values got expanded */
2017 assert_true(session->opts.exp_flags & SSH_OPT_EXP_FLAG_KNOWNHOSTS);
2018 assert_true(session->opts.exp_flags & SSH_OPT_EXP_FLAG_GLOBAL_KNOWNHOSTS);
2019 assert_true(session->opts.exp_flags & SSH_OPT_EXP_FLAG_PROXYCOMMAND);
2020 assert_true(ssh_list_count(session->opts.identity_non_exp) == 0);
2021 assert_true(ssh_list_count(session->opts.identity) > 0);
2022
2023 /* should not change anything calling it again */
2024 rc = ssh_options_apply(session);
2025 assert_ssh_return_code(session, rc);
2026
2027 /* check that the expansion was done only once */
2028 assert_string_equal(session->opts.knownhosts, "%d/.ssh/known_hosts");
2029 assert_string_equal(session->opts.global_knownhosts,
2030 "/etc/%u/libssh/known_hosts");
2031 /* no exec should be added if there already is one */
2032 assert_string_equal(session->opts.ProxyCommand,
2033 "exec echo \"Hello libssh %d!\"");
2034 assert_string_equal(session->opts.identity->root->data,
2035 "%d/do_not_expand");
2036
2037 /* apply should keep the freshest setting */
2038 rc = ssh_options_set(session,
2039 SSH_OPTIONS_KNOWNHOSTS,
2040 "hello there");
2041 assert_ssh_return_code(session, rc);
2042
2043 rc = ssh_options_set(session,
2044 SSH_OPTIONS_GLOBAL_KNOWNHOSTS,
2045 "lorem ipsum");
2046 assert_ssh_return_code(session, rc);
2047
2048 rc = ssh_options_set(session,
2049 SSH_OPTIONS_PROXYCOMMAND,
2050 "mission_impossible");
2051 assert_ssh_return_code(session, rc);
2052
2053 rc = ssh_options_set(session,
2054 SSH_OPTIONS_ADD_IDENTITY,
2055 "007");
2056 assert_ssh_return_code(session, rc);
2057
2058 rc = ssh_options_set(session,
2059 SSH_OPTIONS_ADD_IDENTITY,
2060 "3");
2061 assert_ssh_return_code(session, rc);
2062
2063 rc = ssh_options_set(session,
2064 SSH_OPTIONS_ADD_IDENTITY,
2065 "2");
2066 assert_ssh_return_code(session, rc);
2067
2068 rc = ssh_options_set(session,
2069 SSH_OPTIONS_ADD_IDENTITY,
2070 "1");
2071 assert_ssh_return_code(session, rc);
2072
2073 /* check that flags show need of escape expansion */
2074 assert_false(session->opts.exp_flags & SSH_OPT_EXP_FLAG_KNOWNHOSTS);
2075 assert_false(session->opts.exp_flags & SSH_OPT_EXP_FLAG_GLOBAL_KNOWNHOSTS);
2076 assert_false(session->opts.exp_flags & SSH_OPT_EXP_FLAG_PROXYCOMMAND);
2077 assert_false(ssh_list_count(session->opts.identity_non_exp) == 0);
2078
2079 rc = ssh_options_apply(session);
2080 assert_ssh_return_code(session, rc);
2081
2082 /* check that the values got expanded */
2083 assert_true(session->opts.exp_flags & SSH_OPT_EXP_FLAG_KNOWNHOSTS);
2084 assert_true(session->opts.exp_flags & SSH_OPT_EXP_FLAG_GLOBAL_KNOWNHOSTS);
2085 assert_true(session->opts.exp_flags & SSH_OPT_EXP_FLAG_PROXYCOMMAND);
2086 assert_true(ssh_list_count(session->opts.identity_non_exp) == 0);
2087
2088 assert_string_equal(session->opts.knownhosts, "hello there");
2089 assert_string_equal(session->opts.global_knownhosts, "lorem ipsum");
2090 /* check that the "exec " was added at the beginning */
2091 assert_string_equal(session->opts.ProxyCommand, "exec mission_impossible");
2092 assert_string_equal(session->opts.identity->root->data, "1");
2093
2094 /* check the order of the identity files after double expansion */
2095 awaited_list = ssh_list_new();
2096 /* append the new data in order */
2097 id = strdup("1");
2098 rc = ssh_list_append(awaited_list, id);
2099 assert_int_equal(rc, SSH_OK);
2100 id = strdup("2");
2101 rc = ssh_list_append(awaited_list, id);
2102 assert_int_equal(rc, SSH_OK);
2103 id = strdup("3");
2104 rc = ssh_list_append(awaited_list, id);
2105 assert_int_equal(rc, SSH_OK);
2106 id = strdup("007");
2107 rc = ssh_list_append(awaited_list, id);
2108 assert_int_equal(rc, SSH_OK);
2109 id = strdup("%d/do_not_expand");
2110 rc = ssh_list_append(awaited_list, id);
2111 assert_int_equal(rc, SSH_OK);
2112 /* append the defaults; this list is copied from ssh_new@src/session.c */
2113 id = ssh_path_expand_escape(session, "%d/.ssh/id_ed25519");
2114 rc = ssh_list_append(awaited_list, id);
2115 assert_int_equal(rc, SSH_OK);
2116 #ifdef HAVE_ECC
2117 id = ssh_path_expand_escape(session, "%d/.ssh/id_ecdsa");
2118 rc = ssh_list_append(awaited_list, id);
2119 assert_int_equal(rc, SSH_OK);
2120 #endif
2121 id = ssh_path_expand_escape(session, "%d/.ssh/id_rsa");
2122 rc = ssh_list_append(awaited_list, id);
2123 assert_int_equal(rc, SSH_OK);
2124 #ifdef WITH_FIDO2
2125 /* Add security key identities */
2126 id = ssh_path_expand_escape(session, "%d/.ssh/id_ed25519_sk");
2127 rc = ssh_list_append(awaited_list, id);
2128 assert_int_equal(rc, SSH_OK);
2129
2130 #ifdef HAVE_ECC
2131 id = ssh_path_expand_escape(session, "%d/.ssh/id_ecdsa_sk");
2132 rc = ssh_list_append(awaited_list, id);
2133 assert_int_equal(rc, SSH_OK);
2134 #endif /* HAVE_ECC */
2135 #endif /* WITH_FIDO2 */
2136
2137 assert_int_equal(ssh_list_count(awaited_list),
2138 ssh_list_count(session->opts.identity));
2139
2140 it1 = ssh_list_get_iterator(awaited_list);
2141 assert_non_null(it1);
2142 it2 = ssh_list_get_iterator(session->opts.identity);
2143 assert_non_null(it2);
2144 while (it1 != NULL && it2 != NULL) {
2145 assert_string_equal(it1->data, it2->data);
2146
2147 free((void*)it1->data);
2148 it1 = it1->next;
2149 it2 = it2->next;
2150 }
2151 assert_null(it1);
2152 assert_null(it2);
2153
2154 ssh_list_free(awaited_list);
2155 }
2156
2157 static void torture_options_set_verbosity (void **state)
2158 {
2159 ssh_session session = *state;
2160 int rc, new_level;
2161
2162 rc = ssh_options_set(session,
2163 SSH_OPTIONS_LOG_VERBOSITY_STR,
2164 "3");
2165 assert_int_equal(rc, SSH_OK);
2166 new_level = ssh_get_log_level();
2167 assert_int_equal(new_level, SSH_LOG_PACKET);
2168
2169 rc = ssh_options_set(session,
2170 SSH_OPTIONS_LOG_VERBOSITY_STR,
2171 "datsun");
2172 assert_int_equal(rc, -1);
2173 new_level = ssh_get_log_level();
2174 assert_int_not_equal(new_level, 0);
2175 }
2176
2177 static void torture_options_set_rsa_min_size(void **state)
2178 {
2179 ssh_session session = *state;
2180 int min_allowed = RSA_MIN_KEY_SIZE, key_size, rc;
2181
2182 /* Check that passing NULL leads to failure */
2183 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, NULL);
2184 assert_int_equal(rc, -1);
2185
2186 /*
2187 * Check that supplying a value less than the allowed minimum leads
2188 * to failure
2189 */
2190 key_size = min_allowed - 2;
2191 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &key_size);
2192 assert_int_equal(rc, -1);
2193
2194 /* Check that supplying a negative value leads to failure */
2195 key_size = -10;
2196 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &key_size);
2197 assert_int_equal(rc, -1);
2198
2199 /* Check that supplying 0 succeeds (used to revert to default) */
2200 key_size = 0;
2201 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &key_size);
2202 assert_ssh_return_code(session, rc);
2203
2204 /* Check that supplying allowed minimum succeeds */
2205 key_size = min_allowed;
2206 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &key_size);
2207 assert_ssh_return_code(session, rc);
2208
2209 /* Check that supplying a value greater than allowed minimum succeeds */
2210 key_size = min_allowed + 10;
2211 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &key_size);
2212 assert_ssh_return_code(session, rc);
2213 }
2214
2215 #ifdef WITH_SERVER
2216 const char template[] = "temp_dir_XXXXXX";
2217
2218 struct bind_st {
2219 char *cwd;
2220 char *temp_dir;
2221 ssh_bind bind;
2222 };
2223
2224 static int ssh_bind_setup_files(void **state)
2225 {
2226 struct bind_st *test_state = NULL;
2227 char *cwd = NULL;
2228 char *tmp_dir = NULL;
2229 int rc = 0;
2230
2231 test_state = (struct bind_st *)malloc(sizeof(struct bind_st));
2232 assert_non_null(test_state);
2233
2234 cwd = torture_get_current_working_dir();
2235 assert_non_null(cwd);
2236
2237 tmp_dir = torture_make_temp_dir(template);
2238 assert_non_null(tmp_dir);
2239
2240 test_state->cwd = cwd;
2241 test_state->temp_dir = tmp_dir;
2242
2243 *state = test_state;
2244
2245 rc = torture_change_dir(tmp_dir);
2246 assert_int_equal(rc, 0);
2247
2248 printf("Changed directory to: %s\n", tmp_dir);
2249
2250 /* For ed25519 the test keys are not available in legacy PEM format. Using
2251 * the new OpenSSH format for all algorithms */
2252 torture_write_file(LIBSSH_RSA_TESTKEY,
2253 torture_get_openssh_testkey(SSH_KEYTYPE_RSA, 0));
2254
2255 torture_write_file(LIBSSH_ED25519_TESTKEY,
2256 torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0));
2257 #ifdef HAVE_ECC
2258 torture_write_file(LIBSSH_ECDSA_521_TESTKEY,
2259 torture_get_openssh_testkey(SSH_KEYTYPE_ECDSA_P521, 0));
2260 #endif
2261 torture_write_file(LIBSSH_CUSTOM_BIND_CONFIG_FILE,
2262 "Port 42\n");
2263 return 0;
2264 }
2265
2266
2267 /* sshbind options */
2268 static int sshbind_setup(void **state)
2269 {
2270 int rc;
2271 struct bind_st *test_state = NULL;
2272
2273 rc = ssh_bind_setup_files((void **)&test_state);
2274 assert_int_equal(rc, 0);
2275 assert_non_null(test_state);
2276
2277 test_state->bind = ssh_bind_new();
2278 assert_non_null(test_state->bind);
2279
2280 *state = test_state;
2281
2282 return 0;
2283 }
2284
2285 static int sshbind_teardown(void **state)
2286 {
2287 struct bind_st *test_state = NULL;
2288 int rc;
2289
2290 assert_non_null(state);
2291 test_state = *((struct bind_st **)state);
2292
2293 assert_non_null(test_state);
2294 assert_non_null(test_state->cwd);
2295 assert_non_null(test_state->temp_dir);
2296 assert_non_null(test_state->bind);
2297
2298 rc = torture_change_dir(test_state->cwd);
2299 assert_int_equal(rc, 0);
2300
2301 rc = torture_rmdirs(test_state->temp_dir);
2302 assert_int_equal(rc, 0);
2303
2304 SAFE_FREE(test_state->temp_dir);
2305 SAFE_FREE(test_state->cwd);
2306 ssh_bind_free(test_state->bind);
2307 SAFE_FREE(test_state);
2308
2309 return 0;
2310 }
2311
2312 static void
2313 torture_bind_options_import_key(void **state)
2314 {
2315 struct bind_st *test_state;
2316 ssh_bind bind;
2317 int rc;
2318 const char *base64_key;
2319 ssh_key key = ssh_key_new();
2320
2321 assert_non_null(state);
2322 test_state = *((struct bind_st **)state);
2323 assert_non_null(test_state);
2324 assert_non_null(test_state->bind);
2325 bind = test_state->bind;
2326
2327 /* set null */
2328 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, NULL);
2329 assert_int_equal(rc, -1);
2330 /* set invalid key */
2331 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
2332 assert_int_equal(rc, -1);
2333 SSH_KEY_FREE(key);
2334
2335 /* set ed25519 key */
2336 base64_key = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0);
2337 rc = ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
2338 if (ssh_fips_mode()) {
2339 assert_int_equal(rc, SSH_ERROR);
2340 assert_null(key);
2341 } else {
2342 assert_int_equal(rc, SSH_OK);
2343 assert_non_null(key);
2344 }
2345
2346 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
2347 if (ssh_fips_mode()) {
2348 assert_int_equal(rc, SSH_ERROR);
2349 } else {
2350 assert_int_equal(rc, 0);
2351 }
2352
2353 /* set rsa key */
2354 base64_key = torture_get_testkey(SSH_KEYTYPE_RSA, 0);
2355 rc = ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
2356 assert_int_equal(rc, SSH_OK);
2357 assert_non_null(key);
2358
2359 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
2360 assert_int_equal(rc, 0);
2361 #ifdef HAVE_ECC
2362 /* set ecdsa key */
2363 base64_key = torture_get_testkey(SSH_KEYTYPE_ECDSA_P521, 0);
2364 rc = ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
2365 assert_int_equal(rc, SSH_OK);
2366 assert_non_null(key);
2367
2368 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
2369 assert_int_equal(rc, 0);
2370 #endif
2371 }
2372
2373 static void
2374 torture_bind_options_import_key_str(void **state)
2375 {
2376 struct bind_st *test_state = NULL;
2377 ssh_bind bind = NULL;
2378 int rc;
2379 const char *base64_key = "";
2380
2381 assert_non_null(state);
2382 test_state = *((struct bind_st **)state);
2383 assert_non_null(test_state);
2384 assert_non_null(test_state->bind);
2385 bind = test_state->bind;
2386
2387 /* set null */
2388 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY_STR, NULL);
2389 assert_int_equal(rc, -1);
2390 /* set invalid key */
2391 rc =
2392 ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY_STR, base64_key);
2393 assert_int_equal(rc, -1);
2394
2395 /* set ed25519 key */
2396 base64_key = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0);
2397
2398 rc =
2399 ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY_STR, base64_key);
2400 if (ssh_fips_mode()) {
2401 assert_int_equal(rc, SSH_ERROR);
2402 } else {
2403 assert_int_equal(rc, 0);
2404 }
2405
2406 /* set rsa key */
2407 base64_key = torture_get_testkey(SSH_KEYTYPE_RSA, 0);
2408
2409 rc =
2410 ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY_STR, base64_key);
2411 assert_int_equal(rc, 0);
2412 #ifdef HAVE_ECC
2413 /* set ecdsa key */
2414 base64_key = torture_get_testkey(SSH_KEYTYPE_ECDSA_P521, 0);
2415
2416 rc =
2417 ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY_STR, base64_key);
2418 assert_int_equal(rc, 0);
2419 #endif
2420 }
2421
2422 static void torture_bind_options_hostkey(void **state)
2423 {
2424 struct bind_st *test_state;
2425 ssh_bind bind;
2426 int rc;
2427
2428 assert_non_null(state);
2429 test_state = *((struct bind_st **)state);
2430 assert_non_null(test_state);
2431 assert_non_null(test_state->bind);
2432 bind = test_state->bind;
2433
2434 /* Test RSA key */
2435 rc = ssh_bind_options_set(bind,
2436 SSH_BIND_OPTIONS_HOSTKEY,
2437 LIBSSH_RSA_TESTKEY);
2438 assert_int_equal(rc, 0);
2439 assert_non_null(bind->rsakey);
2440 assert_string_equal(bind->rsakey, LIBSSH_RSA_TESTKEY);
2441
2442 /* Test ED25519 key */
2443 rc = ssh_bind_options_set(bind,
2444 SSH_BIND_OPTIONS_HOSTKEY,
2445 LIBSSH_ED25519_TESTKEY);
2446 if (ssh_fips_mode()) {
2447 assert_int_equal(rc, SSH_ERROR);
2448 assert_null(bind->ed25519key);
2449 } else {
2450 assert_int_equal(rc, 0);
2451 assert_non_null(bind->ed25519key);
2452 assert_string_equal(bind->ed25519key, LIBSSH_ED25519_TESTKEY);
2453 }
2454
2455 #ifdef HAVE_ECC
2456 /* Test ECDSA key */
2457 rc = ssh_bind_options_set(bind,
2458 SSH_BIND_OPTIONS_HOSTKEY,
2459 LIBSSH_ECDSA_521_TESTKEY);
2460 assert_int_equal(rc, 0);
2461 assert_non_null(bind->ecdsakey);
2462 assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
2463 #endif
2464 }
2465
2466 static void torture_bind_options_bindaddr(void **state)
2467 {
2468 struct bind_st *test_state;
2469 ssh_bind bind;
2470 int rc;
2471
2472 const char *address = "127.0.0.1";
2473
2474 assert_non_null(state);
2475 test_state = *((struct bind_st **)state);
2476 assert_non_null(test_state);
2477 assert_non_null(test_state->bind);
2478 bind = test_state->bind;
2479
2480 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDADDR, address);
2481 assert_int_equal(rc, 0);
2482 assert_non_null(bind->bindaddr);
2483 assert_string_equal(bind->bindaddr, address);
2484 }
2485
2486 static void torture_bind_options_bindport(void **state)
2487 {
2488 struct bind_st *test_state;
2489 ssh_bind bind;
2490 unsigned int given_port = 1234;
2491 int rc;
2492
2493 assert_non_null(state);
2494 test_state = *((struct bind_st **)state);
2495 assert_non_null(test_state);
2496 assert_non_null(test_state->bind);
2497 bind = test_state->bind;
2498
2499 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDPORT, &given_port);
2500 assert_int_equal(rc, 0);
2501 assert_int_equal(bind->bindport, 1234);
2502 }
2503
2504 static void torture_bind_options_bindport_str(void **state)
2505 {
2506 struct bind_st *test_state;
2507 ssh_bind bind;
2508 int rc;
2509
2510 assert_non_null(state);
2511 test_state = *((struct bind_st **)state);
2512 assert_non_null(test_state);
2513 assert_non_null(test_state->bind);
2514 bind = test_state->bind;
2515
2516 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDPORT_STR, "23");
2517 assert_int_equal(rc, 0);
2518 assert_int_equal(bind->bindport, 23);
2519
2520 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDPORT_STR, "twentythree");
2521 assert_int_equal(rc, -1);
2522 assert_int_not_equal(bind->bindport, 0);
2523 }
2524
2525 static void torture_bind_options_log_verbosity(void **state)
2526 {
2527 struct bind_st *test_state;
2528 ssh_bind bind;
2529 int verbosity = SSH_LOG_PACKET;
2530 int previous_level, new_level;
2531 int rc;
2532
2533 assert_non_null(state);
2534 test_state = *((struct bind_st **)state);
2535 assert_non_null(test_state);
2536 assert_non_null(test_state->bind);
2537 bind = test_state->bind;
2538
2539 previous_level = ssh_get_log_level();
2540
2541 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &verbosity);
2542 assert_int_equal(rc, 0);
2543
2544 new_level = ssh_get_log_level();
2545 assert_int_equal(new_level, verbosity);
2546
2547 rc = ssh_set_log_level(previous_level);
2548 assert_int_equal(rc, SSH_OK);
2549 }
2550
2551 static void torture_bind_options_log_verbosity_str(void **state)
2552 {
2553 struct bind_st *test_state;
2554 ssh_bind bind;
2555 int rc;
2556 int previous_level, new_level;
2557
2558 assert_non_null(state);
2559 test_state = *((struct bind_st **)state);
2560 assert_non_null(test_state);
2561 assert_non_null(test_state->bind);
2562 bind = test_state->bind;
2563
2564 previous_level = ssh_get_log_level();
2565
2566 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR, "3");
2567 assert_int_equal(rc, 0);
2568
2569 new_level = ssh_get_log_level();
2570 assert_int_equal(new_level, SSH_LOG_PACKET);
2571
2572 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR, "verbosity");
2573 assert_int_equal(rc, -1);
2574 new_level = ssh_get_log_level();
2575 assert_int_not_equal(new_level, 0);
2576
2577 rc = ssh_set_log_level(previous_level);
2578 assert_int_equal(rc, SSH_OK);
2579 }
2580
2581 static void torture_bind_options_rsakey(void **state)
2582 {
2583 struct bind_st *test_state;
2584 ssh_bind bind;
2585 int rc;
2586
2587 assert_non_null(state);
2588 test_state = *((struct bind_st **)state);
2589 assert_non_null(test_state);
2590 assert_non_null(test_state->bind);
2591 bind = test_state->bind;
2592
2593 rc = ssh_bind_options_set(bind,
2594 SSH_BIND_OPTIONS_HOSTKEY,
2595 LIBSSH_RSA_TESTKEY);
2596 assert_int_equal(rc, 0);
2597 assert_non_null(bind->rsakey);
2598 assert_string_equal(bind->rsakey, LIBSSH_RSA_TESTKEY);
2599 }
2600
2601 static void torture_bind_options_set_rsa_min_size(void **state)
2602 {
2603 struct bind_st *test_state = NULL;
2604 ssh_bind bind = NULL;
2605 int rc, min_allowed = RSA_MIN_KEY_SIZE, key_size;
2606
2607 assert_non_null(state);
2608 test_state = *((struct bind_st **)state);
2609 assert_non_null(test_state);
2610 assert_non_null(test_state->bind);
2611 bind = test_state->bind;
2612
2613 /* Check that passing NULL leads to failure */
2614 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_RSA_MIN_SIZE, NULL);
2615 assert_int_equal(rc, -1);
2616
2617 /*
2618 * Check that supplying a value less than the allowed minimum leads
2619 * to failure
2620 */
2621 key_size = min_allowed - 2;
2622 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_RSA_MIN_SIZE, &key_size);
2623 assert_int_equal(rc, -1);
2624
2625 /* Check that supplying a negative value leads to failure */
2626 key_size = -10;
2627 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_RSA_MIN_SIZE, &key_size);
2628 assert_int_equal(rc, -1);
2629
2630 /* Check that supplying 0 succeeds (used to revert to default) */
2631 key_size = 0;
2632 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_RSA_MIN_SIZE, &key_size);
2633 assert_int_equal(rc, 0);
2634
2635 /* Check that supplying allowed minimum succeeds */
2636 key_size = min_allowed;
2637 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_RSA_MIN_SIZE, &key_size);
2638 assert_int_equal(rc, 0);
2639
2640 /* Check that supplying a value greater than allowed minimum succeeds */
2641 key_size = min_allowed + 10;
2642 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_RSA_MIN_SIZE, &key_size);
2643 assert_int_equal(rc, 0);
2644 }
2645
2646 #ifdef HAVE_ECC
2647 static void torture_bind_options_ecdsakey(void **state)
2648 {
2649 struct bind_st *test_state;
2650 ssh_bind bind;
2651 int rc;
2652
2653 assert_non_null(state);
2654 test_state = *((struct bind_st **)state);
2655 assert_non_null(test_state);
2656 assert_non_null(test_state->bind);
2657 bind = test_state->bind;
2658
2659 rc = ssh_bind_options_set(bind,
2660 SSH_BIND_OPTIONS_HOSTKEY,
2661 LIBSSH_ECDSA_521_TESTKEY);
2662 assert_int_equal(rc, 0);
2663 assert_non_null(bind->ecdsakey);
2664 assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
2665 }
2666 #endif
2667
2668 static void torture_bind_options_banner(void **state)
2669 {
2670 struct bind_st *test_state;
2671 ssh_bind bind;
2672 const char *banner = "This is the new banner";
2673 int rc;
2674
2675 assert_non_null(state);
2676 test_state = *((struct bind_st **)state);
2677 assert_non_null(test_state);
2678 assert_non_null(test_state->bind);
2679 bind = test_state->bind;
2680
2681 rc = ssh_bind_options_set(bind,
2682 SSH_BIND_OPTIONS_BANNER,
2683 banner);
2684 assert_int_equal(rc, 0);
2685 assert_non_null(bind->banner);
2686 assert_string_equal(bind->banner, banner);
2687 }
2688
2689 static void torture_bind_options_set_ciphers(void **state)
2690 {
2691 struct bind_st *test_state;
2692 ssh_bind bind;
2693 int rc;
2694
2695 assert_non_null(state);
2696 test_state = *((struct bind_st **)state);
2697 assert_non_null(test_state);
2698 assert_non_null(test_state->bind);
2699 bind = test_state->bind;
2700 assert_non_null(bind->wanted_methods);
2701
2702 /* Test known ciphers */
2703 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_C_S,
2704 "aes128-ctr,aes192-ctr,aes256-ctr");
2705 assert_int_equal(rc, 0);
2706 assert_non_null(bind->wanted_methods[SSH_CRYPT_C_S]);
2707 if (ssh_fips_mode()) {
2708 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S],
2709 "aes128-ctr,aes256-ctr");
2710 } else {
2711 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S],
2712 "aes128-ctr,aes192-ctr,aes256-ctr");
2713 }
2714
2715 /* Test one unknown cipher */
2716 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_C_S,
2717 "aes128-ctr,[email protected],aes256-ctr");
2718 assert_int_equal(rc, 0);
2719 assert_non_null(bind->wanted_methods[SSH_CRYPT_C_S]);
2720 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S],
2721 "aes128-ctr,aes256-ctr");
2722
2723 /* Test all unknown ciphers */
2724 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_C_S,
2725 "[email protected],[email protected]");
2726 assert_int_not_equal(rc, 0);
2727
2728 /* Test known ciphers */
2729 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_S_C,
2730 "aes128-ctr,aes192-ctr,aes256-ctr");
2731 assert_int_equal(rc, 0);
2732 assert_non_null(bind->wanted_methods[SSH_CRYPT_S_C]);
2733 if (ssh_fips_mode()) {
2734 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C],
2735 "aes128-ctr,aes256-ctr");
2736 } else {
2737 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C],
2738 "aes128-ctr,aes192-ctr,aes256-ctr");
2739 }
2740
2741 /* Test one unknown cipher */
2742 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_S_C,
2743 "aes128-ctr,[email protected],aes256-ctr");
2744 assert_int_equal(rc, 0);
2745 assert_non_null(bind->wanted_methods[SSH_CRYPT_S_C]);
2746 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C],
2747 "aes128-ctr,aes256-ctr");
2748
2749 /* Test all unknown ciphers */
2750 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_S_C,
2751 "[email protected],[email protected]");
2752 assert_int_not_equal(rc, 0);
2753 }
2754
2755 static void torture_bind_options_set_key_exchange(void **state)
2756 {
2757 struct bind_st *test_state;
2758 ssh_bind bind;
2759 int rc;
2760
2761 assert_non_null(state);
2762 test_state = *((struct bind_st **)state);
2763 assert_non_null(test_state);
2764 assert_non_null(test_state->bind);
2765 bind = test_state->bind;
2766 assert_non_null(bind->wanted_methods);
2767
2768 /* Test known kexes */
2769 rc = ssh_bind_options_set(bind,
2770 SSH_BIND_OPTIONS_KEY_EXCHANGE,
2771 "curve25519-sha256,[email protected],"
2772 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
2773 "diffie-hellman-group18-sha512,"
2774 "diffie-hellman-group14-sha256,"
2775 "diffie-hellman-group14-sha1");
2776 assert_int_equal(rc, 0);
2777 assert_non_null(bind->wanted_methods[SSH_KEX]);
2778 if (ssh_fips_mode()) {
2779 assert_string_equal(bind->wanted_methods[SSH_KEX],
2780 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
2781 "diffie-hellman-group18-sha512,"
2782 "diffie-hellman-group14-sha256");
2783 } else {
2784 assert_string_equal(bind->wanted_methods[SSH_KEX],
2785 "curve25519-sha256,[email protected],"
2786 "ecdh-sha2-nistp256,diffie-hellman-group16-sha512,"
2787 "diffie-hellman-group18-sha512,"
2788 "diffie-hellman-group14-sha256,"
2789 "diffie-hellman-group14-sha1");
2790 }
2791
2792 /* Test one unknown kex */
2793 rc = ssh_bind_options_set(bind,
2794 SSH_BIND_OPTIONS_KEY_EXCHANGE,
2795 "diffie-hellman-group16-sha512,"
2796 "[email protected],"
2797 "diffie-hellman-group18-sha512");
2798 assert_int_equal(rc, 0);
2799 assert_non_null(bind->wanted_methods[SSH_KEX]);
2800 assert_string_equal(bind->wanted_methods[SSH_KEX],
2801 "diffie-hellman-group16-sha512,"
2802 "diffie-hellman-group18-sha512");
2803
2804 /* Test all unknown kexes */
2805 rc = ssh_bind_options_set(bind,
2806 SSH_BIND_OPTIONS_KEY_EXCHANGE,
2807 "[email protected],[email protected]");
2808 assert_int_not_equal(rc, 0);
2809 }
2810
2811 static void torture_bind_options_set_macs(void **state)
2812 {
2813 struct bind_st *test_state;
2814 ssh_bind bind;
2815 int rc;
2816
2817 assert_non_null(state);
2818 test_state = *((struct bind_st **)state);
2819 assert_non_null(test_state);
2820 assert_non_null(test_state->bind);
2821 bind = test_state->bind;
2822 assert_non_null(bind->wanted_methods);
2823
2824 /* Test known MACs */
2825 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_HMAC_S_C, "hmac-sha1");
2826 assert_int_equal(rc, 0);
2827 assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
2828 assert_string_equal(bind->wanted_methods[SSH_MAC_S_C], "hmac-sha1");
2829
2830 /* Test multiple known MACs */
2831 rc = ssh_bind_options_set(bind,
2832 SSH_BIND_OPTIONS_HMAC_S_C,
2833 "hmac-sha1,hmac-sha2-256");
2834 assert_int_equal(rc, 0);
2835 assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
2836 assert_string_equal(bind->wanted_methods[SSH_MAC_S_C],
2837 "hmac-sha1,hmac-sha2-256");
2838
2839 /* Test unknown MACs */
2840 rc = ssh_bind_options_set(bind,
2841 SSH_BIND_OPTIONS_HMAC_S_C,
2842 "[email protected],"
2843 "hmac-sha1,[email protected]");
2844 assert_int_equal(rc, 0);
2845 assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
2846 assert_string_equal(bind->wanted_methods[SSH_MAC_S_C], "hmac-sha1");
2847
2848 /* Test all unknown MACs */
2849 rc = ssh_bind_options_set(bind,
2850 SSH_BIND_OPTIONS_HMAC_S_C,
2851 "[email protected]");
2852 assert_int_not_equal(rc, 0);
2853
2854 /* Test known MACs */
2855 rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_HMAC_C_S, "hmac-sha1");
2856 assert_int_equal(rc, 0);
2857 assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
2858 assert_string_equal(bind->wanted_methods[SSH_MAC_C_S], "hmac-sha1");
2859
2860 /* Test multiple known MACs */
2861 rc = ssh_bind_options_set(bind,
2862 SSH_BIND_OPTIONS_HMAC_C_S,
2863 "hmac-sha1,hmac-sha2-256");
2864 assert_int_equal(rc, 0);
2865 assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
2866 assert_string_equal(bind->wanted_methods[SSH_MAC_C_S],
2867 "hmac-sha1,hmac-sha2-256");
2868
2869 /* Test unknown MACs */
2870 rc = ssh_bind_options_set(bind,
2871 SSH_BIND_OPTIONS_HMAC_C_S,
2872 "[email protected],"
2873 "hmac-sha1,[email protected]");
2874 assert_int_equal(rc, 0);
2875 assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
2876 assert_string_equal(bind->wanted_methods[SSH_MAC_C_S], "hmac-sha1");
2877
2878 /* Test all unknown MACs */
2879 rc = ssh_bind_options_set(bind,
2880 SSH_BIND_OPTIONS_HMAC_C_S,
2881 "[email protected]");
2882 assert_int_not_equal(rc, 0);
2883 }
2884
2885 static void torture_bind_options_parse_config(void **state)
2886 {
2887 struct bind_st *test_state;
2888 ssh_bind bind;
2889 char *cwd = NULL;
2890 int rc;
2891
2892 assert_non_null(state);
2893 test_state = *((struct bind_st **)state);
2894 assert_non_null(test_state);
2895 assert_non_null(test_state->bind);
2896 bind = test_state->bind;
2897
2898 cwd = torture_get_current_working_dir();
2899 assert_non_null(cwd);
2900
2901 rc = ssh_bind_options_set(bind,
2902 SSH_BIND_OPTIONS_CONFIG_DIR,
2903 (const char *)cwd);
2904 assert_int_equal(rc, 0);
2905 assert_non_null(bind->config_dir);
2906 assert_string_equal(bind->config_dir, cwd);
2907
2908 rc = ssh_bind_options_parse_config(bind,
2909 "%d/" LIBSSH_CUSTOM_BIND_CONFIG_FILE);
2910 assert_int_equal(rc, 0);
2911 assert_int_equal(bind->bindport, 42);
2912
2913 SAFE_FREE(cwd);
2914 }
2915
2916 static void torture_bind_options_config_dir(void **state)
2917 {
2918 struct bind_st *test_state;
2919 ssh_bind bind;
2920 const char *new_dir = "/new/dir/";
2921 const char *replacement_dir = "/replacement/dir/";
2922 int rc;
2923
2924 assert_non_null(state);
2925 test_state = *((struct bind_st **)state);
2926 assert_non_null(test_state);
2927 assert_non_null(test_state->bind);
2928 bind = test_state->bind;
2929
2930 rc = ssh_bind_options_set(bind,
2931 SSH_BIND_OPTIONS_CONFIG_DIR,
2932 new_dir);
2933 assert_int_equal(rc, 0);
2934 assert_non_null(bind->config_dir);
2935 assert_string_equal(bind->config_dir, new_dir);
2936
2937 rc = ssh_bind_options_set(bind,
2938 SSH_BIND_OPTIONS_CONFIG_DIR,
2939 replacement_dir);
2940 assert_int_equal(rc, 0);
2941 assert_non_null(bind->config_dir);
2942 assert_string_equal(bind->config_dir, replacement_dir);
2943 }
2944
2945 static void torture_bind_options_set_pubkey_accepted_key_types(void **state)
2946 {
2947 struct bind_st *test_state;
2948 ssh_bind bind;
2949 int rc;
2950
2951 assert_non_null(state);
2952 test_state = *((struct bind_st **)state);
2953 assert_non_null(test_state);
2954 assert_non_null(test_state->bind);
2955 bind = test_state->bind;
2956
2957 /* Test known Pubkey Types */
2958 rc = ssh_bind_options_set(bind,
2959 SSH_BIND_OPTIONS_PUBKEY_ACCEPTED_KEY_TYPES,
2960 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
2961 assert_int_equal(rc, 0);
2962 assert_non_null(bind->pubkey_accepted_key_types);
2963 if (ssh_fips_mode()) {
2964 assert_string_equal(bind->pubkey_accepted_key_types,
2965 "ecdsa-sha2-nistp384");
2966 } else {
2967 assert_string_equal(bind->pubkey_accepted_key_types,
2968 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
2969 }
2970
2971 SAFE_FREE(bind->pubkey_accepted_key_types);
2972
2973 /* Test with some unknown type */
2974 rc = ssh_bind_options_set(bind,
2975 SSH_BIND_OPTIONS_PUBKEY_ACCEPTED_KEY_TYPES,
2976 "ecdsa-sha2-nistp384,unknown-type,rsa-sha2-256");
2977 assert_int_equal(rc, 0);
2978 assert_non_null(bind->pubkey_accepted_key_types);
2979 assert_string_equal(bind->pubkey_accepted_key_types,
2980 "ecdsa-sha2-nistp384,rsa-sha2-256");
2981
2982 SAFE_FREE(bind->pubkey_accepted_key_types);
2983
2984 /* Test with only unknown type */
2985 rc = ssh_bind_options_set(bind,
2986 SSH_BIND_OPTIONS_PUBKEY_ACCEPTED_KEY_TYPES,
2987 "unknown-type");
2988 assert_int_equal(rc, -1);
2989 assert_null(bind->pubkey_accepted_key_types);
2990
2991 /* Test with something set and then try unknown type */
2992 rc = ssh_bind_options_set(bind,
2993 SSH_BIND_OPTIONS_PUBKEY_ACCEPTED_KEY_TYPES,
2994 "ecdsa-sha2-nistp384");
2995 assert_int_equal(rc, 0);
2996 assert_non_null(bind->pubkey_accepted_key_types);
2997 assert_string_equal(bind->pubkey_accepted_key_types, "ecdsa-sha2-nistp384");
2998 rc = ssh_bind_options_set(bind,
2999 SSH_BIND_OPTIONS_PUBKEY_ACCEPTED_KEY_TYPES,
3000 "unknown-type");
3001 assert_int_equal(rc, -1);
3002
3003 /* Check that nothing changed */
3004 assert_non_null(bind->pubkey_accepted_key_types);
3005 assert_string_equal(bind->pubkey_accepted_key_types, "ecdsa-sha2-nistp384");
3006 }
3007
3008 static void torture_bind_options_set_hostkey_algorithms(void **state)
3009 {
3010 struct bind_st *test_state;
3011 ssh_bind bind;
3012 int rc;
3013
3014 assert_non_null(state);
3015 test_state = *((struct bind_st **)state);
3016 assert_non_null(test_state);
3017 assert_non_null(test_state->bind);
3018 bind = test_state->bind;
3019
3020 /* Test known Pubkey Types */
3021 rc = ssh_bind_options_set(bind,
3022 SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS,
3023 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
3024 assert_int_equal(rc, 0);
3025 assert_non_null(bind->wanted_methods[SSH_HOSTKEYS]);
3026 if (ssh_fips_mode()) {
3027 assert_string_equal(bind->wanted_methods[SSH_HOSTKEYS],
3028 "ecdsa-sha2-nistp384");
3029 } else {
3030 assert_string_equal(bind->wanted_methods[SSH_HOSTKEYS],
3031 "ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
3032 }
3033
3034 SAFE_FREE(bind->wanted_methods[SSH_HOSTKEYS]);
3035
3036 /* Test with some unknown type */
3037 rc = ssh_bind_options_set(bind,
3038 SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS,
3039 "ecdsa-sha2-nistp384,unknown-type");
3040 assert_int_equal(rc, 0);
3041 assert_non_null(bind->wanted_methods[SSH_HOSTKEYS]);
3042 assert_string_equal(bind->wanted_methods[SSH_HOSTKEYS],
3043 "ecdsa-sha2-nistp384");
3044
3045 SAFE_FREE(bind->wanted_methods[SSH_HOSTKEYS]);
3046
3047 /* Test with only unknown type */
3048 rc = ssh_bind_options_set(bind,
3049 SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS,
3050 "unknown-type");
3051 assert_int_equal(rc, -1);
3052 assert_null(bind->wanted_methods[SSH_HOSTKEYS]);
3053
3054 /* Test with something set and then try unknown type */
3055 rc = ssh_bind_options_set(bind,
3056 SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS,
3057 "ecdsa-sha2-nistp384");
3058 assert_int_equal(rc, 0);
3059 assert_non_null(bind->wanted_methods[SSH_HOSTKEYS]);
3060 assert_string_equal(bind->wanted_methods[SSH_HOSTKEYS],
3061 "ecdsa-sha2-nistp384");
3062 rc = ssh_bind_options_set(bind,
3063 SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS,
3064 "unknown-type");
3065 assert_int_equal(rc, -1);
3066
3067 /* Check that nothing changed */
3068 assert_non_null(bind->wanted_methods[SSH_HOSTKEYS]);
3069 assert_string_equal(bind->wanted_methods[SSH_HOSTKEYS],
3070 "ecdsa-sha2-nistp384");
3071 }
3072
3073 #endif /* WITH_SERVER */
3074
3075 int
3076 torture_run_tests(void)
3077 {
3078 int rc;
3079 struct CMUnitTest tests[] = {
3080 cmocka_unit_test_setup_teardown(torture_options_set_host,
3081 setup,
3082 teardown),
3083 cmocka_unit_test_setup_teardown(torture_options_get_host,
3084 setup,
3085 teardown),
3086 cmocka_unit_test_setup_teardown(torture_options_set_port,
3087 setup,
3088 teardown),
3089 cmocka_unit_test_setup_teardown(torture_options_get_port,
3090 setup,
3091 teardown),
3092 cmocka_unit_test_setup_teardown(torture_options_set_fd,
3093 setup,
3094 teardown),
3095 cmocka_unit_test_setup_teardown(torture_options_set_user,
3096 setup,
3097 teardown),
3098 cmocka_unit_test_setup_teardown(torture_options_get_user,
3099 setup,
3100 teardown),
3101 cmocka_unit_test_setup_teardown(torture_options_set_identity,
3102 setup,
3103 teardown),
3104 cmocka_unit_test_setup_teardown(torture_options_get_identity,
3105 setup,
3106 teardown),
3107 cmocka_unit_test_setup_teardown(torture_options_set_global_knownhosts,
3108 setup,
3109 teardown),
3110 cmocka_unit_test_setup_teardown(torture_options_get_global_knownhosts,
3111 setup,
3112 teardown),
3113 cmocka_unit_test_setup_teardown(torture_options_set_knownhosts,
3114 setup,
3115 teardown),
3116 cmocka_unit_test_setup_teardown(torture_options_get_knownhosts,
3117 setup,
3118 teardown),
3119 cmocka_unit_test_setup_teardown(torture_options_proxycommand,
3120 setup,
3121 teardown),
3122 cmocka_unit_test_setup_teardown(torture_options_control_master,
3123 setup,
3124 teardown),
3125 cmocka_unit_test_setup_teardown(torture_options_control_path,
3126 setup,
3127 teardown),
3128 cmocka_unit_test_setup_teardown(torture_options_set_ciphers,
3129 setup,
3130 teardown),
3131 cmocka_unit_test_setup_teardown(torture_options_get_ciphers,
3132 setup,
3133 teardown),
3134 cmocka_unit_test_setup_teardown(torture_options_set_key_exchange,
3135 setup,
3136 teardown),
3137 cmocka_unit_test_setup_teardown(torture_options_get_key_exchange,
3138 setup,
3139 teardown),
3140 cmocka_unit_test_setup_teardown(torture_options_set_hostkey,
3141 setup,
3142 teardown),
3143 cmocka_unit_test_setup_teardown(torture_options_get_hostkey,
3144 setup,
3145 teardown),
3146 cmocka_unit_test_setup_teardown(
3147 torture_options_set_pubkey_accepted_types,
3148 setup,
3149 teardown),
3150 cmocka_unit_test_setup_teardown(
3151 torture_options_get_pubkey_accepted_types,
3152 setup,
3153 teardown),
3154 cmocka_unit_test_setup_teardown(torture_options_set_macs,
3155 setup,
3156 teardown),
3157 cmocka_unit_test_setup_teardown(torture_options_get_macs,
3158 setup,
3159 teardown),
3160 cmocka_unit_test_setup_teardown(torture_options_set_compression,
3161 setup,
3162 teardown),
3163 cmocka_unit_test_setup_teardown(torture_options_get_compression,
3164 setup,
3165 teardown),
3166 cmocka_unit_test_setup_teardown(torture_options_copy, setup, teardown),
3167 cmocka_unit_test_setup_teardown(torture_options_config_host,
3168 setup,
3169 teardown),
3170 cmocka_unit_test_setup_teardown(torture_options_config_match,
3171 setup,
3172 teardown),
3173 cmocka_unit_test_setup_teardown(torture_options_config_match_multi,
3174 setup,
3175 teardown),
3176 cmocka_unit_test_setup_teardown(torture_options_getopt,
3177 setup,
3178 teardown),
3179 cmocka_unit_test_setup_teardown(torture_options_getopt_o_option,
3180 setup,
3181 teardown),
3182 cmocka_unit_test_setup_teardown(torture_options_plus_sign,
3183 setup,
3184 teardown),
3185 cmocka_unit_test_setup_teardown(torture_options_minus_sign,
3186 setup,
3187 teardown),
3188 cmocka_unit_test_setup_teardown(torture_options_caret_sign,
3189 setup,
3190 teardown),
3191 cmocka_unit_test_setup_teardown(torture_options_apply, setup, teardown),
3192 cmocka_unit_test_setup_teardown(torture_options_set_verbosity,
3193 setup,
3194 teardown),
3195 cmocka_unit_test_setup_teardown(torture_options_set_rsa_min_size,
3196 setup,
3197 teardown),
3198 };
3199
3200 #ifdef WITH_SERVER
3201 struct CMUnitTest sshbind_tests[] = {
3202 cmocka_unit_test_setup_teardown(torture_bind_options_import_key,
3203 sshbind_setup,
3204 sshbind_teardown),
3205 cmocka_unit_test_setup_teardown(torture_bind_options_import_key_str,
3206 sshbind_setup,
3207 sshbind_teardown),
3208 cmocka_unit_test_setup_teardown(torture_bind_options_hostkey,
3209 sshbind_setup,
3210 sshbind_teardown),
3211 cmocka_unit_test_setup_teardown(torture_bind_options_bindaddr,
3212 sshbind_setup,
3213 sshbind_teardown),
3214 cmocka_unit_test_setup_teardown(torture_bind_options_bindport,
3215 sshbind_setup,
3216 sshbind_teardown),
3217 cmocka_unit_test_setup_teardown(torture_bind_options_bindport_str,
3218 sshbind_setup,
3219 sshbind_teardown),
3220 cmocka_unit_test_setup_teardown(torture_bind_options_log_verbosity,
3221 sshbind_setup,
3222 sshbind_teardown),
3223 cmocka_unit_test_setup_teardown(torture_bind_options_log_verbosity_str,
3224 sshbind_setup,
3225 sshbind_teardown),
3226 cmocka_unit_test_setup_teardown(torture_bind_options_rsakey,
3227 sshbind_setup,
3228 sshbind_teardown),
3229 cmocka_unit_test_setup_teardown(torture_bind_options_set_rsa_min_size,
3230 sshbind_setup,
3231 sshbind_teardown),
3232 #ifdef HAVE_ECC
3233 cmocka_unit_test_setup_teardown(torture_bind_options_ecdsakey,
3234 sshbind_setup,
3235 sshbind_teardown),
3236 #endif
3237 cmocka_unit_test_setup_teardown(torture_bind_options_banner,
3238 sshbind_setup,
3239 sshbind_teardown),
3240 cmocka_unit_test_setup_teardown(torture_bind_options_set_ciphers,
3241 sshbind_setup,
3242 sshbind_teardown),
3243 cmocka_unit_test_setup_teardown(torture_bind_options_set_key_exchange,
3244 sshbind_setup,
3245 sshbind_teardown),
3246 cmocka_unit_test_setup_teardown(torture_bind_options_set_macs,
3247 sshbind_setup,
3248 sshbind_teardown),
3249 cmocka_unit_test_setup_teardown(torture_bind_options_parse_config,
3250 sshbind_setup,
3251 sshbind_teardown),
3252 cmocka_unit_test_setup_teardown(torture_bind_options_config_dir,
3253 sshbind_setup,
3254 sshbind_teardown),
3255 cmocka_unit_test_setup_teardown(
3256 torture_bind_options_set_pubkey_accepted_key_types,
3257 sshbind_setup,
3258 sshbind_teardown),
3259 cmocka_unit_test_setup_teardown(
3260 torture_bind_options_set_hostkey_algorithms,
3261 sshbind_setup,
3262 sshbind_teardown),
3263 };
3264 #endif /* WITH_SERVER */
3265
3266 ssh_init();
3267 torture_filter_tests(tests);
3268 rc = cmocka_run_group_tests(tests, NULL, NULL);
3269 #ifdef WITH_SERVER
3270 rc += cmocka_run_group_tests(sshbind_tests, NULL, NULL);
3271 #endif /* WITH_SERVER */
3272 ssh_finalize();
3273 return rc;
3274 }