2
* Copyright (C) 2007 International Business Machines
3
* Author(s): Mike Halcrow <mhalcrow@us.ibm.com>
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33
#include "../include/ecryptfs.h"
35
static struct ecryptfs_key_mod_ops *
36
(*builtin_get_key_mod_ops[])(void) = {
37
&passphrase_get_key_mod_ops,
42
* ecryptfs_generate_sig_from_key_data
47
int ecryptfs_generate_sig_from_key_data(unsigned char *sig,
48
unsigned char *key_data,
54
memcpy(&key_type, key_data, sizeof(uint32_t));
55
key_type = ntohl(key_type);
65
static int ecryptfs_dummy_init(char **alias)
67
syslog(LOG_WARNING, "%s: Dummy function substituted for unimplemented "
68
"function in key module\n", __FUNCTION__);
73
static int ecryptfs_dummy_get_gen_key_params(struct key_mod_param **params,
76
if (ecryptfs_verbosity)
78
"%s: Dummy function substituted for unimplemented "
79
"function in key module\n", __FUNCTION__);
86
ecryptfs_dummy_get_gen_key_subgraph_trans_node(struct transition_node **trans,
89
if (ecryptfs_verbosity)
91
"%s: Dummy function substituted for unimplemented "
92
"function in key module\n", __FUNCTION__);
98
ecryptfs_dummy_get_params(struct key_mod_param **params, uint32_t *num_params)
100
if (ecryptfs_verbosity)
102
"%s: Dummy function substituted for unimplemented "
103
"function in key module\n", __FUNCTION__);
110
ecryptfs_dummy_get_param_subgraph_trans_node(struct transition_node **trans,
113
if (ecryptfs_verbosity)
115
"%s: Dummy function substituted for unimplemented "
116
"function in key module\n", __FUNCTION__);
121
static int ecryptfs_dummy_get_blob(unsigned char *blob, size_t *blob_size,
122
struct key_mod_param_val *param_vals,
123
uint32_t num_param_vals)
125
syslog(LOG_WARNING, "%s: Dummy function substituted for unimplemented "
126
"function in key module\n", __FUNCTION__);
132
ecryptfs_dummy_get_key_data(unsigned char *key_data, size_t *key_data_len,
135
if (ecryptfs_verbosity)
137
"%s: Dummy function substituted for unimplemented "
138
"function in key module\n", __FUNCTION__);
144
ecryptfs_dummy_get_key_sig(unsigned char *sig, unsigned char *blob)
146
if (ecryptfs_verbosity)
148
"%s: Dummy function substituted for unimplemented "
149
"function in key module\n", __FUNCTION__);
154
static int ecryptfs_dummy_get_key_hint(unsigned char *hint, size_t *hint_len,
157
if (ecryptfs_verbosity)
159
"%s: Dummy function substituted for unimplemented "
160
"function in key module\n", __FUNCTION__);
166
ecryptfs_dummy_encrypt(char *to, size_t *to_size, char *from, size_t from_size,
167
unsigned char *blob, int blob_type)
169
syslog(LOG_WARNING, "%s: Dummy function substituted for unimplemented "
170
"function in key module\n", __FUNCTION__);
176
ecryptfs_dummy_decrypt(char *to, size_t *to_size, char *from, size_t from_size,
177
unsigned char *blob, int blob_type)
179
syslog(LOG_WARNING, "%s: Dummy function substituted for unimplemented "
180
"function in key module\n", __FUNCTION__);
185
static int ecryptfs_dummy_destroy(unsigned char *blob)
187
if (ecryptfs_verbosity)
189
"%s: Dummy function substituted for unimplemented "
190
"function in key module\n", __FUNCTION__);
194
static int ecryptfs_dummy_finalize(void)
196
if (ecryptfs_verbosity)
198
"%s: Dummy function substituted for unimplemented "
199
"function in key module\n", __FUNCTION__);
203
int ecryptfs_fill_in_dummy_ops(struct ecryptfs_key_mod_ops *key_mod_ops)
205
if (!key_mod_ops->init)
206
key_mod_ops->init = &ecryptfs_dummy_init;
207
if (!key_mod_ops->get_gen_key_params)
208
key_mod_ops->get_gen_key_params =
209
&ecryptfs_dummy_get_gen_key_params;
210
if (!key_mod_ops->get_gen_key_subgraph_trans_node)
211
key_mod_ops->get_gen_key_subgraph_trans_node =
212
&ecryptfs_dummy_get_gen_key_subgraph_trans_node;
213
if (!key_mod_ops->get_params)
214
key_mod_ops->get_params = &ecryptfs_dummy_get_params;
215
if (!key_mod_ops->get_param_subgraph_trans_node)
216
key_mod_ops->get_param_subgraph_trans_node =
217
&ecryptfs_dummy_get_param_subgraph_trans_node;
218
if (!key_mod_ops->get_blob)
219
key_mod_ops->get_blob = &ecryptfs_dummy_get_blob;
220
if (!key_mod_ops->get_key_data)
221
key_mod_ops->get_key_data = &ecryptfs_dummy_get_key_data;
222
if (!key_mod_ops->get_key_sig)
223
key_mod_ops->get_key_sig = &ecryptfs_dummy_get_key_sig;
224
if (!key_mod_ops->get_key_hint)
225
key_mod_ops->get_key_hint = &ecryptfs_dummy_get_key_hint;
226
if (!key_mod_ops->encrypt)
227
key_mod_ops->encrypt = &ecryptfs_dummy_encrypt;
228
if (!key_mod_ops->decrypt)
229
key_mod_ops->decrypt = &ecryptfs_dummy_decrypt;
230
if (!key_mod_ops->destroy)
231
key_mod_ops->destroy = &ecryptfs_dummy_destroy;
232
if (!key_mod_ops->finalize)
233
key_mod_ops->finalize = &ecryptfs_dummy_finalize;
238
* Called from: src/libecryptfs/module_mgr.c::ecryptfs_process_decision_graph
240
int ecryptfs_register_key_modules(struct ecryptfs_ctx* ctx)
244
char *dir_name = NULL;
246
struct ecryptfs_key_mod *curr_key_mod = &(ctx->key_mod_list_head);
247
struct ecryptfs_key_mod_ops *(*walker)(void);
250
if (asprintf(&dir_name, "%s", ECRYPTFS_DEFAULT_KEY_MOD_DIR) == -1) {
254
if (!(dp = opendir(dir_name))) {
256
"ERROR: Could not open key_mod directory\n");
260
while ((ep = readdir(dp))) {
261
struct ecryptfs_key_mod *new_key_mod = NULL;
264
char *key_mod_dir = ECRYPTFS_DEFAULT_KEY_MOD_DIR;
266
struct ecryptfs_key_mod_ops *(*get_key_mod_ops)(void);
268
/* Check if file ends with .so */
269
dir_length = strlen(ep->d_name);
271
|| strcmp((ep->d_name + (dir_length - 3)), ".so"))
273
if (asprintf(&path, "%s/%s", key_mod_dir, ep->d_name) == -1) {
274
syslog(LOG_ERR, "Out of memory\n");
279
handle = dlopen(path, RTLD_LAZY);
281
syslog(LOG_ERR, "Could not open library handle\n");
284
get_key_mod_ops = (struct ecryptfs_key_mod_ops *(*)(void))
285
dlsym(handle, "get_key_mod_ops");
286
if (!get_key_mod_ops) {
287
syslog (LOG_ERR, "Error attempting to get the symbol "
288
"[get_key_mod_ops] from key module [%s]: "
289
"err = [%s]. The key module is likely using "
290
"the deprecated key module API.\n", path,
294
new_key_mod = malloc(sizeof(struct ecryptfs_key_mod));
296
syslog(LOG_ERR, "Out of memory\n");
301
memset(new_key_mod, 0, sizeof(struct ecryptfs_key_mod));
302
new_key_mod->ops = (get_key_mod_ops)();
303
if (!new_key_mod->ops) {
304
syslog (LOG_ERR, "Library function get_key_mod_ops() "
305
"failed to return ops for [%s]\n", path);
310
if ((rc = ecryptfs_fill_in_dummy_ops(new_key_mod->ops))) {
311
syslog (LOG_ERR, "Error attempting to fill in missing "
312
"key module operations for [%s]; rc = [%d]\n",
318
if ((rc = new_key_mod->ops->init(&new_key_mod->alias))) {
319
syslog(LOG_ERR, "Error initializing key module [%s]; "
320
"rc = [%d]\n", path, rc);
325
new_key_mod->lib_handle = handle;
326
new_key_mod->lib_path = path;
327
curr_key_mod->next = new_key_mod;
328
curr_key_mod = new_key_mod;
335
walker = builtin_get_key_mod_ops[i];
337
struct ecryptfs_key_mod *new_key_mod;
338
struct ecryptfs_key_mod *tmp_key_mod;
340
if (!(new_key_mod = malloc(sizeof(struct ecryptfs_key_mod)))) {
341
syslog(LOG_ERR, "Out of memory\n");
345
memset(new_key_mod, 0, sizeof(struct ecryptfs_key_mod));
346
new_key_mod->ops = (walker)();
347
if (!new_key_mod->ops) {
348
syslog (LOG_ERR, "Library function get_key_mod_ops() "
349
"failed to return ops for built-in key "
350
"module in array position [%d]\n", i);
355
if ((rc = new_key_mod->ops->init(&new_key_mod->alias))) {
356
syslog(LOG_ERR, "Error initializing key module in "
357
"array position [%d]\n", i);
362
tmp_key_mod = ctx->key_mod_list_head.next;
363
while (tmp_key_mod) {
364
if (strcmp(tmp_key_mod->alias, new_key_mod->alias)
366
free(new_key_mod->alias);
368
if (ecryptfs_verbosity)
370
"Preferring [%s] file over "
371
"built-in module for key module "
373
tmp_key_mod->lib_path,
377
tmp_key_mod = tmp_key_mod->next;
379
curr_key_mod->next = new_key_mod;
380
curr_key_mod = new_key_mod;
383
walker = builtin_get_key_mod_ops[i];
391
* ecryptfs_find_key_mod
393
* Get the key_mod struct for the given alias.
395
int ecryptfs_find_key_mod(struct ecryptfs_key_mod **key_mod,
396
struct ecryptfs_ctx *ctx, char *key_mod_alias)
398
struct ecryptfs_key_mod *curr;
401
curr = ctx->key_mod_list_head.next;
403
if (!strncmp(curr->alias, key_mod_alias,
404
strlen(curr->alias))) {
415
int ecryptfs_free_key_mod_list(struct ecryptfs_ctx *ctx)
417
struct ecryptfs_key_mod *curr = ctx->key_mod_list_head.next;
418
struct ecryptfs_key_mod *temp;
421
curr->ops->finalize();
422
dlclose (curr->lib_handle);
423
free(curr->lib_path);