refactor: Clean up KeyCode
This commit is contained in:
1 parent
4eea3194fd
commit
8a96c7441e
5 files changed
+119
-105
No files matched your search
@@ -1,7 +1,8 @@
|
||||
#include "lib/utils/crypto/PrivateKey.h"
|
||||
#ifndef _WIN32
|
||||
#include "PublicKey.h"
|
||||
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/FileUtils.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/crypto/iostreambio.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
@@ -15,56 +16,63 @@ namespace openssl {
|
||||
#include <openssl/rsa.h>
|
||||
} // namespace openssl
|
||||
|
||||
namespace bts {
|
||||
namespace crypto {
|
||||
namespace bts::crypto {
|
||||
|
||||
PublicKey::PublicKey(openssl::EVP_PKEY *key) : PrivateKey(key) {}
|
||||
|
||||
PublicKey::PublicKey(const PrivateKey &key) : PrivateKey() {
|
||||
// I don't know how to do this another way
|
||||
openssl::BIO *bio = openssl::BIO_new(openssl::BIO_s_mem());
|
||||
int i = openssl::PEM_write_bio_PUBKEY(bio, key._key);
|
||||
int err = BIO_flush(bio);
|
||||
UNUSED(i);
|
||||
UNUSED(err);
|
||||
auto *bio = openssl::BIO_new(openssl::BIO_s_mem());
|
||||
bts_assert_msg(bio, "Failed to create new BIO");
|
||||
|
||||
int res = openssl::PEM_write_bio_PUBKEY(bio, key._key);
|
||||
bts_assert_msgf(
|
||||
res == 1, "Failed to read public key (PEM_write_bio_PUBKEY returned {})",
|
||||
res);
|
||||
|
||||
res = BIO_flush(bio);
|
||||
bts_assert_msgf(
|
||||
res == 1, "Failed to flush BIO buffer (BIO_flush returned {})", res);
|
||||
|
||||
auto *k = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
bts_assert_msg(k, "Failed to read public key");
|
||||
|
||||
openssl::EVP_PKEY *k = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
BIO_free(bio);
|
||||
bts_assert(k);
|
||||
this->_key = k;
|
||||
}
|
||||
|
||||
PublicKey::~PublicKey() {}
|
||||
PublicKey::~PublicKey() = default;
|
||||
|
||||
const PublicKeyPtr PublicKey::create(const PrivateKey &private_key) {
|
||||
PublicKeyPtr ret(new PublicKey(private_key));
|
||||
return ret;
|
||||
}
|
||||
|
||||
const PublicKeyPtr PublicKey::read_file(const std::string &filename) {
|
||||
PublicKeyPtr ret;
|
||||
openssl::BIO *bio = openssl::BIO_new_file(filename.c_str(), "r");
|
||||
bts_assert_msg(bio, "can't load keyfile");
|
||||
openssl::EVP_PKEY *key = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
if (!key)
|
||||
LOG(Gen, Error, "Invalid public key read");
|
||||
else
|
||||
ret.reset(new PublicKey(key));
|
||||
return ret;
|
||||
const PublicKeyPtr PublicKey::read_file(const std::string &path) {
|
||||
auto *bio = openssl::BIO_new_file(path.c_str(), "r");
|
||||
if (!bio) {
|
||||
LOG(Gen, Error, "Failed to load public key file '{}'", path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto *key = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
if (!key) {
|
||||
LOG(Gen, Error, "Failed to read public key file '{}'", path);
|
||||
return nullptr;
|
||||
}
|
||||
return PublicKeyPtr(new PublicKey(key));
|
||||
}
|
||||
|
||||
const PublicKeyPtr PublicKey::read_string(const std::string &public_key) {
|
||||
PublicKeyPtr ret;
|
||||
std::stringstream oss;
|
||||
oss << public_key;
|
||||
bts::crypto::iostreambio biostream(&oss);
|
||||
openssl::EVP_PKEY *key =
|
||||
openssl::PEM_read_bio_PUBKEY(biostream.getBIO(), 0, 0, 0);
|
||||
if (!key)
|
||||
auto *key = openssl::PEM_read_bio_PUBKEY(biostream.getBIO(), 0, 0, 0);
|
||||
if (!key) {
|
||||
LOG(Gen, Error, "Invalid public key read");
|
||||
else
|
||||
ret.reset(new PublicKey(key));
|
||||
return ret;
|
||||
return nullptr;
|
||||
}
|
||||
return PublicKeyPtr(new PublicKey(key));
|
||||
}
|
||||
|
||||
const PublicKeyPtr PublicKey::read_stream(std::istream &stream) {
|
||||
@@ -107,7 +115,6 @@ bool PublicKey::decrypt(
|
||||
return public_decrypt(in_data, out_data);
|
||||
}
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace bts
|
||||
} // namespace bts::crypto
|
||||
|
||||
#endif
|
||||
#endif
|
||||
Reference in new issue
Block a user