| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#define LIBSSH_STATIC |
| 4 |
|
| 5 |
#include "torture.h" |
| 6 |
#include "torture_key.h" |
| 7 |
#include "legacy.c" |
| 8 |
|
| 9 |
#define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa" |
| 10 |
|
| 11 |
static int setup_rsa_key(void **state) |
| 12 |
{ |
| 13 |
ssh_session session; |
| 14 |
|
| 15 |
unlink(LIBSSH_RSA_TESTKEY); |
| 16 |
unlink(LIBSSH_RSA_TESTKEY ".pub"); |
| 17 |
|
| 18 |
torture_write_file(LIBSSH_RSA_TESTKEY, |
| 19 |
torture_get_testkey(SSH_KEYTYPE_RSA, 0)); |
| 20 |
torture_write_file(LIBSSH_RSA_TESTKEY ".pub", |
| 21 |
torture_get_testkey_pub(SSH_KEYTYPE_RSA)); |
| 22 |
|
| 23 |
session = ssh_new(); |
| 24 |
*state = session; |
| 25 |
|
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
static int setup_both_keys(void **state) { |
| 30 |
int rc; |
| 31 |
|
| 32 |
rc = setup_rsa_key(state); |
| 33 |
if (rc != 0) { |
| 34 |
return rc; |
| 35 |
} |
| 36 |
|
| 37 |
return rc; |
| 38 |
} |
| 39 |
|
| 40 |
static int setup_both_keys_passphrase(void **state) |
| 41 |
{ |
| 42 |
ssh_session session; |
| 43 |
|
| 44 |
torture_write_file(LIBSSH_RSA_TESTKEY, |
| 45 |
torture_get_testkey(SSH_KEYTYPE_RSA, 1)); |
| 46 |
torture_write_file(LIBSSH_RSA_TESTKEY ".pub", |
| 47 |
torture_get_testkey_pub(SSH_KEYTYPE_RSA)); |
| 48 |
|
| 49 |
session = ssh_new(); |
| 50 |
*state = session; |
| 51 |
|
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
static int teardown(void **state) |
| 56 |
{ |
| 57 |
|
| 58 |
unlink(LIBSSH_RSA_TESTKEY); |
| 59 |
unlink(LIBSSH_RSA_TESTKEY ".pub"); |
| 60 |
|
| 61 |
ssh_free(*state); |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static void torture_pubkey_from_file(void **state) { |
| 67 |
ssh_session session = *state; |
| 68 |
ssh_string pubkey = NULL; |
| 69 |
int type, rc; |
| 70 |
|
| 71 |
rc = ssh_try_publickey_from_file(session, LIBSSH_RSA_TESTKEY, &pubkey, &type); |
| 72 |
|
| 73 |
assert_true(rc == 0); |
| 74 |
|
| 75 |
SSH_STRING_FREE(pubkey); |
| 76 |
|
| 77 |
/* test if it returns 1 if pubkey doesn't exist */ |
| 78 |
unlink(LIBSSH_RSA_TESTKEY ".pub"); |
| 79 |
|
| 80 |
rc = ssh_try_publickey_from_file(session, LIBSSH_RSA_TESTKEY, &pubkey, &type); |
| 81 |
assert_true(rc == 1); |
| 82 |
|
| 83 |
/* This free is unnecessary, but the static analyser does not know */ |
| 84 |
SSH_STRING_FREE(pubkey); |
| 85 |
|
| 86 |
/* test if it returns -1 if privkey doesn't exist */ |
| 87 |
unlink(LIBSSH_RSA_TESTKEY); |
| 88 |
|
| 89 |
rc = ssh_try_publickey_from_file(session, LIBSSH_RSA_TESTKEY, &pubkey, &type); |
| 90 |
assert_true(rc == -1); |
| 91 |
|
| 92 |
/* This free is unnecessary, but the static analyser does not know */ |
| 93 |
SSH_STRING_FREE(pubkey); |
| 94 |
} |
| 95 |
|
| 96 |
static int torture_read_one_line(const char *filename, char *buffer, size_t len) |
| 97 |
{ |
| 98 |
FILE *fp; |
| 99 |
size_t nmemb; |
| 100 |
|
| 101 |
fp = fopen(filename, "r"); |
| 102 |
if (fp == NULL) { |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
nmemb = fread(buffer, len - 2, 1, fp); |
| 107 |
if (nmemb != 0 || ferror(fp)) { |
| 108 |
fclose(fp); |
| 109 |
return -1; |
| 110 |
} |
| 111 |
buffer[len - 1] = '\0'; |
| 112 |
|
| 113 |
fclose(fp); |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
static void torture_pubkey_generate_from_privkey(void **state) { |
| 119 |
ssh_session session = *state; |
| 120 |
ssh_private_key privkey = NULL; |
| 121 |
ssh_public_key pubkey = NULL; |
| 122 |
ssh_string pubkey_orig = NULL; |
| 123 |
ssh_string pubkey_new = NULL; |
| 124 |
char pubkey_line_orig[512] = {0}; |
| 125 |
char pubkey_line_new[512] = {0}; |
| 126 |
char *p; |
| 127 |
int type_orig = 0; |
| 128 |
int type_new = 0; |
| 129 |
int rc; |
| 130 |
|
| 131 |
/* read the publickey */ |
| 132 |
rc = ssh_try_publickey_from_file(session, LIBSSH_RSA_TESTKEY, &pubkey_orig, |
| 133 |
&type_orig); |
| 134 |
assert_true(rc == 0); |
| 135 |
assert_non_null(pubkey_orig); |
| 136 |
|
| 137 |
rc = torture_read_one_line(LIBSSH_RSA_TESTKEY ".pub", pubkey_line_orig, |
| 138 |
sizeof(pubkey_line_orig)); |
| 139 |
assert_true(rc == 0); |
| 140 |
|
| 141 |
/* remove the public key, generate it from the private key and write it. */ |
| 142 |
unlink(LIBSSH_RSA_TESTKEY ".pub"); |
| 143 |
|
| 144 |
privkey = privatekey_from_file(session, LIBSSH_RSA_TESTKEY, 0, NULL); |
| 145 |
assert_non_null(privkey); |
| 146 |
|
| 147 |
pubkey = publickey_from_privatekey(privkey); |
| 148 |
assert_non_null(pubkey); |
| 149 |
type_new = privkey->type; |
| 150 |
privatekey_free(privkey); |
| 151 |
|
| 152 |
pubkey_new = publickey_to_string(pubkey); |
| 153 |
publickey_free(pubkey); |
| 154 |
|
| 155 |
assert_non_null(pubkey_new); |
| 156 |
|
| 157 |
assert_true(ssh_string_len(pubkey_orig) == ssh_string_len(pubkey_new)); |
| 158 |
assert_memory_equal(ssh_string_data(pubkey_orig), |
| 159 |
ssh_string_data(pubkey_new), |
| 160 |
ssh_string_len(pubkey_orig)); |
| 161 |
|
| 162 |
rc = ssh_publickey_to_file(session, LIBSSH_RSA_TESTKEY ".pub", pubkey_new, type_new); |
| 163 |
assert_true(rc == 0); |
| 164 |
|
| 165 |
rc = torture_read_one_line(LIBSSH_RSA_TESTKEY ".pub", pubkey_line_new, |
| 166 |
sizeof(pubkey_line_new)); |
| 167 |
assert_true(rc == 0); |
| 168 |
|
| 169 |
/* do not compare hostname */ |
| 170 |
p = strrchr(pubkey_line_orig, ' '); |
| 171 |
if (p != NULL) { |
| 172 |
*p = '\0'; |
| 173 |
} |
| 174 |
p = strrchr(pubkey_line_new, ' '); |
| 175 |
if (p != NULL) { |
| 176 |
*p = '\0'; |
| 177 |
} |
| 178 |
|
| 179 |
assert_string_equal(pubkey_line_orig, pubkey_line_new); |
| 180 |
|
| 181 |
SSH_STRING_FREE(pubkey_orig); |
| 182 |
SSH_STRING_FREE(pubkey_new); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @brief tests the privatekey_from_file function without passphrase |
| 187 |
*/ |
| 188 |
static void torture_privatekey_from_file(void **state) { |
| 189 |
ssh_session session = *state; |
| 190 |
ssh_private_key key = NULL; |
| 191 |
|
| 192 |
key = privatekey_from_file(session, LIBSSH_RSA_TESTKEY, SSH_KEYTYPE_RSA, NULL); |
| 193 |
assert_non_null(key); |
| 194 |
if (key != NULL) { |
| 195 |
privatekey_free(key); |
| 196 |
key = NULL; |
| 197 |
} |
| 198 |
|
| 199 |
/* Test the automatic type discovery */ |
| 200 |
key = privatekey_from_file(session, LIBSSH_RSA_TESTKEY, 0, NULL); |
| 201 |
assert_non_null(key); |
| 202 |
if (key != NULL) { |
| 203 |
privatekey_free(key); |
| 204 |
key = NULL; |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* @brief tests the privatekey_from_file function with passphrase |
| 211 |
*/ |
| 212 |
static void torture_privatekey_from_file_passphrase(void **state) { |
| 213 |
ssh_session session = *state; |
| 214 |
ssh_private_key key = NULL; |
| 215 |
|
| 216 |
key = privatekey_from_file(session, LIBSSH_RSA_TESTKEY, SSH_KEYTYPE_RSA, TORTURE_TESTKEY_PASSWORD); |
| 217 |
assert_non_null(key); |
| 218 |
if (key != NULL) { |
| 219 |
privatekey_free(key); |
| 220 |
key = NULL; |
| 221 |
} |
| 222 |
|
| 223 |
/* Test the automatic type discovery */ |
| 224 |
key = privatekey_from_file(session, LIBSSH_RSA_TESTKEY, 0, TORTURE_TESTKEY_PASSWORD); |
| 225 |
assert_non_null(key); |
| 226 |
if (key != NULL) { |
| 227 |
privatekey_free(key); |
| 228 |
key = NULL; |
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
int torture_run_tests(void) { |
| 234 |
int rc; |
| 235 |
struct CMUnitTest tests[] = { |
| 236 |
cmocka_unit_test_setup_teardown(torture_pubkey_from_file, |
| 237 |
setup_rsa_key, |
| 238 |
teardown), |
| 239 |
cmocka_unit_test_setup_teardown(torture_pubkey_generate_from_privkey, |
| 240 |
setup_rsa_key, |
| 241 |
teardown), |
| 242 |
cmocka_unit_test_setup_teardown(torture_privatekey_from_file, |
| 243 |
setup_both_keys, |
| 244 |
teardown), |
| 245 |
cmocka_unit_test_setup_teardown(torture_privatekey_from_file_passphrase, |
| 246 |
setup_both_keys_passphrase, |
| 247 |
teardown), |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
ssh_init(); |
| 252 |
torture_filter_tests(tests); |
| 253 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 254 |
ssh_finalize(); |
| 255 |
return rc; |
| 256 |
} |