stream_peer_openssl.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*************************************************************************/
  2. /* stream_peer_openssl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 "stream_peer_openssl.h"
  31. // Compatibility with OpenSSL 1.1.0.
  32. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  33. #define BIO_set_num(b, n)
  34. #else
  35. #define BIO_set_num(b, n) ((b)->num = (n))
  36. #define BIO_set_init(b, i) ((b)->init = (i))
  37. #define BIO_set_data(b, p) ((b)->ptr = (p))
  38. #define BIO_get_data(b) ((b)->ptr)
  39. #endif
  40. //hostname matching code from curl
  41. bool StreamPeerOpenSSL::_match_host_name(const char *name, const char *hostname) {
  42. return Tool_Curl_cert_hostcheck(name, hostname) == CURL_HOST_MATCH;
  43. }
  44. Error StreamPeerOpenSSL::_match_common_name(const char *hostname, const X509 *server_cert) {
  45. // Find the position of the CN field in the Subject field of the certificate
  46. int common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name((X509 *)server_cert), NID_commonName, -1);
  47. ERR_FAIL_COND_V(common_name_loc < 0, ERR_INVALID_PARAMETER);
  48. // Extract the CN field
  49. X509_NAME_ENTRY *common_name_entry = X509_NAME_get_entry(X509_get_subject_name((X509 *)server_cert), common_name_loc);
  50. ERR_FAIL_COND_V(common_name_entry == NULL, ERR_INVALID_PARAMETER);
  51. // Convert the CN field to a C string
  52. ASN1_STRING *common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
  53. ERR_FAIL_COND_V(common_name_asn1 == NULL, ERR_INVALID_PARAMETER);
  54. char *common_name_str = (char *)ASN1_STRING_data(common_name_asn1);
  55. // Make sure there isn't an embedded NUL character in the CN
  56. bool malformed_certificate = (size_t)ASN1_STRING_length(common_name_asn1) != strlen(common_name_str);
  57. ERR_FAIL_COND_V(malformed_certificate, ERR_INVALID_PARAMETER);
  58. // Compare expected hostname with the CN
  59. return _match_host_name(common_name_str, hostname) ? OK : FAILED;
  60. }
  61. /**
  62. * Tries to find a match for hostname in the certificate's Subject Alternative Name extension.
  63. *
  64. */
  65. Error StreamPeerOpenSSL::_match_subject_alternative_name(const char *hostname, const X509 *server_cert) {
  66. Error result = FAILED;
  67. int i;
  68. int san_names_nb = -1;
  69. STACK_OF(GENERAL_NAME) *san_names = NULL;
  70. // Try to extract the names within the SAN extension from the certificate
  71. san_names = (STACK_OF(GENERAL_NAME) *)X509_get_ext_d2i((X509 *)server_cert, NID_subject_alt_name, NULL, NULL);
  72. if (san_names == NULL) {
  73. return ERR_FILE_NOT_FOUND;
  74. }
  75. san_names_nb = sk_GENERAL_NAME_num(san_names);
  76. // Check each name within the extension
  77. for (i = 0; i < san_names_nb; i++) {
  78. const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i);
  79. if (current_name->type == GEN_DNS) {
  80. // Current name is a DNS name, let's check it
  81. char *dns_name = (char *)ASN1_STRING_data(current_name->d.dNSName);
  82. // Make sure there isn't an embedded NUL character in the DNS name
  83. if ((size_t)ASN1_STRING_length(current_name->d.dNSName) != strlen(dns_name)) {
  84. result = ERR_INVALID_PARAMETER;
  85. break;
  86. } else { // Compare expected hostname with the DNS name
  87. if (_match_host_name(dns_name, hostname)) {
  88. result = OK;
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
  95. return result;
  96. }
  97. /* See http://archives.seul.org/libevent/users/Jan-2013/msg00039.html */
  98. int StreamPeerOpenSSL::_cert_verify_callback(X509_STORE_CTX *x509_ctx, void *arg) {
  99. /* This is the function that OpenSSL would call if we hadn't called
  100. * SSL_CTX_set_cert_verify_callback(). Therefore, we are "wrapping"
  101. * the default functionality, rather than replacing it. */
  102. bool base_cert_valid = X509_verify_cert(x509_ctx);
  103. if (!base_cert_valid) {
  104. print_line("Cause: " + String(X509_verify_cert_error_string(X509_STORE_CTX_get_error(x509_ctx))));
  105. ERR_print_errors_fp(stdout);
  106. }
  107. X509 *server_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
  108. ERR_FAIL_COND_V(!server_cert, 0);
  109. char cert_str[256];
  110. X509_NAME_oneline(X509_get_subject_name(server_cert),
  111. cert_str, sizeof(cert_str));
  112. print_line("CERT STR: " + String(cert_str));
  113. print_line("VALID: " + itos(base_cert_valid));
  114. if (!base_cert_valid)
  115. return 0;
  116. StreamPeerOpenSSL *ssl = (StreamPeerOpenSSL *)arg;
  117. if (ssl->validate_hostname) {
  118. Error err = _match_subject_alternative_name(ssl->hostname.utf8().get_data(), server_cert);
  119. if (err == ERR_FILE_NOT_FOUND) {
  120. err = _match_common_name(ssl->hostname.utf8().get_data(), server_cert);
  121. }
  122. if (err != OK) {
  123. ssl->status = STATUS_ERROR_HOSTNAME_MISMATCH;
  124. return 0;
  125. }
  126. }
  127. return 1;
  128. }
  129. int StreamPeerOpenSSL::_bio_create(BIO *b) {
  130. BIO_set_init(b, 1);
  131. BIO_set_num(b, 0);
  132. BIO_set_data(b, NULL);
  133. BIO_clear_flags(b, ~0);
  134. return 1;
  135. }
  136. int StreamPeerOpenSSL::_bio_destroy(BIO *b) {
  137. if (b == NULL)
  138. return 0;
  139. BIO_set_data(b, NULL); /* sb_tls_remove() will free it */
  140. BIO_set_init(b, 0);
  141. BIO_clear_flags(b, ~0);
  142. return 1;
  143. }
  144. int StreamPeerOpenSSL::_bio_read(BIO *b, char *buf, int len) {
  145. if (buf == NULL || len <= 0) return 0;
  146. StreamPeerOpenSSL *sp = (StreamPeerOpenSSL *)BIO_get_data(b);
  147. ERR_FAIL_COND_V(sp == NULL, 0);
  148. BIO_clear_retry_flags(b);
  149. if (sp->use_blocking) {
  150. Error err = sp->base->get_data((uint8_t *)buf, len);
  151. if (err != OK) {
  152. return -1;
  153. }
  154. return len;
  155. } else {
  156. int got;
  157. Error err = sp->base->get_partial_data((uint8_t *)buf, len, got);
  158. if (err != OK) {
  159. return -1;
  160. }
  161. if (got == 0) {
  162. BIO_set_retry_read(b);
  163. }
  164. return got;
  165. }
  166. //unreachable
  167. return 0;
  168. }
  169. int StreamPeerOpenSSL::_bio_write(BIO *b, const char *buf, int len) {
  170. if (buf == NULL || len <= 0) return 0;
  171. StreamPeerOpenSSL *sp = (StreamPeerOpenSSL *)BIO_get_data(b);
  172. ERR_FAIL_COND_V(sp == NULL, 0);
  173. BIO_clear_retry_flags(b);
  174. if (sp->use_blocking) {
  175. Error err = sp->base->put_data((const uint8_t *)buf, len);
  176. if (err != OK) {
  177. return -1;
  178. }
  179. return len;
  180. } else {
  181. int sent;
  182. Error err = sp->base->put_partial_data((const uint8_t *)buf, len, sent);
  183. if (err != OK) {
  184. return -1;
  185. }
  186. if (sent == 0) {
  187. BIO_set_retry_write(b);
  188. }
  189. return sent;
  190. }
  191. //unreachable
  192. return 0;
  193. }
  194. long StreamPeerOpenSSL::_bio_ctrl(BIO *b, int cmd, long num, void *ptr) {
  195. if (cmd == BIO_CTRL_FLUSH) {
  196. /* The OpenSSL library needs this */
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. int StreamPeerOpenSSL::_bio_gets(BIO *b, char *buf, int len) {
  202. return -1;
  203. }
  204. int StreamPeerOpenSSL::_bio_puts(BIO *b, const char *str) {
  205. return _bio_write(b, str, strlen(str));
  206. }
  207. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  208. BIO_METHOD *StreamPeerOpenSSL::_bio_method = NULL;
  209. BIO_METHOD *StreamPeerOpenSSL::_get_bio_method() {
  210. if (_bio_method) // already initialized.
  211. return _bio_method;
  212. /* it's a source/sink BIO */
  213. _bio_method = BIO_meth_new(100 | 0x400, "streampeer glue");
  214. BIO_meth_set_write(_bio_method, _bio_write);
  215. BIO_meth_set_read(_bio_method, _bio_read);
  216. BIO_meth_set_puts(_bio_method, _bio_puts);
  217. BIO_meth_set_gets(_bio_method, _bio_gets);
  218. BIO_meth_set_ctrl(_bio_method, _bio_ctrl);
  219. BIO_meth_set_create(_bio_method, _bio_create);
  220. BIO_meth_set_destroy(_bio_method, _bio_destroy);
  221. return _bio_method;
  222. }
  223. #else
  224. BIO_METHOD StreamPeerOpenSSL::_bio_method = {
  225. /* it's a source/sink BIO */
  226. (100 | 0x400),
  227. "streampeer glue",
  228. _bio_write,
  229. _bio_read,
  230. _bio_puts,
  231. _bio_gets,
  232. _bio_ctrl,
  233. _bio_create,
  234. _bio_destroy
  235. };
  236. BIO_METHOD *StreamPeerOpenSSL::_get_bio_method() {
  237. return &_bio_method;
  238. }
  239. #endif
  240. Error StreamPeerOpenSSL::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname) {
  241. if (connected)
  242. disconnect_from_stream();
  243. hostname = p_for_hostname;
  244. status = STATUS_DISCONNECTED;
  245. // Set up a SSL_CTX object, which will tell our BIO object how to do its work
  246. ctx = SSL_CTX_new(SSLv23_client_method());
  247. base = p_base;
  248. validate_certs = p_validate_certs;
  249. validate_hostname = p_for_hostname != "";
  250. if (p_validate_certs) {
  251. if (certs.size()) {
  252. //yay for undocumented OpenSSL functions
  253. X509_STORE *store = SSL_CTX_get_cert_store(ctx);
  254. for (int i = 0; i < certs.size(); i++) {
  255. X509_STORE_add_cert(store, certs[i]);
  256. }
  257. }
  258. //used for testing
  259. //int res = SSL_CTX_load_verify_locations(ctx,"/etc/ssl/certs/ca-certificates.crt",NULL);
  260. //print_line("verify locations res: "+itos(res));
  261. /* Ask OpenSSL to verify the server certificate. Note that this
  262. * does NOT include verifying that the hostname is correct.
  263. * So, by itself, this means anyone with any legitimate
  264. * CA-issued certificate for any website, can impersonate any
  265. * other website in the world. This is not good. See "The
  266. * Most Dangerous Code in the World" article at
  267. * https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html
  268. */
  269. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  270. /* This is how we solve the problem mentioned in the previous
  271. * comment. We "wrap" OpenSSL's validation routine in our
  272. * own routine, which also validates the hostname by calling
  273. * the code provided by iSECPartners. Note that even though
  274. * the "Everything You've Always Wanted to Know About
  275. * Certificate Validation With OpenSSL (But Were Afraid to
  276. * Ask)" paper from iSECPartners says very explicitly not to
  277. * call SSL_CTX_set_cert_verify_callback (at the bottom of
  278. * page 2), what we're doing here is safe because our
  279. * cert_verify_callback() calls X509_verify_cert(), which is
  280. * OpenSSL's built-in routine which would have been called if
  281. * we hadn't set the callback. Therefore, we're just
  282. * "wrapping" OpenSSL's routine, not replacing it. */
  283. SSL_CTX_set_cert_verify_callback(ctx, _cert_verify_callback, this);
  284. //Let the verify_callback catch the verify_depth error so that we get an appropriate error in the logfile. (??)
  285. SSL_CTX_set_verify_depth(ctx, max_cert_chain_depth + 1);
  286. }
  287. ssl = SSL_new(ctx);
  288. bio = BIO_new(_get_bio_method());
  289. BIO_set_data(bio, this);
  290. SSL_set_bio(ssl, bio, bio);
  291. if (p_for_hostname != String()) {
  292. SSL_set_tlsext_host_name(ssl, p_for_hostname.utf8().get_data());
  293. }
  294. use_blocking = true; // let handshake use blocking
  295. // Set the SSL to automatically retry on failure.
  296. SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  297. // Same as before, try to connect.
  298. int result = SSL_connect(ssl);
  299. print_line("CONNECTION RESULT: " + itos(result));
  300. if (result < 1) {
  301. ERR_print_errors_fp(stdout);
  302. _print_error(result);
  303. }
  304. X509 *peer = SSL_get_peer_certificate(ssl);
  305. if (peer) {
  306. bool cert_ok = SSL_get_verify_result(ssl) == X509_V_OK;
  307. print_line("cert_ok: " + itos(cert_ok));
  308. } else if (validate_certs) {
  309. status = STATUS_ERROR_NO_CERTIFICATE;
  310. }
  311. connected = true;
  312. status = STATUS_CONNECTED;
  313. return OK;
  314. }
  315. Error StreamPeerOpenSSL::accept_stream(Ref<StreamPeer> p_base) {
  316. return ERR_UNAVAILABLE;
  317. }
  318. void StreamPeerOpenSSL::_print_error(int err) {
  319. err = SSL_get_error(ssl, err);
  320. switch (err) {
  321. case SSL_ERROR_NONE: ERR_PRINT("NO ERROR: The TLS/SSL I/O operation completed"); break;
  322. case SSL_ERROR_ZERO_RETURN: ERR_PRINT("The TLS/SSL connection has been closed.");
  323. case SSL_ERROR_WANT_READ:
  324. case SSL_ERROR_WANT_WRITE:
  325. ERR_PRINT("The operation did not complete.");
  326. break;
  327. case SSL_ERROR_WANT_CONNECT:
  328. case SSL_ERROR_WANT_ACCEPT:
  329. ERR_PRINT("The connect/accept operation did not complete");
  330. break;
  331. case SSL_ERROR_WANT_X509_LOOKUP:
  332. ERR_PRINT("The operation did not complete because an application callback set by SSL_CTX_set_client_cert_cb() has asked to be called again.");
  333. break;
  334. case SSL_ERROR_SYSCALL:
  335. ERR_PRINT("Some I/O error occurred. The OpenSSL error queue may contain more information on the error.");
  336. break;
  337. case SSL_ERROR_SSL:
  338. ERR_PRINT("A failure in the SSL library occurred, usually a protocol error.");
  339. break;
  340. }
  341. }
  342. Error StreamPeerOpenSSL::put_data(const uint8_t *p_data, int p_bytes) {
  343. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  344. while (p_bytes > 0) {
  345. int ret = SSL_write(ssl, p_data, p_bytes);
  346. if (ret <= 0) {
  347. _print_error(ret);
  348. disconnect_from_stream();
  349. return ERR_CONNECTION_ERROR;
  350. }
  351. p_data += ret;
  352. p_bytes -= ret;
  353. }
  354. return OK;
  355. }
  356. Error StreamPeerOpenSSL::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  357. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  358. if (p_bytes == 0)
  359. return OK;
  360. Error err = put_data(p_data, p_bytes);
  361. if (err != OK)
  362. return err;
  363. r_sent = p_bytes;
  364. return OK;
  365. }
  366. Error StreamPeerOpenSSL::get_data(uint8_t *p_buffer, int p_bytes) {
  367. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  368. while (p_bytes > 0) {
  369. int ret = SSL_read(ssl, p_buffer, p_bytes);
  370. if (ret <= 0) {
  371. _print_error(ret);
  372. disconnect_from_stream();
  373. return ERR_CONNECTION_ERROR;
  374. }
  375. p_buffer += ret;
  376. p_bytes -= ret;
  377. }
  378. return OK;
  379. }
  380. Error StreamPeerOpenSSL::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  381. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  382. if (p_bytes == 0) {
  383. r_received = 0;
  384. return OK;
  385. }
  386. Error err = get_data(p_buffer, p_bytes);
  387. if (err != OK)
  388. return err;
  389. r_received = p_bytes;
  390. return OK;
  391. }
  392. int StreamPeerOpenSSL::get_available_bytes() const {
  393. ERR_FAIL_COND_V(!connected, 0);
  394. return SSL_pending(ssl);
  395. }
  396. StreamPeerOpenSSL::StreamPeerOpenSSL() {
  397. ctx = NULL;
  398. ssl = NULL;
  399. bio = NULL;
  400. connected = false;
  401. use_blocking = true; //might be improved int the future, but for now it always blocks
  402. max_cert_chain_depth = 9;
  403. flags = 0;
  404. }
  405. void StreamPeerOpenSSL::disconnect_from_stream() {
  406. if (!connected)
  407. return;
  408. SSL_shutdown(ssl);
  409. SSL_free(ssl);
  410. SSL_CTX_free(ctx);
  411. base = Ref<StreamPeer>();
  412. connected = false;
  413. validate_certs = false;
  414. validate_hostname = false;
  415. status = STATUS_DISCONNECTED;
  416. }
  417. StreamPeerOpenSSL::Status StreamPeerOpenSSL::get_status() const {
  418. return status;
  419. }
  420. StreamPeerOpenSSL::~StreamPeerOpenSSL() {
  421. disconnect_from_stream();
  422. }
  423. StreamPeerSSL *StreamPeerOpenSSL::_create_func() {
  424. return memnew(StreamPeerOpenSSL);
  425. }
  426. Vector<X509 *> StreamPeerOpenSSL::certs;
  427. void StreamPeerOpenSSL::_load_certs(const PoolByteArray &p_array) {
  428. PoolByteArray::Read r = p_array.read();
  429. BIO *mem = BIO_new(BIO_s_mem());
  430. BIO_puts(mem, (const char *)r.ptr());
  431. while (true) {
  432. X509 *cert = PEM_read_bio_X509(mem, NULL, 0, NULL);
  433. if (!cert)
  434. break;
  435. certs.push_back(cert);
  436. }
  437. BIO_free(mem);
  438. }
  439. void StreamPeerOpenSSL::initialize_ssl() {
  440. available = true;
  441. load_certs_func = _load_certs;
  442. _create = _create_func;
  443. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  444. CRYPTO_malloc_init(); // Initialize malloc, free, etc for OpenSSL's use
  445. #endif
  446. SSL_library_init(); // Initialize OpenSSL's SSL libraries
  447. SSL_load_error_strings(); // Load SSL error strings
  448. ERR_load_BIO_strings(); // Load BIO error strings
  449. OpenSSL_add_all_algorithms(); // Load all available encryption algorithms
  450. String certs_path = GLOBAL_DEF("network/ssl/certificates", "");
  451. ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/certificates", PropertyInfo(Variant::STRING, "network/ssl/certificates", PROPERTY_HINT_FILE, "*.crt"));
  452. if (certs_path != "") {
  453. FileAccess *f = FileAccess::open(certs_path, FileAccess::READ);
  454. if (f) {
  455. PoolByteArray arr;
  456. int flen = f->get_len();
  457. arr.resize(flen + 1);
  458. {
  459. PoolByteArray::Write w = arr.write();
  460. f->get_buffer(w.ptr(), flen);
  461. w[flen] = 0; //end f string
  462. }
  463. memdelete(f);
  464. _load_certs(arr);
  465. print_line("Loaded certs from '" + certs_path + "': " + itos(certs.size()));
  466. }
  467. }
  468. String config_path = GLOBAL_DEF("network/ssl/config", "");
  469. ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/config", PropertyInfo(Variant::STRING, "network/ssl/config", PROPERTY_HINT_FILE, "*.cnf"));
  470. if (config_path != "") {
  471. Vector<uint8_t> data = FileAccess::get_file_as_array(config_path);
  472. if (data.size()) {
  473. data.push_back(0);
  474. BIO *mem = BIO_new(BIO_s_mem());
  475. BIO_puts(mem, (const char *)data.ptr());
  476. while (true) {
  477. X509 *cert = PEM_read_bio_X509(mem, NULL, 0, NULL);
  478. if (!cert)
  479. break;
  480. certs.push_back(cert);
  481. }
  482. BIO_free(mem);
  483. }
  484. print_line("Loaded certs from '" + certs_path + "': " + itos(certs.size()));
  485. }
  486. }
  487. void StreamPeerOpenSSL::finalize_ssl() {
  488. for (int i = 0; i < certs.size(); i++) {
  489. X509_free(certs[i]);
  490. }
  491. certs.clear();
  492. }