xref: /netbsd-src/crypto/external/cpl/tpm-tools/dist/src/tpm_mgmt/tpm_resetdalock.c (revision 431955c163a358f3111f7be0c1fa1643cab0b701)
1 /*
2  * The Initial Developer of the Original Code is International
3  * Business Machines Corporation. Portions created by IBM
4  * Corporation are Copyright (C) 2005, 2007 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 
22 #include "tpm_tspi.h"
23 #include "tpm_utils.h"
24 
25 
26 static BOOL isWellKnown = FALSE;
27 TSS_HCONTEXT hContext = 0;
28 
29 
help(const char * aCmd)30 static void help(const char* aCmd)
31 {
32 	logCmdHelp(aCmd);
33 	logCmdOption("-z, --well-known", _("Use TSS_WELL_KNOWN_SECRET (20 zero bytes) as the owner secret."));
34 }
35 
parse(const int aOpt,const char * aArg)36 static int parse(const int aOpt, const char *aArg)
37 {
38 
39 	switch (aOpt) {
40 	case 'z':
41 		isWellKnown = TRUE;
42 		break;
43 	default:
44 		return -1;
45 	}
46 
47 	return 0;
48 }
49 
50 int
main(int argc,char ** argv)51 main( int argc, char **argv )
52 {
53 	char *szTpmPasswd = NULL;
54 	int tpm_len;
55 	TSS_HTPM hTpm;
56 	TSS_HPOLICY hTpmPolicy;
57 	TSS_BOOL bValue = TRUE;
58 	int iRc = -1;
59 	struct option opts[] = {
60 		{"well-known", no_argument, NULL, 'z'},
61 	};
62 	BYTE wellKnown[TCPA_SHA1_160_HASH_LEN] = TSS_WELL_KNOWN_SECRET;
63 
64 	initIntlSys();
65 
66 	if (genericOptHandler(argc, argv, "z", opts, sizeof(opts) / sizeof(struct option), parse,
67 			      help) != 0)
68 		goto out;
69 
70 	if (contextCreate(&hContext) != TSS_SUCCESS)
71 		goto out;
72 
73 	if (!isWellKnown) {
74 		// Prompt for owner password
75 		szTpmPasswd = GETPASSWD(_("Enter owner password: "), &tpm_len, FALSE);
76 		if (!szTpmPasswd) {
77 			logError(_("Failed to get Owner password\n"));
78 			goto out;
79 		}
80 	} else {
81 		szTpmPasswd = (char *)wellKnown;
82 		tpm_len = sizeof(wellKnown);
83 	}
84 
85 	if (contextConnect(hContext) != TSS_SUCCESS)
86 		goto out_close;
87 
88 	if (contextGetTpm(hContext, &hTpm) != TSS_SUCCESS)
89 		goto out_close;
90 
91 	if (policyGet(hTpm, &hTpmPolicy) != TSS_SUCCESS)
92 		goto out_close;
93 
94 	if (policySetSecret(hTpmPolicy, tpm_len, (BYTE *)szTpmPasswd) != TSS_SUCCESS)
95 		goto out_close;
96 
97 	if (tpmSetStatus(hTpm, TSS_TPMSTATUS_RESETLOCK, bValue) != TSS_SUCCESS)
98 		goto out_close;
99 
100 	iRc = 0;
101 	logSuccess(argv[0]);
102 
103 	out_close:
104 		contextClose(hContext);
105 
106 	out:
107 	if (!isWellKnown && szTpmPasswd)
108 		shredPasswd(szTpmPasswd);
109 
110 	return iRc;
111 }
112