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. 4 5 #include <Cocoa/Cocoa.h> 6 7 #include <dlfcn.h> 8 9 #include "base/files/file_path.h" 10 #include "base/logging.h" 11 #include "base/strings/sys_string_conversions.h" 12 13 #include "chrome/common/importer/firefox_importer_utils.h" 14 #include "chrome/utility/importer/nss_decryptor_mac.h" 15 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"; 26 return false; 27 } 28 base::FilePath nss3_path = dll_path.Append("libnss3.dylib"); 29 30 void* nss_3_lib = dlopen(nss3_path.value().c_str(), RTLD_LAZY); 31 if (!nss_3_lib) { 32 LOG(ERROR) << "Failed to load nss3 lib" << dlerror(); 33 return false; 34 } 35 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"); 43 PK11_Authenticate = 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"); 47 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"; 52 return false; 53 } 54 55 SECStatus result = NSS_Init(db_path.value().c_str()); 56 57 if (result != SECSuccess) { 58 LOG(ERROR) << "NSS_Init Failed returned: " << result; 59 return false; 60 } 61 62 is_nss_initialized_ = true; 63 return true; 64 } 65 66 NSSDecryptor::~NSSDecryptor() { 67 if (NSS_Shutdown && is_nss_initialized_) { 68 NSS_Shutdown(); 69 is_nss_initialized_ = false; 70 } 71 } 72