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 "base/json/json_string_value_serializer.h"
6 #include "base/strings/stringprintf.h"
7 #include "chromeos/dbus/fake_easy_unlock_client.h"
11 // Keys generated using |GenerateEcP256KeyPair| are in the following format:
12 // "{<key_type>: <key_pair_index>}".
13 // <key_pair_index> is an integer identifying
15 // <key_type> specifies whether the key is public or private. It can have one
16 // of the following valies:
17 const char kEc256PrivateKeyKey
[] = "ec_p256_private_key";
18 const char kEc256PublicKeyKey
[] = "ec_p256_public_key";
20 // Extracts key pair index from a key in format "<key_type>: <key_pair_index>}".
21 int ExtractKeyPairIndexFromKey(const std::string
& key
,
22 const std::string
& key_type
) {
23 JSONStringValueSerializer
serializer(key
);
24 scoped_ptr
<base::Value
> json_value(serializer
.Deserialize(NULL
, NULL
));
28 base::DictionaryValue
* json_dictionary
= NULL
;
29 if (!json_value
->GetAsDictionary(&json_dictionary
))
32 int key_pair_index
= -1;
33 if (!json_dictionary
->GetInteger(key_type
, &key_pair_index
))
35 return key_pair_index
;
43 bool FakeEasyUnlockClient::IsEcP256KeyPair(const std::string
& private_key
,
44 const std::string
& public_key
) {
45 int private_key_index
=
46 ExtractKeyPairIndexFromKey(private_key
, kEc256PrivateKeyKey
);
47 int public_key_index
=
48 ExtractKeyPairIndexFromKey(public_key
, kEc256PublicKeyKey
);
50 return private_key_index
> 0 && public_key_index
== private_key_index
;
53 FakeEasyUnlockClient::FakeEasyUnlockClient() : generated_keys_count_(0) {}
55 FakeEasyUnlockClient::~FakeEasyUnlockClient() {}
57 void FakeEasyUnlockClient::Init(dbus::Bus
* bus
) {}
59 void FakeEasyUnlockClient::GenerateEcP256KeyPair(
60 const KeyPairCallback
& callback
) {
61 ++generated_keys_count_
;
64 base::StringPrintf("{\"%s\": %d}",
66 generated_keys_count_
),
67 base::StringPrintf("{\"%s\": %d}",
69 generated_keys_count_
));
72 void FakeEasyUnlockClient::WrapPublicKey(const std::string
& key_algorithm
,
73 const std::string
& public_key
,
74 const DataCallback
& callback
) {
75 callback
.Run(base::StringPrintf(
76 "{\"wrapped_key\": {\"algorithm\":\"%s\", \"key\":\"%s\"}}",
77 key_algorithm
.c_str(),
81 void FakeEasyUnlockClient::PerformECDHKeyAgreement(
82 const std::string
& private_key
,
83 const std::string
& public_key
,
84 const DataCallback
& callback
) {
85 int private_key_index
=
86 ExtractKeyPairIndexFromKey(private_key
, kEc256PrivateKeyKey
);
87 int public_key_index
=
88 ExtractKeyPairIndexFromKey(public_key
, kEc256PublicKeyKey
);
89 if (private_key_index
< 0 || public_key_index
< 0) {
94 // ECDH key agreement should be commutative in respect to key pairs to which
95 // used keys belong, i.e. (key_pair[1].private_key, key_pair[2].public_key)
96 // and (key_pair[2].private_key, key_pair[1].public_key) should produce the
97 // same shared key. To achieve this, identify the created key by sum and
98 // product of the used key pairs.
99 callback
.Run(base::StringPrintf(
100 "{\"secret_key\": [%d, %d]}",
101 private_key_index
+ public_key_index
,
102 private_key_index
* public_key_index
));
105 void FakeEasyUnlockClient::CreateSecureMessage(
106 const std::string
& payload
,
107 const CreateSecureMessageOptions
& options
,
108 const DataCallback
& callback
) {
109 callback
.Run(base::StringPrintf(
110 "{\"securemessage\": {"
111 "\"payload\": \"%s\","
113 "\"associated_data\": \"%s\","
114 "\"public_metadata\": \"%s\","
115 "\"verification_key_id\": \"%s\","
116 "\"decryption_key_id\": \"%s\","
117 "\"encryption_type\": \"%s\","
118 "\"signature_type\": \"%s\""
122 options
.associated_data
.c_str(),
123 options
.public_metadata
.c_str(),
124 options
.verification_key_id
.c_str(),
125 options
.decryption_key_id
.c_str(),
126 options
.encryption_type
.c_str(),
127 options
.signature_type
.c_str()));
130 void FakeEasyUnlockClient::UnwrapSecureMessage(
131 const std::string
& message
,
132 const UnwrapSecureMessageOptions
& options
,
133 const DataCallback
& callback
) {
134 // TODO(tbarzic): Verify that |message| is in the format returned by
135 // |CreateSecureMessage| and extract payload, metadata and
136 // verification_key_id from there.
137 callback
.Run(base::StringPrintf(
138 "{\"unwrapped_securemessage\": {"
139 "\"message\": \"%s\","
141 "\"associated_data\": \"%s\","
142 "\"encryption_type\": \"%s\","
143 "\"signature_type\": \"%s\""
147 options
.associated_data
.c_str(),
148 options
.encryption_type
.c_str(),
149 options
.signature_type
.c_str()));
152 } // namespace chromeos