mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-08 19:02:06 +03:00
tests: Added test for pubkey from privkey.
This commit is contained in:
10
src/pki.c
10
src/pki.c
@@ -733,20 +733,20 @@ ssh_string ssh_pki_publickey_to_blob(const ssh_key key)
|
|||||||
int ssh_pki_export_publickey_base64(const ssh_key key,
|
int ssh_pki_export_publickey_base64(const ssh_key key,
|
||||||
char **b64_key)
|
char **b64_key)
|
||||||
{
|
{
|
||||||
ssh_string key_str;
|
ssh_string key_blob;
|
||||||
unsigned char *b64;
|
unsigned char *b64;
|
||||||
|
|
||||||
if (key == NULL || b64_key == NULL) {
|
if (key == NULL || b64_key == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
key_str = pki_publickey_to_string(key);
|
key_blob = pki_publickey_to_string(key);
|
||||||
if (key_str == NULL) {
|
if (key_blob == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
b64 = bin_to_base64(ssh_string_data(key_str), ssh_string_len(key_str));
|
b64 = bin_to_base64(ssh_string_data(key_blob), ssh_string_len(key_blob));
|
||||||
ssh_string_free(key_str);
|
ssh_string_free(key_blob);
|
||||||
if (b64 == NULL) {
|
if (b64 == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
@@ -93,6 +93,26 @@ static char *read_file(const char *filename) {
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int torture_read_one_line(const char *filename, char *buffer, size_t len) {
|
||||||
|
FILE *fp;
|
||||||
|
size_t rc;
|
||||||
|
|
||||||
|
fp = fopen(filename, "r");
|
||||||
|
if (fp == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = fread(buffer, len, 1, fp);
|
||||||
|
if (rc != 0 || ferror(fp)) {
|
||||||
|
fclose(fp);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void torture_pki_import_privkey_base64_RSA(void **state) {
|
static void torture_pki_import_privkey_base64_RSA(void **state) {
|
||||||
ssh_session session = *state;
|
ssh_session session = *state;
|
||||||
int rc;
|
int rc;
|
||||||
@@ -333,6 +353,45 @@ static void torture_pki_publickey_rsa_base64(void **state)
|
|||||||
ssh_key_free(key);
|
ssh_key_free(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void torture_generate_pubkey_from_privkey(void **state) {
|
||||||
|
char pubkey_original[4096] = {0};
|
||||||
|
char pubkey_generated[4096] = {0};
|
||||||
|
ssh_key privkey;
|
||||||
|
ssh_key pubkey;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
rc = torture_read_one_line(LIBSSH_DSA_TESTKEY ".pub",
|
||||||
|
pubkey_original,
|
||||||
|
sizeof(pubkey_original));
|
||||||
|
assert_true(rc == 0);
|
||||||
|
|
||||||
|
/* remove the public key, generate it from the private key and write it. */
|
||||||
|
unlink(LIBSSH_RSA_TESTKEY ".pub");
|
||||||
|
|
||||||
|
rc = ssh_pki_import_privkey_file(LIBSSH_DSA_TESTKEY,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
&privkey);
|
||||||
|
assert_true(rc == 0);
|
||||||
|
|
||||||
|
pubkey = ssh_pki_publickey_from_privatekey(privkey);
|
||||||
|
assert_true(pubkey != NULL);
|
||||||
|
|
||||||
|
rc = ssh_pki_export_publickey_file(pubkey, LIBSSH_DSA_TESTKEY ".pub");
|
||||||
|
assert_true(rc == 0);
|
||||||
|
|
||||||
|
rc = torture_read_one_line(LIBSSH_DSA_TESTKEY ".pub",
|
||||||
|
pubkey_generated,
|
||||||
|
sizeof(pubkey_generated));
|
||||||
|
assert_true(rc == 0);
|
||||||
|
|
||||||
|
assert_string_equal(pubkey_original, pubkey_generated);
|
||||||
|
|
||||||
|
ssh_key_free(privkey);
|
||||||
|
ssh_key_free(pubkey);
|
||||||
|
}
|
||||||
|
|
||||||
int torture_run_tests(void) {
|
int torture_run_tests(void) {
|
||||||
int rc;
|
int rc;
|
||||||
const UnitTest tests[] = {
|
const UnitTest tests[] = {
|
||||||
@@ -367,6 +426,9 @@ int torture_run_tests(void) {
|
|||||||
setup_rsa_key,
|
setup_rsa_key,
|
||||||
teardown),
|
teardown),
|
||||||
|
|
||||||
|
unit_test_setup_teardown(torture_generate_pubkey_from_privkey,
|
||||||
|
setup_dsa_key,
|
||||||
|
teardown),
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user