Browse Source

Added SRTP transport outline

Paul-Louis Ageneau 5 years ago
parent
commit
5ca2c8355b
3 changed files with 129 additions and 3 deletions
  1. 77 0
      src/dtlssrtptransport.cpp
  2. 48 0
      src/dtlssrtptransport.hpp
  3. 4 3
      src/dtlstransport.hpp

+ 77 - 0
src/dtlssrtptransport.cpp

@@ -0,0 +1,77 @@
+/**
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "dtlssrtptransport.hpp"
+
+#include <exception>
+
+using std::shared_ptr;
+
+namespace rtc {
+
+DtlsSrtpTransport::DtlsSrtpTransport(std::shared_ptr<IceTransport> lower,
+                                     shared_ptr<Certificate> certificate,
+                                     verifier_callback verifierCallback,
+                                     message_callback recvCallback,
+                                     state_callback stateChangeCallback)
+    : DtlsTransport(lower, certificate, std::move(verifierCallback),
+                    std::move(stateChangeCallback)) {
+	onRecv(recvCallback);
+
+	// TODO: global init
+	srtp_init();
+
+	PLOG_DEBUG << "Initializing SRTP transport";
+
+	mPolicy = {};
+	srtp_crypto_policy_set_rtp_default(&mPolicy.rtp);
+	srtp_crypto_policy_set_rtcp_default(&mPolicy.rtcp);
+}
+
+DtlsSrtpTransport::~DtlsSrtpTransport() { stop(); }
+
+void DtlsSrtpTransport::stop() {
+	Transport::stop();
+	onRecv(nullptr);
+
+	// TODO: global cleanup
+	srtp_shutdown();
+}
+
+bool DtlsSrtpTransport::send(message_ptr message) {
+	if (!message)
+		return false;
+
+	PLOG_VERBOSE << "Send size=" << message->size();
+
+	// TODO
+	return false;
+}
+
+void DtlsSrtpTransport::incoming(message_ptr message) {
+	//
+}
+
+void DtlsSrtpTransport::postHandshake() {
+	// TODO: derive keys
+
+	mPolicy.ssrc = mSsrc;
+	mPolicy.key = key;
+}
+
+} // namespace rtc

+ 48 - 0
src/dtlssrtptransport.hpp

@@ -0,0 +1,48 @@
+/**
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_DTLS_SRTP_TRANSPORT_H
+#define RTC_DTLS_SRTP_TRANSPORT_H
+
+#include "dtlstransport.hpp"
+#include "include.hpp"
+
+#include <srtp2/srtp.h>
+
+namespace rtc {
+
+class DtlsSrtpTransport final : public DtlsTransport {
+public:
+	DtlsSrtpTransport(std::shared_ptr<IceTransport> lower, std::shared_ptr<Certificate> certificate,
+	                  verifier_callback verifierCallback, message_callback recvCallback,
+	                  state_callback stateChangeCallback);
+	~DtlsSrtpTransport();
+
+	void stop() override;
+	bool send(message_ptr message) override;
+
+private:
+	void incoming(message_ptr message) override;
+
+	srtp_t mSrtp;
+	srtp_policy_t mPolicy;
+};
+
+} // namespace rtc
+
+#endif

+ 4 - 3
src/dtlstransport.hpp

@@ -52,11 +52,12 @@ public:
 	              verifier_callback verifierCallback, state_callback stateChangeCallback);
 	~DtlsTransport();
 
-	bool stop() override;
-	bool send(message_ptr message) override; // false if dropped
+	virtual bool stop() override;
+	virtual bool send(message_ptr message) override; // false if dropped
 
 private:
-	void incoming(message_ptr message) override;
+	virtual void incoming(message_ptr message) override;
+
 	void runRecvLoop();
 
 	const std::shared_ptr<Certificate> mCertificate;