blob: 4bc028d68e1ff2ed1bdf8be3f9e0220e861c7cf0 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_SRTP_TRANSPORT_H_
12#define PC_SRTP_TRANSPORT_H_
zstein398c3fd2017-07-19 13:38:02 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <cstdint>
zstein398c3fd2017-07-19 13:38:02 -070017#include <memory>
18#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070019#include <vector>
zstein398c3fd2017-07-19 13:38:02 -070020
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/crypto_params.h"
23#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 10:48:35 -070028#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/copy_on_write_buffer.h"
30#include "rtc_base/network_route.h"
zstein398c3fd2017-07-19 13:38:02 -070031
32namespace webrtc {
33
Zhi Huang365381f2018-04-13 16:44:34 -070034// 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 13:38:02 -070038 public:
Zhi Huang2dfc42d2017-12-04 13:38:48 -080039 explicit SrtpTransport(bool rtcp_mux_enabled);
zstein398c3fd2017-07-19 13:38:02 -070040
Zhi Huang365381f2018-04-13 16:44:34 -070041 virtual ~SrtpTransport() = default;
Zhi Huange830e682018-03-30 10:48:35 -070042
43 // SrtpTransportInterface specific implementation.
Bjorn A Mellem34cd4852019-05-24 10:13:10 -070044 virtual RTCError SetSrtpSendKey(const cricket::CryptoParams& params);
45 virtual RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params);
Zhi Huang95e7dbb2018-03-29 00:08:03 +000046
Zhi Huangcf990f52017-09-22 12:12:30 -070047 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
48 const rtc::PacketOptions& options,
49 int flags) override;
50
51 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
52 const rtc::PacketOptions& options,
53 int flags) override;
54
Zhi Huangcf990f52017-09-22 12:12:30 -070055 // The transport becomes active if the send_session_ and recv_session_ are
56 // created.
Zhi Huange830e682018-03-30 10:48:35 -070057 bool IsSrtpActive() const override;
zstein398c3fd2017-07-19 13:38:02 -070058
Zhi Huang365381f2018-04-13 16:44:34 -070059 bool IsWritable(bool rtcp) const override;
60
Zhi Huangcf990f52017-09-22 12:12:30 -070061 // Create new send/recv sessions and set the negotiated crypto keys for RTP
62 // packet encryption. The keys can either come from SDES negotiation or DTLS
63 // handshake.
64 bool SetRtpParams(int send_cs,
65 const uint8_t* send_key,
66 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080067 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070068 int recv_cs,
69 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080070 int recv_key_len,
71 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070072
73 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
74 // packet encryption. The keys can either come from SDES negotiation or DTLS
75 // handshake.
76 bool SetRtcpParams(int send_cs,
77 const uint8_t* send_key,
78 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080079 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070080 int recv_cs,
81 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080082 int recv_key_len,
83 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070084
85 void ResetParams();
86
Zhi Huangcf990f52017-09-22 12:12:30 -070087 // If external auth is enabled, SRTP will write a dummy auth tag that then
88 // later must get replaced before the packet is sent out. Only supported for
89 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
90 // if it is actually used. This method is only valid before the RTP params
91 // have been set.
92 void EnableExternalAuth();
93 bool IsExternalAuthEnabled() const;
94
95 // A SrtpTransport supports external creation of the auth tag if a non-GCM
96 // cipher is used. This method is only valid after the RTP params have
97 // been set.
98 bool IsExternalAuthActive() const;
99
100 // Returns srtp overhead for rtp packets.
101 bool GetSrtpOverhead(int* srtp_overhead) const;
102
103 // Returns rtp auth params from srtp context.
104 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
105
Zhi Huang2a4d70c2017-11-29 15:41:59 -0800106 // Cache RTP Absoulute SendTime extension header ID. This is only used when
107 // external authentication is enabled.
Zhi Huangcf990f52017-09-22 12:12:30 -0700108 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
109 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
110 }
111
Zhi Huang365381f2018-04-13 16:44:34 -0700112 protected:
113 // If the writable state changed, fire the SignalWritableState.
114 void MaybeUpdateWritableState();
Steve Antondb67ba12018-03-19 17:41:42 -0700115
zstein398c3fd2017-07-19 13:38:02 -0700116 private:
117 void ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800118 void CreateSrtpSessions();
zstein398c3fd2017-07-19 13:38:02 -0700119
Amit Hilbuchedd20542019-03-18 12:33:43 -0700120 void OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100121 int64_t packet_time_us) override;
Amit Hilbuchedd20542019-03-18 12:33:43 -0700122 void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100123 int64_t packet_time_us) override;
Zhi Huang365381f2018-04-13 16:44:34 -0700124 void OnNetworkRouteChanged(
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200125 absl::optional<rtc::NetworkRoute> network_route) override;
Zhi Huang95e7dbb2018-03-29 00:08:03 +0000126
Zhi Huang365381f2018-04-13 16:44:34 -0700127 // Override the RtpTransport::OnWritableState.
128 void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800129
Zhi Huangcf990f52017-09-22 12:12:30 -0700130 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 01:12:52 -0700131
Zhi Huangcf990f52017-09-22 12:12:30 -0700132 // Overloaded version, outputs packet index.
133 bool ProtectRtp(void* data,
134 int in_len,
135 int max_len,
136 int* out_len,
137 int64_t* index);
138 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
139
140 // Decrypts/verifies an invidiual RTP/RTCP packet.
141 // If an HMAC is used, this will decrease the packet size.
142 bool UnprotectRtp(void* data, int in_len, int* out_len);
143
144 bool UnprotectRtcp(void* data, int in_len, int* out_len);
145
Zhi Huange830e682018-03-30 10:48:35 -0700146 bool MaybeSetKeyParams();
147 bool ParseKeyParams(const std::string& key_params, uint8_t* key, size_t len);
148
Zhi Huangcf990f52017-09-22 12:12:30 -0700149 const std::string content_name_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700150
151 std::unique_ptr<cricket::SrtpSession> send_session_;
152 std::unique_ptr<cricket::SrtpSession> recv_session_;
153 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
154 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
155
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200156 absl::optional<cricket::CryptoParams> send_params_;
157 absl::optional<cricket::CryptoParams> recv_params_;
158 absl::optional<int> send_cipher_suite_;
159 absl::optional<int> recv_cipher_suite_;
Zhi Huange830e682018-03-30 10:48:35 -0700160 rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
161 rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
162
Zhi Huang365381f2018-04-13 16:44:34 -0700163 bool writable_ = false;
164
Zhi Huangcf990f52017-09-22 12:12:30 -0700165 bool external_auth_enabled_ = false;
166
167 int rtp_abs_sendtime_extn_id_ = -1;
erikvarga@webrtc.orgd76a0fc2018-10-09 12:31:28 +0200168
169 int decryption_failure_count_ = 0;
zstein398c3fd2017-07-19 13:38:02 -0700170};
171
172} // namespace webrtc
173
Steve Anton10542f22019-01-11 09:11:00 -0800174#endif // PC_SRTP_TRANSPORT_H_