1207753Smm/*
2207753Smm * The Initial Developer of the Original Code is International
3207753Smm * Business Machines Corporation. Portions created by IBM
4207753Smm * Corporation are Copyright (C) 2009 International Business
5207753Smm * Machines Corporation. All Rights Reserved.
6207753Smm *
7207753Smm * This program is free software; you can redistribute it and/or modify
8207753Smm * it under the terms of the Common Public License as published by
9207753Smm * IBM Corporation; either version 1 of the License, or (at your option)
10207753Smm * any later version.
11207753Smm *
12207753Smm * This program is distributed in the hope that it will be useful,
13207753Smm * but WITHOUT ANY WARRANTY; without even the implied warranty of
14207753Smm * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15207753Smm * Common Public License for more details.
16207753Smm *
17207753Smm * You should have received a copy of the Common Public License
18207753Smm * along with this program; if not, a copy can be viewed at
19207753Smm * http://www.opensource.org/licenses/cpl1.0.php.
20207753Smm */
21207753Smm#include <limits.h>
22207753Smm#include "tpm_tspi.h"
23207753Smm#include "tpm_utils.h"
24207753Smm#include "tpm_unseal.h"
25207753Smm
26207753Smmstatic void help(const char *aCmd)
27207753Smm{
28207753Smm	logCmdHelp(aCmd);
29207753Smm	logCmdOption("-i, --infile FILE",
30207753Smm		     _
31207753Smm		     ("Filename containing data to unseal."));
32207753Smm	logCmdOption("-o, --outfile FILE",
33207753Smm		     _
34207753Smm		     ("Filename to write unsealed data to.  Default is STDOUT."));
35207753Smm	logCmdOption("-z, --srk-well-known",
36207753Smm		     _
37207753Smm		     ("Use 20 bytes of zeros (TSS_WELL_KNOWN_SECRET) as the SRK secret."));
38207753Smm}
39207753Smm
40207753Smmstatic char in_filename[PATH_MAX] = "", out_filename[PATH_MAX] = "";
41207753Smmstatic BOOL srkWellKnown = FALSE;
42207753Smm
43207753Smmstatic int parse(const int aOpt, const char *aArg)
44207753Smm{
45292588Sdelphij	int rc = -1;
46207753Smm
47207753Smm	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