isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 1 | // Copyright 2014 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 | |
hansberry | e7ad389 | 2016-12-19 22:19:21 | [diff] [blame] | 5 | #include "components/cryptauth/wire_message.h" |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 6 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 10 | #include <limits> |
| 11 | |
peter | e0807cf | 2015-11-20 14:46:24 | [diff] [blame] | 12 | #include "base/base64url.h" |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 13 | #include "base/json/json_reader.h" |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 14 | #include "base/json/json_writer.h" |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 15 | #include "base/macros.h" |
dcheng | 2f01269 | 2016-04-21 00:19:34 | [diff] [blame] | 16 | #include "base/memory/ptr_util.h" |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 17 | #include "base/values.h" |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 18 | #include "components/proximity_auth/logging/logging.h" |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 19 | |
| 20 | // The wire messages have a simple format: |
| 21 | // [ message version ] [ body length ] [ JSON body ] |
| 22 | // 1 byte 2 bytes body length |
| 23 | // The JSON body contains two fields: an optional permit_id field and a required |
| 24 | // data field. |
| 25 | |
hansberry | e7ad389 | 2016-12-19 22:19:21 | [diff] [blame] | 26 | namespace cryptauth { |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // The length of the message header, in bytes. |
| 30 | const size_t kHeaderLength = 3; |
| 31 | |
| 32 | // The protocol version of the message format. |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 33 | const int kMessageFormatVersionThree = 3; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 34 | |
| 35 | const char kPayloadKey[] = "payload"; |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 36 | const char kFeatureKey[] = "feature"; |
| 37 | |
| 38 | // The default feature value. This is the default for backward compatibility |
| 39 | // reasons; previously, the protocol did not transmit the feature in the |
| 40 | // message, but because EasyUnlock was the only feature used, it didn't matter. |
| 41 | // So, if a message is received without a feature, it is assumed to be |
| 42 | // EasyUnlock by default. |
| 43 | const char kDefaultFeature[] = "easy_unlock"; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 44 | |
| 45 | // Parses the |serialized_message|'s header. Returns |true| iff the message has |
| 46 | // a valid header, is complete, and is well-formed according to the header. Sets |
| 47 | // |is_incomplete_message| to true iff the message does not have enough data to |
| 48 | // parse the header, or if the message length encoded in the message header |
| 49 | // exceeds the size of the |serialized_message|. |
| 50 | bool ParseHeader(const std::string& serialized_message, |
| 51 | bool* is_incomplete_message) { |
| 52 | *is_incomplete_message = false; |
| 53 | if (serialized_message.size() < kHeaderLength) { |
| 54 | *is_incomplete_message = true; |
| 55 | return false; |
| 56 | } |
| 57 | |
mostynb | 470748ce | 2014-12-22 21:14:46 | [diff] [blame] | 58 | static_assert(kHeaderLength > 2, "kHeaderLength too small"); |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 59 | size_t version = serialized_message[0]; |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 60 | if (version != kMessageFormatVersionThree) { |
| 61 | PA_LOG(WARNING) << "Error: Invalid message version. Got " << version |
| 62 | << ", expected " << kMessageFormatVersionThree; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 66 | uint16_t expected_body_length = |
| 67 | (static_cast<uint8_t>(serialized_message[1]) << 8) | |
| 68 | (static_cast<uint8_t>(serialized_message[2]) << 0); |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 69 | size_t expected_message_length = kHeaderLength + expected_body_length; |
| 70 | if (serialized_message.size() < expected_message_length) { |
| 71 | *is_incomplete_message = true; |
| 72 | return false; |
| 73 | } |
| 74 | if (serialized_message.size() != expected_message_length) { |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 75 | PA_LOG(WARNING) << "Error: Invalid message length. Got " |
| 76 | << serialized_message.size() << ", expected " |
| 77 | << expected_message_length; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | } // namespace |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 85 | |
hansberry | e7ad389 | 2016-12-19 22:19:21 | [diff] [blame] | 86 | WireMessage::~WireMessage() {} |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 87 | |
| 88 | // static |
dcheng | 2f01269 | 2016-04-21 00:19:34 | [diff] [blame] | 89 | std::unique_ptr<WireMessage> WireMessage::Deserialize( |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 90 | const std::string& serialized_message, |
| 91 | bool* is_incomplete_message) { |
| 92 | if (!ParseHeader(serialized_message, is_incomplete_message)) |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 93 | return nullptr; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 94 | |
dcheng | 2f01269 | 2016-04-21 00:19:34 | [diff] [blame] | 95 | std::unique_ptr<base::Value> body_value = |
olli.raula | 1fd91c7 | 2015-09-08 13:43:25 | [diff] [blame] | 96 | base::JSONReader::Read(serialized_message.substr(kHeaderLength)); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 97 | if (!body_value || !body_value->IsType(base::Value::Type::DICTIONARY)) { |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 98 | PA_LOG(WARNING) << "Error: Unable to parse message as JSON."; |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 99 | return nullptr; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | base::DictionaryValue* body; |
| 103 | bool success = body_value->GetAsDictionary(&body); |
| 104 | DCHECK(success); |
| 105 | |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 106 | std::string payload_base64; |
| 107 | if (!body->GetString(kPayloadKey, &payload_base64) || |
| 108 | payload_base64.empty()) { |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 109 | PA_LOG(WARNING) << "Error: Missing payload."; |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 110 | return nullptr; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | std::string payload; |
peter | e0807cf | 2015-11-20 14:46:24 | [diff] [blame] | 114 | if (!base::Base64UrlDecode(payload_base64, |
| 115 | base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 116 | &payload)) { |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 117 | PA_LOG(WARNING) << "Error: Invalid base64 encoding for payload."; |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 118 | return nullptr; |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 119 | } |
| 120 | |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 121 | std::string feature; |
| 122 | if (!body->GetString(kFeatureKey, &feature) || feature.empty()) { |
| 123 | feature = std::string(kDefaultFeature); |
| 124 | } |
| 125 | |
| 126 | return base::WrapUnique(new WireMessage(payload, feature)); |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 127 | } |
| 128 | |
isherman | 83d08a29 | 2014-09-26 03:32:44 | [diff] [blame] | 129 | std::string WireMessage::Serialize() const { |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 130 | if (payload_.empty()) { |
| 131 | PA_LOG(ERROR) << "Failed to serialize empty wire message."; |
| 132 | return std::string(); |
| 133 | } |
| 134 | |
| 135 | // Create JSON body containing permit id and payload. |
| 136 | base::DictionaryValue body; |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 137 | |
| 138 | std::string base64_payload; |
peter | e0807cf | 2015-11-20 14:46:24 | [diff] [blame] | 139 | base::Base64UrlEncode(payload_, base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 140 | &base64_payload); |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 141 | body.SetString(kPayloadKey, base64_payload); |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 142 | body.SetString(kFeatureKey, feature_); |
tengs | 7b425ad8 | 2015-07-21 00:20:35 | [diff] [blame] | 143 | |
| 144 | std::string json_body; |
| 145 | if (!base::JSONWriter::Write(body, &json_body)) { |
| 146 | PA_LOG(ERROR) << "Failed to convert WireMessage body to JSON: " << body; |
| 147 | return std::string(); |
| 148 | } |
| 149 | |
| 150 | // Create header containing version and payload size. |
| 151 | size_t body_size = json_body.size(); |
| 152 | if (body_size > std::numeric_limits<uint16_t>::max()) { |
| 153 | PA_LOG(ERROR) << "Can not create WireMessage because body size exceeds " |
| 154 | << "16-bit unsigned integer: " << body_size; |
| 155 | return std::string(); |
| 156 | } |
| 157 | |
| 158 | uint8_t header[] = { |
| 159 | static_cast<uint8_t>(kMessageFormatVersionThree), |
| 160 | static_cast<uint8_t>((body_size >> 8) & 0xFF), |
| 161 | static_cast<uint8_t>(body_size & 0xFF), |
| 162 | }; |
| 163 | static_assert(sizeof(header) == kHeaderLength, "Malformed header."); |
| 164 | |
| 165 | std::string header_string(kHeaderLength, 0); |
| 166 | std::memcpy(&header_string[0], header, kHeaderLength); |
| 167 | return header_string + json_body; |
isherman | 83d08a29 | 2014-09-26 03:32:44 | [diff] [blame] | 168 | } |
| 169 | |
khorimoto | f459482 | 2017-01-19 20:14:50 | [diff] [blame] | 170 | WireMessage::WireMessage(const std::string& payload, const std::string& feature) |
| 171 | : payload_(payload), feature_(feature) {} |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 172 | |
hansberry | e7ad389 | 2016-12-19 22:19:21 | [diff] [blame] | 173 | } // namespace cryptauth |