2015-03-16 16:33:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Ericsson AB. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
* materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2015-03-16 16:33:03 +00:00
|
|
|
#include "gstdtlsconnection.h"
|
|
|
|
|
|
|
|
#include "gstdtlsagent.h"
|
|
|
|
#include "gstdtlscertificate.h"
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
# define __AVAILABILITYMACROS__
|
|
|
|
# define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2018-11-06 15:50:29 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
2016-10-26 12:51:01 +00:00
|
|
|
#include <string.h>
|
2018-11-06 15:50:29 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2016-10-26 12:51:01 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_dtls_connection_debug);
|
2015-03-16 17:49:58 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_dtls_connection_debug
|
2015-03-16 16:33:03 +00:00
|
|
|
|
|
|
|
#define SRTP_KEY_LEN 16
|
|
|
|
#define SRTP_SALT_LEN 14
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
SIGNAL_ON_ENCODER_KEY,
|
|
|
|
SIGNAL_ON_DECODER_KEY,
|
|
|
|
SIGNAL_ON_PEER_CERTIFICATE,
|
|
|
|
NUM_SIGNALS
|
2015-03-16 16:33:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[NUM_SIGNALS];
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_AGENT,
|
2020-01-12 14:18:09 +00:00
|
|
|
PROP_CONNECTION_STATE,
|
2015-03-16 16:34:05 +00:00
|
|
|
NUM_PROPERTIES
|
2015-03-16 16:33:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[NUM_PROPERTIES];
|
|
|
|
|
|
|
|
static int connection_ex_index;
|
|
|
|
|
2015-03-19 12:30:00 +00:00
|
|
|
static void handle_timeout (gpointer data, gpointer user_data);
|
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
struct _GstDtlsConnectionPrivate
|
2015-03-16 16:34:05 +00:00
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
BIO *bio;
|
|
|
|
|
|
|
|
gboolean is_client;
|
|
|
|
gboolean is_alive;
|
|
|
|
gboolean keys_exported;
|
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
GstDtlsConnectionState connection_state;
|
2020-01-12 11:56:00 +00:00
|
|
|
gboolean sent_close_notify;
|
|
|
|
gboolean received_close_notify;
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
GMutex mutex;
|
|
|
|
GCond condition;
|
|
|
|
gpointer bio_buffer;
|
|
|
|
gint bio_buffer_len;
|
|
|
|
gint bio_buffer_offset;
|
|
|
|
|
2020-01-12 09:24:15 +00:00
|
|
|
GstDtlsConnectionSendCallback send_callback;
|
|
|
|
gpointer send_callback_user_data;
|
|
|
|
GDestroyNotify send_callback_destroy_notify;
|
2021-05-18 20:31:47 +00:00
|
|
|
GstFlowReturn syscall_flow_return;
|
2015-03-19 12:30:00 +00:00
|
|
|
|
|
|
|
gboolean timeout_pending;
|
|
|
|
GThreadPool *thread_pool;
|
2015-03-16 16:33:03 +00:00
|
|
|
};
|
|
|
|
|
2022-07-30 16:42:50 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstDtlsConnection, gst_dtls_connection,
|
|
|
|
GST_TYPE_OBJECT, G_ADD_PRIVATE (GstDtlsConnection)
|
2018-06-23 22:17:26 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_dtls_connection_debug, "dtlsconnection", 0,
|
|
|
|
"DTLS Connection"));
|
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
static void gst_dtls_connection_finalize (GObject * gobject);
|
|
|
|
static void gst_dtls_connection_set_property (GObject *, guint prop_id,
|
2015-03-16 16:34:05 +00:00
|
|
|
const GValue *, GParamSpec *);
|
2020-01-12 14:18:09 +00:00
|
|
|
static void gst_dtls_connection_get_property (GObject *, guint prop_id,
|
|
|
|
GValue *, GParamSpec *);
|
2015-03-16 16:34:05 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
static void log_state (GstDtlsConnection *, const gchar * str);
|
2020-06-26 07:20:04 +00:00
|
|
|
static gboolean export_srtp_keys (GstDtlsConnection *, GError ** err);
|
2020-01-12 14:18:09 +00:00
|
|
|
static GstFlowReturn openssl_poll (GstDtlsConnection *, gboolean * notify_state,
|
|
|
|
GError ** err);
|
2020-01-12 11:56:00 +00:00
|
|
|
static GstFlowReturn handle_error (GstDtlsConnection * self, int ret,
|
2020-01-12 14:18:09 +00:00
|
|
|
GstResourceError error_type, gboolean * notify_state, GError ** err);
|
2015-03-16 16:34:05 +00:00
|
|
|
static int openssl_verify_callback (int preverify_ok,
|
|
|
|
X509_STORE_CTX * x509_ctx);
|
|
|
|
|
2015-03-16 17:36:49 +00:00
|
|
|
static BIO_METHOD *BIO_s_gst_dtls_connection (void);
|
2015-03-16 16:34:05 +00:00
|
|
|
static int bio_method_write (BIO *, const char *data, int size);
|
|
|
|
static int bio_method_read (BIO *, char *out_buffer, int size);
|
|
|
|
static long bio_method_ctrl (BIO *, int cmd, long arg1, void *arg2);
|
|
|
|
static int bio_method_new (BIO *);
|
|
|
|
static int bio_method_free (BIO *);
|
|
|
|
|
|
|
|
static void
|
2015-03-16 16:48:43 +00:00
|
|
|
gst_dtls_connection_class_init (GstDtlsConnectionClass * klass)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:34:05 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
gobject_class->set_property = gst_dtls_connection_set_property;
|
2020-01-12 14:18:09 +00:00
|
|
|
gobject_class->get_property = gst_dtls_connection_get_property;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
connection_ex_index =
|
dtls: Fix some compiler warnings
gstdtlsconnection.c:128:32: error: passing 'const char [30]' to parameter of type 'void *'
discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
SSL_get_ex_new_index (0, "gstdtlsagent connection index", NULL, NULL,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/openssl/ssl.h:1981:43: note: passing argument to parameter 'argp' here
int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
^
gstdtlsconnection.c:822:40: error: arithmetic on a pointer to void is a GNU extension
[-Werror,-Wpointer-arith]
memcpy (out_buffer, priv->bio_buffer + priv->bio_buffer_offset, copy_size);
~~~~~~~~~~~~~~~~ ^
2015-03-16 16:35:29 +00:00
|
|
|
SSL_get_ex_new_index (0, (gpointer) "gstdtlsagent connection index", NULL,
|
|
|
|
NULL, NULL);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
signals[SIGNAL_ON_DECODER_KEY] =
|
|
|
|
g_signal_new ("on-decoder-key", G_TYPE_FROM_CLASS (klass),
|
2019-08-27 05:59:27 +00:00
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
|
|
|
|
G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_UINT);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
signals[SIGNAL_ON_ENCODER_KEY] =
|
|
|
|
g_signal_new ("on-encoder-key", G_TYPE_FROM_CLASS (klass),
|
2019-08-27 05:59:27 +00:00
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
|
|
|
|
G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_UINT);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
signals[SIGNAL_ON_PEER_CERTIFICATE] =
|
|
|
|
g_signal_new ("on-peer-certificate", G_TYPE_FROM_CLASS (klass),
|
2019-08-27 05:59:27 +00:00
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
properties[PROP_AGENT] =
|
|
|
|
g_param_spec_object ("agent",
|
2015-03-16 16:48:43 +00:00
|
|
|
"DTLS Agent",
|
2015-03-16 16:34:05 +00:00
|
|
|
"Agent to use in creation of the connection",
|
2015-03-16 16:48:43 +00:00
|
|
|
GST_TYPE_DTLS_AGENT,
|
2015-03-16 16:34:05 +00:00
|
|
|
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
properties[PROP_CONNECTION_STATE] =
|
|
|
|
g_param_spec_enum ("connection-state",
|
|
|
|
"Connection State",
|
|
|
|
"Current connection state",
|
|
|
|
GST_DTLS_TYPE_CONNECTION_STATE,
|
|
|
|
GST_DTLS_CONNECTION_STATE_NEW, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
_gst_dtls_init_openssl ();
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
gobject_class->finalize = gst_dtls_connection_finalize;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static void
|
2015-03-16 16:48:43 +00:00
|
|
|
gst_dtls_connection_init (GstDtlsConnection * self)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2018-06-23 22:17:26 +00:00
|
|
|
GstDtlsConnectionPrivate *priv;
|
|
|
|
|
|
|
|
self->priv = priv = gst_dtls_connection_get_instance_private (self);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->ssl = NULL;
|
|
|
|
priv->bio = NULL;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->is_client = FALSE;
|
|
|
|
priv->is_alive = TRUE;
|
|
|
|
priv->keys_exported = FALSE;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->bio_buffer = NULL;
|
|
|
|
priv->bio_buffer_len = 0;
|
|
|
|
priv->bio_buffer_offset = 0;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_init (&priv->mutex);
|
|
|
|
g_cond_init (&priv->condition);
|
2015-03-19 12:30:00 +00:00
|
|
|
|
|
|
|
/* Thread pool for handling timeouts, we only need one thread for that
|
|
|
|
* really and share threads with all other thread pools around there as
|
|
|
|
* this is not going to happen very often */
|
2015-03-19 13:04:28 +00:00
|
|
|
priv->thread_pool = g_thread_pool_new (handle_timeout, self, 1, FALSE, NULL);
|
2015-03-19 12:30:00 +00:00
|
|
|
g_assert (priv->thread_pool);
|
|
|
|
priv->timeout_pending = FALSE;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static void
|
2015-03-16 16:48:43 +00:00
|
|
|
gst_dtls_connection_finalize (GObject * gobject)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:48:43 +00:00
|
|
|
GstDtlsConnection *self = GST_DTLS_CONNECTION (gobject);
|
|
|
|
GstDtlsConnectionPrivate *priv = self->priv;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-19 12:30:00 +00:00
|
|
|
g_thread_pool_free (priv->thread_pool, TRUE, TRUE);
|
|
|
|
priv->thread_pool = NULL;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
SSL_free (priv->ssl);
|
|
|
|
priv->ssl = NULL;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 09:24:15 +00:00
|
|
|
if (priv->send_callback_destroy_notify)
|
|
|
|
priv->send_callback_destroy_notify (priv->send_callback_user_data);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_clear (&priv->mutex);
|
|
|
|
g_cond_clear (&priv->condition);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "finalized");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
G_OBJECT_CLASS (gst_dtls_connection_parent_class)->finalize (gobject);
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 12:51:01 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100001L
|
|
|
|
static void
|
|
|
|
BIO_set_data (BIO * bio, void *ptr)
|
|
|
|
{
|
|
|
|
bio->ptr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
BIO_get_data (BIO * bio)
|
|
|
|
{
|
|
|
|
return bio->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
BIO_set_shutdown (BIO * bio, int shutdown)
|
|
|
|
{
|
|
|
|
bio->shutdown = shutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
BIO_set_init (BIO * bio, int init)
|
|
|
|
{
|
|
|
|
bio->init = init;
|
|
|
|
}
|
|
|
|
|
|
|
|
static X509 *
|
|
|
|
X509_STORE_CTX_get0_cert (X509_STORE_CTX * ctx)
|
|
|
|
{
|
|
|
|
return ctx->cert;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static void
|
2015-03-16 16:48:43 +00:00
|
|
|
gst_dtls_connection_set_property (GObject * object, guint prop_id,
|
2015-03-16 16:34:05 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:48:43 +00:00
|
|
|
GstDtlsConnection *self = GST_DTLS_CONNECTION (object);
|
|
|
|
GstDtlsAgent *agent;
|
|
|
|
GstDtlsConnectionPrivate *priv = self->priv;
|
2015-03-16 16:34:05 +00:00
|
|
|
SSL_CTX *ssl_context;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
switch (prop_id) {
|
2015-03-16 16:33:03 +00:00
|
|
|
case PROP_AGENT:
|
2015-03-16 16:34:05 +00:00
|
|
|
g_return_if_fail (!priv->ssl);
|
2015-03-16 16:48:43 +00:00
|
|
|
agent = GST_DTLS_AGENT (g_value_get_object (value));
|
|
|
|
g_return_if_fail (GST_IS_DTLS_AGENT (agent));
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
ssl_context = _gst_dtls_agent_peek_context (agent);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->ssl = SSL_new (ssl_context);
|
|
|
|
g_return_if_fail (priv->ssl);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
priv->bio = BIO_new (BIO_s_gst_dtls_connection ());
|
2015-03-16 16:34:05 +00:00
|
|
|
g_return_if_fail (priv->bio);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2016-10-26 12:51:01 +00:00
|
|
|
BIO_set_data (priv->bio, self);
|
2015-03-16 16:34:05 +00:00
|
|
|
SSL_set_bio (priv->ssl, priv->bio, priv->bio);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
SSL_set_verify (priv->ssl,
|
|
|
|
SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
|
|
|
|
openssl_verify_callback);
|
|
|
|
SSL_set_ex_data (priv->ssl, connection_ex_index, self);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
log_state (self, "connection created");
|
|
|
|
break;
|
2015-03-16 16:33:03 +00:00
|
|
|
default:
|
2015-03-16 16:34:05 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
static void
|
|
|
|
gst_dtls_connection_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstDtlsConnection *self = GST_DTLS_CONNECTION (object);
|
|
|
|
GstDtlsConnectionPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_CONNECTION_STATE:
|
|
|
|
g_mutex_lock (&priv->mutex);
|
|
|
|
g_value_set_enum (value, priv->connection_state);
|
|
|
|
g_mutex_unlock (&priv->mutex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
gboolean
|
|
|
|
gst_dtls_connection_start (GstDtlsConnection * self, gboolean is_client,
|
|
|
|
GError ** err)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 17:36:49 +00:00
|
|
|
GstDtlsConnectionPrivate *priv;
|
2020-01-12 11:56:00 +00:00
|
|
|
gboolean ret;
|
2020-01-12 14:18:09 +00:00
|
|
|
gboolean notify_state = FALSE;
|
2015-03-16 17:36:49 +00:00
|
|
|
|
|
|
|
priv = self->priv;
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
g_return_val_if_fail (priv->send_callback, FALSE);
|
|
|
|
g_return_val_if_fail (priv->ssl, FALSE);
|
|
|
|
g_return_val_if_fail (priv->bio, FALSE);
|
2015-03-16 16:34:05 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locking @ start");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_lock (&priv->mutex);
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locked @ start");
|
2015-03-16 16:34:05 +00:00
|
|
|
|
|
|
|
priv->is_alive = TRUE;
|
|
|
|
priv->bio_buffer = NULL;
|
|
|
|
priv->bio_buffer_len = 0;
|
|
|
|
priv->bio_buffer_offset = 0;
|
|
|
|
priv->keys_exported = FALSE;
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
priv->sent_close_notify = FALSE;
|
|
|
|
priv->received_close_notify = FALSE;
|
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
/* Client immediately starts connecting, the server waits for a client to
|
|
|
|
* start the handshake process */
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->is_client = is_client;
|
|
|
|
if (priv->is_client) {
|
2020-01-12 14:18:09 +00:00
|
|
|
priv->connection_state = GST_DTLS_CONNECTION_STATE_CONNECTING;
|
|
|
|
notify_state = TRUE;
|
2015-03-16 16:34:05 +00:00
|
|
|
SSL_set_connect_state (priv->ssl);
|
|
|
|
} else {
|
2020-01-12 14:18:09 +00:00
|
|
|
if (priv->connection_state != GST_DTLS_CONNECTION_STATE_NEW) {
|
|
|
|
priv->connection_state = GST_DTLS_CONNECTION_STATE_NEW;
|
|
|
|
notify_state = TRUE;
|
|
|
|
}
|
2015-03-16 16:34:05 +00:00
|
|
|
SSL_set_accept_state (priv->ssl);
|
|
|
|
}
|
|
|
|
log_state (self, "initial state set");
|
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
ret = openssl_poll (self, ¬ify_state, err);
|
2020-01-12 11:56:00 +00:00
|
|
|
if (ret == GST_FLOW_EOS && err) {
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_OPEN_WRITE,
|
|
|
|
"Connection closed");
|
|
|
|
}
|
2015-03-16 16:34:05 +00:00
|
|
|
|
|
|
|
log_state (self, "first poll done");
|
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unlocking @ start");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_unlock (&priv->mutex);
|
2020-01-12 11:56:00 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
return ret == GST_FLOW_OK;
|
2015-03-16 16:34:05 +00:00
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-18 17:17:39 +00:00
|
|
|
static void
|
2015-03-19 12:30:00 +00:00
|
|
|
handle_timeout (gpointer data, gpointer user_data)
|
|
|
|
{
|
2015-03-19 13:04:28 +00:00
|
|
|
GstDtlsConnection *self = user_data;
|
2015-03-19 12:30:00 +00:00
|
|
|
GstDtlsConnectionPrivate *priv;
|
|
|
|
gint ret;
|
2020-01-12 14:18:09 +00:00
|
|
|
gboolean notify_state = FALSE;
|
2015-03-19 12:30:00 +00:00
|
|
|
|
|
|
|
priv = self->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->mutex);
|
|
|
|
priv->timeout_pending = FALSE;
|
|
|
|
if (priv->is_alive) {
|
|
|
|
ret = DTLSv1_handle_timeout (priv->ssl);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "handle timeout returned %d, is_alive: %d", ret,
|
|
|
|
priv->is_alive);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
GST_WARNING_OBJECT (self, "handling timeout failed");
|
|
|
|
} else if (ret > 0) {
|
|
|
|
log_state (self, "handling timeout before poll");
|
2020-01-12 14:18:09 +00:00
|
|
|
openssl_poll (self, ¬ify_state, NULL);
|
2015-03-19 12:30:00 +00:00
|
|
|
log_state (self, "handling timeout after poll");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&priv->mutex);
|
2020-01-12 14:18:09 +00:00
|
|
|
|
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
2015-03-19 12:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
schedule_timeout_handling (GstClock * clock, GstClockTime time, GstClockID id,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GstDtlsConnection *self = user_data;
|
|
|
|
|
|
|
|
g_mutex_lock (&self->priv->mutex);
|
|
|
|
if (self->priv->is_alive && !self->priv->timeout_pending) {
|
|
|
|
self->priv->timeout_pending = TRUE;
|
|
|
|
|
|
|
|
GST_TRACE_OBJECT (self, "Schedule timeout now");
|
2015-03-19 13:04:28 +00:00
|
|
|
g_thread_pool_push (self->priv->thread_pool, GINT_TO_POINTER (0xc0ffee),
|
|
|
|
NULL);
|
2015-03-19 12:30:00 +00:00
|
|
|
}
|
|
|
|
g_mutex_unlock (&self->priv->mutex);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_dtls_connection_check_timeout_locked (GstDtlsConnection * self)
|
2015-03-16 16:34:05 +00:00
|
|
|
{
|
2015-03-16 17:36:49 +00:00
|
|
|
GstDtlsConnectionPrivate *priv;
|
2015-03-19 12:30:00 +00:00
|
|
|
struct timeval timeout;
|
|
|
|
gint64 end_time, wait_time;
|
2015-03-16 17:36:49 +00:00
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
g_return_if_fail (GST_IS_DTLS_CONNECTION (self));
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 17:36:49 +00:00
|
|
|
priv = self->priv;
|
|
|
|
|
2015-03-19 12:30:00 +00:00
|
|
|
if (DTLSv1_get_timeout (priv->ssl, &timeout)) {
|
|
|
|
wait_time = timeout.tv_sec * G_USEC_PER_SEC + timeout.tv_usec;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-19 12:30:00 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "waiting for %" G_GINT64_FORMAT " usec", wait_time);
|
|
|
|
if (wait_time) {
|
2018-02-05 06:41:25 +00:00
|
|
|
GstClock *system_clock = gst_system_clock_obtain ();
|
2015-03-19 12:30:00 +00:00
|
|
|
GstClockID clock_id;
|
2015-09-17 14:20:13 +00:00
|
|
|
#ifndef G_DISABLE_ASSERT
|
2015-03-19 12:30:00 +00:00
|
|
|
GstClockReturn clock_return;
|
2015-09-17 14:20:13 +00:00
|
|
|
#endif
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-19 12:30:00 +00:00
|
|
|
end_time = gst_clock_get_time (system_clock) + wait_time * GST_USECOND;
|
|
|
|
|
|
|
|
clock_id = gst_clock_new_single_shot_id (system_clock, end_time);
|
2015-09-17 14:20:13 +00:00
|
|
|
#ifndef G_DISABLE_ASSERT
|
2015-03-19 12:30:00 +00:00
|
|
|
clock_return =
|
2015-09-17 14:20:13 +00:00
|
|
|
#else
|
|
|
|
(void)
|
|
|
|
#endif
|
2015-03-19 12:30:00 +00:00
|
|
|
gst_clock_id_wait_async (clock_id, schedule_timeout_handling,
|
|
|
|
g_object_ref (self), (GDestroyNotify) g_object_unref);
|
|
|
|
g_assert (clock_return == GST_CLOCK_OK);
|
|
|
|
gst_clock_id_unref (clock_id);
|
2018-02-05 06:41:25 +00:00
|
|
|
gst_object_unref (system_clock);
|
2015-03-19 12:30:00 +00:00
|
|
|
} else {
|
|
|
|
if (self->priv->is_alive && !self->priv->timeout_pending) {
|
|
|
|
self->priv->timeout_pending = TRUE;
|
|
|
|
GST_TRACE_OBJECT (self, "Schedule timeout now");
|
|
|
|
|
2015-03-19 13:04:28 +00:00
|
|
|
g_thread_pool_push (self->priv->thread_pool, GINT_TO_POINTER (0xc0ffee),
|
|
|
|
NULL);
|
2015-03-19 12:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (self, "no timeout set");
|
|
|
|
}
|
2015-03-18 17:17:39 +00:00
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-18 17:17:39 +00:00
|
|
|
void
|
2015-03-19 12:30:00 +00:00
|
|
|
gst_dtls_connection_check_timeout (GstDtlsConnection * self)
|
2015-03-18 17:17:39 +00:00
|
|
|
{
|
|
|
|
GstDtlsConnectionPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_DTLS_CONNECTION (self));
|
|
|
|
|
|
|
|
priv = self->priv;
|
|
|
|
|
|
|
|
GST_TRACE_OBJECT (self, "locking @ start_timeout");
|
|
|
|
g_mutex_lock (&priv->mutex);
|
|
|
|
GST_TRACE_OBJECT (self, "locked @ start_timeout");
|
2015-03-19 12:30:00 +00:00
|
|
|
gst_dtls_connection_check_timeout_locked (self);
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_unlock (&priv->mutex);
|
2015-03-18 17:17:39 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unlocking @ start_timeout");
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
void
|
2015-03-16 16:48:43 +00:00
|
|
|
gst_dtls_connection_stop (GstDtlsConnection * self)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2020-01-12 14:18:09 +00:00
|
|
|
gboolean notify_state = FALSE;
|
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
g_return_if_fail (GST_IS_DTLS_CONNECTION (self));
|
2015-03-16 16:34:05 +00:00
|
|
|
g_return_if_fail (self->priv->ssl);
|
|
|
|
g_return_if_fail (self->priv->bio);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "stopping connection");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locking @ stop");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_lock (&self->priv->mutex);
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locked @ stop");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
self->priv->is_alive = FALSE;
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state != GST_DTLS_CONNECTION_STATE_FAILED
|
|
|
|
&& self->priv->connection_state != GST_DTLS_CONNECTION_STATE_CLOSED) {
|
|
|
|
self->priv->connection_state = GST_DTLS_CONNECTION_STATE_CLOSED;
|
|
|
|
notify_state = TRUE;
|
|
|
|
}
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "signaling @ stop");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_cond_signal (&self->priv->condition);
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "signaled @ stop");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unlocking @ stop");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_unlock (&self->priv->mutex);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "stopped connection");
|
2020-01-12 14:18:09 +00:00
|
|
|
|
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
void
|
2015-03-16 16:48:43 +00:00
|
|
|
gst_dtls_connection_close (GstDtlsConnection * self)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2020-01-12 14:18:09 +00:00
|
|
|
gboolean notify_state = FALSE;
|
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
g_return_if_fail (GST_IS_DTLS_CONNECTION (self));
|
2015-03-16 16:34:05 +00:00
|
|
|
g_return_if_fail (self->priv->ssl);
|
|
|
|
g_return_if_fail (self->priv->bio);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "closing connection");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locking @ close");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_lock (&self->priv->mutex);
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locked @ close");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
if (self->priv->is_alive) {
|
|
|
|
self->priv->is_alive = FALSE;
|
|
|
|
g_cond_signal (&self->priv->condition);
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state != GST_DTLS_CONNECTION_STATE_FAILED
|
|
|
|
&& self->priv->connection_state != GST_DTLS_CONNECTION_STATE_CLOSED) {
|
|
|
|
self->priv->connection_state = GST_DTLS_CONNECTION_STATE_CLOSED;
|
|
|
|
notify_state = TRUE;
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unlocking @ close");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_unlock (&self->priv->mutex);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "closed connection");
|
2020-01-12 14:18:09 +00:00
|
|
|
|
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
void
|
2015-03-16 16:48:43 +00:00
|
|
|
gst_dtls_connection_set_send_callback (GstDtlsConnection * self,
|
2020-01-12 09:24:15 +00:00
|
|
|
GstDtlsConnectionSendCallback callback, gpointer user_data,
|
|
|
|
GDestroyNotify destroy_notify)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2020-01-12 09:24:15 +00:00
|
|
|
GstDtlsConnectionPrivate *priv;
|
|
|
|
|
2015-03-16 16:48:43 +00:00
|
|
|
g_return_if_fail (GST_IS_DTLS_CONNECTION (self));
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 09:24:15 +00:00
|
|
|
priv = self->priv;
|
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locking @ set_send_callback");
|
2020-01-12 09:24:15 +00:00
|
|
|
g_mutex_lock (&priv->mutex);
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locked @ set_send_callback");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 09:24:15 +00:00
|
|
|
if (priv->send_callback_destroy_notify)
|
|
|
|
priv->send_callback_destroy_notify (priv->send_callback_user_data);
|
|
|
|
priv->send_callback = callback;
|
|
|
|
priv->send_callback_user_data = user_data;
|
|
|
|
priv->send_callback_destroy_notify = destroy_notify;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unlocking @ set_send_callback");
|
2020-01-12 09:24:15 +00:00
|
|
|
g_mutex_unlock (&priv->mutex);
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 20:31:47 +00:00
|
|
|
void
|
|
|
|
gst_dtls_connection_set_flow_return (GstDtlsConnection * self,
|
|
|
|
GstFlowReturn flow_ret)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_DTLS_CONNECTION (self));
|
|
|
|
self->priv->syscall_flow_return = flow_ret;
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
GstFlowReturn
|
|
|
|
gst_dtls_connection_process (GstDtlsConnection * self, gpointer data, gsize len,
|
|
|
|
gsize * written, GError ** err)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2020-01-12 11:56:00 +00:00
|
|
|
GstFlowReturn flow_ret = GST_FLOW_OK;
|
2015-03-16 17:36:49 +00:00
|
|
|
GstDtlsConnectionPrivate *priv;
|
2020-01-12 11:56:00 +00:00
|
|
|
int ret;
|
2020-01-12 14:18:09 +00:00
|
|
|
gboolean notify_state = FALSE;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 17:36:49 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DTLS_CONNECTION (self), 0);
|
2015-03-16 16:34:05 +00:00
|
|
|
g_return_val_if_fail (self->priv->ssl, 0);
|
|
|
|
g_return_val_if_fail (self->priv->bio, 0);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 17:36:49 +00:00
|
|
|
priv = self->priv;
|
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locking @ process");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_lock (&priv->mutex);
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locked @ process");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-10-30 11:49:22 +00:00
|
|
|
if (self->priv->received_close_notify
|
|
|
|
|| self->priv->connection_state == GST_DTLS_CONNECTION_STATE_CLOSED) {
|
2020-01-12 11:56:00 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Already received close_notify");
|
|
|
|
g_mutex_unlock (&priv->mutex);
|
|
|
|
return GST_FLOW_EOS;
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state == GST_DTLS_CONNECTION_STATE_FAILED) {
|
2020-01-12 11:56:00 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Had a fatal error before");
|
|
|
|
g_mutex_unlock (&priv->mutex);
|
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
|
|
|
|
"Had fatal error before");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:49:22 +00:00
|
|
|
g_warn_if_fail (!priv->bio_buffer);
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->bio_buffer = data;
|
|
|
|
priv->bio_buffer_len = len;
|
|
|
|
priv->bio_buffer_offset = 0;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
log_state (self, "process start");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
if (SSL_want_write (priv->ssl)) {
|
2020-01-12 14:18:09 +00:00
|
|
|
flow_ret = openssl_poll (self, ¬ify_state, err);
|
2015-03-16 16:34:05 +00:00
|
|
|
log_state (self, "process want write, after poll");
|
2020-01-12 11:56:00 +00:00
|
|
|
if (flow_ret != GST_FLOW_OK) {
|
|
|
|
g_mutex_unlock (&priv->mutex);
|
|
|
|
return flow_ret;
|
|
|
|
}
|
2015-03-16 16:34:05 +00:00
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
/* If we're a server and were in new state then by receiving the first data
|
|
|
|
* we would start the connection process */
|
|
|
|
if (!priv->is_client) {
|
|
|
|
if (self->priv->connection_state == GST_DTLS_CONNECTION_STATE_NEW) {
|
|
|
|
priv->connection_state = GST_DTLS_CONNECTION_STATE_CONNECTING;
|
|
|
|
notify_state = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
ret = SSL_read (priv->ssl, data, len);
|
|
|
|
*written = ret >= 0 ? ret : 0;
|
|
|
|
GST_DEBUG_OBJECT (self, "read result: %d", ret);
|
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
flow_ret =
|
|
|
|
handle_error (self, ret, GST_RESOURCE_ERROR_READ, ¬ify_state, err);
|
2020-01-12 11:56:00 +00:00
|
|
|
if (flow_ret == GST_FLOW_EOS) {
|
|
|
|
self->priv->received_close_notify = TRUE;
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state != GST_DTLS_CONNECTION_STATE_FAILED
|
|
|
|
&& self->priv->connection_state != GST_DTLS_CONNECTION_STATE_CLOSED) {
|
|
|
|
self->priv->connection_state = GST_DTLS_CONNECTION_STATE_CLOSED;
|
|
|
|
notify_state = TRUE;
|
|
|
|
}
|
2020-01-12 11:56:00 +00:00
|
|
|
/* Notify about the connection being properly closed now if both
|
|
|
|
* sides did so */
|
|
|
|
if (self->priv->sent_close_notify && self->priv->send_callback)
|
|
|
|
self->priv->send_callback (self, NULL, 0, NULL);
|
|
|
|
|
|
|
|
g_mutex_unlock (&priv->mutex);
|
2020-01-12 14:18:09 +00:00
|
|
|
|
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
return flow_ret;
|
|
|
|
} else if (flow_ret != GST_FLOW_OK) {
|
|
|
|
g_mutex_unlock (&priv->mutex);
|
2020-01-12 14:18:09 +00:00
|
|
|
|
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
return flow_ret;
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
log_state (self, "process after read");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
flow_ret = openssl_poll (self, ¬ify_state, err);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
log_state (self, "process after poll");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unlocking @ process");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_unlock (&priv->mutex);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
return flow_ret;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
GstFlowReturn
|
|
|
|
gst_dtls_connection_send (GstDtlsConnection * self, gconstpointer data,
|
|
|
|
gsize len, gsize * written, GError ** err)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2020-01-12 11:56:00 +00:00
|
|
|
GstFlowReturn flow_ret;
|
2015-03-16 16:34:05 +00:00
|
|
|
int ret = 0;
|
2020-01-12 14:18:09 +00:00
|
|
|
gboolean notify_state = FALSE;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 17:36:49 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DTLS_CONNECTION (self), 0);
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
g_return_val_if_fail (self->priv->ssl, 0);
|
|
|
|
g_return_val_if_fail (self->priv->bio, 0);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locking @ send");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_lock (&self->priv->mutex);
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "locked @ send");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state == GST_DTLS_CONNECTION_STATE_FAILED) {
|
2020-01-12 11:56:00 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Had a fatal error before");
|
|
|
|
g_mutex_unlock (&self->priv->mutex);
|
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
|
|
|
|
"Had fatal error before");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->priv->sent_close_notify) {
|
|
|
|
len = 0;
|
|
|
|
GST_DEBUG_OBJECT (self, "Not sending new data after close_notify");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
if (written)
|
|
|
|
*written = 0;
|
|
|
|
GST_DEBUG_OBJECT (self, "Sending close_notify");
|
|
|
|
ret = SSL_shutdown (self->priv->ssl);
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state != GST_DTLS_CONNECTION_STATE_CLOSED &&
|
|
|
|
self->priv->connection_state != GST_DTLS_CONNECTION_STATE_FAILED) {
|
|
|
|
self->priv->connection_state = GST_DTLS_CONNECTION_STATE_CLOSED;
|
|
|
|
notify_state = TRUE;
|
|
|
|
}
|
2020-01-12 11:56:00 +00:00
|
|
|
if (ret == 1) {
|
|
|
|
GST_LOG_OBJECT (self, "received peer close_notify already");
|
|
|
|
self->priv->received_close_notify = TRUE;
|
|
|
|
flow_ret = GST_FLOW_EOS;
|
|
|
|
} else if (ret == 0) {
|
|
|
|
GST_LOG_OBJECT (self, "did not receive peer close_notify yet");
|
|
|
|
flow_ret = GST_FLOW_OK;
|
|
|
|
} else {
|
2020-01-12 14:18:09 +00:00
|
|
|
flow_ret =
|
|
|
|
handle_error (self, ret, GST_RESOURCE_ERROR_WRITE, ¬ify_state,
|
|
|
|
err);
|
2020-01-12 11:56:00 +00:00
|
|
|
}
|
|
|
|
} else if (SSL_is_init_finished (self->priv->ssl)) {
|
|
|
|
GST_DEBUG_OBJECT (self, "sending data of %" G_GSIZE_FORMAT " B", len);
|
2015-03-16 16:34:05 +00:00
|
|
|
ret = SSL_write (self->priv->ssl, data, len);
|
2020-01-12 11:56:00 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
if (written)
|
|
|
|
*written = 0;
|
2020-01-12 14:18:09 +00:00
|
|
|
flow_ret =
|
|
|
|
handle_error (self, ret, GST_RESOURCE_ERROR_WRITE, ¬ify_state,
|
|
|
|
err);
|
2020-01-12 11:56:00 +00:00
|
|
|
} else {
|
|
|
|
if (written)
|
|
|
|
*written = ret;
|
|
|
|
flow_ret = GST_FLOW_OK;
|
|
|
|
}
|
2015-03-16 16:34:05 +00:00
|
|
|
} else {
|
2020-01-12 11:56:00 +00:00
|
|
|
if (written)
|
|
|
|
*written = ret;
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"tried to send data before handshake was complete");
|
2020-01-12 11:56:00 +00:00
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
|
|
|
|
"Tried to send data before handshake was complete");
|
|
|
|
flow_ret = GST_FLOW_ERROR;
|
2015-03-16 16:34:05 +00:00
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unlocking @ send");
|
2015-03-16 16:34:05 +00:00
|
|
|
g_mutex_unlock (&self->priv->mutex);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
if (notify_state) {
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
properties[PROP_CONNECTION_STATE]);
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
return flow_ret;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
###### ####### ## ##
|
|
|
|
## ## ## ## ### ##
|
|
|
|
## ## ## #### ##
|
|
|
|
## ## ## ## ## ##
|
|
|
|
## ## ## ## ####
|
|
|
|
## ## ## ## ## ###
|
|
|
|
###### ####### ## ##
|
|
|
|
*/
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static void
|
2015-03-16 16:48:43 +00:00
|
|
|
log_state (GstDtlsConnection * self, const gchar * str)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:48:43 +00:00
|
|
|
GstDtlsConnectionPrivate *priv = self->priv;
|
2015-03-16 16:34:05 +00:00
|
|
|
guint states = 0;
|
|
|
|
|
2018-11-28 11:35:35 +00:00
|
|
|
states |= (!!SSL_is_init_finished (priv->ssl) << 0);
|
|
|
|
states |= (!!SSL_in_init (priv->ssl) << 4);
|
|
|
|
states |= (!!SSL_in_before (priv->ssl) << 8);
|
|
|
|
states |= (!!SSL_in_connect_init (priv->ssl) << 12);
|
|
|
|
states |= (!!SSL_in_accept_init (priv->ssl) << 16);
|
|
|
|
states |= (!!SSL_want_write (priv->ssl) << 20);
|
|
|
|
states |= (!!SSL_want_read (priv->ssl) << 24);
|
2015-03-16 16:34:05 +00:00
|
|
|
|
2016-10-26 12:51:01 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100001L
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "%s: role=%s buf=(%d,%p:%d/%d) %x|%x %s",
|
2015-03-16 16:34:05 +00:00
|
|
|
str,
|
|
|
|
priv->is_client ? "client" : "server",
|
|
|
|
pqueue_size (priv->ssl->d1->sent_messages),
|
|
|
|
priv->bio_buffer,
|
|
|
|
priv->bio_buffer_offset,
|
|
|
|
priv->bio_buffer_len,
|
|
|
|
states, SSL_get_state (priv->ssl), SSL_state_string_long (priv->ssl));
|
2016-10-26 12:51:01 +00:00
|
|
|
#else
|
|
|
|
GST_LOG_OBJECT (self, "%s: role=%s buf=(%p:%d/%d) %x|%x %s",
|
|
|
|
str,
|
|
|
|
priv->is_client ? "client" : "server",
|
|
|
|
priv->bio_buffer,
|
|
|
|
priv->bio_buffer_offset,
|
|
|
|
priv->bio_buffer_len,
|
|
|
|
states, SSL_get_state (priv->ssl), SSL_state_string_long (priv->ssl));
|
|
|
|
#endif
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 07:20:04 +00:00
|
|
|
static gboolean
|
|
|
|
export_srtp_keys (GstDtlsConnection * self, GError ** err)
|
2015-03-16 16:34:05 +00:00
|
|
|
{
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
guint8 v[SRTP_KEY_LEN];
|
|
|
|
} Key;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
guint8 v[SRTP_SALT_LEN];
|
|
|
|
} Salt;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
Key client_key;
|
2015-03-18 08:46:40 +00:00
|
|
|
Key server_key;
|
2015-03-16 16:34:05 +00:00
|
|
|
Salt client_salt;
|
2015-03-18 08:46:40 +00:00
|
|
|
Salt server_salt;
|
2015-03-16 16:34:05 +00:00
|
|
|
} exported_keys;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
Key key;
|
|
|
|
Salt salt;
|
2015-03-18 08:46:40 +00:00
|
|
|
} client_key, server_key;
|
2015-03-16 16:34:05 +00:00
|
|
|
|
|
|
|
SRTP_PROTECTION_PROFILE *profile;
|
2015-03-16 16:48:43 +00:00
|
|
|
GstDtlsSrtpCipher cipher;
|
|
|
|
GstDtlsSrtpAuth auth;
|
2015-03-16 16:34:05 +00:00
|
|
|
gint success;
|
|
|
|
|
|
|
|
static gchar export_string[] = "EXTRACTOR-dtls_srtp";
|
|
|
|
|
|
|
|
success = SSL_export_keying_material (self->priv->ssl,
|
|
|
|
(gpointer) & exported_keys, 60, export_string, strlen (export_string),
|
|
|
|
NULL, 0, 0);
|
|
|
|
|
|
|
|
if (!success) {
|
2020-06-26 07:20:04 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Failed to export SRTP keys");
|
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
|
|
|
|
"Failed to export SRTP keys");
|
|
|
|
return FALSE;
|
2015-03-16 16:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
profile = SSL_get_selected_srtp_profile (self->priv->ssl);
|
|
|
|
|
2020-04-01 16:08:45 +00:00
|
|
|
if (!profile) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
2020-06-26 07:20:04 +00:00
|
|
|
"No SRTP capabilities negotiated during handshake");
|
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
|
|
|
|
"No SRTP capabilities negotiated during handshake");
|
|
|
|
return FALSE;
|
2020-04-01 16:08:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_INFO_OBJECT (self, "keys received, profile is %s", profile->name);
|
2015-03-16 16:34:05 +00:00
|
|
|
|
|
|
|
switch (profile->id) {
|
|
|
|
case SRTP_AES128_CM_SHA1_80:
|
2015-03-16 16:48:43 +00:00
|
|
|
cipher = GST_DTLS_SRTP_CIPHER_AES_128_ICM;
|
|
|
|
auth = GST_DTLS_SRTP_AUTH_HMAC_SHA1_80;
|
2015-03-16 16:34:05 +00:00
|
|
|
break;
|
|
|
|
case SRTP_AES128_CM_SHA1_32:
|
2015-03-16 16:48:43 +00:00
|
|
|
cipher = GST_DTLS_SRTP_CIPHER_AES_128_ICM;
|
|
|
|
auth = GST_DTLS_SRTP_AUTH_HMAC_SHA1_32;
|
2015-03-16 16:34:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-06-26 07:20:04 +00:00
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Invalid/unsupported crypto suite set by handshake");
|
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
|
|
|
|
"Invalid/unsupported crypto suite set by handshake");
|
|
|
|
return FALSE;
|
2015-03-16 16:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client_key.key = exported_keys.client_key;
|
2015-03-18 08:46:40 +00:00
|
|
|
server_key.key = exported_keys.server_key;
|
2015-03-16 16:34:05 +00:00
|
|
|
client_key.salt = exported_keys.client_salt;
|
2015-03-18 08:46:40 +00:00
|
|
|
server_key.salt = exported_keys.server_salt;
|
2015-03-16 16:34:05 +00:00
|
|
|
|
|
|
|
if (self->priv->is_client) {
|
|
|
|
g_signal_emit (self, signals[SIGNAL_ON_ENCODER_KEY], 0, &client_key, cipher,
|
|
|
|
auth);
|
2015-03-18 08:46:40 +00:00
|
|
|
g_signal_emit (self, signals[SIGNAL_ON_DECODER_KEY], 0, &server_key,
|
2015-03-16 16:48:43 +00:00
|
|
|
cipher, auth);
|
2015-03-16 16:34:05 +00:00
|
|
|
} else {
|
2015-03-18 08:46:40 +00:00
|
|
|
g_signal_emit (self, signals[SIGNAL_ON_ENCODER_KEY], 0, &server_key,
|
2015-03-16 16:48:43 +00:00
|
|
|
cipher, auth);
|
2015-03-16 16:34:05 +00:00
|
|
|
g_signal_emit (self, signals[SIGNAL_ON_DECODER_KEY], 0, &client_key, cipher,
|
|
|
|
auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
self->priv->keys_exported = TRUE;
|
2020-06-26 07:20:04 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:33:10 +00:00
|
|
|
static int
|
|
|
|
ssl_warn_cb (const char *str, size_t len, void *u)
|
|
|
|
{
|
|
|
|
GstDtlsConnection *self = u;
|
|
|
|
GST_WARNING_OBJECT (self, "ssl error: %s", str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ssl_err_cb (const char *str, size_t len, void *u)
|
|
|
|
{
|
|
|
|
GstDtlsConnection *self = u;
|
|
|
|
GST_ERROR_OBJECT (self, "ssl error: %s", str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
handle_error (GstDtlsConnection * self, int ret, GstResourceError error_type,
|
2020-01-12 14:18:09 +00:00
|
|
|
gboolean * notify_state, GError ** err)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:34:05 +00:00
|
|
|
int error;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 11:56:00 +00:00
|
|
|
error = SSL_get_error (self->priv->ssl, ret);
|
|
|
|
|
|
|
|
switch (error) {
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
GST_TRACE_OBJECT (self, "No error");
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
GST_ERROR_OBJECT (self, "Fatal SSL error");
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state != GST_DTLS_CONNECTION_STATE_FAILED) {
|
|
|
|
self->priv->connection_state = GST_DTLS_CONNECTION_STATE_FAILED;
|
|
|
|
*notify_state = TRUE;
|
|
|
|
}
|
2020-01-12 11:56:00 +00:00
|
|
|
ERR_print_errors_cb (ssl_err_cb, self);
|
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new_literal (GST_RESOURCE_ERROR, error_type,
|
|
|
|
"Fatal SSL error");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
GST_LOG_OBJECT (self, "Connection was closed");
|
|
|
|
return GST_FLOW_EOS;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
GST_LOG_OBJECT (self, "SSL wants read");
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
GST_LOG_OBJECT (self, "SSL wants write");
|
|
|
|
return GST_FLOW_OK;
|
2021-05-18 20:31:47 +00:00
|
|
|
case SSL_ERROR_SYSCALL:{
|
|
|
|
GstFlowReturn rc = GST_FLOW_OK;
|
2020-10-05 20:40:55 +00:00
|
|
|
/* OpenSSL shouldn't be making real system calls, so we can safely
|
|
|
|
* ignore syscall errors. System interactions should happen through
|
|
|
|
* our BIO.
|
|
|
|
*/
|
2021-05-18 20:31:47 +00:00
|
|
|
if (error_type == GST_RESOURCE_ERROR_WRITE) {
|
|
|
|
rc = self->priv->syscall_flow_return;
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"OpenSSL reported a syscall error. flow_return=%i", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
2020-01-12 11:56:00 +00:00
|
|
|
default:
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state != GST_DTLS_CONNECTION_STATE_FAILED) {
|
|
|
|
self->priv->connection_state = GST_DTLS_CONNECTION_STATE_FAILED;
|
|
|
|
*notify_state = TRUE;
|
|
|
|
}
|
2020-01-12 11:56:00 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Unknown SSL error: %d, ret: %d", error, ret);
|
|
|
|
if (err)
|
|
|
|
*err =
|
|
|
|
g_error_new (GST_RESOURCE_ERROR, error_type,
|
|
|
|
"Unknown SSL error: %d, ret: %d", error, ret);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2020-01-12 14:18:09 +00:00
|
|
|
openssl_poll (GstDtlsConnection * self, gboolean * notify_state, GError ** err)
|
2020-01-12 11:56:00 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
GstFlowReturn flow_ret;
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
log_state (self, "poll: before handshake");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2018-11-05 11:31:38 +00:00
|
|
|
ERR_clear_error ();
|
2015-03-16 16:34:05 +00:00
|
|
|
ret = SSL_do_handshake (self->priv->ssl);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
log_state (self, "poll: after handshake");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2018-11-05 11:37:21 +00:00
|
|
|
switch (ret) {
|
|
|
|
case 1:
|
|
|
|
if (!self->priv->keys_exported) {
|
|
|
|
GST_INFO_OBJECT (self,
|
|
|
|
"handshake just completed successfully, exporting keys");
|
2020-06-26 07:20:04 +00:00
|
|
|
|
|
|
|
if (!export_srtp_keys (self, err))
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
if (self->priv->connection_state != GST_DTLS_CONNECTION_STATE_FAILED
|
|
|
|
&& self->priv->connection_state != GST_DTLS_CONNECTION_STATE_CLOSED
|
|
|
|
&& self->priv->connection_state !=
|
|
|
|
GST_DTLS_CONNECTION_STATE_CONNECTED) {
|
|
|
|
self->priv->connection_state = GST_DTLS_CONNECTION_STATE_CONNECTED;
|
|
|
|
*notify_state = TRUE;
|
|
|
|
}
|
2018-11-05 11:37:21 +00:00
|
|
|
} else {
|
|
|
|
GST_INFO_OBJECT (self, "handshake is completed");
|
|
|
|
}
|
2020-01-12 11:56:00 +00:00
|
|
|
return GST_FLOW_OK;
|
2018-11-05 11:37:21 +00:00
|
|
|
case 0:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "do_handshake encountered EOF");
|
2018-11-05 11:37:21 +00:00
|
|
|
break;
|
|
|
|
case -1:
|
2020-01-12 11:56:00 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "do_handshake encountered potential BIO error");
|
2018-11-05 11:37:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "do_handshake returned %d", ret);
|
2015-03-16 16:34:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-03-29 11:33:10 +00:00
|
|
|
|
2020-01-12 14:18:09 +00:00
|
|
|
flow_ret =
|
|
|
|
handle_error (self, ret, GST_RESOURCE_ERROR_OPEN_WRITE, notify_state,
|
|
|
|
err);
|
2020-01-12 11:56:00 +00:00
|
|
|
|
2018-03-29 11:33:10 +00:00
|
|
|
ERR_print_errors_cb (ssl_warn_cb, self);
|
2020-01-12 11:56:00 +00:00
|
|
|
|
|
|
|
return flow_ret;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static int
|
|
|
|
openssl_verify_callback (int preverify_ok, X509_STORE_CTX * x509_ctx)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:48:43 +00:00
|
|
|
GstDtlsConnection *self;
|
2015-03-16 16:34:05 +00:00
|
|
|
SSL *ssl;
|
|
|
|
BIO *bio;
|
|
|
|
gchar *pem = NULL;
|
|
|
|
gboolean accepted = FALSE;
|
|
|
|
|
|
|
|
ssl =
|
|
|
|
X509_STORE_CTX_get_ex_data (x509_ctx,
|
|
|
|
SSL_get_ex_data_X509_STORE_CTX_idx ());
|
|
|
|
self = SSL_get_ex_data (ssl, connection_ex_index);
|
2015-03-16 16:48:43 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DTLS_CONNECTION (self), FALSE);
|
2015-03-16 16:34:05 +00:00
|
|
|
|
2016-10-26 12:51:01 +00:00
|
|
|
pem = _gst_dtls_x509_to_pem (X509_STORE_CTX_get0_cert (x509_ctx));
|
2015-03-16 16:34:05 +00:00
|
|
|
|
|
|
|
if (!pem) {
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"failed to convert received certificate to pem format");
|
2015-03-16 16:34:05 +00:00
|
|
|
} else {
|
|
|
|
bio = BIO_new (BIO_s_mem ());
|
|
|
|
if (bio) {
|
|
|
|
gchar buffer[2048];
|
|
|
|
gint len;
|
|
|
|
|
|
|
|
len =
|
2016-10-26 12:51:01 +00:00
|
|
|
X509_NAME_print_ex (bio,
|
|
|
|
X509_get_subject_name (X509_STORE_CTX_get0_cert (x509_ctx)), 1,
|
2015-03-16 16:34:05 +00:00
|
|
|
XN_FLAG_MULTILINE);
|
|
|
|
BIO_read (bio, buffer, len);
|
|
|
|
buffer[len] = '\0';
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Peer certificate received:\n%s", buffer);
|
2015-03-16 16:34:05 +00:00
|
|
|
BIO_free (bio);
|
2015-03-16 16:33:03 +00:00
|
|
|
} else {
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "failed to create certificate print membio");
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
g_signal_emit (self, signals[SIGNAL_ON_PEER_CERTIFICATE], 0, pem,
|
|
|
|
&accepted);
|
|
|
|
g_free (pem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return accepted;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
######## #### #######
|
|
|
|
## ## ## ## ##
|
|
|
|
## ## ## ## ##
|
|
|
|
######## ## ## ##
|
|
|
|
## ## ## ## ##
|
|
|
|
## ## ## ## ##
|
|
|
|
######## #### #######
|
|
|
|
*/
|
|
|
|
|
2016-10-26 12:51:01 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100001L
|
2015-03-16 16:33:03 +00:00
|
|
|
static BIO_METHOD custom_bio_methods = {
|
2015-03-16 16:34:05 +00:00
|
|
|
BIO_TYPE_BIO,
|
|
|
|
"stream",
|
|
|
|
bio_method_write,
|
|
|
|
bio_method_read,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
bio_method_ctrl,
|
|
|
|
bio_method_new,
|
|
|
|
bio_method_free,
|
|
|
|
NULL,
|
2015-03-16 16:33:03 +00:00
|
|
|
};
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static BIO_METHOD *
|
2015-03-16 17:36:49 +00:00
|
|
|
BIO_s_gst_dtls_connection (void)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:34:05 +00:00
|
|
|
return &custom_bio_methods;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
2016-10-26 12:51:01 +00:00
|
|
|
#else
|
|
|
|
static BIO_METHOD *custom_bio_methods;
|
|
|
|
|
|
|
|
static BIO_METHOD *
|
|
|
|
BIO_s_gst_dtls_connection (void)
|
|
|
|
{
|
|
|
|
if (custom_bio_methods != NULL)
|
|
|
|
return custom_bio_methods;
|
|
|
|
|
|
|
|
custom_bio_methods = BIO_meth_new (BIO_TYPE_BIO, "stream");
|
|
|
|
if (custom_bio_methods == NULL
|
|
|
|
|| !BIO_meth_set_write (custom_bio_methods, bio_method_write)
|
|
|
|
|| !BIO_meth_set_read (custom_bio_methods, bio_method_read)
|
|
|
|
|| !BIO_meth_set_ctrl (custom_bio_methods, bio_method_ctrl)
|
|
|
|
|| !BIO_meth_set_create (custom_bio_methods, bio_method_new)
|
|
|
|
|| !BIO_meth_set_destroy (custom_bio_methods, bio_method_free)) {
|
|
|
|
BIO_meth_free (custom_bio_methods);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return custom_bio_methods;
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static int
|
|
|
|
bio_method_write (BIO * bio, const char *data, int size)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2016-10-26 12:51:01 +00:00
|
|
|
GstDtlsConnection *self = GST_DTLS_CONNECTION (BIO_get_data (bio));
|
2020-01-12 10:48:55 +00:00
|
|
|
gboolean ret = TRUE;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: writing %d", size);
|
2021-05-18 20:31:47 +00:00
|
|
|
self->priv->syscall_flow_return = GST_FLOW_OK;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 09:24:15 +00:00
|
|
|
if (self->priv->send_callback)
|
2020-01-12 10:48:55 +00:00
|
|
|
ret = self->priv->send_callback (self, data, size,
|
2020-01-12 09:24:15 +00:00
|
|
|
self->priv->send_callback_user_data);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2020-01-12 10:48:55 +00:00
|
|
|
return ret ? size : -1;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static int
|
|
|
|
bio_method_read (BIO * bio, char *out_buffer, int size)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2016-10-26 12:51:01 +00:00
|
|
|
GstDtlsConnection *self = GST_DTLS_CONNECTION (BIO_get_data (bio));
|
2015-03-16 16:48:43 +00:00
|
|
|
GstDtlsConnectionPrivate *priv = self->priv;
|
2015-03-16 16:34:05 +00:00
|
|
|
guint internal_size;
|
|
|
|
gint copy_size;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
internal_size = priv->bio_buffer_len - priv->bio_buffer_offset;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
if (!priv->bio_buffer) {
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: EOF");
|
2015-03-16 16:34:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
if (!out_buffer || size <= 0) {
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_WARNING_OBJECT (self, "BIO: read got invalid arguments");
|
2015-03-16 16:34:05 +00:00
|
|
|
if (internal_size) {
|
|
|
|
BIO_set_retry_read (bio);
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
2015-03-16 16:34:05 +00:00
|
|
|
return internal_size;
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
if (size > internal_size) {
|
|
|
|
copy_size = internal_size;
|
|
|
|
} else {
|
|
|
|
copy_size = size;
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self,
|
2015-03-16 16:34:05 +00:00
|
|
|
"reading %d/%d bytes %d at offset %d, output buff size is %d", copy_size,
|
|
|
|
priv->bio_buffer_len, internal_size, priv->bio_buffer_offset, size);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
dtls: Fix some compiler warnings
gstdtlsconnection.c:128:32: error: passing 'const char [30]' to parameter of type 'void *'
discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
SSL_get_ex_new_index (0, "gstdtlsagent connection index", NULL, NULL,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/openssl/ssl.h:1981:43: note: passing argument to parameter 'argp' here
int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
^
gstdtlsconnection.c:822:40: error: arithmetic on a pointer to void is a GNU extension
[-Werror,-Wpointer-arith]
memcpy (out_buffer, priv->bio_buffer + priv->bio_buffer_offset, copy_size);
~~~~~~~~~~~~~~~~ ^
2015-03-16 16:35:29 +00:00
|
|
|
memcpy (out_buffer, (guint8 *) priv->bio_buffer + priv->bio_buffer_offset,
|
|
|
|
copy_size);
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->bio_buffer_offset += copy_size;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
if (priv->bio_buffer_len == priv->bio_buffer_offset) {
|
|
|
|
priv->bio_buffer = NULL;
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
return copy_size;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static long
|
|
|
|
bio_method_ctrl (BIO * bio, int cmd, long arg1, void *arg2)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2016-10-26 12:51:01 +00:00
|
|
|
GstDtlsConnection *self = GST_DTLS_CONNECTION (BIO_get_data (bio));
|
2015-03-16 16:48:43 +00:00
|
|
|
GstDtlsConnectionPrivate *priv = self->priv;
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
switch (cmd) {
|
2015-03-16 16:33:03 +00:00
|
|
|
case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
|
|
|
|
case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: Timeout set");
|
2015-03-19 12:30:00 +00:00
|
|
|
gst_dtls_connection_check_timeout_locked (self);
|
2015-03-16 16:34:05 +00:00
|
|
|
return 1;
|
2015-03-16 16:33:03 +00:00
|
|
|
case BIO_CTRL_RESET:
|
2015-03-16 16:34:05 +00:00
|
|
|
priv->bio_buffer = NULL;
|
|
|
|
priv->bio_buffer_len = 0;
|
|
|
|
priv->bio_buffer_offset = 0;
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: EOF reset");
|
2015-03-16 16:34:05 +00:00
|
|
|
return 1;
|
|
|
|
case BIO_CTRL_EOF:{
|
2020-03-26 23:34:25 +00:00
|
|
|
gint eof = priv->is_alive == FALSE;
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: EOF query returned %d", eof);
|
2015-03-16 16:34:05 +00:00
|
|
|
return eof;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
case BIO_CTRL_WPENDING:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: pending write");
|
2015-03-16 16:34:05 +00:00
|
|
|
return 1;
|
|
|
|
case BIO_CTRL_PENDING:{
|
|
|
|
gint pending = priv->bio_buffer_len - priv->bio_buffer_offset;
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: %d bytes pending", pending);
|
2015-03-16 16:34:05 +00:00
|
|
|
return pending;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
case BIO_CTRL_FLUSH:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: flushing");
|
2015-03-16 16:34:05 +00:00
|
|
|
return 1;
|
2015-03-16 16:33:03 +00:00
|
|
|
case BIO_CTRL_DGRAM_QUERY_MTU:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "BIO: MTU query, returning 0...");
|
2015-03-16 16:34:05 +00:00
|
|
|
return 0;
|
2015-03-16 16:33:03 +00:00
|
|
|
case BIO_CTRL_DGRAM_MTU_EXCEEDED:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_WARNING_OBJECT (self, "BIO: MTU exceeded");
|
2015-03-16 16:34:05 +00:00
|
|
|
return 0;
|
2015-03-16 16:33:03 +00:00
|
|
|
default:
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (self, "BIO: unhandled ctrl, %d", cmd);
|
2015-03-16 16:34:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static int
|
|
|
|
bio_method_new (BIO * bio)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (NULL, "BIO: new");
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2016-10-26 12:51:01 +00:00
|
|
|
BIO_set_shutdown (bio, 0);
|
|
|
|
BIO_set_init (bio, 1);
|
2015-03-16 16:33:03 +00:00
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
return 1;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:34:05 +00:00
|
|
|
static int
|
|
|
|
bio_method_free (BIO * bio)
|
2015-03-16 16:33:03 +00:00
|
|
|
{
|
2015-03-16 16:34:05 +00:00
|
|
|
if (!bio) {
|
2015-03-16 16:53:11 +00:00
|
|
|
GST_LOG_OBJECT (NULL, "BIO free called with null bio");
|
2015-03-16 16:33:03 +00:00
|
|
|
return 0;
|
2015-03-16 16:34:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 12:51:01 +00:00
|
|
|
GST_LOG_OBJECT (GST_DTLS_CONNECTION (BIO_get_data (bio)), "BIO free");
|
2015-03-16 16:34:05 +00:00
|
|
|
return 0;
|
2015-03-16 16:33:03 +00:00
|
|
|
}
|
2020-01-12 14:18:09 +00:00
|
|
|
|
|
|
|
GType
|
|
|
|
gst_dtls_connection_state_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
static const GEnumValue values[] = {
|
|
|
|
{GST_DTLS_CONNECTION_STATE_NEW, "New connection", "new"},
|
|
|
|
{GST_DTLS_CONNECTION_STATE_CLOSED, "Closed connection on either side",
|
|
|
|
"closed"},
|
|
|
|
{GST_DTLS_CONNECTION_STATE_FAILED, "Failed connection", "failed"},
|
|
|
|
{GST_DTLS_CONNECTION_STATE_CONNECTING, "Connecting", "connecting"},
|
|
|
|
{GST_DTLS_CONNECTION_STATE_CONNECTED, "Successfully connected",
|
|
|
|
"connected"},
|
|
|
|
{0, NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!type) {
|
|
|
|
type = g_enum_register_static ("GstDtlsConnectionState", values);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|