blob: 7cd316b0fd61f9ce259b5f582f606cf5ab861f61 [file] [log] [blame]
Chung-yih Wang70246eb2009-06-29 03:12:56 +08001/*
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
29/*
30 * The specific function 'get_cert' is used in daemons to get the key value
31 * from keystore. Caller should allocate the buffer and the length of the buffer
32 * should be MAX_KEY_VALUE_LENGTH.
33 */
34static inline int get_cert(char *certname, unsigned char *value, int *size)
35{
36 int count, fd, ret = -1;
37 LPC_MARSHAL cmd;
38 char delimiter[] = "_";
39 char *namespace, *keyname;
40 char *context = NULL;
41
42 if (value == NULL) {
43 LOGE("get_cert: value is null\n");
44 return -1;
45 }
46
47 fd = socket_local_client(SOCKET_PATH,
48 ANDROID_SOCKET_NAMESPACE_RESERVED,
49 SOCK_STREAM);
50 if (fd == -1) {
51 LOGE("Keystore service is not up and running.\n");
52 return -1;
53 }
54
55 cmd.opcode = GET;
56 if (((namespace = strtok_r(certname, delimiter, &context)) == NULL) ||
57 ((keyname = strtok_r(NULL, delimiter, &context)) == NULL)) {
58 goto err;
59 }
60 if ((cmd.len = snprintf((char*)cmd.data, BUFFER_MAX, "%s %s", namespace, keyname))
61 > (2 * MAX_KEY_NAME_LENGTH + 1)) goto err;
62
63 if (write_marshal(fd, &cmd)) {
64 LOGE("Incorrect command or command line is too long.\n");
65 goto err;
66 }
67 if (read_marshal(fd, &cmd)) {
68 LOGE("Failed to read the result.\n");
69 goto err;
70 }
71
72 // copy the result if succeeded.
73 if (!cmd.retcode && cmd.len <= BUFFER_MAX) {
74 memcpy(value, cmd.data, cmd.len);
75 ret = 0;
76 *size = cmd.len;
77 }
78err:
79 close(fd);
80 return ret;
81}
82
83#endif