aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2018-04-25 16:25:32 +0200
committerAndreas Schneider <asn@cryptomilk.org>2018-04-25 16:25:32 +0200
commit816234350d45ad5743d4cedecc0ed48cc683ed4f (patch)
tree71bb97c2a8686b8da2b96f461355a9b11667ed8a
parent4aeb0cfd9ce401dc0e3a8e71594a73e1b3b49da2 (diff)
downloadlibssh-816234350d45ad5743d4cedecc0ed48cc683ed4f.tar.gz
libssh-816234350d45ad5743d4cedecc0ed48cc683ed4f.tar.xz
libssh-816234350d45ad5743d4cedecc0ed48cc683ed4f.zip
pki: Fix duplicating ed25519 public keys
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--src/pki_ed25519.c25
-rw-r--r--tests/unittests/torture_pki_ed25519.c67
2 files changed, 80 insertions, 12 deletions
diff --git a/src/pki_ed25519.c b/src/pki_ed25519.c
index c33cacb6..45362c4f 100644
--- a/src/pki_ed25519.c
+++ b/src/pki_ed25519.c
@@ -203,24 +203,27 @@ int pki_ed25519_key_cmp(const ssh_key k1,
*/
int pki_ed25519_key_dup(ssh_key new, const ssh_key key)
{
- if (key->ed25519_privkey == NULL || key->ed25519_pubkey == NULL) {
+ if (key->ed25519_privkey == NULL && key->ed25519_pubkey == NULL) {
return SSH_ERROR;
}
- new->ed25519_privkey = malloc(ED25519_SK_LEN);
- if (new->ed25519_privkey == NULL) {
- return SSH_ERROR;
+ if (key->ed25519_privkey != NULL) {
+ new->ed25519_privkey = malloc(ED25519_SK_LEN);
+ if (new->ed25519_privkey == NULL) {
+ return SSH_ERROR;
+ }
+ memcpy(new->ed25519_privkey, key->ed25519_privkey, ED25519_SK_LEN);
}
- new->ed25519_pubkey = malloc(ED25519_PK_LEN);
- if (new->ed25519_privkey == NULL || new->ed25519_pubkey == NULL){
- SAFE_FREE(new->ed25519_privkey);
- return SSH_ERROR;
+ if (key->ed25519_pubkey != NULL) {
+ new->ed25519_pubkey = malloc(ED25519_PK_LEN);
+ if (new->ed25519_pubkey == NULL) {
+ SAFE_FREE(new->ed25519_privkey);
+ return SSH_ERROR;
+ }
+ memcpy(new->ed25519_pubkey, key->ed25519_pubkey, ED25519_PK_LEN);
}
- memcpy(new->ed25519_privkey, key->ed25519_privkey, ED25519_SK_LEN);
- memcpy(new->ed25519_pubkey, key->ed25519_pubkey, ED25519_PK_LEN);
-
return SSH_OK;
}
diff --git a/tests/unittests/torture_pki_ed25519.c b/tests/unittests/torture_pki_ed25519.c
index 2d85197e..e397e50a 100644
--- a/tests/unittests/torture_pki_ed25519.c
+++ b/tests/unittests/torture_pki_ed25519.c
@@ -445,6 +445,69 @@ static void torture_pki_ed25519_import_privkey_base64_passphrase(void **state)
ssh_key_free(key);
}
+static void torture_pki_ed25519_privkey_dup(void **state)
+{
+ const char *passphrase = torture_get_testkey_passphrase();
+ ssh_key key = NULL;
+ ssh_key dup = NULL;
+ int rc;
+
+ (void) state; /* unused */
+
+ rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_ED25519, 0, 1),
+ passphrase,
+ NULL,
+ NULL,
+ &key);
+ assert_true(rc == 0);
+
+ rc = ssh_key_is_private(key);
+ assert_true(rc == 1);
+
+ dup = ssh_key_dup(key);
+ assert_non_null(dup);
+
+ SAFE_FREE(key);
+ SAFE_FREE(dup);
+}
+
+static void torture_pki_ed25519_pubkey_dup(void **state)
+{
+ ssh_key pubkey = NULL;
+ ssh_key dup = NULL;
+ const char *p = strchr(torture_get_testkey_pub(SSH_KEYTYPE_ED25519, 0), ' ');
+ 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);
+
+ 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);
+ SAFE_FREE(pubkey);
+ SAFE_FREE(dup);
+}
+
int torture_run_tests(void) {
int rc;
const struct CMUnitTest tests[] = {
@@ -467,7 +530,9 @@ int torture_run_tests(void) {
cmocka_unit_test(torture_pki_ed25519_import_privkey_base64_passphrase),
cmocka_unit_test(torture_pki_ed25519_sign),
cmocka_unit_test(torture_pki_ed25519_verify),
- cmocka_unit_test(torture_pki_ed25519_verify_bad)
+ cmocka_unit_test(torture_pki_ed25519_verify_bad),
+ cmocka_unit_test(torture_pki_ed25519_privkey_dup),
+ cmocka_unit_test(torture_pki_ed25519_pubkey_dup),
};
ssh_init();