xref: /netbsd-src/crypto/external/cpl/tpm-tools/dist/src/tpm_mgmt/tpm_setoperatorauth.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 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_utils.h"
23 #include "tpm_tspi.h"
24 
25 static BOOL passUnicode = FALSE;
26 static BOOL isWellKnown = FALSE;
27 TSS_HCONTEXT hContext = 0;
28 
help(const char * aCmd)29 static void help(const char *aCmd)
30 {
31 	logCmdHelp(aCmd);
32 	logUnicodeCmdOption();
33 	logCmdOption("-z, --well-known", _("Use TSS_WELL_KNOWN_SECRET as the operator's default secret."));
34 	logCmdOption("-p, --op_password_unicode", _("Use TSS UNICODE encoding for operator password to comply with applications using TSS popup boxes"));
35 }
36 
parse(const int aOpt,const char * aArg)37 static int parse(const int aOpt, const char *aArg)
38 {
39 
40 	switch (aOpt) {
41 	case 'p':
42 		passUnicode = TRUE;
43 		break;
44 	case 'z':
45 		isWellKnown = TRUE;
46 		break;
47 	default:
48 		return -1;
49 	}
50 	return 0;
51 }
52 
53 static TSS_RESULT
tpmSetOpAuth(TSS_HTPM a_hTpm,TSS_HPOLICY aOpPolicy)54 tpmSetOpAuth(TSS_HTPM a_hTpm, TSS_HPOLICY aOpPolicy)
55 {
56 	TSS_RESULT result = Tspi_TPM_SetOperatorAuth(a_hTpm, aOpPolicy);
57 	tspiResult("Tspi_TPM_SetOperatorAuth", result);
58 	return result;
59 }
60 
main(int argc,char ** argv)61 int main(int argc, char **argv)
62 {
63 
64 	int iRc = -1;
65 	char *passwd = NULL;
66 	int pswd_len;
67 	TSS_HPOLICY hNewPolicy;
68 	TSS_HTPM hTpm;
69 	struct option opts[] = {
70 	{"well-known", no_argument, NULL, 'z'},
71 	{"op_password_unicode", no_argument, NULL, 'p'},
72 	};
73 	BYTE wellKnown[TCPA_SHA1_160_HASH_LEN] = TSS_WELL_KNOWN_SECRET;
74 
75 	initIntlSys();
76 	if (genericOptHandler
77 	    (argc, argv, "zp", opts, sizeof(opts) / sizeof(struct option),
78 	     parse, help) != 0)
79 		goto out;
80 
81 	//Connect to TSS and TPM
82 	if (contextCreate(&hContext) != TSS_SUCCESS)
83 		goto out;
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 	//Prompt for operator password
92 	if (!isWellKnown) {
93 		passwd = _GETPASSWD(_("Enter operator password: "), (int *)&pswd_len, TRUE,
94 				    passUnicode || useUnicode );
95 		if (!passwd) {
96 			logError(_("Failed to get operator password\n"));
97 			goto out_close;
98 		}
99 	} else {
100 		passwd = (char *)wellKnown;
101 		pswd_len = sizeof(wellKnown);
102 	}
103 
104 	if (contextCreateObject(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_OPERATOR,
105 			&hNewPolicy) != TSS_SUCCESS)
106 		goto out_close;
107 
108 	if (policySetSecret(hNewPolicy, (UINT32)pswd_len, (BYTE *)passwd) != TSS_SUCCESS)
109 		goto out_close;
110 
111 	if (!isWellKnown)
112 		shredPasswd(passwd);
113 	passwd = NULL;
114 
115 	if (tpmSetOpAuth(hTpm, hNewPolicy) != TSS_SUCCESS)
116 		goto out_close;
117 
118 	iRc = 0;
119 	out_close:
120 	contextClose(hContext);
121 	out:
122 	return iRc;
123 }
124