4 #include <openssl/engine.h>
5 #include <openssl/err.h>
6 #include <openssl/evp.h>
7 #include <openssl/pem.h>
8 #include <openssl/pkcs12.h>
9 #include <openssl/rsa.h>
11 #include <boost/algorithm/hex.hpp>
12 #include <boost/algorithm/string.hpp>
13 #include <boost/algorithm/string/case_conv.hpp>
19 #include "utilities/utils.h"
23 #undef BIO_new_mem_buf
24 BIO *BIO_new_mem_buf(
const void *,
int);
29 explicit PublicKey(
const boost::filesystem::path &path);
31 explicit PublicKey(Json::Value uptane_json);
33 PublicKey(
const std::string &value, KeyType type);
35 std::string Value()
const {
return value_; }
37 KeyType Type()
const {
return type_; }
41 bool VerifySignature(
const std::string &signature,
const std::string &message)
const;
48 std::string KeyId()
const;
49 bool operator==(
const PublicKey &rhs)
const;
51 bool operator!=(
const PublicKey &rhs)
const {
return !(*
this == rhs); }
58 KeyType type_{KeyType::kUnknown};
63 virtual void update(
const unsigned char *part, uint64_t size) = 0;
64 virtual std::string getHexDigest() = 0;
72 void update(
const unsigned char *part, uint64_t size)
override { crypto_hash_sha512_update(&state_, part, size); }
73 std::string getHexDigest()
override {
74 std::array<unsigned char, crypto_hash_sha512_BYTES> sha512_hash{};
75 crypto_hash_sha512_final(&state_, sha512_hash.data());
76 return boost::algorithm::hex(std::string(reinterpret_cast<char *>(sha512_hash.data()), crypto_hash_sha512_BYTES));
80 crypto_hash_sha512_state state_{};
87 void update(
const unsigned char *part, uint64_t size)
override { crypto_hash_sha256_update(&state_, part, size); }
88 std::string getHexDigest()
override {
89 std::array<unsigned char, crypto_hash_sha256_BYTES> sha256_hash{};
90 crypto_hash_sha256_final(&state_, sha256_hash.data());
91 return boost::algorithm::hex(std::string(reinterpret_cast<char *>(sha256_hash.data()), crypto_hash_sha256_BYTES));
95 crypto_hash_sha256_state state_{};
100 static std::string sha256digest(
const std::string &text);
101 static std::string sha512digest(
const std::string &text);
102 static std::string RSAPSSSign(ENGINE *engine,
const std::string &private_key,
const std::string &message);
103 static std::string Sign(KeyType key_type, ENGINE *engine,
const std::string &private_key,
const std::string &message);
104 static std::string ED25519Sign(
const std::string &private_key,
const std::string &message);
105 static bool parseP12(BIO *p12_bio,
const std::string &p12_password, std::string *out_pkey, std::string *out_cert,
106 std::string *out_ca);
107 static bool extractSubjectCN(
const std::string &cert, std::string *cn);
108 static StructGuard<EVP_PKEY> generateRSAKeyPairEVP(KeyType key_type);
109 static bool generateRSAKeyPair(KeyType key_type, std::string *public_key, std::string *private_key);
110 static bool generateEDKeyPair(std::string *public_key, std::string *private_key);
111 static bool generateKeyPair(KeyType key_type, std::string *public_key, std::string *private_key);
113 static bool RSAPSSVerify(
const std::string &public_key,
const std::string &signature,
const std::string &message);
114 static bool ED25519Verify(
const std::string &public_key,
const std::string &signature,
const std::string &message);
116 static bool IsRsaKeyType(KeyType type);
117 static KeyType IdentifyRSAKeyType(
const std::string &public_key_pem);