mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
RTC: Refine code
This commit is contained in:
parent
9416fddd2b
commit
306a0121ff
2 changed files with 35 additions and 34 deletions
|
@ -386,39 +386,6 @@ srs_error_t ISrsDtlsImpl::on_dtls(char* data, int nb_data)
|
|||
return err;
|
||||
}
|
||||
|
||||
const int SRTP_MASTER_KEY_KEY_LEN = 16;
|
||||
const int SRTP_MASTER_KEY_SALT_LEN = 14;
|
||||
srs_error_t ISrsDtlsImpl::get_srtp_key(std::string& recv_key, std::string& send_key)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
unsigned char material[SRTP_MASTER_KEY_LEN * 2] = {0}; // client(SRTP_MASTER_KEY_KEY_LEN + SRTP_MASTER_KEY_SALT_LEN) + server
|
||||
static const string dtls_srtp_lable = "EXTRACTOR-dtls_srtp";
|
||||
if (!SSL_export_keying_material(dtls, material, sizeof(material), dtls_srtp_lable.c_str(), dtls_srtp_lable.size(), NULL, 0, 0)) {
|
||||
return srs_error_new(ERROR_RTC_SRTP_INIT, "SSL export key r0=%u", ERR_get_error());
|
||||
}
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
std::string client_master_key(reinterpret_cast<char*>(material), SRTP_MASTER_KEY_KEY_LEN);
|
||||
offset += SRTP_MASTER_KEY_KEY_LEN;
|
||||
std::string server_master_key(reinterpret_cast<char*>(material + offset), SRTP_MASTER_KEY_KEY_LEN);
|
||||
offset += SRTP_MASTER_KEY_KEY_LEN;
|
||||
std::string client_master_salt(reinterpret_cast<char*>(material + offset), SRTP_MASTER_KEY_SALT_LEN);
|
||||
offset += SRTP_MASTER_KEY_SALT_LEN;
|
||||
std::string server_master_salt(reinterpret_cast<char*>(material + offset), SRTP_MASTER_KEY_SALT_LEN);
|
||||
|
||||
if (is_dtls_client()) {
|
||||
recv_key = server_master_key + server_master_salt;
|
||||
send_key = client_master_key + client_master_salt;
|
||||
} else {
|
||||
recv_key = client_master_key + client_master_salt;
|
||||
send_key = server_master_key + server_master_salt;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t ISrsDtlsImpl::do_on_dtls(char* data, int nb_data)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
@ -536,6 +503,39 @@ void ISrsDtlsImpl::state_trace(uint8_t* data, int length, bool incoming, int r0,
|
|||
r0, r1, length, content_type, size, handshake_type);
|
||||
}
|
||||
|
||||
const int SRTP_MASTER_KEY_KEY_LEN = 16;
|
||||
const int SRTP_MASTER_KEY_SALT_LEN = 14;
|
||||
srs_error_t ISrsDtlsImpl::get_srtp_key(std::string& recv_key, std::string& send_key)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
unsigned char material[SRTP_MASTER_KEY_LEN * 2] = {0}; // client(SRTP_MASTER_KEY_KEY_LEN + SRTP_MASTER_KEY_SALT_LEN) + server
|
||||
static const string dtls_srtp_lable = "EXTRACTOR-dtls_srtp";
|
||||
if (!SSL_export_keying_material(dtls, material, sizeof(material), dtls_srtp_lable.c_str(), dtls_srtp_lable.size(), NULL, 0, 0)) {
|
||||
return srs_error_new(ERROR_RTC_SRTP_INIT, "SSL export key r0=%u", ERR_get_error());
|
||||
}
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
std::string client_master_key(reinterpret_cast<char*>(material), SRTP_MASTER_KEY_KEY_LEN);
|
||||
offset += SRTP_MASTER_KEY_KEY_LEN;
|
||||
std::string server_master_key(reinterpret_cast<char*>(material + offset), SRTP_MASTER_KEY_KEY_LEN);
|
||||
offset += SRTP_MASTER_KEY_KEY_LEN;
|
||||
std::string client_master_salt(reinterpret_cast<char*>(material + offset), SRTP_MASTER_KEY_SALT_LEN);
|
||||
offset += SRTP_MASTER_KEY_SALT_LEN;
|
||||
std::string server_master_salt(reinterpret_cast<char*>(material + offset), SRTP_MASTER_KEY_SALT_LEN);
|
||||
|
||||
if (is_dtls_client()) {
|
||||
recv_key = server_master_key + server_master_salt;
|
||||
send_key = client_master_key + client_master_salt;
|
||||
} else {
|
||||
recv_key = client_master_key + client_master_salt;
|
||||
send_key = server_master_key + server_master_salt;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
SrsDtlsClientImpl::SrsDtlsClientImpl(ISrsDtlsCallback* callback) : ISrsDtlsImpl(callback)
|
||||
{
|
||||
trd = NULL;
|
||||
|
|
|
@ -129,11 +129,12 @@ public:
|
|||
virtual srs_error_t initialize(std::string version);
|
||||
virtual srs_error_t start_active_handshake() = 0;
|
||||
virtual srs_error_t on_dtls(char* data, int nb_data);
|
||||
srs_error_t get_srtp_key(std::string& recv_key, std::string& send_key);
|
||||
protected:
|
||||
srs_error_t do_on_dtls(char* data, int nb_data);
|
||||
srs_error_t do_handshake();
|
||||
void state_trace(uint8_t* data, int length, bool incoming, int r0, int r1, bool cache, bool arq);
|
||||
public:
|
||||
srs_error_t get_srtp_key(std::string& recv_key, std::string& send_key);
|
||||
protected:
|
||||
virtual void on_ssl_out_data(uint8_t*& data, int& size, bool& cached) = 0;
|
||||
virtual srs_error_t on_final_out_data(uint8_t* data, int size) = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue