4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
36 #include <trousers/trousers.h>
39 extern cmdtable_t commands
[];
42 print_usage(char *progname
, cmdtable_t cmds
[])
46 (void) fprintf(stderr
,
47 gettext("usage: %s command args ...\n"), progname
);
48 (void) fprintf(stderr
,
49 gettext("where 'command' is one of the following:\n"));
50 for (p
= &cmds
[0]; p
->name
!= NULL
; p
++) {
51 (void) fprintf(stderr
, "\t%s %s\n", p
->name
, p
->args
);
56 main(int argc
, char *argv
[])
60 cmdfunc_t fptr
= NULL
;
62 TSS_HCONTEXT hContext
;
65 /* Set up for i18n/l10n. */
66 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D. */
67 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't. */
69 (void) setlocale(LC_ALL
, "");
70 (void) textdomain(TEXT_DOMAIN
);
77 print_usage(progname
, commands
);
81 for (p
= &commands
[0]; p
->name
!= NULL
; p
++) {
82 if (0 == strcmp(p
->name
, argv
[0])) {
88 print_usage(progname
, commands
);
92 if (tpm_preamble(&hContext
, &hTPM
))
94 ret
= fptr(hContext
, hTPM
, argc
, argv
);
95 (void) tpm_postamble(hContext
);
106 print_bytes(BYTE
*bytes
, size_t len
, int formatted
)
109 for (i
= 0; i
< len
; i
++) {
110 (void) printf("%02X ", bytes
[i
]);
111 if (formatted
&& i
% 16 == 7)
113 if (formatted
&& i
% 16 == 15)
121 * TSS convenience functions
125 print_error(TSS_RESULT ret
, char *msg
)
129 /* Print the standard error string and error code. */
130 err_string
= Trspi_Error_String(ret
);
131 (void) fprintf(stderr
, "%s: %s (0x%0x)\n", msg
, err_string
, ret
);
133 /* For a few special cases, add a more verbose error message. */
135 case TPM_E_DEACTIVATED
:
137 (void) fprintf(stderr
,
138 gettext("Enable the TPM and restart Solaris.\n"));
140 case TSP_ERROR(TSS_E_COMM_FAILURE
):
141 (void) fprintf(stderr
,
142 gettext("Make sure the tcsd service "
143 "(svc:/application/security/tcsd) is running.\n"));
149 get_tpm_capability(TSS_HCONTEXT hContext
, TSS_HOBJECT hTPM
, UINT32 cap
,
150 UINT32 subcap
, void *buf
, size_t bufsize
)
156 ret
= Tspi_TPM_GetCapability(hTPM
, cap
, sizeof (subcap
),
157 (BYTE
*)&subcap
, &datalen
, &data
);
159 print_error(ret
, gettext("Get TPM capability"));
163 if (datalen
> bufsize
) {
164 (void) fprintf(stderr
,
165 gettext("Capability 0x%x returned %u bytes "
166 "(expected %u)\n"), cap
, datalen
, bufsize
);
169 bcopy(data
, buf
, datalen
);
171 ret
= Tspi_Context_FreeMemory(hContext
, data
);
173 print_error(ret
, gettext("Free capability buffer"));
181 set_policy_options(TSS_HPOLICY hPolicy
, TSS_FLAG mode
, char *prompt
,
182 UINT32 secret_len
, BYTE
*secret
)
185 BYTE
*unicode_prompt
;
188 ret
= Tspi_Policy_SetSecret(hPolicy
, mode
, secret_len
, secret
);
190 print_error(ret
, gettext("Set policy secret"));
193 if (prompt
!= NULL
) {
194 unicode_prompt
= Trspi_Native_To_UNICODE((BYTE
*)prompt
, &len
);
195 ret
= Tspi_SetAttribData(hPolicy
,
196 TSS_TSPATTRIB_POLICY_POPUPSTRING
,
197 NULL
, len
, unicode_prompt
);
199 print_error(ret
, gettext("Set policy prompt"));
208 set_object_policy(TSS_HOBJECT handle
, TSS_FLAG mode
, char *prompt
,
209 UINT32 secret_len
, BYTE
*secret
)
214 ret
= Tspi_GetPolicyObject(handle
, TSS_POLICY_USAGE
, &hPolicy
);
216 print_error(ret
, gettext("Get object policy"));
220 return (set_policy_options(hPolicy
, mode
, prompt
, secret_len
, secret
));
224 tpm_preamble(TSS_HCONTEXT
*hContext
, TSS_HOBJECT
*hTPM
)
228 ret
= Tspi_Context_Create(hContext
);
230 print_error(ret
, gettext("Create context"));
234 ret
= Tspi_Context_Connect(*hContext
, NULL
);
236 print_error(ret
, gettext("Connect context"));
237 (void) Tspi_Context_Close(*hContext
);
241 ret
= Tspi_Context_GetTpmObject(*hContext
, hTPM
);
243 print_error(ret
, gettext("Get TPM object"));
244 (void) Tspi_Context_Close(*hContext
);
251 tpm_postamble(TSS_HCONTEXT hContext
)
255 ret
= Tspi_Context_Close(hContext
);
257 print_error(ret
, gettext("Close context"));