Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: components/gcm_driver/instance_id/instance_id_impl.cc

Issue 1113213002: Generate Instance ID locally. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove leftover code Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/gcm_driver/instance_id/instance_id_impl.h" 5 #include "components/gcm_driver/instance_id/instance_id_impl.h"
6 6
7 #include <algorithm>
8 #include "base/base64.h"
9 #include "base/bind.h"
7 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/sha1.h"
13 #include "crypto/rsa_private_key.h"
8 14
9 namespace instance_id { 15 namespace instance_id {
10 16
11 // static 17 // static
12 InstanceID* InstanceID::Create(const std::string& app_id) { 18 InstanceID* InstanceID::Create(const std::string& app_id) {
13 return new InstanceIDImpl(app_id); 19 return new InstanceIDImpl(app_id);
14 } 20 }
15 21
16 InstanceIDImpl::InstanceIDImpl(const std::string& app_id) 22 InstanceIDImpl::InstanceIDImpl(const std::string& app_id)
17 : InstanceID(app_id) { 23 : InstanceID(app_id) {
18 } 24 }
19 25
20 InstanceIDImpl::~InstanceIDImpl() { 26 InstanceIDImpl::~InstanceIDImpl() {
21 } 27 }
22 28
23 std::string InstanceIDImpl::GetID() { 29 std::string InstanceIDImpl::GetID() {
24 NOTIMPLEMENTED(); 30 EnsureIDGenerated();
25 return std::string(); 31 return id_;
26 } 32 }
27 33
28 base::Time InstanceIDImpl::GetCreationTime() { 34 base::Time InstanceIDImpl::GetCreationTime() {
29 NOTIMPLEMENTED(); 35 EnsureIDGenerated();
fgorski 2015/04/30 22:13:12 Are you sure we should create ID when code queries
jianli 2015/05/02 00:30:18 Sounds reasonable. Changed.
30 return base::Time(); 36 return creation_time_;
31 } 37 }
32 38
33 void InstanceIDImpl::GetToken( 39 void InstanceIDImpl::GetToken(
34 const std::string& audience, 40 const std::string& audience,
35 const std::string& scope, 41 const std::string& scope,
36 const std::map<std::string, std::string>& options, 42 const std::map<std::string, std::string>& options,
37 const GetTokenCallback& callback) { 43 const GetTokenCallback& callback) {
38 NOTIMPLEMENTED(); 44 NOTIMPLEMENTED();
39 } 45 }
40 46
41 void InstanceIDImpl::DeleteToken(const std::string& audience, 47 void InstanceIDImpl::DeleteToken(const std::string& audience,
42 const std::string& scope, 48 const std::string& scope,
43 const DeleteTokenCallback& callback) { 49 const DeleteTokenCallback& callback) {
44 NOTIMPLEMENTED(); 50 NOTIMPLEMENTED();
45 } 51 }
46 52
47 void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { 53 void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) {
48 NOTIMPLEMENTED(); 54 // TODO(jianli): Delete the key pair from the store.
55 key_pair_.reset();
56 id_.clear();
57 creation_time_ = base::Time();
58
59 base::MessageLoop::current()->PostTask(
60 FROM_HERE,
61 base::Bind(callback, this, InstanceID::SUCCESS));
62 }
63
64 void InstanceIDImpl::EnsureIDGenerated() {
65 if (!id_.empty())
66 return;
67
68 // Generate the public/private key pair based on random number generator if
69 // not yet.
70 if (!key_pair_) {
71 key_pair_.reset(crypto::RSAPrivateKey::Create(2048));
fgorski 2015/04/30 22:13:12 const kKeyLength?
jianli 2015/05/02 00:30:18 Not needed any more.
72 if (!key_pair_) {
73 DVLOG(1) << "Cannot generate key pair.";
74 return;
75 }
76
77 // Record the time.
78 creation_time_ = base::Time::Now();
79
80 // TODO(jianli): Store the key pair.
81 }
82
83 // Now produce the ID based on the public key of the key pair.
84
85 // 1) Computes the SHA1 of public key of the key pair.
86 std::vector<uint8> public_key;
87 if (!key_pair_->ExportPublicKey(&public_key)) {
88 DVLOG(1) << "Cannot export public key.";
89 return;
90 }
91
92 uint8 digest[base::kSHA1Length];
93 base::SHA1HashBytes(public_key.data(), public_key.size(), digest);
94
95 // 2) Transforms the first 4 bits of the SHA1 value to 0x9.
96 uint8 d0 = digest[0];
97 d0 = 0x90 + (0xF & d0);
98 digest[0] = d0 & 0xFF;
99
100 // 3) Truncates the transformed SHA1 value to 8 bytes and encode it in in
101 // Android-compatible base64 scheme:
fgorski 2015/04/30 22:13:12 move below as point 4
jianli 2015/05/02 00:30:17 Not needed any more.
102 // * URL safe: '/' replaced by '_' and '+' replaced by '-'.
103 // * No padding: any trailing '=' will be removed.
104 base::Base64Encode(
105 base::StringPiece(reinterpret_cast<const char*>(digest), 8),
106 &id_);
107 std::replace(id_.begin(), id_.end(), '+', '-');
108 std::replace(id_.begin(), id_.end(), '/', '_');
109 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end());
49 } 110 }
50 111
51 } // namespace instance_id 112 } // namespace instance_id
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698