sbkeysync: Add --keystore and --no-default-keystores options

Add a couple of options to configure the location we read keys from

Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
This commit is contained in:
Jeremy Kerr 2012-08-21 11:59:42 +08:00
parent a151ffdb9d
commit add8d00f31

View file

@ -79,7 +79,7 @@ struct efi_sigdb_desc efi_sigdb_descs[] = {
{ SIGDB_DBX, "dbx", EFI_IMAGE_SECURITY_DATABASE_GUID }, { SIGDB_DBX, "dbx", EFI_IMAGE_SECURITY_DATABASE_GUID },
}; };
static const char *keystore_roots[] = { static const char *default_keystore_dirs[] = {
"/etc/secureboot/keys", "/etc/secureboot/keys",
"/usr/share/secureboot/keys", "/usr/share/secureboot/keys",
}; };
@ -125,6 +125,8 @@ struct sync_context {
struct key_database *db; struct key_database *db;
struct key_database *dbx; struct key_database *dbx;
struct keystore *keystore; struct keystore *keystore;
const char **keystore_dirs;
unsigned int n_keystore_dirs;
bool verbose; bool verbose;
}; };
@ -494,8 +496,8 @@ static int read_keystore(struct sync_context *ctx)
keystore = talloc(ctx, struct keystore); keystore = talloc(ctx, struct keystore);
list_head_init(&keystore->keys); list_head_init(&keystore->keys);
for (i = 0; i < ARRAY_SIZE(keystore_roots); i++) { for (i = 0; i < ctx->n_keystore_dirs; i++) {
update_keystore(keystore, keystore_roots[i]); update_keystore(keystore, ctx->keystore_dirs[i]);
} }
ctx->keystore = keystore; ctx->keystore = keystore;
@ -518,6 +520,8 @@ static struct option options[] = {
{ "version", no_argument, NULL, 'V' }, { "version", no_argument, NULL, 'V' },
{ "efivars-path", required_argument, NULL, 'e' }, { "efivars-path", required_argument, NULL, 'e' },
{ "verbose", no_argument, NULL, 'v' }, { "verbose", no_argument, NULL, 'v' },
{ "no-default-keystores", no_argument, NULL, 'd' },
{ "keystore", required_argument, NULL, 'k' },
{ NULL, 0, NULL, 0 }, { NULL, 0, NULL, 0 },
}; };
@ -528,8 +532,14 @@ static void usage(void)
"\n" "\n"
"Options:\n" "Options:\n"
"\t--efivars-path <dir> Path to efivars mountpoint\n" "\t--efivars-path <dir> Path to efivars mountpoint\n"
" (or regular directory for testing)\n" "\t (or regular directory for testing)\n"
"\t--verbose Print verbose progress information\n", "\t--verbose Print verbose progress information\n"
"\t--keystore <dir> Read keys from <dir>/{db,dbx,KEK}/*\n"
"\t (can be specified multiple times,\n"
"\t first dir takes precedence)\n"
"\t--no-default-keystores\n"
"\t Don't read keys from the default\n"
"\t keystore dirs\n",
toolname); toolname);
} }
@ -538,15 +548,26 @@ static void version(void)
printf("%s %s\n", toolname, VERSION); printf("%s %s\n", toolname, VERSION);
} }
static void add_keystore_dir(struct sync_context *ctx, const char *dir)
{
ctx->keystore_dirs = talloc_realloc(ctx, ctx->keystore_dirs,
const char *, ++ctx->n_keystore_dirs);
ctx->keystore_dirs[ctx->n_keystore_dirs - 1] =
talloc_strdup(ctx->keystore_dirs, dir);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
bool use_default_keystore_dirs;
struct sync_context *ctx; struct sync_context *ctx;
use_default_keystore_dirs = true;
ctx = talloc_zero(NULL, struct sync_context); ctx = talloc_zero(NULL, struct sync_context);
for (;;) { for (;;) {
int idx, c; int idx, c;
c = getopt_long(argc, argv, "e:vhV", options, &idx); c = getopt_long(argc, argv, "e:dkvhV", options, &idx);
if (c == -1) if (c == -1)
break; break;
@ -554,6 +575,12 @@ int main(int argc, char **argv)
case 'e': case 'e':
ctx->efivars_dir = optarg; ctx->efivars_dir = optarg;
break; break;
case 'd':
use_default_keystore_dirs = false;
break;
case 'k':
add_keystore_dir(ctx, optarg);
break;
case 'v': case 'v':
ctx->verbose = true; ctx->verbose = true;
break; break;
@ -584,6 +611,13 @@ int main(int argc, char **argv)
} }
} }
if (use_default_keystore_dirs) {
unsigned int i;
for (i = 0; i < ARRAY_SIZE(default_keystore_dirs); i++)
add_keystore_dir(ctx, default_keystore_dirs[i]);
}
read_key_databases(ctx); read_key_databases(ctx);
read_keystore(ctx); read_keystore(ctx);