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 "chrome/browser/signin/easy_unlock_auth_attempt.h"
7 #include "base/logging.h"
8 #include "chrome/browser/signin/easy_unlock_app_manager.h"
9 #include "chrome/browser/signin/screenlock_bridge.h"
10 #include "crypto/encryptor.h"
11 #include "crypto/symmetric_key.h"
13 #if defined(OS_CHROMEOS)
14 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h"
19 // Decrypts the secret that should be used to login from |wrapped_secret| using
20 // raw AES key |raw_key|.
21 // In a case of error, an empty string is returned.
22 std::string
UnwrapSecret(const std::string
& wrapped_secret
,
23 const std::string
& raw_key
) {
27 // Import the key structure.
28 scoped_ptr
<crypto::SymmetricKey
> key(
29 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES
, raw_key
));
34 std::string
iv(raw_key
.size(), ' ');
35 crypto::Encryptor encryptor
;
36 if (!encryptor
.Init(key
.get(), crypto::Encryptor::CBC
, iv
))
40 if (!encryptor
.Decrypt(wrapped_secret
, &secret
))
48 EasyUnlockAuthAttempt::EasyUnlockAuthAttempt(EasyUnlockAppManager
* app_manager
,
49 const std::string
& user_id
,
51 : app_manager_(app_manager
),
57 EasyUnlockAuthAttempt::~EasyUnlockAuthAttempt() {
58 if (state_
== STATE_RUNNING
)
62 bool EasyUnlockAuthAttempt::Start() {
63 DCHECK(state_
== STATE_IDLE
);
65 if (!ScreenlockBridge::Get()->IsLocked())
68 ScreenlockBridge::LockHandler::AuthType auth_type
=
69 ScreenlockBridge::Get()->lock_handler()->GetAuthType(user_id_
);
71 if (auth_type
!= ScreenlockBridge::LockHandler::USER_CLICK
) {
76 state_
= STATE_RUNNING
;
78 if (!app_manager_
->SendAuthAttemptEvent()) {
86 void EasyUnlockAuthAttempt::FinalizeUnlock(const std::string
& user_id
,
88 if (state_
!= STATE_RUNNING
|| user_id
!= user_id_
)
91 if (!ScreenlockBridge::Get()->IsLocked())
94 if (type_
!= TYPE_UNLOCK
) {
100 ScreenlockBridge::Get()->lock_handler()->Unlock(user_id_
);
102 ScreenlockBridge::Get()->lock_handler()->EnableInput();
108 void EasyUnlockAuthAttempt::FinalizeSignin(const std::string
& user_id
,
109 const std::string
& wrapped_secret
,
110 const std::string
& raw_session_key
) {
111 if (state_
!= STATE_RUNNING
|| user_id
!= user_id_
)
114 if (!ScreenlockBridge::Get()->IsLocked())
117 if (type_
!= TYPE_SIGNIN
) {
122 if (wrapped_secret
.empty()) {
127 std::string unwrapped_secret
= UnwrapSecret(wrapped_secret
, raw_session_key
);
129 std::string key_label
;
130 #if defined(OS_CHROMEOS)
131 key_label
= chromeos::EasyUnlockKeyManager::GetKeyLabel(0u);
132 #endif // defined(OS_CHROMEOS)
134 ScreenlockBridge::Get()->lock_handler()->AttemptEasySignin(
141 void EasyUnlockAuthAttempt::Cancel(const std::string
& user_id
) {
144 if (!ScreenlockBridge::Get()->IsLocked())
147 if (type_
== TYPE_UNLOCK
) {
148 ScreenlockBridge::Get()->lock_handler()->EnableInput();
150 // Attempting signin with an empty secret is equivalent to canceling the
152 ScreenlockBridge::Get()->lock_handler()->AttemptEasySignin(user_id
, "", "");