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 26static 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 logCmdOption("-z, --srk-well-known", 36 _ 37 ("Use 20 bytes of zeros (TSS_WELL_KNOWN_SECRET) as the SRK secret.")); 38} 39 40static char in_filename[PATH_MAX] = "", out_filename[PATH_MAX] = ""; 41static BOOL srkWellKnown = FALSE; 42 43static int parse(const int aOpt, const char *aArg) 44{ 45 int rc = -1; 46 47 switch (aOpt) { 48 case 'i': 49 if (aArg) { 50 strncpy(in_filename, aArg, PATH_MAX); 51 rc = 0; 52 } 53 break; 54 case 'o': 55 if (aArg) { 56 strncpy(out_filename, aArg, PATH_MAX); 57 rc = 0; 58 } 59 break; 60 case 'z': 61 srkWellKnown = TRUE; 62 rc = 0; 63 break; 64 default: 65 break; 66 } 67 return rc; 68 69} 70 71int main(int argc, char **argv) 72{ 73 74 struct option opts[] = 75 { {"infile", required_argument, NULL, 'i'}, 76 {"outfile", required_argument, NULL, 'o'}, 77 {"srk-well-known", no_argument, NULL, 'z'}, 78 }; 79 FILE *fp; 80 int rc=0, tss_size=0, i; 81 unsigned char* tss_data = NULL; 82 83 if (genericOptHandler(argc, argv, "i:o:z", opts, 84 sizeof(opts) / sizeof(struct option), parse, 85 help) != 0) 86 return rc; 87 88 rc = tpmUnsealFile(in_filename, &tss_data, &tss_size, srkWellKnown); 89 90 if (strlen(out_filename) == 0) { 91 for (i=0; i < tss_size; i++) 92 printf("%c", tss_data[i]); 93 goto out; 94 } else if ((fp = fopen(out_filename, "w")) == NULL) { 95 logError(_("Unable to open output file\n")); 96 goto out; 97 } 98 99 if (fwrite(tss_data, tss_size, 1, fp) != 1) { 100 logError(_("Unable to write output file\n")); 101 goto out; 102 } 103 fclose(fp); 104out: 105 free(tss_data); 106 return rc; 107} 108