OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Nikita (slow)
2013/12/09 17:51:08
nit: 2013
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/login/managed/supervised_user_authentication.h " | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/command_line.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "chrome/browser/chromeos/login/supervised_user_manager.h" | |
12 #include "chromeos/chromeos_switches.h" | |
13 #include "crypto/random.h" | |
14 #include "crypto/symmetric_key.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 namespace { | |
19 | |
20 const unsigned kNumIterations = 1234; | |
Nikita (slow)
2013/12/09 16:42:16
nit: Please add short comment about these constant
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
21 const unsigned kSaltSize = 32; | |
22 const unsigned kKeySizeInBits = 256; | |
23 | |
24 std::string CreateSalt() { | |
25 char result[kSaltSize]; | |
26 crypto::RandBytes(&result, sizeof(result)); | |
27 return StringToLowerASCII(base::HexEncode( | |
28 reinterpret_cast<const void*>(result), | |
29 sizeof(result))); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 SupervisedUserAuthentication::SupervisedUserAuthentication( | |
35 SupervisedUserManager* owner) | |
36 : owner_(owner), | |
37 should_migrate_(false), | |
38 target_version_(kPlainPasswordSchema) { | |
39 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
40 if (command_line->HasSwitch(switches::kEnableSupervisedPasswordSync)) { | |
41 should_migrate_ = true; | |
42 target_version_ = kPasswordEncryptedWithSaltSchema; | |
43 } | |
44 } | |
45 | |
46 SupervisedUserAuthentication::~SupervisedUserAuthentication() {} | |
47 | |
48 std::string SupervisedUserAuthentication::TransformPassword( | |
49 const std::string& user_id, | |
50 const std::string& password) { | |
51 int user_schema_version = GetPasswordSchemaVersion(user_id); | |
52 if (kPlainPasswordSchema == user_schema_version) | |
53 return password; | |
54 if (kPasswordEncryptedWithSaltSchema == user_schema_version) { | |
Nikita (slow)
2013/12/09 17:51:08
nit: How about wrapping this into else if
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
55 base::DictionaryValue holder; | |
56 std::string salt; | |
57 owner_->GetPasswordInformation(user_id, &holder); | |
58 holder.GetStringWithoutPathExpansion(kSalt, &salt); | |
59 DCHECK(!salt.empty()); | |
60 return BuildPasswordForSchemaV2(salt, password); | |
61 } | |
62 NOTREACHED(); | |
Nikita (slow)
2013/12/09 17:51:08
nit: Put this block into else?
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
63 return password; | |
64 } | |
65 | |
66 bool SupervisedUserAuthentication::FillDataForNewUser( | |
67 const std::string& user_id, | |
68 const std::string& password, | |
69 base::DictionaryValue* password_data) { | |
70 int schema = target_version_; | |
71 if (schema == kPlainPasswordSchema) | |
Nikita (slow)
2013/12/09 17:51:08
nit: Please keep consistency with previous method:
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
72 return false; | |
73 if (schema == kPasswordEncryptedWithSaltSchema) { | |
Nikita (slow)
2013/12/09 17:51:08
nit:
if (v1) {
..
} else if (v2) {
..
} else {
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
Bernhard Bauer
2013/12/14 03:02:30
Um... sorry, but http://dev.chromium.org/developer
Nikita (slow)
2013/12/15 06:55:12
So this should be like
bool result = false;
if (.
Bernhard Bauer
2013/12/15 15:39:56
No, the else should go, not the return :)
So it w
| |
74 password_data->SetStringWithoutPathExpansion( | |
Bernhard Bauer
2013/12/11 14:46:43
You don't need to store everything as a string. Yo
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Yes, but (as I understand) if I at some point set
Bernhard Bauer
2013/12/13 00:30:50
Sync doesn't know about the preferences as they ar
| |
75 kSchemaVersion, base::IntToString(schema)); | |
Nikita (slow)
2013/12/09 17:51:08
nit: Will it fit? First parameter on previous line
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
76 std::string salt = CreateSalt(); | |
77 password_data->SetStringWithoutPathExpansion( | |
78 kSalt, salt); | |
Nikita (slow)
2013/12/09 17:51:08
nit: Fits on the previous line.
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
79 password_data->SetStringWithoutPathExpansion( | |
80 kPasswordVersion, std::string("1")); | |
Nikita (slow)
2013/12/09 17:51:08
nit: first parameter on the previous line, another
Nikita (slow)
2013/12/09 17:51:08
nit: std::string("1") - magic constant?
Denis Kuznetsov (DE-MUC)
2013/12/12 19:45:24
Done.
| |
81 password_data->SetStringWithoutPathExpansion( | |
82 kEncryptedPassword, BuildPasswordForSchemaV2(salt, password)); | |
83 return true; | |
84 } | |
85 NOTREACHED(); | |
86 return false; | |
87 } | |
88 | |
89 void SupervisedUserAuthentication::StorePasswordData( | |
90 const std::string& user_id, | |
91 const base::DictionaryValue& password_data) { | |
92 DictionaryValue holder; | |
93 owner_->GetPasswordInformation(user_id, &holder); | |
94 const base::Value* value; | |
95 if (password_data.GetWithoutPathExpansion(kSchemaVersion, &value)) | |
96 holder.SetWithoutPathExpansion(kSchemaVersion, value->DeepCopy()); | |
97 if (password_data.GetWithoutPathExpansion(kSalt, &value)) | |
98 holder.SetWithoutPathExpansion(kSalt, value->DeepCopy()); | |
99 if (password_data.GetWithoutPathExpansion(kPasswordVersion, &value)) | |
100 holder.SetWithoutPathExpansion(kPasswordVersion, value->DeepCopy()); | |
101 owner_->SetPasswordInformation(user_id, &holder); | |
102 } | |
103 | |
104 std::string SupervisedUserAuthentication::BuildPasswordForSchemaV2( | |
105 const std::string& salt, | |
106 const std::string& plain_password) { | |
107 scoped_ptr<crypto::SymmetricKey> key( | |
108 crypto::SymmetricKey::DeriveKeyFromPassword( | |
109 crypto::SymmetricKey::AES, | |
110 plain_password, salt, | |
111 kNumIterations, kKeySizeInBits)); | |
112 std::string raw_result, result; | |
113 key->GetRawKey(&raw_result); | |
114 base::Base64Encode(raw_result, &result); | |
115 return result; | |
116 } | |
117 | |
118 bool SupervisedUserAuthentication::PasswordNeedsMigration( | |
119 const std::string& user_id) { | |
120 return GetPasswordSchemaVersion(user_id) < target_version_; | |
121 } | |
122 | |
123 int SupervisedUserAuthentication::GetPasswordSchemaVersion( | |
124 const std::string& user_id) { | |
125 base::DictionaryValue holder; | |
126 std::string schema_version_string; | |
127 owner_->GetPasswordInformation(user_id, &holder); | |
128 // Default version. | |
129 int schema_version = kPlainPasswordSchema; | |
130 if (holder.GetStringWithoutPathExpansion(kSchemaVersion, | |
131 &schema_version_string)) { | |
132 schema_version = atoi(schema_version_string.c_str()); | |
133 } | |
134 return schema_version; | |
135 } | |
136 | |
137 void SupervisedUserAuthentication::SchedulePasswordMigration( | |
138 const std::string& supervised_user_id, | |
139 const std::string& user_password, | |
140 SupervisedUserLoginFlow* user_flow) { | |
141 // TODO(antrim): Add actual migration code once cryptohome has required API. | |
142 } | |
143 | |
144 } // namespace chromeos | |
OLD | NEW |