summaryrefslogtreecommitdiff
path: root/srcs/web_app_enc.c
blob: a5224ea29d3798d85d13d35c0f9b3c8f215cc2d6 (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
/*
 *  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 "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;

	// get APP_DEK.
	//   if not exists, create APP_DEK
	unsigned char *dek = NULL;
	size_t dek_len = -1;
	int ret = get_app_dek(pkg_id, app_type, &dek, &dek_len);

	if (ret == WAE_ERROR_NO_KEY)
		ret = create_app_dek(pkg_id, app_type, &dek, &dek_len);

	if (ret != WAE_ERROR_NONE)
		goto error;

	// encrypt
	ret = encrypt_aes_cbc(dek, dek_len, data, data_len, pencrypted_data, pencrypted_data_len);

error:
	free(dek);

	return ret;
}

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;

	unsigned char *dek = NULL;
	size_t dek_len = -1;
	int ret = get_app_dek(pkg_id, app_type, &dek, &dek_len);

	if (app_type == WAE_DOWNLOADED_GLOBAL_APP && ret == WAE_ERROR_NO_KEY) {
		WAE_SLOGI("app dek for decrypt downloaded app(%s) doesn't exist. This case would be "
				  "needed secure-storage data migration.", pkg_id);

		ret = decrypt_by_old_ss_algo(pkg_id, data, data_len, pdecrypted_data, pdecrypted_data_len);
		if (ret != WAE_ERROR_NONE)
			goto error;
		else
			return WAE_ERROR_NONE;
	} else if (ret != WAE_ERROR_NONE) {
		goto error;
	}

	// decrypt
	ret = decrypt_aes_cbc(dek, dek_len, data, data_len, pdecrypted_data, pdecrypted_data_len);

error:
	free(dek);

	return ret;
}

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;

	unsigned char *dek = NULL;
	size_t dek_len = -1;
	int ret = get_preloaded_app_dek(pkg_id, &dek, &dek_len);

	if (ret == WAE_ERROR_NO_KEY)
		ret = create_preloaded_app_dek(pkg_id, &dek, &dek_len);

	if (ret != WAE_ERROR_NONE)
		goto error;

	// encrypt
	ret = encrypt_aes_cbc(dek, dek_len, data, data_len, pencrypted_data, pencrypted_data_len);

error:
	free(dek);

	return ret;
}

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_dek(pkg_id, app_type);
}