| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/proximity_auth/wire_message.h" | 5 #include "components/proximity_auth/wire_message.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| 11 | 11 |
| 12 #include "base/base64url.h" | 12 #include "base/base64url.h" |
| 13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "components/proximity_auth/logging/logging.h" | 18 #include "components/proximity_auth/logging/logging.h" |
| 18 | 19 |
| 19 // The wire messages have a simple format: | 20 // The wire messages have a simple format: |
| 20 // [ message version ] [ body length ] [ JSON body ] | 21 // [ message version ] [ body length ] [ JSON body ] |
| 21 // 1 byte 2 bytes body length | 22 // 1 byte 2 bytes body length |
| 22 // The JSON body contains two fields: an optional permit_id field and a required | 23 // The JSON body contains two fields: an optional permit_id field and a required |
| 23 // data field. | 24 // data field. |
| 24 | 25 |
| 25 namespace proximity_auth { | 26 namespace proximity_auth { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 73 |
| 73 return true; | 74 return true; |
| 74 } | 75 } |
| 75 | 76 |
| 76 } // namespace | 77 } // namespace |
| 77 | 78 |
| 78 WireMessage::~WireMessage() { | 79 WireMessage::~WireMessage() { |
| 79 } | 80 } |
| 80 | 81 |
| 81 // static | 82 // static |
| 82 scoped_ptr<WireMessage> WireMessage::Deserialize( | 83 std::unique_ptr<WireMessage> WireMessage::Deserialize( |
| 83 const std::string& serialized_message, | 84 const std::string& serialized_message, |
| 84 bool* is_incomplete_message) { | 85 bool* is_incomplete_message) { |
| 85 if (!ParseHeader(serialized_message, is_incomplete_message)) | 86 if (!ParseHeader(serialized_message, is_incomplete_message)) |
| 86 return scoped_ptr<WireMessage>(); | 87 return std::unique_ptr<WireMessage>(); |
| 87 | 88 |
| 88 scoped_ptr<base::Value> body_value = | 89 std::unique_ptr<base::Value> body_value = |
| 89 base::JSONReader::Read(serialized_message.substr(kHeaderLength)); | 90 base::JSONReader::Read(serialized_message.substr(kHeaderLength)); |
| 90 if (!body_value || !body_value->IsType(base::Value::TYPE_DICTIONARY)) { | 91 if (!body_value || !body_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 91 PA_LOG(WARNING) << "Error: Unable to parse message as JSON."; | 92 PA_LOG(WARNING) << "Error: Unable to parse message as JSON."; |
| 92 return scoped_ptr<WireMessage>(); | 93 return std::unique_ptr<WireMessage>(); |
| 93 } | 94 } |
| 94 | 95 |
| 95 base::DictionaryValue* body; | 96 base::DictionaryValue* body; |
| 96 bool success = body_value->GetAsDictionary(&body); | 97 bool success = body_value->GetAsDictionary(&body); |
| 97 DCHECK(success); | 98 DCHECK(success); |
| 98 | 99 |
| 99 // The permit ID is optional. In the Easy Unlock protocol, only the first | 100 // The permit ID is optional. In the Easy Unlock protocol, only the first |
| 100 // message includes this field. | 101 // message includes this field. |
| 101 std::string permit_id; | 102 std::string permit_id; |
| 102 body->GetString(kPermitIdKey, &permit_id); | 103 body->GetString(kPermitIdKey, &permit_id); |
| 103 | 104 |
| 104 std::string payload_base64; | 105 std::string payload_base64; |
| 105 if (!body->GetString(kPayloadKey, &payload_base64) || | 106 if (!body->GetString(kPayloadKey, &payload_base64) || |
| 106 payload_base64.empty()) { | 107 payload_base64.empty()) { |
| 107 PA_LOG(WARNING) << "Error: Missing payload."; | 108 PA_LOG(WARNING) << "Error: Missing payload."; |
| 108 return scoped_ptr<WireMessage>(); | 109 return std::unique_ptr<WireMessage>(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 std::string payload; | 112 std::string payload; |
| 112 if (!base::Base64UrlDecode(payload_base64, | 113 if (!base::Base64UrlDecode(payload_base64, |
| 113 base::Base64UrlDecodePolicy::REQUIRE_PADDING, | 114 base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 114 &payload)) { | 115 &payload)) { |
| 115 PA_LOG(WARNING) << "Error: Invalid base64 encoding for payload."; | 116 PA_LOG(WARNING) << "Error: Invalid base64 encoding for payload."; |
| 116 return scoped_ptr<WireMessage>(); | 117 return std::unique_ptr<WireMessage>(); |
| 117 } | 118 } |
| 118 | 119 |
| 119 return make_scoped_ptr(new WireMessage(payload, permit_id)); | 120 return base::WrapUnique(new WireMessage(payload, permit_id)); |
| 120 } | 121 } |
| 121 | 122 |
| 122 std::string WireMessage::Serialize() const { | 123 std::string WireMessage::Serialize() const { |
| 123 if (payload_.empty()) { | 124 if (payload_.empty()) { |
| 124 PA_LOG(ERROR) << "Failed to serialize empty wire message."; | 125 PA_LOG(ERROR) << "Failed to serialize empty wire message."; |
| 125 return std::string(); | 126 return std::string(); |
| 126 } | 127 } |
| 127 | 128 |
| 128 // Create JSON body containing permit id and payload. | 129 // Create JSON body containing permit id and payload. |
| 129 base::DictionaryValue body; | 130 base::DictionaryValue body; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 } | 163 } |
| 163 | 164 |
| 164 WireMessage::WireMessage(const std::string& payload) | 165 WireMessage::WireMessage(const std::string& payload) |
| 165 : WireMessage(payload, std::string()) {} | 166 : WireMessage(payload, std::string()) {} |
| 166 | 167 |
| 167 WireMessage::WireMessage(const std::string& payload, | 168 WireMessage::WireMessage(const std::string& payload, |
| 168 const std::string& permit_id) | 169 const std::string& permit_id) |
| 169 : payload_(payload), permit_id_(permit_id) {} | 170 : payload_(payload), permit_id_(permit_id) {} |
| 170 | 171 |
| 171 } // namespace proximity_auth | 172 } // namespace proximity_auth |
| OLD | NEW |