提交 4a22ba54 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Work in DTLSAgent.

上级 05d5447c
...@@ -2,26 +2,43 @@ ...@@ -2,26 +2,43 @@
#define MS_RTC_DTLS_AGENT_H #define MS_RTC_DTLS_AGENT_H
#include "common.h" #include "common.h"
#include "RTC/SRTPSession.h"
#include "handles/Timer.h" #include "handles/Timer.h"
#include "RTC/DTLSRole.h"
#include "RTC/FingerprintHash.h"
#include "RTC/SRTPProfile.h"
#include <string> #include <string>
#include <map>
#include <vector> #include <vector>
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/bio.h> #include <openssl/bio.h>
#include <openssl/x509.h> #include <openssl/x509.h>
#include <json/json.h>
namespace RTC namespace RTC
{ {
class DTLSAgent : class DTLSAgent :
public Timer::Listener public Timer::Listener
{ {
public:
enum class DTLSRole
{
NONE = 0,
SERVER = 1,
CLIENT
};
public:
enum class FingerprintHash
{
NONE = 0,
SHA1 = 1,
SHA224,
SHA256,
SHA384,
SHA512
};
private: private:
struct SrtpProfileMapEntry struct SrtpProfileMapEntry
{ {
SRTPProfile profile; RTC::SRTPSession::SRTPProfile profile;
const char* name; const char* name;
}; };
...@@ -37,15 +54,15 @@ namespace RTC ...@@ -37,15 +54,15 @@ namespace RTC
virtual void onDTLSConnected(DTLSAgent* dtlsAgent) = 0; virtual void onDTLSConnected(DTLSAgent* dtlsAgent) = 0;
virtual void onDTLSDisconnected(DTLSAgent* dtlsAgent) = 0; virtual void onDTLSDisconnected(DTLSAgent* dtlsAgent) = 0;
virtual void onDTLSFailed(DTLSAgent* dtlsAgent) = 0; virtual void onDTLSFailed(DTLSAgent* dtlsAgent) = 0;
virtual void onSRTPKeyMaterial(DTLSAgent* dtlsAgent, RTC::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len) = 0; virtual void onSRTPKeyMaterial(DTLSAgent* dtlsAgent, RTC::SRTPSession::SRTPProfile srtp_profile, MS_BYTE* srtp_local_key, size_t srtp_local_key_len, MS_BYTE* srtp_remote_key, size_t srtp_remote_key_len) = 0;
virtual void onDTLSApplicationData(DTLSAgent* dtlsAgent, const MS_BYTE* data, size_t len) = 0; virtual void onDTLSApplicationData(DTLSAgent* dtlsAgent, const MS_BYTE* data, size_t len) = 0;
}; };
public: public:
static void ClassInit(); static void ClassInit();
static void ClassDestroy(); static void ClassDestroy();
static Json::Value& GetLocalFingerprints();
static bool IsDTLS(const MS_BYTE* data, size_t len); static bool IsDTLS(const MS_BYTE* data, size_t len);
static const std::string& GetFingerprint(RTC::FingerprintHash hash);
private: private:
static void GenerateCertificateAndPrivateKey(); static void GenerateCertificateAndPrivateKey();
...@@ -57,8 +74,7 @@ namespace RTC ...@@ -57,8 +74,7 @@ namespace RTC
static X509* certificate; static X509* certificate;
static EVP_PKEY* privateKey; static EVP_PKEY* privateKey;
static SSL_CTX* sslCtx; static SSL_CTX* sslCtx;
typedef std::map<RTC::FingerprintHash, std::string> Fingerprints; static Json::Value localFingerprints;
static Fingerprints fingerprints;
typedef std::vector<SrtpProfileMapEntry> SRTPProfiles; typedef std::vector<SrtpProfileMapEntry> SRTPProfiles;
static SRTPProfiles srtpProfiles; static SRTPProfiles srtpProfiles;
static MS_BYTE sslReadBuffer[]; static MS_BYTE sslReadBuffer[];
...@@ -67,8 +83,8 @@ namespace RTC ...@@ -67,8 +83,8 @@ namespace RTC
DTLSAgent(Listener* listener); DTLSAgent(Listener* listener);
virtual ~DTLSAgent(); virtual ~DTLSAgent();
void Run(RTC::DTLSRole role); void Run(DTLSRole role);
void SetRemoteFingerprint(RTC::FingerprintHash hash, std::string& fingerprint); void SetRemoteFingerprint(FingerprintHash hash, std::string& fingerprint);
void Reset(); void Reset();
void Close(); void Close();
void ProcessDTLSData(const MS_BYTE* data, size_t len); void ProcessDTLSData(const MS_BYTE* data, size_t len);
...@@ -83,8 +99,8 @@ namespace RTC ...@@ -83,8 +99,8 @@ namespace RTC
void SetTimeout(); void SetTimeout();
void ProcessHandshake(); void ProcessHandshake();
bool CheckRemoteFingerprint(); bool CheckRemoteFingerprint();
RTC::SRTPProfile GetNegotiatedSRTPProfile(); RTC::SRTPSession::SRTPProfile GetNegotiatedSRTPProfile();
void ExtractSRTPKeys(RTC::SRTPProfile srtp_profile); void ExtractSRTPKeys(RTC::SRTPSession::SRTPProfile srtp_profile);
/* Callbacks fired by OpenSSL events. */ /* Callbacks fired by OpenSSL events. */
public: public:
...@@ -103,8 +119,8 @@ namespace RTC ...@@ -103,8 +119,8 @@ namespace RTC
BIO* sslBioToNetwork = nullptr; // The BIO in which ssl writes. BIO* sslBioToNetwork = nullptr; // The BIO in which ssl writes.
Timer* timer = nullptr; Timer* timer = nullptr;
// Others. // Others.
RTC::DTLSRole role; DTLSRole role = DTLSRole::NONE;
RTC::FingerprintHash remoteFingerprintHash = RTC::FingerprintHash::NONE; FingerprintHash remoteFingerprintHash = FingerprintHash::NONE;
std::string remoteFingerprint; std::string remoteFingerprint;
bool isRunning = false; bool isRunning = false;
bool isHandshakeDone = false; bool isHandshakeDone = false;
......
#ifndef MS_RTC_DTLS_ROLE_H
#define MS_RTC_DTLS_ROLE_H
namespace RTC
{
enum class DTLSRole
{
NONE = 0,
SERVER = 1,
CLIENT
};
}
#endif
#ifndef MS_RTC_FINGERPRINT_HASH_H
#define MS_RTC_FINGERPRINT_HASH_H
namespace RTC
{
enum class FingerprintHash
{
NONE = 0,
SHA1 = 1,
SHA224,
SHA256,
SHA384,
SHA512
};
}
#endif
#ifndef MS_RTC_SRTP_PROFILE_H
#define MS_RTC_SRTP_PROFILE_H
namespace RTC
{
enum class SRTPProfile
{
NONE = 0,
AES_CM_128_HMAC_SHA1_80 = 1,
AES_CM_128_HMAC_SHA1_32
};
}
#endif
...@@ -3,13 +3,20 @@ ...@@ -3,13 +3,20 @@
#include "common.h" #include "common.h"
#include "DepLibSRTP.h" #include "DepLibSRTP.h"
#include "RTC/SRTPProfile.h"
#include <srtp/srtp.h> #include <srtp/srtp.h>
namespace RTC namespace RTC
{ {
class SRTPSession class SRTPSession
{ {
public:
enum class SRTPProfile
{
NONE = 0,
AES_CM_128_HMAC_SHA1_80 = 1,
AES_CM_128_HMAC_SHA1_32
};
public: public:
enum class Type enum class Type
{ {
...@@ -27,7 +34,7 @@ namespace RTC ...@@ -27,7 +34,7 @@ namespace RTC
static MS_BYTE encryptBuffer[]; static MS_BYTE encryptBuffer[];
public: public:
SRTPSession(Type type, RTC::SRTPProfile profile, MS_BYTE* key, size_t key_len); SRTPSession(Type type, SRTPProfile profile, MS_BYTE* key, size_t key_len);
~SRTPSession(); ~SRTPSession();
bool EncryptRTP(const MS_BYTE** data, size_t* len); bool EncryptRTP(const MS_BYTE** data, size_t* len);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "common.h" #include "common.h"
#include "RTC/IceCandidate.h" #include "RTC/IceCandidate.h"
#include "RTC/IceServer.h" #include "RTC/IceServer.h"
#include "RTC/DTLSAgent.h"
#include "RTC/STUNMessage.h" #include "RTC/STUNMessage.h"
#include "RTC/TransportSource.h" #include "RTC/TransportSource.h"
#include "RTC/UDPSocket.h" #include "RTC/UDPSocket.h"
......
#define MS_CLASS "RTC::Peer" #define MS_CLASS "RTC::Peer"
#include "RTC/Peer.h" #include "RTC/Peer.h"
#include "RTC/DTLSRole.h"
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
......
...@@ -56,7 +56,7 @@ namespace RTC ...@@ -56,7 +56,7 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
SRTPSession::SRTPSession(Type type, RTC::SRTPProfile profile, MS_BYTE* key, size_t key_len) SRTPSession::SRTPSession(Type type, SRTPProfile profile, MS_BYTE* key, size_t key_len)
{ {
MS_TRACE(); MS_TRACE();
...@@ -68,11 +68,11 @@ namespace RTC ...@@ -68,11 +68,11 @@ namespace RTC
switch (profile) switch (profile)
{ {
case RTC::SRTPProfile::AES_CM_128_HMAC_SHA1_80: case SRTPProfile::AES_CM_128_HMAC_SHA1_80:
crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp); crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp);
crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp);
break; break;
case RTC::SRTPProfile::AES_CM_128_HMAC_SHA1_32: case SRTPProfile::AES_CM_128_HMAC_SHA1_32:
crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtp); crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtp);
crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); // NOTE: Must be 80 for RTCP!. crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); // NOTE: Must be 80 for RTCP!.
break; break;
......
...@@ -235,24 +235,30 @@ namespace RTC ...@@ -235,24 +235,30 @@ namespace RTC
static const Json::StaticString k_iceLocalCandidates("iceLocalCandidates"); static const Json::StaticString k_iceLocalCandidates("iceLocalCandidates");
static const Json::StaticString v_RTP("RTP"); static const Json::StaticString v_RTP("RTP");
static const Json::StaticString v_RTCP("RTCP"); static const Json::StaticString v_RTCP("RTCP");
static const Json::StaticString k_dtlsLocalFingerprints("dtlsLocalFingerprints");
Json::Value data; Json::Value data;
// Add `iceComponent`.
if (this->iceComponent == IceComponent::RTP) if (this->iceComponent == IceComponent::RTP)
data[k_iceComponent] = v_RTP; data[k_iceComponent] = v_RTP;
else else
data[k_iceComponent] = v_RTCP; data[k_iceComponent] = v_RTCP;
// Add `iceLocalParameters`.
data[k_iceLocalParameters][k_usernameFragment] = this->iceServer->GetUsernameFragment(); data[k_iceLocalParameters][k_usernameFragment] = this->iceServer->GetUsernameFragment();
data[k_iceLocalParameters][k_password] = this->iceServer->GetPassword(); data[k_iceLocalParameters][k_password] = this->iceServer->GetPassword();
// Add `iceLocalCandidates`.
data[k_iceLocalCandidates] = Json::arrayValue; data[k_iceLocalCandidates] = Json::arrayValue;
for (auto iceCandidate : this->iceLocalCandidates) for (auto iceCandidate : this->iceLocalCandidates)
{ {
data[k_iceLocalCandidates].append(iceCandidate.toJson()); data[k_iceLocalCandidates].append(iceCandidate.toJson());
} }
// Add `dtlsLocalFingerprints`.
data[k_dtlsLocalFingerprints] = RTC::DTLSAgent::GetLocalFingerprints();
return data; return data;
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论