| 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" |
| 23 #include "webrtc/rtc_base/constructormagic.h" | 24 #include "webrtc/rtc_base/constructormagic.h" |
| 24 #include "webrtc/rtc_base/criticalsection.h" | 25 #include "webrtc/rtc_base/criticalsection.h" |
| 26 #include "webrtc/rtc_base/optional.h" |
| 25 #include "webrtc/rtc_base/sslstreamadapter.h" | 27 #include "webrtc/rtc_base/sslstreamadapter.h" |
| 26 #include "webrtc/rtc_base/thread_checker.h" | 28 #include "webrtc/rtc_base/thread_checker.h" |
| 27 | 29 |
| 28 // Forward declaration to avoid pulling in libsrtp headers here | 30 // Forward declaration to avoid pulling in libsrtp headers here |
| 29 struct srtp_event_data_t; | 31 struct srtp_event_data_t; |
| 30 struct srtp_ctx_t_; | 32 struct srtp_ctx_t_; |
| 31 | 33 |
| 32 namespace cricket { | 34 namespace cricket { |
| 33 | 35 |
| 34 class SrtpSession; | |
| 35 | |
| 36 void ShutdownSrtp(); | 36 void ShutdownSrtp(); |
| 37 | 37 |
| 38 // Class to transform SRTP to/from RTP. | 38 // A helper class used to negotiate SDES crypto params. |
| 39 // Initialize by calling SetSend with the local security params, then call | 39 // TODO(zhihuang): Find a better name for this class, like "SdesNegotiator". |
| 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. | |
| 43 class SrtpFilter { | 40 class SrtpFilter { |
| 44 public: | 41 public: |
| 45 enum Mode { | 42 enum Mode { |
| 46 PROTECT, | 43 PROTECT, |
| 47 UNPROTECT | 44 UNPROTECT |
| 48 }; | 45 }; |
| 49 enum Error { | 46 enum Error { |
| 50 ERROR_NONE, | 47 ERROR_NONE, |
| 51 ERROR_FAIL, | 48 ERROR_FAIL, |
| 52 ERROR_AUTH, | 49 ERROR_AUTH, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 69 bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params, | 66 bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params, |
| 70 ContentSource source); | 67 ContentSource source); |
| 71 // Indicates which crypto algorithms and keys were contained in the answer. | 68 // Indicates which crypto algorithms and keys were contained in the answer. |
| 72 // answer_params should contain the negotiated parameters, which may be none, | 69 // answer_params should contain the negotiated parameters, which may be none, |
| 73 // if crypto was not desired or could not be negotiated (and not required). | 70 // if crypto was not desired or could not be negotiated (and not required). |
| 74 // This must be called after SetOffer. If crypto negotiation completes | 71 // This must be called after SetOffer. If crypto negotiation completes |
| 75 // successfully, this will advance the filter to the active state. | 72 // successfully, this will advance the filter to the active state. |
| 76 bool SetAnswer(const std::vector<CryptoParams>& answer_params, | 73 bool SetAnswer(const std::vector<CryptoParams>& answer_params, |
| 77 ContentSource source); | 74 ContentSource source); |
| 78 | 75 |
| 79 // Set the header extension ids that should be encrypted for the given source. | 76 bool ResetParams(); |
| 80 void SetEncryptedHeaderExtensionIds(ContentSource source, | |
| 81 const std::vector<int>& extension_ids); | |
| 82 | 77 |
| 83 // Just set up both sets of keys directly. | 78 rtc::Optional<int> send_cipher_suite() { return send_cipher_suite_; } |
| 84 // Used with DTLS-SRTP. | 79 rtc::Optional<int> recv_cipher_suite() { return recv_cipher_suite_; } |
| 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 | 80 |
| 104 // Encrypts/signs an individual RTP/RTCP packet, in-place. | 81 const rtc::Buffer& send_key() { return send_key_; } |
| 105 // If an HMAC is used, this will increase the packet size. | 82 const rtc::Buffer& recv_key() { return recv_key_; } |
| 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 | |
| 138 bool ResetParams(); | |
| 139 | 83 |
| 140 protected: | 84 protected: |
| 141 bool ExpectOffer(ContentSource source); | 85 bool ExpectOffer(ContentSource source); |
| 86 |
| 142 bool StoreParams(const std::vector<CryptoParams>& params, | 87 bool StoreParams(const std::vector<CryptoParams>& params, |
| 143 ContentSource source); | 88 ContentSource source); |
| 89 |
| 144 bool ExpectAnswer(ContentSource source); | 90 bool ExpectAnswer(ContentSource source); |
| 91 |
| 145 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params, | 92 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params, |
| 146 ContentSource source, | 93 ContentSource source, |
| 147 bool final); | 94 bool final); |
| 148 void CreateSrtpSessions(); | 95 |
| 149 bool NegotiateParams(const std::vector<CryptoParams>& answer_params, | 96 bool NegotiateParams(const std::vector<CryptoParams>& answer_params, |
| 150 CryptoParams* selected_params); | 97 CryptoParams* selected_params); |
| 151 bool ApplyParams(const CryptoParams& send_params, | 98 |
| 152 const CryptoParams& recv_params); | 99 private: |
| 100 bool ApplySendParams(const CryptoParams& send_params); |
| 101 |
| 102 bool ApplyRecvParams(const CryptoParams& recv_params); |
| 103 |
| 153 static bool ParseKeyParams(const std::string& params, | 104 static bool ParseKeyParams(const std::string& params, |
| 154 uint8_t* key, | 105 uint8_t* key, |
| 155 size_t len); | 106 size_t len); |
| 156 | 107 |
| 157 private: | |
| 158 enum State { | 108 enum State { |
| 159 ST_INIT, // SRTP filter unused. | 109 ST_INIT, // SRTP filter unused. |
| 160 ST_SENTOFFER, // Offer with SRTP parameters sent. | 110 ST_SENTOFFER, // Offer with SRTP parameters sent. |
| 161 ST_RECEIVEDOFFER, // Offer with SRTP parameters received. | 111 ST_RECEIVEDOFFER, // Offer with SRTP parameters received. |
| 162 ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto. | 112 ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto. |
| 163 // Received provisional answer without crypto. | 113 // Received provisional answer without crypto. |
| 164 ST_RECEIVEDPRANSWER_NO_CRYPTO, | 114 ST_RECEIVEDPRANSWER_NO_CRYPTO, |
| 165 ST_ACTIVE, // Offer and answer set. | 115 ST_ACTIVE, // Offer and answer set. |
| 166 // SRTP filter is active but new parameters are offered. | 116 // SRTP filter is active but new parameters are offered. |
| 167 // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT. | 117 // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT. |
| 168 ST_SENTUPDATEDOFFER, | 118 ST_SENTUPDATEDOFFER, |
| 169 // SRTP filter is active but new parameters are received. | 119 // SRTP filter is active but new parameters are received. |
| 170 // When the answer is set, the state transitions back to ST_ACTIVE. | 120 // When the answer is set, the state transitions back to ST_ACTIVE. |
| 171 ST_RECEIVEDUPDATEDOFFER, | 121 ST_RECEIVEDUPDATEDOFFER, |
| 172 // SRTP filter is active but the sent answer is only provisional. | 122 // SRTP filter is active but the sent answer is only provisional. |
| 173 // When the final answer is set, the state transitions to ST_ACTIVE or | 123 // When the final answer is set, the state transitions to ST_ACTIVE or |
| 174 // ST_INIT. | 124 // ST_INIT. |
| 175 ST_SENTPRANSWER, | 125 ST_SENTPRANSWER, |
| 176 // SRTP filter is active but the received answer is only provisional. | 126 // SRTP filter is active but the received answer is only provisional. |
| 177 // When the final answer is set, the state transitions to ST_ACTIVE or | 127 // When the final answer is set, the state transitions to ST_ACTIVE or |
| 178 // ST_INIT. | 128 // ST_INIT. |
| 179 ST_RECEIVEDPRANSWER | 129 ST_RECEIVEDPRANSWER |
| 180 }; | 130 }; |
| 181 State state_ = ST_INIT; | 131 State state_ = ST_INIT; |
| 182 bool external_auth_enabled_ = false; | |
| 183 std::vector<CryptoParams> offer_params_; | 132 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_; | |
| 188 CryptoParams applied_send_params_; | 133 CryptoParams applied_send_params_; |
| 189 CryptoParams applied_recv_params_; | 134 CryptoParams applied_recv_params_; |
| 190 std::vector<int> send_encrypted_header_extension_ids_; | 135 rtc::Optional<int> send_cipher_suite_; |
| 191 std::vector<int> recv_encrypted_header_extension_ids_; | 136 rtc::Optional<int> recv_cipher_suite_; |
| 137 rtc::Buffer send_key_; |
| 138 rtc::Buffer recv_key_; |
| 192 }; | 139 }; |
| 193 | 140 |
| 194 } // namespace cricket | 141 } // namespace cricket |
| 195 | 142 |
| 196 #endif // WEBRTC_PC_SRTPFILTER_H_ | 143 #endif // WEBRTC_PC_SRTPFILTER_H_ |
| OLD | NEW |