OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "base/nss_util.h" | |
6 #include "base/nss_util_internal.h" | |
7 | |
8 #include <nss.h> | |
9 #include <pk11pub.h> | |
10 #include <plarena.h> | |
11 #include <prerror.h> | |
12 #include <prinit.h> | |
13 #include <prtime.h> | |
14 #include <secmod.h> | |
15 | |
16 #if defined(OS_LINUX) | |
17 #include <linux/nfs_fs.h> | |
18 #include <sys/vfs.h> | |
19 #endif | |
20 | |
21 #include <vector> | |
22 | |
23 #include "base/crypto/scoped_nss_types.h" | |
24 #include "base/environment.h" | |
25 #include "base/file_path.h" | |
26 #include "base/file_util.h" | |
27 #include "base/lazy_instance.h" | |
28 #include "base/logging.h" | |
29 #include "base/memory/scoped_ptr.h" | |
30 #include "base/native_library.h" | |
31 #include "base/stringprintf.h" | |
32 #include "base/threading/thread_restrictions.h" | |
33 | |
34 // USE_NSS means we use NSS for everything crypto-related. If USE_NSS is not | |
35 // defined, such as on Mac and Windows, we use NSS for SSL only -- we don't | |
36 // use NSS for crypto or certificate verification, and we don't use the NSS | |
37 // certificate and key databases. | |
38 #if defined(USE_NSS) | |
39 #include "base/crypto/crypto_module_blocking_password_delegate.h" | |
40 #include "base/synchronization/lock.h" | |
41 #endif // defined(USE_NSS) | |
42 | |
43 namespace base { | |
44 | |
45 namespace { | |
46 | |
47 #if defined(OS_CHROMEOS) | |
48 const char kNSSDatabaseName[] = "Real NSS database"; | |
49 | |
50 // Constants for loading opencryptoki. | |
51 const char kOpencryptokiModuleName[] = "opencryptoki"; | |
52 const char kOpencryptokiPath[] = "/usr/lib/opencryptoki/libopencryptoki.so"; | |
53 | |
54 // TODO(gspencer): Get these values from cryptohomed's dbus API when | |
55 // we ask if it has initialized the TPM yet. These should not be | |
56 // hard-coded here. | |
57 const char kTPMTokenName[] = "Initialized by CrOS"; | |
58 const char kTPMUserPIN[] = "111111"; | |
59 const char kTPMSecurityOfficerPIN[] = "000000"; | |
60 | |
61 // Fake certificate authority database used for testing. | |
62 static const FilePath::CharType kReadOnlyCertDB[] = | |
63 FILE_PATH_LITERAL("/etc/fake_root_ca/nssdb"); | |
64 #endif // defined(OS_CHROMEOS) | |
65 | |
66 std::string GetNSSErrorMessage() { | |
67 std::string result; | |
68 if (PR_GetErrorTextLength()) { | |
69 scoped_array<char> error_text(new char[PR_GetErrorTextLength() + 1]); | |
70 PRInt32 copied = PR_GetErrorText(error_text.get()); | |
71 result = std::string(error_text.get(), copied); | |
72 } else { | |
73 result = StringPrintf("NSS error code: %d", PR_GetError()); | |
74 } | |
75 return result; | |
76 } | |
77 | |
78 #if defined(USE_NSS) | |
79 FilePath GetDefaultConfigDirectory() { | |
80 FilePath dir = file_util::GetHomeDir(); | |
81 if (dir.empty()) { | |
82 LOG(ERROR) << "Failed to get home directory."; | |
83 return dir; | |
84 } | |
85 dir = dir.AppendASCII(".pki").AppendASCII("nssdb"); | |
86 if (!file_util::CreateDirectory(dir)) { | |
87 LOG(ERROR) << "Failed to create ~/.pki/nssdb directory."; | |
88 dir.clear(); | |
89 } | |
90 return dir; | |
91 } | |
92 | |
93 // On non-chromeos platforms, return the default config directory. | |
94 // On chromeos, return a read-only directory with fake root CA certs for testing | |
95 // (which will not exist on non-testing images). These root CA certs are used | |
96 // by the local Google Accounts server mock we use when testing our login code. | |
97 // If this directory is not present, NSS_Init() will fail. It is up to the | |
98 // caller to failover to NSS_NoDB_Init() at that point. | |
99 FilePath GetInitialConfigDirectory() { | |
100 #if defined(OS_CHROMEOS) | |
101 return FilePath(kReadOnlyCertDB); | |
102 #else | |
103 return GetDefaultConfigDirectory(); | |
104 #endif // defined(OS_CHROMEOS) | |
105 } | |
106 | |
107 // This callback for NSS forwards all requests to a caller-specified | |
108 // CryptoModuleBlockingPasswordDelegate object. | |
109 char* PKCS11PasswordFunc(PK11SlotInfo* slot, PRBool retry, void* arg) { | |
110 #if defined(OS_CHROMEOS) | |
111 // If we get asked for a password for the TPM, then return the | |
112 // static password we use. | |
113 if (PK11_GetTokenName(slot) == base::GetTPMTokenName()) | |
114 return PORT_Strdup(GetTPMUserPIN().c_str()); | |
115 #endif | |
116 base::CryptoModuleBlockingPasswordDelegate* delegate = | |
117 reinterpret_cast<base::CryptoModuleBlockingPasswordDelegate*>(arg); | |
118 if (delegate) { | |
119 bool cancelled = false; | |
120 std::string password = delegate->RequestPassword(PK11_GetTokenName(slot), | |
121 retry != PR_FALSE, | |
122 &cancelled); | |
123 if (cancelled) | |
124 return NULL; | |
125 char* result = PORT_Strdup(password.c_str()); | |
126 password.replace(0, password.size(), password.size(), 0); | |
127 return result; | |
128 } | |
129 DLOG(ERROR) << "PK11 password requested with NULL arg"; | |
130 return NULL; | |
131 } | |
132 | |
133 // NSS creates a local cache of the sqlite database if it detects that the | |
134 // filesystem the database is on is much slower than the local disk. The | |
135 // detection doesn't work with the latest versions of sqlite, such as 3.6.22 | |
136 // (NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=578561). So we set | |
137 // the NSS environment variable NSS_SDB_USE_CACHE to "yes" to override NSS's | |
138 // detection when database_dir is on NFS. See http://crbug.com/48585. | |
139 // | |
140 // TODO(wtc): port this function to other USE_NSS platforms. It is defined | |
141 // only for OS_LINUX simply because the statfs structure is OS-specific. | |
142 // | |
143 // Because this function sets an environment variable it must be run before we | |
144 // go multi-threaded. | |
145 void UseLocalCacheOfNSSDatabaseIfNFS(const FilePath& database_dir) { | |
146 #if defined(OS_LINUX) | |
147 struct statfs buf; | |
148 if (statfs(database_dir.value().c_str(), &buf) == 0) { | |
149 if (buf.f_type == NFS_SUPER_MAGIC) { | |
150 scoped_ptr<Environment> env(Environment::Create()); | |
151 const char* use_cache_env_var = "NSS_SDB_USE_CACHE"; | |
152 if (!env->HasVar(use_cache_env_var)) | |
153 env->SetVar(use_cache_env_var, "yes"); | |
154 } | |
155 } | |
156 #endif // defined(OS_LINUX) | |
157 } | |
158 | |
159 // A helper class that acquires the SECMOD list read lock while the | |
160 // AutoSECMODListReadLock is in scope. | |
161 class AutoSECMODListReadLock { | |
162 public: | |
163 AutoSECMODListReadLock() | |
164 : lock_(SECMOD_GetDefaultModuleListLock()) { | |
165 SECMOD_GetReadLock(lock_); | |
166 } | |
167 | |
168 ~AutoSECMODListReadLock() { | |
169 SECMOD_ReleaseReadLock(lock_); | |
170 } | |
171 | |
172 private: | |
173 SECMODListLock* lock_; | |
174 DISALLOW_COPY_AND_ASSIGN(AutoSECMODListReadLock); | |
175 }; | |
176 | |
177 PK11SlotInfo* FindSlotWithTokenName(const std::string& token_name) { | |
178 AutoSECMODListReadLock auto_lock; | |
179 SECMODModuleList* head = SECMOD_GetDefaultModuleList(); | |
180 for (SECMODModuleList* item = head; item != NULL; item = item->next) { | |
181 int slot_count = item->module->loaded ? item->module->slotCount : 0; | |
182 for (int i = 0; i < slot_count; i++) { | |
183 PK11SlotInfo* slot = item->module->slots[i]; | |
184 if (PK11_GetTokenName(slot) == token_name) | |
185 return PK11_ReferenceSlot(slot); | |
186 } | |
187 } | |
188 return NULL; | |
189 } | |
190 | |
191 #endif // defined(USE_NSS) | |
192 | |
193 // A singleton to initialize/deinitialize NSPR. | |
194 // Separate from the NSS singleton because we initialize NSPR on the UI thread. | |
195 // Now that we're leaking the singleton, we could merge back with the NSS | |
196 // singleton. | |
197 class NSPRInitSingleton { | |
198 private: | |
199 friend struct DefaultLazyInstanceTraits<NSPRInitSingleton>; | |
200 | |
201 NSPRInitSingleton() { | |
202 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); | |
203 } | |
204 | |
205 // NOTE(willchan): We don't actually execute this code since we leak NSS to | |
206 // prevent non-joinable threads from using NSS after it's already been shut | |
207 // down. | |
208 ~NSPRInitSingleton() { | |
209 PL_ArenaFinish(); | |
210 PRStatus prstatus = PR_Cleanup(); | |
211 if (prstatus != PR_SUCCESS) { | |
212 LOG(ERROR) << "PR_Cleanup failed; was NSPR initialized on wrong thread?"; | |
213 } | |
214 } | |
215 }; | |
216 | |
217 LazyInstance<NSPRInitSingleton, LeakyLazyInstanceTraits<NSPRInitSingleton> > | |
218 g_nspr_singleton(LINKER_INITIALIZED); | |
219 | |
220 class NSSInitSingleton { | |
221 public: | |
222 #if defined(OS_CHROMEOS) | |
223 void OpenPersistentNSSDB() { | |
224 if (!chromeos_user_logged_in_) { | |
225 // GetDefaultConfigDirectory causes us to do blocking IO on UI thread. | |
226 // Temporarily allow it until we fix http://crbug.com/70119 | |
227 ThreadRestrictions::ScopedAllowIO allow_io; | |
228 chromeos_user_logged_in_ = true; | |
229 | |
230 // This creates another DB slot in NSS that is read/write, unlike | |
231 // the fake root CA cert DB and the "default" crypto key | |
232 // provider, which are still read-only (because we initialized | |
233 // NSS before we had a cryptohome mounted). | |
234 software_slot_ = OpenUserDB(GetDefaultConfigDirectory(), | |
235 kNSSDatabaseName); | |
236 } | |
237 } | |
238 | |
239 bool EnableTPMForNSS() { | |
240 if (!opencryptoki_module_) { | |
241 // This loads the opencryptoki module so we can talk to the | |
242 // hardware TPM. | |
243 opencryptoki_module_ = LoadModule( | |
244 kOpencryptokiModuleName, | |
245 kOpencryptokiPath, | |
246 // trustOrder=100 -- means it'll select this as the most | |
247 // trusted slot for the mechanisms it provides. | |
248 // slotParams=... -- selects RSA as the only mechanism, and only | |
249 // asks for the password when necessary (instead of every | |
250 // time, or after a timeout). | |
251 "trustOrder=100 slotParams=(1={slotFlags=[RSA] askpw=only})"); | |
252 if (opencryptoki_module_) { | |
253 // We shouldn't need to initialize the TPM PIN here because | |
254 // it'll be taken care of by cryptohomed, but we have to make | |
255 // sure that it is initialized. | |
256 | |
257 // TODO(gspencer): replace this with a dbus call that will | |
258 // check to see that cryptohomed has initialized the PINs, and | |
259 // will fetch the token name and PINs for accessing the TPM. | |
260 EnsureTPMInit(); | |
261 | |
262 // If this is set, then we'll use the TPM by default. | |
263 tpm_slot_ = GetTPMSlot(); | |
264 return true; | |
265 } | |
266 } | |
267 return false; | |
268 } | |
269 | |
270 std::string GetTPMTokenName() { | |
271 // TODO(gspencer): This should come from the dbus interchange with | |
272 // cryptohomed instead of being hard-coded. | |
273 return std::string(kTPMTokenName); | |
274 } | |
275 | |
276 std::string GetTPMUserPIN() { | |
277 // TODO(gspencer): This should come from the dbus interchange with | |
278 // cryptohomed instead of being hard-coded. | |
279 return std::string(kTPMUserPIN); | |
280 } | |
281 | |
282 PK11SlotInfo* GetTPMSlot() { | |
283 return FindSlotWithTokenName(GetTPMTokenName()); | |
284 } | |
285 #endif // defined(OS_CHROMEOS) | |
286 | |
287 | |
288 bool OpenTestNSSDB(const FilePath& path, const char* description) { | |
289 test_slot_ = OpenUserDB(path, description); | |
290 return !!test_slot_; | |
291 } | |
292 | |
293 void CloseTestNSSDB() { | |
294 if (test_slot_) { | |
295 SECStatus status = SECMOD_CloseUserDB(test_slot_); | |
296 if (status != SECSuccess) | |
297 LOG(ERROR) << "SECMOD_CloseUserDB failed: " << PORT_GetError(); | |
298 PK11_FreeSlot(test_slot_); | |
299 test_slot_ = NULL; | |
300 } | |
301 } | |
302 | |
303 PK11SlotInfo* GetPublicNSSKeySlot() { | |
304 if (test_slot_) | |
305 return PK11_ReferenceSlot(test_slot_); | |
306 if (software_slot_) | |
307 return PK11_ReferenceSlot(software_slot_); | |
308 return PK11_GetInternalKeySlot(); | |
309 } | |
310 | |
311 PK11SlotInfo* GetPrivateNSSKeySlot() { | |
312 if (test_slot_) | |
313 return PK11_ReferenceSlot(test_slot_); | |
314 // If the TPM slot has been opened, then return that one. | |
315 if (tpm_slot_) | |
316 return PK11_ReferenceSlot(tpm_slot_); | |
317 // If it hasn't, then return the software slot. | |
318 if (software_slot_) | |
319 return PK11_ReferenceSlot(software_slot_); | |
320 return PK11_GetInternalKeySlot(); | |
321 } | |
322 | |
323 #if defined(USE_NSS) | |
324 Lock* write_lock() { | |
325 return &write_lock_; | |
326 } | |
327 #endif // defined(USE_NSS) | |
328 | |
329 // This method is used to force NSS to be initialized without a DB. | |
330 // Call this method before NSSInitSingleton() is constructed. | |
331 static void ForceNoDBInit() { | |
332 force_nodb_init_ = true; | |
333 } | |
334 | |
335 private: | |
336 friend struct DefaultLazyInstanceTraits<NSSInitSingleton>; | |
337 | |
338 NSSInitSingleton() | |
339 : opencryptoki_module_(NULL), | |
340 software_slot_(NULL), | |
341 test_slot_(NULL), | |
342 tpm_slot_(NULL), | |
343 root_(NULL), | |
344 chromeos_user_logged_in_(false) { | |
345 EnsureNSPRInit(); | |
346 | |
347 // We *must* have NSS >= 3.12.3. See bug 26448. | |
348 COMPILE_ASSERT( | |
349 (NSS_VMAJOR == 3 && NSS_VMINOR == 12 && NSS_VPATCH >= 3) || | |
350 (NSS_VMAJOR == 3 && NSS_VMINOR > 12) || | |
351 (NSS_VMAJOR > 3), | |
352 nss_version_check_failed); | |
353 // Also check the run-time NSS version. | |
354 // NSS_VersionCheck is a >= check, not strict equality. | |
355 if (!NSS_VersionCheck("3.12.3")) { | |
356 // It turns out many people have misconfigured NSS setups, where | |
357 // their run-time NSPR doesn't match the one their NSS was compiled | |
358 // against. So rather than aborting, complain loudly. | |
359 LOG(ERROR) << "NSS_VersionCheck(\"3.12.3\") failed. " | |
360 "We depend on NSS >= 3.12.3, and this error is not fatal " | |
361 "only because many people have busted NSS setups (for " | |
362 "example, using the wrong version of NSPR). " | |
363 "Please upgrade to the latest NSS and NSPR, and if you " | |
364 "still get this error, contact your distribution " | |
365 "maintainer."; | |
366 } | |
367 | |
368 SECStatus status = SECFailure; | |
369 bool nodb_init = force_nodb_init_; | |
370 | |
371 #if !defined(USE_NSS) | |
372 // Use the system certificate store, so initialize NSS without database. | |
373 nodb_init = true; | |
374 #endif | |
375 | |
376 if (nodb_init) { | |
377 status = NSS_NoDB_Init(NULL); | |
378 if (status != SECSuccess) { | |
379 LOG(ERROR) << "Error initializing NSS without a persistent " | |
380 "database: " << GetNSSErrorMessage(); | |
381 } | |
382 } else { | |
383 #if defined(USE_NSS) | |
384 FilePath database_dir = GetInitialConfigDirectory(); | |
385 if (!database_dir.empty()) { | |
386 // This duplicates the work which should have been done in | |
387 // EarlySetupForNSSInit. However, this function is idempotent so | |
388 // there's no harm done. | |
389 UseLocalCacheOfNSSDatabaseIfNFS(database_dir); | |
390 | |
391 // Initialize with a persistent database (likely, ~/.pki/nssdb). | |
392 // Use "sql:" which can be shared by multiple processes safely. | |
393 std::string nss_config_dir = | |
394 StringPrintf("sql:%s", database_dir.value().c_str()); | |
395 #if defined(OS_CHROMEOS) | |
396 status = NSS_Init(nss_config_dir.c_str()); | |
397 #else | |
398 status = NSS_InitReadWrite(nss_config_dir.c_str()); | |
399 #endif | |
400 if (status != SECSuccess) { | |
401 LOG(ERROR) << "Error initializing NSS with a persistent " | |
402 "database (" << nss_config_dir | |
403 << "): " << GetNSSErrorMessage(); | |
404 } | |
405 } | |
406 if (status != SECSuccess) { | |
407 VLOG(1) << "Initializing NSS without a persistent database."; | |
408 status = NSS_NoDB_Init(NULL); | |
409 if (status != SECSuccess) { | |
410 LOG(ERROR) << "Error initializing NSS without a persistent " | |
411 "database: " << GetNSSErrorMessage(); | |
412 return; | |
413 } | |
414 } | |
415 | |
416 PK11_SetPasswordFunc(PKCS11PasswordFunc); | |
417 | |
418 // If we haven't initialized the password for the NSS databases, | |
419 // initialize an empty-string password so that we don't need to | |
420 // log in. | |
421 PK11SlotInfo* slot = PK11_GetInternalKeySlot(); | |
422 if (slot) { | |
423 // PK11_InitPin may write to the keyDB, but no other thread can use NSS | |
424 // yet, so we don't need to lock. | |
425 if (PK11_NeedUserInit(slot)) | |
426 PK11_InitPin(slot, NULL, NULL); | |
427 PK11_FreeSlot(slot); | |
428 } | |
429 | |
430 root_ = InitDefaultRootCerts(); | |
431 #endif // defined(USE_NSS) | |
432 } | |
433 } | |
434 | |
435 // NOTE(willchan): We don't actually execute this code since we leak NSS to | |
436 // prevent non-joinable threads from using NSS after it's already been shut | |
437 // down. | |
438 ~NSSInitSingleton() { | |
439 if (tpm_slot_) { | |
440 PK11_FreeSlot(tpm_slot_); | |
441 tpm_slot_ = NULL; | |
442 } | |
443 if (software_slot_) { | |
444 SECMOD_CloseUserDB(software_slot_); | |
445 PK11_FreeSlot(software_slot_); | |
446 software_slot_ = NULL; | |
447 } | |
448 CloseTestNSSDB(); | |
449 if (root_) { | |
450 SECMOD_UnloadUserModule(root_); | |
451 SECMOD_DestroyModule(root_); | |
452 root_ = NULL; | |
453 } | |
454 if (opencryptoki_module_) { | |
455 SECMOD_UnloadUserModule(opencryptoki_module_); | |
456 SECMOD_DestroyModule(opencryptoki_module_); | |
457 opencryptoki_module_ = NULL; | |
458 } | |
459 | |
460 SECStatus status = NSS_Shutdown(); | |
461 if (status != SECSuccess) { | |
462 // We VLOG(1) because this failure is relatively harmless (leaking, but | |
463 // we're shutting down anyway). | |
464 VLOG(1) << "NSS_Shutdown failed; see http://crbug.com/4609"; | |
465 } | |
466 } | |
467 | |
468 #if defined(USE_NSS) | |
469 // Load nss's built-in root certs. | |
470 SECMODModule* InitDefaultRootCerts() { | |
471 SECMODModule* root = LoadModule("Root Certs", "libnssckbi.so", NULL); | |
472 if (root) | |
473 return root; | |
474 | |
475 // Aw, snap. Can't find/load root cert shared library. | |
476 // This will make it hard to talk to anybody via https. | |
477 NOTREACHED(); | |
478 return NULL; | |
479 } | |
480 | |
481 // Load the given module for this NSS session. | |
482 SECMODModule* LoadModule(const char* name, | |
483 const char* library_path, | |
484 const char* params) { | |
485 std::string modparams = StringPrintf( | |
486 "name=\"%s\" library=\"%s\" %s", | |
487 name, library_path, params ? params : ""); | |
488 | |
489 // Shouldn't need to const_cast here, but SECMOD doesn't properly | |
490 // declare input string arguments as const. Bug | |
491 // https://bugzilla.mozilla.org/show_bug.cgi?id=642546 was filed | |
492 // on NSS codebase to address this. | |
493 SECMODModule* module = SECMOD_LoadUserModule( | |
494 const_cast<char*>(modparams.c_str()), NULL, PR_FALSE); | |
495 if (!module) { | |
496 LOG(ERROR) << "Error loading " << name << " module into NSS: " | |
497 << GetNSSErrorMessage(); | |
498 return NULL; | |
499 } | |
500 return module; | |
501 } | |
502 #endif | |
503 | |
504 #if defined(OS_CHROMEOS) | |
505 void EnsureTPMInit() { | |
506 base::ScopedPK11Slot tpm_slot(GetTPMSlot()); | |
507 if (tpm_slot.get()) { | |
508 // TODO(gspencer): Remove this in favor of the dbus API for | |
509 // cryptohomed when that is available. | |
510 if (PK11_NeedUserInit(tpm_slot.get())) { | |
511 PK11_InitPin(tpm_slot.get(), | |
512 kTPMSecurityOfficerPIN, | |
513 kTPMUserPIN); | |
514 } | |
515 } | |
516 } | |
517 #endif | |
518 | |
519 static PK11SlotInfo* OpenUserDB(const FilePath& path, | |
520 const char* description) { | |
521 const std::string modspec = | |
522 StringPrintf("configDir='sql:%s' tokenDescription='%s'", | |
523 path.value().c_str(), description); | |
524 PK11SlotInfo* db_slot = SECMOD_OpenUserDB(modspec.c_str()); | |
525 if (db_slot) { | |
526 if (PK11_NeedUserInit(db_slot)) | |
527 PK11_InitPin(db_slot, NULL, NULL); | |
528 } | |
529 else { | |
530 LOG(ERROR) << "Error opening persistent database (" << modspec | |
531 << "): " << GetNSSErrorMessage(); | |
532 } | |
533 return db_slot; | |
534 } | |
535 | |
536 // If this is set to true NSS is forced to be initialized without a DB. | |
537 static bool force_nodb_init_; | |
538 | |
539 SECMODModule* opencryptoki_module_; | |
540 PK11SlotInfo* software_slot_; | |
541 PK11SlotInfo* test_slot_; | |
542 PK11SlotInfo* tpm_slot_; | |
543 SECMODModule* root_; | |
544 bool chromeos_user_logged_in_; | |
545 #if defined(USE_NSS) | |
546 // TODO(davidben): When https://bugzilla.mozilla.org/show_bug.cgi?id=564011 | |
547 // is fixed, we will no longer need the lock. | |
548 Lock write_lock_; | |
549 #endif // defined(USE_NSS) | |
550 }; | |
551 | |
552 // static | |
553 bool NSSInitSingleton::force_nodb_init_ = false; | |
554 | |
555 LazyInstance<NSSInitSingleton, LeakyLazyInstanceTraits<NSSInitSingleton> > | |
556 g_nss_singleton(LINKER_INITIALIZED); | |
557 | |
558 } // namespace | |
559 | |
560 #if defined(USE_NSS) | |
561 void EarlySetupForNSSInit() { | |
562 FilePath database_dir = GetInitialConfigDirectory(); | |
563 if (!database_dir.empty()) | |
564 UseLocalCacheOfNSSDatabaseIfNFS(database_dir); | |
565 } | |
566 #endif | |
567 | |
568 void EnsureNSPRInit() { | |
569 g_nspr_singleton.Get(); | |
570 } | |
571 | |
572 void EnsureNSSInit() { | |
573 // Initializing SSL causes us to do blocking IO. | |
574 // Temporarily allow it until we fix | |
575 // http://code.google.com/p/chromium/issues/detail?id=59847 | |
576 ThreadRestrictions::ScopedAllowIO allow_io; | |
577 g_nss_singleton.Get(); | |
578 } | |
579 | |
580 void ForceNSSNoDBInit() { | |
581 NSSInitSingleton::ForceNoDBInit(); | |
582 } | |
583 | |
584 void DisableNSSForkCheck() { | |
585 scoped_ptr<Environment> env(Environment::Create()); | |
586 env->SetVar("NSS_STRICT_NOFORK", "DISABLED"); | |
587 } | |
588 | |
589 void LoadNSSLibraries() { | |
590 // Some NSS libraries are linked dynamically so load them here. | |
591 #if defined(USE_NSS) | |
592 // Try to search for multiple directories to load the libraries. | |
593 std::vector<FilePath> paths; | |
594 | |
595 // Use relative path to Search PATH for the library files. | |
596 paths.push_back(FilePath()); | |
597 | |
598 // For Debian derivaties NSS libraries are located here. | |
599 paths.push_back(FilePath("/usr/lib/nss")); | |
600 | |
601 // A list of library files to load. | |
602 std::vector<std::string> libs; | |
603 libs.push_back("libsoftokn3.so"); | |
604 libs.push_back("libfreebl3.so"); | |
605 | |
606 // For each combination of library file and path, check for existence and | |
607 // then load. | |
608 size_t loaded = 0; | |
609 for (size_t i = 0; i < libs.size(); ++i) { | |
610 for (size_t j = 0; j < paths.size(); ++j) { | |
611 FilePath path = paths[j].Append(libs[i]); | |
612 NativeLibrary lib = base::LoadNativeLibrary(path); | |
613 if (lib) { | |
614 ++loaded; | |
615 break; | |
616 } | |
617 } | |
618 } | |
619 | |
620 if (loaded == libs.size()) { | |
621 VLOG(3) << "NSS libraries loaded."; | |
622 } else { | |
623 LOG(WARNING) << "Failed to load NSS libraries."; | |
624 } | |
625 #endif | |
626 } | |
627 | |
628 bool CheckNSSVersion(const char* version) { | |
629 return !!NSS_VersionCheck(version); | |
630 } | |
631 | |
632 #if defined(USE_NSS) | |
633 bool OpenTestNSSDB(const FilePath& path, const char* description) { | |
634 return g_nss_singleton.Get().OpenTestNSSDB(path, description); | |
635 } | |
636 | |
637 void CloseTestNSSDB() { | |
638 g_nss_singleton.Get().CloseTestNSSDB(); | |
639 } | |
640 | |
641 Lock* GetNSSWriteLock() { | |
642 return g_nss_singleton.Get().write_lock(); | |
643 } | |
644 | |
645 AutoNSSWriteLock::AutoNSSWriteLock() : lock_(GetNSSWriteLock()) { | |
646 // May be NULL if the lock is not needed in our version of NSS. | |
647 if (lock_) | |
648 lock_->Acquire(); | |
649 } | |
650 | |
651 AutoNSSWriteLock::~AutoNSSWriteLock() { | |
652 if (lock_) { | |
653 lock_->AssertAcquired(); | |
654 lock_->Release(); | |
655 } | |
656 } | |
657 #endif // defined(USE_NSS) | |
658 | |
659 #if defined(OS_CHROMEOS) | |
660 void OpenPersistentNSSDB() { | |
661 g_nss_singleton.Get().OpenPersistentNSSDB(); | |
662 } | |
663 | |
664 bool EnableTPMForNSS() { | |
665 return g_nss_singleton.Get().EnableTPMForNSS(); | |
666 } | |
667 | |
668 std::string GetTPMTokenName() { | |
669 return g_nss_singleton.Get().GetTPMTokenName(); | |
670 } | |
671 | |
672 std::string GetTPMUserPIN() { | |
673 return g_nss_singleton.Get().GetTPMUserPIN(); | |
674 } | |
675 #endif // defined(OS_CHROMEOS) | |
676 | |
677 // TODO(port): Implement this more simply. We can convert by subtracting an | |
678 // offset (the difference between NSPR's and base::Time's epochs). | |
679 Time PRTimeToBaseTime(PRTime prtime) { | |
680 PRExplodedTime prxtime; | |
681 PR_ExplodeTime(prtime, PR_GMTParameters, &prxtime); | |
682 | |
683 Time::Exploded exploded; | |
684 exploded.year = prxtime.tm_year; | |
685 exploded.month = prxtime.tm_month + 1; | |
686 exploded.day_of_week = prxtime.tm_wday; | |
687 exploded.day_of_month = prxtime.tm_mday; | |
688 exploded.hour = prxtime.tm_hour; | |
689 exploded.minute = prxtime.tm_min; | |
690 exploded.second = prxtime.tm_sec; | |
691 exploded.millisecond = prxtime.tm_usec / 1000; | |
692 | |
693 return Time::FromUTCExploded(exploded); | |
694 } | |
695 | |
696 PK11SlotInfo* GetPublicNSSKeySlot() { | |
697 return g_nss_singleton.Get().GetPublicNSSKeySlot(); | |
698 } | |
699 | |
700 PK11SlotInfo* GetPrivateNSSKeySlot() { | |
701 return g_nss_singleton.Get().GetPrivateNSSKeySlot(); | |
702 } | |
703 | |
704 } // namespace base | |
OLD | NEW |