Fix content_browsertests on Linux GN build.
[chromium-blink-merge.git] / chromeos / dbus / fake_easy_unlock_client.cc
blobee741fb380595713db6880b93279503530f8c5ee
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"
9 namespace {
11 // Keys generated using |GenerateEcP256KeyPair| are in the following format:
12 // "{<key_type>: <key_pair_index>}".
13 // <key_pair_index> is an integer identifying
14 // the key pair.
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));
25 if (!json_value)
26 return -1;
28 base::DictionaryValue* json_dictionary = NULL;
29 if (!json_value->GetAsDictionary(&json_dictionary))
30 return -1;
32 int key_pair_index = -1;
33 if (!json_dictionary->GetInteger(key_type, &key_pair_index))
34 return -1;
35 return key_pair_index;
38 } // namespace
40 namespace chromeos {
42 // static
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_;
63 callback.Run(
64 base::StringPrintf("{\"%s\": %d}",
65 kEc256PrivateKeyKey,
66 generated_keys_count_),
67 base::StringPrintf("{\"%s\": %d}",
68 kEc256PublicKeyKey,
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(),
78 public_key.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) {
90 callback.Run("");
91 return;
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\","
112 "\"key\": \"%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\""
119 "}}",
120 payload.c_str(),
121 options.key.c_str(),
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\","
140 "\"key\": \"%s\","
141 "\"associated_data\": \"%s\","
142 "\"encryption_type\": \"%s\","
143 "\"signature_type\": \"%s\""
144 "}}",
145 message.c_str(),
146 options.key.c_str(),
147 options.associated_data.c_str(),
148 options.encryption_type.c_str(),
149 options.signature_type.c_str()));
152 } // namespace chromeos