|
| 1 | +/* |
| 2 | + * The Initial Developer of the Original Code is International |
| 3 | + * Business Machines Corporation. Portions created by IBM |
| 4 | + * Corporation are Copyright (C) 2009 International Business |
| 5 | + * Machines Corporation. All Rights Reserved. |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the Common Public License as published by |
| 9 | + * IBM Corporation; either version 1 of the License, or (at your option) |
| 10 | + * any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * Common Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the Common Public License |
| 18 | + * along with this program; if not, a copy can be viewed at |
| 19 | + * http://www.opensource.org/licenses/cpl1.0.php. |
| 20 | + */ |
| 21 | +#include <limits.h> |
| 22 | +#include "tpm_tspi.h" |
| 23 | +#include "tpm_utils.h" |
| 24 | +#include "tpm_unseal.h" |
| 25 | + |
| 26 | +static void help(const char *aCmd) |
| 27 | +{ |
| 28 | + logCmdHelp(aCmd); |
| 29 | + logCmdOption("-i, --infile FILE", |
| 30 | + _ |
| 31 | + ("Filename containing data to unseal.")); |
| 32 | + logCmdOption("-o, --outfile FILE", |
| 33 | + _ |
| 34 | + ("Filename to write unsealed data to. Default is STDOUT.")); |
| 35 | +} |
| 36 | + |
| 37 | +static char in_filename[PATH_MAX] = "", out_filename[PATH_MAX] = ""; |
| 38 | + |
| 39 | +static int parse(const int aOpt, const char *aArg) |
| 40 | +{ |
| 41 | + int rc = -1; |
| 42 | + |
| 43 | + switch (aOpt) { |
| 44 | + case 'i': |
| 45 | + if (aArg) { |
| 46 | + strncpy(in_filename, aArg, PATH_MAX); |
| 47 | + rc = 0; |
| 48 | + } |
| 49 | + break; |
| 50 | + case 'o': |
| 51 | + if (aArg) { |
| 52 | + strncpy(out_filename, aArg, PATH_MAX); |
| 53 | + rc = 0; |
| 54 | + } |
| 55 | + break; |
| 56 | + default: |
| 57 | + break; |
| 58 | + } |
| 59 | + return rc; |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +int main(int argc, char **argv) |
| 64 | +{ |
| 65 | + |
| 66 | + struct option opts[] = |
| 67 | + { {"infile", required_argument, NULL, 'i'}, |
| 68 | + {"outfile", required_argument, NULL, 'o'}, |
| 69 | + }; |
| 70 | + FILE *fp; |
| 71 | + int rc=0, tss_size=0, i; |
| 72 | + unsigned char* tss_data = NULL; |
| 73 | + |
| 74 | + |
| 75 | + if (genericOptHandler(argc, argv, "i:o", opts, |
| 76 | + sizeof(opts) / sizeof(struct option), parse, |
| 77 | + help) != 0) |
| 78 | + return rc; |
| 79 | + |
| 80 | + rc = tpmUnsealFile(in_filename, &tss_data, &tss_size); |
| 81 | + |
| 82 | + if (strlen(out_filename) == 0) { |
| 83 | + printf("\n----\n"); |
| 84 | + for (i=0; i < tss_size; i++) |
| 85 | + printf("%c", tss_data[i]); |
| 86 | + printf("\n----\n"); |
| 87 | + free(tss_data); |
| 88 | + return rc; |
| 89 | + } else if ((fp = fopen(out_filename, "w")) == NULL) { |
| 90 | + logError(_("Unable to open output file")); |
| 91 | + return rc; |
| 92 | + } |
| 93 | + |
| 94 | + fwrite(tss_data, tss_size, 1, fp); |
| 95 | + fclose(fp); |
| 96 | + free(tss_data); |
| 97 | + return rc; |
| 98 | +} |
0 commit comments