blob: 5bc99705309133eaf8085d479a60e5a646a97c0b [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 Huang95e7dbb2018-03-29 00:08:03 +000023#include "pc/rtptransportinternaladapter.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070024#include "pc/srtpsession.h"
Zhi Huange830e682018-03-30 10:48:35 -070025#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
zstein398c3fd2017-07-19 13:38:02 -070027
28namespace webrtc {
29
Zhi Huang95e7dbb2018-03-29 00:08:03 +000030// This class will eventually be a wrapper around RtpTransportInternal
31// that protects and unprotects sent and received RTP packets.
32class SrtpTransport : public RtpTransportInternalAdapter {
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 Huange830e682018-03-30 10:48:35 -070036 explicit SrtpTransport(std::unique_ptr<RtpTransport> rtp_transport);
37
38 virtual ~SrtpTransport() {}
39
40 // SrtpTransportInterface overrides.
41 PacketTransportInterface* GetRtpPacketTransport() const override {
42 return rtp_transport_->GetRtpPacketTransport();
43 }
44 PacketTransportInterface* GetRtcpPacketTransport() const override {
45 return rtp_transport_->GetRtcpPacketTransport();
46 }
47
48 // TODO(zstein): Use these RtcpParameters for configuration elsewhere.
49 RTCError SetParameters(const RtpTransportParameters& parameters) override {
50 return rtp_transport_->SetParameters(parameters);
51 }
52 RtpTransportParameters GetParameters() const override {
53 return rtp_transport_->GetParameters();
54 }
55
56 // SrtpTransportInterface specific implementation.
57 RTCError SetSrtpSendKey(const cricket::CryptoParams& params) override;
58 RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params) override;
Zhi Huang95e7dbb2018-03-29 00:08:03 +000059
Zhi Huangcf990f52017-09-22 12:12:30 -070060 bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
61 const rtc::PacketOptions& options,
62 int flags) override;
63
64 bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
65 const rtc::PacketOptions& options,
66 int flags) override;
67
Zhi Huangcf990f52017-09-22 12:12:30 -070068 // The transport becomes active if the send_session_ and recv_session_ are
69 // created.
Zhi Huange830e682018-03-30 10:48:35 -070070 bool IsSrtpActive() const override;
zstein398c3fd2017-07-19 13:38:02 -070071
Zhi Huangcf990f52017-09-22 12:12:30 -070072 // Create new send/recv sessions and set the negotiated crypto keys for RTP
73 // packet encryption. The keys can either come from SDES negotiation or DTLS
74 // handshake.
75 bool SetRtpParams(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 // Create new send/recv sessions and set the negotiated crypto keys for RTCP
85 // packet encryption. The keys can either come from SDES negotiation or DTLS
86 // handshake.
87 bool SetRtcpParams(int send_cs,
88 const uint8_t* send_key,
89 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -080090 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -070091 int recv_cs,
92 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -080093 int recv_key_len,
94 const std::vector<int>& recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -070095
96 void ResetParams();
97
Zhi Huangcf990f52017-09-22 12:12:30 -070098 // If external auth is enabled, SRTP will write a dummy auth tag that then
99 // later must get replaced before the packet is sent out. Only supported for
100 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
101 // if it is actually used. This method is only valid before the RTP params
102 // have been set.
103 void EnableExternalAuth();
104 bool IsExternalAuthEnabled() const;
105
106 // A SrtpTransport supports external creation of the auth tag if a non-GCM
107 // cipher is used. This method is only valid after the RTP params have
108 // been set.
109 bool IsExternalAuthActive() const;
110
111 // Returns srtp overhead for rtp packets.
112 bool GetSrtpOverhead(int* srtp_overhead) const;
113
114 // Returns rtp auth params from srtp context.
115 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
116
Zhi Huang2a4d70c2017-11-29 15:41:59 -0800117 // Cache RTP Absoulute SendTime extension header ID. This is only used when
118 // external authentication is enabled.
Zhi Huangcf990f52017-09-22 12:12:30 -0700119 void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
120 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
121 }
122
Steve Antondb67ba12018-03-19 17:41:42 -0700123 void SetMetricsObserver(
124 rtc::scoped_refptr<MetricsObserverInterface> metrics_observer) override;
125
zstein398c3fd2017-07-19 13:38:02 -0700126 private:
127 void ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800128 void CreateSrtpSessions();
zstein398c3fd2017-07-19 13:38:02 -0700129
Zhi Huang95e7dbb2018-03-29 00:08:03 +0000130 bool SendPacket(bool rtcp,
131 rtc::CopyOnWriteBuffer* packet,
132 const rtc::PacketOptions& options,
133 int flags);
134
135 void OnPacketReceived(bool rtcp,
136 rtc::CopyOnWriteBuffer* packet,
137 const rtc::PacketTime& packet_time);
138 void OnReadyToSend(bool ready) { SignalReadyToSend(ready); }
139 void OnNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute> network_route);
140
141 void OnWritableState(bool writable) { SignalWritableState(writable); }
142
143 void OnSentPacket(const rtc::SentPacket& sent_packet) {
144 SignalSentPacket(sent_packet);
145 }
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800146
Zhi Huangcf990f52017-09-22 12:12:30 -0700147 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
zhihuangeb23e172017-09-19 01:12:52 -0700148
Zhi Huangcf990f52017-09-22 12:12:30 -0700149 // Overloaded version, outputs packet index.
150 bool ProtectRtp(void* data,
151 int in_len,
152 int max_len,
153 int* out_len,
154 int64_t* index);
155 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
156
157 // Decrypts/verifies an invidiual RTP/RTCP packet.
158 // If an HMAC is used, this will decrease the packet size.
159 bool UnprotectRtp(void* data, int in_len, int* out_len);
160
161 bool UnprotectRtcp(void* data, int in_len, int* out_len);
162
Zhi Huange830e682018-03-30 10:48:35 -0700163 bool MaybeSetKeyParams();
164 bool ParseKeyParams(const std::string& key_params, uint8_t* key, size_t len);
165
Zhi Huangcf990f52017-09-22 12:12:30 -0700166 const std::string content_name_;
Zhi Huange830e682018-03-30 10:48:35 -0700167 std::unique_ptr<RtpTransport> rtp_transport_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700168
169 std::unique_ptr<cricket::SrtpSession> send_session_;
170 std::unique_ptr<cricket::SrtpSession> recv_session_;
171 std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
172 std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
173
Zhi Huange830e682018-03-30 10:48:35 -0700174 rtc::Optional<cricket::CryptoParams> send_params_;
175 rtc::Optional<cricket::CryptoParams> recv_params_;
176 rtc::Optional<int> send_cipher_suite_;
177 rtc::Optional<int> recv_cipher_suite_;
178 rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
179 rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
180
Zhi Huangcf990f52017-09-22 12:12:30 -0700181 bool external_auth_enabled_ = false;
182
183 int rtp_abs_sendtime_extn_id_ = -1;
Steve Antondb67ba12018-03-19 17:41:42 -0700184
185 rtc::scoped_refptr<MetricsObserverInterface> metrics_observer_;
zstein398c3fd2017-07-19 13:38:02 -0700186};
187
188} // namespace webrtc
189
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200190#endif // PC_SRTPTRANSPORT_H_