1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
** Copyright 2008, 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.
*/
#include "keystore.h"
static DIR *open_keystore(const char *dir)
{
DIR *d;
if ((d = opendir(dir)) == NULL) {
if (mkdir(dir, 0770) < 0) {
LOGE("cannot create dir '%s': %s\n", dir, strerror(errno));
unlink(dir);
return NULL;
}
d = open_keystore(dir);
}
return d;
}
static int list_files(const char *dir, char reply[REPLY_MAX]) {
struct dirent *de;
DIR *d;
if ((d = open_keystore(dir)) == NULL) {
return -1;
}
reply[0]=0;
while ((de = readdir(d))) {
if (de->d_type != DT_REG) continue;
strlcat(reply, " ", REPLY_MAX);
if (strlcat(reply, de->d_name, REPLY_MAX) >= REPLY_MAX) {
LOGE("reply is too long(too many files under '%s'\n", dir);
return -1;
}
}
closedir(d);
return 0;
}
static int copy_keyfile(const char *keystore, const char *srcfile) {
int srcfd, dstfd;
int length;
char buf[2048];
char dstfile[KEYNAME_LENGTH];
const char *filename = strrchr(srcfile, '/');
strlcpy(dstfile, keystore, KEYNAME_LENGTH);
strlcat(dstfile, "/", KEYNAME_LENGTH);
if (strlcat(dstfile, filename ? filename + 1 : srcfile,
KEYNAME_LENGTH) >= KEYNAME_LENGTH) {
LOGE("keyname is too long '%s'\n", srcfile);
return -1;
}
if ((srcfd = open(srcfile, O_RDONLY)) == -1) {
LOGE("Cannot open the original file '%s'\n", srcfile);
return -1;
}
if ((dstfd = open(dstfile, O_CREAT|O_RDWR)) == -1) {
LOGE("Cannot open the destination file '%s'\n", dstfile);
return -1;
}
while((length = read(srcfd, buf, 2048)) > 0) {
write(dstfd, buf, length);
}
close(srcfd);
close(dstfd);
chmod(dstfile, 0440);
return 0;
}
static int install_key(const char *dir, const char *keyfile)
{
struct dirent *de;
DIR *d;
if ((d = open_keystore(dir)) == NULL) {
return -1;
}
return copy_keyfile(dir, keyfile);
}
static int remove_key(const char *dir, const char *keyfile)
{
char dstfile[KEYNAME_LENGTH];
strlcpy(dstfile, dir, KEYNAME_LENGTH);
strlcat(dstfile, "/", KEYNAME_LENGTH);
if (strlcat(dstfile, keyfile, KEYNAME_LENGTH) >= KEYNAME_LENGTH) {
LOGE("keyname is too long '%s'\n", keyfile);
return -1;
}
if (unlink(dstfile)) {
LOGE("cannot delete '%s': %s\n", dstfile, strerror(errno));
return -1;
}
return 0;
}
int list_certs(char reply[REPLY_MAX])
{
return list_files(CERTS_DIR, reply);
}
int list_userkeys(char reply[REPLY_MAX])
{
return list_files(USERKEYS_DIR, reply);
}
int install_cert(const char *certfile)
{
return install_key(CERTS_DIR, certfile);
}
int install_userkey(const char *keyfile)
{
return install_key(USERKEYS_DIR, keyfile);
}
int remove_cert(const char *certfile)
{
return remove_key(CERTS_DIR, certfile);
}
int remove_userkey(const char *keyfile)
{
return remove_key(USERKEYS_DIR, keyfile);
}
|