OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2009 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_PC_SRTPFILTER_H_ | 11 #ifndef WEBRTC_PC_SRTPFILTER_H_ |
12 #define WEBRTC_PC_SRTPFILTER_H_ | 12 #define WEBRTC_PC_SRTPFILTER_H_ |
13 | 13 |
14 #include <list> | 14 #include <list> |
15 #include <map> | 15 #include <map> |
16 #include <memory> | 16 #include <memory> |
17 #include <string> | 17 #include <string> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 #include "webrtc/media/base/cryptoparams.h" | 20 #include "webrtc/media/base/cryptoparams.h" |
21 #include "webrtc/p2p/base/sessiondescription.h" | 21 #include "webrtc/p2p/base/sessiondescription.h" |
22 #include "webrtc/rtc_base/basictypes.h" | 22 #include "webrtc/rtc_base/basictypes.h" |
23 #include "webrtc/rtc_base/buffer.h" | |
24 #include "webrtc/rtc_base/constructormagic.h" | 23 #include "webrtc/rtc_base/constructormagic.h" |
25 #include "webrtc/rtc_base/criticalsection.h" | 24 #include "webrtc/rtc_base/criticalsection.h" |
26 #include "webrtc/rtc_base/optional.h" | |
27 #include "webrtc/rtc_base/sslstreamadapter.h" | 25 #include "webrtc/rtc_base/sslstreamadapter.h" |
28 #include "webrtc/rtc_base/thread_checker.h" | 26 #include "webrtc/rtc_base/thread_checker.h" |
29 | 27 |
30 // Forward declaration to avoid pulling in libsrtp headers here | 28 // Forward declaration to avoid pulling in libsrtp headers here |
31 struct srtp_event_data_t; | 29 struct srtp_event_data_t; |
32 struct srtp_ctx_t_; | 30 struct srtp_ctx_t_; |
33 | 31 |
34 namespace cricket { | 32 namespace cricket { |
35 | 33 |
| 34 class SrtpSession; |
| 35 |
36 void ShutdownSrtp(); | 36 void ShutdownSrtp(); |
37 | 37 |
38 // A helper class used to negotiate SDES crypto params. | 38 // Class to transform SRTP to/from RTP. |
39 // TODO(zhihuang): Find a better name for this class, like "SdesNegotiator". | 39 // Initialize by calling SetSend with the local security params, then call |
| 40 // SetRecv once the remote security params are received. At that point |
| 41 // Protect/UnprotectRt(c)p can be called to encrypt/decrypt data. |
| 42 // TODO: Figure out concurrency policy for SrtpFilter. |
40 class SrtpFilter { | 43 class SrtpFilter { |
41 public: | 44 public: |
42 enum Mode { | 45 enum Mode { |
43 PROTECT, | 46 PROTECT, |
44 UNPROTECT | 47 UNPROTECT |
45 }; | 48 }; |
46 enum Error { | 49 enum Error { |
47 ERROR_NONE, | 50 ERROR_NONE, |
48 ERROR_FAIL, | 51 ERROR_FAIL, |
49 ERROR_AUTH, | 52 ERROR_AUTH, |
(...skipping 16 matching lines...) Expand all Loading... |
66 bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params, | 69 bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params, |
67 ContentSource source); | 70 ContentSource source); |
68 // Indicates which crypto algorithms and keys were contained in the answer. | 71 // Indicates which crypto algorithms and keys were contained in the answer. |
69 // answer_params should contain the negotiated parameters, which may be none, | 72 // answer_params should contain the negotiated parameters, which may be none, |
70 // if crypto was not desired or could not be negotiated (and not required). | 73 // if crypto was not desired or could not be negotiated (and not required). |
71 // This must be called after SetOffer. If crypto negotiation completes | 74 // This must be called after SetOffer. If crypto negotiation completes |
72 // successfully, this will advance the filter to the active state. | 75 // successfully, this will advance the filter to the active state. |
73 bool SetAnswer(const std::vector<CryptoParams>& answer_params, | 76 bool SetAnswer(const std::vector<CryptoParams>& answer_params, |
74 ContentSource source); | 77 ContentSource source); |
75 | 78 |
| 79 // Set the header extension ids that should be encrypted for the given source. |
| 80 void SetEncryptedHeaderExtensionIds(ContentSource source, |
| 81 const std::vector<int>& extension_ids); |
| 82 |
| 83 // Just set up both sets of keys directly. |
| 84 // Used with DTLS-SRTP. |
| 85 bool SetRtpParams(int send_cs, |
| 86 const uint8_t* send_key, |
| 87 int send_key_len, |
| 88 int recv_cs, |
| 89 const uint8_t* recv_key, |
| 90 int recv_key_len); |
| 91 bool UpdateRtpParams(int send_cs, |
| 92 const uint8_t* send_key, |
| 93 int send_key_len, |
| 94 int recv_cs, |
| 95 const uint8_t* recv_key, |
| 96 int recv_key_len); |
| 97 bool SetRtcpParams(int send_cs, |
| 98 const uint8_t* send_key, |
| 99 int send_key_len, |
| 100 int recv_cs, |
| 101 const uint8_t* recv_key, |
| 102 int recv_key_len); |
| 103 |
| 104 // Encrypts/signs an individual RTP/RTCP packet, in-place. |
| 105 // If an HMAC is used, this will increase the packet size. |
| 106 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len); |
| 107 // Overloaded version, outputs packet index. |
| 108 bool ProtectRtp(void* data, |
| 109 int in_len, |
| 110 int max_len, |
| 111 int* out_len, |
| 112 int64_t* index); |
| 113 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len); |
| 114 // Decrypts/verifies an invidiual RTP/RTCP packet. |
| 115 // If an HMAC is used, this will decrease the packet size. |
| 116 bool UnprotectRtp(void* data, int in_len, int* out_len); |
| 117 bool UnprotectRtcp(void* data, int in_len, int* out_len); |
| 118 |
| 119 // Returns rtp auth params from srtp context. |
| 120 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len); |
| 121 |
| 122 // Returns srtp overhead for rtp packets. |
| 123 bool GetSrtpOverhead(int* srtp_overhead) const; |
| 124 |
| 125 // If external auth is enabled, SRTP will write a dummy auth tag that then |
| 126 // later must get replaced before the packet is sent out. Only supported for |
| 127 // non-GCM cipher suites and can be checked through "IsExternalAuthActive" |
| 128 // if it is actually used. This method is only valid before the RTP params |
| 129 // have been set. |
| 130 void EnableExternalAuth(); |
| 131 bool IsExternalAuthEnabled() const; |
| 132 |
| 133 // A SRTP filter supports external creation of the auth tag if a non-GCM |
| 134 // cipher is used. This method is only valid after the RTP params have |
| 135 // been set. |
| 136 bool IsExternalAuthActive() const; |
| 137 |
76 bool ResetParams(); | 138 bool ResetParams(); |
77 | 139 |
78 rtc::Optional<int> send_cipher_suite() { return send_cipher_suite_; } | |
79 rtc::Optional<int> recv_cipher_suite() { return recv_cipher_suite_; } | |
80 | |
81 const rtc::Buffer& send_key() { return send_key_; } | |
82 const rtc::Buffer& recv_key() { return recv_key_; } | |
83 | |
84 protected: | 140 protected: |
85 bool ExpectOffer(ContentSource source); | 141 bool ExpectOffer(ContentSource source); |
86 | |
87 bool StoreParams(const std::vector<CryptoParams>& params, | 142 bool StoreParams(const std::vector<CryptoParams>& params, |
88 ContentSource source); | 143 ContentSource source); |
89 | |
90 bool ExpectAnswer(ContentSource source); | 144 bool ExpectAnswer(ContentSource source); |
91 | |
92 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params, | 145 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params, |
93 ContentSource source, | 146 ContentSource source, |
94 bool final); | 147 bool final); |
95 | 148 void CreateSrtpSessions(); |
96 bool NegotiateParams(const std::vector<CryptoParams>& answer_params, | 149 bool NegotiateParams(const std::vector<CryptoParams>& answer_params, |
97 CryptoParams* selected_params); | 150 CryptoParams* selected_params); |
98 | 151 bool ApplyParams(const CryptoParams& send_params, |
99 private: | 152 const CryptoParams& recv_params); |
100 bool ApplySendParams(const CryptoParams& send_params); | |
101 | |
102 bool ApplyRecvParams(const CryptoParams& recv_params); | |
103 | |
104 static bool ParseKeyParams(const std::string& params, | 153 static bool ParseKeyParams(const std::string& params, |
105 uint8_t* key, | 154 uint8_t* key, |
106 size_t len); | 155 size_t len); |
107 | 156 |
| 157 private: |
108 enum State { | 158 enum State { |
109 ST_INIT, // SRTP filter unused. | 159 ST_INIT, // SRTP filter unused. |
110 ST_SENTOFFER, // Offer with SRTP parameters sent. | 160 ST_SENTOFFER, // Offer with SRTP parameters sent. |
111 ST_RECEIVEDOFFER, // Offer with SRTP parameters received. | 161 ST_RECEIVEDOFFER, // Offer with SRTP parameters received. |
112 ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto. | 162 ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto. |
113 // Received provisional answer without crypto. | 163 // Received provisional answer without crypto. |
114 ST_RECEIVEDPRANSWER_NO_CRYPTO, | 164 ST_RECEIVEDPRANSWER_NO_CRYPTO, |
115 ST_ACTIVE, // Offer and answer set. | 165 ST_ACTIVE, // Offer and answer set. |
116 // SRTP filter is active but new parameters are offered. | 166 // SRTP filter is active but new parameters are offered. |
117 // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT. | 167 // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT. |
118 ST_SENTUPDATEDOFFER, | 168 ST_SENTUPDATEDOFFER, |
119 // SRTP filter is active but new parameters are received. | 169 // SRTP filter is active but new parameters are received. |
120 // When the answer is set, the state transitions back to ST_ACTIVE. | 170 // When the answer is set, the state transitions back to ST_ACTIVE. |
121 ST_RECEIVEDUPDATEDOFFER, | 171 ST_RECEIVEDUPDATEDOFFER, |
122 // SRTP filter is active but the sent answer is only provisional. | 172 // SRTP filter is active but the sent answer is only provisional. |
123 // When the final answer is set, the state transitions to ST_ACTIVE or | 173 // When the final answer is set, the state transitions to ST_ACTIVE or |
124 // ST_INIT. | 174 // ST_INIT. |
125 ST_SENTPRANSWER, | 175 ST_SENTPRANSWER, |
126 // SRTP filter is active but the received answer is only provisional. | 176 // SRTP filter is active but the received answer is only provisional. |
127 // When the final answer is set, the state transitions to ST_ACTIVE or | 177 // When the final answer is set, the state transitions to ST_ACTIVE or |
128 // ST_INIT. | 178 // ST_INIT. |
129 ST_RECEIVEDPRANSWER | 179 ST_RECEIVEDPRANSWER |
130 }; | 180 }; |
131 State state_ = ST_INIT; | 181 State state_ = ST_INIT; |
| 182 bool external_auth_enabled_ = false; |
132 std::vector<CryptoParams> offer_params_; | 183 std::vector<CryptoParams> offer_params_; |
| 184 std::unique_ptr<SrtpSession> send_session_; |
| 185 std::unique_ptr<SrtpSession> recv_session_; |
| 186 std::unique_ptr<SrtpSession> send_rtcp_session_; |
| 187 std::unique_ptr<SrtpSession> recv_rtcp_session_; |
133 CryptoParams applied_send_params_; | 188 CryptoParams applied_send_params_; |
134 CryptoParams applied_recv_params_; | 189 CryptoParams applied_recv_params_; |
135 rtc::Optional<int> send_cipher_suite_; | 190 std::vector<int> send_encrypted_header_extension_ids_; |
136 rtc::Optional<int> recv_cipher_suite_; | 191 std::vector<int> recv_encrypted_header_extension_ids_; |
137 rtc::Buffer send_key_; | |
138 rtc::Buffer recv_key_; | |
139 }; | 192 }; |
140 | 193 |
141 } // namespace cricket | 194 } // namespace cricket |
142 | 195 |
143 #endif // WEBRTC_PC_SRTPFILTER_H_ | 196 #endif // WEBRTC_PC_SRTPFILTER_H_ |
OLD | NEW |