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
3 // found in the LICENSE file.
5 #include <Cocoa/Cocoa.h>
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/strings/sys_string_conversions.h"
13 #include "chrome/common/importer/firefox_importer_utils.h"
14 #include "chrome/utility/importer/nss_decryptor_mac.h"
16 // Important!! : On OS X the nss3 libraries are compiled with depedencies
17 // on one another, referenced using dyld's @executable_path directive.
18 // To make a long story short in order to get the libraries to load, dyld's
19 // fallback path needs to be set to the directory containing the libraries.
20 // To do so, the process this function runs in must have the
21 // DYLD_FALLBACK_LIBRARY_PATH set on startup to said directory.
22 bool NSSDecryptor::Init(const base::FilePath& dll_path,
23 const base::FilePath& db_path) {
24 if (getenv("DYLD_FALLBACK_LIBRARY_PATH") == NULL) {
25 LOG(ERROR) << "DYLD_FALLBACK_LIBRARY_PATH variable not set";
28 base::FilePath nss3_path = dll_path.Append("libnss3.dylib");
30 void* nss_3_lib = dlopen(nss3_path.value().c_str(), RTLD_LAZY);
32 LOG(ERROR) << "Failed to load nss3 lib" << dlerror();
36 NSS_Init = (NSSInitFunc)dlsym(nss_3_lib, "NSS_Init");
37 NSS_Shutdown = (NSSShutdownFunc)dlsym(nss_3_lib, "NSS_Shutdown");
38 PK11_GetInternalKeySlot =
39 (PK11GetInternalKeySlotFunc)dlsym(nss_3_lib, "PK11_GetInternalKeySlot");
40 PK11_CheckUserPassword =
41 (PK11CheckUserPasswordFunc)dlsym(nss_3_lib, "PK11_CheckUserPassword");
42 PK11_FreeSlot = (PK11FreeSlotFunc)dlsym(nss_3_lib, "PK11_FreeSlot");
44 (PK11AuthenticateFunc)dlsym(nss_3_lib, "PK11_Authenticate");
45 PK11SDR_Decrypt = (PK11SDRDecryptFunc)dlsym(nss_3_lib, "PK11SDR_Decrypt");
46 SECITEM_FreeItem = (SECITEMFreeItemFunc)dlsym(nss_3_lib, "SECITEM_FreeItem");
48 if (!NSS_Init || !NSS_Shutdown || !PK11_GetInternalKeySlot ||
49 !PK11_CheckUserPassword || !PK11_FreeSlot || !PK11_Authenticate ||
50 !PK11SDR_Decrypt || !SECITEM_FreeItem) {
51 LOG(ERROR) << "NSS3 importer couldn't find entry points";
55 SECStatus result = NSS_Init(db_path.value().c_str());
57 if (result != SECSuccess) {
58 LOG(ERROR) << "NSS_Init Failed returned: " << result;
62 is_nss_initialized_ = true;
66 NSSDecryptor::~NSSDecryptor() {
67 if (NSS_Shutdown && is_nss_initialized_) {
69 is_nss_initialized_ = false;