blob: b91b943558934b46b587e7d522b246430ea97838 [file] [log] [blame]
isherman06c6a9a72014-09-10 05:48:211// 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
hansberrye7ad3892016-12-19 22:19:215#include "components/cryptauth/wire_message.h"
isherman06c6a9a72014-09-10 05:48:216
avif57136c12015-12-25 23:27:457#include <stddef.h>
8#include <stdint.h>
9
tengs7b425ad82015-07-21 00:20:3510#include <limits>
11
petere0807cf2015-11-20 14:46:2412#include "base/base64url.h"
isherman3ca088e12014-09-16 00:21:0313#include "base/json/json_reader.h"
tengs7b425ad82015-07-21 00:20:3514#include "base/json/json_writer.h"
isherman3ca088e12014-09-16 00:21:0315#include "base/macros.h"
dcheng2f012692016-04-21 00:19:3416#include "base/memory/ptr_util.h"
isherman3ca088e12014-09-16 00:21:0317#include "base/values.h"
tengs7b425ad82015-07-21 00:20:3518#include "components/proximity_auth/logging/logging.h"
isherman3ca088e12014-09-16 00:21:0319
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
hansberrye7ad3892016-12-19 22:19:2126namespace cryptauth {
isherman3ca088e12014-09-16 00:21:0327namespace {
28
29// The length of the message header, in bytes.
30const size_t kHeaderLength = 3;
31
32// The protocol version of the message format.
tengs7b425ad82015-07-21 00:20:3533const int kMessageFormatVersionThree = 3;
isherman3ca088e12014-09-16 00:21:0334
35const char kPayloadKey[] = "payload";
khorimotof4594822017-01-19 20:14:5036const 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.
43const char kDefaultFeature[] = "easy_unlock";
isherman3ca088e12014-09-16 00:21:0344
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|.
50bool 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
mostynb470748ce2014-12-22 21:14:4658 static_assert(kHeaderLength > 2, "kHeaderLength too small");
isherman3ca088e12014-09-16 00:21:0359 size_t version = serialized_message[0];
tengs7b425ad82015-07-21 00:20:3560 if (version != kMessageFormatVersionThree) {
61 PA_LOG(WARNING) << "Error: Invalid message version. Got " << version
62 << ", expected " << kMessageFormatVersionThree;
isherman3ca088e12014-09-16 00:21:0363 return false;
64 }
65
tengs7b425ad82015-07-21 00:20:3566 uint16_t expected_body_length =
67 (static_cast<uint8_t>(serialized_message[1]) << 8) |
68 (static_cast<uint8_t>(serialized_message[2]) << 0);
isherman3ca088e12014-09-16 00:21:0369 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) {
tengs7b425ad82015-07-21 00:20:3575 PA_LOG(WARNING) << "Error: Invalid message length. Got "
76 << serialized_message.size() << ", expected "
77 << expected_message_length;
isherman3ca088e12014-09-16 00:21:0378 return false;
79 }
80
81 return true;
82}
83
84} // namespace
isherman06c6a9a72014-09-10 05:48:2185
hansberrye7ad3892016-12-19 22:19:2186WireMessage::~WireMessage() {}
isherman06c6a9a72014-09-10 05:48:2187
88// static
dcheng2f012692016-04-21 00:19:3489std::unique_ptr<WireMessage> WireMessage::Deserialize(
isherman3ca088e12014-09-16 00:21:0390 const std::string& serialized_message,
91 bool* is_incomplete_message) {
92 if (!ParseHeader(serialized_message, is_incomplete_message))
khorimotof4594822017-01-19 20:14:5093 return nullptr;
isherman3ca088e12014-09-16 00:21:0394
dcheng2f012692016-04-21 00:19:3495 std::unique_ptr<base::Value> body_value =
olli.raula1fd91c72015-09-08 13:43:2596 base::JSONReader::Read(serialized_message.substr(kHeaderLength));
jdoerriedc72ee942016-12-07 15:43:2897 if (!body_value || !body_value->IsType(base::Value::Type::DICTIONARY)) {
tengs7b425ad82015-07-21 00:20:3598 PA_LOG(WARNING) << "Error: Unable to parse message as JSON.";
khorimotof4594822017-01-19 20:14:5099 return nullptr;
isherman3ca088e12014-09-16 00:21:03100 }
101
102 base::DictionaryValue* body;
103 bool success = body_value->GetAsDictionary(&body);
104 DCHECK(success);
105
isherman3ca088e12014-09-16 00:21:03106 std::string payload_base64;
107 if (!body->GetString(kPayloadKey, &payload_base64) ||
108 payload_base64.empty()) {
tengs7b425ad82015-07-21 00:20:35109 PA_LOG(WARNING) << "Error: Missing payload.";
khorimotof4594822017-01-19 20:14:50110 return nullptr;
isherman3ca088e12014-09-16 00:21:03111 }
112
113 std::string payload;
petere0807cf2015-11-20 14:46:24114 if (!base::Base64UrlDecode(payload_base64,
115 base::Base64UrlDecodePolicy::REQUIRE_PADDING,
116 &payload)) {
tengs7b425ad82015-07-21 00:20:35117 PA_LOG(WARNING) << "Error: Invalid base64 encoding for payload.";
khorimotof4594822017-01-19 20:14:50118 return nullptr;
isherman3ca088e12014-09-16 00:21:03119 }
120
khorimotof4594822017-01-19 20:14:50121 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));
isherman06c6a9a72014-09-10 05:48:21127}
128
isherman83d08a292014-09-26 03:32:44129std::string WireMessage::Serialize() const {
tengs7b425ad82015-07-21 00:20:35130 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;
tengs7b425ad82015-07-21 00:20:35137
138 std::string base64_payload;
petere0807cf2015-11-20 14:46:24139 base::Base64UrlEncode(payload_, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
140 &base64_payload);
tengs7b425ad82015-07-21 00:20:35141 body.SetString(kPayloadKey, base64_payload);
khorimotof4594822017-01-19 20:14:50142 body.SetString(kFeatureKey, feature_);
tengs7b425ad82015-07-21 00:20:35143
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;
isherman83d08a292014-09-26 03:32:44168}
169
khorimotof4594822017-01-19 20:14:50170WireMessage::WireMessage(const std::string& payload, const std::string& feature)
171 : payload_(payload), feature_(feature) {}
isherman06c6a9a72014-09-10 05:48:21172
hansberrye7ad3892016-12-19 22:19:21173} // namespace cryptauth