stream_peer_openssl.cpp 18 KB

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