6#include <openssl/pem.h>
7#include <openssl/rsa.h>
13 using asn1_string_t = util::safe_ptr<ASN1_STRING, ASN1_STRING_free>;
15 cert_chain_t::cert_chain_t():
16 _certs {}, _cert_ctx { X509_STORE_CTX_new() } {
18 void cert_chain_t::add(p_named_cert_t& named_cert_p) {
19 x509_store_t x509_store { X509_STORE_new() };
21 X509_STORE_add_cert(x509_store.get(), x509(named_cert_p->cert).get());
22 _certs.emplace_back(std::make_pair(named_cert_p, std::move(x509_store)));
25 void cert_chain_t::clear() {
29 static int openssl_verify_cb(
int ok, X509_STORE_CTX *ctx) {
30 int err_code = X509_STORE_CTX_get_error(ctx);
36 case X509_V_ERR_CERT_NOT_YET_VALID:
37 case X509_V_ERR_CERT_HAS_EXPIRED:
57 for (
auto &[named_cert_p, x509_store] : _certs) {
58 auto fg = util::fail_guard([
this]() {
59 X509_STORE_CTX_cleanup(_cert_ctx.get());
62 X509_STORE_CTX_init(_cert_ctx.get(), x509_store.get(), cert,
nullptr);
63 X509_STORE_CTX_set_verify_cb(_cert_ctx.get(), openssl_verify_cb);
68 X509_STORE_CTX_set_flags(_cert_ctx.get(), X509_V_FLAG_PARTIAL_CHAIN);
70 auto err = X509_verify_cert(_cert_ctx.get());
73 named_cert_out = named_cert_p;
77 err_code = X509_STORE_CTX_get_error(_cert_ctx.get());
79 if (err_code != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT && err_code != X509_V_ERR_INVALID_CA) {
80 return X509_verify_cert_error_string(err_code);
84 return X509_verify_cert_error_string(err_code);
89 static int init_decrypt_gcm(cipher_ctx_t &ctx, aes_t *key, aes_t *iv,
bool padding) {
90 ctx.reset(EVP_CIPHER_CTX_new());
96 if (EVP_DecryptInit_ex(ctx.get(), EVP_aes_128_gcm(),
nullptr,
nullptr,
nullptr) != 1) {
100 if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN, iv->size(),
nullptr) != 1) {
104 if (EVP_DecryptInit_ex(ctx.get(),
nullptr,
nullptr, key->data(), iv->data()) != 1) {
107 EVP_CIPHER_CTX_set_padding(ctx.get(), padding);
112 static int init_encrypt_gcm(cipher_ctx_t &ctx, aes_t *key, aes_t *iv,
bool padding) {
113 ctx.reset(EVP_CIPHER_CTX_new());
116 if (EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_gcm(),
nullptr,
nullptr,
nullptr) != 1) {
120 if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN, iv->size(),
nullptr) != 1) {
124 if (EVP_EncryptInit_ex(ctx.get(),
nullptr,
nullptr, key->data(), iv->data()) != 1) {
127 EVP_CIPHER_CTX_set_padding(ctx.get(), padding);
132 static int init_encrypt_cbc(cipher_ctx_t &ctx, aes_t *key, aes_t *iv,
bool padding) {
133 ctx.reset(EVP_CIPHER_CTX_new());
136 if (EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(),
nullptr, key->data(), iv->data()) != 1) {
140 EVP_CIPHER_CTX_set_padding(ctx.get(), padding);
145 int gcm_t::decrypt(
const std::string_view &tagged_cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv) {
146 if (!decrypt_ctx && init_decrypt_gcm(decrypt_ctx, &key, iv, padding)) {
152 if (EVP_DecryptInit_ex(decrypt_ctx.get(),
nullptr,
nullptr,
nullptr, iv->data()) != 1) {
156 auto cipher = tagged_cipher.substr(tag_size);
157 auto tag = tagged_cipher.substr(0, tag_size);
159 plaintext.resize(round_to_pkcs7_padded(cipher.size()));
161 int update_outlen, final_outlen;
163 if (EVP_DecryptUpdate(decrypt_ctx.get(), plaintext.data(), &update_outlen, (
const std::uint8_t *) cipher.data(), cipher.size()) != 1) {
167 if (EVP_CIPHER_CTX_ctrl(decrypt_ctx.get(), EVP_CTRL_GCM_SET_TAG, tag.size(),
const_cast<char *
>(tag.data())) != 1) {
171 if (EVP_DecryptFinal_ex(decrypt_ctx.get(), plaintext.data() + update_outlen, &final_outlen) != 1) {
175 plaintext.resize(update_outlen + final_outlen);
184 int gcm_t::encrypt(
const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv) {
185 if (!encrypt_ctx && init_encrypt_gcm(encrypt_ctx, &key, iv, padding)) {
191 if (EVP_EncryptInit_ex(encrypt_ctx.get(),
nullptr,
nullptr,
nullptr, iv->data()) != 1) {
195 int update_outlen, final_outlen;
198 if (EVP_EncryptUpdate(encrypt_ctx.get(), ciphertext, &update_outlen, (
const std::uint8_t *) plaintext.data(), plaintext.size()) != 1) {
203 if (EVP_EncryptFinal_ex(encrypt_ctx.get(), ciphertext + update_outlen, &final_outlen) != 1) {
207 if (EVP_CIPHER_CTX_ctrl(encrypt_ctx.get(), EVP_CTRL_GCM_GET_TAG, tag_size, tag) != 1) {
211 return update_outlen + final_outlen;
214 int gcm_t::encrypt(
const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv) {
216 return encrypt(plaintext, tagged_cipher, tagged_cipher + tag_size, iv);
219 int ecb_t::decrypt(
const std::string_view &cipher, std::vector<std::uint8_t> &plaintext) {
220 auto fg = util::fail_guard([
this]() {
221 EVP_CIPHER_CTX_reset(decrypt_ctx.get());
225 if (EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_aes_128_ecb(),
nullptr, key.data(),
nullptr) != 1) {
229 EVP_CIPHER_CTX_set_padding(decrypt_ctx.get(), padding);
230 plaintext.resize(round_to_pkcs7_padded(cipher.size()));
232 int update_outlen, final_outlen;
234 if (EVP_DecryptUpdate(decrypt_ctx.get(), plaintext.data(), &update_outlen, (
const std::uint8_t *) cipher.data(), cipher.size()) != 1) {
238 if (EVP_DecryptFinal_ex(decrypt_ctx.get(), plaintext.data() + update_outlen, &final_outlen) != 1) {
242 plaintext.resize(update_outlen + final_outlen);
246 int ecb_t::encrypt(
const std::string_view &plaintext, std::vector<std::uint8_t> &cipher) {
247 auto fg = util::fail_guard([
this]() {
248 EVP_CIPHER_CTX_reset(encrypt_ctx.get());
252 if (EVP_EncryptInit_ex(encrypt_ctx.get(), EVP_aes_128_ecb(),
nullptr, key.data(),
nullptr) != 1) {
256 EVP_CIPHER_CTX_set_padding(encrypt_ctx.get(), padding);
257 cipher.resize(round_to_pkcs7_padded(plaintext.size()));
259 int update_outlen, final_outlen;
262 if (EVP_EncryptUpdate(encrypt_ctx.get(), cipher.data(), &update_outlen, (
const std::uint8_t *) plaintext.data(), plaintext.size()) != 1) {
266 if (EVP_EncryptFinal_ex(encrypt_ctx.get(), cipher.data() + update_outlen, &final_outlen) != 1) {
270 cipher.resize(update_outlen + final_outlen);
279 int cbc_t::encrypt(
const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv) {
280 if (!encrypt_ctx && init_encrypt_cbc(encrypt_ctx, &key, iv, padding)) {
286 if (EVP_EncryptInit_ex(encrypt_ctx.get(),
nullptr,
nullptr,
nullptr, iv->data()) != 1) {
290 int update_outlen, final_outlen;
293 if (EVP_EncryptUpdate(encrypt_ctx.get(), cipher, &update_outlen, (
const std::uint8_t *) plaintext.data(), plaintext.size()) != 1) {
297 if (EVP_EncryptFinal_ex(encrypt_ctx.get(), cipher + update_outlen, &final_outlen) != 1) {
301 return update_outlen + final_outlen;
304 int cbc_t::decrypt(
const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv) {
306 decrypt_ctx.reset(EVP_CIPHER_CTX_new());
307 if (EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_aes_128_cbc(),
nullptr, key.data(), iv->data()) != 1) {
311 EVP_CIPHER_CTX_set_padding(decrypt_ctx.get(), padding);
314 if (EVP_DecryptInit_ex(decrypt_ctx.get(),
nullptr,
nullptr,
nullptr, iv->data()) != 1) {
319 plaintext.resize(cipher.size() + 16);
321 int update_outlen, final_outlen;
322 if (EVP_DecryptUpdate(decrypt_ctx.get(), plaintext.data(), &update_outlen, (
const std::uint8_t *) cipher.data(), cipher.size()) != 1) {
326 if (EVP_DecryptFinal_ex(decrypt_ctx.get(), plaintext.data() + update_outlen, &final_outlen) != 1) {
330 plaintext.resize(update_outlen + final_outlen);
334 ecb_t::ecb_t(
const aes_t &key,
bool padding):
335 cipher_t {EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_new(), key, padding} {
338 cbc_t::cbc_t(
const aes_t &key,
bool padding):
339 cipher_t {nullptr, nullptr, key, padding} {
342 gcm_t::gcm_t(
const crypto::aes_t &key,
bool padding):
343 cipher_t {nullptr, nullptr, key, padding} {
348 aes_t gen_aes_key(
const std::array<uint8_t, 16> &salt,
const std::string_view &pin) {
351 std::string salt_pin;
352 salt_pin.reserve(salt.size() +
pin.size());
354 salt_pin.insert(std::end(salt_pin), std::begin(salt), std::end(salt));
355 salt_pin.insert(std::end(salt_pin), std::begin(pin), std::end(pin));
357 auto hsh =
hash(salt_pin);
359 std::copy(std::begin(hsh), std::begin(hsh) + key.size(), std::begin(key));
364 sha256_t
hash(
const std::string_view &plaintext) {
366 EVP_Digest(plaintext.data(), plaintext.size(), hsh.data(),
nullptr, EVP_sha256(),
nullptr);
370 x509_t x509(
const std::string_view &x) {
371 bio_t io {BIO_new(BIO_s_mem())};
373 BIO_write(io.get(), x.data(), x.size());
376 PEM_read_bio_X509(io.get(), &p,
nullptr,
nullptr);
381 pkey_t pkey(
const std::string_view &k) {
382 bio_t io {BIO_new(BIO_s_mem())};
384 BIO_write(io.get(), k.data(), k.size());
387 PEM_read_bio_PrivateKey(io.get(), &p,
nullptr,
nullptr);
392 std::string pem(x509_t &x509) {
393 bio_t bio {BIO_new(BIO_s_mem())};
395 PEM_write_bio_X509(bio.get(), x509.get());
397 BIO_get_mem_ptr(bio.get(), &mem_ptr);
399 return {mem_ptr->data, mem_ptr->length};
402 std::string pem(pkey_t &pkey) {
403 bio_t bio {BIO_new(BIO_s_mem())};
405 PEM_write_bio_PrivateKey(bio.get(), pkey.get(),
nullptr,
nullptr, 0,
nullptr,
nullptr);
407 BIO_get_mem_ptr(bio.get(), &mem_ptr);
409 return {mem_ptr->data, mem_ptr->length};
412 std::string_view signature(
const x509_t &x) {
415 const ASN1_BIT_STRING *asn1 =
nullptr;
416 X509_get0_signature(&asn1,
nullptr, x.get());
418#if OPENSSL_VERSION_NUMBER >= 0x40100000L
419 const auto length = ASN1_STRING_get_length(asn1);
421 const auto length = (std::size_t) ASN1_STRING_length(asn1);
423 return {(
const char *) ASN1_STRING_get0_data(asn1), length};
426 std::string rand(std::size_t bytes) {
430 RAND_bytes((uint8_t *) r.data(), r.size());
435 std::vector<uint8_t> sign(
const pkey_t &pkey,
const std::string_view &data,
const EVP_MD *md) {
436 md_ctx_t ctx {EVP_MD_CTX_create()};
438 if (EVP_DigestSignInit(ctx.get(),
nullptr, md,
nullptr, (EVP_PKEY *) pkey.get()) != 1) {
442 if (EVP_DigestSignUpdate(ctx.get(), data.data(), data.size()) != 1) {
447 if (EVP_DigestSignFinal(ctx.get(),
nullptr, &slen) != 1) {
451 std::vector<uint8_t> digest(slen);
452 if (EVP_DigestSignFinal(ctx.get(), digest.data(), &slen) != 1) {
459 creds_t gen_creds(
const std::string_view &cn, std::uint32_t key_bits) {
460 x509_t x509 {X509_new()};
461 pkey_ctx_t ctx {EVP_PKEY_CTX_new_id(EVP_PKEY_RSA,
nullptr)};
464 EVP_PKEY_keygen_init(ctx.get());
465 EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(), key_bits);
466 EVP_PKEY_keygen(ctx.get(), &pkey);
468 X509_set_version(x509.get(), 2);
471 bignum_t serial {BN_new()};
472 BN_rand(serial.get(), 159, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);
473 BN_set_negative(serial.get(), 0);
474 BN_to_ASN1_INTEGER(serial.get(), X509_get_serialNumber(x509.get()));
476 constexpr auto year = 60 * 60 * 24 * 365;
477#if OPENSSL_VERSION_NUMBER < 0x10100000L
478 X509_gmtime_adj(X509_get_notBefore(x509.get()), 0);
479 X509_gmtime_adj(X509_get_notAfter(x509.get()), 20 * year);
481 asn1_string_t not_before {ASN1_STRING_dup(X509_get0_notBefore(x509.get()))};
482 asn1_string_t not_after {ASN1_STRING_dup(X509_get0_notAfter(x509.get()))};
484 X509_gmtime_adj(not_before.get(), 0);
485 X509_gmtime_adj(not_after.get(), 20 * year);
487 X509_set1_notBefore(x509.get(), not_before.get());
488 X509_set1_notAfter(x509.get(), not_after.get());
491 X509_set_pubkey(x509.get(), pkey.get());
493 util::safe_ptr<X509_NAME, X509_NAME_free> name {X509_NAME_new()};
494 if (!name || X509_NAME_add_entry_by_txt(name.get(),
"CN", MBSTRING_ASC, (
const std::uint8_t *) cn.data(), cn.size(), -1, 0) != 1 ||
495 X509_set_subject_name(x509.get(), name.get()) != 1) {
499 X509_set_issuer_name(x509.get(), name.get());
500 X509_sign(x509.get(), pkey.get(), EVP_sha256());
502 return {pem(x509), pem(pkey)};
505 std::vector<uint8_t> sign256(
const pkey_t &pkey,
const std::string_view &data) {
506 return sign(pkey, data, EVP_sha256());
509 bool verify(
const x509_t &x509,
const std::string_view &data,
const std::string_view &signature,
const EVP_MD *md) {
510 auto pkey = X509_get0_pubkey(x509.get());
512 md_ctx_t ctx {EVP_MD_CTX_create()};
514 if (EVP_DigestVerifyInit(ctx.get(),
nullptr, md,
nullptr, pkey) != 1) {
518 if (EVP_DigestVerifyUpdate(ctx.get(), data.data(), data.size()) != 1) {
522 if (EVP_DigestVerifyFinal(ctx.get(), (
const uint8_t *) signature.data(), signature.size()) != 1) {
529 bool verify256(
const x509_t &x509,
const std::string_view &data,
const std::string_view &signature) {
530 return verify(x509, data, signature, EVP_sha256());
533 void md_ctx_destroy(EVP_MD_CTX *ctx) {
534 EVP_MD_CTX_destroy(ctx);
537 std::string rand_alphabet(std::size_t bytes,
const std::string_view &alphabet) {
538 auto value = rand(bytes);
540 for (std::size_t i = 0; i != value.size(); ++i) {
541 value[i] = alphabet[value[i] % alphabet.length()];
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.
Declarations for cryptography functions.
bool pin(std::string pin, std::string name)
Compare the user supplied pin to the Moonlight pin.