blob: 4b4923e5c736f2a10796b9b01dba4cbaaa670258 [file] [log] [blame]
Chia-chi Yehfd70bed2009-07-06 11:13:25 +08001/*
Chia-chi Yehc4b14452009-09-18 17:23:53 +08002 * 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 */
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080016
17#ifndef __KEYSTORE_GET_H__
18#define __KEYSTORE_GET_H__
19
20#include <stdio.h>
Chia-chi Yehc4b14452009-09-18 17:23:53 +080021#include <stdint.h>
Chia-chi Yehc4b14452009-09-18 17:23:53 +080022#include <unistd.h>
23#include <sys/types.h>
24#include <sys/socket.h>
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080025
Chia-chi Yehc4b14452009-09-18 17:23:53 +080026#include <cutils/sockets.h>
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080027
Chia-chi Yehc4b14452009-09-18 17:23:53 +080028#define KEYSTORE_MESSAGE_SIZE 65535
29
Chia-chi Yeh61b09cb2010-03-08 17:21:35 +080030#ifdef __cplusplus
31extern "C" {
32#endif
33
Chia-chi Yehc4b14452009-09-18 17:23:53 +080034/* This function is provided for native components to get values from keystore.
Chia-chi Yehc741a2f2010-09-30 15:17:58 +080035 * Users are required to link against libcutils. Keys and values are 8-bit safe.
Chia-chi Yeh0df3c162010-03-17 16:31:20 +080036 * The first two arguments are the key and its length. The third argument
37 * specifies the buffer to store the retrieved value, which must be an array of
38 * KEYSTORE_MESSAGE_SIZE bytes. This function returns the length of the value or
39 * -1 if an error happens. */
Chia-chi Yeh61b09cb2010-03-08 17:21:35 +080040static int keystore_get(const char *key, int length, char *value)
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080041{
Chia-chi Yehc4b14452009-09-18 17:23:53 +080042 uint8_t bytes[2] = {length >> 8, length};
43 uint8_t code = 'g';
44 int sock;
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080045
Chia-chi Yeh61b09cb2010-03-08 17:21:35 +080046 if (length < 0 || length > KEYSTORE_MESSAGE_SIZE) {
Chia-chi Yehc4b14452009-09-18 17:23:53 +080047 return -1;
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080048 }
Chia-chi Yehc4b14452009-09-18 17:23:53 +080049 sock = socket_local_client("keystore", ANDROID_SOCKET_NAMESPACE_RESERVED,
50 SOCK_STREAM);
51 if (sock == -1) {
52 return -1;
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080053 }
Chia-chi Yehc4b14452009-09-18 17:23:53 +080054 if (send(sock, &code, 1, 0) == 1 && send(sock, bytes, 2, 0) == 2 &&
55 send(sock, key, length, 0) == length && shutdown(sock, SHUT_WR) == 0 &&
56 recv(sock, &code, 1, 0) == 1 && code == /* NO_ERROR */ 1 &&
57 recv(sock, &bytes[0], 1, 0) == 1 && recv(sock, &bytes[1], 1, 0) == 1) {
58 int offset = 0;
59 length = bytes[0] << 8 | bytes[1];
60 while (offset < length) {
61 int n = recv(sock, &value[offset], length - offset, 0);
62 if (n <= 0) {
63 length = -1;
64 break;
65 }
66 offset += n;
67 }
Chia-chi Yehc741a2f2010-09-30 15:17:58 +080068 } else {
69 length = -1;
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080070 }
Chia-chi Yehc741a2f2010-09-30 15:17:58 +080071
Chia-chi Yehc4b14452009-09-18 17:23:53 +080072 close(sock);
73 return length;
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080074}
75
Chia-chi Yeh61b09cb2010-03-08 17:21:35 +080076#ifdef __cplusplus
77}
78#endif
79
Chia-chi Yehfd70bed2009-07-06 11:13:25 +080080#endif