2017-10-28 15:31:37 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-09-04 10:55:35 +04:00
|
|
|
#define LIBSSH_STATIC
|
|
|
|
|
2022-06-30 17:43:35 +03:00
|
|
|
#include "pki.c"
|
2014-09-04 10:55:35 +04:00
|
|
|
#include "torture.h"
|
2018-01-09 23:30:50 +03:00
|
|
|
#include "torture_key.h"
|
2018-01-09 00:47:38 +03:00
|
|
|
#include "torture_pki.h"
|
2014-09-04 10:55:35 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#define LIBSSH_ED25519_TESTKEY "libssh_testkey.id_ed25519"
|
2018-01-10 23:54:27 +03:00
|
|
|
#define LIBSSH_ED25519_TESTKEY_PASSPHRASE "libssh_testkey_passphrase.id_ed25519"
|
2014-09-04 10:55:35 +04:00
|
|
|
|
2018-11-20 17:00:24 +03:00
|
|
|
const char template[] = "temp_dir_XXXXXX";
|
2014-09-04 10:55:35 +04:00
|
|
|
const unsigned char HASH[] = "12345678901234567890";
|
|
|
|
const uint8_t ref_signature[ED25519_SIG_LEN]=
|
|
|
|
"\xbb\x8d\x55\x9f\x06\x14\x39\x24\xb4\xe1\x5a\x57\x3d\x9d\xbe\x22"
|
|
|
|
"\x1b\xc1\x32\xd5\x55\x16\x00\x64\xce\xb4\xc3\xd2\xe3\x6f\x5e\x8d"
|
|
|
|
"\x10\xa3\x18\x93\xdf\xa4\x96\x81\x11\x8e\x1e\x26\x14\x8a\x08\x1b"
|
|
|
|
"\x01\x6a\x60\x59\x9c\x4a\x55\xa3\x16\x56\xf6\xc4\x50\x42\x7f\x03";
|
|
|
|
|
2018-11-20 17:00:24 +03:00
|
|
|
struct pki_st {
|
|
|
|
char *cwd;
|
|
|
|
char *temp_dir;
|
|
|
|
};
|
|
|
|
|
2018-01-09 00:47:38 +03:00
|
|
|
static int setup_ed25519_key(void **state)
|
|
|
|
{
|
2018-09-06 17:52:32 +03:00
|
|
|
const char *keystring = NULL;
|
2018-11-20 17:00:24 +03:00
|
|
|
struct pki_st *test_state = NULL;
|
|
|
|
char *cwd = NULL;
|
|
|
|
char *tmp_dir = NULL;
|
|
|
|
int rc = 0;
|
2018-09-06 17:52:32 +03:00
|
|
|
|
2018-11-20 17:00:24 +03:00
|
|
|
test_state = (struct pki_st *)malloc(sizeof(struct pki_st));
|
|
|
|
assert_non_null(test_state);
|
|
|
|
|
|
|
|
cwd = torture_get_current_working_dir();
|
|
|
|
assert_non_null(cwd);
|
|
|
|
|
|
|
|
tmp_dir = torture_make_temp_dir(template);
|
|
|
|
assert_non_null(tmp_dir);
|
|
|
|
|
|
|
|
test_state->cwd = cwd;
|
|
|
|
test_state->temp_dir = tmp_dir;
|
|
|
|
|
|
|
|
*state = test_state;
|
2018-01-09 00:47:38 +03:00
|
|
|
|
2018-11-20 17:00:24 +03:00
|
|
|
rc = torture_change_dir(tmp_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
printf("Changed directory to: %s\n", tmp_dir);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
2019-03-12 19:25:49 +03:00
|
|
|
keystring = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0);
|
2018-09-06 17:52:32 +03:00
|
|
|
torture_write_file(LIBSSH_ED25519_TESTKEY, keystring);
|
2019-03-12 19:25:49 +03:00
|
|
|
keystring = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 1);
|
2018-09-06 17:52:32 +03:00
|
|
|
torture_write_file(LIBSSH_ED25519_TESTKEY_PASSPHRASE, keystring);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
torture_write_file(LIBSSH_ED25519_TESTKEY ".pub",
|
2019-03-12 19:25:49 +03:00
|
|
|
torture_get_testkey_pub(SSH_KEYTYPE_ED25519));
|
2019-02-28 23:27:31 +03:00
|
|
|
torture_write_file(LIBSSH_ED25519_TESTKEY "-cert.pub",
|
|
|
|
torture_get_testkey_pub(SSH_KEYTYPE_ED25519_CERT01));
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int teardown(void **state) {
|
2018-11-20 17:00:24 +03:00
|
|
|
struct pki_st *test_state = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
test_state = *((struct pki_st **)state);
|
|
|
|
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->cwd);
|
|
|
|
assert_non_null(test_state->temp_dir);
|
|
|
|
|
|
|
|
rc = torture_change_dir(test_state->cwd);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
rc = torture_rmdirs(test_state->temp_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
2018-11-20 17:00:24 +03:00
|
|
|
SAFE_FREE(test_state->temp_dir);
|
|
|
|
SAFE_FREE(test_state->cwd);
|
|
|
|
SAFE_FREE(test_state);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:46:45 +03:00
|
|
|
static void torture_pki_ed25519_import_pubkey_file(void **state)
|
|
|
|
{
|
|
|
|
ssh_key pubkey = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void)state;
|
|
|
|
|
|
|
|
/* The key doesn't have the hostname as comment after the key */
|
|
|
|
rc = ssh_pki_import_pubkey_file(LIBSSH_ED25519_TESTKEY ".pub", &pubkey);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
assert_non_null(pubkey);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(pubkey);
|
2018-09-13 14:46:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_pki_ed25519_import_pubkey_from_openssh_privkey(void **state)
|
|
|
|
{
|
|
|
|
ssh_key pubkey = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void)state;
|
|
|
|
|
|
|
|
/* The key doesn't have the hostname as comment after the key */
|
|
|
|
rc = ssh_pki_import_pubkey_file(LIBSSH_ED25519_TESTKEY_PASSPHRASE, &pubkey);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
assert_non_null(pubkey);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(pubkey);
|
2018-09-13 14:46:45 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 00:47:38 +03:00
|
|
|
static void torture_pki_ed25519_import_privkey_base64(void **state)
|
|
|
|
{
|
|
|
|
int rc;
|
2018-08-20 18:47:23 +03:00
|
|
|
char *key_str = NULL;
|
|
|
|
ssh_key key = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
const char *passphrase = torture_get_testkey_passphrase();
|
|
|
|
enum ssh_keytypes_e type;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
key_str = torture_pki_read_file(LIBSSH_ED25519_TESTKEY);
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(key_str);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_base64(key_str, passphrase, NULL, NULL, &key);
|
|
|
|
assert_true(rc == 0);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
type = ssh_key_type(key);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519);
|
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
rc = ssh_key_is_public(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
free(key_str);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-29 14:31:22 +03:00
|
|
|
static void torture_pki_ed25519_import_privkey_base64_comment(void **state)
|
|
|
|
{
|
|
|
|
int rc, file_str_len;
|
|
|
|
const char *comment_str = "#this is line-comment\n#this is another\n";
|
|
|
|
char *key_str = NULL, *file_str = NULL;
|
|
|
|
ssh_key key = NULL;
|
|
|
|
const char *passphrase = torture_get_testkey_passphrase();
|
|
|
|
enum ssh_keytypes_e type;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
key_str = torture_pki_read_file(LIBSSH_ED25519_TESTKEY);
|
|
|
|
assert_non_null(key_str);
|
|
|
|
|
|
|
|
file_str_len = strlen(comment_str) + strlen(key_str) + 1;
|
|
|
|
file_str = malloc(file_str_len);
|
|
|
|
assert_non_null(file_str);
|
|
|
|
rc = snprintf(file_str, file_str_len, "%s%s", comment_str, key_str);
|
|
|
|
assert_int_equal(rc, file_str_len - 1);
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_base64(file_str, passphrase, NULL, NULL, &key);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_non_null(key);
|
|
|
|
|
|
|
|
type = ssh_key_type(key);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519);
|
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
rc = ssh_key_is_public(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
free(key_str);
|
|
|
|
free(file_str);
|
|
|
|
SSH_KEY_FREE(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_pki_ed25519_import_privkey_base64_whitespace(void **state)
|
|
|
|
{
|
|
|
|
int rc, file_str_len;
|
|
|
|
const char *whitespace_str = " \n\t\t\t\t\t\n\n\n\n\n";
|
|
|
|
char *key_str = NULL, *file_str = NULL;
|
|
|
|
ssh_key key = NULL;
|
|
|
|
const char *passphrase = torture_get_testkey_passphrase();
|
|
|
|
enum ssh_keytypes_e type;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
key_str = torture_pki_read_file(LIBSSH_ED25519_TESTKEY);
|
|
|
|
assert_non_null(key_str);
|
|
|
|
|
|
|
|
file_str_len = strlen(whitespace_str) + strlen(key_str) + 1;
|
|
|
|
file_str = malloc(file_str_len);
|
|
|
|
assert_non_null(file_str);
|
|
|
|
rc = snprintf(file_str, file_str_len, "%s%s", whitespace_str, key_str);
|
|
|
|
assert_int_equal(rc, file_str_len - 1);
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_base64(file_str, passphrase, NULL, NULL, &key);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_non_null(key);
|
|
|
|
|
|
|
|
type = ssh_key_type(key);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519);
|
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
rc = ssh_key_is_public(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
free(key_str);
|
|
|
|
free(file_str);
|
|
|
|
SSH_KEY_FREE(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-04 23:13:52 +03:00
|
|
|
static void torture_pki_ed25519_import_export_privkey_base64(void **state)
|
|
|
|
{
|
|
|
|
char *b64_key = NULL;
|
|
|
|
ssh_key key = NULL;
|
|
|
|
const char *passphrase = torture_get_testkey_passphrase();
|
|
|
|
enum ssh_keytypes_e type;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_base64(torture_get_openssh_testkey(SSH_KEYTYPE_ED25519,
|
|
|
|
false),
|
|
|
|
passphrase,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&key);
|
|
|
|
assert_return_code(rc, errno);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(key);
|
2018-09-04 23:13:52 +03:00
|
|
|
|
|
|
|
type = ssh_key_type(key);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519);
|
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
rc = ssh_pki_export_privkey_base64(key,
|
|
|
|
passphrase,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&b64_key);
|
|
|
|
assert_return_code(rc, errno);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(b64_key);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
2018-09-04 23:13:52 +03:00
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_base64(b64_key,
|
|
|
|
passphrase,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&key);
|
|
|
|
assert_return_code(rc, errno);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(key);
|
2018-09-04 23:13:52 +03:00
|
|
|
|
|
|
|
type = ssh_key_type(key);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519);
|
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
SSH_STRING_FREE_CHAR(b64_key);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
2018-09-04 23:13:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 00:47:38 +03:00
|
|
|
static void torture_pki_ed25519_publickey_from_privatekey(void **state)
|
|
|
|
{
|
|
|
|
int rc;
|
2018-08-20 18:47:23 +03:00
|
|
|
ssh_key key = NULL;
|
|
|
|
ssh_key pubkey = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
const char *passphrase = NULL;
|
2018-09-06 17:52:32 +03:00
|
|
|
const char *keystring = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
2019-03-12 19:25:49 +03:00
|
|
|
keystring = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0);
|
2018-09-06 17:52:32 +03:00
|
|
|
rc = ssh_pki_import_privkey_base64(keystring,
|
2018-01-09 00:47:38 +03:00
|
|
|
passphrase,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&key);
|
|
|
|
assert_true(rc == 0);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
|
|
|
|
assert_true(rc == SSH_OK);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(pubkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
|
|
|
SSH_KEY_FREE(pubkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
}
|
|
|
|
|
2019-02-28 23:27:31 +03:00
|
|
|
static void torture_pki_ed25519_import_cert_file(void **state)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
ssh_key cert = NULL;
|
|
|
|
enum ssh_keytypes_e type;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
rc = ssh_pki_import_cert_file(LIBSSH_ED25519_TESTKEY "-cert.pub", &cert);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_non_null(cert);
|
|
|
|
|
|
|
|
type = ssh_key_type(cert);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519_CERT01);
|
|
|
|
|
|
|
|
rc = ssh_key_is_public(cert);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
SSH_KEY_FREE(cert);
|
|
|
|
}
|
|
|
|
|
2018-01-09 00:47:38 +03:00
|
|
|
static void torture_pki_ed25519_publickey_base64(void **state)
|
|
|
|
{
|
|
|
|
enum ssh_keytypes_e type;
|
2018-08-20 18:47:23 +03:00
|
|
|
char *b64_key = NULL, *key_buf = NULL, *p = NULL;
|
|
|
|
const char *q = NULL;
|
|
|
|
ssh_key key = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
2019-03-12 19:25:49 +03:00
|
|
|
key_buf = strdup(torture_get_testkey_pub(SSH_KEYTYPE_ED25519));
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(key_buf);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
q = p = key_buf;
|
2018-10-01 20:23:52 +03:00
|
|
|
while (p != NULL && *p != '\0' && *p != ' ') p++;
|
|
|
|
if (p != NULL) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
type = ssh_key_type_from_name(q);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519);
|
|
|
|
|
|
|
|
q = ++p;
|
2018-10-01 20:23:52 +03:00
|
|
|
while (p != NULL && *p != '\0' && *p != ' ') p++;
|
|
|
|
if (p != NULL) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_pki_import_pubkey_base64(q, type, &key);
|
|
|
|
assert_true(rc == 0);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_pki_export_pubkey_base64(key, &b64_key);
|
|
|
|
assert_true(rc == 0);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(b64_key);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
assert_string_equal(q, b64_key);
|
|
|
|
|
|
|
|
free(b64_key);
|
|
|
|
free(key_buf);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_pki_ed25519_generate_pubkey_from_privkey(void **state)
|
|
|
|
{
|
|
|
|
char pubkey_generated[4096] = {0};
|
2018-08-20 18:47:23 +03:00
|
|
|
ssh_key privkey = NULL;
|
|
|
|
ssh_key pubkey = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
int rc;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
/* remove the public key, generate it from the private key and write it. */
|
|
|
|
unlink(LIBSSH_ED25519_TESTKEY ".pub");
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
|
|
|
rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey);
|
|
|
|
assert_true(rc == SSH_OK);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(pubkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_pki_export_pubkey_file(pubkey, LIBSSH_ED25519_TESTKEY ".pub");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
|
|
|
rc = torture_read_one_line(LIBSSH_ED25519_TESTKEY ".pub",
|
|
|
|
pubkey_generated,
|
|
|
|
sizeof(pubkey_generated));
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
2019-03-12 19:25:49 +03:00
|
|
|
len = torture_pubkey_len(torture_get_testkey_pub(SSH_KEYTYPE_ED25519));
|
|
|
|
assert_memory_equal(torture_get_testkey_pub(SSH_KEYTYPE_ED25519),
|
2018-01-09 00:47:38 +03:00
|
|
|
pubkey_generated,
|
|
|
|
len);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(privkey);
|
|
|
|
SSH_KEY_FREE(pubkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_pki_ed25519_generate_key(void **state)
|
|
|
|
{
|
|
|
|
int rc;
|
2019-08-22 19:25:09 +03:00
|
|
|
ssh_key key = NULL, pubkey = NULL;
|
2018-08-20 18:47:23 +03:00
|
|
|
ssh_signature sign = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
enum ssh_keytypes_e type = SSH_KEYTYPE_UNKNOWN;
|
|
|
|
const char *type_char = NULL;
|
|
|
|
ssh_session session=ssh_new();
|
2019-08-22 19:11:13 +03:00
|
|
|
uint8_t *raw_sig_data = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
(void) state;
|
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_non_null(session);
|
|
|
|
|
2018-01-09 00:47:38 +03:00
|
|
|
rc = ssh_pki_generate(SSH_KEYTYPE_ED25519, 256, &key);
|
|
|
|
assert_true(rc == SSH_OK);
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(key);
|
2019-08-22 19:25:09 +03:00
|
|
|
rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
|
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
assert_non_null(pubkey);
|
2019-04-29 17:29:16 +03:00
|
|
|
sign = pki_do_sign(key, HASH, 20, SSH_DIGEST_AUTO);
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(sign);
|
2019-08-23 14:30:46 +03:00
|
|
|
rc = ssh_pki_signature_verify(session, sign, pubkey, HASH, 20);
|
2018-01-09 00:47:38 +03:00
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
type = ssh_key_type(key);
|
|
|
|
assert_true(type == SSH_KEYTYPE_ED25519);
|
|
|
|
type_char = ssh_key_type_to_char(type);
|
|
|
|
assert_true(strcmp(type_char, "ssh-ed25519") == 0);
|
|
|
|
|
|
|
|
/* try an invalid signature */
|
2019-08-22 19:11:13 +03:00
|
|
|
#ifdef HAVE_OPENSSL_ED25519
|
|
|
|
raw_sig_data = ssh_string_data(sign->raw_sig);
|
|
|
|
#else
|
|
|
|
raw_sig_data = (uint8_t *)sign->ed25519_sig;
|
|
|
|
#endif
|
|
|
|
assert_non_null(raw_sig_data);
|
|
|
|
(raw_sig_data)[3]^= 0xff;
|
2019-08-23 14:30:46 +03:00
|
|
|
rc = ssh_pki_signature_verify(session, sign, pubkey, HASH, 20);
|
2018-01-09 00:47:38 +03:00
|
|
|
assert_true(rc == SSH_ERROR);
|
|
|
|
|
|
|
|
ssh_signature_free(sign);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
2019-08-22 19:25:09 +03:00
|
|
|
SSH_KEY_FREE(pubkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
ssh_free(session);
|
|
|
|
}
|
|
|
|
|
2019-02-15 02:06:38 +03:00
|
|
|
static void torture_pki_ed25519_cert_verify(void **state)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
ssh_key privkey = NULL, cert = NULL;
|
|
|
|
ssh_signature sign = NULL;
|
|
|
|
ssh_session session=ssh_new();
|
|
|
|
(void) state;
|
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_non_null(session);
|
|
|
|
|
2019-02-15 02:06:38 +03:00
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_non_null(privkey);
|
|
|
|
|
|
|
|
rc = ssh_pki_import_cert_file(LIBSSH_ED25519_TESTKEY "-cert.pub", &cert);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_non_null(cert);
|
|
|
|
|
2019-04-29 17:29:16 +03:00
|
|
|
sign = pki_do_sign(privkey, HASH, 20, SSH_DIGEST_AUTO);
|
2019-02-15 02:06:38 +03:00
|
|
|
assert_non_null(sign);
|
2019-08-23 14:30:46 +03:00
|
|
|
rc = ssh_pki_signature_verify(session, sign, cert, HASH, 20);
|
2019-02-15 02:06:38 +03:00
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
ssh_signature_free(sign);
|
|
|
|
SSH_KEY_FREE(privkey);
|
|
|
|
SSH_KEY_FREE(cert);
|
|
|
|
|
|
|
|
ssh_free(session);
|
|
|
|
}
|
|
|
|
|
2018-01-09 00:47:38 +03:00
|
|
|
static void torture_pki_ed25519_write_privkey(void **state)
|
|
|
|
{
|
2018-08-20 18:47:23 +03:00
|
|
|
ssh_key origkey = NULL;
|
|
|
|
ssh_key privkey = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&origkey);
|
|
|
|
assert_true(rc == 0);
|
2018-09-13 17:33:49 +03:00
|
|
|
assert_non_null(origkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
unlink(LIBSSH_ED25519_TESTKEY);
|
|
|
|
|
|
|
|
rc = ssh_pki_export_privkey_file(origkey,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
LIBSSH_ED25519_TESTKEY);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == 0);
|
2018-09-13 17:33:49 +03:00
|
|
|
assert_non_null(privkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_key_cmp(origkey, privkey, SSH_KEY_CMP_PRIVATE);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
|
|
|
unlink(LIBSSH_ED25519_TESTKEY);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(privkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
/* do the same with passphrase */
|
|
|
|
rc = ssh_pki_export_privkey_file(origkey,
|
|
|
|
torture_get_testkey_passphrase(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
LIBSSH_ED25519_TESTKEY);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
/* opening without passphrase should fail */
|
|
|
|
assert_true(rc == SSH_ERROR);
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY,
|
|
|
|
torture_get_testkey_passphrase(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == 0);
|
2018-09-13 17:33:49 +03:00
|
|
|
assert_non_null(privkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_key_cmp(origkey, privkey, SSH_KEY_CMP_PRIVATE);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
unlink(LIBSSH_ED25519_TESTKEY);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(origkey);
|
|
|
|
SSH_KEY_FREE(privkey);
|
2018-01-10 23:54:27 +03:00
|
|
|
|
|
|
|
/* Test with passphrase */
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY_PASSPHRASE,
|
|
|
|
torture_get_testkey_passphrase(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&origkey);
|
|
|
|
assert_true(rc == 0);
|
2018-09-13 17:33:49 +03:00
|
|
|
assert_non_null(origkey);
|
2018-01-10 23:54:27 +03:00
|
|
|
|
|
|
|
unlink(LIBSSH_ED25519_TESTKEY_PASSPHRASE);
|
|
|
|
rc = ssh_pki_export_privkey_file(origkey,
|
|
|
|
torture_get_testkey_passphrase(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
LIBSSH_ED25519_TESTKEY_PASSPHRASE);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
|
|
|
/* Test with invalid passphrase */
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY_PASSPHRASE,
|
|
|
|
"invalid secret",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == SSH_ERROR);
|
|
|
|
|
|
|
|
rc = ssh_pki_import_privkey_file(LIBSSH_ED25519_TESTKEY_PASSPHRASE,
|
|
|
|
torture_get_testkey_passphrase(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == 0);
|
2018-09-13 17:33:49 +03:00
|
|
|
assert_non_null(privkey);
|
2018-01-10 23:54:27 +03:00
|
|
|
|
|
|
|
rc = ssh_key_cmp(origkey, privkey, SSH_KEY_CMP_PRIVATE);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(origkey);
|
|
|
|
SSH_KEY_FREE(privkey);
|
2018-01-09 00:47:38 +03:00
|
|
|
}
|
|
|
|
|
2018-09-06 17:52:32 +03:00
|
|
|
static void torture_pki_ed25519_sign(void **state)
|
|
|
|
{
|
2018-08-20 18:47:23 +03:00
|
|
|
ssh_key privkey = NULL;
|
|
|
|
ssh_signature sig = NULL;
|
|
|
|
ssh_string blob = NULL;
|
2018-09-06 17:52:32 +03:00
|
|
|
const char *keystring = NULL;
|
2014-09-04 10:55:35 +04:00
|
|
|
int rc;
|
2018-08-06 10:05:40 +03:00
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
2014-09-04 10:55:35 +04:00
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
(void)state;
|
2018-08-06 10:05:40 +03:00
|
|
|
|
2019-03-12 19:25:49 +03:00
|
|
|
keystring = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0);
|
2018-09-06 17:52:32 +03:00
|
|
|
rc = ssh_pki_import_privkey_base64(keystring,
|
2018-08-06 10:05:40 +03:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
2014-09-04 10:55:35 +04:00
|
|
|
assert_true(rc == SSH_OK);
|
2018-08-06 10:05:40 +03:00
|
|
|
assert_non_null(privkey);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
sig = pki_do_sign(privkey, HASH, sizeof(HASH), SSH_DIGEST_AUTO);
|
|
|
|
assert_non_null(sig);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
|
|
|
blob = pki_signature_to_blob(sig);
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(blob);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
|
|
|
assert_int_equal(ssh_string_len(blob), sizeof(ref_signature));
|
2019-08-30 19:41:16 +03:00
|
|
|
assert_memory_equal(ssh_string_data(blob), ref_signature,
|
|
|
|
sizeof(ref_signature));
|
|
|
|
|
2014-09-04 10:55:35 +04:00
|
|
|
ssh_signature_free(sig);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(privkey);
|
2018-09-24 19:56:22 +03:00
|
|
|
SSH_STRING_FREE(blob);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:41:16 +03:00
|
|
|
static void torture_pki_ed25519_sign_openssh_privkey_passphrase(void **state)
|
|
|
|
{
|
|
|
|
ssh_key privkey = NULL;
|
|
|
|
ssh_signature sig = NULL;
|
|
|
|
ssh_string blob = NULL;
|
|
|
|
const char *keystring = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)state;
|
|
|
|
|
|
|
|
keystring = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 1);
|
|
|
|
rc = ssh_pki_import_privkey_base64(keystring,
|
|
|
|
torture_get_testkey_passphrase(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
assert_non_null(privkey);
|
|
|
|
|
|
|
|
sig = pki_do_sign(privkey, HASH, sizeof(HASH), SSH_DIGEST_AUTO);
|
|
|
|
assert_non_null(sig);
|
|
|
|
|
|
|
|
blob = pki_signature_to_blob(sig);
|
|
|
|
assert_non_null(blob);
|
|
|
|
assert_int_equal(ssh_string_len(blob), sizeof(ref_signature));
|
|
|
|
assert_memory_equal(ssh_string_data(blob), ref_signature,
|
|
|
|
sizeof(ref_signature));
|
|
|
|
|
|
|
|
ssh_signature_free(sig);
|
|
|
|
SSH_KEY_FREE(privkey);
|
|
|
|
SSH_STRING_FREE(blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENSSL_ED25519
|
|
|
|
static void torture_pki_ed25519_sign_pkcs8_privkey(void **state)
|
|
|
|
{
|
|
|
|
ssh_key privkey = NULL;
|
|
|
|
ssh_signature sig = NULL;
|
|
|
|
ssh_string blob = NULL;
|
|
|
|
const char *keystring = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)state;
|
|
|
|
|
|
|
|
keystring = torture_get_testkey(SSH_KEYTYPE_ED25519, 0);
|
|
|
|
rc = ssh_pki_import_privkey_base64(keystring,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
assert_non_null(privkey);
|
|
|
|
|
|
|
|
sig = pki_do_sign(privkey, HASH, sizeof(HASH), SSH_DIGEST_AUTO);
|
|
|
|
assert_non_null(sig);
|
|
|
|
|
|
|
|
blob = pki_signature_to_blob(sig);
|
|
|
|
assert_non_null(blob);
|
|
|
|
assert_int_equal(ssh_string_len(blob), sizeof(ref_signature));
|
|
|
|
assert_memory_equal(ssh_string_data(blob), ref_signature,
|
|
|
|
sizeof(ref_signature));
|
|
|
|
|
|
|
|
ssh_signature_free(sig);
|
|
|
|
SSH_KEY_FREE(privkey);
|
|
|
|
SSH_STRING_FREE(blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_pki_ed25519_sign_pkcs8_privkey_passphrase(void **state)
|
|
|
|
{
|
|
|
|
ssh_key privkey = NULL;
|
|
|
|
ssh_signature sig = NULL;
|
|
|
|
ssh_string blob = NULL;
|
|
|
|
const char *keystring = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)state;
|
|
|
|
|
|
|
|
keystring = torture_get_testkey(SSH_KEYTYPE_ED25519, 1);
|
|
|
|
rc = ssh_pki_import_privkey_base64(keystring,
|
|
|
|
torture_get_testkey_passphrase(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&privkey);
|
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
assert_non_null(privkey);
|
|
|
|
|
|
|
|
sig = pki_do_sign(privkey, HASH, sizeof(HASH), SSH_DIGEST_AUTO);
|
|
|
|
assert_non_null(sig);
|
|
|
|
|
|
|
|
blob = pki_signature_to_blob(sig);
|
|
|
|
assert_non_null(blob);
|
|
|
|
assert_int_equal(ssh_string_len(blob), sizeof(ref_signature));
|
|
|
|
assert_memory_equal(ssh_string_data(blob), ref_signature,
|
|
|
|
sizeof(ref_signature));
|
|
|
|
|
|
|
|
ssh_signature_free(sig);
|
|
|
|
SSH_KEY_FREE(privkey);
|
|
|
|
SSH_STRING_FREE(blob);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_OPENSSL_ED25519 */
|
|
|
|
|
2014-09-04 10:55:35 +04:00
|
|
|
static void torture_pki_ed25519_verify(void **state){
|
2018-08-20 18:47:23 +03:00
|
|
|
ssh_key pubkey = NULL;
|
|
|
|
ssh_signature sig = NULL;
|
2019-08-22 19:11:13 +03:00
|
|
|
ssh_session session = NULL;
|
2014-09-04 10:55:35 +04:00
|
|
|
ssh_string blob = ssh_string_new(ED25519_SIG_LEN);
|
2019-03-12 19:25:49 +03:00
|
|
|
char *pkey_ptr = strdup(strchr(torture_get_testkey_pub(SSH_KEYTYPE_ED25519), ' ') + 1);
|
2018-08-20 18:47:23 +03:00
|
|
|
char *ptr = NULL;
|
2019-08-22 19:11:13 +03:00
|
|
|
uint8_t *raw_sig_data = NULL;
|
2014-09-04 10:55:35 +04:00
|
|
|
int rc;
|
|
|
|
(void) state;
|
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
session = ssh_new();
|
|
|
|
assert_non_null(session);
|
|
|
|
|
2014-09-04 10:55:35 +04:00
|
|
|
/* remove trailing comment */
|
|
|
|
ptr = strchr(pkey_ptr, ' ');
|
|
|
|
if(ptr != NULL){
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
rc = ssh_pki_import_pubkey_base64(pkey_ptr, SSH_KEYTYPE_ED25519, &pubkey);
|
|
|
|
assert_true(rc == SSH_OK);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(pubkey);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
2020-12-10 17:01:32 +03:00
|
|
|
rc = ssh_string_fill(blob, ref_signature, ED25519_SIG_LEN);
|
|
|
|
assert_int_equal(rc, 0);
|
2018-08-07 13:17:29 +03:00
|
|
|
sig = pki_signature_from_blob(pubkey, blob, SSH_KEYTYPE_ED25519, SSH_DIGEST_AUTO);
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(sig);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
2019-08-23 14:30:46 +03:00
|
|
|
rc = ssh_pki_signature_verify(session, sig, pubkey, HASH, sizeof(HASH));
|
2014-09-04 10:55:35 +04:00
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
/* Alter signature and expect verification error */
|
|
|
|
#if defined(HAVE_OPENSSL_ED25519)
|
|
|
|
raw_sig_data = ssh_string_data(sig->raw_sig);
|
|
|
|
#else
|
|
|
|
raw_sig_data = (uint8_t *)sig->ed25519_sig;
|
|
|
|
#endif
|
|
|
|
assert_non_null(raw_sig_data);
|
|
|
|
(raw_sig_data)[3]^= 0xff;
|
2019-08-23 14:30:46 +03:00
|
|
|
rc = ssh_pki_signature_verify(session, sig, pubkey, HASH, sizeof(HASH));
|
2019-08-22 19:11:13 +03:00
|
|
|
assert_true(rc == SSH_ERROR);
|
|
|
|
|
2014-09-04 10:55:35 +04:00
|
|
|
ssh_signature_free(sig);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(pubkey);
|
2018-09-24 19:56:22 +03:00
|
|
|
SSH_STRING_FREE(blob);
|
2015-04-02 16:04:34 +03:00
|
|
|
free(pkey_ptr);
|
2019-08-22 19:11:13 +03:00
|
|
|
ssh_free(session);
|
2014-09-04 10:55:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_pki_ed25519_verify_bad(void **state){
|
2018-08-20 18:47:23 +03:00
|
|
|
ssh_key pubkey = NULL;
|
|
|
|
ssh_signature sig = NULL;
|
2019-08-22 19:11:13 +03:00
|
|
|
ssh_session session = NULL;
|
2014-09-04 10:55:35 +04:00
|
|
|
ssh_string blob = ssh_string_new(ED25519_SIG_LEN);
|
2019-03-12 19:25:49 +03:00
|
|
|
char *pkey_ptr = strdup(strchr(torture_get_testkey_pub(SSH_KEYTYPE_ED25519), ' ') + 1);
|
2018-08-20 18:47:23 +03:00
|
|
|
char *ptr = NULL;
|
2014-09-04 10:55:35 +04:00
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
(void) state;
|
|
|
|
|
2019-08-22 19:11:13 +03:00
|
|
|
/* Skip test if in FIPS mode */
|
|
|
|
if (ssh_fips_mode()) {
|
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
session = ssh_new();
|
|
|
|
assert_non_null(session);
|
|
|
|
|
2014-09-04 10:55:35 +04:00
|
|
|
/* remove trailing comment */
|
|
|
|
ptr = strchr(pkey_ptr, ' ');
|
|
|
|
if(ptr != NULL){
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
rc = ssh_pki_import_pubkey_base64(pkey_ptr, SSH_KEYTYPE_ED25519, &pubkey);
|
|
|
|
assert_true(rc == SSH_OK);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(pubkey);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
|
|
|
/* alter signature and expect false result */
|
|
|
|
|
|
|
|
for (i=0; i < ED25519_SIG_LEN; ++i){
|
2020-12-10 17:01:32 +03:00
|
|
|
rc = ssh_string_fill(blob, ref_signature, ED25519_SIG_LEN);
|
|
|
|
assert_int_equal(rc, 0);
|
2014-09-04 10:55:35 +04:00
|
|
|
((uint8_t *)ssh_string_data(blob))[i] ^= 0xff;
|
2018-08-07 13:17:29 +03:00
|
|
|
sig = pki_signature_from_blob(pubkey, blob, SSH_KEYTYPE_ED25519, SSH_DIGEST_AUTO);
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(sig);
|
2014-09-04 10:55:35 +04:00
|
|
|
|
2019-08-23 14:30:46 +03:00
|
|
|
rc = ssh_pki_signature_verify(session, sig, pubkey, HASH, sizeof(HASH));
|
2014-09-04 10:55:35 +04:00
|
|
|
assert_true(rc == SSH_ERROR);
|
|
|
|
ssh_signature_free(sig);
|
|
|
|
|
|
|
|
}
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(pubkey);
|
2018-09-24 19:56:22 +03:00
|
|
|
SSH_STRING_FREE(blob);
|
2015-04-02 16:04:34 +03:00
|
|
|
free(pkey_ptr);
|
2019-08-22 19:11:13 +03:00
|
|
|
ssh_free(session);
|
2014-09-04 10:55:35 +04:00
|
|
|
}
|
|
|
|
|
2018-01-09 00:47:38 +03:00
|
|
|
static void torture_pki_ed25519_import_privkey_base64_passphrase(void **state)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
ssh_key key = NULL;
|
|
|
|
const char *passphrase = torture_get_testkey_passphrase();
|
2018-09-06 17:52:32 +03:00
|
|
|
const char *testkey = NULL;
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
/* same for ED25519 */
|
2019-03-12 19:25:49 +03:00
|
|
|
testkey = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 1);
|
2018-09-06 17:52:32 +03:00
|
|
|
rc = ssh_pki_import_privkey_base64(testkey,
|
2018-01-09 00:47:38 +03:00
|
|
|
passphrase,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&key);
|
|
|
|
assert_true(rc == 0);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
|
|
|
|
/* test if it returns -1 if passphrase is wrong */
|
2018-09-06 17:52:32 +03:00
|
|
|
rc = ssh_pki_import_privkey_base64(testkey,
|
2018-01-09 00:47:38 +03:00
|
|
|
"wrong passphrase !!",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&key);
|
|
|
|
assert_true(rc == -1);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
2018-01-09 00:47:38 +03:00
|
|
|
}
|
|
|
|
|
2018-04-25 17:25:32 +03:00
|
|
|
static void torture_pki_ed25519_privkey_dup(void **state)
|
|
|
|
{
|
|
|
|
const char *passphrase = torture_get_testkey_passphrase();
|
|
|
|
ssh_key key = NULL;
|
|
|
|
ssh_key dup = NULL;
|
2018-09-06 17:52:32 +03:00
|
|
|
const char *testkey = NULL;
|
2018-04-25 17:25:32 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
2019-03-12 19:25:49 +03:00
|
|
|
testkey = torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 1);
|
2018-09-06 17:52:32 +03:00
|
|
|
rc = ssh_pki_import_privkey_base64(testkey,
|
2018-04-25 17:25:32 +03:00
|
|
|
passphrase,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&key);
|
|
|
|
assert_true(rc == 0);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(key);
|
2018-04-25 17:25:32 +03:00
|
|
|
|
|
|
|
rc = ssh_key_is_private(key);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
dup = ssh_key_dup(key);
|
|
|
|
assert_non_null(dup);
|
|
|
|
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(key);
|
|
|
|
SSH_KEY_FREE(dup);
|
2018-04-25 17:25:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_pki_ed25519_pubkey_dup(void **state)
|
|
|
|
{
|
|
|
|
ssh_key pubkey = NULL;
|
|
|
|
ssh_key dup = NULL;
|
2019-03-12 19:25:49 +03:00
|
|
|
const char *p = strchr(torture_get_testkey_pub(SSH_KEYTYPE_ED25519), ' ');
|
2018-04-25 17:25:32 +03:00
|
|
|
char *pub_str = NULL;
|
|
|
|
char *q = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
pub_str = strdup(p + 1);
|
|
|
|
assert_non_null(pub_str);
|
|
|
|
|
|
|
|
q = strchr(pub_str, ' ');
|
|
|
|
assert_non_null(q);
|
|
|
|
*q = '\0';
|
|
|
|
|
|
|
|
rc = ssh_pki_import_pubkey_base64(pub_str,
|
|
|
|
SSH_KEYTYPE_ED25519,
|
|
|
|
&pubkey);
|
|
|
|
assert_true(rc == 0);
|
2019-01-15 16:35:37 +03:00
|
|
|
assert_non_null(pubkey);
|
2018-04-25 17:25:32 +03:00
|
|
|
|
|
|
|
rc = ssh_key_is_public(pubkey);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
dup = ssh_key_dup(pubkey);
|
|
|
|
assert_non_null(dup);
|
|
|
|
|
|
|
|
rc = ssh_key_is_public(dup);
|
|
|
|
assert_true(rc == 1);
|
|
|
|
|
|
|
|
SAFE_FREE(pub_str);
|
2018-09-24 19:42:22 +03:00
|
|
|
SSH_KEY_FREE(pubkey);
|
|
|
|
SSH_KEY_FREE(dup);
|
2018-04-25 17:25:32 +03:00
|
|
|
}
|
|
|
|
|
2014-09-04 10:55:35 +04:00
|
|
|
int torture_run_tests(void) {
|
|
|
|
int rc;
|
2018-09-04 23:13:52 +03:00
|
|
|
struct CMUnitTest tests[] = {
|
2018-09-13 14:46:45 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_import_pubkey_file,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_import_pubkey_from_openssh_privkey,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
2018-01-09 00:47:38 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_import_privkey_base64,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
2019-07-29 14:31:22 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_import_privkey_base64_comment,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_import_privkey_base64_whitespace,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
2018-09-04 23:13:52 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_import_export_privkey_base64,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
2018-01-09 00:47:38 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_publickey_from_privatekey,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
2019-02-28 23:27:31 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_import_cert_file,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
2018-01-09 00:47:38 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_publickey_base64,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_generate_pubkey_from_privkey,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
|
|
|
cmocka_unit_test(torture_pki_ed25519_generate_key),
|
2019-02-15 02:06:38 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_cert_verify,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
2018-01-09 00:47:38 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_pki_ed25519_write_privkey,
|
|
|
|
setup_ed25519_key,
|
|
|
|
teardown),
|
|
|
|
cmocka_unit_test(torture_pki_ed25519_import_privkey_base64_passphrase),
|
2015-09-07 11:39:51 +03:00
|
|
|
cmocka_unit_test(torture_pki_ed25519_sign),
|
2019-08-30 19:41:16 +03:00
|
|
|
cmocka_unit_test(torture_pki_ed25519_sign_openssh_privkey_passphrase),
|
|
|
|
#ifdef HAVE_OPENSSL_ED25519
|
|
|
|
cmocka_unit_test(torture_pki_ed25519_sign_pkcs8_privkey),
|
|
|
|
cmocka_unit_test(torture_pki_ed25519_sign_pkcs8_privkey_passphrase),
|
|
|
|
#endif
|
2015-09-07 11:39:51 +03:00
|
|
|
cmocka_unit_test(torture_pki_ed25519_verify),
|
2018-04-25 17:25:32 +03:00
|
|
|
cmocka_unit_test(torture_pki_ed25519_verify_bad),
|
|
|
|
cmocka_unit_test(torture_pki_ed25519_privkey_dup),
|
|
|
|
cmocka_unit_test(torture_pki_ed25519_pubkey_dup),
|
2014-09-04 10:55:35 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ssh_init();
|
2018-09-04 23:13:52 +03:00
|
|
|
torture_filter_tests(tests);
|
2015-09-07 11:39:51 +03:00
|
|
|
rc = cmocka_run_group_tests(tests, NULL, NULL);
|
2014-09-04 10:55:35 +04:00
|
|
|
ssh_finalize();
|
|
|
|
return rc;
|
|
|
|
}
|