Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 1 | /* |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 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 | */ |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 16 | |
| 17 | #ifndef __KEYSTORE_GET_H__ |
| 18 | #define __KEYSTORE_GET_H__ |
| 19 | |
| 20 | #include <stdio.h> |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 21 | #include <stdint.h> |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/socket.h> |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 25 | |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 26 | #include <cutils/sockets.h> |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 27 | |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 28 | #define KEYSTORE_MESSAGE_SIZE 65535 |
| 29 | |
Chia-chi Yeh | 61b09cb | 2010-03-08 17:21:35 +0800 | [diff] [blame] | 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 34 | /* This function is provided for native components to get values from keystore. |
Chia-chi Yeh | c741a2f | 2010-09-30 15:17:58 +0800 | [diff] [blame] | 35 | * Users are required to link against libcutils. Keys and values are 8-bit safe. |
Chia-chi Yeh | 0df3c16 | 2010-03-17 16:31:20 +0800 | [diff] [blame] | 36 | * 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 Yeh | 61b09cb | 2010-03-08 17:21:35 +0800 | [diff] [blame] | 40 | static int keystore_get(const char *key, int length, char *value) |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 41 | { |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 42 | uint8_t bytes[2] = {length >> 8, length}; |
| 43 | uint8_t code = 'g'; |
| 44 | int sock; |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 45 | |
Chia-chi Yeh | 61b09cb | 2010-03-08 17:21:35 +0800 | [diff] [blame] | 46 | if (length < 0 || length > KEYSTORE_MESSAGE_SIZE) { |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 47 | return -1; |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 48 | } |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 49 | sock = socket_local_client("keystore", ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 50 | SOCK_STREAM); |
| 51 | if (sock == -1) { |
| 52 | return -1; |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 53 | } |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 54 | 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 Yeh | c741a2f | 2010-09-30 15:17:58 +0800 | [diff] [blame] | 68 | } else { |
| 69 | length = -1; |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 70 | } |
Chia-chi Yeh | c741a2f | 2010-09-30 15:17:58 +0800 | [diff] [blame] | 71 | |
Chia-chi Yeh | c4b1445 | 2009-09-18 17:23:53 +0800 | [diff] [blame] | 72 | close(sock); |
| 73 | return length; |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Chia-chi Yeh | 61b09cb | 2010-03-08 17:21:35 +0800 | [diff] [blame] | 76 | #ifdef __cplusplus |
| 77 | } |
| 78 | #endif |
| 79 | |
Chia-chi Yeh | fd70bed | 2009-07-06 11:13:25 +0800 | [diff] [blame] | 80 | #endif |