printkey.c (4950B)
1 #include <cofyc/argparse.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 #include "common.h" 6 #include "registry.h" 7 #include "sfinx.h" 8 #include "util/file.h" 9 10 static int cmd_printkey(int argc, const char **argv) { 11 const char *key_file = NULL; 12 const char *out_file = NULL; 13 const char *format = "hdr"; 14 const char *in_format = NULL; 15 int hash = 0; 16 int public_only = 0; 17 int single = 0; 18 int force = 0; 19 int path_len = -1; 20 uint8_t *data = NULL; 21 uint8_t *out = NULL; 22 size_t data_len = 0; 23 size_t out_len = 0; 24 sfinx_key key; 25 sfinx_status status; 26 int rc = 0; 27 28 static const char *const usages[] = { 29 "sfinx printkey -k file [-L hash] [-p len] [--in-fmt fmt] [--out-fmt fmt] [--single] [-o file] [--public-only] " 30 "[--force]", 31 NULL, 32 }; 33 struct argparse_option options[] = { 34 OPT_HELP(), 35 OPT_STRING('k', "key-file", &key_file, "key file to read", NULL, 0, 0), 36 OPT_STRING(0, "out-fmt", &format, "output format (default hdr)", NULL, 0, 0), 37 OPT_STRING(0, "in-fmt", &in_format, "input format (default auto)", NULL, 0, 0), 38 OPT_INTEGER('L', "hash", &hash, "hash size (224/256/384/512)", NULL, 0, 0), 39 OPT_INTEGER('p', "path-len", &path_len, "path length to derive the public key for", NULL, 0, 0), 40 OPT_STRING('o', "out", &out_file, "write to a file instead of stdout", NULL, 0, 0), 41 OPT_BOOLEAN(0, "public-only", &public_only, "output only the public key", NULL, 0, 0), 42 OPT_BOOLEAN(0, "single", &single, "single-key mode, derive the N = 0 public key", NULL, 0, 0), 43 OPT_BOOLEAN(0, "force", &force, "overwrite an existing file", NULL, 0, 0), 44 OPT_END(), 45 }; 46 struct argparse argparse; 47 argparse_init(&argparse, options, usages, 0); 48 argparse_describe(&argparse, "\nPrint or convert a key file", NULL); 49 argc = argparse_parse(&argparse, argc, argv); 50 51 if (argc > 0) { 52 fprintf(stderr, "sfinx: printkey: unexpected argument: %s\n", argv[0]); 53 return 1; 54 } 55 if (!key_file) { 56 fprintf(stderr, "sfinx: printkey: missing --key-file\n"); 57 return 1; 58 } 59 if (single && path_len != -1) { 60 fprintf(stderr, "sfinx: printkey: --single cannot be combined with --path-len\n"); 61 return 1; 62 } 63 if (path_len != -1 && (path_len <= 0 || !sfinx_path_len_valid((size_t)path_len))) { 64 fprintf(stderr, "sfinx: printkey: invalid path length: %d\n", path_len); 65 return 1; 66 } 67 if (util_file_read(key_file, &data, &data_len) != 0) { 68 fprintf(stderr, "sfinx: printkey: cannot read %s\n", key_file); 69 return 1; 70 } 71 72 status = sfinx_key_decode(data, data_len, in_format, &key); 73 free(data); 74 if (status != SFINX_OK) { 75 fprintf(stderr, "sfinx: printkey: cannot decode %s: %s\n", key_file, sfinx_strerror(status)); 76 return 1; 77 } 78 if (hash != 0) { 79 key.hash = (sfinx_hash)hash; 80 } 81 if (sfinx_hash_len(key.hash) == 0) { 82 fprintf(stderr, "sfinx: printkey: invalid hash size: %d\n", (int)key.hash); 83 sfinx_key_free(&key); 84 return 1; 85 } 86 if (path_len != -1) { 87 key.path_len = (ssize_t)path_len; 88 } 89 status = cli_key_derive(&key, single, key.path_len); 90 if (status != SFINX_OK) { 91 fprintf(stderr, "sfinx: printkey: cannot derive public key: %s\n", sfinx_strerror(status)); 92 sfinx_key_free(&key); 93 return 1; 94 } 95 if (public_only) { 96 free(key.seed); 97 key.seed = NULL; 98 key.seed_len = 0; 99 } 100 101 if (!sfinx_format_find(format)) { 102 fprintf(stderr, "sfinx: printkey: unknown format: %s\n", format); 103 sfinx_key_free(&key); 104 return 1; 105 } 106 out_len = sfinx_key_encode_len(&key, format); 107 if (out_len == 0) { 108 fprintf(stderr, "sfinx: printkey: cannot encode key as %s\n", format); 109 sfinx_key_free(&key); 110 return 1; 111 } 112 out = malloc(out_len); 113 if (!out) { 114 fprintf(stderr, "sfinx: printkey: out of memory\n"); 115 sfinx_key_free(&key); 116 return 1; 117 } 118 status = sfinx_key_encode(&key, format, out, out_len, &out_len); 119 if (status != SFINX_OK) { 120 fprintf(stderr, "sfinx: printkey: cannot encode: %s\n", sfinx_strerror(status)); 121 rc = 1; 122 } else if (out_file) { 123 if (util_file_write(out_file, out, out_len, key.seed ? 0600 : 0644, force) != 0) { 124 fprintf(stderr, "sfinx: printkey: cannot write %s\n", out_file); 125 rc = 1; 126 } 127 } else if (fwrite(out, 1, out_len, stdout) != out_len) { 128 rc = 1; 129 } 130 free(out); 131 sfinx_key_free(&key); 132 return rc; 133 } 134 135 static struct cli_command printkey_command = { 136 .names = (const char *const[]){"printkey", NULL}, 137 .display = "printkey", 138 .description = "Print or convert a key file", 139 .help = 140 "sfinx printkey -k file [-L hash] [--in-fmt fmt] [--out-fmt fmt] [--single] [-o file] [--public-only] " 141 "[--force]", 142 .fn = cmd_printkey, 143 }; 144 145 __attribute__((constructor)) static void printkey_register(void) { 146 cli_command_register(&printkey_command); 147 }