blob: 13abd6b47d6acdabd63187313ab2da7ab8767803 [file] [log] [blame]
zstein398c3fd2017-07-19 13:38:02 -07001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_SRTPTRANSPORT_H_
12#define PC_SRTPTRANSPORT_H_
zstein398c3fd2017-07-19 13:38:02 -070013
14#include <memory>
15#include <string>
16#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070017#include <vector>
zstein398c3fd2017-07-19 13:38:02 -070018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "pc/rtptransportinternal.h"
20#include "pc/srtpfilter.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070021#include "pc/srtpsession.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
zstein398c3fd2017-07-19 13:38:02 -070023
24namespace webrtc {
25
26// This class will eventually be a wrapper around RtpTransportInternal
Zhi Huangcf990f52017-09-22 12:12:30 -070027// that protects and unprotects sent and received RTP packets.
zstein398c3fd2017-07-19 13:38:02 -070028class SrtpTransport : public RtpTransportInternal {
29 public:
30 SrtpTransport(bool rtcp_mux_enabled, const std::string& content_name);
31
zstein398c3fd2017-07-19 13:38:02 -070032 SrtpTransport(std::unique_ptr<RtpTransportInternal> transport,
33 const std::string& content_name);
34
35 void SetRtcpMuxEnabled(bool enable) override {
36 rtp_transport_->SetRtcpMuxEnabled(enable);
37 }
38
39 rtc::PacketTransportInternal* rtp_packet_transport() const override {
40 return rtp_transport_->rtp_packet_transport();
41 }
42
43 void SetRtpPacketTransport(rtc::PacketTransportInternal* rtp) override {
44 rtp_transport_->SetRtpPacketTransport(rtp);
45 }
46
47 PacketTransportInterface* GetRtpPacketTransport() const override {
48 return rtp_transport_->GetRtpPacketTransport();
49 }
50
51 rtc::PacketTransportInternal* rtcp_packet_transport() const override {
52 return rtp_transport_->rtcp_packet_transport();
53 }
54 void SetRtcpPacketTransport(rtc::PacketTransportInternal* rtcp) override {
55 rtp_transport_->SetRtcpPacketTransport(rtcp);
56 }
57
58 PacketTransportInterface* GetRtcpPacketTransport() const override {
59 return rtp_transport_->GetRtcpPacketTransport();
60 }
61
Zhi Huangcf990f52017-09-22 12:12:30 -070062 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
63 const rtc::PacketOptions& options,
64 int flags) override;
65
66 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
67 const rtc::PacketOptions& options,
68 int flags) override;
69
zstein398c3fd2017-07-19 13:38:02 -070070 bool IsWritable(bool rtcp) const override {
71 return rtp_transport_->IsWritable(rtcp);
72 }
73
Zhi Huangcf990f52017-09-22 12:12:30 -070074 // The transport becomes active if the send_session_ and recv_session_ are
75 // created.
76 bool IsActive() const;
zstein398c3fd2017-07-19 13:38:02 -070077
78 bool HandlesPayloadType(int payload_type) const override {
79 return rtp_transport_->HandlesPayloadType(payload_type);
80 }
81
82 void AddHandledPayloadType(int payload_type) override {
83 rtp_transport_->AddHandledPayloadType(payload_type);
84 }
85
sprangdb2a9fc2017-08-09 06:42:32 -070086 RTCError SetParameters(const RtpTransportParameters& parameters) override {
87 return rtp_transport_->SetParameters(parameters);
zstein398c3fd2017-07-19 13:38:02 -070088 }
89
sprangdb2a9fc2017-08-09 06:42:32 -070090 RtpTransportParameters GetParameters() const override {
91 return rtp_transport_->GetParameters();
zstein398c3fd2017-07-19 13:38:02 -070092 }
93
94 // TODO(zstein): Remove this when we remove RtpTransportAdapter.
95 RtpTransportAdapter* GetInternal() override { return nullptr; }
96
Zhi Huangcf990f52017-09-22 12:12:30 -070097 // Create new send/recv sessions and set the negotiated crypto keys for RTP
98 // packet encryption. The keys can either come from SDES negotiation or DTLS
99 // handshake.
100 bool SetRtpParams(int send_cs,
101 const uint8_t* send_key,
102 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800103 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -0700104 int recv_cs,
105 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800106 int recv_key_len,
107 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -0700108
109 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
110 // packet encryption. The keys can either come from SDES negotiation or DTLS
111 // handshake.
112 bool SetRtcpParams(int send_cs,
113 const uint8_t* send_key,
114 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800115 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -0700116 int recv_cs,
117 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800118 int recv_key_len,
119 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -0700120
121 void ResetParams();
122
Zhi Huangcf990f52017-09-22 12:12:30 -0700123 // If external auth is enabled, SRTP will write a dummy auth tag that then
124 // later must get replaced before the packet is sent out. Only supported for
125 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
126 // if it is actually used. This method is only valid before the RTP params
127 // have been set.
128 void EnableExternalAuth();
129 bool IsExternalAuthEnabled() const;
130
131 // A SrtpTransport supports external creation of the auth tag if a non-GCM
132 // cipher is used. This method is only valid after the RTP params have
133 // been set.
134 bool IsExternalAuthActive() const;
135
136 // Returns srtp overhead for rtp packets.
137 bool GetSrtpOverhead(int* srtp_overhead) const;
138
139 // Returns rtp auth params from srtp context.
140 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
141
142 // Helper method to get RTP Absoulute SendTime extension header id if
143 // present in remote supported extensions list.
144 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
145 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
146 }
147
zstein398c3fd2017-07-19 13:38:02 -0700148 private:
Zhi Huangcf990f52017-09-22 12:12:30 -0700149 void CreateSrtpSessions();
150
zstein398c3fd2017-07-19 13:38:02 -0700151 void ConnectToRtpTransport();
152
Zhi Huangcf990f52017-09-22 12:12:30 -0700153 bool SendPacket(bool rtcp,
154 rtc::CopyOnWriteBuffer* packet,
155 const rtc::PacketOptions& options,
156 int flags);
157
zstein398c3fd2017-07-19 13:38:02 -0700158 void OnPacketReceived(bool rtcp,
159 rtc::CopyOnWriteBuffer* packet,
160 const rtc::PacketTime& packet_time);
161
162 void OnReadyToSend(bool ready) { SignalReadyToSend(ready); }
163
Zhi Huangcf990f52017-09-22 12:12:30 -0700164 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 01:12:52 -0700165
Zhi Huangcf990f52017-09-22 12:12:30 -0700166 // Overloaded version, outputs packet index.
167 bool ProtectRtp(void* data,
168 int in_len,
169 int max_len,
170 int* out_len,
171 int64_t* index);
172 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
173
174 // Decrypts/verifies an invidiual RTP/RTCP packet.
175 // If an HMAC is used, this will decrease the packet size.
176 bool UnprotectRtp(void* data, int in_len, int* out_len);
177
178 bool UnprotectRtcp(void* data, int in_len, int* out_len);
179
180 const std::string content_name_;
zstein398c3fd2017-07-19 13:38:02 -0700181 std::unique_ptr<RtpTransportInternal> rtp_transport_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700182
183 std::unique_ptr<cricket::SrtpSession> send_session_;
184 std::unique_ptr<cricket::SrtpSession> recv_session_;
185 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
186 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
187
Zhi Huangcf990f52017-09-22 12:12:30 -0700188 bool external_auth_enabled_ = false;
189
190 int rtp_abs_sendtime_extn_id_ = -1;
zstein398c3fd2017-07-19 13:38:02 -0700191};
192
193} // namespace webrtc
194
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200195#endif // PC_SRTPTRANSPORT_H_