aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2011-08-16 23:03:12 +0200
committerAndreas Schneider <asn@cryptomilk.org>2011-08-16 23:06:13 +0200
commit2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4 (patch)
tree363d4a5ce5b06828e2f3c659fd48005c594256af
parentc39181437b10fb49cd236f625c599c01af41f1b2 (diff)
downloadlibssh-2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4.tar.gz
libssh-2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4.tar.xz
libssh-2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4.zip
tests: Added test for pubkey from privkey.
-rw-r--r--src/pki.c10
-rw-r--r--tests/unittests/torture_pki.c62
2 files changed, 67 insertions, 5 deletions
diff --git a/src/pki.c b/src/pki.c
index 2fe1528..0acc370 100644
--- a/src/pki.c
+++ b/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,
char **b64_key)
{
- ssh_string key_str;
+ ssh_string key_blob;
unsigned char *b64;
if (key == NULL || b64_key == NULL) {
return SSH_ERROR;
}
- key_str = pki_publickey_to_string(key);
- if (key_str == NULL) {
+ key_blob = pki_publickey_to_string(key);
+ if (key_blob == NULL) {
return SSH_ERROR;
}
- b64 = bin_to_base64(ssh_string_data(key_str), ssh_string_len(key_str));
- ssh_string_free(key_str);
+ b64 = bin_to_base64(ssh_string_data(key_blob), ssh_string_len(key_blob));
+ ssh_string_free(key_blob);
if (b64 == NULL) {
return SSH_ERROR;
}
diff --git a/tests/unittests/torture_pki.c b/tests/unittests/torture_pki.c
index 643fc12..3d6da3d 100644
--- a/tests/unittests/torture_pki.c
+++ b/tests/unittests/torture_pki.c
@@ -93,6 +93,26 @@ static char *read_file(const char *filename) {
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) {
ssh_session session = *state;
int rc;
@@ -333,6 +353,45 @@ static void torture_pki_publickey_rsa_base64(void **state)
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 rc;
const UnitTest tests[] = {
@@ -367,6 +426,9 @@ int torture_run_tests(void) {
setup_rsa_key,
teardown),
+ unit_test_setup_teardown(torture_generate_pubkey_from_privkey,
+ setup_dsa_key,
+ teardown),
};