blob: 03c353c530e545ca872585270435d033e8ff44a5 [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,
103 int recv_cs,
104 const uint8_t* recv_key,
105 int recv_key_len);
106
107 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
108 // packet encryption. The keys can either come from SDES negotiation or DTLS
109 // handshake.
110 bool SetRtcpParams(int send_cs,
111 const uint8_t* send_key,
112 int send_key_len,
113 int recv_cs,
114 const uint8_t* recv_key,
115 int recv_key_len);
116
117 void ResetParams();
118
119 // Set the header extension ids that should be encrypted for the given source.
120 // This method doesn't immediately update the SRTP session with the new IDs,
121 // and you need to call SetRtpParams for that to happen.
122 void SetEncryptedHeaderExtensionIds(cricket::ContentSource source,
123 const std::vector<int>& extension_ids);
124
125 // If external auth is enabled, SRTP will write a dummy auth tag that then
126 // later must get replaced before the packet is sent out. Only supported for
127 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
128 // if it is actually used. This method is only valid before the RTP params
129 // have been set.
130 void EnableExternalAuth();
131 bool IsExternalAuthEnabled() const;
132
133 // A SrtpTransport supports external creation of the auth tag if a non-GCM
134 // cipher is used. This method is only valid after the RTP params have
135 // been set.
136 bool IsExternalAuthActive() const;
137
138 // Returns srtp overhead for rtp packets.
139 bool GetSrtpOverhead(int* srtp_overhead) const;
140
141 // Returns rtp auth params from srtp context.
142 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
143
144 // Helper method to get RTP Absoulute SendTime extension header id if
145 // present in remote supported extensions list.
146 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
147 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
148 }
149
zstein398c3fd2017-07-19 13:38:02 -0700150 private:
Zhi Huangcf990f52017-09-22 12:12:30 -0700151 void CreateSrtpSessions();
152
zstein398c3fd2017-07-19 13:38:02 -0700153 void ConnectToRtpTransport();
154
Zhi Huangcf990f52017-09-22 12:12:30 -0700155 bool SendPacket(bool rtcp,
156 rtc::CopyOnWriteBuffer* packet,
157 const rtc::PacketOptions& options,
158 int flags);
159
zstein398c3fd2017-07-19 13:38:02 -0700160 void OnPacketReceived(bool rtcp,
161 rtc::CopyOnWriteBuffer* packet,
162 const rtc::PacketTime& packet_time);
163
164 void OnReadyToSend(bool ready) { SignalReadyToSend(ready); }
165
Zhi Huangcf990f52017-09-22 12:12:30 -0700166 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 01:12:52 -0700167
Zhi Huangcf990f52017-09-22 12:12:30 -0700168 // Overloaded version, outputs packet index.
169 bool ProtectRtp(void* data,
170 int in_len,
171 int max_len,
172 int* out_len,
173 int64_t* index);
174 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
175
176 // Decrypts/verifies an invidiual RTP/RTCP packet.
177 // If an HMAC is used, this will decrease the packet size.
178 bool UnprotectRtp(void* data, int in_len, int* out_len);
179
180 bool UnprotectRtcp(void* data, int in_len, int* out_len);
181
182 const std::string content_name_;
zstein398c3fd2017-07-19 13:38:02 -0700183 std::unique_ptr<RtpTransportInternal> rtp_transport_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700184
185 std::unique_ptr<cricket::SrtpSession> send_session_;
186 std::unique_ptr<cricket::SrtpSession> recv_session_;
187 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
188 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
189
190 std::vector<int> send_encrypted_header_extension_ids_;
191 std::vector<int> recv_encrypted_header_extension_ids_;
192 bool external_auth_enabled_ = false;
193
194 int rtp_abs_sendtime_extn_id_ = -1;
zstein398c3fd2017-07-19 13:38:02 -0700195};
196
197} // namespace webrtc
198
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200199#endif // PC_SRTPTRANSPORT_H_