| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#define LIBSSH_STATIC |
| 4 |
|
| 5 |
#include "torture.h" |
| 6 |
#include "libssh/libssh.h" |
| 7 |
#include "libssh/session.h" |
| 8 |
#include "libssh/crypto.h" |
| 9 |
#include "libssh/buffer.h" |
| 10 |
#include "libssh/socket.h" |
| 11 |
#include "libssh/callbacks.h" |
| 12 |
|
| 13 |
#include "socket.c" |
| 14 |
|
| 15 |
uint8_t test_data[]="\x02" |
| 16 |
"This is test data. Use it to check the validity of packet functions." |
| 17 |
"This is test data. Use it to check the validity of packet functions." |
| 18 |
"This is test data. Use it to check the validity of packet functions." |
| 19 |
"This is test data. Use it to check the validity of packet functions."; |
| 20 |
uint8_t key[]="iekaeshoa7ooCie2shai8shahngee3ONsee3xoishooj0ojei6aeChieth1iraPh"; |
| 21 |
uint8_t iv[]="eixaxughoomah4ui7Aew3ohxuolaifuu"; |
| 22 |
uint8_t mac[]="thook2Jai0ahmahyae7ChuuruoPhee8Y"; |
| 23 |
|
| 24 |
static uint8_t *copy_data(uint8_t *data, size_t len){ |
| 25 |
uint8_t *ret = malloc(len); |
| 26 |
assert_non_null(ret); |
| 27 |
memcpy(ret, data, len); |
| 28 |
return ret; |
| 29 |
} |
| 30 |
|
| 31 |
static SSH_PACKET_CALLBACK(copy_packet_data){ |
| 32 |
uint8_t *response = user; |
| 33 |
size_t len = ssh_buffer_get_len(packet); |
| 34 |
(void)type; |
| 35 |
(void)session; |
| 36 |
|
| 37 |
if(len > 1024){ |
| 38 |
len = 1024; |
| 39 |
} |
| 40 |
ssh_buffer_get_data(packet, response, len); |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
torture_packet(const char *cipher, const char *mac_type, |
| 47 |
const char *comp_type, size_t payload_len) |
| 48 |
{ |
| 49 |
ssh_session session = ssh_new(); |
| 50 |
int verbosity = torture_libssh_verbosity(); |
| 51 |
struct ssh_crypto_struct *crypto; |
| 52 |
struct ssh_cipher_struct *in_cipher; |
| 53 |
struct ssh_cipher_struct *out_cipher; |
| 54 |
int rc; |
| 55 |
int sockets[2]; |
| 56 |
uint8_t buffer[1024]; |
| 57 |
uint8_t response[1024]; |
| 58 |
size_t encrypted_packet_len; |
| 59 |
ssh_packet_callback callbacks[]={copy_packet_data}; |
| 60 |
struct ssh_packet_callbacks_struct cb = { |
| 61 |
.start=2, |
| 62 |
.n_callbacks=1, |
| 63 |
.callbacks=callbacks, |
| 64 |
.user=response |
| 65 |
}; |
| 66 |
int cmp; |
| 67 |
|
| 68 |
assert_non_null(session); |
| 69 |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 70 |
crypto = session->next_crypto; |
| 71 |
|
| 72 |
rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); |
| 73 |
assert_int_equal(rc, 0); |
| 74 |
|
| 75 |
crypto->kex_methods[SSH_KEX] = strdup("[email protected]"); |
| 76 |
crypto->kex_methods[SSH_HOSTKEYS] = strdup("ssh-rsa"); |
| 77 |
crypto->kex_methods[SSH_CRYPT_C_S] = strdup(cipher); |
| 78 |
crypto->kex_methods[SSH_CRYPT_S_C] = strdup(cipher); |
| 79 |
crypto->kex_methods[SSH_MAC_C_S] = strdup(mac_type); |
| 80 |
crypto->kex_methods[SSH_MAC_S_C] = strdup(mac_type); |
| 81 |
crypto->kex_methods[SSH_COMP_C_S] = strdup(comp_type); |
| 82 |
crypto->kex_methods[SSH_COMP_S_C] = strdup(comp_type); |
| 83 |
crypto->kex_methods[SSH_LANG_C_S] = strdup("none"); |
| 84 |
crypto->kex_methods[SSH_LANG_S_C] = strdup("none"); |
| 85 |
rc = crypt_set_algorithms_client(session); |
| 86 |
assert_int_equal(rc, SSH_OK); |
| 87 |
session->current_crypto = session->next_crypto; |
| 88 |
session->next_crypto = crypto_new(); |
| 89 |
crypto->encryptkey = copy_data(key, sizeof(key)); |
| 90 |
crypto->decryptkey = copy_data(key, sizeof(key)); |
| 91 |
crypto->encryptIV = copy_data(iv, sizeof(iv)); |
| 92 |
crypto->decryptIV = copy_data(iv, sizeof(iv)); |
| 93 |
crypto->encryptMAC = copy_data(mac, sizeof(mac)); |
| 94 |
crypto->decryptMAC = copy_data(mac, sizeof(mac)); |
| 95 |
|
| 96 |
in_cipher = session->current_crypto->in_cipher; |
| 97 |
if (in_cipher->set_decrypt_key != NULL) { |
| 98 |
rc = in_cipher->set_decrypt_key(in_cipher, |
| 99 |
session->current_crypto->decryptkey, |
| 100 |
session->current_crypto->decryptIV); |
| 101 |
assert_int_equal(rc, SSH_OK); |
| 102 |
} |
| 103 |
|
| 104 |
out_cipher = session->current_crypto->out_cipher; |
| 105 |
if (out_cipher->set_decrypt_key != NULL) { |
| 106 |
rc = out_cipher->set_encrypt_key(out_cipher, |
| 107 |
session->current_crypto->encryptkey, |
| 108 |
session->current_crypto->encryptIV); |
| 109 |
assert_int_equal(rc, SSH_OK); |
| 110 |
} |
| 111 |
session->current_crypto->used = SSH_DIRECTION_BOTH; |
| 112 |
|
| 113 |
assert_non_null(session->out_buffer); |
| 114 |
ssh_buffer_add_data(session->out_buffer, test_data, payload_len); |
| 115 |
session->socket->fd = sockets[0]; |
| 116 |
session->socket->write_wontblock = 1; |
| 117 |
rc = ssh_packet_send(session); |
| 118 |
assert_int_equal(rc, SSH_OK); |
| 119 |
|
| 120 |
rc = recv(sockets[1], buffer, sizeof(buffer), 0); |
| 121 |
assert_true(rc > 0); |
| 122 |
encrypted_packet_len = rc; |
| 123 |
cmp = strcmp(comp_type, "none"); |
| 124 |
if (cmp == 0) { |
| 125 |
assert_in_range(encrypted_packet_len, |
| 126 |
payload_len + 4, |
| 127 |
payload_len + (32 * 3)); |
| 128 |
} |
| 129 |
rc = send(sockets[0], buffer, encrypted_packet_len, 0); |
| 130 |
assert_int_equal(rc, encrypted_packet_len); |
| 131 |
|
| 132 |
ssh_packet_set_callbacks(session, &cb); |
| 133 |
ssh_burn(response, sizeof(response)); |
| 134 |
rc = ssh_packet_socket_callback(buffer, encrypted_packet_len, session); |
| 135 |
assert_int_not_equal(rc, SSH_ERROR); |
| 136 |
if(payload_len > 0){ |
| 137 |
assert_memory_equal(response, test_data+1, payload_len-1); |
| 138 |
} |
| 139 |
close(sockets[0]); |
| 140 |
close(sockets[1]); |
| 141 |
session->socket->fd = SSH_INVALID_SOCKET; |
| 142 |
ssh_free(session); |
| 143 |
} |
| 144 |
|
| 145 |
static void torture_packet_aes128_ctr_etm(UNUSED_PARAM(void **state)) |
| 146 |
{ |
| 147 |
int i; |
| 148 |
for (i = 1; i < 256; ++i) { |
| 149 |
torture_packet("aes128-ctr", "[email protected]", "none", i); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
static void torture_packet_aes192_ctr_etm(UNUSED_PARAM(void **state)) |
| 154 |
{ |
| 155 |
int i; |
| 156 |
for (i = 1; i < 256; ++i) { |
| 157 |
torture_packet("aes192-ctr", "[email protected]", "none", i); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static void torture_packet_aes256_ctr_etm(UNUSED_PARAM(void **state)) |
| 162 |
{ |
| 163 |
int i; |
| 164 |
for (i = 1; i < 256; ++i) { |
| 165 |
torture_packet("aes256-ctr", "[email protected]", "none", i); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
#ifdef WITH_INSECURE_NONE |
| 170 |
static void torture_packet_none_sha1(UNUSED_PARAM(void **state)) |
| 171 |
{ |
| 172 |
int i; |
| 173 |
|
| 174 |
for (i = 1; i < 256; ++i) { |
| 175 |
torture_packet("none", "hmac-sha1", "none", i); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
static void torture_packet_aes128_ctr_none(UNUSED_PARAM(void **state)) |
| 180 |
{ |
| 181 |
int i; |
| 182 |
|
| 183 |
for (i = 1; i < 256; ++i) { |
| 184 |
torture_packet("aes128-ctr", "none", "none", i); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static void torture_packet_none_none(UNUSED_PARAM(void **state)) |
| 189 |
{ |
| 190 |
int i; |
| 191 |
|
| 192 |
for (i = 1; i < 256; ++i) { |
| 193 |
torture_packet("none", "none", "none", i); |
| 194 |
} |
| 195 |
} |
| 196 |
#endif /* WITH_INSECURE_NONE */ |
| 197 |
|
| 198 |
static void torture_packet_aes128_ctr(void **state) |
| 199 |
{ |
| 200 |
int i; |
| 201 |
(void)state; /* unused */ |
| 202 |
for (i=1;i<256;++i){ |
| 203 |
torture_packet("aes128-ctr", "hmac-sha1", "none", i); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
static void torture_packet_aes192_ctr(void **state) |
| 208 |
{ |
| 209 |
int i; |
| 210 |
(void)state; /* unused */ |
| 211 |
for (i=1;i<256;++i){ |
| 212 |
torture_packet("aes192-ctr", "hmac-sha1", "none", i); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static void torture_packet_aes256_ctr(void **state) |
| 217 |
{ |
| 218 |
int i; |
| 219 |
(void)state; /* unused */ |
| 220 |
for (i=1;i<256;++i){ |
| 221 |
torture_packet("aes256-ctr", "hmac-sha1", "none", i); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
static void torture_packet_aes128_cbc(void **state) |
| 226 |
{ |
| 227 |
int i; |
| 228 |
(void)state; /* unused */ |
| 229 |
for (i=1;i<256;++i){ |
| 230 |
torture_packet("aes128-cbc", "hmac-sha1", "none", i); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static void torture_packet_aes192_cbc(void **state) |
| 235 |
{ |
| 236 |
int i; |
| 237 |
(void)state; /* unused */ |
| 238 |
for (i=1;i<256;++i){ |
| 239 |
torture_packet("aes192-cbc", "hmac-sha1", "none", i); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
static void torture_packet_aes256_cbc(void **state) |
| 244 |
{ |
| 245 |
int i; |
| 246 |
(void)state; /* unused */ |
| 247 |
for (i=1;i<256;++i){ |
| 248 |
torture_packet("aes256-cbc", "hmac-sha1", "none", i); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
static void torture_packet_aes128_cbc_etm(UNUSED_PARAM(void **state)) |
| 253 |
{ |
| 254 |
int i; |
| 255 |
for (i = 1; i < 256; ++i) { |
| 256 |
torture_packet("aes128-cbc", "[email protected]", "none", i); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
static void torture_packet_aes192_cbc_etm(UNUSED_PARAM(void **state)) |
| 261 |
{ |
| 262 |
int i; |
| 263 |
for (i = 1; i < 256; ++i) { |
| 264 |
torture_packet("aes192-cbc", "[email protected]", "none", i); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
static void torture_packet_aes256_cbc_etm(UNUSED_PARAM(void **state)) |
| 269 |
{ |
| 270 |
int i; |
| 271 |
for (i = 1; i < 256; ++i) { |
| 272 |
torture_packet("aes256-cbc", "[email protected]", "none", i); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static void torture_packet_3des_cbc(UNUSED_PARAM(void **state)) |
| 277 |
{ |
| 278 |
int i; |
| 279 |
|
| 280 |
/* 3des is not completely FIPS-allowed cipher since 140-3 */ |
| 281 |
if (ssh_fips_mode()) { |
| 282 |
skip(); |
| 283 |
} |
| 284 |
|
| 285 |
for (i=1;i<256;++i){ |
| 286 |
torture_packet("3des-cbc", "hmac-sha1", "none", i); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
static void torture_packet_3des_cbc_etm(UNUSED_PARAM(void **state)) |
| 291 |
{ |
| 292 |
int i; |
| 293 |
|
| 294 |
/* 3des is not completely FIPS-allowed cipher since 140-3 */ |
| 295 |
if (ssh_fips_mode()) { |
| 296 |
skip(); |
| 297 |
} |
| 298 |
|
| 299 |
for (i = 1; i < 256; ++i) { |
| 300 |
torture_packet("3des-cbc", "[email protected]", "none", i); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
static void torture_packet_chacha20(void **state) |
| 305 |
{ |
| 306 |
int i; |
| 307 |
(void)state; /* unused */ |
| 308 |
|
| 309 |
/* Chacha20-poly1305 is not FIPS-allowed cipher */ |
| 310 |
if (ssh_fips_mode()) { |
| 311 |
skip(); |
| 312 |
} |
| 313 |
|
| 314 |
for (i=1;i<256;++i){ |
| 315 |
torture_packet("[email protected]", "none", "none", i); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
static void torture_packet_aes128_gcm(void **state) |
| 320 |
{ |
| 321 |
int i; |
| 322 |
(void)state; /* unused */ |
| 323 |
for (i=1;i<256;++i){ |
| 324 |
torture_packet("[email protected]", "none", "none", i); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
static void torture_packet_aes256_gcm(void **state) |
| 329 |
{ |
| 330 |
int i; |
| 331 |
(void)state; /* unused */ |
| 332 |
for (i=1;i<256;++i){ |
| 333 |
torture_packet("[email protected]", "none", "none", i); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
#ifdef WITH_ZLIB |
| 338 |
static void torture_packet_compress_zlib(void **state) |
| 339 |
{ |
| 340 |
int i; |
| 341 |
(void)state; /* unused */ |
| 342 |
for (i=1;i<256;++i){ |
| 343 |
torture_packet("aes256-ctr", "hmac-sha1", "zlib", i); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
static void torture_packet_compress_zlib_openssh(void **state) |
| 348 |
{ |
| 349 |
int i; |
| 350 |
(void)state; /* unused */ |
| 351 |
for (i=1;i<256;++i){ |
| 352 |
torture_packet("aes256-ctr", "hmac-sha1", "[email protected]", i); |
| 353 |
} |
| 354 |
} |
| 355 |
#endif /* WITH_ZLIB */ |
| 356 |
|
| 357 |
int torture_run_tests(void) { |
| 358 |
int rc; |
| 359 |
struct CMUnitTest tests[] = { |
| 360 |
cmocka_unit_test(torture_packet_aes128_ctr), |
| 361 |
cmocka_unit_test(torture_packet_aes192_ctr), |
| 362 |
cmocka_unit_test(torture_packet_aes256_ctr), |
| 363 |
cmocka_unit_test(torture_packet_aes128_ctr_etm), |
| 364 |
cmocka_unit_test(torture_packet_aes192_ctr_etm), |
| 365 |
cmocka_unit_test(torture_packet_aes256_ctr_etm), |
| 366 |
cmocka_unit_test(torture_packet_aes128_cbc), |
| 367 |
cmocka_unit_test(torture_packet_aes192_cbc), |
| 368 |
cmocka_unit_test(torture_packet_aes256_cbc), |
| 369 |
cmocka_unit_test(torture_packet_aes128_cbc_etm), |
| 370 |
cmocka_unit_test(torture_packet_aes192_cbc_etm), |
| 371 |
cmocka_unit_test(torture_packet_aes256_cbc_etm), |
| 372 |
cmocka_unit_test(torture_packet_3des_cbc), |
| 373 |
cmocka_unit_test(torture_packet_3des_cbc_etm), |
| 374 |
cmocka_unit_test(torture_packet_chacha20), |
| 375 |
cmocka_unit_test(torture_packet_aes128_gcm), |
| 376 |
cmocka_unit_test(torture_packet_aes256_gcm), |
| 377 |
#ifdef WITH_ZLIB |
| 378 |
cmocka_unit_test(torture_packet_compress_zlib), |
| 379 |
cmocka_unit_test(torture_packet_compress_zlib_openssh), |
| 380 |
#endif /* WITH_ZLIB */ |
| 381 |
#ifdef WITH_INSECURE_NONE |
| 382 |
cmocka_unit_test(torture_packet_none_sha1), |
| 383 |
cmocka_unit_test(torture_packet_aes128_ctr_none), |
| 384 |
cmocka_unit_test(torture_packet_none_none), |
| 385 |
#endif |
| 386 |
}; |
| 387 |
|
| 388 |
ssh_init(); |
| 389 |
torture_filter_tests(tests); |
| 390 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 391 |
ssh_finalize(); |
| 392 |
return rc; |
| 393 |
} |