OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/password_manager/password_store_mac.h" | 5 #include "chrome/browser/password_manager/password_store_mac.h" |
6 #include "chrome/browser/password_manager/password_store_mac_internal.h" | 6 #include "chrome/browser/password_manager/password_store_mac_internal.h" |
7 | 7 |
8 #include <CoreServices/CoreServices.h> | 8 #include <CoreServices/CoreServices.h> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/mac/mac_logging.h" | 16 #include "base/mac/mac_logging.h" |
16 #include "base/mac/mac_util.h" | 17 #include "base/mac/mac_util.h" |
17 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
18 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 switch (auth_type) { | 227 switch (auth_type) { |
227 case kSecAuthenticationTypeHTMLForm: return PasswordForm::SCHEME_HTML; | 228 case kSecAuthenticationTypeHTMLForm: return PasswordForm::SCHEME_HTML; |
228 case kSecAuthenticationTypeHTTPBasic: return PasswordForm::SCHEME_BASIC; | 229 case kSecAuthenticationTypeHTTPBasic: return PasswordForm::SCHEME_BASIC; |
229 case kSecAuthenticationTypeHTTPDigest: return PasswordForm::SCHEME_DIGEST; | 230 case kSecAuthenticationTypeHTTPDigest: return PasswordForm::SCHEME_DIGEST; |
230 default: return PasswordForm::SCHEME_OTHER; | 231 default: return PasswordForm::SCHEME_OTHER; |
231 } | 232 } |
232 } | 233 } |
233 | 234 |
234 bool FillPasswordFormFromKeychainItem(const AppleKeychain& keychain, | 235 bool FillPasswordFormFromKeychainItem(const AppleKeychain& keychain, |
235 const SecKeychainItemRef& keychain_item, | 236 const SecKeychainItemRef& keychain_item, |
236 PasswordForm* form) { | 237 PasswordForm* form, |
238 bool extract_password_data) { | |
237 DCHECK(form); | 239 DCHECK(form); |
238 | 240 |
239 SecKeychainAttributeInfo attrInfo; | 241 SecKeychainAttributeInfo attrInfo; |
240 UInt32 tags[] = { kSecAccountItemAttr, | 242 UInt32 tags[] = { kSecAccountItemAttr, |
241 kSecServerItemAttr, | 243 kSecServerItemAttr, |
242 kSecPortItemAttr, | 244 kSecPortItemAttr, |
243 kSecPathItemAttr, | 245 kSecPathItemAttr, |
244 kSecProtocolItemAttr, | 246 kSecProtocolItemAttr, |
245 kSecAuthenticationTypeItemAttr, | 247 kSecAuthenticationTypeItemAttr, |
246 kSecSecurityDomainItemAttr, | 248 kSecSecurityDomainItemAttr, |
247 kSecCreationDateItemAttr, | 249 kSecCreationDateItemAttr, |
248 kSecNegativeItemAttr }; | 250 kSecNegativeItemAttr }; |
249 attrInfo.count = arraysize(tags); | 251 attrInfo.count = arraysize(tags); |
250 attrInfo.tag = tags; | 252 attrInfo.tag = tags; |
251 attrInfo.format = NULL; | 253 attrInfo.format = NULL; |
252 | 254 |
253 SecKeychainAttributeList *attrList; | 255 SecKeychainAttributeList *attrList; |
254 UInt32 password_length; | 256 UInt32 password_length; |
255 void* password_data; | 257 |
258 // If |extract_password_data| is false, do not pass in a reference to | |
259 // |password_data|. ItemCopyAttributesAndData will then extract only the | |
260 // attributes of |keychain_item| (doesn't require OS authorization), and not | |
261 // attempt to extract its password data (requires OS authorization). | |
262 void* password_data = NULL; | |
263 void** password_data_ref = extract_password_data ? &password_data : NULL; | |
264 | |
256 OSStatus result = keychain.ItemCopyAttributesAndData(keychain_item, &attrInfo, | 265 OSStatus result = keychain.ItemCopyAttributesAndData(keychain_item, &attrInfo, |
257 NULL, &attrList, | 266 NULL, &attrList, |
258 &password_length, | 267 &password_length, |
259 &password_data); | 268 password_data_ref); |
260 | 269 |
261 if (result != noErr) { | 270 if (result != noErr) { |
262 // We don't log errSecAuthFailed because that just means that the user | 271 // We don't log errSecAuthFailed because that just means that the user |
263 // chose not to allow us access to the item. | 272 // chose not to allow us access to the item. |
264 if (result != errSecAuthFailed) { | 273 if (result != errSecAuthFailed) { |
265 OSSTATUS_LOG(ERROR, result) << "Keychain data load failed"; | 274 OSSTATUS_LOG(ERROR, result) << "Keychain data load failed"; |
266 } | 275 } |
267 return false; | 276 return false; |
268 } | 277 } |
269 | 278 |
270 UTF8ToUTF16(static_cast<const char *>(password_data), password_length, | 279 if (extract_password_data) { |
271 &(form->password_value)); | 280 UTF8ToUTF16(static_cast<const char *>(password_data), password_length, |
281 &(form->password_value)); | |
282 } | |
272 | 283 |
273 int port = kAnyPort; | 284 int port = kAnyPort; |
274 std::string server; | 285 std::string server; |
275 std::string security_domain; | 286 std::string security_domain; |
276 std::string path; | 287 std::string path; |
277 for (unsigned int i = 0; i < attrList->count; i++) { | 288 for (unsigned int i = 0; i < attrList->count; i++) { |
278 SecKeychainAttribute attr = attrList->attr[i]; | 289 SecKeychainAttribute attr = attrList->attr[i]; |
279 if (!attr.data) { | 290 if (!attr.data) { |
280 continue; | 291 continue; |
281 } | 292 } |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
439 database_blacklist_forms.begin(), | 450 database_blacklist_forms.begin(), |
440 database_blacklist_forms.end()); | 451 database_blacklist_forms.end()); |
441 | 452 |
442 // Clear out all the Keychain entries we used. | 453 // Clear out all the Keychain entries we used. |
443 DeleteVectorElementsInSet(keychain_forms, used_keychain_forms); | 454 DeleteVectorElementsInSet(keychain_forms, used_keychain_forms); |
444 } | 455 } |
445 | 456 |
446 std::vector<PasswordForm*> GetPasswordsForForms( | 457 std::vector<PasswordForm*> GetPasswordsForForms( |
447 const AppleKeychain& keychain, | 458 const AppleKeychain& keychain, |
448 std::vector<PasswordForm*>* database_forms) { | 459 std::vector<PasswordForm*>* database_forms) { |
460 // We first load the attributes of all items in the keychain without loading | |
461 // their password data, and then match items in |database_forms| against them. | |
462 // This allows us to avoid individually searching through the keychain for | |
463 // passwords matching each form in |database_forms|, and results in a | |
464 // significant performance gain, since we are replacing O(N) keychain search | |
465 // operations with a single operation that loads all keychain items, and then | |
466 // selective reads of only the passwords we're interested in. | |
stuartmorgan
2013/09/18 19:23:32
The we/us feels like noise here; would read better
Raghu Simha
2013/09/19 21:23:23
Done.
| |
467 // See crbug.com/263685. | |
468 | |
469 // First load references to all items in the system keychain, and copy all | |
470 // their attributes into a container of pairs of pointers to PasswordForms and | |
471 // SecKeychainItemRefs without copying any password data. | |
472 // This operation does not require OS authorization. | |
449 MacKeychainPasswordFormAdapter keychain_adapter(&keychain); | 473 MacKeychainPasswordFormAdapter keychain_adapter(&keychain); |
474 std::vector<SecKeychainItemRef> keychain_items = | |
475 keychain_adapter.GetAllPasswordFormKeychainItems(); | |
476 std::vector<std::pair<SecKeychainItemRef*, PasswordForm*> > item_form_pairs; | |
477 for (std::vector<SecKeychainItemRef>::iterator i = keychain_items.begin(); | |
478 i != keychain_items.end(); ++i) { | |
479 PasswordForm* form_without_password = new PasswordForm(); | |
480 internal_keychain_helpers::FillPasswordFormFromKeychainItem( | |
481 keychain, | |
482 *i, | |
483 form_without_password, | |
484 false); // Load password attributes, but not password data. | |
485 item_form_pairs.push_back(std::make_pair(&(*i), form_without_password)); | |
486 } | |
stuartmorgan
2013/09/18 19:23:32
Please extract this into a helper method; this add
Raghu Simha
2013/09/19 21:23:23
Done.
| |
450 | 487 |
488 // Next, compare the attributes of the PasswordForms in |database_forms| | |
489 // against those in |item_form_pairs|, and extract password data for each | |
490 // matching PasswordForm using its corresponding SecKeychainItemRef. | |
451 std::vector<PasswordForm*> merged_forms; | 491 std::vector<PasswordForm*> merged_forms; |
452 for (std::vector<PasswordForm*>::iterator i = database_forms->begin(); | 492 for (std::vector<PasswordForm*>::iterator i = database_forms->begin(); |
453 i != database_forms->end();) { | 493 i != database_forms->end();) { |
454 std::vector<PasswordForm*> db_form_container(1, *i); | 494 std::vector<PasswordForm*> db_form_container(1, *i); |
455 std::vector<PasswordForm*> keychain_matches = | 495 std::vector<PasswordForm*> keychain_matches; |
456 keychain_adapter.PasswordsMergeableWithForm(**i); | 496 for (std::vector<std::pair<SecKeychainItemRef*, PasswordForm*> >::iterator j |
497 = item_form_pairs.begin(); j != item_form_pairs.end(); ++j) { | |
498 if ((*i)->username_value == j->second->username_value && | |
499 (*i)->scheme == j->second->scheme && | |
500 GURL((*i)->signon_realm) == GURL(j->second->signon_realm)) { | |
stuartmorgan
2013/09/18 19:23:32
Isn't this just FormsMatchForMerge? Except that it
Raghu Simha
2013/09/19 21:23:23
I was getting tripped up by the fact that when Fil
| |
501 // Create a new object, since the caller is responsible for deleting the | |
502 // returned forms. | |
503 PasswordForm* form_with_password = new PasswordForm(); | |
504 internal_keychain_helpers::FillPasswordFormFromKeychainItem( | |
505 keychain, | |
506 *(j->first), | |
507 form_with_password, | |
508 true); // Load password attributes and data. | |
509 keychain_matches.push_back(form_with_password); | |
stuartmorgan
2013/09/18 19:23:32
What happened to all the logic in MatchingKeychain
Raghu Simha
2013/09/19 21:23:23
My earlier reasoning was as follows:
- Since serve
| |
510 } | |
511 } | |
stuartmorgan
2013/09/18 19:23:32
Again, please extract a helper method.
Raghu Simha
2013/09/19 21:23:23
Done.
| |
457 MergePasswordForms(&keychain_matches, &db_form_container, &merged_forms); | 512 MergePasswordForms(&keychain_matches, &db_form_container, &merged_forms); |
458 if (db_form_container.empty()) { | 513 if (db_form_container.empty()) { |
459 i = database_forms->erase(i); | 514 i = database_forms->erase(i); |
460 } else { | 515 } else { |
461 ++i; | 516 ++i; |
462 } | 517 } |
463 STLDeleteElements(&keychain_matches); | 518 STLDeleteElements(&keychain_matches); |
464 } | 519 } |
520 | |
521 // Clean up temporary PasswordForms and SecKeychainItemRefs. | |
522 STLDeleteContainerPairSecondPointers(item_form_pairs.begin(), | |
523 item_form_pairs.end()); | |
524 for (std::vector<SecKeychainItemRef>::iterator i = keychain_items.begin(); | |
525 i != keychain_items.end(); ++i) { | |
526 keychain.Free(*i); | |
527 } | |
465 return merged_forms; | 528 return merged_forms; |
466 } | 529 } |
467 | 530 |
468 } // namespace internal_keychain_helpers | 531 } // namespace internal_keychain_helpers |
469 | 532 |
470 #pragma mark - | 533 #pragma mark - |
471 | 534 |
472 MacKeychainPasswordFormAdapter::MacKeychainPasswordFormAdapter( | 535 MacKeychainPasswordFormAdapter::MacKeychainPasswordFormAdapter( |
473 const AppleKeychain* keychain) | 536 const AppleKeychain* keychain) |
474 : keychain_(keychain), finds_only_owned_(false) { | 537 : keychain_(keychain), finds_only_owned_(false) { |
475 } | 538 } |
476 | 539 |
477 std::vector<PasswordForm*> | 540 std::vector<PasswordForm*> |
478 MacKeychainPasswordFormAdapter::PasswordsFillingForm( | 541 MacKeychainPasswordFormAdapter::PasswordsFillingForm( |
479 const PasswordForm& query_form) { | 542 const PasswordForm& query_form) { |
480 std::vector<SecKeychainItemRef> keychain_items = | 543 std::vector<SecKeychainItemRef> keychain_items = |
481 MatchingKeychainItems(query_form.signon_realm, query_form.scheme, | 544 MatchingKeychainItems(query_form.signon_realm, query_form.scheme, |
482 NULL, NULL); | 545 NULL, NULL); |
483 | 546 |
484 return ConvertKeychainItemsToForms(&keychain_items); | 547 return ConvertKeychainItemsToForms(&keychain_items); |
485 } | 548 } |
486 | 549 |
487 std::vector<PasswordForm*> | 550 std::vector<PasswordForm*> |
488 MacKeychainPasswordFormAdapter::PasswordsMergeableWithForm( | 551 MacKeychainPasswordFormAdapter::PasswordsMergeableWithForm( |
stuartmorgan
2013/09/18 19:23:32
Looks like this is dead code now.
Raghu Simha
2013/09/19 21:23:23
Removed and replaced with ExtractPasswordsMergeabl
| |
489 const PasswordForm& query_form) { | 552 const PasswordForm& query_form) { |
490 std::string username = UTF16ToUTF8(query_form.username_value); | 553 std::string username = UTF16ToUTF8(query_form.username_value); |
491 std::vector<SecKeychainItemRef> keychain_items = | 554 std::vector<SecKeychainItemRef> keychain_items = |
492 MatchingKeychainItems(query_form.signon_realm, query_form.scheme, | 555 MatchingKeychainItems(query_form.signon_realm, query_form.scheme, |
493 NULL, username.c_str()); | 556 NULL, username.c_str()); |
494 | 557 |
495 return ConvertKeychainItemsToForms(&keychain_items); | 558 return ConvertKeychainItemsToForms(&keychain_items); |
496 } | 559 } |
497 | 560 |
498 PasswordForm* MacKeychainPasswordFormAdapter::PasswordExactlyMatchingForm( | 561 PasswordForm* MacKeychainPasswordFormAdapter::PasswordExactlyMatchingForm( |
499 const PasswordForm& query_form) { | 562 const PasswordForm& query_form) { |
500 SecKeychainItemRef keychain_item = KeychainItemForForm(query_form); | 563 SecKeychainItemRef keychain_item = KeychainItemForForm(query_form); |
501 if (keychain_item) { | 564 if (keychain_item) { |
502 PasswordForm* form = new PasswordForm(); | 565 PasswordForm* form = new PasswordForm(); |
503 internal_keychain_helpers::FillPasswordFormFromKeychainItem(*keychain_, | 566 internal_keychain_helpers::FillPasswordFormFromKeychainItem(*keychain_, |
504 keychain_item, | 567 keychain_item, |
505 form); | 568 form, |
569 true); | |
506 keychain_->Free(keychain_item); | 570 keychain_->Free(keychain_item); |
507 return form; | 571 return form; |
508 } | 572 } |
509 return NULL; | 573 return NULL; |
510 } | 574 } |
511 | 575 |
512 bool MacKeychainPasswordFormAdapter::HasPasswordsMergeableWithForm( | 576 bool MacKeychainPasswordFormAdapter::HasPasswordsMergeableWithForm( |
513 const PasswordForm& query_form) { | 577 const PasswordForm& query_form) { |
514 std::string username = UTF16ToUTF8(query_form.username_value); | 578 std::string username = UTF16ToUTF8(query_form.username_value); |
515 std::vector<SecKeychainItemRef> matches = | 579 std::vector<SecKeychainItemRef> matches = |
516 MatchingKeychainItems(query_form.signon_realm, query_form.scheme, | 580 MatchingKeychainItems(query_form.signon_realm, query_form.scheme, |
517 NULL, username.c_str()); | 581 NULL, username.c_str()); |
518 for (std::vector<SecKeychainItemRef>::iterator i = matches.begin(); | 582 for (std::vector<SecKeychainItemRef>::iterator i = matches.begin(); |
519 i != matches.end(); ++i) { | 583 i != matches.end(); ++i) { |
520 keychain_->Free(*i); | 584 keychain_->Free(*i); |
521 } | 585 } |
522 | 586 |
523 return !matches.empty(); | 587 return !matches.empty(); |
524 } | 588 } |
525 | 589 |
526 std::vector<PasswordForm*> | 590 std::vector<SecKeychainItemRef> |
527 MacKeychainPasswordFormAdapter::GetAllPasswordFormPasswords() { | 591 MacKeychainPasswordFormAdapter::GetAllPasswordFormKeychainItems() { |
528 SecAuthenticationType supported_auth_types[] = { | 592 SecAuthenticationType supported_auth_types[] = { |
529 kSecAuthenticationTypeHTMLForm, | 593 kSecAuthenticationTypeHTMLForm, |
530 kSecAuthenticationTypeHTTPBasic, | 594 kSecAuthenticationTypeHTTPBasic, |
531 kSecAuthenticationTypeHTTPDigest, | 595 kSecAuthenticationTypeHTTPDigest, |
532 }; | 596 }; |
533 | 597 |
534 std::vector<SecKeychainItemRef> matches; | 598 std::vector<SecKeychainItemRef> matches; |
535 for (unsigned int i = 0; i < arraysize(supported_auth_types); ++i) { | 599 for (unsigned int i = 0; i < arraysize(supported_auth_types); ++i) { |
536 KeychainSearch keychain_search(*keychain_); | 600 KeychainSearch keychain_search(*keychain_); |
537 keychain_search.Init(NULL, 0, kSecProtocolTypeAny, supported_auth_types[i], | 601 keychain_search.Init(NULL, 0, kSecProtocolTypeAny, supported_auth_types[i], |
538 NULL, NULL, NULL, CreatorCodeForSearch()); | 602 NULL, NULL, NULL, CreatorCodeForSearch()); |
539 keychain_search.FindMatchingItems(&matches); | 603 keychain_search.FindMatchingItems(&matches); |
540 } | 604 } |
605 return matches; | |
606 } | |
541 | 607 |
542 return ConvertKeychainItemsToForms(&matches); | 608 std::vector<PasswordForm*> |
609 MacKeychainPasswordFormAdapter::GetAllPasswordFormPasswords() { | |
610 std::vector<SecKeychainItemRef> items = GetAllPasswordFormKeychainItems(); | |
611 return ConvertKeychainItemsToForms(&items); | |
543 } | 612 } |
544 | 613 |
545 bool MacKeychainPasswordFormAdapter::AddPassword(const PasswordForm& form) { | 614 bool MacKeychainPasswordFormAdapter::AddPassword(const PasswordForm& form) { |
546 // We should never be trying to store a blacklist in the keychain. | 615 // We should never be trying to store a blacklist in the keychain. |
547 DCHECK(!form.blacklisted_by_user); | 616 DCHECK(!form.blacklisted_by_user); |
548 | 617 |
549 std::string server; | 618 std::string server; |
550 std::string security_domain; | 619 std::string security_domain; |
551 int port; | 620 int port; |
552 bool is_secure; | 621 bool is_secure; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 finds_only_owned_ = finds_only_owned; | 669 finds_only_owned_ = finds_only_owned; |
601 } | 670 } |
602 | 671 |
603 std::vector<PasswordForm*> | 672 std::vector<PasswordForm*> |
604 MacKeychainPasswordFormAdapter::ConvertKeychainItemsToForms( | 673 MacKeychainPasswordFormAdapter::ConvertKeychainItemsToForms( |
605 std::vector<SecKeychainItemRef>* items) { | 674 std::vector<SecKeychainItemRef>* items) { |
606 std::vector<PasswordForm*> keychain_forms; | 675 std::vector<PasswordForm*> keychain_forms; |
607 for (std::vector<SecKeychainItemRef>::const_iterator i = items->begin(); | 676 for (std::vector<SecKeychainItemRef>::const_iterator i = items->begin(); |
608 i != items->end(); ++i) { | 677 i != items->end(); ++i) { |
609 PasswordForm* form = new PasswordForm(); | 678 PasswordForm* form = new PasswordForm(); |
610 if (internal_keychain_helpers::FillPasswordFormFromKeychainItem(*keychain_, | 679 if (internal_keychain_helpers::FillPasswordFormFromKeychainItem( |
611 *i, form)) { | 680 *keychain_, *i, form, true)) { |
612 keychain_forms.push_back(form); | 681 keychain_forms.push_back(form); |
613 } | 682 } |
614 keychain_->Free(*i); | 683 keychain_->Free(*i); |
615 } | 684 } |
616 items->clear(); | 685 items->clear(); |
617 return keychain_forms; | 686 return keychain_forms; |
618 } | 687 } |
619 | 688 |
620 SecKeychainItemRef MacKeychainPasswordFormAdapter::KeychainItemForForm( | 689 SecKeychainItemRef MacKeychainPasswordFormAdapter::KeychainItemForForm( |
621 const PasswordForm& form) { | 690 const PasswordForm& form) { |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1018 owned_keychain_adapter.SetFindsOnlyOwnedItems(true); | 1087 owned_keychain_adapter.SetFindsOnlyOwnedItems(true); |
1019 for (std::vector<PasswordForm*>::const_iterator i = forms.begin(); | 1088 for (std::vector<PasswordForm*>::const_iterator i = forms.begin(); |
1020 i != forms.end(); ++i) { | 1089 i != forms.end(); ++i) { |
1021 owned_keychain_adapter.RemovePassword(**i); | 1090 owned_keychain_adapter.RemovePassword(**i); |
1022 } | 1091 } |
1023 } | 1092 } |
1024 | 1093 |
1025 void PasswordStoreMac::CreateNotificationService() { | 1094 void PasswordStoreMac::CreateNotificationService() { |
1026 notification_service_.reset(content::NotificationService::Create()); | 1095 notification_service_.reset(content::NotificationService::Create()); |
1027 } | 1096 } |
OLD | NEW |