blob: e34053be2c512306a49a7dc8a565d197f00487db [file] [log] [blame]
Chia-chi Yehadbc99b2009-09-18 10:15:37 +08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdint.h>
19#include <string.h>
20#include <unistd.h>
21#include <signal.h>
22#include <errno.h>
23#include <dirent.h>
24#include <fcntl.h>
25#include <limits.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <arpa/inet.h>
31
32#include <openssl/aes.h>
33#include <openssl/evp.h>
34#include <openssl/md5.h>
35
36#define LOG_TAG "keystore"
37#include <cutils/log.h>
38#include <cutils/sockets.h>
39#include <private/android_filesystem_config.h>
40
41#include "keystore.h"
42
43/* KeyStore is a secured storage for key-value pairs. In this implementation,
44 * each file stores one key-value pair. Keys are encoded in file names, and
45 * values are encrypted with checksums. The encryption key is protected by a
46 * user-defined password. To keep things simple, buffers are always larger than
47 * the maximum space we needed, so boundary checks on buffers are omitted. */
48
Chia-chi Yeh4165dd22010-03-09 09:41:32 +080049#define KEY_SIZE ((NAME_MAX - 15) / 2)
Chia-chi Yehadbc99b2009-09-18 10:15:37 +080050#define VALUE_SIZE 32768
51#define PASSWORD_SIZE VALUE_SIZE
52
53/* Here is the encoding of keys. This is necessary in order to allow arbitrary
54 * characters in keys. Characters in [0-~] are not encoded. Others are encoded
55 * into two bytes. The first byte is one of [+-.] which represents the first
56 * two bits of the character. The second byte encodes the rest of the bits into
57 * [0-o]. Therefore in the worst case the length of a key gets doubled. Note
58 * that Base64 cannot be used here due to the need of prefix match on keys. */
59
60static int encode_key(char *out, uint8_t *in, int length)
61{
62 int i;
63 for (i = length; i > 0; --i, ++in, ++out) {
64 if (*in >= '0' && *in <= '~') {
65 *out = *in;
66 } else {
67 *out = '+' + (*in >> 6);
68 *++out = '0' + (*in & 0x3F);
69 ++length;
70 }
71 }
72 *out = 0;
73 return length;
74}
75
76static int decode_key(uint8_t *out, char *in, int length)
77{
78 int i;
79 for (i = 0; i < length; ++i, ++in, ++out) {
80 if (*in >= '0' && *in <= '~') {
81 *out = *in;
82 } else {
83 *out = (*in - '+') << 6;
84 *out |= (*++in - '0') & 0x3F;
85 --length;
86 }
87 }
88 *out = 0;
89 return length;
90}
91
92/* Here is the protocol used in both requests and responses:
93 * code [length_1 message_1 ... length_n message_n] end-of-file
94 * where code is one byte long and lengths are unsigned 16-bit integers in
95 * network order. Thus the maximum length of a message is 65535 bytes. */
96
97static int the_socket = -1;
98
99static int recv_code(int8_t *code)
100{
101 return recv(the_socket, code, 1, 0) == 1;
102}
103
104static int recv_message(uint8_t *message, int length)
105{
106 uint8_t bytes[2];
107 if (recv(the_socket, &bytes[0], 1, 0) != 1 ||
108 recv(the_socket, &bytes[1], 1, 0) != 1) {
109 return -1;
110 } else {
111 int offset = bytes[0] << 8 | bytes[1];
112 if (length < offset) {
113 return -1;
114 }
115 length = offset;
116 offset = 0;
117 while (offset < length) {
118 int n = recv(the_socket, &message[offset], length - offset, 0);
119 if (n <= 0) {
120 return -1;
121 }
122 offset += n;
123 }
124 }
125 return length;
126}
127
Chia-chi Yeh626c46b2009-09-20 10:15:46 +0800128static int recv_end_of_file()
129{
130 uint8_t byte;
131 return recv(the_socket, &byte, 1, 0) == 0;
132}
133
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800134static void send_code(int8_t code)
135{
136 send(the_socket, &code, 1, 0);
137}
138
139static void send_message(uint8_t *message, int length)
140{
141 uint16_t bytes = htons(length);
142 send(the_socket, &bytes, 2, 0);
143 send(the_socket, message, length, 0);
144}
145
Chia-chi Yeh25099762010-10-01 01:27:34 +0800146/* Here is the file format. There are two parts in blob.value, the secret and
147 * the description. The secret is stored in ciphertext, and its original size
148 * can be found in blob.length. The description is stored after the secret in
149 * plaintext, and its size is specified in blob.info. The total size of the two
150 * parts must be no more than VALUE_SIZE bytes. The first three bytes of the
151 * file are reserved for future use and are always set to zero. Fields other
152 * than blob.info, blob.length, and blob.value are modified by encrypt_blob()
153 * and decrypt_blob(). Thus they should not be accessed from outside. */
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800154
155static int the_entropy = -1;
156
157static struct __attribute__((packed)) {
Chia-chi Yeh25099762010-10-01 01:27:34 +0800158 uint8_t reserved[3];
159 uint8_t info;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800160 uint8_t vector[AES_BLOCK_SIZE];
161 uint8_t encrypted[0];
162 uint8_t digest[MD5_DIGEST_LENGTH];
163 uint8_t digested[0];
164 int32_t length;
165 uint8_t value[VALUE_SIZE + AES_BLOCK_SIZE];
166} blob;
167
168static int8_t encrypt_blob(char *name, AES_KEY *aes_key)
169{
170 uint8_t vector[AES_BLOCK_SIZE];
Chia-chi Yehfa4ae74e2009-12-22 17:02:45 +0800171 int length;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800172 int fd;
173
Chia-chi Yeh463d75b2010-09-30 17:28:01 +0800174 if (read(the_entropy, blob.vector, AES_BLOCK_SIZE) != AES_BLOCK_SIZE) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800175 return SYSTEM_ERROR;
176 }
177
Chia-chi Yeh25099762010-10-01 01:27:34 +0800178 length = blob.length + (blob.value - blob.encrypted);
Chia-chi Yeh5fe85982009-12-30 10:38:39 +0800179 length = (length + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE * AES_BLOCK_SIZE;
Chia-chi Yehfa4ae74e2009-12-22 17:02:45 +0800180
Chia-chi Yeh25099762010-10-01 01:27:34 +0800181 if (blob.info != 0) {
182 memmove(&blob.encrypted[length], &blob.value[blob.length], blob.info);
183 }
184
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800185 blob.length = htonl(blob.length);
Chia-chi Yeh5fe85982009-12-30 10:38:39 +0800186 MD5(blob.digested, length - (blob.digested - blob.encrypted), blob.digest);
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800187
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800188 memcpy(vector, blob.vector, AES_BLOCK_SIZE);
189 AES_cbc_encrypt(blob.encrypted, blob.encrypted, length, aes_key, vector,
190 AES_ENCRYPT);
191
Chia-chi Yeh25099762010-10-01 01:27:34 +0800192 memset(blob.reserved, 0, sizeof(blob.reserved));
193 length += (blob.encrypted - (uint8_t *)&blob) + blob.info;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800194
195 fd = open(".tmp", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
Chia-chi Yehfa4ae74e2009-12-22 17:02:45 +0800196 length -= write(fd, &blob, length);
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800197 close(fd);
Chia-chi Yehfa4ae74e2009-12-22 17:02:45 +0800198 return (length || rename(".tmp", name)) ? SYSTEM_ERROR : NO_ERROR;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800199}
200
201static int8_t decrypt_blob(char *name, AES_KEY *aes_key)
202{
203 int fd = open(name, O_RDONLY);
204 int length;
205
206 if (fd == -1) {
207 return (errno == ENOENT) ? KEY_NOT_FOUND : SYSTEM_ERROR;
208 }
209 length = read(fd, &blob, sizeof(blob));
210 close(fd);
211
Chia-chi Yeh25099762010-10-01 01:27:34 +0800212 length -= (blob.encrypted - (uint8_t *)&blob) + blob.info;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800213 if (length < blob.value - blob.encrypted || length % AES_BLOCK_SIZE != 0) {
214 return VALUE_CORRUPTED;
215 }
216
217 AES_cbc_encrypt(blob.encrypted, blob.encrypted, length, aes_key,
218 blob.vector, AES_DECRYPT);
219 length -= blob.digested - blob.encrypted;
Chia-chi Yehfa4ae74e2009-12-22 17:02:45 +0800220 if (memcmp(blob.digest, MD5(blob.digested, length, NULL),
221 MD5_DIGEST_LENGTH)) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800222 return VALUE_CORRUPTED;
223 }
224
225 length -= blob.value - blob.digested;
226 blob.length = ntohl(blob.length);
Chia-chi Yeh25099762010-10-01 01:27:34 +0800227 if (blob.length < 0 || blob.length > length) {
228 return VALUE_CORRUPTED;
229 }
230 if (blob.info != 0) {
231 memmove(&blob.value[blob.length], &blob.value[length], blob.info);
232 }
233 return NO_ERROR;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800234}
235
236/* Here are the actions. Each of them is a function without arguments. All
237 * information is defined in global variables, which are set properly before
238 * performing an action. The number of parameters required by each action is
Chia-chi Yehc4b14452009-09-18 17:23:53 +0800239 * fixed and defined in a table. If the return value of an action is positive,
240 * it will be treated as a response code and transmitted to the client. Note
241 * that the lengths of parameters are checked when they are received, so
242 * boundary checks on parameters are omitted. */
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800243
244#define MAX_PARAM 2
245#define MAX_RETRY 4
246
247static uid_t uid = -1;
248static int8_t state = UNINITIALIZED;
249static int8_t retry = MAX_RETRY;
250
251static struct {
252 int length;
253 uint8_t value[VALUE_SIZE];
254} params[MAX_PARAM];
255
256static AES_KEY encryption_key;
257static AES_KEY decryption_key;
258
259static int8_t test()
260{
261 return state;
262}
263
264static int8_t get()
265{
266 char name[NAME_MAX];
267 int n = sprintf(name, "%u_", uid);
268 encode_key(&name[n], params[0].value, params[0].length);
269 n = decrypt_blob(name, &decryption_key);
270 if (n != NO_ERROR) {
271 return n;
272 }
273 send_code(NO_ERROR);
274 send_message(blob.value, blob.length);
275 return -NO_ERROR;
276}
277
278static int8_t insert()
279{
280 char name[NAME_MAX];
281 int n = sprintf(name, "%u_", uid);
282 encode_key(&name[n], params[0].value, params[0].length);
Chia-chi Yeh25099762010-10-01 01:27:34 +0800283 blob.info = 0;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800284 blob.length = params[1].length;
285 memcpy(blob.value, params[1].value, params[1].length);
286 return encrypt_blob(name, &encryption_key);
287}
288
289static int8_t delete()
290{
291 char name[NAME_MAX];
292 int n = sprintf(name, "%u_", uid);
293 encode_key(&name[n], params[0].value, params[0].length);
294 return (unlink(name) && errno != ENOENT) ? SYSTEM_ERROR : NO_ERROR;
295}
296
297static int8_t exist()
298{
299 char name[NAME_MAX];
300 int n = sprintf(name, "%u_", uid);
301 encode_key(&name[n], params[0].value, params[0].length);
302 if (access(name, R_OK) == -1) {
303 return (errno != ENOENT) ? SYSTEM_ERROR : KEY_NOT_FOUND;
304 }
305 return NO_ERROR;
306}
307
Chia-chi Yeh1f680222009-09-22 02:57:52 +0800308static int8_t saw()
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800309{
310 DIR *dir = opendir(".");
311 struct dirent *file;
312 char name[NAME_MAX];
313 int n;
314
315 if (!dir) {
316 return SYSTEM_ERROR;
317 }
318 n = sprintf(name, "%u_", uid);
319 n += encode_key(&name[n], params[0].value, params[0].length);
320 send_code(NO_ERROR);
321 while ((file = readdir(dir)) != NULL) {
322 if (!strncmp(name, file->d_name, n)) {
323 char *p = &file->d_name[n];
324 params[0].length = decode_key(params[0].value, p, strlen(p));
325 send_message(params[0].value, params[0].length);
326 }
327 }
328 closedir(dir);
329 return -NO_ERROR;
330}
331
332static int8_t reset()
333{
334 DIR *dir = opendir(".");
335 struct dirent *file;
336
337 memset(&encryption_key, 0, sizeof(encryption_key));
338 memset(&decryption_key, 0, sizeof(decryption_key));
339 state = UNINITIALIZED;
340 retry = MAX_RETRY;
341
342 if (!dir) {
343 return SYSTEM_ERROR;
344 }
345 while ((file = readdir(dir)) != NULL) {
Chia-chi Yehc4b14452009-09-18 17:23:53 +0800346 unlink(file->d_name);
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800347 }
348 closedir(dir);
Chia-chi Yehc4b14452009-09-18 17:23:53 +0800349 return NO_ERROR;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800350}
351
352#define MASTER_KEY_FILE ".masterkey"
353#define MASTER_KEY_SIZE 16
Chia-chi Yeh25099762010-10-01 01:27:34 +0800354#define SALT_SIZE 16
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800355
Chia-chi Yeh25099762010-10-01 01:27:34 +0800356static void set_key(uint8_t *key, uint8_t *password, int length, uint8_t *salt)
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800357{
Chia-chi Yeh25099762010-10-01 01:27:34 +0800358 if (salt) {
359 PKCS5_PBKDF2_HMAC_SHA1((char *)password, length, salt, SALT_SIZE,
360 8192, MASTER_KEY_SIZE, key);
361 } else {
362 PKCS5_PBKDF2_HMAC_SHA1((char *)password, length, (uint8_t *)"keystore",
363 sizeof("keystore"), 1024, MASTER_KEY_SIZE, key);
364 }
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800365}
366
Chia-chi Yeh25099762010-10-01 01:27:34 +0800367/* Here is the history. To improve the security, the parameters to generate the
368 * master key has been changed. To make a seamless transition, we update the
369 * file using the same password when the user unlock it for the first time. If
370 * any thing goes wrong during the transition, the new file will not overwrite
371 * the old one. This avoids permanent damages of the existing data. */
372
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800373static int8_t password()
374{
375 uint8_t key[MASTER_KEY_SIZE];
376 AES_KEY aes_key;
Chia-chi Yeh25099762010-10-01 01:27:34 +0800377 int8_t response = SYSTEM_ERROR;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800378
379 if (state == UNINITIALIZED) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800380 if (read(the_entropy, blob.value, MASTER_KEY_SIZE) != MASTER_KEY_SIZE) {
381 return SYSTEM_ERROR;
382 }
383 } else {
Chia-chi Yeh25099762010-10-01 01:27:34 +0800384 int fd = open(MASTER_KEY_FILE, O_RDONLY);
385 uint8_t *salt = NULL;
386 if (fd != -1) {
387 int length = read(fd, &blob, sizeof(blob));
388 close(fd);
389 if (length > SALT_SIZE && blob.info == SALT_SIZE) {
390 salt = (uint8_t *)&blob + length - SALT_SIZE;
391 }
392 }
393
394 set_key(key, params[0].value, params[0].length, salt);
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800395 AES_set_decrypt_key(key, MASTER_KEY_SIZE * 8, &aes_key);
Chia-chi Yeh25099762010-10-01 01:27:34 +0800396 response = decrypt_blob(MASTER_KEY_FILE, &aes_key);
397 if (response == SYSTEM_ERROR) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800398 return SYSTEM_ERROR;
399 }
Chia-chi Yeh25099762010-10-01 01:27:34 +0800400 if (response != NO_ERROR || blob.length != MASTER_KEY_SIZE) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800401 if (retry <= 0) {
Chia-chi Yeh626c46b2009-09-20 10:15:46 +0800402 reset();
403 return UNINITIALIZED;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800404 }
405 return WRONG_PASSWORD + --retry;
406 }
Chia-chi Yeh25099762010-10-01 01:27:34 +0800407
408 if (!salt && params[1].length == -1) {
409 params[1] = params[0];
410 }
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800411 }
412
413 if (params[1].length == -1) {
414 memcpy(key, blob.value, MASTER_KEY_SIZE);
415 } else {
Chia-chi Yeh25099762010-10-01 01:27:34 +0800416 uint8_t *salt = &blob.value[MASTER_KEY_SIZE];
417 if (read(the_entropy, salt, SALT_SIZE) != SALT_SIZE) {
418 return SYSTEM_ERROR;
419 }
420
421 set_key(key, params[1].value, params[1].length, salt);
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800422 AES_set_encrypt_key(key, MASTER_KEY_SIZE * 8, &aes_key);
423 memcpy(key, blob.value, MASTER_KEY_SIZE);
Chia-chi Yeh25099762010-10-01 01:27:34 +0800424 blob.info = SALT_SIZE;
425 blob.length = MASTER_KEY_SIZE;
426 response = encrypt_blob(MASTER_KEY_FILE, &aes_key);
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800427 }
428
Chia-chi Yeh25099762010-10-01 01:27:34 +0800429 if (response == NO_ERROR) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800430 AES_set_encrypt_key(key, MASTER_KEY_SIZE * 8, &encryption_key);
431 AES_set_decrypt_key(key, MASTER_KEY_SIZE * 8, &decryption_key);
432 state = NO_ERROR;
433 retry = MAX_RETRY;
434 }
Chia-chi Yeh25099762010-10-01 01:27:34 +0800435 return response;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800436}
437
438static int8_t lock()
439{
440 memset(&encryption_key, 0, sizeof(encryption_key));
441 memset(&decryption_key, 0, sizeof(decryption_key));
442 state = LOCKED;
Chia-chi Yehc4b14452009-09-18 17:23:53 +0800443 return NO_ERROR;
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800444}
445
446static int8_t unlock()
447{
448 params[1].length = -1;
449 return password();
450}
451
452/* Here are the permissions, actions, users, and the main function. */
453
454enum perm {
455 TEST = 1,
456 GET = 2,
457 INSERT = 4,
458 DELETE = 8,
459 EXIST = 16,
Chia-chi Yeh1f680222009-09-22 02:57:52 +0800460 SAW = 32,
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800461 RESET = 64,
462 PASSWORD = 128,
463 LOCK = 256,
464 UNLOCK = 512,
465};
466
467static struct action {
468 int8_t (*run)();
469 int8_t code;
470 int8_t state;
471 uint32_t perm;
472 int lengths[MAX_PARAM];
473} actions[] = {
474 {test, 't', 0, TEST, {0}},
475 {get, 'g', NO_ERROR, GET, {KEY_SIZE}},
476 {insert, 'i', NO_ERROR, INSERT, {KEY_SIZE, VALUE_SIZE}},
Chia-chi Yeh626c46b2009-09-20 10:15:46 +0800477 {delete, 'd', 0, DELETE, {KEY_SIZE}},
478 {exist, 'e', 0, EXIST, {KEY_SIZE}},
Chia-chi Yeh1f680222009-09-22 02:57:52 +0800479 {saw, 's', 0, SAW, {KEY_SIZE}},
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800480 {reset, 'r', 0, RESET, {0}},
481 {password, 'p', 0, PASSWORD, {PASSWORD_SIZE, PASSWORD_SIZE}},
482 {lock, 'l', NO_ERROR, LOCK, {0}},
483 {unlock, 'u', LOCKED, UNLOCK, {PASSWORD_SIZE}},
484 {NULL, 0 , 0, 0, {0}},
485};
486
487static struct user {
488 uid_t uid;
489 uid_t euid;
490 uint32_t perms;
491} users[] = {
Chia-chi Yeh4165dd22010-03-09 09:41:32 +0800492 {AID_SYSTEM, ~0, ~GET},
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800493 {AID_VPN, AID_SYSTEM, GET},
494 {AID_WIFI, AID_SYSTEM, GET},
Chia-chi Yeh4165dd22010-03-09 09:41:32 +0800495 {AID_ROOT, AID_SYSTEM, GET},
Brian Carlstrom8c2a1a92011-04-08 13:44:08 -0700496 {AID_KEYCHAIN, AID_SYSTEM, TEST | GET | SAW},
Chia-chi Yeh4165dd22010-03-09 09:41:32 +0800497 {~0, ~0, TEST | GET | INSERT | DELETE | EXIST | SAW},
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800498};
499
500static int8_t process(int8_t code) {
501 struct user *user = users;
502 struct action *action = actions;
503 int i;
504
Chia-chi Yeh4165dd22010-03-09 09:41:32 +0800505 while (~user->uid && user->uid != uid) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800506 ++user;
507 }
508 while (action->code && action->code != code) {
509 ++action;
510 }
511 if (!action->code) {
512 return UNDEFINED_ACTION;
513 }
514 if (!(action->perm & user->perms)) {
515 return PERMISSION_DENIED;
516 }
517 if (action->state && action->state != state) {
518 return state;
519 }
Chia-chi Yeh4165dd22010-03-09 09:41:32 +0800520 if (~user->euid) {
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800521 uid = user->euid;
522 }
523 for (i = 0; i < MAX_PARAM && action->lengths[i]; ++i) {
524 params[i].length = recv_message(params[i].value, action->lengths[i]);
525 if (params[i].length == -1) {
526 return PROTOCOL_ERROR;
527 }
528 }
Chia-chi Yeh626c46b2009-09-20 10:15:46 +0800529 if (!recv_end_of_file()) {
530 return PROTOCOL_ERROR;
531 }
Chia-chi Yehadbc99b2009-09-18 10:15:37 +0800532 return action->run();
533}
534
535#define RANDOM_DEVICE "/dev/urandom"
536
537int main(int argc, char **argv)
538{
539 int control_socket = android_get_control_socket("keystore");
540 if (argc < 2) {
541 LOGE("A directory must be specified!");
542 return 1;
543 }
544 if (chdir(argv[1]) == -1) {
545 LOGE("chdir: %s: %s", argv[1], strerror(errno));
546 return 1;
547 }
548 if ((the_entropy = open(RANDOM_DEVICE, O_RDONLY)) == -1) {
549 LOGE("open: %s: %s", RANDOM_DEVICE, strerror(errno));
550 return 1;
551 }
552 if (listen(control_socket, 3) == -1) {
553 LOGE("listen: %s", strerror(errno));
554 return 1;
555 }
556
557 signal(SIGPIPE, SIG_IGN);
558 if (access(MASTER_KEY_FILE, R_OK) == 0) {
559 state = LOCKED;
560 }
561
562 while ((the_socket = accept(control_socket, NULL, 0)) != -1) {
563 struct timeval tv = {.tv_sec = 3};
564 struct ucred cred;
565 socklen_t size = sizeof(cred);
566 int8_t request;
567
568 setsockopt(the_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
569 setsockopt(the_socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
570
571 if (getsockopt(the_socket, SOL_SOCKET, SO_PEERCRED, &cred, &size)) {
572 LOGW("getsockopt: %s", strerror(errno));
573 } else if (recv_code(&request)) {
574 int8_t old_state = state;
575 int8_t response;
576 uid = cred.uid;
577
578 if ((response = process(request)) > 0) {
579 send_code(response);
580 response = -response;
581 }
582
583 LOGI("uid: %d action: %c -> %d state: %d -> %d retry: %d",
584 cred.uid, request, -response, old_state, state, retry);
585 }
586 close(the_socket);
587 }
588 LOGE("accept: %s", strerror(errno));
589 return 1;
590}