2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2011, Google Inc.
|
|
|
|
* Copyright 2011, RTFM, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TALK_P2P_BASE_DTLSTRANSPORTCHANNEL_H_
|
|
|
|
#define TALK_P2P_BASE_DTLSTRANSPORTCHANNEL_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-08-13 19:26:08 +02:00
|
|
|
#include "talk/p2p/base/transportchannelimpl.h"
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/buffer.h"
|
|
|
|
#include "webrtc/base/scoped_ptr.h"
|
|
|
|
#include "webrtc/base/sslstreamadapter.h"
|
|
|
|
#include "webrtc/base/stream.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
// A bridge between a packet-oriented/channel-type interface on
|
|
|
|
// the bottom and a StreamInterface on the top.
|
2014-07-29 19:36:52 +02:00
|
|
|
class StreamInterfaceChannel : public rtc::StreamInterface,
|
2013-07-10 02:45:36 +02:00
|
|
|
public sigslot::has_slots<> {
|
|
|
|
public:
|
2014-07-29 19:36:52 +02:00
|
|
|
StreamInterfaceChannel(rtc::Thread* owner, TransportChannel* channel)
|
2013-07-10 02:45:36 +02:00
|
|
|
: channel_(channel),
|
2014-07-29 19:36:52 +02:00
|
|
|
state_(rtc::SS_OPEN),
|
2013-07-10 02:45:36 +02:00
|
|
|
fifo_(kFifoSize, owner) {
|
|
|
|
fifo_.SignalEvent.connect(this, &StreamInterfaceChannel::OnEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push in a packet; this gets pulled out from Read().
|
|
|
|
bool OnPacketReceived(const char* data, size_t size);
|
|
|
|
|
|
|
|
// Implementations of StreamInterface
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual rtc::StreamState GetState() const { return state_; }
|
|
|
|
virtual void Close() { state_ = rtc::SS_CLOSED; }
|
|
|
|
virtual rtc::StreamResult Read(void* buffer, size_t buffer_len,
|
2013-07-10 02:45:36 +02:00
|
|
|
size_t* read, int* error);
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual rtc::StreamResult Write(const void* data, size_t data_len,
|
2013-07-10 02:45:36 +02:00
|
|
|
size_t* written, int* error);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const size_t kFifoSize = 8192;
|
|
|
|
|
|
|
|
// Forward events
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual void OnEvent(rtc::StreamInterface* stream, int sig, int err);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
TransportChannel* channel_; // owned by DtlsTransportChannelWrapper
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::StreamState state_;
|
|
|
|
rtc::FifoBuffer fifo_;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style
|
|
|
|
// packet-based interface, wrapping an existing TransportChannel instance
|
|
|
|
// (e.g a P2PTransportChannel)
|
|
|
|
// Here's the way this works:
|
|
|
|
//
|
|
|
|
// DtlsTransportChannelWrapper {
|
|
|
|
// SSLStreamAdapter* dtls_ {
|
|
|
|
// StreamInterfaceChannel downward_ {
|
|
|
|
// TransportChannelImpl* channel_;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// - Data which comes into DtlsTransportChannelWrapper from the underlying
|
|
|
|
// channel_ via OnReadPacket() is checked for whether it is DTLS
|
|
|
|
// or not, and if it is, is passed to DtlsTransportChannelWrapper::
|
|
|
|
// HandleDtlsPacket, which pushes it into to downward_.
|
|
|
|
// dtls_ is listening for events on downward_, so it immediately calls
|
|
|
|
// downward_->Read().
|
|
|
|
//
|
|
|
|
// - Data written to DtlsTransportChannelWrapper is passed either to
|
|
|
|
// downward_ or directly to channel_, depending on whether DTLS is
|
|
|
|
// negotiated and whether the flags include PF_SRTP_BYPASS
|
|
|
|
//
|
|
|
|
// - The SSLStreamAdapter writes to downward_->Write()
|
|
|
|
// which translates it into packet writes on channel_.
|
|
|
|
class DtlsTransportChannelWrapper : public TransportChannelImpl {
|
|
|
|
public:
|
|
|
|
enum State {
|
|
|
|
STATE_NONE, // No state or rejected.
|
|
|
|
STATE_OFFERED, // Our identity has been set.
|
|
|
|
STATE_ACCEPTED, // The other side sent a fingerprint.
|
|
|
|
STATE_STARTED, // We are negotiating.
|
|
|
|
STATE_OPEN, // Negotiation complete.
|
|
|
|
STATE_CLOSED // Connection closed.
|
|
|
|
};
|
|
|
|
|
|
|
|
// The parameters here are:
|
|
|
|
// transport -- the DtlsTransport that created us
|
|
|
|
// channel -- the TransportChannel we are wrapping
|
|
|
|
DtlsTransportChannelWrapper(Transport* transport,
|
|
|
|
TransportChannelImpl* channel);
|
|
|
|
virtual ~DtlsTransportChannelWrapper();
|
|
|
|
|
2013-08-24 01:21:25 +02:00
|
|
|
virtual void SetIceRole(IceRole role) {
|
|
|
|
channel_->SetIceRole(role);
|
|
|
|
}
|
2013-08-12 23:18:15 +02:00
|
|
|
virtual IceRole GetIceRole() const {
|
|
|
|
return channel_->GetIceRole();
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
2014-02-14 01:56:12 +01:00
|
|
|
virtual size_t GetConnectionCount() const {
|
|
|
|
return channel_->GetConnectionCount();
|
|
|
|
}
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual bool SetLocalIdentity(rtc::SSLIdentity *identity);
|
|
|
|
virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
virtual bool SetRemoteFingerprint(const std::string& digest_alg,
|
|
|
|
const uint8* digest,
|
|
|
|
size_t digest_len);
|
|
|
|
virtual bool IsDtlsActive() const { return dtls_state_ != STATE_NONE; }
|
|
|
|
|
|
|
|
// Called to send a packet (via DTLS, if turned on).
|
2013-09-23 22:34:45 +02:00
|
|
|
virtual int SendPacket(const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::PacketOptions& options,
|
2013-09-23 22:34:45 +02:00
|
|
|
int flags);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// TransportChannel calls that we forward to the wrapped transport.
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual int SetOption(rtc::Socket::Option opt, int value) {
|
2013-07-10 02:45:36 +02:00
|
|
|
return channel_->SetOption(opt, value);
|
|
|
|
}
|
|
|
|
virtual int GetError() {
|
|
|
|
return channel_->GetError();
|
|
|
|
}
|
|
|
|
virtual bool GetStats(ConnectionInfos* infos) {
|
|
|
|
return channel_->GetStats(infos);
|
|
|
|
}
|
2013-08-12 23:18:15 +02:00
|
|
|
virtual const std::string SessionId() const {
|
2013-07-10 02:45:36 +02:00
|
|
|
return channel_->SessionId();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the ciphers to use for DTLS-SRTP. If this method is not called
|
|
|
|
// before DTLS starts, or |ciphers| is empty, SRTP keys won't be negotiated.
|
|
|
|
// This method should be called before SetupDtls.
|
|
|
|
virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers);
|
|
|
|
|
|
|
|
// Find out which DTLS-SRTP cipher was negotiated
|
|
|
|
virtual bool GetSrtpCipher(std::string* cipher);
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual bool GetSslRole(rtc::SSLRole* role) const;
|
|
|
|
virtual bool SetSslRole(rtc::SSLRole role);
|
2013-08-24 01:21:25 +02:00
|
|
|
|
2013-10-09 17:37:36 +02:00
|
|
|
// Once DTLS has been established, this method retrieves the certificate in
|
|
|
|
// use by the remote peer, for use in external identity verification.
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const;
|
2013-10-09 17:37:36 +02:00
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
// Once DTLS has established (i.e., this channel is writable), this method
|
|
|
|
// extracts the keys negotiated during the DTLS handshake, for use in external
|
|
|
|
// encryption. DTLS-SRTP uses this to extract the needed SRTP keys.
|
|
|
|
// See the SSLStreamAdapter documentation for info on the specific parameters.
|
|
|
|
virtual bool ExportKeyingMaterial(const std::string& label,
|
|
|
|
const uint8* context,
|
|
|
|
size_t context_len,
|
|
|
|
bool use_context,
|
|
|
|
uint8* result,
|
|
|
|
size_t result_len) {
|
|
|
|
return (dtls_.get()) ? dtls_->ExportKeyingMaterial(label, context,
|
|
|
|
context_len,
|
|
|
|
use_context,
|
|
|
|
result, result_len)
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransportChannelImpl calls.
|
|
|
|
virtual Transport* GetTransport() {
|
|
|
|
return transport_;
|
|
|
|
}
|
2013-08-12 23:18:15 +02:00
|
|
|
virtual void SetIceTiebreaker(uint64 tiebreaker) {
|
|
|
|
channel_->SetIceTiebreaker(tiebreaker);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
2014-02-03 17:57:16 +01:00
|
|
|
virtual bool GetIceProtocolType(IceProtocolType* type) const {
|
|
|
|
return channel_->GetIceProtocolType(type);
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
virtual void SetIceProtocolType(IceProtocolType type) {
|
|
|
|
channel_->SetIceProtocolType(type);
|
|
|
|
}
|
|
|
|
virtual void SetIceCredentials(const std::string& ice_ufrag,
|
|
|
|
const std::string& ice_pwd) {
|
|
|
|
channel_->SetIceCredentials(ice_ufrag, ice_pwd);
|
|
|
|
}
|
|
|
|
virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
|
|
|
|
const std::string& ice_pwd) {
|
|
|
|
channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd);
|
|
|
|
}
|
|
|
|
virtual void SetRemoteIceMode(IceMode mode) {
|
|
|
|
channel_->SetRemoteIceMode(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Connect();
|
|
|
|
virtual void Reset();
|
|
|
|
|
|
|
|
virtual void OnSignalingReady() {
|
|
|
|
channel_->OnSignalingReady();
|
|
|
|
}
|
|
|
|
virtual void OnCandidate(const Candidate& candidate) {
|
|
|
|
channel_->OnCandidate(candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed by DtlsTransport.
|
|
|
|
TransportChannelImpl* channel() { return channel_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void OnReadableState(TransportChannel* channel);
|
|
|
|
void OnWritableState(TransportChannel* channel);
|
|
|
|
void OnReadPacket(TransportChannel* channel, const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::PacketTime& packet_time, int flags);
|
2013-07-10 02:45:36 +02:00
|
|
|
void OnReadyToSend(TransportChannel* channel);
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err);
|
2013-07-10 02:45:36 +02:00
|
|
|
bool SetupDtls();
|
|
|
|
bool MaybeStartDtls();
|
|
|
|
bool HandleDtlsPacket(const char* data, size_t size);
|
|
|
|
void OnRequestSignaling(TransportChannelImpl* channel);
|
|
|
|
void OnCandidateReady(TransportChannelImpl* channel, const Candidate& c);
|
|
|
|
void OnCandidatesAllocationDone(TransportChannelImpl* channel);
|
|
|
|
void OnRoleConflict(TransportChannelImpl* channel);
|
|
|
|
void OnRouteChange(TransportChannel* channel, const Candidate& candidate);
|
2014-02-14 01:56:12 +01:00
|
|
|
void OnConnectionRemoved(TransportChannelImpl* channel);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
Transport* transport_; // The transport_ that created us.
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread* worker_thread_; // Everything should occur on this thread.
|
2013-07-10 02:45:36 +02:00
|
|
|
TransportChannelImpl* channel_; // Underlying channel, owned by transport_.
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::scoped_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream
|
2013-07-10 02:45:36 +02:00
|
|
|
StreamInterfaceChannel* downward_; // Wrapper for channel_, owned by dtls_.
|
|
|
|
std::vector<std::string> srtp_ciphers_; // SRTP ciphers to use with DTLS.
|
|
|
|
State dtls_state_;
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SSLIdentity* local_identity_;
|
|
|
|
rtc::SSLRole ssl_role_;
|
|
|
|
rtc::Buffer remote_fingerprint_value_;
|
2013-07-10 02:45:36 +02:00
|
|
|
std::string remote_fingerprint_algorithm_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(DtlsTransportChannelWrapper);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cricket
|
|
|
|
|
|
|
|
#endif // TALK_P2P_BASE_DTLSTRANSPORTCHANNEL_H_
|