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 security key messages to 256KB. | 17 // Limit remote 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 SecurityKeyMessageType SecurityKeyMessage::MessageTypeFromValue(int value) { | 36 RemoteSecurityKeyMessageType SecurityKeyMessage::MessageTypeFromValue( |
| 37 int value) { |
37 // Note: The static_cast from enum value to int should be safe since the enum | 38 // Note: The static_cast from enum value to int should be safe since the enum |
38 // type is an unsigned 8bit value. | 39 // type is an unsigned 8bit value. |
39 switch (value) { | 40 switch (value) { |
40 case static_cast<int>(SecurityKeyMessageType::CONNECT): | 41 case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT): |
41 case static_cast<int>(SecurityKeyMessageType::CONNECT_RESPONSE): | 42 case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT_RESPONSE): |
42 case static_cast<int>(SecurityKeyMessageType::CONNECT_ERROR): | 43 case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT_ERROR): |
43 case static_cast<int>(SecurityKeyMessageType::REQUEST): | 44 case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST): |
44 case static_cast<int>(SecurityKeyMessageType::REQUEST_RESPONSE): | 45 case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST_RESPONSE): |
45 case static_cast<int>(SecurityKeyMessageType::REQUEST_ERROR): | 46 case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST_ERROR): |
46 case static_cast<int>(SecurityKeyMessageType::UNKNOWN_COMMAND): | 47 case static_cast<int>(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND): |
47 case static_cast<int>(SecurityKeyMessageType::UNKNOWN_ERROR): | 48 case static_cast<int>(RemoteSecurityKeyMessageType::UNKNOWN_ERROR): |
48 case static_cast<int>(SecurityKeyMessageType::INVALID): | 49 case static_cast<int>(RemoteSecurityKeyMessageType::INVALID): |
49 return static_cast<SecurityKeyMessageType>(value); | 50 return static_cast<RemoteSecurityKeyMessageType>(value); |
50 | 51 |
51 default: | 52 default: |
52 LOG(ERROR) << "Unknown message type passed in: " << value; | 53 LOG(ERROR) << "Unknown message type passed in: " << value; |
53 return SecurityKeyMessageType::INVALID; | 54 return RemoteSecurityKeyMessageType::INVALID; |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
57 std::unique_ptr<SecurityKeyMessage> SecurityKeyMessage::CreateMessageForTest( | 58 std::unique_ptr<SecurityKeyMessage> SecurityKeyMessage::CreateMessageForTest( |
58 SecurityKeyMessageType type, | 59 RemoteSecurityKeyMessageType type, |
59 const std::string& payload) { | 60 const std::string& payload) { |
60 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); | 61 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); |
61 message->type_ = type; | 62 message->type_ = type; |
62 message->payload_ = payload; | 63 message->payload_ = payload; |
63 | 64 |
64 return message; | 65 return message; |
65 } | 66 } |
66 | 67 |
67 bool SecurityKeyMessage::ParseMessage(const std::string& message_data) { | 68 bool SecurityKeyMessage::ParseMessage(const std::string& message_data) { |
68 if (!IsValidMessageSize(message_data.size())) { | 69 if (!IsValidMessageSize(message_data.size())) { |
69 return false; | 70 return false; |
70 } | 71 } |
71 | 72 |
72 // The first char of the message is the message type. | 73 // The first char of the message is the message type. |
73 type_ = MessageTypeFromValue(message_data[0]); | 74 type_ = MessageTypeFromValue(message_data[0]); |
74 if (type_ == SecurityKeyMessageType::INVALID) { | 75 if (type_ == RemoteSecurityKeyMessageType::INVALID) { |
75 return false; | 76 return false; |
76 } | 77 } |
77 | 78 |
78 payload_.clear(); | 79 payload_.clear(); |
79 if (message_data.size() > kMessageTypeSizeBytes) { | 80 if (message_data.size() > kMessageTypeSizeBytes) { |
80 payload_ = message_data.substr(1); | 81 payload_ = message_data.substr(1); |
81 } | 82 } |
82 | 83 |
83 return true; | 84 return true; |
84 } | 85 } |
85 | 86 |
86 } // namespace remoting | 87 } // namespace remoting |
OLD | NEW |