Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 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 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 //static const int IPPROTO_UDP = 17; | 28 //static const int IPPROTO_UDP = 17; |
| 29 static const int kNonceTimeout = 60 * 60 * 1000; // 60 minutes | 29 static const int kNonceTimeout = 60 * 60 * 1000; // 60 minutes |
| 30 static const int kDefaultAllocationTimeout = 10 * 60 * 1000; // 10 minutes | 30 static const int kDefaultAllocationTimeout = 10 * 60 * 1000; // 10 minutes |
| 31 static const int kPermissionTimeout = 5 * 60 * 1000; // 5 minutes | 31 static const int kPermissionTimeout = 5 * 60 * 1000; // 5 minutes |
| 32 static const int kChannelTimeout = 10 * 60 * 1000; // 10 minutes | 32 static const int kChannelTimeout = 10 * 60 * 1000; // 10 minutes |
| 33 | 33 |
| 34 static const int kMinChannelNumber = 0x4000; | 34 static const int kMinChannelNumber = 0x4000; |
| 35 static const int kMaxChannelNumber = 0x7FFF; | 35 static const int kMaxChannelNumber = 0x7FFF; |
| 36 | 36 |
| 37 static const size_t kNonceKeySize = 16; | 37 static const size_t kNonceKeySize = 16; |
| 38 static const size_t kNonceSize = 40; | 38 static const size_t kNonceSize = 48; |
|
pthatcher1
2016/03/15 04:10:50
Why do we need to change the nonce size?
honghaiz3
2016/03/15 17:35:33
Nonce is generated from timestamp. As timestamp ch
| |
| 39 | 39 |
| 40 static const size_t TURN_CHANNEL_HEADER_SIZE = 4U; | 40 static const size_t TURN_CHANNEL_HEADER_SIZE = 4U; |
| 41 | 41 |
| 42 // TODO(mallinath) - Move these to a common place. | 42 // TODO(mallinath) - Move these to a common place. |
| 43 inline bool IsTurnChannelData(uint16_t msg_type) { | 43 inline bool IsTurnChannelData(uint16_t msg_type) { |
| 44 // The first two bits of a channel data message are 0b01. | 44 // The first two bits of a channel data message are 0b01. |
| 45 return ((msg_type & 0xC000) == 0x4000); | 45 return ((msg_type & 0xC000) == 0x4000); |
| 46 } | 46 } |
| 47 | 47 |
| 48 // IDs used for posted messages for TurnServerAllocation. | 48 // IDs used for posted messages for TurnServerAllocation. |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 // If the actual socket allocation fails, send an internal error. | 385 // If the actual socket allocation fails, send an internal error. |
| 386 TurnServerAllocation* alloc = CreateAllocation(conn, proto, key); | 386 TurnServerAllocation* alloc = CreateAllocation(conn, proto, key); |
| 387 if (alloc) { | 387 if (alloc) { |
| 388 alloc->HandleTurnMessage(msg); | 388 alloc->HandleTurnMessage(msg); |
| 389 } else { | 389 } else { |
| 390 SendErrorResponse(conn, msg, STUN_ERROR_SERVER_ERROR, | 390 SendErrorResponse(conn, msg, STUN_ERROR_SERVER_ERROR, |
| 391 "Failed to allocate socket"); | 391 "Failed to allocate socket"); |
| 392 } | 392 } |
| 393 } | 393 } |
| 394 | 394 |
| 395 std::string TurnServer::GenerateNonce(uint32_t now) const { | 395 std::string TurnServer::GenerateNonce(int64_t now) const { |
| 396 // Generate a nonce of the form hex(now + HMAC-MD5(nonce_key_, now)) | 396 // Generate a nonce of the form hex(now + HMAC-MD5(nonce_key_, now)) |
| 397 std::string input(reinterpret_cast<const char*>(&now), sizeof(now)); | 397 std::string input(reinterpret_cast<const char*>(&now), sizeof(now)); |
| 398 std::string nonce = rtc::hex_encode(input.c_str(), input.size()); | 398 std::string nonce = rtc::hex_encode(input.c_str(), input.size()); |
| 399 nonce += rtc::ComputeHmac(rtc::DIGEST_MD5, nonce_key_, input); | 399 nonce += rtc::ComputeHmac(rtc::DIGEST_MD5, nonce_key_, input); |
| 400 ASSERT(nonce.size() == kNonceSize); | 400 ASSERT(nonce.size() == kNonceSize); |
| 401 | |
| 401 return nonce; | 402 return nonce; |
| 402 } | 403 } |
| 403 | 404 |
| 404 bool TurnServer::ValidateNonce(const std::string& nonce) const { | 405 bool TurnServer::ValidateNonce(const std::string& nonce) const { |
| 405 // Check the size. | 406 // Check the size. |
| 406 if (nonce.size() != kNonceSize) { | 407 if (nonce.size() != kNonceSize) { |
| 407 return false; | 408 return false; |
| 408 } | 409 } |
| 409 | 410 |
| 410 // Decode the timestamp. | 411 // Decode the timestamp. |
| 411 uint32_t then; | 412 int64_t then; |
| 412 char* p = reinterpret_cast<char*>(&then); | 413 char* p = reinterpret_cast<char*>(&then); |
| 413 size_t len = rtc::hex_decode(p, sizeof(then), | 414 size_t len = rtc::hex_decode(p, sizeof(then), |
| 414 nonce.substr(0, sizeof(then) * 2)); | 415 nonce.substr(0, sizeof(then) * 2)); |
| 415 if (len != sizeof(then)) { | 416 if (len != sizeof(then)) { |
| 416 return false; | 417 return false; |
| 417 } | 418 } |
| 418 | 419 |
| 419 // Verify the HMAC. | 420 // Verify the HMAC. |
| 420 if (nonce.substr(sizeof(then) * 2) != rtc::ComputeHmac( | 421 if (nonce.substr(sizeof(then) * 2) != rtc::ComputeHmac( |
| 421 rtc::DIGEST_MD5, nonce_key_, std::string(p, sizeof(then)))) { | 422 rtc::DIGEST_MD5, nonce_key_, std::string(p, sizeof(then)))) { |
| 422 return false; | 423 return false; |
| 423 } | 424 } |
| 424 | 425 |
| 425 // Validate the timestamp. | 426 // Validate the timestamp. |
| 426 return rtc::TimeSince(then) < kNonceTimeout; | 427 return rtc::Time64() - then < kNonceTimeout; |
| 427 } | 428 } |
| 428 | 429 |
| 429 TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { | 430 TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { |
| 430 AllocationMap::const_iterator it = allocations_.find(*conn); | 431 AllocationMap::const_iterator it = allocations_.find(*conn); |
| 431 return (it != allocations_.end()) ? it->second : NULL; | 432 return (it != allocations_.end()) ? it->second : NULL; |
| 432 } | 433 } |
| 433 | 434 |
| 434 TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, | 435 TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, |
| 435 int proto, | 436 int proto, |
| 436 const std::string& key) { | 437 const std::string& key) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 457 << ", code=" << code << ", reason=" << reason; | 458 << ", code=" << code << ", reason=" << reason; |
| 458 SendStun(conn, &resp); | 459 SendStun(conn, &resp); |
| 459 } | 460 } |
| 460 | 461 |
| 461 void TurnServer::SendErrorResponseWithRealmAndNonce( | 462 void TurnServer::SendErrorResponseWithRealmAndNonce( |
| 462 TurnServerConnection* conn, const StunMessage* msg, | 463 TurnServerConnection* conn, const StunMessage* msg, |
| 463 int code, const std::string& reason) { | 464 int code, const std::string& reason) { |
| 464 TurnMessage resp; | 465 TurnMessage resp; |
| 465 InitErrorResponse(msg, code, reason, &resp); | 466 InitErrorResponse(msg, code, reason, &resp); |
| 466 | 467 |
| 467 uint32_t timestamp = rtc::Time(); | 468 int64_t timestamp = rtc::Time64(); |
| 468 if (ts_for_next_nonce_) { | 469 if (ts_for_next_nonce_) { |
| 469 timestamp = ts_for_next_nonce_; | 470 timestamp = ts_for_next_nonce_; |
| 470 ts_for_next_nonce_ = 0; | 471 ts_for_next_nonce_ = 0; |
| 471 } | 472 } |
| 472 VERIFY(resp.AddAttribute( | 473 VERIFY(resp.AddAttribute( |
| 473 new StunByteStringAttribute(STUN_ATTR_NONCE, GenerateNonce(timestamp)))); | 474 new StunByteStringAttribute(STUN_ATTR_NONCE, GenerateNonce(timestamp)))); |
| 474 VERIFY(resp.AddAttribute(new StunByteStringAttribute( | 475 VERIFY(resp.AddAttribute(new StunByteStringAttribute( |
| 475 STUN_ATTR_REALM, realm_))); | 476 STUN_ATTR_REALM, realm_))); |
| 476 SendStun(conn, &resp); | 477 SendStun(conn, &resp); |
| 477 } | 478 } |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 810 STUN_ATTR_DATA, data, size))); | 811 STUN_ATTR_DATA, data, size))); |
| 811 server_->SendStun(&conn_, &msg); | 812 server_->SendStun(&conn_, &msg); |
| 812 } else { | 813 } else { |
| 813 LOG_J(LS_WARNING, this) << "Received external packet without permission, " | 814 LOG_J(LS_WARNING, this) << "Received external packet without permission, " |
| 814 << "peer=" << addr; | 815 << "peer=" << addr; |
| 815 } | 816 } |
| 816 } | 817 } |
| 817 | 818 |
| 818 int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) { | 819 int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) { |
| 819 // Return the smaller of our default lifetime and the requested lifetime. | 820 // Return the smaller of our default lifetime and the requested lifetime. |
| 820 uint32_t lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds | 821 int lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds |
| 821 const StunUInt32Attribute* lifetime_attr = msg->GetUInt32(STUN_ATTR_LIFETIME); | 822 const StunUInt32Attribute* lifetime_attr = msg->GetUInt32(STUN_ATTR_LIFETIME); |
| 822 if (lifetime_attr && lifetime_attr->value() < lifetime) { | 823 if (lifetime_attr && static_cast<int>(lifetime_attr->value()) < lifetime) { |
| 823 lifetime = lifetime_attr->value(); | 824 lifetime = static_cast<int>(lifetime_attr->value()); |
| 824 } | 825 } |
| 825 return lifetime; | 826 return lifetime; |
| 826 } | 827 } |
| 827 | 828 |
| 828 bool TurnServerAllocation::HasPermission(const rtc::IPAddress& addr) { | 829 bool TurnServerAllocation::HasPermission(const rtc::IPAddress& addr) { |
| 829 return (FindPermission(addr) != NULL); | 830 return (FindPermission(addr) != NULL); |
| 830 } | 831 } |
| 831 | 832 |
| 832 void TurnServerAllocation::AddPermission(const rtc::IPAddress& addr) { | 833 void TurnServerAllocation::AddPermission(const rtc::IPAddress& addr) { |
| 833 Permission* perm = FindPermission(addr); | 834 Permission* perm = FindPermission(addr); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 947 thread_->PostDelayed(kChannelTimeout, this, MSG_ALLOCATION_TIMEOUT); | 948 thread_->PostDelayed(kChannelTimeout, this, MSG_ALLOCATION_TIMEOUT); |
| 948 } | 949 } |
| 949 | 950 |
| 950 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { | 951 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { |
| 951 ASSERT(msg->message_id == MSG_ALLOCATION_TIMEOUT); | 952 ASSERT(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
| 952 SignalDestroyed(this); | 953 SignalDestroyed(this); |
| 953 delete this; | 954 delete this; |
| 954 } | 955 } |
| 955 | 956 |
| 956 } // namespace cricket | 957 } // namespace cricket |
| OLD | NEW |