-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptohome_key_loader_unittest.cc
160 lines (135 loc) · 5.51 KB
/
cryptohome_key_loader_unittest.cc
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cryptohome/cryptohome_key_loader.h"
#include <map>
#include <string>
#include <base/files/file_path.h>
#include <brillo/secure_blob.h>
#include <libhwsec/frontend/cryptohome/mock_frontend.h>
#include <libhwsec-foundation/error/testing_helper.h>
#include "cryptohome/mock_platform.h"
using ::hwsec::TPMError;
using ::hwsec::TPMRetryAction;
using ::hwsec_foundation::error::testing::ReturnError;
using ::hwsec_foundation::error::testing::ReturnValue;
using ::testing::_;
using ::testing::ByMove;
using ::testing::Invoke;
using ::testing::NiceMock;
using ::testing::Return;
namespace cryptohome {
constexpr char kDefaultCryptohomeKeyFile[] = "/home/.shadow/cryptohome.key";
constexpr hwsec::KeyToken kTestKeyToken = 17;
constexpr hwsec::KeyAlgoType kTestKeyAlgo = hwsec::KeyAlgoType::kRsa;
// Tests that need to do more setup work before calling Service::Initialize can
// use this instead of ServiceTest.
class CryptohomeKeyLoaderTest : public ::testing::Test {
public:
CryptohomeKeyLoaderTest()
: cryptohome_key_loader_(&hwsec_,
&platform_,
kTestKeyAlgo,
base::FilePath(kDefaultCryptohomeKeyFile)) {}
CryptohomeKeyLoaderTest(const CryptohomeKeyLoaderTest&) = delete;
CryptohomeKeyLoaderTest& operator=(const CryptohomeKeyLoaderTest&) = delete;
virtual ~CryptohomeKeyLoaderTest() = default;
// Default mock implementations for |tpm_| methods.
// For TPM-related flags: enabled is always true, other flags are settable.
bool IsReady() const { return is_hwsec_ready_; }
void SetIsReady(bool is_hwsec_ready) { is_hwsec_ready_ = is_hwsec_ready; }
void SetUp() override {
ON_CALL(hwsec_, IsEnabled()).WillByDefault(ReturnValue(true));
ON_CALL(hwsec_, IsReady()).WillByDefault([this]() { return IsReady(); });
}
void TearDown() override {}
Platform* GetPlatform() { return &platform_; }
protected:
brillo::Blob kTestKeyBlob = brillo::BlobFromString("test_key_blob");
brillo::Blob kTestKeyBlob2 = brillo::BlobFromString("test_key_blob2");
hwsec::ScopedKey GetTestScopedKey() {
return hwsec::ScopedKey(hwsec::Key{.token = 17},
hwsec_.GetFakeMiddlewareDerivative());
}
bool HasStoredCryptohomeKey(brillo::Blob blob) {
brillo::Blob stored_blob;
if (!platform_.ReadFile(base::FilePath(kDefaultCryptohomeKeyFile),
&stored_blob)) {
return false;
}
if (stored_blob != blob) {
return false;
}
return true;
}
bool HasLoadedCryptohomeKey(hwsec::KeyToken token) {
if (!cryptohome_key_loader_.HasCryptohomeKey()) {
return false;
}
hwsec::Key key = cryptohome_key_loader_.GetCryptohomeKey();
if (key.token != token) {
return false;
}
return true;
}
bool is_hwsec_ready_ = false;
std::map<base::FilePath, brillo::Blob> files_;
NiceMock<hwsec::MockCryptohomeFrontend> hwsec_;
NiceMock<MockPlatform> platform_;
// Declare cryptohome_key_loader_ last, so it gets destroyed before all the
// mocks.
CryptohomeKeyLoader cryptohome_key_loader_;
};
ACTION_P(GenerateWrappedKey, wrapped_key) {
*arg0 = brillo::SecureBlob(wrapped_key);
return true;
}
ACTION_P2(LoadWrappedKeyToHandle, tpm, handle) {
arg1->reset(tpm, handle);
return nullptr;
}
TEST_F(CryptohomeKeyLoaderTest, LoadCryptohomeKeySuccess) {
SetIsReady(true);
platform_.WriteFile(base::FilePath(kDefaultCryptohomeKeyFile), kTestKeyBlob);
EXPECT_CALL(hwsec_, LoadKey(kTestKeyBlob))
.WillOnce(Return(ByMove(GetTestScopedKey())));
cryptohome_key_loader_.Init();
EXPECT_TRUE(HasLoadedCryptohomeKey(kTestKeyToken));
}
TEST_F(CryptohomeKeyLoaderTest, LoadCryptohomeKeyNotOwned) {
SetIsReady(false);
platform_.WriteFile(base::FilePath(kDefaultCryptohomeKeyFile), kTestKeyBlob);
EXPECT_CALL(hwsec_, LoadKey(_)).Times(0);
EXPECT_CALL(hwsec_, CreateCryptohomeKey(_)).Times(0);
cryptohome_key_loader_.Init();
EXPECT_FALSE(cryptohome_key_loader_.HasCryptohomeKey());
}
TEST_F(CryptohomeKeyLoaderTest, ReCreateCryptohomeKeyAfterLoadFailure) {
// Permanent failure while loading the key leads to re-creating, storing
// and loading the new key.
SetIsReady(true);
platform_.WriteFile(base::FilePath(kDefaultCryptohomeKeyFile), kTestKeyBlob);
EXPECT_CALL(hwsec_, LoadKey(kTestKeyBlob))
.WillOnce(ReturnError<TPMError>("fake", TPMRetryAction::kNoRetry));
EXPECT_CALL(hwsec_, CreateCryptohomeKey(kTestKeyAlgo))
.WillOnce(Return(ByMove(hwsec::CryptohomeFrontend::CreateKeyResult{
.key = GetTestScopedKey(),
.key_blob = kTestKeyBlob2,
})));
cryptohome_key_loader_.Init();
EXPECT_TRUE(HasLoadedCryptohomeKey(kTestKeyToken));
EXPECT_TRUE(HasStoredCryptohomeKey(kTestKeyBlob2));
}
TEST_F(CryptohomeKeyLoaderTest, ReCreateCryptohomeKeyFailureDuringKeyCreation) {
// Permanent failure while loading the key leads to an attempt to re-create
// the key. Which fails. So, nothing new is stored or loaded.
SetIsReady(true);
platform_.WriteFile(base::FilePath(kDefaultCryptohomeKeyFile), kTestKeyBlob);
EXPECT_CALL(hwsec_, LoadKey(kTestKeyBlob))
.WillOnce(ReturnError<TPMError>("fake", TPMRetryAction::kNoRetry));
EXPECT_CALL(hwsec_, CreateCryptohomeKey(kTestKeyAlgo))
.WillOnce(ReturnError<TPMError>("fake", TPMRetryAction::kNoRetry));
cryptohome_key_loader_.Init();
EXPECT_TRUE(HasStoredCryptohomeKey(kTestKeyBlob));
}
} // namespace cryptohome