diff options
-rw-r--r-- | src/pki_ed25519.c | 25 | ||||
-rw-r--r-- | tests/unittests/torture_pki_ed25519.c | 67 |
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(); |