replicant-frameworks_native/cmds/keystore/netkeystore.h
Chung-yih Wang c186c66a29 Implement the generic mini-keystore for security.
1. We will progressively migrate to this implementation.
2. For richc to have a quick review on the keymgmt part.
3. Add remove_key and make sure all functions are working.
4. Add permission check for get operation.
5. Return the retry count if unlock failed.
6. Add the reset operation for keystore reset.
7. Add the putfile shell command for putting the key value from file.
8. Fix the boundary issue during parsing command.
9. Use the ' ' as delimiter and reset the reply structure for each request.
10. Add change password retry count check.
11. Extract the read_marshal/write_marshal for certtool.h.
12. Remove the old implementation.
2009-07-02 14:08:20 +08:00

97 lines
2.3 KiB
C

/*
**
** Copyright 2009, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#ifndef __NETKEYSTORE_H__
#define __NETKEYSTORE_H__
#include <stdio.h>
#include <cutils/sockets.h>
#include <cutils/log.h>
#include "common.h"
static inline int readx(int s, void *_buf, int count)
{
char *buf = _buf;
int n = 0, r;
if (count < 0) return -1;
while (n < count) {
r = read(s, buf + n, count - n);
if (r < 0) {
if (errno == EINTR) continue;
LOGE("read error: %s\n", strerror(errno));
return -1;
}
if (r == 0) {
LOGE("eof\n");
return -1; /* EOF */
}
n += r;
}
return 0;
}
static inline int writex(int s, const void *_buf, int count)
{
const char *buf = _buf;
int n = 0, r;
if (count < 0) return -1;
while (n < count) {
r = write(s, buf + n, count - n);
if (r < 0) {
if (errno == EINTR) continue;
LOGE("write error: %s\n", strerror(errno));
return -1;
}
n += r;
}
return 0;
}
static inline int read_marshal(int s, LPC_MARSHAL *cmd)
{
if (readx(s, cmd, 2 * sizeof(uint32_t))) {
LOGE("failed to read header\n");
return -1;
}
if (cmd->len > BUFFER_MAX) {
LOGE("invalid size %d\n", cmd->len);
return -1;
}
if (readx(s, cmd->data, cmd->len)) {
LOGE("failed to read data\n");
return -1;
}
cmd->data[cmd->len] = 0;
return 0;
}
static inline int write_marshal(int s, LPC_MARSHAL *cmd)
{
if (writex(s, cmd, 2 * sizeof(uint32_t))) {
LOGE("failed to write marshal header\n");
return -1;
}
if (writex(s, cmd->data, cmd->len)) {
LOGE("failed to write marshal data\n");
return -1;
}
return 0;
}
#endif