blob: 70829d45e0490f796aabcf24841b5983d8283f32 [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 Huange830e682018-03-30 10:48:35 -070019#include "api/ortc/srtptransportinterface.h"
20#include "p2p/base/dtlstransportinternal.h"
Zhi Huang942bc2e2017-11-13 13:26:07 -080021#include "p2p/base/icetransportinternal.h"
Zhi Huange830e682018-03-30 10:48:35 -070022#include "pc/rtptransport.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070023#include "pc/srtpsession.h"
Zhi Huange830e682018-03-30 10:48:35 -070024#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
zstein398c3fd2017-07-19 13:38:02 -070026
27namespace webrtc {
28
Zhi Huang365381f2018-04-13 16:44:34 -070029// This subclass of the RtpTransport is used for SRTP which is reponsible for
30// protecting/unprotecting the packets. It provides interfaces to set the crypto
31// parameters for the SrtpSession underneath.
32class SrtpTransport : public RtpTransport {
zstein398c3fd2017-07-19 13:38:02 -070033 public:
Zhi Huang2dfc42d2017-12-04 13:38:48 -080034 explicit SrtpTransport(bool rtcp_mux_enabled);
zstein398c3fd2017-07-19 13:38:02 -070035
Zhi Huang365381f2018-04-13 16:44:34 -070036 virtual ~SrtpTransport() = default;
Zhi Huange830e682018-03-30 10:48:35 -070037
38 // SrtpTransportInterface specific implementation.
39 RTCError SetSrtpSendKey(const cricket::CryptoParams& params) override;
40 RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params) override;
Zhi Huang95e7dbb2018-03-29 00:08:03 +000041
Zhi Huangcf990f52017-09-22 12:12:30 -070042 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
43 const rtc::PacketOptions& options,
44 int flags) override;
45
46 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
47 const rtc::PacketOptions& options,
48 int flags) override;
49
Zhi Huangcf990f52017-09-22 12:12:30 -070050 // The transport becomes active if the send_session_ and recv_session_ are
51 // created.
Zhi Huange830e682018-03-30 10:48:35 -070052 bool IsSrtpActive() const override;
zstein398c3fd2017-07-19 13:38:02 -070053
Zhi Huang365381f2018-04-13 16:44:34 -070054 bool IsWritable(bool rtcp) const override;
55
Zhi Huangcf990f52017-09-22 12:12:30 -070056 // Create new send/recv sessions and set the negotiated crypto keys for RTP
57 // packet encryption. The keys can either come from SDES negotiation or DTLS
58 // handshake.
59 bool SetRtpParams(int send_cs,
60 const uint8_t* send_key,
61 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080062 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070063 int recv_cs,
64 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080065 int recv_key_len,
66 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070067
68 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
69 // packet encryption. The keys can either come from SDES negotiation or DTLS
70 // handshake.
71 bool SetRtcpParams(int send_cs,
72 const uint8_t* send_key,
73 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080074 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070075 int recv_cs,
76 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080077 int recv_key_len,
78 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070079
80 void ResetParams();
81
Zhi Huangcf990f52017-09-22 12:12:30 -070082 // If external auth is enabled, SRTP will write a dummy auth tag that then
83 // later must get replaced before the packet is sent out. Only supported for
84 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
85 // if it is actually used. This method is only valid before the RTP params
86 // have been set.
87 void EnableExternalAuth();
88 bool IsExternalAuthEnabled() const;
89
90 // A SrtpTransport supports external creation of the auth tag if a non-GCM
91 // cipher is used. This method is only valid after the RTP params have
92 // been set.
93 bool IsExternalAuthActive() const;
94
95 // Returns srtp overhead for rtp packets.
96 bool GetSrtpOverhead(int* srtp_overhead) const;
97
98 // Returns rtp auth params from srtp context.
99 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
100
Zhi Huang2a4d70c2017-11-29 15:41:59 -0800101 // Cache RTP Absoulute SendTime extension header ID. This is only used when
102 // external authentication is enabled.
Zhi Huangcf990f52017-09-22 12:12:30 -0700103 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
104 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
105 }
106
Zhi Huang365381f2018-04-13 16:44:34 -0700107 protected:
108 // If the writable state changed, fire the SignalWritableState.
109 void MaybeUpdateWritableState();
Steve Antondb67ba12018-03-19 17:41:42 -0700110
zstein398c3fd2017-07-19 13:38:02 -0700111 private:
112 void ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800113 void CreateSrtpSessions();
zstein398c3fd2017-07-19 13:38:02 -0700114
Zhi Huang365381f2018-04-13 16:44:34 -0700115 void OnRtpPacketReceived(rtc::CopyOnWriteBuffer* packet,
116 const rtc::PacketTime& packet_time) override;
117 void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet,
118 const rtc::PacketTime& packet_time) override;
119 void OnNetworkRouteChanged(
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200120 absl::optional<rtc::NetworkRoute> network_route) override;
Zhi Huang95e7dbb2018-03-29 00:08:03 +0000121
Zhi Huang365381f2018-04-13 16:44:34 -0700122 // Override the RtpTransport::OnWritableState.
123 void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
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
Zhi Huange830e682018-03-30 10:48:35 -0700141 bool MaybeSetKeyParams();
142 bool ParseKeyParams(const std::string& key_params, uint8_t* key, size_t len);
143
Zhi Huangcf990f52017-09-22 12:12:30 -0700144 const std::string content_name_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700145
146 std::unique_ptr<cricket::SrtpSession> send_session_;
147 std::unique_ptr<cricket::SrtpSession> recv_session_;
148 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
149 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
150
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200151 absl::optional<cricket::CryptoParams> send_params_;
152 absl::optional<cricket::CryptoParams> recv_params_;
153 absl::optional<int> send_cipher_suite_;
154 absl::optional<int> recv_cipher_suite_;
Zhi Huange830e682018-03-30 10:48:35 -0700155 rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
156 rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
157
Zhi Huang365381f2018-04-13 16:44:34 -0700158 bool writable_ = false;
159
Zhi Huangcf990f52017-09-22 12:12:30 -0700160 bool external_auth_enabled_ = false;
161
162 int rtp_abs_sendtime_extn_id_ = -1;
erikvarga@webrtc.orgd76a0fc2018-10-09 12:31:28 +0200163
164 int decryption_failure_count_ = 0;
zstein398c3fd2017-07-19 13:38:02 -0700165};
166
167} // namespace webrtc
168
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200169#endif // PC_SRTPTRANSPORT_H_