OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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. | |
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 // Byte size of hash salt. | |
21 const unsigned kSaltSize = 32; | |
22 | |
23 // Parameters of cryptographic hashing. | |
24 const unsigned kNumIterations = 1234; | |
25 const unsigned kKeySizeInBits = 256; | |
26 | |
27 std::string CreateSalt() { | |
28 char result[kSaltSize]; | |
29 crypto::RandBytes(&result, sizeof(result)); | |
30 return StringToLowerASCII(base::HexEncode( | |
31 reinterpret_cast<const void*>(result), | |
32 sizeof(result))); | |
33 } | |
34 | |
35 std::string BuildPasswordForHashWithSaltSchema( | |
36 const std::string& salt, | |
37 const std::string& plain_password) { | |
38 scoped_ptr<crypto::SymmetricKey> key( | |
39 crypto::SymmetricKey::DeriveKeyFromPassword( | |
40 crypto::SymmetricKey::AES, | |
41 plain_password, salt, | |
42 kNumIterations, kKeySizeInBits)); | |
43 std::string raw_result, result; | |
44 key->GetRawKey(&raw_result); | |
45 base::Base64Encode(raw_result, &result); | |
46 return result; | |
47 } | |
48 | |
49 | |
50 | |
Bernhard Bauer
2013/12/19 18:09:58
Nit: remove two empty lines?
| |
51 } // namespace | |
Bernhard Bauer
2013/12/19 18:09:58
Nit: two spaces before //
| |
52 | |
53 SupervisedUserAuthentication::SupervisedUserAuthentication( | |
54 SupervisedUserManager* owner) | |
55 : owner_(owner), | |
56 migration_enabled_(false), | |
57 stable_schema_(SCHEMA_PLAIN) { | |
58 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
59 if (command_line->HasSwitch(switches::kEnableSupervisedPasswordSync)) { | |
60 migration_enabled_ = true; | |
61 stable_schema_ = SCHEMA_SALT_HASHED; | |
62 } | |
63 } | |
64 | |
65 SupervisedUserAuthentication::~SupervisedUserAuthentication() {} | |
66 | |
67 std::string SupervisedUserAuthentication::TransformPassword( | |
68 const std::string& user_id, | |
69 const std::string& password) { | |
70 int user_schema = GetPasswordSchema(user_id); | |
71 if (SCHEMA_PLAIN == user_schema) | |
Bernhard Bauer
2013/12/19 18:09:58
Nit: Could you swap these? It reads much better th
| |
72 return password; | |
73 | |
74 if (SCHEMA_SALT_HASHED == user_schema) { | |
75 base::DictionaryValue holder; | |
76 std::string salt; | |
77 owner_->GetPasswordInformation(user_id, &holder); | |
78 holder.GetStringWithoutPathExpansion(kSalt, &salt); | |
79 DCHECK(!salt.empty()); | |
80 return BuildPasswordForHashWithSaltSchema(salt, password); | |
81 } | |
82 NOTREACHED(); | |
83 return password; | |
84 } | |
85 | |
86 bool SupervisedUserAuthentication::FillDataForNewUser( | |
87 const std::string& user_id, | |
88 const std::string& password, | |
89 base::DictionaryValue* password_data) { | |
90 Schema schema = stable_schema_; | |
91 if (SCHEMA_PLAIN == schema) { | |
92 return false; | |
93 } else if (SCHEMA_SALT_HASHED == schema) { | |
Bernhard Bauer
2013/12/19 18:09:58
Nit: No else if you return in the previous branch.
| |
94 password_data->SetStringWithoutPathExpansion(kSchemaVersion, | |
95 base::IntToString(schema)); | |
96 std::string salt = CreateSalt(); | |
97 password_data->SetStringWithoutPathExpansion(kSalt, salt); | |
98 password_data->SetStringWithoutPathExpansion(kPasswordRevision, | |
99 base::IntToString(kMinPasswordRevision)); | |
100 password_data->SetStringWithoutPathExpansion(kEncryptedPassword, | |
101 BuildPasswordForHashWithSaltSchema(salt, password)); | |
102 return true; | |
103 } else { | |
104 NOTREACHED(); | |
105 return false; | |
106 } | |
107 } | |
108 | |
109 void SupervisedUserAuthentication::StorePasswordData( | |
110 const std::string& user_id, | |
111 const base::DictionaryValue& password_data) { | |
112 DictionaryValue holder; | |
113 owner_->GetPasswordInformation(user_id, &holder); | |
114 const base::Value* value; | |
115 if (password_data.GetWithoutPathExpansion(kSchemaVersion, &value)) | |
116 holder.SetWithoutPathExpansion(kSchemaVersion, value->DeepCopy()); | |
117 if (password_data.GetWithoutPathExpansion(kSalt, &value)) | |
118 holder.SetWithoutPathExpansion(kSalt, value->DeepCopy()); | |
119 if (password_data.GetWithoutPathExpansion(kPasswordRevision, &value)) | |
120 holder.SetWithoutPathExpansion(kPasswordRevision, value->DeepCopy()); | |
121 owner_->SetPasswordInformation(user_id, &holder); | |
122 } | |
123 | |
124 bool SupervisedUserAuthentication::PasswordNeedsMigration( | |
125 const std::string& user_id) { | |
126 return GetPasswordSchema(user_id) < stable_schema_; | |
127 } | |
128 | |
129 SupervisedUserAuthentication::Schema | |
130 SupervisedUserAuthentication::GetPasswordSchema( | |
131 const std::string& user_id) { | |
132 base::DictionaryValue holder; | |
133 std::string schema_version_string; | |
134 owner_->GetPasswordInformation(user_id, &holder); | |
135 // Default version. | |
136 Schema schema_version = SCHEMA_PLAIN; | |
137 if (holder.GetStringWithoutPathExpansion(kSchemaVersion, | |
138 &schema_version_string)) { | |
139 schema_version = static_cast<Schema>(atoi(schema_version_string.c_str())); | |
140 } | |
141 return schema_version; | |
142 } | |
143 | |
144 void SupervisedUserAuthentication::SchedulePasswordMigration( | |
145 const std::string& supervised_user_id, | |
146 const std::string& user_password, | |
147 SupervisedUserLoginFlow* user_flow) { | |
148 // TODO(antrim): Add actual migration code once cryptohome has required API. | |
149 } | |
150 | |
151 } // namespace chromeos | |
OLD | NEW |