1 // Copyright (c) 2012 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 #ifndef CRYPTO_NSS_UTIL_H_
6 #define CRYPTO_NSS_UTIL_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/macros.h"
14 #include "crypto/crypto_export.h"
15 
16 namespace base {
17 class Time;
18 }  // namespace base
19 
20 // This file specifically doesn't depend on any NSS or NSPR headers because it
21 // is included by various (non-crypto) parts of chrome to call the
22 // initialization functions.
23 namespace crypto {
24 
25 // Initialize NRPR if it isn't already initialized.  This function is
26 // thread-safe, and NSPR will only ever be initialized once.
27 CRYPTO_EXPORT void EnsureNSPRInit();
28 
29 // Initialize NSS if it isn't already initialized.  This must be called before
30 // any other NSS functions.  This function is thread-safe, and NSS will only
31 // ever be initialized once.
32 CRYPTO_EXPORT void EnsureNSSInit();
33 
34 // Check if the current NSS version is greater than or equals to |version|.
35 // A sample version string is "3.12.3".
36 bool CheckNSSVersion(const char* version);
37 
38 #if defined(OS_CHROMEOS)
39 // Indicates that NSS should use the Chaps library so that we
40 // can access the TPM through NSS.  InitializeTPMTokenAndSystemSlot and
41 // InitializeTPMForChromeOSUser must still be called to load the slots.
42 CRYPTO_EXPORT void EnableTPMTokenForNSS();
43 
44 // Returns true if EnableTPMTokenForNSS has been called.
45 CRYPTO_EXPORT bool IsTPMTokenEnabledForNSS();
46 
47 // Returns true if the TPM is owned and PKCS#11 initialized with the
48 // user and security officer PINs, and has been enabled in NSS by
49 // calling EnableTPMForNSS, and Chaps has been successfully
50 // loaded into NSS.
51 // If |callback| is non-null and the function returns false, the |callback| will
52 // be run once the TPM is ready. |callback| will never be run if the function
53 // returns true.
54 CRYPTO_EXPORT bool IsTPMTokenReady(base::OnceClosure callback)
55     WARN_UNUSED_RESULT;
56 
57 // Initialize the TPM token and system slot. The |callback| will run on the same
58 // thread with true if the token and slot were successfully loaded or were
59 // already initialized. |callback| will be passed false if loading failed.  Once
60 // called, InitializeTPMTokenAndSystemSlot must not be called again until the
61 // |callback| has been run.
62 CRYPTO_EXPORT void InitializeTPMTokenAndSystemSlot(
63     int system_slot_id,
64     base::OnceCallback<void(bool)> callback);
65 #endif
66 
67 // Convert a NSS PRTime value into a base::Time object.
68 // We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
69 CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64_t prtime);
70 
71 // Convert a base::Time object into a PRTime value.
72 // We use a int64_t instead of PRTime here to avoid depending on NSPR headers.
73 CRYPTO_EXPORT int64_t BaseTimeToPRTime(base::Time time);
74 
75 }  // namespace crypto
76 
77 #endif  // CRYPTO_NSS_UTIL_H_
78