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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* Copyright (C) 2012 Red Hat Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* Author: Stef Walter <stefw@redhat.com>
*/
#include "config.h"
#include "asn1.h"
#include "attrs.h"
#include "builder.h"
#include "compat.h"
#define P11_DEBUG_FLAG P11_DEBUG_TRUST
#include "debug.h"
#include "errno.h"
#include "message.h"
#include "module.h"
#include "parser.h"
#include "pkcs11.h"
#include "pkcs11x.h"
#include "token.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _p11_token {
p11_parser *parser;
p11_index *index;
p11_builder *builder;
char *path;
char *label;
CK_SLOT_ID slot;
int loaded;
};
static int
loader_load_file (p11_token *token,
const char *filename,
struct stat *sb,
int flags)
{
int ret;
ret = p11_parse_file (token->parser, filename, flags);
switch (ret) {
case P11_PARSE_SUCCESS:
p11_debug ("loaded: %s", filename);
return 1;
case P11_PARSE_UNRECOGNIZED:
p11_debug ("skipped: %s", filename);
return 0;
default:
p11_debug ("failed to parse: %s", filename);
return 0;
}
}
static int
loader_load_directory (p11_token *token,
const char *directory,
int flags)
{
struct dirent *dp;
struct stat sb;
char *path;
int total = 0;
int ret;
DIR *dir;
/* First we load all the modules */
dir = opendir (directory);
if (!dir) {
p11_message ("couldn't list directory: %s: %s",
directory, strerror (errno));
return 0;
}
/* We're within a global mutex, so readdir is safe */
while ((dp = readdir (dir)) != NULL) {
path = strconcat (directory, "/", dp->d_name, NULL);
return_val_if_fail (path != NULL, -1);
if (stat (path, &sb) < 0) {
p11_message ("couldn't stat path: %s", path);
} else if (!S_ISDIR (sb.st_mode)) {
ret = loader_load_file (token, path, &sb, flags);
return_val_if_fail (ret >= 0, ret);
total += ret;
}
free (path);
}
closedir (dir);
return total;
}
static int
loader_load_subdirectory (p11_token *token,
const char *directory,
const char *subdir,
int flags)
{
struct stat sb;
char *path;
int ret = 0;
if (asprintf (&path, "%s/%s", directory, subdir) < 0)
return_val_if_reached (-1);
if (stat (path, &sb) >= 0 && S_ISDIR (sb.st_mode))
ret = loader_load_directory (token, path, flags);
free (path);
return ret;
}
static int
loader_load_path (p11_token *token,
const char *path)
{
struct stat sb;
int total;
int ret;
if (stat (path, &sb) < 0) {
if (errno == ENOENT) {
p11_message ("trust certificate path does not exist: %s",
path);
} else {
p11_message ("cannot access trust certificate path: %s: %s",
path, strerror (errno));
}
return 0;
}
if (S_ISDIR (sb.st_mode)) {
total = 0;
ret = loader_load_subdirectory (token, path, "anchors", P11_PARSE_FLAG_ANCHOR);
return_val_if_fail (ret >= 0, ret);
total += ret;
ret = loader_load_subdirectory (token, path, "blacklist", P11_PARSE_FLAG_BLACKLIST);
return_val_if_fail (ret >= 0, ret);
total += ret;
ret = loader_load_directory (token, path, P11_PARSE_FLAG_NONE);
return_val_if_fail (ret >= 0, ret);
total += ret;
return total;
} else {
return loader_load_file (token, path, &sb, P11_PARSE_FLAG_ANCHOR);
}
}
static int
load_builtin_objects (p11_token *token)
{
CK_OBJECT_CLASS builtin = CKO_NSS_BUILTIN_ROOT_LIST;
CK_BBOOL vtrue = CK_TRUE;
CK_BBOOL vfalse = CK_FALSE;
CK_RV rv;
const char *trust_anchor_roots = "Trust Anchor Roots";
CK_ATTRIBUTE builtin_root_list[] = {
{ CKA_CLASS, &builtin, sizeof (builtin) },
{ CKA_TOKEN, &vtrue, sizeof (vtrue) },
{ CKA_PRIVATE, &vfalse, sizeof (vfalse) },
{ CKA_MODIFIABLE, &vfalse, sizeof (vfalse) },
{ CKA_LABEL, (void *)trust_anchor_roots, strlen (trust_anchor_roots) },
{ CKA_INVALID },
};
p11_index_batch (token->index);
rv = p11_index_take (token->index, p11_attrs_dup (builtin_root_list), NULL);
return_val_if_fail (rv == CKR_OK, 0);
p11_index_finish (token->index);
return 1;
}
int
p11_token_load (p11_token *token)
{
int builtins;
int count;
if (token->loaded)
return 0;
token->loaded = 1;
builtins = load_builtin_objects (token);
count = loader_load_path (token, token->path);
return_val_if_fail (count >= 0, count);
return count + builtins;
}
void
p11_token_free (p11_token *token)
{
if (!token)
return;
p11_index_free (token->index);
p11_parser_free (token->parser);
p11_builder_free (token->builder);
free (token->path);
free (token->label);
free (token);
}
p11_token *
p11_token_new (CK_SLOT_ID slot,
const char *path,
const char *label)
{
p11_token *token;
return_val_if_fail (path != NULL, NULL);
return_val_if_fail (label != NULL, NULL);
token = calloc (1, sizeof (p11_token));
return_val_if_fail (token != NULL, NULL);
token->builder = p11_builder_new (P11_BUILDER_FLAG_TOKEN);
return_val_if_fail (token->builder != NULL, NULL);
token->index = p11_index_new (p11_builder_build,
p11_builder_changed,
token->builder);
return_val_if_fail (token->index != NULL, NULL);
token->parser = p11_parser_new (token->index,
p11_builder_get_cache (token->builder));
return_val_if_fail (token->parser != NULL, NULL);
token->path = strdup (path);
return_val_if_fail (token->path != NULL, NULL);
token->label = strdup (label);
return_val_if_fail (token->label != NULL, NULL);
token->slot = slot;
token->loaded = 0;
p11_debug ("token: %s: %s", token->label, token->path);
return token;
}
const char *
p11_token_get_label (p11_token *token)
{
return_val_if_fail (token != NULL, NULL);
return token->label;
}
const char *
p11_token_get_path (p11_token *token)
{
return_val_if_fail (token != NULL, NULL);
return token->path;
}
CK_SLOT_ID
p11_token_get_slot (p11_token *token)
{
return_val_if_fail (token != NULL, 0);
return token->slot;
}
p11_index *
p11_token_index (p11_token *token)
{
return_val_if_fail (token != NULL, NULL);
return token->index;
}
|