Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <array>
9
10// lib includes
11#include <openssl/evp.h>
12#include <openssl/rand.h>
13#include <openssl/sha.h>
14#include <openssl/x509.h>
15
16// local includes
17#include "utility.h"
18
19namespace crypto {
23 struct creds_t {
24 std::string x509;
25 std::string pkey;
26 };
27
33 void md_ctx_destroy(EVP_MD_CTX *ctx);
34
38 using sha256_t = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>;
39
43 using aes_t = std::vector<std::uint8_t>;
80
86 sha256_t hash(const std::string_view &plaintext);
87
95 aes_t gen_aes_key(const std::array<uint8_t, 16> &salt, const std::string_view &pin);
102 x509_t x509(const std::string_view &x);
109 pkey_t pkey(const std::string_view &k);
116 std::string pem(x509_t &x509);
123 std::string pem(pkey_t &pkey);
124
132 std::vector<uint8_t> sign256(const pkey_t &pkey, const std::string_view &data);
141 bool verify256(const x509_t &x509, const std::string_view &data, const std::string_view &signature);
142
150 creds_t gen_creds(const std::string_view &cn, std::uint32_t key_bits);
151
158 std::string_view signature(const x509_t &x);
159
166 std::string rand(std::size_t bytes);
174 std::string rand_alphabet(std::size_t bytes, const std::string_view &alphabet = std::string_view {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!%&()=-"});
175
180 public:
182
183
188 void add(x509_t &&cert);
189
193 void clear();
194
201 const char *verify(x509_t::element_type *cert);
202
203 private:
204 std::vector<std::pair<x509_t, x509_store_t>> _certs;
205 x509_store_ctx_t _cert_ctx {X509_STORE_CTX_new()};
206 };
207
208 namespace cipher {
209 constexpr std::size_t tag_size = 16;
210
217 constexpr std::size_t round_to_pkcs7_padded(std::size_t size) {
218 return ((size + 15) / 16) * 16;
219 }
220
233
237 class ecb_t: public cipher_t {
238 public:
239 ecb_t() = default;
243 ecb_t(ecb_t &&) noexcept = default;
249 ecb_t &operator=(ecb_t &&) noexcept = default;
250
257 explicit ecb_t(const aes_t &key_material, bool use_padding = true);
258
266 int encrypt(const std::string_view &plaintext, std::vector<std::uint8_t> &cipher);
274 int decrypt(const std::string_view &cipher, std::vector<std::uint8_t> &plaintext);
275 };
276
280 class gcm_t: public cipher_t {
281 public:
282 gcm_t() = default;
286 gcm_t(gcm_t &&) noexcept = default;
292 gcm_t &operator=(gcm_t &&) noexcept = default;
293
300 explicit gcm_t(const crypto::aes_t &key_material, bool use_padding = true);
301
310 int encrypt(const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv);
311
320 int encrypt(const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv);
321
330 int decrypt(const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
331 };
332
336 class cbc_t: public cipher_t {
337 public:
338 cbc_t() = default;
342 cbc_t(cbc_t &&) noexcept = default;
348 cbc_t &operator=(cbc_t &&) noexcept = default;
349
356 explicit cbc_t(const crypto::aes_t &key_material, bool use_padding = true);
357
366 int encrypt(const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv);
367 };
368 } // namespace cipher
369} // namespace crypto
cert_chain_t(cert_chain_t &&) noexcept=default
Defaulted move constructor for the generated type.
void add(x509_t &&cert)
Add a certificate to the verification chain.
Definition crypto.cpp:25
const char * verify(x509_t::element_type *cert)
Verify that a certificate exactly matches one stored trust anchor.
Definition crypto.cpp:52
void clear()
Remove all certificates from the verification chain.
Definition crypto.cpp:32
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:282
cbc_t(cbc_t &&) noexcept=default
Construct an AES-CBC cipher helper with key material and IV state.
AES-GCM encrypt/decrypt context pair used for GameStream messages.
Definition crypto.h:224
cipher_ctx_t decrypt_ctx
Decrypt ctx.
Definition crypto.h:226
bool padding
Enables block padding for the cipher.
Definition crypto.h:231
aes_t key
AES key used by the cipher context.
Definition crypto.h:229
cipher_ctx_t encrypt_ctx
Encrypt ctx.
Definition crypto.h:227
int decrypt(const std::string_view &cipher, std::vector< std::uint8_t > &plaintext)
Decrypt ciphertext into the supplied plaintext buffer.
Definition crypto.cpp:220
ecb_t(ecb_t &&) noexcept=default
Construct an AES-ECB cipher helper with key material.
int encrypt(const std::string_view &plaintext, std::vector< std::uint8_t > &cipher)
Encrypt plaintext into the supplied ciphertext buffer.
Definition crypto.cpp:248
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
gcm_t(gcm_t &&) noexcept=default
Construct an AES-GCM cipher helper with key material and IV state.
int decrypt(const std::string_view &cipher, std::vector< std::uint8_t > &plaintext, aes_t *iv)
Decrypt ciphertext into the supplied plaintext buffer.
Definition crypto.cpp:144
std::string_view signature(const x509_t &x)
Return the certificate signature bytes.
Definition crypto.cpp:404
aes_t gen_aes_key(const std::array< uint8_t, 16 > &salt, const std::string_view &pin)
Derive the AES key used by the pairing protocol.
Definition crypto.cpp:325
std::string rand(std::size_t bytes)
Generate cryptographically secure random bytes.
Definition crypto.cpp:419
void md_ctx_destroy(EVP_MD_CTX *ctx)
Destroy an OpenSSL message digest context.
Definition crypto.cpp:553
bool verify256(const x509_t &x509, const std::string_view &data, const std::string_view &signature)
Verify a SHA-256 signature with the certificate public key.
Definition crypto.cpp:546
x509_t x509(const std::string_view &x)
Parse PEM text into an X.509 certificate object.
Definition crypto.cpp:350
std::string rand_alphabet(std::size_t bytes, const std::string_view &alphabet)
Generate random text from the supplied alphabet.
Definition crypto.cpp:560
sha256_t hash(const std::string_view &plaintext)
Hashes the given plaintext using SHA-256.
Definition crypto.cpp:341
pkey_t pkey(const std::string_view &k)
Parse PEM text into an OpenSSL private key object.
Definition crypto.cpp:364
std::vector< uint8_t > sign256(const pkey_t &pkey, const std::string_view &data)
Sign data with SHA-256.
Definition crypto.cpp:510
std::string pem(x509_t &x509)
Serialize an OpenSSL object to PEM text.
Definition crypto.cpp:378
creds_t gen_creds(const std::string_view &cn, std::uint32_t key_bits)
Generate a self-signed certificate and private key for Sunshine pairing.
Definition crypto.cpp:463
std::vector< std::uint8_t > aes_t
Byte buffer containing AES key material.
Definition crypto.h:43
util::safe_ptr< EVP_PKEY, &EVP_PKEY_free > pkey_t
Owning pointer for an OpenSSL public or private key.
Definition crypto.h:71
util::safe_ptr< X509, &X509_free > x509_t
Owning pointer for an OpenSSL X.509 certificate.
Definition crypto.h:47
util::safe_ptr< X509_STORE, &X509_STORE_free > x509_store_t
Owning pointer for an OpenSSL certificate store.
Definition crypto.h:51
std::array< std::uint8_t, SHA256_DIGEST_LENGTH > sha256_t
Fixed-size SHA-256 digest byte array.
Definition crypto.h:38
constexpr std::size_t round_to_pkcs7_padded(std::size_t size)
Round a byte count up to the next PKCS#7 padding boundary.
Definition crypto.h:217
util::safe_ptr< BIGNUM, &BN_free > bignum_t
Owning pointer for an OpenSSL BIGNUM.
Definition crypto.h:79
util::safe_ptr< EVP_CIPHER_CTX, &EVP_CIPHER_CTX_free > cipher_ctx_t
Owning pointer for an OpenSSL cipher context.
Definition crypto.h:59
util::safe_ptr< BIO, &BIO_free_all > bio_t
Owning pointer for an OpenSSL BIO chain.
Definition crypto.h:67
util::safe_ptr< EVP_MD_CTX, &md_ctx_destroy > md_ctx_t
Owning pointer for an OpenSSL message digest context.
Definition crypto.h:63
constexpr std::size_t tag_size
Tag size.
Definition crypto.h:209
util::safe_ptr< X509_STORE_CTX, &X509_STORE_CTX_free > x509_store_ctx_t
Owning pointer for an OpenSSL certificate verification context.
Definition crypto.h:55
util::safe_ptr< EVP_PKEY_CTX, &EVP_PKEY_CTX_free > pkey_ctx_t
Owning pointer for an OpenSSL key-generation context.
Definition crypto.h:75
PEM-encoded certificate and private key pair.
Definition crypto.h:23
std::string x509
PEM-encoded X.509 certificate.
Definition crypto.h:24
std::string pkey
Private key PEM string or path.
Definition crypto.h:25
Declarations for utility functions.
uniq_ptr< T, Destroy< T *, void, function > > safe_ptr
Unique pointer using a compile-time C-style destroy function.
Definition utility.h:1590
#define KITTY_DECL_CONSTR(x)
Declare the standard move operations and out-of-line default constructor for a type.
Definition utility.h:111