1a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root/*
2a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * Copyright (C) 2009 The Android Open Source Project
3a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *
4a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * Licensed under the Apache License, Version 2.0 (the "License");
5a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * you may not use this file except in compliance with the License.
6a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * You may obtain a copy of the License at
7a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *
8a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *      http://www.apache.org/licenses/LICENSE-2.0
9a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root *
10a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * Unless required by applicable law or agreed to in writing, software
11a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * distributed under the License is distributed on an "AS IS" BASIS,
12a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * See the License for the specific language governing permissions and
14a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * limitations under the License.
15a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root */
16a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
17a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#ifndef __KEYSTORE_GET_H__
18a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#define __KEYSTORE_GET_H__
19a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
20a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <stdio.h>
21a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <stdint.h>
22a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <unistd.h>
23a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <sys/types.h>
24a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <sys/socket.h>
25a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
26a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#include <cutils/sockets.h>
27a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
28a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#define KEYSTORE_MESSAGE_SIZE 65535
29a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
30a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#ifdef __cplusplus
31a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Rootextern "C" {
32a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#endif
33a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
34a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root/* This function is provided for native components to get values from keystore.
35a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * Users are required to link against libcutils. Keys and values are 8-bit safe.
36a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * The first two arguments are the key and its length. The third argument
37a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * specifies the buffer to store the retrieved value, which must be an array of
38a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * KEYSTORE_MESSAGE_SIZE bytes. This function returns the length of the value or
39a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root * -1 if an error happens. */
40a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Rootstatic int keystore_get(const char *key, int length, char *value)
41a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root{
42a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    uint8_t bytes[2] = {length >> 8, length};
43a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    uint8_t code = 'g';
44a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    int sock;
45a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
46a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    if (length < 0 || length > KEYSTORE_MESSAGE_SIZE) {
47a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        return -1;
48a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
49a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    sock = socket_local_client("keystore", ANDROID_SOCKET_NAMESPACE_RESERVED,
50a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root                               SOCK_STREAM);
51a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    if (sock == -1) {
52a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        return -1;
53a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
54a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    if (send(sock, &code, 1, 0) == 1 && send(sock, bytes, 2, 0) == 2 &&
55a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        send(sock, key, length, 0) == length && shutdown(sock, SHUT_WR) == 0 &&
56a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        recv(sock, &code, 1, 0) == 1 && code == /* NO_ERROR */ 1 &&
57a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        recv(sock, &bytes[0], 1, 0) == 1 && recv(sock, &bytes[1], 1, 0) == 1) {
58a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        int offset = 0;
59a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        length = bytes[0] << 8 | bytes[1];
60a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        while (offset < length) {
61a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            int n = recv(sock, &value[offset], length - offset, 0);
62a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            if (n <= 0) {
63a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root                length = -1;
64a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root                break;
65a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            }
66a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root            offset += n;
67a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        }
68a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    } else {
69a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root        length = -1;
70a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    }
71a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
72a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    close(sock);
73a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root    return length;
74a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root}
75a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
76a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#ifdef __cplusplus
77a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root}
78a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#endif
79a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root
80a91203b08350b2fc7efda5b1eab39e7541476b3aKenny Root#endif
81