Aktualizr
C++ SOTA Client
crypto.h
1 #ifndef CRYPTO_H_
2 #define CRYPTO_H_
3 
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>
10 #include <sodium.h>
11 #include <boost/algorithm/hex.hpp>
12 #include <boost/algorithm/string.hpp>
13 #include <boost/algorithm/string/case_conv.hpp>
14 
15 #include <string>
16 #include <utility>
17 
18 #include "libaktualizr/types.h"
19 #include "utilities/utils.h"
20 
21 // some older versions of openssl have BIO_new_mem_buf defined with fisrt parameter of type (void*)
22 // which is not true and breaks our build
23 #undef BIO_new_mem_buf
24 BIO *BIO_new_mem_buf(const void *, int);
25 
27  public:
28  using Ptr = std::shared_ptr<MultiPartHasher>;
29  static Ptr create(Hash::Type hash_type);
30 
31  virtual void update(const unsigned char *part, uint64_t size) = 0;
32  virtual void reset() = 0;
33  virtual std::string getHexDigest() = 0;
34  virtual Hash getHash() = 0;
35  virtual ~MultiPartHasher() = default;
36 };
37 
39  public:
40  MultiPartSHA512Hasher() { crypto_hash_sha512_init(&state_); }
41  ~MultiPartSHA512Hasher() override = default;
42  void update(const unsigned char *part, uint64_t size) override { crypto_hash_sha512_update(&state_, part, size); }
43  void reset() override { crypto_hash_sha512_init(&state_); }
44  std::string getHexDigest() override {
45  std::array<unsigned char, crypto_hash_sha512_BYTES> sha512_hash{};
46  crypto_hash_sha512_final(&state_, sha512_hash.data());
47  return boost::algorithm::hex(std::string(reinterpret_cast<char *>(sha512_hash.data()), crypto_hash_sha512_BYTES));
48  }
49 
50  Hash getHash() override { return Hash(Hash::Type::kSha512, getHexDigest()); }
51 
52  private:
53  crypto_hash_sha512_state state_{};
54 };
55 
57  public:
58  MultiPartSHA256Hasher() { crypto_hash_sha256_init(&state_); }
59  ~MultiPartSHA256Hasher() override = default;
60  void update(const unsigned char *part, uint64_t size) override { crypto_hash_sha256_update(&state_, part, size); }
61  void reset() override { crypto_hash_sha256_init(&state_); }
62  std::string getHexDigest() override {
63  std::array<unsigned char, crypto_hash_sha256_BYTES> sha256_hash{};
64  crypto_hash_sha256_final(&state_, sha256_hash.data());
65  return boost::algorithm::hex(std::string(reinterpret_cast<char *>(sha256_hash.data()), crypto_hash_sha256_BYTES));
66  }
67 
68  Hash getHash() override { return Hash(Hash::Type::kSha256, getHexDigest()); }
69 
70  private:
71  crypto_hash_sha256_state state_{};
72 };
73 
74 class Crypto {
75  public:
76  static std::string sha256digest(const std::string &text);
77  static std::string sha512digest(const std::string &text);
78  static std::string RSAPSSSign(ENGINE *engine, const std::string &private_key, const std::string &message);
79  static std::string Sign(KeyType key_type, ENGINE *engine, const std::string &private_key, const std::string &message);
80  static std::string ED25519Sign(const std::string &private_key, const std::string &message);
81  static bool parseP12(BIO *p12_bio, const std::string &p12_password, std::string *out_pkey, std::string *out_cert,
82  std::string *out_ca);
83  static std::string extractSubjectCN(const std::string &cert);
84  static StructGuard<EVP_PKEY> generateRSAKeyPairEVP(KeyType key_type);
85  static StructGuard<EVP_PKEY> generateRSAKeyPairEVP(const int bits);
86  static bool generateRSAKeyPair(KeyType key_type, std::string *public_key, std::string *private_key);
87  static bool generateEDKeyPair(std::string *public_key, std::string *private_key);
88  static bool generateKeyPair(KeyType key_type, std::string *public_key, std::string *private_key);
89 
90  static bool RSAPSSVerify(const std::string &public_key, const std::string &signature, const std::string &message);
91  static bool ED25519Verify(const std::string &public_key, const std::string &signature, const std::string &message);
92 
93  static bool IsRsaKeyType(KeyType type);
94  static KeyType IdentifyRSAKeyType(const std::string &public_key_pem);
95 
96  static StructGuard<X509> generateCert(const int rsa_bits, const int cert_days, const std::string &cert_c,
97  const std::string &cert_st, const std::string &cert_o,
98  const std::string &cert_cn, bool self_sign = false);
99  static void signCert(const std::string &cacert_path, const std::string &capkey_path, X509 *const certificate);
100  static void serializeCert(std::string *pkey, std::string *cert, X509 *const certificate);
101 };
102 
103 #endif // CRYPTO_H_
Hash
The Hash class The hash of a file or Uptane metadata.
Definition: types.h:159
types.h
MultiPartSHA256Hasher
Definition: crypto.h:56
MultiPartSHA512Hasher
Definition: crypto.h:38
Crypto::generateRSAKeyPair
static bool generateRSAKeyPair(KeyType key_type, std::string *public_key, std::string *private_key)
Generate a RSA keypair.
Definition: crypto.cc:405
Crypto
Definition: crypto.h:74
MultiPartHasher
Definition: crypto.h:26