blob: 818aabb2a3493b8ed599cbebbe500abba3eb4dfd [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
Zhi Huang942bc2e2017-11-13 13:26:07 -080019#include "p2p/base/icetransportinternal.h"
Zhi Huang95e7dbb2018-03-29 00:08:03 +000020#include "pc/rtptransportinternaladapter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "pc/srtpfilter.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070022#include "pc/srtpsession.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
zstein398c3fd2017-07-19 13:38:02 -070024
25namespace webrtc {
26
Zhi Huang95e7dbb2018-03-29 00:08:03 +000027// This class will eventually be a wrapper around RtpTransportInternal
28// that protects and unprotects sent and received RTP packets.
29class SrtpTransport : public RtpTransportInternalAdapter {
zstein398c3fd2017-07-19 13:38:02 -070030 public:
Zhi Huang2dfc42d2017-12-04 13:38:48 -080031 explicit SrtpTransport(bool rtcp_mux_enabled);
zstein398c3fd2017-07-19 13:38:02 -070032
Zhi Huang95e7dbb2018-03-29 00:08:03 +000033 explicit SrtpTransport(std::unique_ptr<RtpTransportInternal> rtp_transport);
34
Zhi Huangcf990f52017-09-22 12:12:30 -070035 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
36 const rtc::PacketOptions& options,
37 int flags) override;
38
39 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
40 const rtc::PacketOptions& options,
41 int flags) override;
42
Zhi Huangcf990f52017-09-22 12:12:30 -070043 // The transport becomes active if the send_session_ and recv_session_ are
44 // created.
45 bool IsActive() const;
zstein398c3fd2017-07-19 13:38:02 -070046
zstein398c3fd2017-07-19 13:38:02 -070047 // TODO(zstein): Remove this when we remove RtpTransportAdapter.
48 RtpTransportAdapter* GetInternal() override { return nullptr; }
49
Zhi Huangcf990f52017-09-22 12:12:30 -070050 // Create new send/recv sessions and set the negotiated crypto keys for RTP
51 // packet encryption. The keys can either come from SDES negotiation or DTLS
52 // handshake.
53 bool SetRtpParams(int send_cs,
54 const uint8_t* send_key,
55 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080056 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070057 int recv_cs,
58 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080059 int recv_key_len,
60 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070061
62 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
63 // packet encryption. The keys can either come from SDES negotiation or DTLS
64 // handshake.
65 bool SetRtcpParams(int send_cs,
66 const uint8_t* send_key,
67 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080068 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070069 int recv_cs,
70 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080071 int recv_key_len,
72 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070073
74 void ResetParams();
75
Zhi Huangcf990f52017-09-22 12:12:30 -070076 // If external auth is enabled, SRTP will write a dummy auth tag that then
77 // later must get replaced before the packet is sent out. Only supported for
78 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
79 // if it is actually used. This method is only valid before the RTP params
80 // have been set.
81 void EnableExternalAuth();
82 bool IsExternalAuthEnabled() const;
83
84 // A SrtpTransport supports external creation of the auth tag if a non-GCM
85 // cipher is used. This method is only valid after the RTP params have
86 // been set.
87 bool IsExternalAuthActive() const;
88
89 // Returns srtp overhead for rtp packets.
90 bool GetSrtpOverhead(int* srtp_overhead) const;
91
92 // Returns rtp auth params from srtp context.
93 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
94
Zhi Huang2a4d70c2017-11-29 15:41:59 -080095 // Cache RTP Absoulute SendTime extension header ID. This is only used when
96 // external authentication is enabled.
Zhi Huangcf990f52017-09-22 12:12:30 -070097 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
98 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
99 }
100
Steve Antondb67ba12018-03-19 17:41:42 -0700101 void SetMetricsObserver(
102 rtc::scoped_refptr<MetricsObserverInterface> metrics_observer) override;
103
zstein398c3fd2017-07-19 13:38:02 -0700104 private:
105 void ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800106 void CreateSrtpSessions();
zstein398c3fd2017-07-19 13:38:02 -0700107
Zhi Huang95e7dbb2018-03-29 00:08:03 +0000108 bool SendPacket(bool rtcp,
109 rtc::CopyOnWriteBuffer* packet,
110 const rtc::PacketOptions& options,
111 int flags);
112
113 void OnPacketReceived(bool rtcp,
114 rtc::CopyOnWriteBuffer* packet,
115 const rtc::PacketTime& packet_time);
116 void OnReadyToSend(bool ready) { SignalReadyToSend(ready); }
117 void OnNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute> network_route);
118
119 void OnWritableState(bool writable) { SignalWritableState(writable); }
120
121 void OnSentPacket(const rtc::SentPacket& sent_packet) {
122 SignalSentPacket(sent_packet);
123 }
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800124
Zhi Huangcf990f52017-09-22 12:12:30 -0700125 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 01:12:52 -0700126
Zhi Huangcf990f52017-09-22 12:12:30 -0700127 // Overloaded version, outputs packet index.
128 bool ProtectRtp(void* data,
129 int in_len,
130 int max_len,
131 int* out_len,
132 int64_t* index);
133 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
134
135 // Decrypts/verifies an invidiual RTP/RTCP packet.
136 // If an HMAC is used, this will decrease the packet size.
137 bool UnprotectRtp(void* data, int in_len, int* out_len);
138
139 bool UnprotectRtcp(void* data, int in_len, int* out_len);
140
141 const std::string content_name_;
Zhi Huang95e7dbb2018-03-29 00:08:03 +0000142 std::unique_ptr<RtpTransportInternal> rtp_transport_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700143
144 std::unique_ptr<cricket::SrtpSession> send_session_;
145 std::unique_ptr<cricket::SrtpSession> recv_session_;
146 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
147 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
148
Zhi Huangcf990f52017-09-22 12:12:30 -0700149 bool external_auth_enabled_ = false;
150
151 int rtp_abs_sendtime_extn_id_ = -1;
Steve Antondb67ba12018-03-19 17:41:42 -0700152
153 rtc::scoped_refptr<MetricsObserverInterface> metrics_observer_;
zstein398c3fd2017-07-19 13:38:02 -0700154};
155
156} // namespace webrtc
157
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200158#endif // PC_SRTPTRANSPORT_H_