| 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 PC_SRTPFILTER_H_ | 11 #ifndef PC_SRTPFILTER_H_ |
| 12 #define PC_SRTPFILTER_H_ | 12 #define 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 "api/optional.h" | |
| 21 #include "media/base/cryptoparams.h" | 20 #include "media/base/cryptoparams.h" |
| 22 #include "p2p/base/sessiondescription.h" | 21 #include "p2p/base/sessiondescription.h" |
| 23 #include "rtc_base/basictypes.h" | 22 #include "rtc_base/basictypes.h" |
| 24 #include "rtc_base/buffer.h" | |
| 25 #include "rtc_base/constructormagic.h" | 23 #include "rtc_base/constructormagic.h" |
| 26 #include "rtc_base/criticalsection.h" | 24 #include "rtc_base/criticalsection.h" |
| 27 #include "rtc_base/sslstreamadapter.h" | 25 #include "rtc_base/sslstreamadapter.h" |
| 28 #include "rtc_base/thread_checker.h" | 26 #include "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 |
| 40 // call |
| 41 // SetRecv once the remote security params are received. At that point |
| 42 // Protect/UnprotectRt(c)p can be called to encrypt/decrypt data. |
| 43 // TODO: Figure out concurrency policy for SrtpFilter. |
| 40 class SrtpFilter { | 44 class SrtpFilter { |
| 41 public: | 45 public: |
| 42 enum Mode { | 46 enum Mode { |
| 43 PROTECT, | 47 PROTECT, |
| 44 UNPROTECT | 48 UNPROTECT |
| 45 }; | 49 }; |
| 46 enum Error { | 50 enum Error { |
| 47 ERROR_NONE, | 51 ERROR_NONE, |
| 48 ERROR_FAIL, | 52 ERROR_FAIL, |
| 49 ERROR_AUTH, | 53 ERROR_AUTH, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 66 bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params, | 70 bool SetProvisionalAnswer(const std::vector<CryptoParams>& answer_params, |
| 67 ContentSource source); | 71 ContentSource source); |
| 68 // Indicates which crypto algorithms and keys were contained in the answer. | 72 // Indicates which crypto algorithms and keys were contained in the answer. |
| 69 // answer_params should contain the negotiated parameters, which may be none, | 73 // 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). | 74 // if crypto was not desired or could not be negotiated (and not required). |
| 71 // This must be called after SetOffer. If crypto negotiation completes | 75 // This must be called after SetOffer. If crypto negotiation completes |
| 72 // successfully, this will advance the filter to the active state. | 76 // successfully, this will advance the filter to the active state. |
| 73 bool SetAnswer(const std::vector<CryptoParams>& answer_params, | 77 bool SetAnswer(const std::vector<CryptoParams>& answer_params, |
| 74 ContentSource source); | 78 ContentSource source); |
| 75 | 79 |
| 80 // Set the header extension ids that should be encrypted for the given |
| 81 // source. |
| 82 void SetEncryptedHeaderExtensionIds(ContentSource source, |
| 83 const std::vector<int>& extension_ids); |
| 84 // Just set up both sets of keys directly. |
| 85 // Used with DTLS-SRTP. |
| 86 bool SetRtpParams(int send_cs, |
| 87 const uint8_t* send_key, |
| 88 int send_key_len, |
| 89 int recv_cs, |
| 90 const uint8_t* recv_key, |
| 91 int recv_key_len); |
| 92 bool UpdateRtpParams(int send_cs, |
| 93 const uint8_t* send_key, |
| 94 int send_key_len, |
| 95 int recv_cs, |
| 96 const uint8_t* recv_key, |
| 97 int recv_key_len); |
| 98 bool SetRtcpParams(int send_cs, |
| 99 const uint8_t* send_key, |
| 100 int send_key_len, |
| 101 int recv_cs, |
| 102 const uint8_t* recv_key, |
| 103 int recv_key_len); |
| 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 // Returns rtp auth params from srtp context. |
| 119 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len); |
| 120 // Returns srtp overhead for rtp packets. |
| 121 bool GetSrtpOverhead(int* srtp_overhead) const; |
| 122 // If external auth is enabled, SRTP will write a dummy auth tag that |
| 123 // then |
| 124 // later must get replaced before the packet is sent out. Only |
| 125 // supported for |
| 126 // non-GCM cipher suites and can be checked through |
| 127 // "IsExternalAuthActive" |
| 128 // if it is actually used. This method is only valid before the RTP |
| 129 // params |
| 130 // have been set. |
| 131 void EnableExternalAuth(); |
| 132 bool IsExternalAuthEnabled() const; |
| 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 | 142 |
| 87 bool StoreParams(const std::vector<CryptoParams>& params, | 143 bool StoreParams(const std::vector<CryptoParams>& params, |
| 88 ContentSource source); | 144 ContentSource source); |
| 89 | 145 |
| 90 bool ExpectAnswer(ContentSource source); | 146 bool ExpectAnswer(ContentSource source); |
| 91 | 147 |
| 92 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params, | 148 bool DoSetAnswer(const std::vector<CryptoParams>& answer_params, |
| 93 ContentSource source, | 149 ContentSource source, |
| 94 bool final); | 150 bool final); |
| 95 | 151 |
| 152 void CreateSrtpSessions(); |
| 96 bool NegotiateParams(const std::vector<CryptoParams>& answer_params, | 153 bool NegotiateParams(const std::vector<CryptoParams>& answer_params, |
| 97 CryptoParams* selected_params); | 154 CryptoParams* selected_params); |
| 98 | 155 |
| 99 private: | 156 bool ApplyParams(const CryptoParams& send_params, |
| 100 bool ApplySendParams(const CryptoParams& send_params); | 157 const CryptoParams& recv_params); |
| 101 | |
| 102 bool ApplyRecvParams(const CryptoParams& recv_params); | |
| 103 | |
| 104 static bool ParseKeyParams(const std::string& params, | 158 static bool ParseKeyParams(const std::string& params, |
| 105 uint8_t* key, | 159 uint8_t* key, |
| 106 size_t len); | 160 size_t len); |
| 107 | 161 |
| 162 private: |
| 108 enum State { | 163 enum State { |
| 109 ST_INIT, // SRTP filter unused. | 164 ST_INIT, // SRTP filter unused. |
| 110 ST_SENTOFFER, // Offer with SRTP parameters sent. | 165 ST_SENTOFFER, // Offer with SRTP parameters sent. |
| 111 ST_RECEIVEDOFFER, // Offer with SRTP parameters received. | 166 ST_RECEIVEDOFFER, // Offer with SRTP parameters received. |
| 112 ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto. | 167 ST_SENTPRANSWER_NO_CRYPTO, // Sent provisional answer without crypto. |
| 113 // Received provisional answer without crypto. | 168 // Received provisional answer without crypto. |
| 114 ST_RECEIVEDPRANSWER_NO_CRYPTO, | 169 ST_RECEIVEDPRANSWER_NO_CRYPTO, |
| 115 ST_ACTIVE, // Offer and answer set. | 170 ST_ACTIVE, // Offer and answer set. |
| 116 // SRTP filter is active but new parameters are offered. | 171 // SRTP filter is active but new parameters are offered. |
| 117 // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT. | 172 // When the answer is set, the state transitions to ST_ACTIVE or ST_INIT. |
| 118 ST_SENTUPDATEDOFFER, | 173 ST_SENTUPDATEDOFFER, |
| 119 // SRTP filter is active but new parameters are received. | 174 // SRTP filter is active but new parameters are received. |
| 120 // When the answer is set, the state transitions back to ST_ACTIVE. | 175 // When the answer is set, the state transitions back to ST_ACTIVE. |
| 121 ST_RECEIVEDUPDATEDOFFER, | 176 ST_RECEIVEDUPDATEDOFFER, |
| 122 // SRTP filter is active but the sent answer is only provisional. | 177 // 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 | 178 // When the final answer is set, the state transitions to ST_ACTIVE or |
| 124 // ST_INIT. | 179 // ST_INIT. |
| 125 ST_SENTPRANSWER, | 180 ST_SENTPRANSWER, |
| 126 // SRTP filter is active but the received answer is only provisional. | 181 // 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 | 182 // When the final answer is set, the state transitions to ST_ACTIVE or |
| 128 // ST_INIT. | 183 // ST_INIT. |
| 129 ST_RECEIVEDPRANSWER | 184 ST_RECEIVEDPRANSWER |
| 130 }; | 185 }; |
| 131 State state_ = ST_INIT; | 186 State state_ = ST_INIT; |
| 187 bool external_auth_enabled_ = false; |
| 132 std::vector<CryptoParams> offer_params_; | 188 std::vector<CryptoParams> offer_params_; |
| 189 std::unique_ptr<SrtpSession> send_session_; |
| 190 std::unique_ptr<SrtpSession> recv_session_; |
| 191 std::unique_ptr<SrtpSession> send_rtcp_session_; |
| 192 std::unique_ptr<SrtpSession> recv_rtcp_session_; |
| 133 CryptoParams applied_send_params_; | 193 CryptoParams applied_send_params_; |
| 134 CryptoParams applied_recv_params_; | 194 CryptoParams applied_recv_params_; |
| 135 rtc::Optional<int> send_cipher_suite_; | 195 std::vector<int> send_encrypted_header_extension_ids_; |
| 136 rtc::Optional<int> recv_cipher_suite_; | 196 std::vector<int> recv_encrypted_header_extension_ids_; |
| 137 rtc::Buffer send_key_; | |
| 138 rtc::Buffer recv_key_; | |
| 139 }; | 197 }; |
| 140 | 198 |
| 141 } // namespace cricket | 199 } // namespace cricket |
| 142 | 200 |
| 143 #endif // PC_SRTPFILTER_H_ | 201 #endif // PC_SRTPFILTER_H_ |
| OLD | NEW |