blob: 9f20e1d8f662cd5cfc20139f5f88e99360be54f9 [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>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "pc/rtptransportinternal.h"
19#include "pc/srtpfilter.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070020#include "pc/srtpsession.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
zstein398c3fd2017-07-19 13:38:02 -070022
23namespace webrtc {
24
25// This class will eventually be a wrapper around RtpTransportInternal
Zhi Huangcf990f52017-09-22 12:12:30 -070026// that protects and unprotects sent and received RTP packets.
zstein398c3fd2017-07-19 13:38:02 -070027class SrtpTransport : public RtpTransportInternal {
28 public:
29 SrtpTransport(bool rtcp_mux_enabled, const std::string& content_name);
30
zstein398c3fd2017-07-19 13:38:02 -070031 SrtpTransport(std::unique_ptr<RtpTransportInternal> transport,
32 const std::string& content_name);
33
34 void SetRtcpMuxEnabled(bool enable) override {
35 rtp_transport_->SetRtcpMuxEnabled(enable);
36 }
37
38 rtc::PacketTransportInternal* rtp_packet_transport() const override {
39 return rtp_transport_->rtp_packet_transport();
40 }
41
42 void SetRtpPacketTransport(rtc::PacketTransportInternal* rtp) override {
43 rtp_transport_->SetRtpPacketTransport(rtp);
44 }
45
46 PacketTransportInterface* GetRtpPacketTransport() const override {
47 return rtp_transport_->GetRtpPacketTransport();
48 }
49
50 rtc::PacketTransportInternal* rtcp_packet_transport() const override {
51 return rtp_transport_->rtcp_packet_transport();
52 }
53 void SetRtcpPacketTransport(rtc::PacketTransportInternal* rtcp) override {
54 rtp_transport_->SetRtcpPacketTransport(rtcp);
55 }
56
57 PacketTransportInterface* GetRtcpPacketTransport() const override {
58 return rtp_transport_->GetRtcpPacketTransport();
59 }
60
Zhi Huangcf990f52017-09-22 12:12:30 -070061 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
62 const rtc::PacketOptions& options,
63 int flags) override;
64
65 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
66 const rtc::PacketOptions& options,
67 int flags) override;
68
zstein398c3fd2017-07-19 13:38:02 -070069 bool IsWritable(bool rtcp) const override {
70 return rtp_transport_->IsWritable(rtcp);
71 }
72
Zhi Huangcf990f52017-09-22 12:12:30 -070073 // The transport becomes active if the send_session_ and recv_session_ are
74 // created.
75 bool IsActive() const;
zstein398c3fd2017-07-19 13:38:02 -070076
77 bool HandlesPayloadType(int payload_type) const override {
78 return rtp_transport_->HandlesPayloadType(payload_type);
79 }
80
81 void AddHandledPayloadType(int payload_type) override {
82 rtp_transport_->AddHandledPayloadType(payload_type);
83 }
84
sprangdb2a9fc2017-08-09 06:42:32 -070085 RTCError SetParameters(const RtpTransportParameters& parameters) override {
86 return rtp_transport_->SetParameters(parameters);
zstein398c3fd2017-07-19 13:38:02 -070087 }
88
sprangdb2a9fc2017-08-09 06:42:32 -070089 RtpTransportParameters GetParameters() const override {
90 return rtp_transport_->GetParameters();
zstein398c3fd2017-07-19 13:38:02 -070091 }
92
93 // TODO(zstein): Remove this when we remove RtpTransportAdapter.
94 RtpTransportAdapter* GetInternal() override { return nullptr; }
95
Zhi Huangcf990f52017-09-22 12:12:30 -070096 // Create new send/recv sessions and set the negotiated crypto keys for RTP
97 // packet encryption. The keys can either come from SDES negotiation or DTLS
98 // handshake.
99 bool SetRtpParams(int send_cs,
100 const uint8_t* send_key,
101 int send_key_len,
102 int recv_cs,
103 const uint8_t* recv_key,
104 int recv_key_len);
105
106 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
107 // packet encryption. The keys can either come from SDES negotiation or DTLS
108 // handshake.
109 bool SetRtcpParams(int send_cs,
110 const uint8_t* send_key,
111 int send_key_len,
112 int recv_cs,
113 const uint8_t* recv_key,
114 int recv_key_len);
115
116 void ResetParams();
117
118 // Set the header extension ids that should be encrypted for the given source.
119 // This method doesn't immediately update the SRTP session with the new IDs,
120 // and you need to call SetRtpParams for that to happen.
121 void SetEncryptedHeaderExtensionIds(cricket::ContentSource source,
122 const std::vector<int>& extension_ids);
123
124 // If external auth is enabled, SRTP will write a dummy auth tag that then
125 // later must get replaced before the packet is sent out. Only supported for
126 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
127 // if it is actually used. This method is only valid before the RTP params
128 // have been set.
129 void EnableExternalAuth();
130 bool IsExternalAuthEnabled() const;
131
132 // A SrtpTransport supports external creation of the auth tag if a non-GCM
133 // cipher is used. This method is only valid after the RTP params have
134 // been set.
135 bool IsExternalAuthActive() const;
136
137 // Returns srtp overhead for rtp packets.
138 bool GetSrtpOverhead(int* srtp_overhead) const;
139
140 // Returns rtp auth params from srtp context.
141 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
142
143 // Helper method to get RTP Absoulute SendTime extension header id if
144 // present in remote supported extensions list.
145 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
146 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
147 }
148
zstein398c3fd2017-07-19 13:38:02 -0700149 private:
Zhi Huangcf990f52017-09-22 12:12:30 -0700150 void CreateSrtpSessions();
151
zstein398c3fd2017-07-19 13:38:02 -0700152 void ConnectToRtpTransport();
153
Zhi Huangcf990f52017-09-22 12:12:30 -0700154 bool SendPacket(bool rtcp,
155 rtc::CopyOnWriteBuffer* packet,
156 const rtc::PacketOptions& options,
157 int flags);
158
zstein398c3fd2017-07-19 13:38:02 -0700159 void OnPacketReceived(bool rtcp,
160 rtc::CopyOnWriteBuffer* packet,
161 const rtc::PacketTime& packet_time);
162
163 void OnReadyToSend(bool ready) { SignalReadyToSend(ready); }
164
Zhi Huangcf990f52017-09-22 12:12:30 -0700165 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 01:12:52 -0700166
Zhi Huangcf990f52017-09-22 12:12:30 -0700167 // Overloaded version, outputs packet index.
168 bool ProtectRtp(void* data,
169 int in_len,
170 int max_len,
171 int* out_len,
172 int64_t* index);
173 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
174
175 // Decrypts/verifies an invidiual RTP/RTCP packet.
176 // If an HMAC is used, this will decrease the packet size.
177 bool UnprotectRtp(void* data, int in_len, int* out_len);
178
179 bool UnprotectRtcp(void* data, int in_len, int* out_len);
180
181 const std::string content_name_;
zstein398c3fd2017-07-19 13:38:02 -0700182 std::unique_ptr<RtpTransportInternal> rtp_transport_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700183
184 std::unique_ptr<cricket::SrtpSession> send_session_;
185 std::unique_ptr<cricket::SrtpSession> recv_session_;
186 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
187 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
188
189 std::vector<int> send_encrypted_header_extension_ids_;
190 std::vector<int> recv_encrypted_header_extension_ids_;
191 bool external_auth_enabled_ = false;
192
193 int rtp_abs_sendtime_extn_id_ = -1;
zstein398c3fd2017-07-19 13:38:02 -0700194};
195
196} // namespace webrtc
197
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200198#endif // PC_SRTPTRANSPORT_H_