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