|
| 1 | +#include "config.h" |
| 2 | + |
| 3 | +#define LIBSSH_STATIC |
| 4 | + |
| 5 | +#include "torture.h" |
| 6 | +#include "torture_key.h" |
| 7 | +#include "legacy.c" |
| 8 | +#include "dh.c" |
| 9 | + |
| 10 | +static int setup_rsa_key(void **state) |
| 11 | +{ |
| 12 | + int rc=0; |
| 13 | + enum ssh_keytypes_e type; |
| 14 | + char *b64_key, *p; |
| 15 | + ssh_key key; |
| 16 | + |
| 17 | + const char *q; |
| 18 | + |
| 19 | + b64_key = strdup(torture_get_testkey_pub(SSH_KEYTYPE_RSA, 0)); |
| 20 | + assert_true(b64_key != NULL); |
| 21 | + |
| 22 | + q = p = b64_key; |
| 23 | + while (*p != ' ') p++; |
| 24 | + *p = '\0'; |
| 25 | + |
| 26 | + type = ssh_key_type_from_name(q); |
| 27 | + assert_true(type == SSH_KEYTYPE_RSA); |
| 28 | + |
| 29 | + q = ++p; |
| 30 | + while (*p != ' ') p++; |
| 31 | + *p = '\0'; |
| 32 | + |
| 33 | + rc = ssh_pki_import_pubkey_base64(q, type, &key); |
| 34 | + assert_true(rc == 0); |
| 35 | + |
| 36 | + free(b64_key); |
| 37 | + *state = key; |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +static int teardown(void **state) |
| 43 | +{ |
| 44 | + ssh_key_free(*state); |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +static void torture_md5_hash(void **state) |
| 49 | +{ |
| 50 | + ssh_key pubkey = *state; |
| 51 | + unsigned char *hash = NULL; |
| 52 | + char *hexa = NULL; |
| 53 | + size_t hlen; |
| 54 | + int rc = 0; |
| 55 | + |
| 56 | + rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5, &hash, &hlen); |
| 57 | + assert_true(rc == 0); |
| 58 | + |
| 59 | + hexa = ssh_get_hexa(hash, hlen); |
| 60 | + assert_string_equal(hexa, |
| 61 | + "50:15:a0:9b:92:bf:33:1c:01:c5:8c:fe:18:fa:ce:78"); |
| 62 | + |
| 63 | + ssh_string_free_char(hexa); |
| 64 | +} |
| 65 | + |
| 66 | +static void torture_sha1_hash(void **state) |
| 67 | +{ |
| 68 | + ssh_key pubkey = *state; |
| 69 | + unsigned char *hash = NULL; |
| 70 | + char *sha1 = NULL; |
| 71 | + int rc = 0; |
| 72 | + size_t hlen; |
| 73 | + |
| 74 | + rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen); |
| 75 | + assert_true(rc == 0); |
| 76 | + |
| 77 | + sha1 = ssh_get_b64_unpadded(hash, hlen); |
| 78 | + assert_string_equal(sha1, "6wP+houujQmxLBiFugTcoeoODCM"); |
| 79 | + |
| 80 | + ssh_string_free_char(sha1); |
| 81 | +} |
| 82 | + |
| 83 | +static void torture_sha256_hash(void **state) |
| 84 | +{ |
| 85 | + ssh_key pubkey = *state; |
| 86 | + unsigned char *hash = NULL; |
| 87 | + char *sha256 = NULL; |
| 88 | + int rc = 0; |
| 89 | + size_t hlen; |
| 90 | + |
| 91 | + rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256, &hash, &hlen); |
| 92 | + assert_true(rc == 0); |
| 93 | + |
| 94 | + sha256 = ssh_get_b64_unpadded(hash, hlen); |
| 95 | + assert_string_equal(sha256, "jXstVLLe84fSDo1kEYGn6iumnPCSorhaiWxnJz8VTII"); |
| 96 | + |
| 97 | + ssh_string_free_char(sha256); |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +int torture_run_tests(void) { |
| 102 | + int rc; |
| 103 | + struct CMUnitTest tests[] = { |
| 104 | + cmocka_unit_test_setup_teardown(torture_md5_hash, |
| 105 | + setup_rsa_key, |
| 106 | + teardown), |
| 107 | + cmocka_unit_test_setup_teardown(torture_sha1_hash, |
| 108 | + setup_rsa_key, |
| 109 | + teardown), |
| 110 | + cmocka_unit_test_setup_teardown(torture_sha256_hash, |
| 111 | + setup_rsa_key, |
| 112 | + teardown), |
| 113 | + }; |
| 114 | + |
| 115 | + torture_filter_tests(tests); |
| 116 | + rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 117 | + return rc; |
| 118 | +} |
0 commit comments