blob: ed923792f1d53b34a36a1605e5a562ae5db4c244 [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>
15#include <cstdint>
zstein398c3fd2017-07-19 13:38:02 -070016#include <memory>
17#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070018#include <vector>
zstein398c3fd2017-07-19 13:38:02 -070019
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/crypto_params.h"
22#include "api/rtc_error.h"
23#include "p2p/base/packet_transport_internal.h"
24#include "pc/rtp_transport.h"
25#include "pc/srtp_session.h"
26#include "rtc_base/async_packet_socket.h"
Zhi Huange830e682018-03-30 10:48:35 -070027#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/copy_on_write_buffer.h"
29#include "rtc_base/network_route.h"
zstein398c3fd2017-07-19 13:38:02 -070030
31namespace webrtc {
32
Zhi Huang365381f2018-04-13 16:44:34 -070033// This subclass of the RtpTransport is used for SRTP which is reponsible for
34// protecting/unprotecting the packets. It provides interfaces to set the crypto
35// parameters for the SrtpSession underneath.
36class SrtpTransport : public RtpTransport {
zstein398c3fd2017-07-19 13:38:02 -070037 public:
Zhi Huang2dfc42d2017-12-04 13:38:48 -080038 explicit SrtpTransport(bool rtcp_mux_enabled);
zstein398c3fd2017-07-19 13:38:02 -070039
Zhi Huang365381f2018-04-13 16:44:34 -070040 virtual ~SrtpTransport() = default;
Zhi Huange830e682018-03-30 10:48:35 -070041
42 // SrtpTransportInterface specific implementation.
Bjorn A Mellem34cd4852019-05-24 10:13:10 -070043 virtual RTCError SetSrtpSendKey(const cricket::CryptoParams& params);
44 virtual RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params);
Zhi Huang95e7dbb2018-03-29 00:08:03 +000045
Zhi Huangcf990f52017-09-22 12:12:30 -070046 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
47 const rtc::PacketOptions& options,
48 int flags) override;
49
50 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
51 const rtc::PacketOptions& options,
52 int flags) override;
53
Zhi Huangcf990f52017-09-22 12:12:30 -070054 // The transport becomes active if the send_session_ and recv_session_ are
55 // created.
Zhi Huange830e682018-03-30 10:48:35 -070056 bool IsSrtpActive() const override;
zstein398c3fd2017-07-19 13:38:02 -070057
Zhi Huang365381f2018-04-13 16:44:34 -070058 bool IsWritable(bool rtcp) const override;
59
Zhi Huangcf990f52017-09-22 12:12:30 -070060 // Create new send/recv sessions and set the negotiated crypto keys for RTP
61 // packet encryption. The keys can either come from SDES negotiation or DTLS
62 // handshake.
63 bool SetRtpParams(int send_cs,
64 const uint8_t* send_key,
65 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080066 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070067 int recv_cs,
68 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080069 int recv_key_len,
70 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070071
72 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
73 // packet encryption. The keys can either come from SDES negotiation or DTLS
74 // handshake.
75 bool SetRtcpParams(int send_cs,
76 const uint8_t* send_key,
77 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080078 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070079 int recv_cs,
80 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080081 int recv_key_len,
82 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070083
84 void ResetParams();
85
Zhi Huangcf990f52017-09-22 12:12:30 -070086 // If external auth is enabled, SRTP will write a dummy auth tag that then
87 // later must get replaced before the packet is sent out. Only supported for
88 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
89 // if it is actually used. This method is only valid before the RTP params
90 // have been set.
91 void EnableExternalAuth();
92 bool IsExternalAuthEnabled() const;
93
94 // A SrtpTransport supports external creation of the auth tag if a non-GCM
95 // cipher is used. This method is only valid after the RTP params have
96 // been set.
97 bool IsExternalAuthActive() const;
98
99 // Returns srtp overhead for rtp packets.
100 bool GetSrtpOverhead(int* srtp_overhead) const;
101
102 // Returns rtp auth params from srtp context.
103 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
104
Zhi Huang2a4d70c2017-11-29 15:41:59 -0800105 // Cache RTP Absoulute SendTime extension header ID. This is only used when
106 // external authentication is enabled.
Zhi Huangcf990f52017-09-22 12:12:30 -0700107 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
108 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
109 }
110
Zhi Huang365381f2018-04-13 16:44:34 -0700111 protected:
112 // If the writable state changed, fire the SignalWritableState.
113 void MaybeUpdateWritableState();
Steve Antondb67ba12018-03-19 17:41:42 -0700114
zstein398c3fd2017-07-19 13:38:02 -0700115 private:
116 void ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800117 void CreateSrtpSessions();
zstein398c3fd2017-07-19 13:38:02 -0700118
Amit Hilbuchedd20542019-03-18 12:33:43 -0700119 void OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100120 int64_t packet_time_us) override;
Amit Hilbuchedd20542019-03-18 12:33:43 -0700121 void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100122 int64_t packet_time_us) override;
Zhi Huang365381f2018-04-13 16:44:34 -0700123 void OnNetworkRouteChanged(
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200124 absl::optional<rtc::NetworkRoute> network_route) override;
Zhi Huang95e7dbb2018-03-29 00:08:03 +0000125
Zhi Huang365381f2018-04-13 16:44:34 -0700126 // Override the RtpTransport::OnWritableState.
127 void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800128
Zhi Huangcf990f52017-09-22 12:12:30 -0700129 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 01:12:52 -0700130
Zhi Huangcf990f52017-09-22 12:12:30 -0700131 // Overloaded version, outputs packet index.
132 bool ProtectRtp(void* data,
133 int in_len,
134 int max_len,
135 int* out_len,
136 int64_t* index);
137 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
138
139 // Decrypts/verifies an invidiual RTP/RTCP packet.
140 // If an HMAC is used, this will decrease the packet size.
141 bool UnprotectRtp(void* data, int in_len, int* out_len);
142
143 bool UnprotectRtcp(void* data, int in_len, int* out_len);
144
Zhi Huange830e682018-03-30 10:48:35 -0700145 bool MaybeSetKeyParams();
146 bool ParseKeyParams(const std::string& key_params, uint8_t* key, size_t len);
147
Zhi Huangcf990f52017-09-22 12:12:30 -0700148 const std::string content_name_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700149
150 std::unique_ptr<cricket::SrtpSession> send_session_;
151 std::unique_ptr<cricket::SrtpSession> recv_session_;
152 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
153 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
154
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200155 absl::optional<cricket::CryptoParams> send_params_;
156 absl::optional<cricket::CryptoParams> recv_params_;
157 absl::optional<int> send_cipher_suite_;
158 absl::optional<int> recv_cipher_suite_;
Zhi Huange830e682018-03-30 10:48:35 -0700159 rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
160 rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
161
Zhi Huang365381f2018-04-13 16:44:34 -0700162 bool writable_ = false;
163
Zhi Huangcf990f52017-09-22 12:12:30 -0700164 bool external_auth_enabled_ = false;
165
166 int rtp_abs_sendtime_extn_id_ = -1;
erikvarga@webrtc.orgd76a0fc2018-10-09 12:31:28 +0200167
168 int decryption_failure_count_ = 0;
zstein398c3fd2017-07-19 13:38:02 -0700169};
170
171} // namespace webrtc
172
Steve Anton10542f22019-01-11 09:11:00 -0800173#endif // PC_SRTP_TRANSPORT_H_