2012-10-03 00:04:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
|
|
|
|
#ifndef transportlayerdtls_h__
|
|
|
|
#define transportlayerdtls_h__
|
|
|
|
|
|
|
|
#include <queue>
|
2015-04-01 21:21:06 +03:00
|
|
|
#include <set>
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
#include "sigslot.h"
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2016-03-02 23:28:27 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2017-02-04 02:01:05 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "ScopedNSSTypes.h"
|
|
|
|
#include "m_cpp_utils.h"
|
|
|
|
#include "dtlsidentity.h"
|
|
|
|
#include "transportlayer.h"
|
2018-08-23 09:03:00 +03:00
|
|
|
#include "ssl.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2018-09-11 06:58:34 +03:00
|
|
|
// RFC 5764 (we don't support the NULL cipher)
|
|
|
|
static const uint16_t kDtlsSrtpAes128CmHmacSha1_80 = 0x0001;
|
|
|
|
static const uint16_t kDtlsSrtpAes128CmHmacSha1_32 = 0x0002;
|
|
|
|
// RFC 7714
|
|
|
|
static const uint16_t kDtlsSrtpAeadAes128Gcm = 0x0007;
|
|
|
|
static const uint16_t kDtlsSrtpAeadAes256Gcm = 0x0008;
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
struct Packet;
|
|
|
|
|
|
|
|
class TransportLayerNSPRAdapter {
|
|
|
|
public:
|
2014-08-16 01:41:29 +04:00
|
|
|
explicit TransportLayerNSPRAdapter(TransportLayer* output)
|
2015-07-15 22:23:10 +03:00
|
|
|
: output_(output), input_(), enabled_(true) {}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2018-05-10 01:13:35 +03:00
|
|
|
void PacketReceived(MediaPacket& packet);
|
2014-08-04 23:35:00 +04:00
|
|
|
int32_t Recv(void* buf, int32_t buflen);
|
2012-10-03 00:04:58 +04:00
|
|
|
int32_t Write(const void* buf, int32_t length);
|
2015-07-15 22:23:10 +03:00
|
|
|
void SetEnabled(bool enabled) { enabled_ = enabled; }
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_ASSIGN(TransportLayerNSPRAdapter);
|
|
|
|
|
|
|
|
TransportLayer* output_;
|
2018-05-10 01:13:35 +03:00
|
|
|
std::queue<MediaPacket*> input_;
|
2015-07-15 22:23:10 +03:00
|
|
|
bool enabled_;
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TransportLayerDtls final : public TransportLayer {
|
2012-10-03 00:04:58 +04:00
|
|
|
public:
|
2018-08-23 09:03:00 +03:00
|
|
|
TransportLayerDtls() = default;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
virtual ~TransportLayerDtls();
|
|
|
|
|
|
|
|
enum Role { CLIENT, SERVER };
|
|
|
|
enum Verification { VERIFY_UNSET, VERIFY_ALLOW_ALL, VERIFY_DIGEST };
|
|
|
|
|
|
|
|
// DTLS-specific operations
|
|
|
|
void SetRole(Role role) { role_ = role; }
|
|
|
|
Role role() { return role_; }
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void SetIdentity(const RefPtr<DtlsIdentity>& identity) {
|
2012-10-03 00:04:58 +04:00
|
|
|
identity_ = identity;
|
|
|
|
}
|
2015-04-01 21:21:06 +03:00
|
|
|
nsresult SetAlpn(const std::set<std::string>& allowedAlpn,
|
|
|
|
const std::string& alpnDefault);
|
|
|
|
const std::string& GetNegotiatedAlpn() const { return alpn_; }
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
nsresult SetVerificationAllowAll();
|
2019-01-29 17:56:19 +03:00
|
|
|
|
|
|
|
nsresult SetVerificationDigest(const DtlsDigest& digest);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2014-08-04 19:49:00 +04:00
|
|
|
nsresult GetCipherSuite(uint16_t* cipherSuite) const;
|
|
|
|
|
2018-08-23 09:03:00 +03:00
|
|
|
nsresult SetSrtpCiphers(const std::vector<uint16_t>& ciphers);
|
2014-08-04 19:49:00 +04:00
|
|
|
nsresult GetSrtpCipher(uint16_t* cipher) const;
|
2018-10-16 06:30:57 +03:00
|
|
|
static std::vector<uint16_t> GetDefaultSrtpCiphers();
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
nsresult ExportKeyingMaterial(const std::string& label, bool use_context,
|
|
|
|
const std::string& context, unsigned char* out,
|
|
|
|
unsigned int outlen);
|
|
|
|
|
|
|
|
// Transport layer overrides.
|
2017-02-07 01:44:14 +03:00
|
|
|
nsresult InitInternal() override;
|
|
|
|
void WasInserted() override;
|
2018-05-10 01:13:35 +03:00
|
|
|
TransportResult SendPacket(MediaPacket& packet) override;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Signals
|
|
|
|
void StateChange(TransportLayer* layer, State state);
|
2018-05-10 01:13:35 +03:00
|
|
|
void PacketReceived(TransportLayer* layer, MediaPacket& packet);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2014-08-04 19:50:00 +04:00
|
|
|
// For testing use only. Returns the fd.
|
2016-10-20 03:29:58 +03:00
|
|
|
PRFileDesc* internal_fd() {
|
|
|
|
CheckThread();
|
|
|
|
return ssl_fd_.get();
|
|
|
|
}
|
2014-08-04 19:50:00 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
TRANSPORT_LAYER_ID("dtls")
|
|
|
|
|
2017-02-04 02:01:05 +03:00
|
|
|
protected:
|
|
|
|
void SetState(State state, const char* file, unsigned line) override;
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
private:
|
|
|
|
DISALLOW_COPY_ASSIGN(TransportLayerDtls);
|
|
|
|
|
|
|
|
bool Setup();
|
2018-08-23 09:03:00 +03:00
|
|
|
bool SetupCipherSuites(UniquePRFileDesc& ssl_fd);
|
2016-10-20 03:29:58 +03:00
|
|
|
bool SetupAlpn(UniquePRFileDesc& ssl_fd) const;
|
2018-05-10 01:13:35 +03:00
|
|
|
void GetDecryptedPackets();
|
2012-10-03 00:04:58 +04:00
|
|
|
void Handshake();
|
|
|
|
|
2015-04-01 21:21:06 +03:00
|
|
|
bool CheckAlpn();
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
static SECStatus GetClientAuthDataHook(void* arg, PRFileDesc* fd,
|
|
|
|
CERTDistNames* caNames,
|
|
|
|
CERTCertificate** pRetCert,
|
|
|
|
SECKEYPrivateKey** pRetKey);
|
|
|
|
static SECStatus AuthCertificateHook(void* arg, PRFileDesc* fd,
|
|
|
|
PRBool checksig, PRBool isServer);
|
|
|
|
SECStatus AuthCertificateHook(PRFileDesc* fd, PRBool checksig,
|
|
|
|
PRBool isServer);
|
|
|
|
|
|
|
|
static void TimerCallback(nsITimer* timer, void* arg);
|
|
|
|
|
2019-01-29 17:56:19 +03:00
|
|
|
SECStatus CheckDigest(const DtlsDigest& digest,
|
2016-10-20 03:29:58 +03:00
|
|
|
UniqueCERTCertificate& cert) const;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2017-02-04 02:01:05 +03:00
|
|
|
void RecordHandshakeCompletionTelemetry(TransportLayer::State endState);
|
2018-11-10 23:29:57 +03:00
|
|
|
void RecordTlsTelemetry();
|
2017-02-04 02:01:05 +03:00
|
|
|
|
2018-08-23 09:03:00 +03:00
|
|
|
static PRBool WriteSrtpXtn(PRFileDesc* fd, SSLHandshakeType message,
|
|
|
|
uint8_t* data, unsigned int* len,
|
|
|
|
unsigned int max_len, void* arg);
|
|
|
|
|
|
|
|
static SECStatus HandleSrtpXtn(PRFileDesc* fd, SSLHandshakeType message,
|
|
|
|
const uint8_t* data, unsigned int len,
|
|
|
|
SSLAlertDescription* alert, void* arg);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DtlsIdentity> identity_;
|
2015-04-01 21:21:06 +03:00
|
|
|
// What ALPN identifiers are permitted.
|
|
|
|
std::set<std::string> alpn_allowed_;
|
|
|
|
// What ALPN identifier is used if ALPN is not supported.
|
|
|
|
// The empty string indicates that ALPN is required.
|
|
|
|
std::string alpn_default_;
|
|
|
|
// What ALPN string was negotiated.
|
|
|
|
std::string alpn_;
|
2018-08-23 09:03:00 +03:00
|
|
|
std::vector<uint16_t> enabled_srtp_ciphers_;
|
|
|
|
uint16_t srtp_cipher_ = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2018-08-23 09:03:00 +03:00
|
|
|
Role role_ = CLIENT;
|
|
|
|
Verification verification_mode_ = VERIFY_UNSET;
|
2019-01-29 17:56:19 +03:00
|
|
|
std::vector<DtlsDigest> digests_;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Must delete nspr_io_adapter after ssl_fd_ b/c ssl_fd_ causes an alert
|
|
|
|
// (ssl_fd_ contains an un-owning pointer to nspr_io_adapter_)
|
2018-08-23 09:03:00 +03:00
|
|
|
UniquePtr<TransportLayerNSPRAdapter> nspr_io_adapter_ = nullptr;
|
|
|
|
UniquePRFileDesc ssl_fd_ = nullptr;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2018-08-23 09:03:00 +03:00
|
|
|
nsCOMPtr<nsITimer> timer_ = nullptr;
|
|
|
|
bool auth_hook_called_ = false;
|
|
|
|
bool cert_ok_ = false;
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
#endif
|