Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(766)

Side by Side Diff: chrome/browser/importer/firefox_importer_utils.cc

Issue 159165: Split NSSDecryptor out into platform specific files.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/importer/firefox_importer_utils.h" 5 #include "chrome/browser/importer/firefox_importer_utils.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/sys_string_conversions.h"
13 #include "base/values.h" 12 #include "base/values.h"
14 #include "chrome/browser/search_engines/template_url.h" 13 #include "chrome/browser/search_engines/template_url.h"
15 #include "chrome/browser/search_engines/template_url_model.h" 14 #include "chrome/browser/search_engines/template_url_model.h"
16 #include "chrome/browser/search_engines/template_url_parser.h" 15 #include "chrome/browser/search_engines/template_url_parser.h"
17 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
18 #include "net/base/base64.h"
19 #include "webkit/glue/password_form.h"
20
21 using webkit_glue::PasswordForm;
22 17
23 namespace { 18 namespace {
24 19
25 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from 20 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from
26 // the search URL when importing search engines. 21 // the search URL when importing search engines.
27 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter { 22 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter {
28 public: 23 public:
29 FirefoxURLParameterFilter() { } 24 FirefoxURLParameterFilter() { }
30 ~FirefoxURLParameterFilter() { } 25 ~FirefoxURLParameterFilter() { }
31 26
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 std::vector<std::string> urls; 323 std::vector<std::string> urls;
329 SplitString(default_homepages, '|', &urls); 324 SplitString(default_homepages, '|', &urls);
330 325
331 for (size_t i = 0; i < urls.size(); ++i) { 326 for (size_t i = 0; i < urls.size(); ++i) {
332 if (homepage.spec() == GURL(urls[i]).spec()) 327 if (homepage.spec() == GURL(urls[i]).spec())
333 return true; 328 return true;
334 } 329 }
335 330
336 return false; 331 return false;
337 } 332 }
338
339 // class NSSDecryptor.
340
341 NSSDecryptor::NSSDecryptor()
342 : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL),
343 PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL),
344 PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL),
345 PL_ArenaFinish(NULL), PR_Cleanup(NULL),
346 nss3_dll_(NULL), softokn3_dll_(NULL),
347 is_nss_initialized_(false) {
348 }
349
350 NSSDecryptor::~NSSDecryptor() {
351 Free();
352 }
353
354 bool NSSDecryptor::InitNSS(const std::wstring& db_path,
355 base::NativeLibrary plds4_dll,
356 base::NativeLibrary nspr4_dll) {
357 // NSPR DLLs are already loaded now.
358 if (plds4_dll == NULL || nspr4_dll == NULL) {
359 Free();
360 return false;
361 }
362
363 // Gets the function address.
364 NSS_Init = (NSSInitFunc)
365 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Init");
366 NSS_Shutdown = (NSSShutdownFunc)
367 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Shutdown");
368 PK11_GetInternalKeySlot = (PK11GetInternalKeySlotFunc)
369 base::GetFunctionPointerFromNativeLibrary(nss3_dll_,
370 "PK11_GetInternalKeySlot");
371 PK11_FreeSlot = (PK11FreeSlotFunc)
372 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_FreeSlot");
373 PK11_Authenticate = (PK11AuthenticateFunc)
374 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_Authenticate");
375 PK11SDR_Decrypt = (PK11SDRDecryptFunc)
376 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11SDR_Decrypt");
377 SECITEM_FreeItem = (SECITEMFreeItemFunc)
378 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "SECITEM_FreeItem");
379 PL_ArenaFinish = (PLArenaFinishFunc)
380 base::GetFunctionPointerFromNativeLibrary(plds4_dll, "PL_ArenaFinish");
381 PR_Cleanup = (PRCleanupFunc)
382 base::GetFunctionPointerFromNativeLibrary(nspr4_dll, "PR_Cleanup");
383
384 if (NSS_Init == NULL || NSS_Shutdown == NULL ||
385 PK11_GetInternalKeySlot == NULL || PK11_FreeSlot == NULL ||
386 PK11_Authenticate == NULL || PK11SDR_Decrypt == NULL ||
387 SECITEM_FreeItem == NULL || PL_ArenaFinish == NULL ||
388 PR_Cleanup == NULL) {
389 Free();
390 return false;
391 }
392
393 SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str());
394 if (result != SECSuccess) {
395 Free();
396 return false;
397 }
398
399 is_nss_initialized_ = true;
400 return true;
401 }
402
403 void NSSDecryptor::Free() {
404 if (is_nss_initialized_) {
405 NSS_Shutdown();
406 PL_ArenaFinish();
407 PR_Cleanup();
408 is_nss_initialized_ = false;
409 }
410 if (softokn3_dll_ != NULL)
411 base::UnloadNativeLibrary(softokn3_dll_);
412 if (nss3_dll_ != NULL)
413 base::UnloadNativeLibrary(nss3_dll_);
414 NSS_Init = NULL;
415 NSS_Shutdown = NULL;
416 PK11_GetInternalKeySlot = NULL;
417 PK11_FreeSlot = NULL;
418 PK11_Authenticate = NULL;
419 PK11SDR_Decrypt = NULL;
420 SECITEM_FreeItem = NULL;
421 PL_ArenaFinish = NULL;
422 PR_Cleanup = NULL;
423 nss3_dll_ = NULL;
424 softokn3_dll_ = NULL;
425 }
426
427 // This method is based on some Firefox code in
428 // security/manager/ssl/src/nsSDR.cpp
429 // The license block is:
430
431 /* ***** BEGIN LICENSE BLOCK *****
432 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
433 *
434 * The contents of this file are subject to the Mozilla Public License Version
435 * 1.1 (the "License"); you may not use this file except in compliance with
436 * the License. You may obtain a copy of the License at
437 * http://www.mozilla.org/MPL/
438 *
439 * Software distributed under the License is distributed on an "AS IS" basis,
440 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
441 * for the specific language governing rights and limitations under the
442 * License.
443 *
444 * The Original Code is the Netscape security libraries.
445 *
446 * The Initial Developer of the Original Code is
447 * Netscape Communications Corporation.
448 * Portions created by the Initial Developer are Copyright (C) 1994-2000
449 * the Initial Developer. All Rights Reserved.
450 *
451 * Contributor(s):
452 *
453 * Alternatively, the contents of this file may be used under the terms of
454 * either the GNU General Public License Version 2 or later (the "GPL"), or
455 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
456 * in which case the provisions of the GPL or the LGPL are applicable instead
457 * of those above. If you wish to allow use of your version of this file only
458 * under the terms of either the GPL or the LGPL, and not to allow others to
459 * use your version of this file under the terms of the MPL, indicate your
460 * decision by deleting the provisions above and replace them with the notice
461 * and other provisions required by the GPL or the LGPL. If you do not delete
462 * the provisions above, a recipient may use your version of this file under
463 * the terms of any one of the MPL, the GPL or the LGPL.
464 *
465 * ***** END LICENSE BLOCK ***** */
466
467 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const {
468 // Do nothing if NSS is not loaded.
469 if (!nss3_dll_)
470 return std::wstring();
471
472 // The old style password is encoded in base64. They are identified
473 // by a leading '~'. Otherwise, we should decrypt the text.
474 std::string plain;
475 if (crypt[0] != '~') {
476 std::string decoded_data;
477 net::Base64Decode(crypt, &decoded_data);
478 PK11SlotInfo* slot = NULL;
479 slot = PK11_GetInternalKeySlot();
480 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL);
481 if (result != SECSuccess) {
482 PK11_FreeSlot(slot);
483 return std::wstring();
484 }
485
486 SECItem request;
487 request.data = reinterpret_cast<unsigned char*>(
488 const_cast<char*>(decoded_data.data()));
489 request.len = static_cast<unsigned int>(decoded_data.size());
490 SECItem reply;
491 reply.data = NULL;
492 reply.len = 0;
493 result = PK11SDR_Decrypt(&request, &reply, NULL);
494 if (result == SECSuccess)
495 plain.assign(reinterpret_cast<char*>(reply.data), reply.len);
496
497 SECITEM_FreeItem(&reply, PR_FALSE);
498 PK11_FreeSlot(slot);
499 } else {
500 // Deletes the leading '~' before decoding.
501 net::Base64Decode(crypt.substr(1), &plain);
502 }
503
504 return UTF8ToWide(plain);
505 }
506
507 // There are three versions of password filess. They store saved user
508 // names and passwords.
509 // References:
510 // http://kb.mozillazine.org/Signons.txt
511 // http://kb.mozillazine.org/Signons2.txt
512 // http://kb.mozillazine.org/Signons3.txt
513 void NSSDecryptor::ParseSignons(const std::string& content,
514 std::vector<PasswordForm>* forms) {
515 forms->clear();
516
517 // Splits the file content into lines.
518 std::vector<std::string> lines;
519 SplitString(content, '\n', &lines);
520
521 // The first line is the file version. We skip the unknown versions.
522 if (lines.empty())
523 return;
524 int version;
525 if (lines[0] == "#2c")
526 version = 1;
527 else if (lines[0] == "#2d")
528 version = 2;
529 else if (lines[0] == "#2e")
530 version = 3;
531 else
532 return;
533
534 GURL::Replacements rep;
535 rep.ClearQuery();
536 rep.ClearRef();
537 rep.ClearUsername();
538 rep.ClearPassword();
539
540 // Reads never-saved list. Domains are stored one per line.
541 size_t i;
542 for (i = 1; i < lines.size() && lines[i].compare(".") != 0; ++i) {
543 PasswordForm form;
544 form.origin = GURL(lines[i]).ReplaceComponents(rep);
545 form.signon_realm = form.origin.GetOrigin().spec();
546 form.blacklisted_by_user = true;
547 forms->push_back(form);
548 }
549 ++i;
550
551 // Reads saved passwords. The information is stored in blocks
552 // seperated by lines that only contain a dot. We find a block
553 // by the seperator and parse them one by one.
554 while (i < lines.size()) {
555 size_t begin = i;
556 size_t end = i + 1;
557 while (end < lines.size() && lines[end].compare(".") != 0)
558 ++end;
559 i = end + 1;
560
561 // A block has at least five lines.
562 if (end - begin < 5)
563 continue;
564
565 PasswordForm form;
566
567 // The first line is the site URL.
568 // For HTTP authentication logins, the URL may contain http realm,
569 // which will be in bracket:
570 // sitename:8080 (realm)
571 GURL url;
572 std::string realm;
573 const char kRealmBracketBegin[] = " (";
574 const char kRealmBracketEnd[] = ")";
575 if (lines[begin].find(kRealmBracketBegin) != std::string::npos) {
576 // In this case, the scheme may not exsit. We assume that the
577 // scheme is HTTP.
578 if (lines[begin].find("://") == std::string::npos)
579 lines[begin] = "http://" + lines[begin];
580
581 size_t start = lines[begin].find(kRealmBracketBegin);
582 url = GURL(lines[begin].substr(0, start));
583
584 start += std::string(kRealmBracketBegin).size();
585 size_t end = lines[begin].rfind(kRealmBracketEnd);
586 realm = lines[begin].substr(start, end - start);
587 } else {
588 // Don't have http realm. It is the URL that the following passwords
589 // belong to.
590 url = GURL(lines[begin]);
591 }
592 // Skips this block if the URL is not valid.
593 if (!url.is_valid())
594 continue;
595 form.origin = url.ReplaceComponents(rep);
596 form.signon_realm = form.origin.GetOrigin().spec();
597 if (!realm.empty())
598 form.signon_realm += realm;
599 form.ssl_valid = form.origin.SchemeIsSecure();
600 ++begin;
601
602 // There may be multiple username/password pairs for this site.
603 // In this case, they are saved in one block without a seperated
604 // line (contains a dot).
605 while (begin + 4 < end) {
606 // The user name.
607 form.username_element = UTF8ToWide(lines[begin++]);
608 form.username_value = Decrypt(lines[begin++]);
609 // The element name has a leading '*'.
610 if (lines[begin].at(0) == '*') {
611 form.password_element = UTF8ToWide(lines[begin++].substr(1));
612 form.password_value = Decrypt(lines[begin++]);
613 } else {
614 // Maybe the file is bad, we skip to next block.
615 break;
616 }
617 // The action attribute from the form element. This line exists
618 // in versin 2 or above.
619 if (version >= 2) {
620 if (begin < end)
621 form.action = GURL(lines[begin]).ReplaceComponents(rep);
622 ++begin;
623 }
624 // Version 3 has an extra line for further use.
625 if (version == 3) {
626 ++begin;
627 }
628
629 forms->push_back(form);
630 }
631 }
632 }
OLDNEW
« no previous file with comments | « chrome/browser/importer/firefox_importer_utils.h ('k') | chrome/browser/importer/firefox_importer_utils_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698