Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Oleksandr Kulkov | 9c7cf955 | 2022-06-02 12:25:43 | [diff] [blame] | 5 | #include "chrome/browser/certificate_provider/pin_dialog_manager.h" |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 6 | |
Jan Wilken Dörrie | b5a41c3 | 2020-12-09 18:55:47 | [diff] [blame] | 7 | #include "base/containers/contains.h" |
Lei Zhang | a64664b | 2021-06-30 16:09:38 | [diff] [blame] | 8 | #include "base/containers/cxx20_erase.h" |
Avi Drissman | 02e49e58 | 2023-01-07 01:23:18 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 10 | #include "base/logging.h" |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 11 | |
Oleksandr Kulkov | 1213dee8 | 2022-05-17 20:45:34 | [diff] [blame] | 12 | namespace chromeos { |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 13 | |
| 14 | // Define timeout for issued sign_request_id. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 15 | constexpr base::TimeDelta kSignRequestIdTimeout = base::Minutes(10); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 16 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 17 | PinDialogManager::PinDialogManager() = default; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 18 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 19 | PinDialogManager::~PinDialogManager() = default; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 20 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 21 | void PinDialogManager::AddSignRequestId( |
| 22 | const std::string& extension_id, |
| 23 | int sign_request_id, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame^] | 24 | const std::optional<AccountId>& authenticating_user_account_id) { |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 25 | ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 26 | sign_requests_.insert( |
| 27 | std::make_pair(key, SignRequestState(/*begin_time=*/base::Time::Now(), |
| 28 | authenticating_user_account_id))); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 29 | } |
| 30 | |
Oleksandr Kulkov | d47d745 | 2021-11-30 18:16:59 | [diff] [blame] | 31 | void PinDialogManager::RemoveSignRequest(const std::string& extension_id, |
| 32 | int sign_request_id) { |
Maksim Ivanov | 7652d0d | 2019-09-04 19:12:19 | [diff] [blame] | 33 | if (active_dialog_state_ && |
| 34 | active_dialog_state_->extension_id == extension_id && |
| 35 | active_dialog_state_->sign_request_id == sign_request_id) { |
| 36 | CloseActiveDialog(); |
| 37 | } |
| 38 | |
| 39 | ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
| 40 | sign_requests_.erase(key); |
| 41 | } |
| 42 | |
Oleksandr Kulkov | 2ba63f01 | 2021-12-15 15:07:43 | [diff] [blame] | 43 | int PinDialogManager::StoredSignRequestsForTesting() const { |
| 44 | return sign_requests_.size(); |
| 45 | } |
| 46 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 47 | PinDialogManager::RequestPinResult PinDialogManager::RequestPin( |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 48 | const std::string& extension_id, |
| 49 | const std::string& extension_name, |
| 50 | int sign_request_id, |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame] | 51 | security_token_pin::CodeType code_type, |
| 52 | security_token_pin::ErrorLabel error_label, |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 53 | int attempts_left, |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 54 | RequestPinCallback callback) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 55 | DCHECK_GE(attempts_left, -1); |
| 56 | const bool accept_input = (attempts_left != 0); |
| 57 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 58 | // Check the validity of sign_request_id. |
| 59 | const SignRequestState* const sign_request_state = |
| 60 | FindSignRequestState(extension_id, sign_request_id); |
| 61 | if (!sign_request_state) |
| 62 | return RequestPinResult::kInvalidId; |
| 63 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 64 | // Start from sanity checks, as the extension might have issued this call |
| 65 | // incorrectly. |
| 66 | if (active_dialog_state_) { |
| 67 | // The active dialog exists already, so we need to make sure it belongs to |
| 68 | // the same extension and the user submitted some input. |
| 69 | if (extension_id != active_dialog_state_->extension_id) |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 70 | return RequestPinResult::kOtherFlowInProgress; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 71 | if (active_dialog_state_->request_pin_callback || |
| 72 | active_dialog_state_->stop_pin_request_callback) { |
| 73 | // Extension requests a PIN without having received any input from its |
| 74 | // previous request. Reject the new request. |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 75 | return RequestPinResult::kDialogDisplayedAlready; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 76 | } |
| 77 | } else { |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 78 | // Check that the sign request hasn't timed out yet. |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 79 | const base::Time current_time = base::Time::Now(); |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 80 | if (current_time - sign_request_state->begin_time > kSignRequestIdTimeout) |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 81 | return RequestPinResult::kInvalidId; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 82 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 83 | // A new dialog will be opened, so initialize the related internal state. |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 84 | active_dialog_state_.emplace(GetHostForNewDialog(), extension_id, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 85 | extension_name, sign_request_id, code_type); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 86 | } |
| 87 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 88 | active_dialog_state_->request_pin_callback = std::move(callback); |
| 89 | active_dialog_state_->host->ShowSecurityTokenPinDialog( |
| 90 | extension_name, code_type, accept_input, error_label, attempts_left, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 91 | sign_request_state->authenticating_user_account_id, |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 92 | base::BindOnce(&PinDialogManager::OnPinEntered, |
| 93 | weak_factory_.GetWeakPtr()), |
| 94 | base::BindOnce(&PinDialogManager::OnPinDialogClosed, |
| 95 | weak_factory_.GetWeakPtr())); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 96 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 97 | return RequestPinResult::kSuccess; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 98 | } |
| 99 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 100 | PinDialogManager::StopPinRequestResult |
Maksim Ivanov | b2985b1 | 2019-08-08 15:23:58 | [diff] [blame] | 101 | PinDialogManager::StopPinRequestWithError( |
| 102 | const std::string& extension_id, |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame] | 103 | security_token_pin::ErrorLabel error_label, |
Maksim Ivanov | b2985b1 | 2019-08-08 15:23:58 | [diff] [blame] | 104 | StopPinRequestCallback callback) { |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame] | 105 | DCHECK_NE(error_label, security_token_pin::ErrorLabel::kNone); |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 106 | |
| 107 | // Perform sanity checks, as the extension might have issued this call |
| 108 | // incorrectly. |
| 109 | if (!active_dialog_state_ || |
| 110 | active_dialog_state_->extension_id != extension_id) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 111 | return StopPinRequestResult::kNoActiveDialog; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 112 | } |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 113 | if (active_dialog_state_->request_pin_callback || |
| 114 | active_dialog_state_->stop_pin_request_callback) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 115 | return StopPinRequestResult::kNoUserInput; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 116 | } |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 117 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 118 | const SignRequestState* const sign_request_state = |
| 119 | FindSignRequestState(extension_id, active_dialog_state_->sign_request_id); |
| 120 | if (!sign_request_state) |
| 121 | return StopPinRequestResult::kNoActiveDialog; |
| 122 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 123 | active_dialog_state_->stop_pin_request_callback = std::move(callback); |
| 124 | active_dialog_state_->host->ShowSecurityTokenPinDialog( |
| 125 | active_dialog_state_->extension_name, active_dialog_state_->code_type, |
| 126 | /*enable_user_input=*/false, error_label, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 127 | /*attempts_left=*/-1, sign_request_state->authenticating_user_account_id, |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 128 | base::BindOnce(&PinDialogManager::OnPinEntered, |
| 129 | weak_factory_.GetWeakPtr()), |
| 130 | base::BindOnce(&PinDialogManager::OnPinDialogClosed, |
| 131 | weak_factory_.GetWeakPtr())); |
| 132 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 133 | return StopPinRequestResult::kSuccess; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 134 | } |
| 135 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 136 | bool PinDialogManager::LastPinDialogClosed( |
| 137 | const std::string& extension_id) const { |
| 138 | auto iter = last_response_closed_.find(extension_id); |
| 139 | return iter != last_response_closed_.end() && iter->second; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | bool PinDialogManager::CloseDialog(const std::string& extension_id) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 143 | // Perform sanity checks, as the extension might have issued this call |
| 144 | // incorrectly. |
| 145 | if (!active_dialog_state_ || |
| 146 | extension_id != active_dialog_state_->extension_id) { |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 147 | LOG(ERROR) << "StopPinRequest called by unexpected extension: " |
| 148 | << extension_id; |
| 149 | return false; |
| 150 | } |
| 151 | |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 152 | CloseActiveDialog(); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
| 156 | void PinDialogManager::ExtensionUnloaded(const std::string& extension_id) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 157 | if (active_dialog_state_ && |
| 158 | active_dialog_state_->extension_id == extension_id) { |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 159 | CloseActiveDialog(); |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 160 | } |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 161 | |
| 162 | last_response_closed_[extension_id] = false; |
| 163 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 164 | for (auto it = sign_requests_.cbegin(); it != sign_requests_.cend();) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 165 | if (it->first.first == extension_id) |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 166 | sign_requests_.erase(it++); |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 167 | else |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 168 | ++it; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 172 | void PinDialogManager::AddPinDialogHost( |
| 173 | SecurityTokenPinDialogHost* pin_dialog_host) { |
| 174 | DCHECK(!base::Contains(added_dialog_hosts_, pin_dialog_host)); |
| 175 | added_dialog_hosts_.push_back(pin_dialog_host); |
| 176 | } |
| 177 | |
| 178 | void PinDialogManager::RemovePinDialogHost( |
| 179 | SecurityTokenPinDialogHost* pin_dialog_host) { |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 180 | if (active_dialog_state_ && active_dialog_state_->host == pin_dialog_host) |
| 181 | CloseActiveDialog(); |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 182 | DCHECK(base::Contains(added_dialog_hosts_, pin_dialog_host)); |
| 183 | base::Erase(added_dialog_hosts_, pin_dialog_host); |
| 184 | } |
| 185 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 186 | PinDialogManager::SignRequestState::SignRequestState( |
| 187 | base::Time begin_time, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame^] | 188 | const std::optional<AccountId>& authenticating_user_account_id) |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 189 | : begin_time(begin_time), |
| 190 | authenticating_user_account_id(authenticating_user_account_id) {} |
| 191 | |
| 192 | PinDialogManager::SignRequestState::SignRequestState(const SignRequestState&) = |
| 193 | default; |
Oleksandr Kulkov | 9c7cf955 | 2022-06-02 12:25:43 | [diff] [blame] | 194 | PinDialogManager::SignRequestState& |
| 195 | PinDialogManager::SignRequestState::operator=(const SignRequestState&) = |
| 196 | default; |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 197 | |
| 198 | PinDialogManager::SignRequestState::~SignRequestState() = default; |
| 199 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 200 | PinDialogManager::ActiveDialogState::ActiveDialogState( |
| 201 | SecurityTokenPinDialogHost* host, |
| 202 | const std::string& extension_id, |
| 203 | const std::string& extension_name, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 204 | int sign_request_id, |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame] | 205 | security_token_pin::CodeType code_type) |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 206 | : host(host), |
| 207 | extension_id(extension_id), |
| 208 | extension_name(extension_name), |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 209 | sign_request_id(sign_request_id), |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 210 | code_type(code_type) {} |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 211 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 212 | PinDialogManager::ActiveDialogState::~ActiveDialogState() = default; |
| 213 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 214 | PinDialogManager::SignRequestState* PinDialogManager::FindSignRequestState( |
| 215 | const std::string& extension_id, |
| 216 | int sign_request_id) { |
| 217 | const ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
| 218 | const auto sign_request_iter = sign_requests_.find(key); |
| 219 | if (sign_request_iter == sign_requests_.end()) |
| 220 | return nullptr; |
| 221 | return &sign_request_iter->second; |
| 222 | } |
| 223 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 224 | void PinDialogManager::OnPinEntered(const std::string& user_input) { |
| 225 | DCHECK(!active_dialog_state_->stop_pin_request_callback); |
| 226 | last_response_closed_[active_dialog_state_->extension_id] = false; |
| 227 | if (active_dialog_state_->request_pin_callback) |
| 228 | std::move(active_dialog_state_->request_pin_callback).Run(user_input); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void PinDialogManager::OnPinDialogClosed() { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 232 | DCHECK(!active_dialog_state_->request_pin_callback || |
| 233 | !active_dialog_state_->stop_pin_request_callback); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 234 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 235 | last_response_closed_[active_dialog_state_->extension_id] = true; |
| 236 | if (active_dialog_state_->request_pin_callback) { |
| 237 | std::move(active_dialog_state_->request_pin_callback) |
| 238 | .Run(/*user_input=*/std::string()); |
| 239 | } |
| 240 | if (active_dialog_state_->stop_pin_request_callback) |
| 241 | std::move(active_dialog_state_->stop_pin_request_callback).Run(); |
| 242 | active_dialog_state_.reset(); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 243 | } |
| 244 | |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 245 | SecurityTokenPinDialogHost* PinDialogManager::GetHostForNewDialog() { |
| 246 | if (added_dialog_hosts_.empty()) |
| 247 | return &default_dialog_host_; |
| 248 | return added_dialog_hosts_.back(); |
| 249 | } |
| 250 | |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 251 | void PinDialogManager::CloseActiveDialog() { |
| 252 | if (!active_dialog_state_) |
| 253 | return; |
| 254 | |
| 255 | // Ignore any further callbacks from the host. Instead of relying on the host |
| 256 | // to call the closing callback, run OnPinDialogClosed() below explicitly. |
| 257 | weak_factory_.InvalidateWeakPtrs(); |
| 258 | |
| 259 | active_dialog_state_->host->CloseSecurityTokenPinDialog(); |
| 260 | OnPinDialogClosed(); |
| 261 | DCHECK(!active_dialog_state_); |
| 262 | } |
| 263 | |
Oleksandr Kulkov | 1213dee8 | 2022-05-17 20:45:34 | [diff] [blame] | 264 | } // namespace chromeos |