8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / cmd-crypto / tpmadm / main.c
blob1cdc02b14d6f7f8b09c6e736da4043c37c7196c7
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <strings.h>
32 #include <libintl.h>
33 #include <locale.h>
35 #include <tss/tspi.h>
36 #include <trousers/trousers.h>
37 #include "tpmadm.h"
39 extern cmdtable_t commands[];
41 static void
42 print_usage(char *progname, cmdtable_t cmds[])
44 cmdtable_t *p;
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);
55 int
56 main(int argc, char *argv[])
58 char *progname;
59 cmdtable_t *p;
60 cmdfunc_t fptr = NULL;
61 int ret;
62 TSS_HCONTEXT hContext;
63 TSS_HOBJECT hTPM;
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. */
68 #endif
69 (void) setlocale(LC_ALL, "");
70 (void) textdomain(TEXT_DOMAIN);
72 progname = argv[0];
73 argc--;
74 argv++;
76 if (argc <= 0) {
77 print_usage(progname, commands);
78 return (ERR_USAGE);
81 for (p = &commands[0]; p->name != NULL; p++) {
82 if (0 == strcmp(p->name, argv[0])) {
83 fptr = p->func;
84 break;
87 if (fptr == NULL) {
88 print_usage(progname, commands);
89 return (ERR_USAGE);
92 if (tpm_preamble(&hContext, &hTPM))
93 return (ERR_FAIL);
94 ret = fptr(hContext, hTPM, argc, argv);
95 (void) tpm_postamble(hContext);
97 return (ret);
102 * Utility functions
105 void
106 print_bytes(BYTE *bytes, size_t len, int formatted)
108 int i;
109 for (i = 0; i < len; i++) {
110 (void) printf("%02X ", bytes[i]);
111 if (formatted && i % 16 == 7)
112 (void) printf(" ");
113 if (formatted && i % 16 == 15)
114 (void) printf("\n");
116 (void) printf("\n");
121 * TSS convenience functions
124 void
125 print_error(TSS_RESULT ret, char *msg)
127 char *err_string;
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. */
134 switch (ret) {
135 case TPM_E_DEACTIVATED:
136 case TPM_E_DISABLED:
137 (void) fprintf(stderr,
138 gettext("Enable the TPM and restart Solaris.\n"));
139 break;
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"));
144 break;
149 get_tpm_capability(TSS_HCONTEXT hContext, TSS_HOBJECT hTPM, UINT32 cap,
150 UINT32 subcap, void *buf, size_t bufsize)
152 TSS_RESULT ret;
153 UINT32 datalen;
154 BYTE *data;
156 ret = Tspi_TPM_GetCapability(hTPM, cap, sizeof (subcap),
157 (BYTE *)&subcap, &datalen, &data);
158 if (ret) {
159 print_error(ret, gettext("Get TPM capability"));
160 return (ERR_FAIL);
163 if (datalen > bufsize) {
164 (void) fprintf(stderr,
165 gettext("Capability 0x%x returned %u bytes "
166 "(expected %u)\n"), cap, datalen, bufsize);
167 return (ERR_FAIL);
169 bcopy(data, buf, datalen);
171 ret = Tspi_Context_FreeMemory(hContext, data);
172 if (ret) {
173 print_error(ret, gettext("Free capability buffer"));
174 return (ERR_FAIL);
177 return (0);
181 set_policy_options(TSS_HPOLICY hPolicy, TSS_FLAG mode, char *prompt,
182 UINT32 secret_len, BYTE *secret)
184 TSS_RESULT ret;
185 BYTE *unicode_prompt;
186 UINT32 len;
188 ret = Tspi_Policy_SetSecret(hPolicy, mode, secret_len, secret);
189 if (ret) {
190 print_error(ret, gettext("Set policy secret"));
191 return (ERR_FAIL);
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);
198 if (ret) {
199 print_error(ret, gettext("Set policy prompt"));
200 return (ERR_FAIL);
204 return (0);
208 set_object_policy(TSS_HOBJECT handle, TSS_FLAG mode, char *prompt,
209 UINT32 secret_len, BYTE *secret)
211 TSS_HPOLICY hPolicy;
212 TSS_RESULT ret;
214 ret = Tspi_GetPolicyObject(handle, TSS_POLICY_USAGE, &hPolicy);
215 if (ret) {
216 print_error(ret, gettext("Get object policy"));
217 return (ERR_FAIL);
220 return (set_policy_options(hPolicy, mode, prompt, secret_len, secret));
224 tpm_preamble(TSS_HCONTEXT *hContext, TSS_HOBJECT *hTPM)
226 TSS_RESULT ret;
228 ret = Tspi_Context_Create(hContext);
229 if (ret) {
230 print_error(ret, gettext("Create context"));
231 return (ERR_FAIL);
234 ret = Tspi_Context_Connect(*hContext, NULL);
235 if (ret) {
236 print_error(ret, gettext("Connect context"));
237 (void) Tspi_Context_Close(*hContext);
238 return (ERR_FAIL);
241 ret = Tspi_Context_GetTpmObject(*hContext, hTPM);
242 if (ret) {
243 print_error(ret, gettext("Get TPM object"));
244 (void) Tspi_Context_Close(*hContext);
245 return (ERR_FAIL);
247 return (0);
251 tpm_postamble(TSS_HCONTEXT hContext)
253 TSS_RESULT ret;
255 ret = Tspi_Context_Close(hContext);
256 if (ret) {
257 print_error(ret, gettext("Close context"));
258 return (ERR_FAIL);
260 return (0);