summaryrefslogtreecommitdiff
path: root/tests/internals.cpp
blob: 1fa9aff85e5ba1654576d6a22cb72493b1a51f8a (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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *
 * @file        internals.cpp
 * @author      Dongsun Lee (ds73.lee@samsung.com)
 *              Kyungwook Tak (k.tak@samsung.com)
 * @version     2.0
 * @brief       internal functions test
 */
#include "web_app_enc.h"

#include <string>
#include <cstring>
#include <unistd.h>

#include <boost/test/unit_test.hpp>

#include "key_handler.h"
#include "crypto_service.h"

#include "test-common.h"

namespace {

using rb_raii = std::unique_ptr<raw_buffer_s, void(*)(raw_buffer_s *)>;
using ce_raii = std::unique_ptr<crypto_element_s, void(*)(crypto_element_s *)>;
using map_raii = std::unique_ptr<crypto_element_map_s, void(*)(crypto_element_map_s *)>;

inline rb_raii _safe(raw_buffer_s *ptr)
{
	return rb_raii(ptr, buffer_destroy);
}

inline ce_raii _safe(crypto_element_s *ptr)
{
	return ce_raii(ptr, crypto_element_destroy);
}

inline map_raii _safe(crypto_element_map_s *ptr)
{
	return map_raii(ptr, crypto_element_map_destroy);
}

crypto_element_s *_create_ce(void)
{
	raw_buffer_s *dek = buffer_create(32);
	raw_buffer_s *iv = buffer_create(16);
	crypto_element_s *ce = crypto_element_create(dek, iv);

	if (ce == nullptr) {
		buffer_destroy(dek);
		buffer_destroy(iv);
	} else if (_get_random(ce->dek) != WAE_ERROR_NONE) {
		crypto_element_destroy(ce);
		ce = nullptr;
	} else if (_get_random(ce->iv) != WAE_ERROR_NONE) {
		crypto_element_destroy(ce);
		ce = nullptr;
	}

	BOOST_REQUIRE(ce != nullptr);

	return ce;
}

}

BOOST_AUTO_TEST_SUITE(SYSTEM)

BOOST_AUTO_TEST_SUITE(INTERNALS)

BOOST_AUTO_TEST_CASE(encrypt_decrypt_app_dek)
{
	const char *private_key =
		"-----BEGIN RSA PRIVATE KEY-----\n"
		"MIIEpgIBAAKCAQEA0kWtjpRO7Zh2KX2naVE/BDJdrfwK9xexfNA0MkY2VJ4J2AKM\n"
		"YTj1D1jntceryupCEHOvP3rum+WsFvPXduz9+VKnSsSqj4jcTUubtpDUGA5G79Iq\n"
		"LEPFuSBa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5b17nxTO8rvX416fIftG2ixgQ7vR6us0\n"
		"m9+jq56ZFj9zP4eHJudf9h9yNo5TgwVXnMCGh/4iGbcMJgrrsfxUHu5VNiK5UCSj\n"
		"VtqAZGDoZVryUMIkXQVhezIRAoGBAN7QUIqcGbcUA24257Wu4hVlrUN+WPCAyDEr\n"
		"aL/x/ZV5eXaoYwQlw6LuGpTDOmDgfN2M5FyARuOL/LOIRaSLGXnIU4WoeUSCd8VM\n"
		"6Z9Og7bMnrpjfPEUDBH02hcH1kkNPUwLOZgva2Dm0tdSIcpSWFVTu/E4Io4uQHi8\n"
		"DVqc2ZsNAoGBAJT76ezXNSSv8hnrKqTpwgTicpqhRZ3eFQjyl4HRL26AJMKv++x8\n"
		"4/IsVIwxaHzpbN3nnCjmAHV4gX9YpxVnvYcZflC9WZeDkwNMLmPYb3Zg27EzSMfQ\n"
		"8yrfWJZo3qobipcHf1yohAt4fHk9kUKtPHEwp0xKe//rfhswLb3VCzvQ\n"
		"-----END RSA PRIVATE KEY-----";

	const char *public_key =
		"-----BEGIN PUBLIC KEY-----\n"
		"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0kWtjpRO7Zh2KX2naVE/\n"
		"BDJdrfwK9xexfNA0MkY2VJ4J2AKMYTj1D1jntceryupCEHOvP3rum+WsFvPXduz9\n"
		"+VKnSsSqj4jcTUubtpDUGA5G79IqLEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq\n"
		"8JkqBPoCCwtUs73ruE9VbtsBO/kTlASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCw\n"
		"S2OTvQDNvsXfFnA0ZJEEYw/rZLirj7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12Yxq\n"
		"Hdy7gnJXodLhvE/cR4SN9VW7+qmCMBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4\n"
		"+wIDAQAB\n"
		"-----END PUBLIC KEY-----";

	raw_buffer_s *dek = buffer_create(32);

	auto _raii1 = _safe(dek);

	BOOST_REQUIRE_MESSAGE(dek != nullptr && dek->size == 32, "Failed to create buffer");
	BOOST_REQUIRE_MESSAGE(_get_random(dek) == WAE_ERROR_NONE, "Failed to get random");

	raw_buffer_s pubkey;

	pubkey.buf = (unsigned char *)public_key;
	pubkey.size = strlen(public_key);

	raw_buffer_s *encrypted = nullptr;
	int ret = encrypt_app_dek(&pubkey, dek, &encrypted);

	auto _raii2 = _safe(encrypted);

	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to encrypt_app_dek. ec: " << ret);

	raw_buffer_s prikey;
	prikey.buf = (unsigned char *)private_key;
	prikey.size = strlen(private_key);

	raw_buffer_s *decrypted = nullptr;
	ret = decrypt_app_dek(&prikey, nullptr, encrypted, &decrypted);

	auto _raii3 = _safe(decrypted);

	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to decrypt_app_dek. ec: " << ret);

	BOOST_REQUIRE_MESSAGE(Wae::Test::bytes_to_hex(dek) == Wae::Test::bytes_to_hex(decrypted),
			"encrypted/decrypted dek isn't valid. "
			"dek(" << Wae::Test::bytes_to_hex(dek) << ") "
			"decrypted(" << Wae::Test::bytes_to_hex(decrypted) << ")");
}

BOOST_AUTO_TEST_CASE(encrypt_decrypt_aes_cbc)
{
	raw_buffer_s *data = buffer_create(16);

	auto _raii1 = _safe(data);

	BOOST_REQUIRE_MESSAGE(data != nullptr && data->size == 16, "Failed to create buffer");

	crypto_element_s *ce = _create_ce();

	auto _raii2 = _safe(ce);

	raw_buffer_s *encrypted = nullptr;
	int ret = encrypt_aes_cbc(ce, data, &encrypted);

	auto _raii3 = _safe(encrypted);

	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to encrypt_aes_cbc. ec: " << ret);

	raw_buffer_s *decrypted = nullptr;
	ret = decrypt_aes_cbc(ce, encrypted, &decrypted);

	auto _raii4 = _safe(decrypted);

	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to decrypt_aes_cbc. ec: " << ret);
	BOOST_REQUIRE_MESSAGE(Wae::Test::bytes_to_hex(data) == Wae::Test::bytes_to_hex(decrypted),
			"decrypted plaintext isn't valid. "
			"plaintext(" << Wae::Test::bytes_to_hex(data) << ") "
			"decrypted(" << Wae::Test::bytes_to_hex(decrypted) << ")");
}

BOOST_AUTO_TEST_CASE(cache)
{
	const char *pkg1 = "pkg1";
	const char *pkg2 = "pkg2";
	const char *pkg3 = "pkg3";
	const char *pkgDummy = "dummy";

	auto ce1 = _create_ce();
	auto ce2 = _create_ce();
	auto ce3 = _create_ce();

	crypto_element_map_s *map = nullptr;

	int tmp = crypto_element_map_add(&map, pkg1, ce1);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to add ce to map. ret: " << tmp);

	tmp = crypto_element_map_add(&map, pkg2, ce2);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to add ce to map. ret: " << tmp);

	tmp = crypto_element_map_add(&map, pkg3, ce3);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to add ce to map. ret: " << tmp);

	BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg1) == ce1,
			"cached ce has different address with actual.");
	BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg2) == ce2,
			"cached ce has different address with actual.");
	BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg3) == ce3,
			"cached ce has different address with actual.");
	BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkgDummy) == nullptr,
			"something returned with pkg dummy from map which should be null.");

	crypto_element_map_remove(&map, pkg3);

	BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg3) == nullptr,
			"removed pkg(" << pkg3 << ") is returned from map which should be null.");

	auto ce4 = _create_ce();
	tmp = crypto_element_map_add(&map, pkg1, ce4);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to update ce to map. ret: " << tmp);

	BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg1) == ce4,
			"cached ce has different address with actual.");

	crypto_element_map_destroy(map);
}

BOOST_AUTO_TEST_CASE(cache_max)
{
	crypto_element_map_s *map = nullptr;

	for (size_t i = 0; i < MAX_MAP_ELEMENT_SIZE + 1; ++i) {
		BOOST_REQUIRE(crypto_element_map_add(&map, std::to_string(i).c_str(), _create_ce()) == WAE_ERROR_NONE);
	}

	BOOST_REQUIRE(crypto_element_map_get(map, "0") == nullptr);

	crypto_element_map_destroy(map);
}

BOOST_AUTO_TEST_CASE(read_write_encrypted_app_dek)
{
	const char *pkg_id = "write_test_pkg";

	raw_buffer_s *dek = buffer_create(256);

	auto raii1 = _safe(dek);

	BOOST_REQUIRE(dek != nullptr);
	BOOST_REQUIRE(_get_random(dek) == WAE_ERROR_NONE);

	int ret = _write_encrypted_app_dek_to_file(pkg_id, dek);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to write_encrypted_app_dek_to_file. ec: " << ret);

	raw_buffer_s *readed = nullptr;
	ret = _read_encrypted_app_dek_from_file(pkg_id, &readed);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to read_encrypted_app_dek_from_file. ec: " << ret);

	auto raii2 = _safe(readed);

	BOOST_REQUIRE_MESSAGE(Wae::Test::bytes_to_hex(dek) == Wae::Test::bytes_to_hex(readed),
			"dek isn't match after write/read file. "
			"dek(" << Wae::Test::bytes_to_hex(dek) << ") "
			"readed(" << Wae::Test::bytes_to_hex(readed) << ")");
}

BOOST_AUTO_TEST_CASE(get_create_preloaded_app_dek_1)
{
	const char *pkg_id = "TEST_PKG_ID_FOR_CREATE";

	const crypto_element_s *readed = nullptr;
	int ret = get_preloaded_app_ce(pkg_id, &readed);

	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NO_KEY,
			"preloaded app ce to create is already exist. ec: " << ret);

	const crypto_element_s *ce = nullptr;
	ret = create_preloaded_app_ce(pkg_id, &ce);

	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
			"Failed to create_preloaded_app_ce. ec: " << ret);

	ret = get_preloaded_app_ce(pkg_id, &readed);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
			"Failed to get_preloaded_app_ce. ec: " << ret);

	BOOST_REQUIRE_MESSAGE(readed == ce, "cached ce address and actual is different!");
}

BOOST_AUTO_TEST_CASE(get_create_preloaded_app_dek_2)
{
	const char *pkg_id1 = "TEST_PKGID_1";
	const char *pkg_id2 = "TEST_PKGID_2";

	char path1[MAX_PATH_LEN] = {0, };
	char path2[MAX_PATH_LEN] = {0, };
	_get_preloaded_app_dek_file_path(pkg_id1, sizeof(path1), path1);
	_get_preloaded_app_dek_file_path(pkg_id2, sizeof(path2), path2);

	// remove old test data
	remove_app_ce(0, pkg_id1, WAE_PRELOADED_APP);
	remove_app_ce(0, pkg_id2, WAE_PRELOADED_APP);
	unlink(path1);
	unlink(path2);

	// create 2 ces for preloaded app
	const crypto_element_s *ce1 = nullptr;
	int ret = create_preloaded_app_ce(pkg_id1, &ce1);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
			"Failed to create_preloaded_app_ce. ec: " << ret);

	const crypto_element_s *ce2 = nullptr;
	ret = create_preloaded_app_ce(pkg_id2, &ce2);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
			"Failed to create_preloaded_app_ce. ec: " << ret);

	ret = load_preloaded_app_deks(true);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
			"Failed to load_preloaded_app_deks. ec: " << ret);

	const crypto_element_s *readed1 = nullptr;
	ret = get_app_ce(0, pkg_id1, WAE_PRELOADED_APP, false, &readed1);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to get_app_dek. ec: " << ret);

	const crypto_element_s *readed2 = nullptr;
	ret = get_app_ce(0, pkg_id2, WAE_PRELOADED_APP, false, &readed2);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to get_app_dek. ec: " << ret);

	BOOST_REQUIRE_MESSAGE(readed1 == ce1, "cached ce and actual address is different!");
	BOOST_REQUIRE_MESSAGE(readed2 == ce2, "cached ce and actual address is different!");

	ret = remove_app_ce(0, pkg_id1, WAE_PRELOADED_APP);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed remove app ce. ec: " << ret);

	ret = remove_app_ce(0, pkg_id2, WAE_PRELOADED_APP);
	BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed remove app ce. ec: " << ret);
}

BOOST_AUTO_TEST_SUITE_END() // INTERNALS

BOOST_AUTO_TEST_SUITE_END() // SYSTEM