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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
* Copyright (c) 2016, Ford Motor Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the Ford Motor Company nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __QNXNTO__
#include <openssl/ssl3.h>
#else
#include <openssl/ssl.h>
#endif //__QNXNTO__
#include <limits>
#include <fstream>
#include <sstream>
#include "gtest/gtest.h"
#include "security_manager/crypto_manager_impl.h"
#include "security_manager/mock_security_manager_settings.h"
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::NiceMock;
namespace {
const size_t kUpdatesBeforeHour = 24;
const std::string kAllCiphers = "ALL";
const std::string kCaCertPath = "";
#ifdef __QNXNTO__
const std::string kFordCipher = SSL3_TXT_RSA_DES_192_CBC3_SHA;
#else
// Used cipher from ford protocol requirement
const std::string kFordCipher = TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384;
#endif
}
namespace test {
namespace components {
namespace crypto_manager_test {
class CryptoManagerTest : public testing::Test {
protected:
static void SetUpTestCase() {
std::ifstream certificate_file("server/spt_credential.p12.enc");
ASSERT_TRUE(certificate_file.is_open())
<< "Could not open certificate data file";
const std::string certificate(
(std::istreambuf_iterator<char>(certificate_file)),
std::istreambuf_iterator<char>());
ASSERT_FALSE(certificate.empty()) << "Certificate data file is empty";
certificate_data_base64_ = certificate;
}
void SetUp() OVERRIDE {
ASSERT_FALSE(certificate_data_base64_.empty());
mock_security_manager_settings_ =
new NiceMock<security_manager_test::MockCryptoManagerSettings>();
utils::SharedPtr<security_manager::CryptoManagerSettings> scrypto =
utils::SharedPtr<security_manager::CryptoManagerSettings>::
static_pointer_cast<security_manager::CryptoManagerSettings>(
mock_security_manager_settings_);
crypto_manager_ = new security_manager::CryptoManagerImpl(scrypto);
}
void TearDown() OVERRIDE {
delete mock_security_manager_settings_;
testing::Mock::AsyncVerifyAndClearExpectations(1000);
}
void InitSecurityManager() {
SetInitialValues(security_manager::CLIENT,
security_manager::TLSv1_2,
kAllCiphers);
const bool crypto_manager_initialization = crypto_manager_->Init();
ASSERT_TRUE(crypto_manager_initialization);
}
void SetInitialValues(security_manager::Mode mode,
security_manager::Protocol protocol,
const std::string& cipher) {
ON_CALL(*mock_security_manager_settings_, security_manager_mode())
.WillByDefault(Return(mode));
ON_CALL(*mock_security_manager_settings_,
security_manager_protocol_name()).WillByDefault(Return(protocol));
ON_CALL(*mock_security_manager_settings_, certificate_data())
.WillByDefault(ReturnRef(certificate_data_base64_));
ON_CALL(*mock_security_manager_settings_, ciphers_list())
.WillByDefault(ReturnRef(cipher));
ON_CALL(*mock_security_manager_settings_, ca_cert_path())
.WillByDefault(ReturnRef(kCaCertPath));
ON_CALL(*mock_security_manager_settings_, verify_peer())
.WillByDefault(Return(false));
}
security_manager::CryptoManager* crypto_manager_;
static std::string certificate_data_base64_;
NiceMock<security_manager_test::MockCryptoManagerSettings>*
mock_security_manager_settings_;
};
std::string CryptoManagerTest::certificate_data_base64_;
TEST_F(CryptoManagerTest, UsingBeforeInit) {
EXPECT_TRUE(crypto_manager_->CreateSSLContext() == NULL);
EXPECT_EQ(std::string("Initialization is not completed"),
crypto_manager_->LastError());
}
TEST_F(CryptoManagerTest, WrongInit) {
// We have to cast (-1) to security_manager::Protocol Enum to be accepted by
// crypto_manager_->Init(...)
// Unknown protocol version
security_manager::Protocol UNKNOWN =
static_cast<security_manager::Protocol>(-1);
EXPECT_CALL(*mock_security_manager_settings_, security_manager_mode())
.WillRepeatedly(Return(security_manager::SERVER));
EXPECT_CALL(*mock_security_manager_settings_, security_manager_protocol_name())
.WillOnce(Return(UNKNOWN));
EXPECT_FALSE(crypto_manager_->Init());
EXPECT_NE( std::string(),crypto_manager_->LastError());
// Unexistent cipher value
const std::string invalid_cipher = "INVALID_UNKNOWN_CIPHER";
EXPECT_CALL(*mock_security_manager_settings_, security_manager_protocol_name())
.WillOnce(Return(security_manager::TLSv1_2));
EXPECT_CALL(*mock_security_manager_settings_, certificate_data())
.WillOnce(ReturnRef(certificate_data_base64_));
EXPECT_CALL(*mock_security_manager_settings_, ciphers_list())
.WillRepeatedly(ReturnRef(invalid_cipher));
EXPECT_FALSE(crypto_manager_->Init());
EXPECT_NE(std::string(), crypto_manager_->LastError());
}
// #ifndef __QNXNTO__
TEST_F(CryptoManagerTest, CorrectInit) {
// Empty cert and key values for SERVER
SetInitialValues(security_manager::SERVER,
security_manager::TLSv1_2,
kFordCipher);
EXPECT_TRUE(crypto_manager_->Init());
// Recall init
SetInitialValues(security_manager::CLIENT,
security_manager::TLSv1_2,
kFordCipher);
EXPECT_TRUE(crypto_manager_->Init());
// Recall init with other protocols
SetInitialValues(security_manager::CLIENT,
security_manager::TLSv1_2,
kFordCipher);
EXPECT_TRUE(crypto_manager_->Init());
SetInitialValues(security_manager::CLIENT,
security_manager::TLSv1_1,
kFordCipher);
EXPECT_TRUE(crypto_manager_->Init());
// Cipher value
SetInitialValues(security_manager::SERVER,
security_manager::TLSv1_2,
kAllCiphers);
EXPECT_TRUE(crypto_manager_->Init());
}
// #endif // __QNX__
TEST_F(CryptoManagerTest, ReleaseSSLContext_Null) {
EXPECT_NO_THROW(crypto_manager_->ReleaseSSLContext(NULL));
}
TEST_F(CryptoManagerTest, CreateReleaseSSLContext) {
const size_t max_payload_size = 1000u;
SetInitialValues(security_manager::CLIENT, security_manager::TLSv1_2,
kAllCiphers);
EXPECT_TRUE(crypto_manager_->Init());
EXPECT_CALL(*mock_security_manager_settings_, security_manager_mode())
.Times(2)
.WillRepeatedly(Return(security_manager::CLIENT));
EXPECT_CALL(*mock_security_manager_settings_, maximum_payload_size())
.Times(1)
.WillRepeatedly(Return(max_payload_size));
security_manager::SSLContext* context = crypto_manager_->CreateSSLContext();
EXPECT_TRUE(context);
EXPECT_NO_THROW(crypto_manager_->ReleaseSSLContext(context));
}
TEST_F(CryptoManagerTest, OnCertificateUpdated) {
InitSecurityManager();
EXPECT_TRUE(crypto_manager_->OnCertificateUpdated(certificate_data_base64_));
}
TEST_F(CryptoManagerTest, OnCertificateUpdated_UpdateNotRequired) {
size_t updates_before = 0;
SetInitialValues(security_manager::CLIENT,
security_manager::TLSv1_2,
kAllCiphers);
ASSERT_TRUE(crypto_manager_->Init());
EXPECT_CALL(*mock_security_manager_settings_, update_before_hours())
.WillOnce(Return(updates_before));
EXPECT_FALSE(crypto_manager_->IsCertificateUpdateRequired());
size_t max_updates_ = std::numeric_limits<size_t>::max();
SetInitialValues(security_manager::CLIENT,
security_manager::TLSv1_2,
kAllCiphers);
EXPECT_CALL(*mock_security_manager_settings_, update_before_hours())
.WillOnce(Return(max_updates_));
ASSERT_TRUE(crypto_manager_->Init());
EXPECT_TRUE(crypto_manager_->IsCertificateUpdateRequired());
}
TEST_F(CryptoManagerTest, OnCertificateUpdated_NotInitialized) {
EXPECT_FALSE(crypto_manager_->OnCertificateUpdated(certificate_data_base64_));
}
TEST_F(CryptoManagerTest, OnCertificateUpdated_NullString) {
InitSecurityManager();
EXPECT_FALSE(crypto_manager_->OnCertificateUpdated(std::string()));
}
TEST_F(CryptoManagerTest, OnCertificateUpdated_MalformedSign) {
InitSecurityManager();
// Corrupt the middle symbol
certificate_data_base64_[certificate_data_base64_.size() / 2] = '?';
EXPECT_FALSE(crypto_manager_->OnCertificateUpdated(certificate_data_base64_));
}
TEST_F(CryptoManagerTest, OnCertificateUpdated_WrongInitFolder) {
SetInitialValues(security_manager::CLIENT,
security_manager::TLSv1_2,
kAllCiphers);
ASSERT_TRUE(crypto_manager_->Init());
const std::string certificate = "wrong_data";
ASSERT_FALSE(certificate.empty());
EXPECT_FALSE(crypto_manager_->OnCertificateUpdated(certificate));
}
} // namespace crypto_manager_test
} // namespace components
} // namespace test
|