|
@@ -28,19 +28,11 @@
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
/*************************************************************************/
|
|
|
|
|
|
-#include "stream_peer_mbed_tls.h"
|
|
|
+#include "stream_peer_mbedtls.h"
|
|
|
|
|
|
#include "core/io/stream_peer_tcp.h"
|
|
|
#include "core/os/file_access.h"
|
|
|
|
|
|
-static void my_debug(void *ctx, int level,
|
|
|
- const char *file, int line,
|
|
|
- const char *str) {
|
|
|
-
|
|
|
- printf("%s:%04d: %s", file, line, str);
|
|
|
- fflush(stdout);
|
|
|
-}
|
|
|
-
|
|
|
void _print_error(int ret) {
|
|
|
printf("mbedtls error: returned -0x%x\n\n", -ret);
|
|
|
fflush(stdout);
|
|
@@ -86,18 +78,14 @@ int StreamPeerMbedTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
|
|
|
|
|
|
void StreamPeerMbedTLS::_cleanup() {
|
|
|
|
|
|
- mbedtls_ssl_free(&ssl);
|
|
|
- mbedtls_ssl_config_free(&conf);
|
|
|
- mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
|
- mbedtls_entropy_free(&entropy);
|
|
|
-
|
|
|
+ ssl_ctx->clear();
|
|
|
base = Ref<StreamPeer>();
|
|
|
status = STATUS_DISCONNECTED;
|
|
|
}
|
|
|
|
|
|
Error StreamPeerMbedTLS::_do_handshake() {
|
|
|
int ret = 0;
|
|
|
- while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
|
|
|
+ while ((ret = mbedtls_ssl_handshake(ssl_ctx->get_context())) != 0) {
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
// An error occurred.
|
|
|
ERR_PRINTS("TLS handshake error: " + itos(ret));
|
|
@@ -118,39 +106,17 @@ Error StreamPeerMbedTLS::_do_handshake() {
|
|
|
return OK;
|
|
|
}
|
|
|
|
|
|
-Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname) {
|
|
|
-
|
|
|
- ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
|
|
|
+Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname, Ref<X509Certificate> p_ca_certs) {
|
|
|
|
|
|
base = p_base;
|
|
|
int ret = 0;
|
|
|
int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
|
|
|
|
|
|
- mbedtls_ssl_init(&ssl);
|
|
|
- mbedtls_ssl_config_init(&conf);
|
|
|
- mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
- mbedtls_entropy_init(&entropy);
|
|
|
+ Error err = ssl_ctx->init_client(MBEDTLS_SSL_TRANSPORT_STREAM, authmode, p_ca_certs);
|
|
|
+ ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
|
|
- ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
|
|
|
- if (ret != 0) {
|
|
|
- ERR_PRINTS(" failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret));
|
|
|
- _cleanup();
|
|
|
- return FAILED;
|
|
|
- }
|
|
|
-
|
|
|
- mbedtls_ssl_config_defaults(&conf,
|
|
|
- MBEDTLS_SSL_IS_CLIENT,
|
|
|
- MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
- MBEDTLS_SSL_PRESET_DEFAULT);
|
|
|
-
|
|
|
- mbedtls_ssl_conf_authmode(&conf, authmode);
|
|
|
- mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
|
|
|
- mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
|
|
- mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
|
|
|
- mbedtls_ssl_setup(&ssl, &conf);
|
|
|
- mbedtls_ssl_set_hostname(&ssl, p_for_hostname.utf8().get_data());
|
|
|
-
|
|
|
- mbedtls_ssl_set_bio(&ssl, this, bio_send, bio_recv, NULL);
|
|
|
+ mbedtls_ssl_set_hostname(ssl_ctx->get_context(), p_for_hostname.utf8().get_data());
|
|
|
+ mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL);
|
|
|
|
|
|
status = STATUS_HANDSHAKING;
|
|
|
|
|
@@ -162,11 +128,24 @@ Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_valida
|
|
|
return OK;
|
|
|
}
|
|
|
|
|
|
-Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base) {
|
|
|
+Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain) {
|
|
|
+
|
|
|
+ Error err = ssl_ctx->init_server(MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_VERIFY_NONE, p_key, p_cert);
|
|
|
+ ERR_FAIL_COND_V(err != OK, err);
|
|
|
+
|
|
|
+ base = p_base;
|
|
|
+
|
|
|
+ mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL);
|
|
|
|
|
|
+ status = STATUS_HANDSHAKING;
|
|
|
+
|
|
|
+ if ((err = _do_handshake()) != OK) {
|
|
|
+ return FAILED;
|
|
|
+ }
|
|
|
+
|
|
|
+ status = STATUS_CONNECTED;
|
|
|
return OK;
|
|
|
}
|
|
|
-
|
|
|
Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) {
|
|
|
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
|
|
@@ -197,7 +176,7 @@ Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, in
|
|
|
if (p_bytes == 0)
|
|
|
return OK;
|
|
|
|
|
|
- int ret = mbedtls_ssl_write(&ssl, p_data, p_bytes);
|
|
|
+ int ret = mbedtls_ssl_write(ssl_ctx->get_context(), p_data, p_bytes);
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
// Non blocking IO
|
|
|
ret = 0;
|
|
@@ -243,7 +222,7 @@ Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r
|
|
|
|
|
|
r_received = 0;
|
|
|
|
|
|
- int ret = mbedtls_ssl_read(&ssl, p_buffer, p_bytes);
|
|
|
+ int ret = mbedtls_ssl_read(ssl_ctx->get_context(), p_buffer, p_bytes);
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
ret = 0; // non blocking io
|
|
|
} else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
|
@@ -273,7 +252,7 @@ void StreamPeerMbedTLS::poll() {
|
|
|
// We could pass NULL as second parameter, but some behaviour sanitizers doesn't seem to like that.
|
|
|
// Passing a 1 byte buffer to workaround it.
|
|
|
uint8_t byte;
|
|
|
- int ret = mbedtls_ssl_read(&ssl, &byte, 0);
|
|
|
+ int ret = mbedtls_ssl_read(ssl_ctx->get_context(), &byte, 0);
|
|
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
// Nothing to read/write (non blocking IO)
|
|
@@ -298,10 +277,11 @@ int StreamPeerMbedTLS::get_available_bytes() const {
|
|
|
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0);
|
|
|
|
|
|
- return mbedtls_ssl_get_bytes_avail(&ssl);
|
|
|
+ return mbedtls_ssl_get_bytes_avail(&(ssl_ctx->ssl));
|
|
|
}
|
|
|
StreamPeerMbedTLS::StreamPeerMbedTLS() {
|
|
|
|
|
|
+ ssl_ctx.instance();
|
|
|
status = STATUS_DISCONNECTED;
|
|
|
}
|
|
|
|
|
@@ -317,7 +297,7 @@ void StreamPeerMbedTLS::disconnect_from_stream() {
|
|
|
Ref<StreamPeerTCP> tcp = base;
|
|
|
if (tcp.is_valid() && tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
|
|
|
// We are still connected on the socket, try to send close notify.
|
|
|
- mbedtls_ssl_close_notify(&ssl);
|
|
|
+ mbedtls_ssl_close_notify(ssl_ctx->get_context());
|
|
|
}
|
|
|
|
|
|
_cleanup();
|
|
@@ -333,28 +313,9 @@ StreamPeerSSL *StreamPeerMbedTLS::_create_func() {
|
|
|
return memnew(StreamPeerMbedTLS);
|
|
|
}
|
|
|
|
|
|
-mbedtls_x509_crt StreamPeerMbedTLS::cacert;
|
|
|
-
|
|
|
-void StreamPeerMbedTLS::_load_certs(const PoolByteArray &p_array) {
|
|
|
- int arr_len = p_array.size();
|
|
|
- PoolByteArray::Read r = p_array.read();
|
|
|
- int err = mbedtls_x509_crt_parse(&cacert, &r[0], arr_len);
|
|
|
- if (err != 0) {
|
|
|
- WARN_PRINTS("Error parsing some certificates: " + itos(err));
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
void StreamPeerMbedTLS::initialize_ssl() {
|
|
|
|
|
|
_create = _create_func;
|
|
|
- load_certs_func = _load_certs;
|
|
|
-
|
|
|
- mbedtls_x509_crt_init(&cacert);
|
|
|
-
|
|
|
-#ifdef DEBUG_ENABLED
|
|
|
- mbedtls_debug_set_threshold(1);
|
|
|
-#endif
|
|
|
-
|
|
|
available = true;
|
|
|
}
|
|
|
|
|
@@ -362,6 +323,4 @@ void StreamPeerMbedTLS::finalize_ssl() {
|
|
|
|
|
|
available = false;
|
|
|
_create = NULL;
|
|
|
- load_certs_func = NULL;
|
|
|
- mbedtls_x509_crt_free(&cacert);
|
|
|
}
|