提交 05d5447c authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Transport: Expose `dtlsLocalFingerprints` (fixes #14)

上级 ba0f3a3e
...@@ -55,6 +55,11 @@ class Transport extends EventEmitter ...@@ -55,6 +55,11 @@ class Transport extends EventEmitter
return this._data.iceLocalCandidates; return this._data.iceLocalCandidates;
} }
get dtlsLocalFingerprints()
{
return this._data.dtlsLocalFingerprints;
}
/** /**
* Close the Transport * Close the Transport
*/ */
......
...@@ -62,18 +62,11 @@ namespace RTC ...@@ -62,18 +62,11 @@ namespace RTC
X509* DTLSAgent::certificate = nullptr; X509* DTLSAgent::certificate = nullptr;
EVP_PKEY* DTLSAgent::privateKey = nullptr; EVP_PKEY* DTLSAgent::privateKey = nullptr;
SSL_CTX* DTLSAgent::sslCtx = nullptr; SSL_CTX* DTLSAgent::sslCtx = nullptr;
DTLSAgent::Fingerprints DTLSAgent::fingerprints Json::Value DTLSAgent::localFingerprints = Json::Value(Json::arrayValue);
{
{ RTC::FingerprintHash::SHA1, "" },
{ RTC::FingerprintHash::SHA224, "" },
{ RTC::FingerprintHash::SHA256, "" },
{ RTC::FingerprintHash::SHA384, "" },
{ RTC::FingerprintHash::SHA512, "" }
};
DTLSAgent::SRTPProfiles DTLSAgent::srtpProfiles = DTLSAgent::SRTPProfiles DTLSAgent::srtpProfiles =
{ {
{ RTC::SRTPProfile::AES_CM_128_HMAC_SHA1_80, "SRTP_AES128_CM_SHA1_80" }, { RTC::SRTPSession::SRTPProfile::AES_CM_128_HMAC_SHA1_80, "SRTP_AES128_CM_SHA1_80" },
{ RTC::SRTPProfile::AES_CM_128_HMAC_SHA1_32, "SRTP_AES128_CM_SHA1_32" } { RTC::SRTPSession::SRTPProfile::AES_CM_128_HMAC_SHA1_32, "SRTP_AES128_CM_SHA1_32" }
}; };
MS_BYTE DTLSAgent::sslReadBuffer[MS_SSL_READ_BUFFER_SIZE]; MS_BYTE DTLSAgent::sslReadBuffer[MS_SSL_READ_BUFFER_SIZE];
...@@ -108,12 +101,11 @@ namespace RTC ...@@ -108,12 +101,11 @@ namespace RTC
SSL_CTX_free(DTLSAgent::sslCtx); SSL_CTX_free(DTLSAgent::sslCtx);
} }
const std::string& DTLSAgent::GetFingerprint(RTC::FingerprintHash hash) Json::Value& DTLSAgent::GetLocalFingerprints()
{ {
MS_TRACE(); MS_TRACE();
// NOTE: Don't check if the given hash is supported (should give a compilation error). return DTLSAgent::localFingerprints;
return DTLSAgent::fingerprints.at(hash);
} }
void DTLSAgent::GenerateCertificateAndPrivateKey() void DTLSAgent::GenerateCertificateAndPrivateKey()
...@@ -415,37 +407,47 @@ namespace RTC ...@@ -415,37 +407,47 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
for (auto it = DTLSAgent::fingerprints.begin(); it != DTLSAgent::fingerprints.end(); ++it) static const std::vector<FingerprintHash> hashes =
{
FingerprintHash::SHA1,
FingerprintHash::SHA224,
FingerprintHash::SHA256,
FingerprintHash::SHA384,
FingerprintHash::SHA512
};
for (auto hash : hashes)
{ {
RTC::FingerprintHash hash = it->first;
std::string hash_str; std::string hash_str;
unsigned char binary_fingerprint[EVP_MAX_MD_SIZE]; unsigned char binary_fingerprint[EVP_MAX_MD_SIZE];
unsigned int size = 0; unsigned int size = 0;
char hex_fingerprint[(EVP_MAX_MD_SIZE * 3) + 1]; char hex_fingerprint[(EVP_MAX_MD_SIZE * 2) + 1];
char hex_fingerprint_sdp[(EVP_MAX_MD_SIZE * 3) + 1];
const EVP_MD* hash_function; const EVP_MD* hash_function;
int ret; int ret;
Json::Value jsonFingerprint;
switch (hash) switch (hash)
{ {
case RTC::FingerprintHash::SHA1: case FingerprintHash::SHA1:
hash_function = EVP_sha1(); hash_function = EVP_sha1();
hash_str = "SHA-1"; hash_str = "sha-1";
break; break;
case RTC::FingerprintHash::SHA224: case FingerprintHash::SHA224:
hash_function = EVP_sha224(); hash_function = EVP_sha224();
hash_str = "SHA-224"; hash_str = "sha-224";
break; break;
case RTC::FingerprintHash::SHA256: case FingerprintHash::SHA256:
hash_function = EVP_sha256(); hash_function = EVP_sha256();
hash_str = "SHA-256"; hash_str = "sha-256";
break; break;
case RTC::FingerprintHash::SHA384: case FingerprintHash::SHA384:
hash_function = EVP_sha384(); hash_function = EVP_sha384();
hash_str = "SHA-384"; hash_str = "sha-384";
break; break;
case RTC::FingerprintHash::SHA512: case FingerprintHash::SHA512:
hash_function = EVP_sha512(); hash_function = EVP_sha512();
hash_str = "SHA-512"; hash_str = "sha-512";
break; break;
default: default:
MS_ABORT("unknown hash"); MS_ABORT("unknown hash");
...@@ -458,18 +460,28 @@ namespace RTC ...@@ -458,18 +460,28 @@ namespace RTC
MS_THROW_ERROR("Fingerprints generation failed"); MS_THROW_ERROR("Fingerprints generation failed");
} }
// Convert to hexadecimal format (NOTE: %.2X uses uppercase letters). // Convert to hexadecimal format in lowecase without colons.
for (unsigned int i = 0; i < size; i++) for (unsigned int i = 0; i < size; i++)
{ {
std::sprintf(hex_fingerprint + (i * 3), "%.2X:", binary_fingerprint[i]); std::sprintf(hex_fingerprint + (i * 2), "%.2x", binary_fingerprint[i]);
} }
// NOTE: The -1 removes the last ":". hex_fingerprint[size * 2] = '\0';
hex_fingerprint[(size * 3) - 1] = '\0';
MS_DEBUG("%-7s fingerprint: %s", hash_str.c_str(), hex_fingerprint); MS_DEBUG("%-7s fingerprint: %s", hash_str.c_str(), hex_fingerprint);
// Store in the map. // Convert to hexadecimal SDP format in uppercase with colons.
DTLSAgent::fingerprints[hash].assign(hex_fingerprint); for (unsigned int i = 0; i < size; i++)
{
std::sprintf(hex_fingerprint_sdp + (i * 3), "%.2X:", binary_fingerprint[i]);
}
// NOTE: The -1 removes the last ":".
hex_fingerprint_sdp[(size * 3) - 1] = '\0';
// Store in the JSON.
jsonFingerprint["algorithm"] = hash_str;
jsonFingerprint["value"] = hex_fingerprint;
jsonFingerprint["valueForSDP"] = hex_fingerprint_sdp;
DTLSAgent::localFingerprints.append(jsonFingerprint);
} }
} }
...@@ -545,7 +557,7 @@ namespace RTC ...@@ -545,7 +557,7 @@ namespace RTC
} }
} }
void DTLSAgent::Run(RTC::DTLSRole role) void DTLSAgent::Run(DTLSRole role)
{ {
MS_TRACE(); MS_TRACE();
...@@ -557,27 +569,27 @@ namespace RTC ...@@ -557,27 +569,27 @@ namespace RTC
switch (this->role) switch (this->role)
{ {
case RTC::DTLSRole::SERVER: case DTLSRole::SERVER:
SSL_set_accept_state(this->ssl); SSL_set_accept_state(this->ssl);
SSL_do_handshake(this->ssl); SSL_do_handshake(this->ssl);
break; break;
case RTC::DTLSRole::CLIENT: case DTLSRole::CLIENT:
SSL_set_connect_state(this->ssl); SSL_set_connect_state(this->ssl);
SSL_do_handshake(this->ssl); SSL_do_handshake(this->ssl);
SendPendingOutgoingDTLSData(); SendPendingOutgoingDTLSData();
SetTimeout(); SetTimeout();
break; break;
case RTC::DTLSRole::NONE: case DTLSRole::NONE:
MS_ABORT("no valid DTLS role given"); MS_ABORT("no valid DTLS role given");
break; break;
} }
} }
void DTLSAgent::SetRemoteFingerprint(RTC::FingerprintHash hash, std::string& fingerprint) void DTLSAgent::SetRemoteFingerprint(FingerprintHash hash, std::string& fingerprint)
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT(hash != RTC::FingerprintHash::NONE, "no fingerprint hash provided"); MS_ASSERT(hash != FingerprintHash::NONE, "no fingerprint hash provided");
this->remoteFingerprintHash = hash; this->remoteFingerprintHash = hash;
this->remoteFingerprint = fingerprint; this->remoteFingerprint = fingerprint;
...@@ -620,7 +632,7 @@ namespace RTC ...@@ -620,7 +632,7 @@ namespace RTC
SSL_shutdown(this->ssl); SSL_shutdown(this->ssl);
SendPendingOutgoingDTLSData(); SendPendingOutgoingDTLSData();
this->role = RTC::DTLSRole::NONE; this->role = DTLSRole::NONE;
this->isRunning = false; this->isRunning = false;
this->isHandshakeDone = false; this->isHandshakeDone = false;
this->isHandshakeDoneNow = false; this->isHandshakeDoneNow = false;
...@@ -740,7 +752,7 @@ namespace RTC ...@@ -740,7 +752,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
MS_DEBUG("[role: %s | running: %s | handshake done: %s | connected: %s]", MS_DEBUG("[role: %s | running: %s | handshake done: %s | connected: %s]",
(this->role == RTC::DTLSRole::SERVER ? "server" : (this->role == RTC::DTLSRole::CLIENT ? "client" : "none")), (this->role == DTLSRole::SERVER ? "server" : (this->role == DTLSRole::CLIENT ? "client" : "none")),
this->isRunning ? "yes" : "no", this->isRunning ? "yes" : "no",
this->isHandshakeDone ? "yes" : "no", this->isHandshakeDone ? "yes" : "no",
this->isConnected ? "yes" : "no"); this->isConnected ? "yes" : "no");
...@@ -802,7 +814,7 @@ namespace RTC ...@@ -802,7 +814,7 @@ namespace RTC
{ {
bool was_connected = this->isConnected; bool was_connected = this->isConnected;
this->role = RTC::DTLSRole::NONE; this->role = DTLSRole::NONE;
this->isRunning = false; this->isRunning = false;
this->isHandshakeDone = false; this->isHandshakeDone = false;
this->isHandshakeDoneNow = false; this->isHandshakeDoneNow = false;
...@@ -910,23 +922,22 @@ namespace RTC ...@@ -910,23 +922,22 @@ namespace RTC
// If the remote fingerprint is not yet set then do nothing (this method // If the remote fingerprint is not yet set then do nothing (this method
// will be called when the fingerprint is set). // will be called when the fingerprint is set).
if (this->remoteFingerprintHash == RTC::FingerprintHash::NONE) if (this->remoteFingerprintHash == FingerprintHash::NONE)
{ {
MS_DEBUG("remote fingerprint not yet set, waiting for it"); MS_DEBUG("remote fingerprint not yet set, waiting for it");
return; return;
} }
// Validate the remote fingerprint. // Validate the remote fingerprint.
// TODO: uncomment!!! if (!CheckRemoteFingerprint())
// if (!CheckRemoteFingerprint()) {
// { // Notify to the listener.
// // Notify to the listener. this->listener->onDTLSFailed(this);
// this->listener->onDTLSFailed(this);
// // Reset DTLS. // Reset DTLS.
// Reset(); Reset();
// return; return;
// } }
this->isConnected = true; this->isConnected = true;
...@@ -934,10 +945,10 @@ namespace RTC ...@@ -934,10 +945,10 @@ namespace RTC
this->listener->onDTLSConnected(this); this->listener->onDTLSConnected(this);
// Get the negotiated SRTP profile. // Get the negotiated SRTP profile.
RTC::SRTPProfile srtp_profile; RTC::SRTPSession::SRTPProfile srtp_profile;
srtp_profile = GetNegotiatedSRTPProfile(); srtp_profile = GetNegotiatedSRTPProfile();
if (srtp_profile != RTC::SRTPProfile::NONE) if (srtp_profile != RTC::SRTPSession::SRTPProfile::NONE)
{ {
// Extract the SRTP keys (will notify the listener with them). // Extract the SRTP keys (will notify the listener with them).
ExtractSRTPKeys(srtp_profile); ExtractSRTPKeys(srtp_profile);
...@@ -953,7 +964,7 @@ namespace RTC ...@@ -953,7 +964,7 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT(this->remoteFingerprintHash != RTC::FingerprintHash::NONE, "remote fingerprint not set"); MS_ASSERT(this->remoteFingerprintHash != FingerprintHash::NONE, "remote fingerprint not set");
X509* certificate; X509* certificate;
std::string hash_str; std::string hash_str;
...@@ -972,25 +983,25 @@ namespace RTC ...@@ -972,25 +983,25 @@ namespace RTC
switch (this->remoteFingerprintHash) switch (this->remoteFingerprintHash)
{ {
case RTC::FingerprintHash::SHA1: case FingerprintHash::SHA1:
hash_function = EVP_sha1(); hash_function = EVP_sha1();
hash_str = "SHA-1"; hash_str = "sha-1";
break; break;
case RTC::FingerprintHash::SHA224: case FingerprintHash::SHA224:
hash_function = EVP_sha224(); hash_function = EVP_sha224();
hash_str = "SHA-224"; hash_str = "sha-224";
break; break;
case RTC::FingerprintHash::SHA256: case FingerprintHash::SHA256:
hash_function = EVP_sha256(); hash_function = EVP_sha256();
hash_str = "SHA-256"; hash_str = "sha-256";
break; break;
case RTC::FingerprintHash::SHA384: case FingerprintHash::SHA384:
hash_function = EVP_sha384(); hash_function = EVP_sha384();
hash_str = "SHA-384"; hash_str = "sha-384";
break; break;
case RTC::FingerprintHash::SHA512: case FingerprintHash::SHA512:
hash_function = EVP_sha512(); hash_function = EVP_sha512();
hash_str = "SHA-512"; hash_str = "sha-512";
break; break;
default: default:
MS_ABORT("unknown hash"); MS_ABORT("unknown hash");
...@@ -1025,11 +1036,11 @@ namespace RTC ...@@ -1025,11 +1036,11 @@ namespace RTC
} }
inline inline
RTC::SRTPProfile DTLSAgent::GetNegotiatedSRTPProfile() RTC::SRTPSession::SRTPProfile DTLSAgent::GetNegotiatedSRTPProfile()
{ {
MS_TRACE(); MS_TRACE();
RTC::SRTPProfile negotiated_srtp_profile = RTC::SRTPProfile::NONE; RTC::SRTPSession::SRTPProfile negotiated_srtp_profile = RTC::SRTPSession::SRTPProfile::NONE;
// Ensure that the SRTP profile has been negotiated. // Ensure that the SRTP profile has been negotiated.
SRTP_PROTECTION_PROFILE* ssl_srtp_profile = SSL_get_selected_srtp_profile(this->ssl); SRTP_PROTECTION_PROFILE* ssl_srtp_profile = SSL_get_selected_srtp_profile(this->ssl);
...@@ -1049,13 +1060,13 @@ namespace RTC ...@@ -1049,13 +1060,13 @@ namespace RTC
negotiated_srtp_profile = profile_entry->profile; negotiated_srtp_profile = profile_entry->profile;
} }
} }
MS_ASSERT(negotiated_srtp_profile != RTC::SRTPProfile::NONE, "chosen SRTP profile is not an available one"); MS_ASSERT(negotiated_srtp_profile != RTC::SRTPSession::SRTPProfile::NONE, "chosen SRTP profile is not an available one");
return negotiated_srtp_profile; return negotiated_srtp_profile;
} }
inline inline
void DTLSAgent::ExtractSRTPKeys(RTC::SRTPProfile srtp_profile) void DTLSAgent::ExtractSRTPKeys(RTC::SRTPSession::SRTPProfile srtp_profile)
{ {
MS_TRACE(); MS_TRACE();
...@@ -1073,19 +1084,19 @@ namespace RTC ...@@ -1073,19 +1084,19 @@ namespace RTC
switch (this->role) switch (this->role)
{ {
case RTC::DTLSRole::SERVER: case DTLSRole::SERVER:
srtp_remote_key = srtp_material; srtp_remote_key = srtp_material;
srtp_local_key = srtp_remote_key + MS_SRTP_MASTER_KEY_LENGTH; srtp_local_key = srtp_remote_key + MS_SRTP_MASTER_KEY_LENGTH;
srtp_remote_salt = srtp_local_key + MS_SRTP_MASTER_KEY_LENGTH; srtp_remote_salt = srtp_local_key + MS_SRTP_MASTER_KEY_LENGTH;
srtp_local_salt = srtp_remote_salt + MS_SRTP_MASTER_SALT_LENGTH; srtp_local_salt = srtp_remote_salt + MS_SRTP_MASTER_SALT_LENGTH;
break; break;
case RTC::DTLSRole::CLIENT: case DTLSRole::CLIENT:
srtp_local_key = srtp_material; srtp_local_key = srtp_material;
srtp_remote_key = srtp_local_key + MS_SRTP_MASTER_KEY_LENGTH; srtp_remote_key = srtp_local_key + MS_SRTP_MASTER_KEY_LENGTH;
srtp_local_salt = srtp_remote_key + MS_SRTP_MASTER_KEY_LENGTH; srtp_local_salt = srtp_remote_key + MS_SRTP_MASTER_KEY_LENGTH;
srtp_remote_salt = srtp_local_salt + MS_SRTP_MASTER_SALT_LENGTH; srtp_remote_salt = srtp_local_salt + MS_SRTP_MASTER_SALT_LENGTH;
break; break;
case RTC::DTLSRole::NONE: case DTLSRole::NONE:
MS_ABORT("no DTLS role set"); MS_ABORT("no DTLS role set");
break; break;
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论