printkey.c (3396B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #include "cli/command.h" 5 #include "cli/common.h" 6 #include "cofyc/argparse.h" 7 8 static int cmd_printkey(int argc, const char **argv) { 9 const char *keyFile = NULL; 10 const char *format = "hdr"; 11 const char *outFile = NULL; 12 int pubOnly = 0; 13 struct KeyPair *kp; 14 struct Format *fmt; 15 struct KeyPair out; 16 FILE *fout = stdout; 17 size_t encoded_length; 18 char *encoded; 19 int result; 20 21 static const char *const usages[] = { 22 "supercop printkey [options]", 23 NULL, 24 }; 25 struct argparse_option options[] = { 26 OPT_HELP(), 27 OPT_STRING('k', "key-file", &keyFile, "Select key file to use for the operation", NULL, 0, 0), 28 OPT_STRING('f', "format", &format, "Output format (0x00, asc, hdr, defaults to hdr)", NULL, 0, 0), 29 OPT_STRING('o', "out", &outFile, "Write output to file instead of stdout", NULL, 0, 0), 30 OPT_BOOLEAN(0, "public-only", &pubOnly, "Output only the public half of the key", NULL, 0, 0), 31 OPT_END(), 32 }; 33 34 struct argparse argparse; 35 argparse_init(&argparse, options, usages, ARGPARSE_STOP_AT_NON_OPTION); 36 argparse_describe(&argparse, "\nPrint contents of key file", NULL); 37 argparse_parse(&argparse, argc, argv); 38 39 if (!keyFile) { 40 fprintf(stderr, "Missing required argument: key-file\n\n"); 41 argparse_usage(&argparse); 42 return 1; 43 } 44 45 kp = readKeyFile(keyFile); 46 if (!kp || !kp->public_key) { 47 fprintf(stderr, "Could not decode key file: unknown or invalid format\n"); 48 if (kp) keypair_free(kp); 49 return 1; 50 } 51 52 fmt = supercop_find_format(format); 53 if (!fmt) { 54 fprintf(stderr, "Unknown format: %s\n", format); 55 keypair_free(kp); 56 return 1; 57 } 58 59 if (outFile) { 60 fout = fopen(outFile, "w+"); 61 if (!fout) { 62 fprintf(stderr, "Could not open output file\n"); 63 keypair_free(kp); 64 return 1; 65 } 66 } 67 68 // Shallow copy: encoders never take ownership, priv stripped on request 69 out = *kp; 70 if (pubOnly) out.private_key = NULL; 71 72 encoded = fmt->encode(&out, &encoded_length); 73 if (!encoded) { 74 fprintf(stderr, "Error while encoding key\n"); 75 if (outFile && fout) fclose(fout); 76 keypair_free(kp); 77 return 1; 78 } 79 fwrite(encoded, 1, encoded_length, fout); 80 if (encoded) free(encoded); 81 82 result = 0; 83 if (outFile && fout) fclose(fout); 84 keypair_free(kp); 85 return result; 86 } 87 88 void __attribute__((constructor)) cmd_printkey_setup(void) { 89 struct cmd_struct *cmd = calloc(1, sizeof(struct cmd_struct)); 90 if (!cmd) { 91 fprintf(stderr, "Failed to allocate memory for printkey command\n"); 92 return; 93 } 94 cmd->next = commands; 95 cmd->fn = cmd_printkey; 96 static const char *printkey_names[] = {"printkey", NULL}; 97 cmd->name = printkey_names; 98 cmd->display = "printkey"; 99 cmd->description = "Print contents of key file"; 100 cmd->help_text = 101 "supercop printkey - Print contents of key file\n" 102 "\n" 103 "Usage:\n" 104 " supercop printkey [options]\n" 105 "\n" 106 "Options:\n" 107 " -k, --key-file <path> Select key file to use for the operation\n" 108 " -f, --format <name> Output format: 0x00, asc, hdr (default hdr)\n" 109 " -o, --out <path> Write output to file instead of stdout\n" 110 " --public-only Output only the public half of the key\n"; 111 commands = cmd; 112 }