Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
crypto.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <array>
9
10// lib includes
11#include <list>
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>
17
18// local includes
19#include "utility.h"
20
21namespace crypto {
22 struct creds_t {
23 std::string x509;
24 std::string pkey;
25 };
26
27 void md_ctx_destroy(EVP_MD_CTX *);
28
29 using sha256_t = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>;
30
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>;
41
45 enum class PERM: uint32_t {
46 _reserved = 1,
47
48 _input = _reserved << 8, // Input permission group
49 input_controller = _input << 0, // Allow controller input
50 input_touch = _input << 1, // Allow touch input
51 input_pen = _input << 2, // Allow pen input
52 input_mouse = _input << 3, // Allow mouse input
53 input_kbd = _input << 4, // Allow keyboard input
54 _all_inputs = input_controller | input_touch | input_pen | input_mouse | input_kbd,
55
56 _operation = _input << 8, // Operation permission group
57 clipboard_set = _operation << 0, // Allow set clipboard from client
58 clipboard_read = _operation << 1, // Allow read clipboard from host
59 file_upload = _operation << 2, // Allow upload files to host
60 file_dwnload = _operation << 3, // Allow download files from host
61 server_cmd = _operation << 4, // Allow execute server cmd
62 _all_opeiations = clipboard_set | clipboard_read | file_upload | file_dwnload | server_cmd,
63
64 _action = _operation << 8, // Action permission group
65 list = _action << 0, // Allow list apps
66 view = _action << 1, // Allow view streams
67 launch = _action << 2, // Allow launch apps
68 _allow_view = view | launch, // If no view permission is granted, disconnect the device upon permission update
69 _all_actions = list | view | launch,
70
71 _default = view | list, // Default permissions for new clients
72 _no = 0, // No permissions are granted
73 _all = _all_inputs | _all_opeiations | _all_actions, // All current permissions
74 };
75
76 inline constexpr PERM
77 operator&(PERM x, PERM y) {
78 return static_cast<PERM>(static_cast<uint32_t>(x) & static_cast<uint32_t>(y));
79 }
80
81 inline constexpr bool
82 operator!(PERM p) {
83 return static_cast<uint32_t>(p) == 0;
84 }
85
87 std::string cmd;
88 bool elevated;
89
90 // Serialize method using nlohmann::json
91 static inline nlohmann::json serialize(const command_entry_t& entry) {
92 nlohmann::json node;
93 node["cmd"] = entry.cmd;
94 node["elevated"] = entry.elevated;
95 return node;
96 }
97 };
98
99 struct named_cert_t {
100 std::string name;
101 std::string uuid;
102 std::string cert;
103 std::string display_mode;
104 std::list<command_entry_t> do_cmds;
105 std::list<command_entry_t> undo_cmds;
106 PERM perm;
107 bool enable_legacy_ordering;
108 bool allow_client_commands;
109 bool always_use_virtual_display;
110 };
111
112 using p_named_cert_t = std::shared_ptr<named_cert_t>;
113
119 sha256_t hash(const std::string_view &plaintext);
120
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);
126
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);
129
130 creds_t gen_creds(const std::string_view &cn, std::uint32_t key_bits);
131
132 std::string_view signature(const x509_t &x);
133
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!%&()=-"});
136
137 class cert_chain_t {
138 public:
139 KITTY_DECL_CONSTR(cert_chain_t)
140
141 void add(p_named_cert_t& named_cert_p);
142
143 void clear();
144
145 const char *verify(x509_t::element_type *cert, p_named_cert_t& named_cert_out);
146
147 private:
148 std::vector<std::pair<p_named_cert_t, x509_store_t>> _certs;
149 x509_store_ctx_t _cert_ctx;
150 };
151
152 namespace cipher {
153 constexpr std::size_t tag_size = 16;
154
155 constexpr std::size_t round_to_pkcs7_padded(std::size_t size) {
156 return ((size + 15) / 16) * 16;
157 }
158
159 class cipher_t {
160 public:
161 cipher_ctx_t decrypt_ctx;
162 cipher_ctx_t encrypt_ctx;
163
164 aes_t key;
165
166 bool padding;
167 };
168
169 class ecb_t: public cipher_t {
170 public:
171 ecb_t() = default;
172 ecb_t(ecb_t &&) noexcept = default;
173 ecb_t &operator=(ecb_t &&) noexcept = default;
174
175 ecb_t(const aes_t &key, bool padding = true);
176
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);
179 };
180
181 class gcm_t: public cipher_t {
182 public:
183 gcm_t() = default;
184 gcm_t(gcm_t &&) noexcept = default;
185 gcm_t &operator=(gcm_t &&) noexcept = default;
186
187 gcm_t(const crypto::aes_t &key, bool padding = true);
188
197 int encrypt(const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv);
198
207 int encrypt(const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv);
208
209 int decrypt(const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
210 };
211
212 class cbc_t: public cipher_t {
213 public:
214 cbc_t() = default;
215 cbc_t(cbc_t &&) noexcept = default;
216 cbc_t &operator=(cbc_t &&) noexcept = default;
217
218 cbc_t(const crypto::aes_t &key, bool padding = true);
219
228 int encrypt(const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv);
229
234 int decrypt(const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
235 };
236 } // namespace cipher
237} // namespace crypto
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...
Definition crypto.cpp:55
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...
Definition crypto.cpp:279
int decrypt(const std::string_view &cipher, std::vector< std::uint8_t > &plaintext, aes_t *iv)
Decrypts the ciphertext using AES CBC mode.
Definition crypto.cpp:304
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.
Definition crypto.cpp:184
sha256_t hash(const std::string_view &plaintext)
Hashes the given plaintext using SHA-256.
Definition crypto.cpp:364
PERM
The permissions of a client.
Definition crypto.h:45
Definition crypto.h:86
Declarations for utility functions.