blob: dd86006ee1772c3770e2044991203f98b801c04b [file] [log] [blame]
zstein398c3fd2017-07-19 20:38:021/*
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
Steve Anton10542f22019-01-11 17:11:0011#ifndef PC_SRTP_TRANSPORT_H_
12#define PC_SRTP_TRANSPORT_H_
zstein398c3fd2017-07-19 20:38:0213
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Yves Gerey3e707812018-11-28 15:47:4916#include <cstdint>
zstein398c3fd2017-07-19 20:38:0217#include <memory>
Florent Castelli8037fc62024-08-29 13:00:4018#include <optional>
zstein398c3fd2017-07-19 20:38:0219#include <string>
Steve Anton36b29d12017-10-30 16:57:4220#include <vector>
zstein398c3fd2017-07-19 20:38:0221
Jonas Orelande62c2f22022-03-29 09:04:4822#include "api/field_trials_view.h"
Steve Anton10542f22019-01-11 17:11:0023#include "api/rtc_error.h"
24#include "p2p/base/packet_transport_internal.h"
25#include "pc/rtp_transport.h"
26#include "pc/srtp_session.h"
27#include "rtc_base/async_packet_socket.h"
Zhi Huange830e682018-03-30 17:48:3528#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 17:11:0029#include "rtc_base/copy_on_write_buffer.h"
30#include "rtc_base/network_route.h"
zstein398c3fd2017-07-19 20:38:0231
32namespace webrtc {
33
Zhi Huang365381f2018-04-13 23:44:3434// This subclass of the RtpTransport is used for SRTP which is reponsible for
35// protecting/unprotecting the packets. It provides interfaces to set the crypto
36// parameters for the SrtpSession underneath.
37class SrtpTransport : public RtpTransport {
zstein398c3fd2017-07-19 20:38:0238 public:
Jonas Orelande62c2f22022-03-29 09:04:4839 SrtpTransport(bool rtcp_mux_enabled, const FieldTrialsView& field_trials);
zstein398c3fd2017-07-19 20:38:0240
Zhi Huang365381f2018-04-13 23:44:3441 virtual ~SrtpTransport() = default;
Zhi Huange830e682018-03-30 17:48:3542
Zhi Huangcf990f52017-09-22 19:12:3043 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
44 const rtc::PacketOptions& options,
45 int flags) override;
46
47 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
48 const rtc::PacketOptions& options,
49 int flags) override;
50
Zhi Huangcf990f52017-09-22 19:12:3051 // The transport becomes active if the send_session_ and recv_session_ are
52 // created.
Zhi Huange830e682018-03-30 17:48:3553 bool IsSrtpActive() const override;
zstein398c3fd2017-07-19 20:38:0254
Zhi Huang365381f2018-04-13 23:44:3455 bool IsWritable(bool rtcp) const override;
56
Zhi Huangcf990f52017-09-22 19:12:3057 // Create new send/recv sessions and set the negotiated crypto keys for RTP
58 // packet encryption. The keys can either come from SDES negotiation or DTLS
59 // handshake.
Philipp Hancke55b89a82023-08-01 09:34:0560 bool SetRtpParams(int send_crypto_suite,
Philipp Hancke9a653392024-09-14 03:30:1261 const rtc::ZeroOnFreeBuffer<uint8_t>& send_key,
Zhi Huangc99b6c72017-11-11 00:44:4662 const std::vector<int>& send_extension_ids,
Philipp Hancke55b89a82023-08-01 09:34:0563 int recv_crypto_suite,
Philipp Hancke9a653392024-09-14 03:30:1264 const rtc::ZeroOnFreeBuffer<uint8_t>& recv_key,
Zhi Huangc99b6c72017-11-11 00:44:4665 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 19:12:3066
67 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
68 // packet encryption. The keys can either come from SDES negotiation or DTLS
69 // handshake.
Philipp Hancke55b89a82023-08-01 09:34:0570 bool SetRtcpParams(int send_crypto_suite,
Philipp Hancke9a653392024-09-14 03:30:1271 const rtc::ZeroOnFreeBuffer<uint8_t>& send_key,
Zhi Huangc99b6c72017-11-11 00:44:4672 const std::vector<int>& send_extension_ids,
Philipp Hancke55b89a82023-08-01 09:34:0573 int recv_crypto_suite,
Philipp Hancke9a653392024-09-14 03:30:1274 const rtc::ZeroOnFreeBuffer<uint8_t>& recv_key,
Zhi Huangc99b6c72017-11-11 00:44:4675 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 19:12:3076
77 void ResetParams();
78
Zhi Huangcf990f52017-09-22 19:12:3079 // If external auth is enabled, SRTP will write a dummy auth tag that then
80 // later must get replaced before the packet is sent out. Only supported for
Philipp Hancke55b89a82023-08-01 09:34:0581 // non-GCM crypto suites and can be checked through "IsExternalAuthActive"
Zhi Huangcf990f52017-09-22 19:12:3082 // if it is actually used. This method is only valid before the RTP params
83 // have been set.
84 void EnableExternalAuth();
85 bool IsExternalAuthEnabled() const;
86
87 // A SrtpTransport supports external creation of the auth tag if a non-GCM
88 // cipher is used. This method is only valid after the RTP params have
89 // been set.
90 bool IsExternalAuthActive() const;
91
92 // Returns srtp overhead for rtp packets.
93 bool GetSrtpOverhead(int* srtp_overhead) const;
94
95 // Returns rtp auth params from srtp context.
96 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
97
Zhi Huang2a4d70c2017-11-29 23:41:5998 // Cache RTP Absoulute SendTime extension header ID. This is only used when
99 // external authentication is enabled.
Zhi Huangcf990f52017-09-22 19:12:30100 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
101 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
102 }
103
Philipp Hancke977b56c2023-11-08 09:20:51104 // In addition to unregistering the sink, the SRTP transport
105 // disassociates all SSRCs of the sink from libSRTP.
106 bool UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) override;
107
Zhi Huang365381f2018-04-13 23:44:34108 protected:
109 // If the writable state changed, fire the SignalWritableState.
110 void MaybeUpdateWritableState();
Steve Antondb67ba12018-03-20 00:41:42111
zstein398c3fd2017-07-19 20:38:02112 private:
113 void ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 18:41:57114 void CreateSrtpSessions();
zstein398c3fd2017-07-19 20:38:02115
Per K9e0bf9b2024-02-27 08:25:11116 void OnRtpPacketReceived(const rtc::ReceivedPacket& packet) override;
117 void OnRtcpPacketReceived(const rtc::ReceivedPacket& packet) override;
Zhi Huang365381f2018-04-13 23:44:34118 void OnNetworkRouteChanged(
Florent Castelli8037fc62024-08-29 13:00:40119 std::optional<rtc::NetworkRoute> network_route) override;
Zhi Huang95e7dbb2018-03-29 00:08:03120
Zhi Huang365381f2018-04-13 23:44:34121 // Override the RtpTransport::OnWritableState.
122 void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
Zhi Huangcd3fc5d2017-11-29 18:41:57123
Zhi Huangcf990f52017-09-22 19:12:30124 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 08:12:52125
Zhi Huangcf990f52017-09-22 19:12:30126 // Overloaded version, outputs packet index.
127 bool ProtectRtp(void* data,
128 int in_len,
129 int max_len,
130 int* out_len,
131 int64_t* index);
132 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
133
134 // Decrypts/verifies an invidiual RTP/RTCP packet.
135 // If an HMAC is used, this will decrease the packet size.
136 bool UnprotectRtp(void* data, int in_len, int* out_len);
137
138 bool UnprotectRtcp(void* data, int in_len, int* out_len);
139
140 const std::string content_name_;
Zhi Huangcf990f52017-09-22 19:12:30141
142 std::unique_ptr<cricket::SrtpSession> send_session_;
143 std::unique_ptr<cricket::SrtpSession> recv_session_;
144 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
145 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
146
Florent Castelli8037fc62024-08-29 13:00:40147 std::optional<int> send_crypto_suite_;
148 std::optional<int> recv_crypto_suite_;
Zhi Huange830e682018-03-30 17:48:35149 rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
150 rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
151
Zhi Huang365381f2018-04-13 23:44:34152 bool writable_ = false;
153
Zhi Huangcf990f52017-09-22 19:12:30154 bool external_auth_enabled_ = false;
155
156 int rtp_abs_sendtime_extn_id_ = -1;
erikvarga@webrtc.orgd76a0fc2018-10-09 10:31:28157
158 int decryption_failure_count_ = 0;
Jonas Orelanded99dae2022-03-09 08:28:10159
Jonas Orelande62c2f22022-03-29 09:04:48160 const FieldTrialsView& field_trials_;
zstein398c3fd2017-07-19 20:38:02161};
162
163} // namespace webrtc
164
Steve Anton10542f22019-01-11 17:11:00165#endif // PC_SRTP_TRANSPORT_H_