| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/security_key/security_key_message.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace { |
| 10 |
| 11 // Limit remote security key messages to 256KB. |
| 12 const uint32_t kMaxSecurityKeyMessageByteCount = 256 * 1024; |
| 13 |
| 14 const int kRemoteSecurityKeyMessageControlCodeByteCount = 1; |
| 15 |
| 16 } // namespace |
| 17 |
| 18 namespace remoting { |
| 19 |
| 20 SecurityKeyMessage::SecurityKeyMessage() {} |
| 21 |
| 22 SecurityKeyMessage::~SecurityKeyMessage() {} |
| 23 |
| 24 bool SecurityKeyMessage::IsValidMessageSize(uint32_t message_size) { |
| 25 return message_size > 0 && message_size <= kMaxSecurityKeyMessageByteCount; |
| 26 } |
| 27 |
| 28 RemoteSecurityKeyMessageType SecurityKeyMessage::MessageTypeFromValue( |
| 29 int value) { |
| 30 // Note: The static_cast from enum value to int should be safe since the enum |
| 31 // type is an unsigned 8bit value. |
| 32 switch (value) { |
| 33 case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT): |
| 34 case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT_RESPONSE): |
| 35 case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT_ERROR): |
| 36 case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST): |
| 37 case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST_RESPONSE): |
| 38 case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST_ERROR): |
| 39 case static_cast<int>(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND): |
| 40 case static_cast<int>(RemoteSecurityKeyMessageType::UNKNOWN_ERROR): |
| 41 case static_cast<int>(RemoteSecurityKeyMessageType::INVALID): |
| 42 return static_cast<RemoteSecurityKeyMessageType>(value); |
| 43 |
| 44 default: |
| 45 LOG(ERROR) << "Unknown message type passed in: " << value; |
| 46 return RemoteSecurityKeyMessageType::INVALID; |
| 47 } |
| 48 } |
| 49 |
| 50 bool SecurityKeyMessage::ParseMessage(const std::string& message_data) { |
| 51 if (!IsValidMessageSize(message_data.size())) { |
| 52 return false; |
| 53 } |
| 54 |
| 55 // The first char of the message is the message type. |
| 56 type_ = MessageTypeFromValue(message_data[0]); |
| 57 if (type_ == RemoteSecurityKeyMessageType::INVALID) { |
| 58 return false; |
| 59 } |
| 60 |
| 61 payload_.clear(); |
| 62 if (message_data.size() > kRemoteSecurityKeyMessageControlCodeByteCount) { |
| 63 payload_ = message_data.substr(1); |
| 64 } |
| 65 |
| 66 return true; |
| 67 } |
| 68 |
| 69 } // namespace remoting |
| OLD | NEW |