aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/sec.c
blob: b0ab647baa5ade6b35d5b9a0a7657b25ca7f85a9 (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
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2011 Simon Busch <morphis@gravedo.de>
 * Copyright (C) 2011-2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * libsamsung-ipc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>

#include <samsung-ipc.h>

int ipc_sec_pin_status_setup(struct ipc_sec_pin_status_request_data *data,
			     unsigned char type, const char *pin1,
			     const char *pin2)
{
	size_t pin1_length;
	size_t pin2_length;

	if (data == NULL)
		return -1;

	memset(data, 0, sizeof(struct ipc_sec_pin_status_request_data));
	data->type = type;

	if (pin1 != NULL) {
		pin1_length = strlen(pin1);
		if (pin1_length > sizeof(data->pin1))
			pin1_length = sizeof(data->pin1);

		data->pin1_length = (unsigned char) pin1_length;
		strncpy((char *) data->pin1, pin1, pin1_length);
	}

	if (pin2 != NULL) {
		pin2_length = strlen(pin2);
		if (pin2_length > sizeof(data->pin2))
			pin2_length = sizeof(data->pin2);

		data->pin2_length = (unsigned char) pin2_length;
		strncpy((char *) data->pin2, pin2, pin2_length);
	}

	return 0;
}

int ipc_sec_phone_lock_request_set_setup(
	struct ipc_sec_phone_lock_request_set_data *data,
	unsigned char facility_type, unsigned char active, const char *password)
{
	size_t password_length;

	if (data == NULL)
		return -1;

	memset(data, 0, sizeof(struct ipc_sec_phone_lock_request_set_data));
	data->facility_type = facility_type;
	data->active = active;

	if (password != NULL) {
		password_length = strlen(password);
		if (password_length > sizeof(data->password))
			password_length = sizeof(data->password);

		data->password_length = (unsigned char) password_length;
		strncpy((char *) data->password, password, password_length);
	} else {
		data->password_length = 0;
	}

	return 0;
}

int ipc_sec_change_locking_pw_setup(
	struct ipc_sec_change_locking_pw_data *data,
	unsigned char facility_type, const char *password_old,
	const char *password_new)
{
	size_t password_old_length;
	size_t password_new_length;

	if (data == NULL)
		return -1;

	memset(data, 0, sizeof(struct ipc_sec_change_locking_pw_data));
	data->facility_type = facility_type;

	if (password_old != NULL) {
		password_old_length = strlen(password_old);
		if (password_old_length > sizeof(data->password_old))
			password_old_length = sizeof(data->password_old);

		data->password_old_length = (unsigned char) password_old_length;
		strncpy((char *) data->password_old, password_old,
			password_old_length);
	} else {
		data->password_old_length = 0;
	}

	if (password_new != NULL) {
		password_new_length = strlen(password_new);
		if (password_new_length > sizeof(data->password_new))
			password_new_length = sizeof(data->password_new);

		data->password_new_length = (unsigned char) password_new_length;
		strncpy((char *) data->password_new, password_new,
			password_new_length);
	} else {
		data->password_new_length = 0;
	}

	return 0;
}

size_t ipc_sec_rsim_access_size_setup(
	struct ipc_sec_rsim_access_request_header *header,
	const void *sim_io_data, size_t sim_io_size)
{
	size_t size;

	if (header == NULL)
		return 0;

	if (sim_io_data == NULL)
		sim_io_size = 0;

	size = sizeof(struct ipc_sec_rsim_access_request_header) + sim_io_size;

	return size;
}

void *ipc_sec_rsim_access_setup(
	struct ipc_sec_rsim_access_request_header *header,
	const void *sim_io_data, size_t sim_io_size)
{
	void *data;
	size_t size;
	unsigned char *p;

	if (header == NULL)
		return NULL;

	if (sim_io_data == NULL)
		sim_io_size = 0;

	size = ipc_sec_rsim_access_size_setup(header, sim_io_data, sim_io_size);
	if (size == 0)
		return NULL;

	data = calloc(1, size);

	p = (unsigned char *) data;

	memcpy(p, header, sizeof(struct ipc_sec_rsim_access_request_header));
	p += sizeof(struct ipc_sec_rsim_access_request_header);

	if (sim_io_data != NULL && sim_io_size > 0) {
		memcpy(p, sim_io_data, sim_io_size);
		p += sim_io_size;
	}

	return data;
}

size_t ipc_sec_rsim_access_size_extract(const void *data, size_t size)
{
	struct ipc_sec_rsim_access_response_header *header;

	if (data == NULL ||
	    size < sizeof(struct ipc_sec_rsim_access_response_header)) {
		return 0;
	}

	header = (struct ipc_sec_rsim_access_response_header *) data;
	if (header->length == 0 ||
	    header->length > size -
	    sizeof(struct ipc_sec_rsim_access_response_header)) {
		return 0;
	}

	return (size_t) header->length;
}

void *ipc_sec_rsim_access_extract(const void *data, size_t size)
{
	struct ipc_sec_rsim_access_response_header *header;
	void *rsim_data;

	if (data == NULL ||
	    size < sizeof(struct ipc_sec_rsim_access_response_header)) {
		return NULL;
	}

	header = (struct ipc_sec_rsim_access_response_header *) data;
	if (header->length == 0 ||
	    header->length > size -
	    sizeof(struct ipc_sec_rsim_access_response_header)) {
		return NULL;
	}

	rsim_data = (void *) (
		(unsigned char *) data +
		sizeof(struct ipc_sec_rsim_access_response_header));

	return rsim_data;
}

int ipc_sec_lock_information_setup(
	struct ipc_sec_lock_information_request_data *data, unsigned char type)
{
	if (data == NULL)
		return -1;

	memset(data, 0, sizeof(struct ipc_sec_lock_information_request_data));
	data->magic = 0x01;
	data->type = type;

	return 0;
}