summaryrefslogtreecommitdiffstats
path: root/chrome/browser/importer/nss_decryptor_mac.mm
blob: dc6dd0d0242a38dc78a1d032d531cc3157375f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <Cocoa/Cocoa.h>

#include <dlfcn.h>

#include "base/sys_string_conversions.h"

#include "chrome/browser/importer/nss_decryptor_mac.h"
#include "chrome/browser/importer/firefox_importer_utils.h"

#include "base/logging.h"

// static
const wchar_t NSSDecryptor::kNSS3Library[] = L"libnss3.dylib";

// Important!! : On OS X the nss3 libraries are compiled with depedencies
// on one another, referenced using dyld's @executable_path directive.
// To make a long story short in order to get the libraries to load, dyld's
// fallback path needs to be set to the directory containing the libraries.
// To do so, the process this function runs in must have the
// DYLD_FALLBACK_LIBRARY_PATH set on startup to said directory.
bool NSSDecryptor::Init(const std::wstring& dll_path,
                        const std::wstring& db_path) {
  FilePath dylib_file_path = FilePath::FromWStringHack(dll_path);
  FilePath nss3_path = dylib_file_path.Append("libnss3.dylib");

  void *nss_3_lib = dlopen(nss3_path.value().c_str(), RTLD_LAZY);
  if (!nss_3_lib) {
    LOG(ERROR) << "Failed to load nss3 lib" << dlerror();
    return false;
  }

  NSS_Init = (NSSInitFunc)dlsym(nss_3_lib, "NSS_Init");
  NSS_Shutdown = (NSSShutdownFunc)dlsym(nss_3_lib, "NSS_Shutdown");
  PK11_GetInternalKeySlot =
      (PK11GetInternalKeySlotFunc)dlsym(nss_3_lib, "PK11_GetInternalKeySlot");
  PK11_CheckUserPassword =
      (PK11CheckUserPasswordFunc)dlsym(nss_3_lib, "PK11_CheckUserPassword");
  PK11_FreeSlot = (PK11FreeSlotFunc)dlsym(nss_3_lib, "PK11_FreeSlot");
  PK11_Authenticate =
      (PK11AuthenticateFunc)dlsym(nss_3_lib, "PK11_Authenticate");
  PK11SDR_Decrypt = (PK11SDRDecryptFunc)dlsym(nss_3_lib, "PK11SDR_Decrypt");
  SECITEM_FreeItem = (SECITEMFreeItemFunc)dlsym(nss_3_lib, "SECITEM_FreeItem");

  if (!NSS_Init || !NSS_Shutdown || !PK11_GetInternalKeySlot ||
      !PK11_CheckUserPassword || !PK11_FreeSlot || !PK11_Authenticate ||
      !PK11SDR_Decrypt || !SECITEM_FreeItem) {
    LOG(ERROR) << "NSS3 importer couldn't find entry points";
    return false;
  }

  SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str());

  if (result != SECSuccess) {
    LOG(ERROR) << "NSS_Init Failed returned: " << result;
    return false;
  }

  is_nss_initialized_ = true;
  return true;
}

NSSDecryptor::~NSSDecryptor() {
  if (NSS_Shutdown && is_nss_initialized_) {
    NSS_Shutdown();
    is_nss_initialized_ = false;
  }
}