Skip to content

Commit

Permalink
Add loadIdentity
Browse files Browse the repository at this point in the history
  • Loading branch information
Focshole committed Feb 9, 2023
1 parent 655aa07 commit b4da6ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/opendht/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ OPENDHT_PUBLIC Identity generateEcIdentity(const std::string& name, const Identi
OPENDHT_PUBLIC Identity generateEcIdentity(const std::string& name = "dhtnode", const Identity& ca = {});

OPENDHT_PUBLIC void saveIdentity(const Identity& id, const std::string& path, const std::string& privkey_password = {});
OPENDHT_PUBLIC Identity loadIdentity(const std::string &path,const std::string &privkey_password = {});

/**
* Performs SHA512, SHA256 or SHA1, depending on hash_length.
Expand Down
20 changes: 20 additions & 0 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,26 @@ saveIdentity(const Identity& id, const std::string& path, const std::string& pri
}
}

Identity
loadIdentity(const std::string &path,const std::string &privkey_password)
{
std::ifstream pkStream(path + ".pem", std::ios::in | std::ios::binary);
std::vector<uint8_t> pkContent((std::istreambuf_iterator<char>(pkStream)),
std::istreambuf_iterator<char>());
auto key = std::make_shared<PrivateKey>(pkContent, privkey_password);
pkStream.close();
// Create a certificate
gnutls_x509_crt_t gnuCert;
if (gnutls_x509_crt_init(&gnuCert) != GNUTLS_E_SUCCESS)
throw std::runtime_error("Failed to initialize gnutls certificate struct");
gnutls_datum_t crtContent;
// Read the certificate file
gnutls_load_file((path + ".crt").c_str(), &crtContent);
gnutls_x509_crt_import(gnuCert, &crtContent, GNUTLS_X509_FMT_PEM);
auto cert = std::make_shared<Certificate>(gnuCert);
return {std::move(key), std::move(cert)};
}

void
setValidityPeriod(gnutls_x509_crt_t cert, int64_t validity)
{
Expand Down

0 comments on commit b4da6ee

Please sign in to comment.