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