Chung-yih Wang | 70246eb | 2009-06-29 03:12:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2009, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef __CERTTOOL_H__ |
| 19 | #define __CERTTOOL_H__ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <cutils/sockets.h> |
| 24 | #include <cutils/log.h> |
| 25 | |
| 26 | #include "common.h" |
| 27 | #include "netkeystore.h" |
| 28 | |
Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 29 | #define CERT_NAME_LEN (2 * MAX_KEY_NAME_LENGTH + 2) |
| 30 | |
Chung-yih Wang | 70246eb | 2009-06-29 03:12:56 +0800 | [diff] [blame] | 31 | /* |
| 32 | * The specific function 'get_cert' is used in daemons to get the key value |
| 33 | * from keystore. Caller should allocate the buffer and the length of the buffer |
| 34 | * should be MAX_KEY_VALUE_LENGTH. |
| 35 | */ |
Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 36 | static inline int get_cert(const char *certname, unsigned char *value, int *size) |
Chung-yih Wang | 70246eb | 2009-06-29 03:12:56 +0800 | [diff] [blame] | 37 | { |
| 38 | int count, fd, ret = -1; |
| 39 | LPC_MARSHAL cmd; |
| 40 | char delimiter[] = "_"; |
| 41 | char *namespace, *keyname; |
| 42 | char *context = NULL; |
Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 43 | char cname[CERT_NAME_LEN]; |
Chung-yih Wang | 70246eb | 2009-06-29 03:12:56 +0800 | [diff] [blame] | 44 | |
Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 45 | if ((certname == NULL) || (value == NULL)) { |
| 46 | LOGE("get_cert: certname or value is null\n"); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | if (strlcpy(cname, certname, CERT_NAME_LEN) >= CERT_NAME_LEN) { |
| 51 | LOGE("get_cert: keyname is too long\n"); |
Chung-yih Wang | 70246eb | 2009-06-29 03:12:56 +0800 | [diff] [blame] | 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | fd = socket_local_client(SOCKET_PATH, |
| 56 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 57 | SOCK_STREAM); |
| 58 | if (fd == -1) { |
| 59 | LOGE("Keystore service is not up and running.\n"); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | cmd.opcode = GET; |
Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 64 | if (((namespace = strtok_r(cname, delimiter, &context)) == NULL) || |
Chung-yih Wang | 70246eb | 2009-06-29 03:12:56 +0800 | [diff] [blame] | 65 | ((keyname = strtok_r(NULL, delimiter, &context)) == NULL)) { |
| 66 | goto err; |
| 67 | } |
| 68 | if ((cmd.len = snprintf((char*)cmd.data, BUFFER_MAX, "%s %s", namespace, keyname)) |
| 69 | > (2 * MAX_KEY_NAME_LENGTH + 1)) goto err; |
| 70 | |
| 71 | if (write_marshal(fd, &cmd)) { |
| 72 | LOGE("Incorrect command or command line is too long.\n"); |
| 73 | goto err; |
| 74 | } |
| 75 | if (read_marshal(fd, &cmd)) { |
| 76 | LOGE("Failed to read the result.\n"); |
| 77 | goto err; |
| 78 | } |
| 79 | |
| 80 | // copy the result if succeeded. |
| 81 | if (!cmd.retcode && cmd.len <= BUFFER_MAX) { |
| 82 | memcpy(value, cmd.data, cmd.len); |
| 83 | ret = 0; |
| 84 | *size = cmd.len; |
| 85 | } |
| 86 | err: |
| 87 | close(fd); |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | #endif |