1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <libintl.h>
#include <locale.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>
#include "tpmadm.h"
extern cmdtable_t commands[];
static void
print_usage(char *progname, cmdtable_t cmds[])
{
cmdtable_t *p;
(void) fprintf(stderr,
gettext("usage: %s command args ...\n"), progname);
(void) fprintf(stderr,
gettext("where 'command' is one of the following:\n"));
for (p = &cmds[0]; p->name != NULL; p++) {
(void) fprintf(stderr, "\t%s %s\n", p->name, p->args);
}
}
int
main(int argc, char *argv[])
{
char *progname;
cmdtable_t *p;
cmdfunc_t fptr = NULL;
int ret;
TSS_HCONTEXT hContext;
TSS_HOBJECT hTPM;
/* Set up for i18n/l10n. */
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D. */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't. */
#endif
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
progname = argv[0];
argc--;
argv++;
if (argc <= 0) {
print_usage(progname, commands);
return (ERR_USAGE);
}
for (p = &commands[0]; p->name != NULL; p++) {
if (0 == strcmp(p->name, argv[0])) {
fptr = p->func;
break;
}
}
if (fptr == NULL) {
print_usage(progname, commands);
return (ERR_USAGE);
}
if (tpm_preamble(&hContext, &hTPM))
return (ERR_FAIL);
ret = fptr(hContext, hTPM, argc, argv);
(void) tpm_postamble(hContext);
return (ret);
}
/*
* Utility functions
*/
void
print_bytes(BYTE *bytes, size_t len, int formatted)
{
int i;
for (i = 0; i < len; i++) {
(void) printf("%02X ", bytes[i]);
if (formatted && i % 16 == 7)
(void) printf(" ");
if (formatted && i % 16 == 15)
(void) printf("\n");
}
(void) printf("\n");
}
/*
* TSS convenience functions
*/
void
print_error(TSS_RESULT ret, char *msg)
{
char *err_string;
/* Print the standard error string and error code. */
err_string = Trspi_Error_String(ret);
(void) fprintf(stderr, "%s: %s (0x%0x)\n", msg, err_string, ret);
/* For a few special cases, add a more verbose error message. */
switch (ret) {
case TPM_E_DEACTIVATED:
case TPM_E_DISABLED:
(void) fprintf(stderr,
gettext("Enable the TPM and reboot.\n"));
break;
case TSP_ERROR(TSS_E_COMM_FAILURE):
(void) fprintf(stderr,
gettext("Make sure the tcsd service "
"(svc:/application/security/tcsd) is running.\n"));
break;
}
}
int
get_tpm_capability(TSS_HCONTEXT hContext, TSS_HOBJECT hTPM, UINT32 cap,
UINT32 subcap, void *buf, size_t bufsize)
{
TSS_RESULT ret;
UINT32 datalen;
BYTE *data;
ret = Tspi_TPM_GetCapability(hTPM, cap, sizeof (subcap),
(BYTE *)&subcap, &datalen, &data);
if (ret) {
print_error(ret, gettext("Get TPM capability"));
return (ERR_FAIL);
}
if (datalen > bufsize) {
(void) fprintf(stderr,
gettext("Capability 0x%x returned %u bytes "
"(expected %u)\n"), cap, datalen, bufsize);
return (ERR_FAIL);
}
bcopy(data, buf, datalen);
ret = Tspi_Context_FreeMemory(hContext, data);
if (ret) {
print_error(ret, gettext("Free capability buffer"));
return (ERR_FAIL);
}
return (0);
}
int
set_policy_options(TSS_HPOLICY hPolicy, TSS_FLAG mode, char *prompt,
UINT32 secret_len, BYTE *secret)
{
TSS_RESULT ret;
BYTE *unicode_prompt;
UINT32 len;
ret = Tspi_Policy_SetSecret(hPolicy, mode, secret_len, secret);
if (ret) {
print_error(ret, gettext("Set policy secret"));
return (ERR_FAIL);
}
if (prompt != NULL) {
unicode_prompt = Trspi_Native_To_UNICODE((BYTE *)prompt, &len);
ret = Tspi_SetAttribData(hPolicy,
TSS_TSPATTRIB_POLICY_POPUPSTRING,
0, len, unicode_prompt);
if (ret) {
print_error(ret, gettext("Set policy prompt"));
return (ERR_FAIL);
}
}
return (0);
}
int
set_object_policy(TSS_HOBJECT handle, TSS_FLAG mode, char *prompt,
UINT32 secret_len, BYTE *secret)
{
TSS_HPOLICY hPolicy;
TSS_RESULT ret;
ret = Tspi_GetPolicyObject(handle, TSS_POLICY_USAGE, &hPolicy);
if (ret) {
print_error(ret, gettext("Get object policy"));
return (ERR_FAIL);
}
return (set_policy_options(hPolicy, mode, prompt, secret_len, secret));
}
int
tpm_preamble(TSS_HCONTEXT *hContext, TSS_HOBJECT *hTPM)
{
TSS_RESULT ret;
ret = Tspi_Context_Create(hContext);
if (ret) {
print_error(ret, gettext("Create context"));
return (ERR_FAIL);
}
ret = Tspi_Context_Connect(*hContext, NULL);
if (ret) {
print_error(ret, gettext("Connect context"));
(void) Tspi_Context_Close(*hContext);
return (ERR_FAIL);
}
ret = Tspi_Context_GetTpmObject(*hContext, hTPM);
if (ret) {
print_error(ret, gettext("Get TPM object"));
(void) Tspi_Context_Close(*hContext);
return (ERR_FAIL);
}
return (0);
}
int
tpm_postamble(TSS_HCONTEXT hContext)
{
TSS_RESULT ret;
ret = Tspi_Context_Close(hContext);
if (ret) {
print_error(ret, gettext("Close context"));
return (ERR_FAIL);
}
return (0);
}
|