2020-03-06 15:01:48 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2020 Winlin
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-05-11 04:07:55 +00:00
|
|
|
#ifndef SRS_APP_RTC_DTLS_HPP
|
|
|
|
#define SRS_APP_RTC_DTLS_HPP
|
2020-03-06 15:01:48 +00:00
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2020-04-03 07:03:09 +00:00
|
|
|
class SrsRequest;
|
|
|
|
|
2020-03-06 15:01:48 +00:00
|
|
|
#include <openssl/ssl.h>
|
2020-06-25 06:50:14 +00:00
|
|
|
#include <srtp2/srtp.h>
|
2020-03-06 15:01:48 +00:00
|
|
|
|
2020-06-24 12:03:21 +00:00
|
|
|
class SrsDtlsCertificate
|
2020-03-06 15:01:48 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string fingerprint;
|
2020-06-24 12:03:21 +00:00
|
|
|
bool ecdsa_mode;
|
2020-06-24 09:09:26 +00:00
|
|
|
X509* dtls_cert;
|
|
|
|
EVP_PKEY* dtls_pkey;
|
|
|
|
EC_KEY* eckey;
|
2020-06-24 12:03:21 +00:00
|
|
|
public:
|
|
|
|
SrsDtlsCertificate();
|
|
|
|
virtual ~SrsDtlsCertificate();
|
|
|
|
public:
|
|
|
|
// Initialize DTLS certificate.
|
|
|
|
srs_error_t initialize();
|
|
|
|
// dtls_cert
|
|
|
|
X509* get_cert();
|
|
|
|
// public key
|
|
|
|
EVP_PKEY* get_public_key();
|
|
|
|
// ECDSA key
|
|
|
|
EC_KEY* get_ecdsa_key();
|
|
|
|
// certificate fingerprint
|
|
|
|
std::string get_fingerprint();
|
|
|
|
// whether is ecdsa
|
|
|
|
bool is_ecdsa();
|
|
|
|
};
|
|
|
|
|
2020-06-24 12:21:36 +00:00
|
|
|
// @global config object.
|
|
|
|
extern SrsDtlsCertificate* _srs_rtc_dtls_certificate;
|
2020-06-24 12:03:21 +00:00
|
|
|
|
2020-06-25 04:03:21 +00:00
|
|
|
class ISrsDtlsCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ISrsDtlsCallback();
|
|
|
|
virtual ~ISrsDtlsCallback();
|
|
|
|
public:
|
|
|
|
// DTLS handshake done callback.
|
|
|
|
virtual srs_error_t on_dtls_handshake_done() = 0;
|
|
|
|
// DTLS receive application data callback.
|
|
|
|
virtual srs_error_t on_dtls_application_data(const char* data, const int len) = 0;
|
|
|
|
// DTLS write dtls data.
|
|
|
|
virtual srs_error_t write_dtls_data(void* data, int size) = 0;
|
|
|
|
};
|
|
|
|
|
2020-06-24 12:03:21 +00:00
|
|
|
class SrsDtls
|
|
|
|
{
|
2020-06-25 04:03:21 +00:00
|
|
|
private:
|
|
|
|
SSL_CTX* dtls_ctx;
|
|
|
|
SSL* dtls;
|
|
|
|
BIO* bio_in;
|
|
|
|
BIO* bio_out;
|
|
|
|
|
|
|
|
ISrsDtlsCallback* callback;
|
|
|
|
|
|
|
|
bool handshake_done;
|
2020-06-24 12:21:36 +00:00
|
|
|
public:
|
2020-06-25 04:03:21 +00:00
|
|
|
SrsDtls(ISrsDtlsCallback* callback);
|
2020-03-06 15:01:48 +00:00
|
|
|
virtual ~SrsDtls();
|
|
|
|
public:
|
2020-06-25 04:03:21 +00:00
|
|
|
srs_error_t initialize(SrsRequest* r);
|
|
|
|
srs_error_t do_handshake();
|
|
|
|
srs_error_t on_dtls(char* data, int nb_data);
|
2020-06-25 07:01:05 +00:00
|
|
|
srs_error_t get_srtp_key(std::string& recv_key, std::string& send_key);
|
2020-06-25 04:03:21 +00:00
|
|
|
private:
|
|
|
|
SSL_CTX* build_dtls_ctx();
|
|
|
|
srs_error_t handshake();
|
2020-03-06 15:01:48 +00:00
|
|
|
};
|
|
|
|
|
2020-06-25 06:50:14 +00:00
|
|
|
class SrsSRTP
|
|
|
|
{
|
|
|
|
private:
|
2020-06-25 12:47:17 +00:00
|
|
|
srtp_t recv_ctx_;
|
|
|
|
srtp_t send_ctx_;
|
2020-06-25 06:50:14 +00:00
|
|
|
public:
|
|
|
|
SrsSRTP();
|
|
|
|
virtual ~SrsSRTP();
|
|
|
|
public:
|
2020-06-25 12:47:17 +00:00
|
|
|
// Intialize srtp context with recv_key and send_key.
|
|
|
|
srs_error_t initialize(std::string recv_key, std::string send_key);
|
2020-06-25 06:50:14 +00:00
|
|
|
public:
|
2020-06-25 12:47:17 +00:00
|
|
|
// Encrypt the input plaintext to output cipher with nb_cipher bytes.
|
|
|
|
// @remark Note that the nb_cipher is the size of input plaintext, and
|
|
|
|
// it also is the length of output cipher when return.
|
|
|
|
srs_error_t protect_rtp(const char* plaintext, char* cipher, int& nb_cipher);
|
|
|
|
srs_error_t protect_rtcp(const char* plaintext, char* cipher, int& nb_cipher);
|
|
|
|
// Encrypt the input rtp_hdr with *len_ptr bytes.
|
|
|
|
// @remark the input plaintext and out cipher reuse rtp_hdr.
|
2020-06-25 06:50:14 +00:00
|
|
|
srs_error_t protect_rtp2(void* rtp_hdr, int* len_ptr);
|
2020-06-25 12:47:17 +00:00
|
|
|
// Decrypt the input cipher to output cipher with nb_cipher bytes.
|
|
|
|
// @remark Note that the nb_plaintext is the size of input cipher, and
|
|
|
|
// it also is the length of output plaintext when return.
|
|
|
|
srs_error_t unprotect_rtp(const char* cipher, char* plaintext, int& nb_plaintext);
|
|
|
|
srs_error_t unprotect_rtcp(const char* cipher, char* plaintext, int& nb_plaintext);
|
2020-06-25 06:50:14 +00:00
|
|
|
};
|
|
|
|
|
2020-03-06 15:01:48 +00:00
|
|
|
#endif
|