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/file_path.h" 10 #include "base/logging.h" 11 #include "base/sys_string_conversions.h" 12 13 #include "chrome/browser/importer/nss_decryptor_mac.h" 14 #include "chrome/browser/importer/firefox_importer_utils.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 FilePath& dll_path, const FilePath& db_path) { 23 if (getenv("DYLD_FALLBACK_LIBRARY_PATH") == NULL) { 24 LOG(ERROR) << "DYLD_FALLBACK_LIBRARY_PATH variable not set"; 25 return false; 26 } 27 FilePath nss3_path = dll_path.Append("libnss3.dylib"); 28 29 void* nss_3_lib = dlopen(nss3_path.value().c_str(), RTLD_LAZY); 30 if (!nss_3_lib) { 31 LOG(ERROR) << "Failed to load nss3 lib" << dlerror(); 32 return false; 33 } 34 35 NSS_Init = (NSSInitFunc)dlsym(nss_3_lib, "NSS_Init"); 36 NSS_Shutdown = (NSSShutdownFunc)dlsym(nss_3_lib, "NSS_Shutdown"); 37 PK11_GetInternalKeySlot = 38 (PK11GetInternalKeySlotFunc)dlsym(nss_3_lib, "PK11_GetInternalKeySlot"); 39 PK11_CheckUserPassword = 40 (PK11CheckUserPasswordFunc)dlsym(nss_3_lib, "PK11_CheckUserPassword"); 41 PK11_FreeSlot = (PK11FreeSlotFunc)dlsym(nss_3_lib, "PK11_FreeSlot"); 42 PK11_Authenticate = 43 (PK11AuthenticateFunc)dlsym(nss_3_lib, "PK11_Authenticate"); 44 PK11SDR_Decrypt = (PK11SDRDecryptFunc)dlsym(nss_3_lib, "PK11SDR_Decrypt"); 45 SECITEM_FreeItem = (SECITEMFreeItemFunc)dlsym(nss_3_lib, "SECITEM_FreeItem"); 46 47 if (!NSS_Init || !NSS_Shutdown || !PK11_GetInternalKeySlot || 48 !PK11_CheckUserPassword || !PK11_FreeSlot || !PK11_Authenticate || 49 !PK11SDR_Decrypt || !SECITEM_FreeItem) { 50 LOG(ERROR) << "NSS3 importer couldn't find entry points"; 51 return false; 52 } 53 54 SECStatus result = NSS_Init(db_path.value().c_str()); 55 56 if (result != SECSuccess) { 57 LOG(ERROR) << "NSS_Init Failed returned: " << result; 58 return false; 59 } 60 61 is_nss_initialized_ = true; 62 return true; 63 } 64 65 NSSDecryptor::~NSSDecryptor() { 66 if (NSS_Shutdown && is_nss_initialized_) { 67 NSS_Shutdown(); 68 is_nss_initialized_ = false; 69 } 70 } 71