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.
5 #include "chromeos/dbus/easy_unlock_client.h"
10 #include "base/compiler_specific.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/object_proxy.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
21 // Reads array of bytes from a dbus message reader and converts it to string.
22 std::string
PopResponseData(dbus::MessageReader
* reader
) {
23 const uint8
* bytes
= NULL
;
25 if (!reader
->PopArrayOfBytes(&bytes
, &length
))
28 return std::string(reinterpret_cast<const char*>(bytes
), length
);
31 // Converts string to array of bytes and writes it using dbus meddage writer.
32 void AppendStringAsByteArray(const std::string
& data
,
33 dbus::MessageWriter
* writer
) {
34 writer
->AppendArrayOfBytes(reinterpret_cast<const uint8
*>(data
.data()),
38 // The EasyUnlockClient used in production (and returned by
39 // EasyUnlockClient::Create).
40 class EasyUnlockClientImpl
: public EasyUnlockClient
{
42 EasyUnlockClientImpl() : proxy_(NULL
), weak_ptr_factory_(this) {}
44 virtual ~EasyUnlockClientImpl() {}
46 // EasyUnlockClient override.
47 virtual void PerformECDHKeyAgreement(const std::string
& private_key
,
48 const std::string
& public_key
,
49 const DataCallback
& callback
) OVERRIDE
{
50 dbus::MethodCall
method_call(
51 easy_unlock::kEasyUnlockServiceInterface
,
52 easy_unlock::kPerformECDHKeyAgreementMethod
);
53 dbus::MessageWriter
writer(&method_call
);
54 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
55 // not guaranteed here, so the method uses byte arrays.
56 AppendStringAsByteArray(private_key
, &writer
);
57 AppendStringAsByteArray(public_key
, &writer
);
58 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
59 base::Bind(&EasyUnlockClientImpl::OnData
,
60 weak_ptr_factory_
.GetWeakPtr(),
64 // EasyUnlockClient override.
65 virtual void GenerateEcP256KeyPair(const KeyPairCallback
& callback
) OVERRIDE
{
66 dbus::MethodCall
method_call(
67 easy_unlock::kEasyUnlockServiceInterface
,
68 easy_unlock::kGenerateEcP256KeyPairMethod
);
69 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
70 base::Bind(&EasyUnlockClientImpl::OnKeyPair
,
71 weak_ptr_factory_
.GetWeakPtr(),
75 // EasyUnlockClient override.
76 virtual void CreateSecureMessage(const std::string
& payload
,
77 const std::string
& secret_key
,
78 const std::string
& associated_data
,
79 const std::string
& public_metadata
,
80 const std::string
& verification_key_id
,
81 const std::string
& decryption_key_id
,
82 const std::string
& encryption_type
,
83 const std::string
& signature_type
,
84 const DataCallback
& callback
) OVERRIDE
{
85 dbus::MethodCall
method_call(
86 easy_unlock::kEasyUnlockServiceInterface
,
87 easy_unlock::kCreateSecureMessageMethod
);
88 dbus::MessageWriter
writer(&method_call
);
89 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
90 // not guaranteed here, so the method uses byte arrays.
91 AppendStringAsByteArray(payload
, &writer
);
92 AppendStringAsByteArray(secret_key
, &writer
);
93 AppendStringAsByteArray(associated_data
, &writer
);
94 AppendStringAsByteArray(public_metadata
, &writer
);
95 AppendStringAsByteArray(verification_key_id
, &writer
);
96 AppendStringAsByteArray(decryption_key_id
, &writer
);
97 writer
.AppendString(encryption_type
);
98 writer
.AppendString(signature_type
);
99 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
100 base::Bind(&EasyUnlockClientImpl::OnData
,
101 weak_ptr_factory_
.GetWeakPtr(),
105 // EasyUnlockClient override.
106 virtual void UnwrapSecureMessage(const std::string
& message
,
107 const std::string
& secret_key
,
108 const std::string
& associated_data
,
109 const std::string
& encryption_type
,
110 const std::string
& signature_type
,
111 const DataCallback
& callback
) OVERRIDE
{
112 dbus::MethodCall
method_call(
113 easy_unlock::kEasyUnlockServiceInterface
,
114 easy_unlock::kUnwrapSecureMessageMethod
);
115 dbus::MessageWriter
writer(&method_call
);
116 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
117 // not guaranteed here, so the method uses byte arrays.
118 AppendStringAsByteArray(message
, &writer
);
119 AppendStringAsByteArray(secret_key
, &writer
);
120 AppendStringAsByteArray(associated_data
, &writer
);
121 writer
.AppendString(encryption_type
);
122 writer
.AppendString(signature_type
);
123 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
124 base::Bind(&EasyUnlockClientImpl::OnData
,
125 weak_ptr_factory_
.GetWeakPtr(),
130 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
133 easy_unlock::kEasyUnlockServiceName
,
134 dbus::ObjectPath(easy_unlock::kEasyUnlockServicePath
));
138 void OnData(const DataCallback
& callback
, dbus::Response
* response
) {
144 dbus::MessageReader
reader(response
);
145 callback
.Run(PopResponseData(&reader
));
148 void OnKeyPair(const KeyPairCallback
& callback
, dbus::Response
* response
) {
150 callback
.Run("", "");
154 dbus::MessageReader
reader(response
);
155 std::string private_key
= PopResponseData(&reader
);
156 std::string public_key
= PopResponseData(&reader
);
158 if (public_key
.empty() || private_key
.empty()) {
159 callback
.Run("", "");
163 callback
.Run(private_key
, public_key
);
166 dbus::ObjectProxy
* proxy_
;
168 // Note: This should remain the last member so it'll be destroyed and
169 // invalidate its weak pointers before any other members are destroyed.
170 base::WeakPtrFactory
<EasyUnlockClientImpl
> weak_ptr_factory_
;
172 DISALLOW_COPY_AND_ASSIGN(EasyUnlockClientImpl
);
177 EasyUnlockClient::EasyUnlockClient() {
180 EasyUnlockClient::~EasyUnlockClient() {
184 EasyUnlockClient
* EasyUnlockClient::Create() {
185 return new EasyUnlockClientImpl();
188 } // namespace chromeos