summaryrefslogtreecommitdiff
path: root/srcs/web_app_enc.c
blob: 22da42012b6e2890b13513effe14180792d8d5ae (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
/*
 *  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        web_app_enc.c
 * @author      Dongsun Lee (ds73.lee@samsung.com)
 * @version     1.0
 * @brief       provides fucntions for encryption and decryption of web application.
 */
#include "web_app_enc.h"

#include <stdlib.h>

#include "decrypt_migrated_wgt.h"
#include "key_handler.h"
#include "crypto_service.h"
#include "types.h"
#include "wae_log.h"

int _wae_encrypt_downloaded_web_application(
		const char *pkg_id, wae_app_type_e app_type,
		const unsigned char *data, size_t data_len,
		unsigned char **pencrypted_data, size_t *pencrypted_data_len)
{
	if (pkg_id == NULL || data == NULL || data_len == 0 || pencrypted_data == NULL ||
			pencrypted_data_len == NULL)
		return WAE_ERROR_INVALID_PARAMETER;

	const crypto_element_s *e = NULL;
	int ret = get_app_ce(pkg_id, app_type, false, &e);

	if (ret == WAE_ERROR_NO_KEY)
		ret = create_app_ce(pkg_id, app_type, &e);

	if (ret != WAE_ERROR_NONE)
		return ret;

	raw_buffer_s _data;
	_data.buf = (unsigned char *)data;
	_data.size = data_len;

	raw_buffer_s *_encrypted_data = NULL;
	ret = encrypt_aes_cbc(e, &_data, &_encrypted_data);
	if (ret != WAE_ERROR_NONE)
		return ret;

	*pencrypted_data = _encrypted_data->buf;
	*pencrypted_data_len = _encrypted_data->size;

	free(_encrypted_data);

	return WAE_ERROR_NONE;
}

int _wae_decrypt_downloaded_web_application(const char *pkg_id, wae_app_type_e app_type,
		const unsigned char *data, size_t data_len,
		unsigned char **pdecrypted_data, size_t *pdecrypted_data_len)
{
	if (pkg_id == NULL || data == NULL || data_len == 0 || pdecrypted_data == NULL ||
			pdecrypted_data_len == NULL)
		return WAE_ERROR_INVALID_PARAMETER;

	raw_buffer_s _data;
	_data.buf = (unsigned char *)data;
	_data.size = data_len;

	const crypto_element_s *ce = NULL;
	int ret = get_app_ce(pkg_id, app_type, true, &ce);

	if (ret != WAE_ERROR_NONE)
		return ret;

	raw_buffer_s *_decrypted_data = NULL;
	if (ce->is_migrated_app)
		ret = decrypt_by_old_ss_algo(ce, &_data, &_decrypted_data);
	else
		ret = decrypt_aes_cbc(ce, &_data, &_decrypted_data);

	if (ret != WAE_ERROR_NONE)
		return ret;

	*pdecrypted_data = _decrypted_data->buf;
	*pdecrypted_data_len = _decrypted_data->size;

	free(_decrypted_data);

	return WAE_ERROR_NONE;
}

int _wae_encrypt_preloaded_web_application(const char *pkg_id,
		const unsigned char *data, size_t data_len,
		unsigned char **pencrypted_data, size_t *pencrypted_data_len)
{
	if (pkg_id == NULL || data == NULL || data_len == 0 || pencrypted_data == NULL ||
			pencrypted_data_len == NULL)
		return WAE_ERROR_INVALID_PARAMETER;

	const crypto_element_s *e = NULL;
	int ret = get_preloaded_app_ce(pkg_id, &e);

	if (ret == WAE_ERROR_NO_KEY)
		ret = create_preloaded_app_ce(pkg_id, &e);

	if (ret != WAE_ERROR_NONE)
		return ret;

	raw_buffer_s _data;
	_data.buf = (unsigned char *)data;
	_data.size = data_len;

	raw_buffer_s *_encrypted_data = NULL;
	ret = encrypt_aes_cbc(e, &_data, &_encrypted_data);

	if (ret != WAE_ERROR_NONE)
		return ret;

	*pencrypted_data = _encrypted_data->buf;
	*pencrypted_data_len = _encrypted_data->size;

	free(_encrypted_data);

	return WAE_ERROR_NONE;
}

int _wae_decrypt_preloaded_web_application(const char *pkg_id, wae_app_type_e app_type,
		const unsigned char *data, size_t data_len,
		unsigned char **pdecrypted_data, size_t *pdecrypted_data_len)
{
	// same with the decryption of downloaded web application
	return _wae_decrypt_downloaded_web_application(pkg_id, app_type,
			data, data_len, pdecrypted_data, pdecrypted_data_len);
}

int wae_encrypt_web_application(const char *pkg_id, wae_app_type_e app_type,
								const unsigned char *data, size_t data_len,
								unsigned char **pencrypted_data, size_t *pencrypted_data_len)
{
	if (app_type == WAE_PRELOADED_APP)
		return _wae_encrypt_preloaded_web_application(pkg_id,
				data, data_len, pencrypted_data, pencrypted_data_len);
	else
		return _wae_encrypt_downloaded_web_application(pkg_id, app_type,
				data, data_len, pencrypted_data, pencrypted_data_len);
}

int wae_decrypt_web_application(const char *pkg_id, wae_app_type_e app_type,
								const unsigned char *data, size_t data_len,
								unsigned char **pdecrypted_data, size_t *pdecrypted_data_len)
{
	if (app_type == WAE_PRELOADED_APP)
		return _wae_decrypt_preloaded_web_application(pkg_id, app_type,
				data, data_len, pdecrypted_data, pdecrypted_data_len);
	else
		return _wae_decrypt_downloaded_web_application(pkg_id, app_type,
				data, data_len, pdecrypted_data, pdecrypted_data_len);
}

int wae_remove_app_dek(const char *pkg_id, wae_app_type_e app_type)
{
	return remove_app_ce(pkg_id, app_type);
}