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 __KEYSTORE_GET_H__ 19#define __KEYSTORE_GET_H__ 20 21#include <stdio.h> 22#include <stdlib.h> 23#include <string.h> 24 25#include "certtool.h" 26 27/* This function is provided to native components to get values from keystore. 28 * Users are required to link against libcutils. If something goes wrong, NULL 29 * is returned. Otherwise it returns the value in dynamically allocated memory 30 * and sets the size if the pointer is not NULL. One can release the memory by 31 * calling free(). */ 32static char *keystore_get(const char *key, int *size) 33{ 34 char buffer[MAX_KEY_VALUE_LENGTH]; 35 char *value; 36 int length; 37 38 if (get_cert(key, (unsigned char *)buffer, &length) != 0) { 39 return NULL; 40 } 41 value = malloc(length + 1); 42 if (!value) { 43 return NULL; 44 } 45 memcpy(value, buffer, length); 46 value[length] = 0; 47 if (size) { 48 *size = length; 49 } 50 return value; 51} 52 53#endif 54