Skip to content

Commit

Permalink
Test for #364 Export ED25519 private - add Tests
Browse files Browse the repository at this point in the history
Test export of ED25519 key as SSH2, PEM, PKCS#8 and PKCS#8 encrypted
  • Loading branch information
chris2511 committed Aug 22, 2024
1 parent 8afb392 commit 6d3adb3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
44 changes: 42 additions & 2 deletions test/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ void verify_key(const QString &name, QList<unsigned> hashes, bool priv)
unsigned hash = pki->hash();
qDebug() << pki->getIntName() << hash;
QVERIFY2(hashes.contains(hash),
qPrintable(QString("%1 not expected in %2")
qPrintable(QString("%1 not expected in %2 (%3)")
.arg(pki->getIntName())
.arg(name)
.arg(name).arg(hash)
)
);
pki_key *key = dynamic_cast<pki_key*>(pki);
Expand Down Expand Up @@ -174,6 +174,7 @@ void test_main::exportFormat()
#define INTER_HASH 376625776
#define END_HASH 94304590
#define ENDKEY_HASH 1121702347
#define ED25519_HASH 318722247

#define xstr(s) str(s)
#define str(s) #s
Expand Down Expand Up @@ -304,6 +305,45 @@ void test_main::exportFormat()
verify_key(file, QList<unsigned> { ENDKEY_HASH }, true);
check_pems(file, 0);

// Import ED25519 Key
key = new pki_evp();
key->fromPEMbyteArray(pemdata["ED25519 Key"].toUtf8(), QString("ED25519 Key"));
openssl_error();
Database.insert(key);
dbstatus();

list.clear();
key = dynamic_cast<pki_key*>(keys->getByName("ED25519 Key"));
list << keys->index(key);

// Export ED25519 as Private SSH Key
file = AUTOFILE(ED25519PRIVSSH)
export_by_id(22, file, list, keys);
verify_key(file, QList<unsigned> { ED25519_HASH }, true);
check_pems(file, 1, QStringList{ "BEGIN OPENSSH PRIVATE KEY" });

// Export ED25519 as unencrypted PEM Private Key
file = AUTOFILE(ED25519PRIVPEM)
export_by_id(20, file, list, keys);
verify_key(file, QList<unsigned> { ED25519_HASH }, true);
check_pems(file, 1, QStringList{ "BEGIN PRIVATE KEY" });

// Export ED25519 as unencrypted PKCS#8 Key (Same output as above)
file = AUTOFILE(ED25519PRIVPKCS8)
export_by_id(29, file, list, keys);
verify_key(file, QList<unsigned> { ED25519_HASH }, true);
check_pems(file, 1, QStringList{ "BEGIN PRIVATE KEY" });

// Export ED25519 as encrypted PKCS#8 Key
file = AUTOFILE(ED25519PRIVPKCS8ENC)
pwdialog->setExpectations(QList<pw_expect*>{
new pw_expect("pass", pw_ok),
new pw_expect("pass", pw_ok),
});
export_by_id(28, file, list, keys);
verify_key(file, QList<unsigned> { ED25519_HASH }, true);
check_pems(file, 1, QStringList{ "BEGIN ENCRYPTED PRIVATE KEY" });

} catch (...) {
QString m = QString("Exception thrown L %1").arg(l);
QVERIFY2(false, m.toUtf8().constData());
Expand Down
6 changes: 6 additions & 0 deletions test/pem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,11 @@ hes5O2psnF/9xFdbW/JmS/+Oh4J6bqSxHVphh3H/AoGAWqJA+jO2d75r9JI6+GVx
PHyIfBzGNa1JrRLkQwrfR2E=
-----END PRIVATE KEY-----
)PEM"
},
{ "ED25519 Key", R"PEM(
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIPsR1kBH7fqfR0vUA+tjRUoRpgkfP8EJ/tjsh1dfCRmQ
-----END PRIVATE KEY-----
)PEM"
}
};

0 comments on commit 6d3adb3

Please sign in to comment.