1 /*
2  * COPYRIGHT (c) International Business Machines Corp. 2005-2017
3  *
4  * This program is provided under the terms of the Common Public License,
5  * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6  * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7  * found in the file LICENSE file or at
8  * https://opensource.org/licenses/cpl1.0.php
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <memory.h>
15 
16 #include <dlfcn.h>
17 #include <sys/timeb.h>
18 
19 #include "pkcs11types.h"
20 
21 #include "regress.h"
22 #include "common.c"
23 
24 CK_FUNCTION_LIST *funcs;
25 int do_GetFunctionList(void);
26 
do_InitPIN(CK_FUNCTION_LIST * funcs,CK_SLOT_ID slot_id,char * sologinpass,char * userinitpass)27 int do_InitPIN(CK_FUNCTION_LIST * funcs, CK_SLOT_ID slot_id, char *sologinpass,
28                char *userinitpass)
29 {
30     CK_RV rc;
31     CK_SESSION_HANDLE session;
32     CK_FLAGS flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
33 
34     rc = funcs->C_OpenSession(slot_id, flags, NULL, NULL, &session);
35     if (rc != CKR_OK) {
36         show_error("C_OpenSession", rc);
37         return rc;
38     }
39 
40     rc = funcs->C_Login(session, CKU_SO, (CK_CHAR_PTR) sologinpass,
41                         strlen(sologinpass));
42     if (rc != CKR_OK) {
43         show_error("C_Login", rc);
44         return rc;
45     }
46 
47     printf("Logged in the SO successfully, calling C_InitPIN...\n");
48 
49     rc = funcs->C_InitPIN(session, (CK_CHAR_PTR) userinitpass,
50                           strlen(userinitpass));
51     if (rc != CKR_OK) {
52         show_error("C_InitPIN", rc);
53         funcs->C_Logout(session);
54         funcs->C_CloseSession(session);
55         return rc;
56     }
57 
58     printf("Success.\n");
59 
60     rc = funcs->C_Logout(session);
61     if (rc != CKR_OK) {
62         show_error("C_Logout", rc);
63         return rc;
64     }
65 
66     printf("Logged out.\n");
67 
68     rc = funcs->C_CloseSession(session);
69     if (rc != CKR_OK) {
70         show_error("C_CloseSession", rc);
71         return rc;
72     }
73 
74     return rc;
75 }
76 
init_pin_usage(char * argv0)77 void init_pin_usage(char *argv0)
78 {
79     printf("usage:  %s [-slot <num>] [-h] -sopass pass -userpass pass\n\n",
80            argv0);
81     printf("By default, Slot #%lu is used, as user\n\n", SLOT_ID);
82     exit(-1);
83 }
84 
main(int argc,char ** argv)85 int main(int argc, char **argv)
86 {
87     CK_C_INITIALIZE_ARGS cinit_args;
88     CK_RV rc = 0;
89     CK_SLOT_ID slot_id = 0;
90     char *sopass = NULL, *userpass = NULL;
91     int i;
92 
93     for (i = 1; i < argc; i++) {
94         if (strcmp(argv[i], "-sopass") == 0) {
95             ++i;
96             sopass = argv[i];
97         } else if (strcmp(argv[i], "-userpass") == 0) {
98             ++i;
99             userpass = argv[i];
100         } else if (strcmp(argv[i], "-slot") == 0) {
101             ++i;
102             slot_id = atoi(argv[i]);
103         } else {
104             init_pin_usage(argv[0]);
105         }
106     }
107 
108     if (!sopass || !userpass)
109         init_pin_usage(argv[0]);
110 
111     if (slot_id != SLOT_ID)
112         printf("Using user specified slot %lu.\n", slot_id);
113 
114     rc = do_GetFunctionList();
115     if (funcs == NULL)
116         return -1;
117 
118     memset(&cinit_args, 0, sizeof(cinit_args));
119     cinit_args.flags = CKF_OS_LOCKING_OK;
120 
121     rc = funcs->C_Initialize(&cinit_args);
122     if (rc != CKR_OK) {
123         show_error("C_Initialize", rc);
124         return -1;
125     }
126 
127     rc = do_InitPIN(funcs, slot_id, sopass, userpass);
128 
129     funcs->C_Finalize(NULL);
130 
131     return rc;
132 }
133