1*af101e7fSchristos /*
2*af101e7fSchristos * The Initial Developer of the Original Code is International
3*af101e7fSchristos * Business Machines Corporation. Portions created by IBM
4*af101e7fSchristos * Corporation are Copyright (C) 2005 International Business
5*af101e7fSchristos * Machines Corporation. All Rights Reserved.
6*af101e7fSchristos *
7*af101e7fSchristos * This program is free software; you can redistribute it and/or modify
8*af101e7fSchristos * it under the terms of the Common Public License as published by
9*af101e7fSchristos * IBM Corporation; either version 1 of the License, or (at your option)
10*af101e7fSchristos * any later version.
11*af101e7fSchristos *
12*af101e7fSchristos * This program is distributed in the hope that it will be useful,
13*af101e7fSchristos * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*af101e7fSchristos * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*af101e7fSchristos * Common Public License for more details.
16*af101e7fSchristos *
17*af101e7fSchristos * You should have received a copy of the Common Public License
18*af101e7fSchristos * along with this program; if not, a copy can be viewed at
19*af101e7fSchristos * http://www.opensource.org/licenses/cpl1.0.php.
20*af101e7fSchristos */
21*af101e7fSchristos
22*af101e7fSchristos #include "data_object.h"
23*af101e7fSchristos #include "data_common.h"
24*af101e7fSchristos
25*af101e7fSchristos #include <tpm_pkcs11.h>
26*af101e7fSchristos #include <tpm_utils.h>
27*af101e7fSchristos
28*af101e7fSchristos #include <stdlib.h>
29*af101e7fSchristos #include <unistd.h>
30*af101e7fSchristos #define _GNU_SOURCE
31*af101e7fSchristos #include <getopt.h>
32*af101e7fSchristos
33*af101e7fSchristos
34*af101e7fSchristos /*
35*af101e7fSchristos * Global variables
36*af101e7fSchristos */
37*af101e7fSchristos int g_bPublic = FALSE; // Public object specifier
38*af101e7fSchristos int g_bExtended = FALSE; // Extended information display specifier
39*af101e7fSchristos char *g_pszToken = NULL; // Token label to be used
40*af101e7fSchristos
41*af101e7fSchristos /*
42*af101e7fSchristos * parseCallback
43*af101e7fSchristos * Process the command specific options.
44*af101e7fSchristos */
45*af101e7fSchristos int
parseCallback(int a_iOpt,const char * a_pszOptArg)46*af101e7fSchristos parseCallback( int a_iOpt,
47*af101e7fSchristos const char *a_pszOptArg ) {
48*af101e7fSchristos
49*af101e7fSchristos switch ( a_iOpt ) {
50*af101e7fSchristos // Use the specified token label when finding the token
51*af101e7fSchristos case 'k':
52*af101e7fSchristos if ( !a_pszOptArg )
53*af101e7fSchristos return -1;
54*af101e7fSchristos
55*af101e7fSchristos g_pszToken = strdup( a_pszOptArg );
56*af101e7fSchristos break;
57*af101e7fSchristos
58*af101e7fSchristos case 'p':
59*af101e7fSchristos g_bPublic = TRUE;
60*af101e7fSchristos break;
61*af101e7fSchristos
62*af101e7fSchristos case 'x':
63*af101e7fSchristos g_bExtended = TRUE;
64*af101e7fSchristos break;
65*af101e7fSchristos }
66*af101e7fSchristos
67*af101e7fSchristos return 0;
68*af101e7fSchristos }
69*af101e7fSchristos
70*af101e7fSchristos /*
71*af101e7fSchristos * usageCallback
72*af101e7fSchristos * Display command usage information.
73*af101e7fSchristos */
74*af101e7fSchristos void
usageCallback(const char * a_pszCmd)75*af101e7fSchristos usageCallback( const char *a_pszCmd ) {
76*af101e7fSchristos
77*af101e7fSchristos logCmdHelp( a_pszCmd );
78*af101e7fSchristos logCmdOption( "-k, --token STRING",
79*af101e7fSchristos _("Use STRING to identify the label of the PKCS#11 token to be used") );
80*af101e7fSchristos logCmdOption( "-p, --public",
81*af101e7fSchristos _("Display only public objects") );
82*af101e7fSchristos logCmdOption( "-x, --extended",
83*af101e7fSchristos _("Display additional information about the objects") );
84*af101e7fSchristos }
85*af101e7fSchristos
86*af101e7fSchristos /*
87*af101e7fSchristos * parseCmd
88*af101e7fSchristos * Parse the command line options.
89*af101e7fSchristos */
90*af101e7fSchristos int
parseCmd(int a_iArgc,char ** a_pszArgv)91*af101e7fSchristos parseCmd( int a_iArgc,
92*af101e7fSchristos char **a_pszArgv ) {
93*af101e7fSchristos
94*af101e7fSchristos char *pszShortOpts = "k:px";
95*af101e7fSchristos struct option stLongOpts[] = {
96*af101e7fSchristos { "token", required_argument, NULL, 'k' },
97*af101e7fSchristos { "public", no_argument, NULL, 'p' },
98*af101e7fSchristos { "extended", no_argument, NULL, 'x' },
99*af101e7fSchristos };
100*af101e7fSchristos
101*af101e7fSchristos int iNumLongOpts = sizeof( stLongOpts ) / sizeof( struct option );
102*af101e7fSchristos
103*af101e7fSchristos return genericOptHandler( a_iArgc, a_pszArgv,
104*af101e7fSchristos pszShortOpts, stLongOpts, iNumLongOpts,
105*af101e7fSchristos parseCallback, usageCallback );
106*af101e7fSchristos }
107*af101e7fSchristos
108*af101e7fSchristos int
main(int a_iArgc,char ** a_pszArgv)109*af101e7fSchristos main( int a_iArgc,
110*af101e7fSchristos char **a_pszArgv ) {
111*af101e7fSchristos
112*af101e7fSchristos int rc = 1;
113*af101e7fSchristos
114*af101e7fSchristos char *pszPin = NULL;
115*af101e7fSchristos
116*af101e7fSchristos CK_RV rv = CKR_OK;
117*af101e7fSchristos CK_SESSION_HANDLE hSession = 0;
118*af101e7fSchristos CK_OBJECT_HANDLE *hObject;
119*af101e7fSchristos CK_ULONG ulCount;
120*af101e7fSchristos
121*af101e7fSchristos // Set up i18n
122*af101e7fSchristos initIntlSys( );
123*af101e7fSchristos
124*af101e7fSchristos // Parse the command
125*af101e7fSchristos if ( parseCmd( a_iArgc, a_pszArgv ) == -1 )
126*af101e7fSchristos goto out;
127*af101e7fSchristos
128*af101e7fSchristos // Open the PKCS#11 TPM Token
129*af101e7fSchristos rv = openToken( g_pszToken );
130*af101e7fSchristos if ( rv != CKR_OK )
131*af101e7fSchristos goto out;
132*af101e7fSchristos
133*af101e7fSchristos // Make sure the token is initialized
134*af101e7fSchristos if ( !isTokenInitialized( ) ) {
135*af101e7fSchristos logMsg( TOKEN_NOT_INIT_ERROR );
136*af101e7fSchristos goto out;
137*af101e7fSchristos }
138*af101e7fSchristos
139*af101e7fSchristos // Open a session
140*af101e7fSchristos rv = openTokenSession( CKF_RW_SESSION, &hSession );
141*af101e7fSchristos if ( rv != CKR_OK )
142*af101e7fSchristos goto out;
143*af101e7fSchristos
144*af101e7fSchristos // Check the scope of the request, which will determine the login
145*af101e7fSchristos // requirements:
146*af101e7fSchristos // Public = no password, no login
147*af101e7fSchristos // Private = user password, user login
148*af101e7fSchristos if ( !g_bPublic ) {
149*af101e7fSchristos pszPin = getPlainPasswd( TOKEN_USER_PIN_PROMPT, FALSE );
150*af101e7fSchristos if ( !pszPin )
151*af101e7fSchristos goto out;
152*af101e7fSchristos
153*af101e7fSchristos // Login to the token
154*af101e7fSchristos rv = loginToken( hSession, CKU_USER, pszPin );
155*af101e7fSchristos if ( rv != CKR_OK )
156*af101e7fSchristos goto out;
157*af101e7fSchristos }
158*af101e7fSchristos
159*af101e7fSchristos rv = findObjects( hSession, NULL, 0, &hObject, &ulCount );
160*af101e7fSchristos if ( rv != CKR_OK )
161*af101e7fSchristos goto out;
162*af101e7fSchristos
163*af101e7fSchristos if ( ulCount > 0 ) {
164*af101e7fSchristos while ( ulCount > 0 )
165*af101e7fSchristos displayObject( hSession, hObject[ --ulCount ], g_bExtended );
166*af101e7fSchristos }
167*af101e7fSchristos else {
168*af101e7fSchristos logMsg( NO_TOKEN_OBJECTS );
169*af101e7fSchristos }
170*af101e7fSchristos
171*af101e7fSchristos free( hObject );
172*af101e7fSchristos
173*af101e7fSchristos rc = 0;
174*af101e7fSchristos
175*af101e7fSchristos out:
176*af101e7fSchristos shredPasswd( pszPin );
177*af101e7fSchristos
178*af101e7fSchristos if ( hSession )
179*af101e7fSchristos closeTokenSession( hSession );
180*af101e7fSchristos
181*af101e7fSchristos closeToken( );
182*af101e7fSchristos
183*af101e7fSchristos if ( rc == 0 )
184*af101e7fSchristos logInfo( TOKEN_CMD_SUCCESS, a_pszArgv[ 0 ] );
185*af101e7fSchristos else
186*af101e7fSchristos logInfo( TOKEN_CMD_FAILED, a_pszArgv[ 0 ] );
187*af101e7fSchristos
188*af101e7fSchristos return rc;
189*af101e7fSchristos }
190