Files
spark/src/lib/utils/crypto/PublicKey.cpp
T

131 lines
3.8 KiB
C++

#include "lib/utils/crypto/PrivateKey.h"
#ifndef _WIN32
#include "PublicKey.h"
#include "lib/config/protos/keycode.pb.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"
#include <sstream>
namespace openssl {
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
} // namespace openssl
namespace bts::crypto {
PublicKey::PublicKey(openssl::EVP_PKEY *key) : PrivateKey(key) {}
PublicKey::PublicKey(const PrivateKey &key) : PrivateKey() {
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");
BIO_free(bio);
bts_assert(k);
this->_key = k;
}
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 &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(const bts::config::PublicKey &public_key) {
if (public_key.key_lines().size() > 0) {
return read_string(
fmt::to_string(fmt::join(public_key.key_lines(), "\n")));
}
return read_string(
bts::spartan::FileUtils::read_file(public_key.key_file_path()));
}
const PublicKeyPtr PublicKey::read_string(const std::string &public_key) {
std::stringstream oss;
oss << public_key;
bts::crypto::iostreambio biostream(&oss);
auto *key = openssl::PEM_read_bio_PUBKEY(biostream.getBIO(), 0, 0, 0);
if (!key) {
LOG(Gen, Error, "Invalid public key read");
return nullptr;
}
return PublicKeyPtr(new PublicKey(key));
}
const PublicKeyPtr PublicKey::read_stream(std::istream &stream) {
PublicKeyPtr ret;
stream.seekg(0, std::ios::end);
int length = stream.tellg();
stream.seekg(0, std::ios::beg);
char *data = new char[length];
stream.read(data, length);
openssl::BIO *bio = openssl::BIO_new(openssl::BIO_s_mem());
BIO_write(bio, data, length);
delete[] data;
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;
}
void PublicKey::write(openssl::BIO *bio, CipherType::type) const {
// const openssl::EVP_CIPHER* ciph =
// openssl::EVP_get_cipherbyname(CipherType::name[(int)cipher_type]);
// bts_assert_msg(ciph, "unknown cipher type");
int nbytes = openssl::PEM_write_bio_PUBKEY(bio, _key);
UNUSED(nbytes);
int err = BIO_flush(bio);
UNUSED(err);
}
bool PublicKey::encrypt(
const std::iostream &in_data, std::iostream &out_data) const {
return public_encrypt(in_data, out_data);
}
bool PublicKey::decrypt(
const std::iostream &in_data, std::iostream &out_data) const {
return public_decrypt(in_data, out_data);
}
} // namespace bts::crypto
#endif