OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2004 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 #include "webrtc/pc/mediasession.h" | 11 #include "webrtc/pc/mediasession.h" |
12 | 12 |
13 #include <algorithm> // For std::find_if, std::sort. | 13 #include <algorithm> // For std::find_if, std::sort. |
14 #include <functional> | 14 #include <functional> |
15 #include <map> | 15 #include <map> |
16 #include <memory> | 16 #include <memory> |
17 #include <set> | 17 #include <set> |
18 #include <unordered_map> | 18 #include <unordered_map> |
19 #include <utility> | 19 #include <utility> |
20 | 20 |
21 #include "webrtc/base/base64.h" | |
22 #include "webrtc/base/buffer.h" | |
21 #include "webrtc/base/helpers.h" | 23 #include "webrtc/base/helpers.h" |
22 #include "webrtc/base/logging.h" | 24 #include "webrtc/base/logging.h" |
23 #include "webrtc/base/stringutils.h" | 25 #include "webrtc/base/stringutils.h" |
24 #include "webrtc/media/base/cryptoparams.h" | 26 #include "webrtc/media/base/cryptoparams.h" |
25 #include "webrtc/media/base/mediaconstants.h" | 27 #include "webrtc/media/base/mediaconstants.h" |
26 #include "webrtc/p2p/base/p2pconstants.h" | 28 #include "webrtc/p2p/base/p2pconstants.h" |
27 #include "webrtc/pc/channelmanager.h" | 29 #include "webrtc/pc/channelmanager.h" |
28 #include "webrtc/pc/srtpfilter.h" | 30 #include "webrtc/pc/srtpfilter.h" |
29 | 31 |
30 #ifdef HAVE_SCTP | 32 #ifdef HAVE_SCTP |
31 #include "webrtc/media/sctp/sctpdataengine.h" | 33 #include "webrtc/media/sctp/sctpdataengine.h" |
32 #else | 34 #else |
33 static const uint32_t kMaxSctpSid = 1023; | 35 static const uint32_t kMaxSctpSid = 1023; |
34 #endif | 36 #endif |
35 | 37 |
36 namespace { | 38 namespace { |
37 const char kInline[] = "inline:"; | 39 const char kInline[] = "inline:"; |
38 | 40 |
39 void GetSupportedCryptoSuiteNames(void (*func)(std::vector<int>*), | 41 void GetSupportedCryptoSuiteNames(void (*func)(const rtc::CryptoOptions&, |
42 std::vector<int>*), | |
43 const rtc::CryptoOptions& crypto_options, | |
40 std::vector<std::string>* names) { | 44 std::vector<std::string>* names) { |
41 #ifdef HAVE_SRTP | 45 #ifdef HAVE_SRTP |
42 std::vector<int> crypto_suites; | 46 std::vector<int> crypto_suites; |
43 func(&crypto_suites); | 47 func(crypto_options, &crypto_suites); |
44 for (const auto crypto : crypto_suites) { | 48 for (const auto crypto : crypto_suites) { |
45 names->push_back(rtc::SrtpCryptoSuiteToName(crypto)); | 49 names->push_back(rtc::SrtpCryptoSuiteToName(crypto)); |
46 } | 50 } |
47 #endif | 51 #endif |
48 } | 52 } |
49 } // namespace | 53 } // namespace |
50 | 54 |
51 namespace cricket { | 55 namespace cricket { |
52 | 56 |
53 | 57 |
(...skipping 21 matching lines...) Expand all Loading... | |
75 return false; | 79 return false; |
76 } | 80 } |
77 | 81 |
78 const MediaContentDescription* mdesc = | 82 const MediaContentDescription* mdesc = |
79 static_cast<const MediaContentDescription*>(content->description); | 83 static_cast<const MediaContentDescription*>(content->description); |
80 return mdesc && mdesc->type() == media_type; | 84 return mdesc && mdesc->type() == media_type; |
81 } | 85 } |
82 | 86 |
83 static bool CreateCryptoParams(int tag, const std::string& cipher, | 87 static bool CreateCryptoParams(int tag, const std::string& cipher, |
84 CryptoParams *out) { | 88 CryptoParams *out) { |
85 std::string key; | 89 int key_len; |
86 key.reserve(SRTP_MASTER_KEY_BASE64_LEN); | 90 int salt_len; |
87 | 91 if (!rtc::GetSrtpKeyAndSaltLengths( |
88 if (!rtc::CreateRandomString(SRTP_MASTER_KEY_BASE64_LEN, &key)) { | 92 rtc::SrtpCryptoSuiteFromName(cipher), &key_len, &salt_len)) { |
89 return false; | 93 return false; |
90 } | 94 } |
95 | |
96 int master_key_len = key_len + salt_len; | |
97 rtc::Buffer master_key(master_key_len); | |
98 if (!rtc::CreateRandomData(master_key_len, master_key.data())) { | |
99 return false; | |
100 } | |
101 | |
102 std::string key; | |
103 rtc::Base64::EncodeFromArray(master_key.data(), master_key_len, &key); | |
mattdr
2016/05/10 22:29:13
if we use a string buffer, we can use key = rtc::B
joachim
2016/05/10 23:09:44
Done.
| |
104 | |
91 out->tag = tag; | 105 out->tag = tag; |
92 out->cipher_suite = cipher; | 106 out->cipher_suite = cipher; |
93 out->key_params = kInline; | 107 out->key_params = kInline; |
94 out->key_params += key; | 108 out->key_params += key; |
95 return true; | 109 return true; |
96 } | 110 } |
97 | 111 |
98 #ifdef HAVE_SRTP | 112 #ifdef HAVE_SRTP |
99 static bool AddCryptoParams(const std::string& cipher_suite, | 113 static bool AddCryptoParams(const std::string& cipher_suite, |
100 CryptoParamsVec *out) { | 114 CryptoParamsVec *out) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 for (CryptoParamsVec::const_iterator it = cryptos.begin(); | 153 for (CryptoParamsVec::const_iterator it = cryptos.begin(); |
140 it != cryptos.end(); ++it) { | 154 it != cryptos.end(); ++it) { |
141 if (crypto.Matches(*it)) { | 155 if (crypto.Matches(*it)) { |
142 *out = *it; | 156 *out = *it; |
143 return true; | 157 return true; |
144 } | 158 } |
145 } | 159 } |
146 return false; | 160 return false; |
147 } | 161 } |
148 | 162 |
149 // For audio, HMAC 32 is prefered because of the low overhead. | 163 // For audio, HMAC 32 is prefered over HMAC 80 because of the low overhead. |
150 void GetSupportedAudioCryptoSuites(std::vector<int>* crypto_suites) { | 164 void GetSupportedAudioCryptoSuites(const rtc::CryptoOptions& crypto_options, |
165 std::vector<int>* crypto_suites) { | |
151 #ifdef HAVE_SRTP | 166 #ifdef HAVE_SRTP |
167 if (crypto_options.enable_gcm_crypto_suites) { | |
168 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM); | |
169 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM); | |
170 } | |
152 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32); | 171 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32); |
153 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80); | 172 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80); |
154 #endif | 173 #endif |
155 } | 174 } |
156 | 175 |
157 void GetSupportedAudioCryptoSuiteNames( | 176 void GetSupportedAudioCryptoSuiteNames(const rtc::CryptoOptions& crypto_options, |
158 std::vector<std::string>* crypto_suite_names) { | 177 std::vector<std::string>* crypto_suite_names) { |
159 GetSupportedCryptoSuiteNames(GetSupportedAudioCryptoSuites, | 178 GetSupportedCryptoSuiteNames(GetSupportedAudioCryptoSuites, |
160 crypto_suite_names); | 179 crypto_options, crypto_suite_names); |
161 } | 180 } |
162 | 181 |
163 void GetSupportedVideoCryptoSuites(std::vector<int>* crypto_suites) { | 182 void GetSupportedVideoCryptoSuites(const rtc::CryptoOptions& crypto_options, |
164 GetDefaultSrtpCryptoSuites(crypto_suites); | 183 std::vector<int>* crypto_suites) { |
184 GetDefaultSrtpCryptoSuites(crypto_options, crypto_suites); | |
165 } | 185 } |
166 | 186 |
167 void GetSupportedVideoCryptoSuiteNames( | 187 void GetSupportedVideoCryptoSuiteNames(const rtc::CryptoOptions& crypto_options, |
168 std::vector<std::string>* crypto_suite_names) { | 188 std::vector<std::string>* crypto_suite_names) { |
169 GetSupportedCryptoSuiteNames(GetSupportedVideoCryptoSuites, | 189 GetSupportedCryptoSuiteNames(GetSupportedVideoCryptoSuites, |
170 crypto_suite_names); | 190 crypto_options, crypto_suite_names); |
171 } | 191 } |
172 | 192 |
173 void GetSupportedDataCryptoSuites(std::vector<int>* crypto_suites) { | 193 void GetSupportedDataCryptoSuites(const rtc::CryptoOptions& crypto_options, |
174 GetDefaultSrtpCryptoSuites(crypto_suites); | 194 std::vector<int>* crypto_suites) { |
195 GetDefaultSrtpCryptoSuites(crypto_options, crypto_suites); | |
175 } | 196 } |
176 | 197 |
177 void GetSupportedDataCryptoSuiteNames( | 198 void GetSupportedDataCryptoSuiteNames(const rtc::CryptoOptions& crypto_options, |
178 std::vector<std::string>* crypto_suite_names) { | 199 std::vector<std::string>* crypto_suite_names) { |
179 GetSupportedCryptoSuiteNames(GetSupportedDataCryptoSuites, | 200 GetSupportedCryptoSuiteNames(GetSupportedDataCryptoSuites, |
180 crypto_suite_names); | 201 crypto_options, crypto_suite_names); |
181 } | 202 } |
182 | 203 |
183 void GetDefaultSrtpCryptoSuites(std::vector<int>* crypto_suites) { | 204 void GetDefaultSrtpCryptoSuites(const rtc::CryptoOptions& crypto_options, |
205 std::vector<int>* crypto_suites) { | |
184 #ifdef HAVE_SRTP | 206 #ifdef HAVE_SRTP |
207 if (crypto_options.enable_gcm_crypto_suites) { | |
208 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM); | |
209 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM); | |
210 } | |
185 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80); | 211 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80); |
186 #endif | 212 #endif |
187 } | 213 } |
188 | 214 |
189 void GetDefaultSrtpCryptoSuiteNames( | 215 void GetDefaultSrtpCryptoSuiteNames(const rtc::CryptoOptions& crypto_options, |
190 std::vector<std::string>* crypto_suite_names) { | 216 std::vector<std::string>* crypto_suite_names) { |
191 GetSupportedCryptoSuiteNames(GetDefaultSrtpCryptoSuites, crypto_suite_names); | 217 GetSupportedCryptoSuiteNames(GetDefaultSrtpCryptoSuites, |
218 crypto_options, crypto_suite_names); | |
192 } | 219 } |
193 | 220 |
194 // For video support only 80-bit SHA1 HMAC. For audio 32-bit HMAC is | 221 // Support any GCM cipher (if enabled through options). For video support only |
195 // tolerated unless bundle is enabled because it is low overhead. Pick the | 222 // 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated unless bundle is enabled |
196 // crypto in the list that is supported. | 223 // because it is low overhead. |
224 // Pick the crypto in the list that is supported. | |
197 static bool SelectCrypto(const MediaContentDescription* offer, | 225 static bool SelectCrypto(const MediaContentDescription* offer, |
198 bool bundle, | 226 bool bundle, |
227 const rtc::CryptoOptions& crypto_options, | |
199 CryptoParams *crypto) { | 228 CryptoParams *crypto) { |
200 bool audio = offer->type() == MEDIA_TYPE_AUDIO; | 229 bool audio = offer->type() == MEDIA_TYPE_AUDIO; |
201 const CryptoParamsVec& cryptos = offer->cryptos(); | 230 const CryptoParamsVec& cryptos = offer->cryptos(); |
202 | 231 |
203 for (CryptoParamsVec::const_iterator i = cryptos.begin(); | 232 for (CryptoParamsVec::const_iterator i = cryptos.begin(); |
204 i != cryptos.end(); ++i) { | 233 i != cryptos.end(); ++i) { |
205 if (rtc::CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite || | 234 if ((crypto_options.enable_gcm_crypto_suites && |
235 rtc::IsGcmCryptoSuiteName(i->cipher_suite)) || | |
236 rtc::CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite || | |
206 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio && | 237 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio && |
207 !bundle)) { | 238 !bundle)) { |
208 return CreateCryptoParams(i->tag, i->cipher_suite, crypto); | 239 return CreateCryptoParams(i->tag, i->cipher_suite, crypto); |
209 } | 240 } |
210 } | 241 } |
211 return false; | 242 return false; |
212 } | 243 } |
213 | 244 |
214 static const StreamParams* FindFirstStreamParamsByCname( | 245 static const StreamParams* FindFirstStreamParamsByCname( |
215 const StreamParamsVec& params_vec, | 246 const StreamParamsVec& params_vec, |
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1060 &negotiated_rtp_extensions); | 1091 &negotiated_rtp_extensions); |
1061 answer->set_rtp_header_extensions(negotiated_rtp_extensions); | 1092 answer->set_rtp_header_extensions(negotiated_rtp_extensions); |
1062 | 1093 |
1063 answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux()); | 1094 answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux()); |
1064 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) { | 1095 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) { |
1065 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size()); | 1096 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size()); |
1066 } | 1097 } |
1067 | 1098 |
1068 if (sdes_policy != SEC_DISABLED) { | 1099 if (sdes_policy != SEC_DISABLED) { |
1069 CryptoParams crypto; | 1100 CryptoParams crypto; |
1070 if (SelectCrypto(offer, bundle_enabled, &crypto)) { | 1101 if (SelectCrypto(offer, bundle_enabled, options.crypto_options, &crypto)) { |
1071 if (current_cryptos) { | 1102 if (current_cryptos) { |
1072 FindMatchingCrypto(*current_cryptos, crypto, &crypto); | 1103 FindMatchingCrypto(*current_cryptos, crypto, &crypto); |
1073 } | 1104 } |
1074 answer->AddCrypto(crypto); | 1105 answer->AddCrypto(crypto); |
1075 } | 1106 } |
1076 } | 1107 } |
1077 | 1108 |
1078 if (answer->cryptos().empty() && | 1109 if (answer->cryptos().empty() && |
1079 (offer->crypto_required() == CT_SDES || sdes_policy == SEC_REQUIRED)) { | 1110 (offer->crypto_required() == CT_SDES || sdes_policy == SEC_REQUIRED)) { |
1080 return false; | 1111 return false; |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1576 GetFirstAudioContent(current_description); | 1607 GetFirstAudioContent(current_description); |
1577 std::string content_name = | 1608 std::string content_name = |
1578 current_audio_content ? current_audio_content->name : CN_AUDIO; | 1609 current_audio_content ? current_audio_content->name : CN_AUDIO; |
1579 | 1610 |
1580 cricket::SecurePolicy sdes_policy = | 1611 cricket::SecurePolicy sdes_policy = |
1581 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED | 1612 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED |
1582 : secure(); | 1613 : secure(); |
1583 | 1614 |
1584 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription()); | 1615 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription()); |
1585 std::vector<std::string> crypto_suites; | 1616 std::vector<std::string> crypto_suites; |
1586 GetSupportedAudioCryptoSuiteNames(&crypto_suites); | 1617 GetSupportedAudioCryptoSuiteNames(options.crypto_options, &crypto_suites); |
1587 if (!CreateMediaContentOffer( | 1618 if (!CreateMediaContentOffer( |
1588 options, | 1619 options, |
1589 audio_codecs, | 1620 audio_codecs, |
1590 sdes_policy, | 1621 sdes_policy, |
1591 GetCryptos(GetFirstAudioContentDescription(current_description)), | 1622 GetCryptos(GetFirstAudioContentDescription(current_description)), |
1592 crypto_suites, | 1623 crypto_suites, |
1593 audio_rtp_extensions, | 1624 audio_rtp_extensions, |
1594 add_legacy_, | 1625 add_legacy_, |
1595 current_streams, | 1626 current_streams, |
1596 audio.get())) { | 1627 audio.get())) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1636 GetFirstVideoContent(current_description); | 1667 GetFirstVideoContent(current_description); |
1637 std::string content_name = | 1668 std::string content_name = |
1638 current_video_content ? current_video_content->name : CN_VIDEO; | 1669 current_video_content ? current_video_content->name : CN_VIDEO; |
1639 | 1670 |
1640 cricket::SecurePolicy sdes_policy = | 1671 cricket::SecurePolicy sdes_policy = |
1641 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED | 1672 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED |
1642 : secure(); | 1673 : secure(); |
1643 | 1674 |
1644 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription()); | 1675 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription()); |
1645 std::vector<std::string> crypto_suites; | 1676 std::vector<std::string> crypto_suites; |
1646 GetSupportedVideoCryptoSuiteNames(&crypto_suites); | 1677 GetSupportedVideoCryptoSuiteNames(options.crypto_options, &crypto_suites); |
1647 if (!CreateMediaContentOffer( | 1678 if (!CreateMediaContentOffer( |
1648 options, | 1679 options, |
1649 video_codecs, | 1680 video_codecs, |
1650 sdes_policy, | 1681 sdes_policy, |
1651 GetCryptos(GetFirstVideoContentDescription(current_description)), | 1682 GetCryptos(GetFirstVideoContentDescription(current_description)), |
1652 crypto_suites, | 1683 crypto_suites, |
1653 video_rtp_extensions, | 1684 video_rtp_extensions, |
1654 add_legacy_, | 1685 add_legacy_, |
1655 current_streams, | 1686 current_streams, |
1656 video.get())) { | 1687 video.get())) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1712 // SDES doesn't make sense for SCTP, so we disable it, and we only | 1743 // SDES doesn't make sense for SCTP, so we disable it, and we only |
1713 // get SDES crypto suites for RTP-based data channels. | 1744 // get SDES crypto suites for RTP-based data channels. |
1714 sdes_policy = cricket::SEC_DISABLED; | 1745 sdes_policy = cricket::SEC_DISABLED; |
1715 // Unlike SetMediaProtocol below, we need to set the protocol | 1746 // Unlike SetMediaProtocol below, we need to set the protocol |
1716 // before we call CreateMediaContentOffer. Otherwise, | 1747 // before we call CreateMediaContentOffer. Otherwise, |
1717 // CreateMediaContentOffer won't know this is SCTP and will | 1748 // CreateMediaContentOffer won't know this is SCTP and will |
1718 // generate SSRCs rather than SIDs. | 1749 // generate SSRCs rather than SIDs. |
1719 data->set_protocol( | 1750 data->set_protocol( |
1720 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp); | 1751 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp); |
1721 } else { | 1752 } else { |
1722 GetSupportedDataCryptoSuiteNames(&crypto_suites); | 1753 GetSupportedDataCryptoSuiteNames(options.crypto_options, &crypto_suites); |
1723 } | 1754 } |
1724 | 1755 |
1725 if (!CreateMediaContentOffer( | 1756 if (!CreateMediaContentOffer( |
1726 options, | 1757 options, |
1727 *data_codecs, | 1758 *data_codecs, |
1728 sdes_policy, | 1759 sdes_policy, |
1729 GetCryptos(GetFirstDataContentDescription(current_description)), | 1760 GetCryptos(GetFirstDataContentDescription(current_description)), |
1730 crypto_suites, | 1761 crypto_suites, |
1731 RtpHeaderExtensions(), | 1762 RtpHeaderExtensions(), |
1732 add_legacy_, | 1763 add_legacy_, |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2004 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); | 2035 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); |
2005 } | 2036 } |
2006 | 2037 |
2007 const DataContentDescription* GetFirstDataContentDescription( | 2038 const DataContentDescription* GetFirstDataContentDescription( |
2008 const SessionDescription* sdesc) { | 2039 const SessionDescription* sdesc) { |
2009 return static_cast<const DataContentDescription*>( | 2040 return static_cast<const DataContentDescription*>( |
2010 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); | 2041 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); |
2011 } | 2042 } |
2012 | 2043 |
2013 } // namespace cricket | 2044 } // namespace cricket |
OLD | NEW |