12#include <openssl/evp.h>
13#include <openssl/rand.h>
14#include <openssl/sha.h>
15#include <openssl/x509.h>
16#include <nlohmann/json.hpp>
27 void md_ctx_destroy(EVP_MD_CTX *);
29 using sha256_t = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>;
31 using aes_t = std::vector<std::uint8_t>;
32 using x509_t = util::safe_ptr<X509, X509_free>;
33 using x509_store_t = util::safe_ptr<X509_STORE, X509_STORE_free>;
34 using x509_store_ctx_t = util::safe_ptr<X509_STORE_CTX, X509_STORE_CTX_free>;
35 using cipher_ctx_t = util::safe_ptr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
36 using md_ctx_t = util::safe_ptr<EVP_MD_CTX, md_ctx_destroy>;
37 using bio_t = util::safe_ptr<BIO, BIO_free_all>;
38 using pkey_t = util::safe_ptr<EVP_PKEY, EVP_PKEY_free>;
39 using pkey_ctx_t = util::safe_ptr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
40 using bignum_t = util::safe_ptr<BIGNUM, BN_free>;
45 enum class PERM: uint32_t {
48 _input = _reserved << 8,
49 input_controller = _input << 0,
50 input_touch = _input << 1,
51 input_pen = _input << 2,
52 input_mouse = _input << 3,
53 input_kbd = _input << 4,
54 _all_inputs = input_controller | input_touch | input_pen | input_mouse | input_kbd,
56 _operation = _input << 8,
57 clipboard_set = _operation << 0,
58 clipboard_read = _operation << 1,
59 file_upload = _operation << 2,
60 file_dwnload = _operation << 3,
61 server_cmd = _operation << 4,
62 _all_opeiations = clipboard_set | clipboard_read | file_upload | file_dwnload | server_cmd,
64 _action = _operation << 8,
67 launch = _action << 2,
68 _allow_view = view | launch,
69 _all_actions = list | view | launch,
71 _default = view | list,
73 _all = _all_inputs | _all_opeiations | _all_actions,
77 operator&(PERM x, PERM y) {
78 return static_cast<PERM
>(
static_cast<uint32_t
>(x) &
static_cast<uint32_t
>(y));
83 return static_cast<uint32_t
>(p) == 0;
93 node[
"cmd"] = entry.cmd;
94 node[
"elevated"] = entry.elevated;
103 std::string display_mode;
104 std::list<command_entry_t> do_cmds;
105 std::list<command_entry_t> undo_cmds;
107 bool enable_legacy_ordering;
108 bool allow_client_commands;
109 bool always_use_virtual_display;
112 using p_named_cert_t = std::shared_ptr<named_cert_t>;
119 sha256_t
hash(
const std::string_view &plaintext);
121 aes_t gen_aes_key(
const std::array<uint8_t, 16> &salt,
const std::string_view &pin);
122 x509_t x509(
const std::string_view &x);
123 pkey_t pkey(
const std::string_view &k);
124 std::string pem(x509_t &x509);
125 std::string pem(pkey_t &pkey);
127 std::vector<uint8_t> sign256(
const pkey_t &pkey,
const std::string_view &data);
128 bool verify256(
const x509_t &x509,
const std::string_view &data,
const std::string_view &signature);
130 creds_t gen_creds(
const std::string_view &cn, std::uint32_t key_bits);
132 std::string_view signature(
const x509_t &x);
134 std::string rand(std::size_t bytes);
135 std::string rand_alphabet(std::size_t bytes,
const std::string_view &alphabet = std::string_view {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!%&()=-"});
139 KITTY_DECL_CONSTR(cert_chain_t)
141 void add(p_named_cert_t& named_cert_p);
145 const char *
verify(x509_t::element_type *cert, p_named_cert_t& named_cert_out);
148 std::vector<std::pair<p_named_cert_t, x509_store_t>> _certs;
149 x509_store_ctx_t _cert_ctx;
153 constexpr std::size_t tag_size = 16;
155 constexpr std::size_t round_to_pkcs7_padded(std::size_t size) {
156 return ((size + 15) / 16) * 16;
161 cipher_ctx_t decrypt_ctx;
162 cipher_ctx_t encrypt_ctx;
172 ecb_t(ecb_t &&)
noexcept =
default;
173 ecb_t &operator=(ecb_t &&)
noexcept =
default;
175 ecb_t(
const aes_t &key,
bool padding =
true);
177 int encrypt(
const std::string_view &plaintext, std::vector<std::uint8_t> &cipher);
178 int decrypt(
const std::string_view &cipher, std::vector<std::uint8_t> &plaintext);
184 gcm_t(gcm_t &&)
noexcept =
default;
185 gcm_t &operator=(gcm_t &&)
noexcept =
default;
187 gcm_t(
const crypto::aes_t &key,
bool padding =
true);
197 int encrypt(
const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv);
207 int encrypt(
const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv);
209 int decrypt(
const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
215 cbc_t(cbc_t &&)
noexcept =
default;
216 cbc_t &operator=(cbc_t &&)
noexcept =
default;
218 cbc_t(
const crypto::aes_t &key,
bool padding =
true);
228 int encrypt(
const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv);
234 int decrypt(
const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
const char * verify(x509_t::element_type *cert, p_named_cert_t &named_cert_out)
Verify the certificate chain. When certificates from two or more instances of Moonlight have been add...
int encrypt(const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv)
Encrypts the plaintext using AES CBC mode. length of cipher must be at least: round_to_pkcs7_padded(p...
int decrypt(const std::string_view &cipher, std::vector< std::uint8_t > &plaintext, aes_t *iv)
Decrypts the ciphertext using AES CBC mode.
int encrypt(const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv)
Encrypts the plaintext using AES GCM mode.
sha256_t hash(const std::string_view &plaintext)
Hashes the given plaintext using SHA-256.
PERM
The permissions of a client.
Declarations for utility functions.