crypto_mbedtls.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /**************************************************************************/
  2. /* crypto_mbedtls.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "crypto_mbedtls.h"
  31. #include "core/os/file_access.h"
  32. #include "core/engine.h"
  33. #include "core/io/certs_compressed.gen.h"
  34. #include "core/io/compression.h"
  35. #include "core/project_settings.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_settings.h"
  38. #endif
  39. #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
  40. #define PEM_END_CRT "-----END CERTIFICATE-----\n"
  41. #include <mbedtls/debug.h>
  42. #include <mbedtls/md.h>
  43. #include <mbedtls/pem.h>
  44. CryptoKey *CryptoKeyMbedTLS::create() {
  45. return memnew(CryptoKeyMbedTLS);
  46. }
  47. Error CryptoKeyMbedTLS::load(String p_path, bool p_public_only) {
  48. ERR_FAIL_COND_V_MSG(locks, ERR_ALREADY_IN_USE, "Key is in use");
  49. PoolByteArray out;
  50. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  51. ERR_FAIL_COND_V_MSG(!f, ERR_INVALID_PARAMETER, "Cannot open CryptoKeyMbedTLS file '" + p_path + "'.");
  52. uint64_t flen = f->get_len();
  53. out.resize(flen + 1);
  54. {
  55. PoolByteArray::Write w = out.write();
  56. f->get_buffer(w.ptr(), flen);
  57. w[flen] = 0; //end f string
  58. }
  59. memdelete(f);
  60. int ret = 0;
  61. if (p_public_only) {
  62. ret = mbedtls_pk_parse_public_key(&pkey, out.read().ptr(), out.size());
  63. } else {
  64. ret = mbedtls_pk_parse_key(&pkey, out.read().ptr(), out.size(), nullptr, 0);
  65. }
  66. // We MUST zeroize the memory for safety!
  67. mbedtls_platform_zeroize(out.write().ptr(), out.size());
  68. ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing key '" + itos(ret) + "'.");
  69. public_only = p_public_only;
  70. return OK;
  71. }
  72. Error CryptoKeyMbedTLS::save(String p_path, bool p_public_only) {
  73. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE);
  74. ERR_FAIL_COND_V_MSG(!f, ERR_INVALID_PARAMETER, "Cannot save CryptoKeyMbedTLS file '" + p_path + "'.");
  75. unsigned char w[16000];
  76. memset(w, 0, sizeof(w));
  77. int ret = 0;
  78. if (p_public_only) {
  79. ret = mbedtls_pk_write_pubkey_pem(&pkey, w, sizeof(w));
  80. } else {
  81. ret = mbedtls_pk_write_key_pem(&pkey, w, sizeof(w));
  82. }
  83. if (ret != 0) {
  84. memdelete(f);
  85. mbedtls_platform_zeroize(w, sizeof(w)); // Zeroize anything we might have written.
  86. ERR_FAIL_V_MSG(FAILED, "Error writing key '" + itos(ret) + "'.");
  87. }
  88. size_t len = strlen((char *)w);
  89. f->store_buffer(w, len);
  90. memdelete(f);
  91. mbedtls_platform_zeroize(w, sizeof(w)); // Zeroize temporary buffer.
  92. return OK;
  93. }
  94. Error CryptoKeyMbedTLS::load_from_string(String p_string_key, bool p_public_only) {
  95. int ret = 0;
  96. if (p_public_only) {
  97. ret = mbedtls_pk_parse_public_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size());
  98. } else {
  99. ret = mbedtls_pk_parse_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size(), nullptr, 0);
  100. }
  101. ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing key '" + itos(ret) + "'.");
  102. public_only = p_public_only;
  103. return OK;
  104. }
  105. String CryptoKeyMbedTLS::save_to_string(bool p_public_only) {
  106. unsigned char w[16000];
  107. memset(w, 0, sizeof(w));
  108. int ret = 0;
  109. if (p_public_only) {
  110. ret = mbedtls_pk_write_pubkey_pem(&pkey, w, sizeof(w));
  111. } else {
  112. ret = mbedtls_pk_write_key_pem(&pkey, w, sizeof(w));
  113. }
  114. if (ret != 0) {
  115. mbedtls_platform_zeroize(w, sizeof(w));
  116. ERR_FAIL_V_MSG("", "Error saving key '" + itos(ret) + "'.");
  117. }
  118. String s = String::utf8((char *)w);
  119. return s;
  120. }
  121. X509Certificate *X509CertificateMbedTLS::create() {
  122. return memnew(X509CertificateMbedTLS);
  123. }
  124. Error X509CertificateMbedTLS::load(String p_path) {
  125. ERR_FAIL_COND_V_MSG(locks, ERR_ALREADY_IN_USE, "Certificate is in use");
  126. PoolByteArray out;
  127. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  128. ERR_FAIL_COND_V_MSG(!f, ERR_INVALID_PARAMETER, "Cannot open X509CertificateMbedTLS file '" + p_path + "'.");
  129. uint64_t flen = f->get_len();
  130. out.resize(flen + 1);
  131. {
  132. PoolByteArray::Write w = out.write();
  133. f->get_buffer(w.ptr(), flen);
  134. w[flen] = 0; //end f string
  135. }
  136. memdelete(f);
  137. int ret = mbedtls_x509_crt_parse(&cert, out.read().ptr(), out.size());
  138. ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing some certificates: " + itos(ret));
  139. return OK;
  140. }
  141. Error X509CertificateMbedTLS::load_from_memory(const uint8_t *p_buffer, int p_len) {
  142. ERR_FAIL_COND_V_MSG(locks, ERR_ALREADY_IN_USE, "Certificate is in use");
  143. int ret = mbedtls_x509_crt_parse(&cert, p_buffer, p_len);
  144. ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing certificates: " + itos(ret));
  145. return OK;
  146. }
  147. Error X509CertificateMbedTLS::save(String p_path) {
  148. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE);
  149. ERR_FAIL_COND_V_MSG(!f, ERR_INVALID_PARAMETER, "Cannot save X509CertificateMbedTLS file '" + p_path + "'.");
  150. mbedtls_x509_crt *crt = &cert;
  151. while (crt) {
  152. unsigned char w[4096];
  153. size_t wrote = 0;
  154. int ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT, cert.raw.p, cert.raw.len, w, sizeof(w), &wrote);
  155. if (ret != 0 || wrote == 0) {
  156. memdelete(f);
  157. ERR_FAIL_V_MSG(FAILED, "Error writing certificate '" + itos(ret) + "'.");
  158. }
  159. f->store_buffer(w, wrote - 1); // don't write the string terminator
  160. crt = crt->next;
  161. }
  162. memdelete(f);
  163. return OK;
  164. }
  165. bool HMACContextMbedTLS::is_md_type_allowed(mbedtls_md_type_t p_md_type) {
  166. switch (p_md_type) {
  167. case MBEDTLS_MD_SHA1:
  168. case MBEDTLS_MD_SHA256:
  169. return true;
  170. default:
  171. return false;
  172. }
  173. }
  174. HMACContext *HMACContextMbedTLS::create() {
  175. return memnew(HMACContextMbedTLS);
  176. }
  177. Error HMACContextMbedTLS::start(HashingContext::HashType p_hash_type, PoolByteArray p_key) {
  178. ERR_FAIL_COND_V_MSG(ctx != nullptr, ERR_FILE_ALREADY_IN_USE, "HMACContext already started.");
  179. // HMAC keys can be any size.
  180. ERR_FAIL_COND_V_MSG(p_key.empty(), ERR_INVALID_PARAMETER, "Key must not be empty.");
  181. hash_type = p_hash_type;
  182. mbedtls_md_type_t ht = CryptoMbedTLS::md_type_from_hashtype(p_hash_type, hash_len);
  183. bool allowed = HMACContextMbedTLS::is_md_type_allowed(ht);
  184. ERR_FAIL_COND_V_MSG(!allowed, ERR_INVALID_PARAMETER, "Unsupported hash type.");
  185. ctx = memalloc(sizeof(mbedtls_md_context_t));
  186. mbedtls_md_init((mbedtls_md_context_t *)ctx);
  187. mbedtls_md_setup((mbedtls_md_context_t *)ctx, mbedtls_md_info_from_type((mbedtls_md_type_t)ht), 1);
  188. int ret = mbedtls_md_hmac_starts((mbedtls_md_context_t *)ctx, (const uint8_t *)p_key.read().ptr(), (size_t)p_key.size());
  189. return ret ? FAILED : OK;
  190. }
  191. Error HMACContextMbedTLS::update(PoolByteArray p_data) {
  192. ERR_FAIL_COND_V_MSG(ctx == nullptr, ERR_INVALID_DATA, "Start must be called before update.");
  193. ERR_FAIL_COND_V_MSG(p_data.empty(), ERR_INVALID_PARAMETER, "Src must not be empty.");
  194. int ret = mbedtls_md_hmac_update((mbedtls_md_context_t *)ctx, (const uint8_t *)p_data.read().ptr(), (size_t)p_data.size());
  195. return ret ? FAILED : OK;
  196. }
  197. PoolByteArray HMACContextMbedTLS::finish() {
  198. ERR_FAIL_COND_V_MSG(ctx == nullptr, PoolByteArray(), "Start must be called before finish.");
  199. ERR_FAIL_COND_V_MSG(hash_len == 0, PoolByteArray(), "Unsupported hash type.");
  200. PoolByteArray out;
  201. out.resize(hash_len);
  202. unsigned char *out_ptr = (unsigned char *)out.write().ptr();
  203. int ret = mbedtls_md_hmac_finish((mbedtls_md_context_t *)ctx, out_ptr);
  204. mbedtls_md_free((mbedtls_md_context_t *)ctx);
  205. memfree((mbedtls_md_context_t *)ctx);
  206. ctx = nullptr;
  207. hash_len = 0;
  208. ERR_FAIL_COND_V_MSG(ret, PoolByteArray(), "Error received while finishing HMAC");
  209. return out;
  210. }
  211. HMACContextMbedTLS::~HMACContextMbedTLS() {
  212. if (ctx != nullptr) {
  213. mbedtls_md_free((mbedtls_md_context_t *)ctx);
  214. memfree((mbedtls_md_context_t *)ctx);
  215. }
  216. }
  217. Crypto *CryptoMbedTLS::create() {
  218. return memnew(CryptoMbedTLS);
  219. }
  220. void CryptoMbedTLS::initialize_crypto() {
  221. #ifdef DEBUG_ENABLED
  222. mbedtls_debug_set_threshold(1);
  223. #endif
  224. Crypto::_create = create;
  225. Crypto::_load_default_certificates = load_default_certificates;
  226. X509CertificateMbedTLS::make_default();
  227. CryptoKeyMbedTLS::make_default();
  228. HMACContextMbedTLS::make_default();
  229. }
  230. void CryptoMbedTLS::finalize_crypto() {
  231. Crypto::_create = nullptr;
  232. Crypto::_load_default_certificates = nullptr;
  233. if (default_certs) {
  234. memdelete(default_certs);
  235. default_certs = nullptr;
  236. }
  237. X509CertificateMbedTLS::finalize();
  238. CryptoKeyMbedTLS::finalize();
  239. HMACContextMbedTLS::finalize();
  240. }
  241. CryptoMbedTLS::CryptoMbedTLS() {
  242. mbedtls_ctr_drbg_init(&ctr_drbg);
  243. mbedtls_entropy_init(&entropy);
  244. int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
  245. if (ret != 0) {
  246. ERR_PRINT(" failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret));
  247. }
  248. }
  249. CryptoMbedTLS::~CryptoMbedTLS() {
  250. mbedtls_ctr_drbg_free(&ctr_drbg);
  251. mbedtls_entropy_free(&entropy);
  252. }
  253. X509CertificateMbedTLS *CryptoMbedTLS::default_certs = nullptr;
  254. X509CertificateMbedTLS *CryptoMbedTLS::get_default_certificates() {
  255. return default_certs;
  256. }
  257. void CryptoMbedTLS::load_default_certificates(String p_path) {
  258. ERR_FAIL_COND(default_certs != nullptr);
  259. default_certs = memnew(X509CertificateMbedTLS);
  260. ERR_FAIL_COND(default_certs == nullptr);
  261. if (p_path != "") {
  262. // Use certs defined in project settings.
  263. default_certs->load(p_path);
  264. }
  265. #ifdef BUILTIN_CERTS_ENABLED
  266. else {
  267. // Use builtin certs only if user did not override it in project settings.
  268. PoolByteArray out;
  269. out.resize(_certs_uncompressed_size + 1);
  270. PoolByteArray::Write w = out.write();
  271. Compression::decompress(w.ptr(), _certs_uncompressed_size, _certs_compressed, _certs_compressed_size, Compression::MODE_DEFLATE);
  272. w[_certs_uncompressed_size] = 0; // Make sure it ends with string terminator
  273. #ifdef DEBUG_ENABLED
  274. print_verbose("Loaded builtin certs");
  275. #endif
  276. default_certs->load_from_memory(out.read().ptr(), out.size());
  277. }
  278. #endif
  279. }
  280. Ref<CryptoKey> CryptoMbedTLS::generate_rsa(int p_bytes) {
  281. Ref<CryptoKeyMbedTLS> out;
  282. out.instance();
  283. int ret = mbedtls_pk_setup(&(out->pkey), mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
  284. ERR_FAIL_COND_V(ret != 0, nullptr);
  285. ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(out->pkey), mbedtls_ctr_drbg_random, &ctr_drbg, p_bytes, 65537);
  286. out->public_only = false;
  287. ERR_FAIL_COND_V(ret != 0, nullptr);
  288. return out;
  289. }
  290. Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) {
  291. Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS>>(p_key);
  292. ERR_FAIL_COND_V_MSG(key.is_null(), nullptr, "Invalid private key argument.");
  293. mbedtls_x509write_cert crt;
  294. mbedtls_x509write_crt_init(&crt);
  295. mbedtls_x509write_crt_set_subject_key(&crt, &(key->pkey));
  296. mbedtls_x509write_crt_set_issuer_key(&crt, &(key->pkey));
  297. mbedtls_x509write_crt_set_subject_name(&crt, p_issuer_name.utf8().get_data());
  298. mbedtls_x509write_crt_set_issuer_name(&crt, p_issuer_name.utf8().get_data());
  299. mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
  300. mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
  301. mbedtls_mpi serial;
  302. mbedtls_mpi_init(&serial);
  303. uint8_t rand_serial[20];
  304. mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, 20);
  305. ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, 20), nullptr);
  306. mbedtls_x509write_crt_set_serial(&crt, &serial);
  307. mbedtls_x509write_crt_set_validity(&crt, p_not_before.utf8().get_data(), p_not_after.utf8().get_data());
  308. mbedtls_x509write_crt_set_basic_constraints(&crt, 1, -1);
  309. mbedtls_x509write_crt_set_basic_constraints(&crt, 1, 0);
  310. unsigned char buf[4096];
  311. memset(buf, 0, 4096);
  312. int ret = mbedtls_x509write_crt_pem(&crt, buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg);
  313. mbedtls_mpi_free(&serial);
  314. mbedtls_x509write_crt_free(&crt);
  315. ERR_FAIL_COND_V_MSG(ret != 0, nullptr, "Failed to generate certificate: " + itos(ret));
  316. buf[4095] = '\0'; // Make sure strlen can't fail.
  317. Ref<X509CertificateMbedTLS> out;
  318. out.instance();
  319. out->load_from_memory(buf, strlen((char *)buf) + 1); // Use strlen to find correct output size.
  320. return out;
  321. }
  322. PoolByteArray CryptoMbedTLS::generate_random_bytes(int p_bytes) {
  323. PoolByteArray out;
  324. out.resize(p_bytes);
  325. mbedtls_ctr_drbg_random(&ctr_drbg, out.write().ptr(), p_bytes);
  326. return out;
  327. }
  328. mbedtls_md_type_t CryptoMbedTLS::md_type_from_hashtype(HashingContext::HashType p_hash_type, int &r_size) {
  329. switch (p_hash_type) {
  330. case HashingContext::HASH_MD5:
  331. r_size = 16;
  332. return MBEDTLS_MD_MD5;
  333. case HashingContext::HASH_SHA1:
  334. r_size = 20;
  335. return MBEDTLS_MD_SHA1;
  336. case HashingContext::HASH_SHA256:
  337. r_size = 32;
  338. return MBEDTLS_MD_SHA256;
  339. default:
  340. r_size = 0;
  341. ERR_FAIL_V_MSG(MBEDTLS_MD_NONE, "Invalid hash type.");
  342. }
  343. }
  344. Vector<uint8_t> CryptoMbedTLS::sign(HashingContext::HashType p_hash_type, Vector<uint8_t> p_hash, Ref<CryptoKey> p_key) {
  345. int size;
  346. mbedtls_md_type_t type = CryptoMbedTLS::md_type_from_hashtype(p_hash_type, size);
  347. ERR_FAIL_COND_V_MSG(type == MBEDTLS_MD_NONE, Vector<uint8_t>(), "Invalid hash type.");
  348. ERR_FAIL_COND_V_MSG(p_hash.size() != size, Vector<uint8_t>(), "Invalid hash provided. Size must be " + itos(size));
  349. Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS>>(p_key);
  350. ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
  351. ERR_FAIL_COND_V_MSG(key->is_public_only(), Vector<uint8_t>(), "Invalid key provided. Cannot sign with public_only keys.");
  352. size_t sig_size = 0;
  353. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  354. Vector<uint8_t> out;
  355. int ret = mbedtls_pk_sign(&(key->pkey), type, p_hash.ptr(), size, buf, &sig_size, mbedtls_ctr_drbg_random, &ctr_drbg);
  356. ERR_FAIL_COND_V_MSG(ret, out, "Error while signing: " + itos(ret));
  357. out.resize(sig_size);
  358. memcpy(out.ptrw(), buf, sig_size);
  359. return out;
  360. }
  361. bool CryptoMbedTLS::verify(HashingContext::HashType p_hash_type, Vector<uint8_t> p_hash, Vector<uint8_t> p_signature, Ref<CryptoKey> p_key) {
  362. int size;
  363. mbedtls_md_type_t type = CryptoMbedTLS::md_type_from_hashtype(p_hash_type, size);
  364. ERR_FAIL_COND_V_MSG(type == MBEDTLS_MD_NONE, false, "Invalid hash type.");
  365. ERR_FAIL_COND_V_MSG(p_hash.size() != size, false, "Invalid hash provided. Size must be " + itos(size));
  366. Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS>>(p_key);
  367. ERR_FAIL_COND_V_MSG(!key.is_valid(), false, "Invalid key provided.");
  368. return mbedtls_pk_verify(&(key->pkey), type, p_hash.ptr(), size, p_signature.ptr(), p_signature.size()) == 0;
  369. }
  370. Vector<uint8_t> CryptoMbedTLS::encrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_plaintext) {
  371. Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS>>(p_key);
  372. ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
  373. uint8_t buf[1024];
  374. size_t size;
  375. Vector<uint8_t> out;
  376. int ret = mbedtls_pk_encrypt(&(key->pkey), p_plaintext.ptr(), p_plaintext.size(), buf, &size, sizeof(buf), mbedtls_ctr_drbg_random, &ctr_drbg);
  377. ERR_FAIL_COND_V_MSG(ret, out, "Error while encrypting: " + itos(ret));
  378. out.resize(size);
  379. memcpy(out.ptrw(), buf, size);
  380. return out;
  381. }
  382. Vector<uint8_t> CryptoMbedTLS::decrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_ciphertext) {
  383. Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS>>(p_key);
  384. ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
  385. ERR_FAIL_COND_V_MSG(key->is_public_only(), Vector<uint8_t>(), "Invalid key provided. Cannot decrypt using a public_only key.");
  386. uint8_t buf[2048];
  387. size_t size;
  388. Vector<uint8_t> out;
  389. int ret = mbedtls_pk_decrypt(&(key->pkey), p_ciphertext.ptr(), p_ciphertext.size(), buf, &size, sizeof(buf), mbedtls_ctr_drbg_random, &ctr_drbg);
  390. ERR_FAIL_COND_V_MSG(ret, out, "Error while decrypting: " + itos(ret));
  391. out.resize(size);
  392. memcpy(out.ptrw(), buf, size);
  393. return out;
  394. }