Debian Codegraph @

testcases/login/init_pin.c

raw
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
18 #include "pkcs11types.h"
19
20 #include "regress.h"
21 #include "common.c"
22
23 int do_InitPIN(CK_FUNCTION_LIST * funcs, CK_SLOT_ID slot_id, char *sologinpass,
24 char *userinitpass)
25 {
26 CK_RV rc;
27 CK_SESSION_HANDLE session;
28 CK_FLAGS flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
29
30 rc = funcs->C_OpenSession(slot_id, flags, NULL, NULL, &session);
31 if (rc != CKR_OK) {
32 show_error("C_OpenSession", rc);
33 return rc;
34 }
35
36 rc = funcs->C_Login(session, CKU_SO, (CK_CHAR_PTR) sologinpass,
37 strlen(sologinpass));
38 if (rc != CKR_OK) {
39 show_error("C_Login", rc);
40 return rc;
41 }
42
43 printf("Logged in the SO successfully, calling C_InitPIN...\n");
44
45 rc = funcs->C_InitPIN(session, (CK_CHAR_PTR) userinitpass,
46 strlen(userinitpass));
47 if (rc != CKR_OK) {
48 show_error("C_InitPIN", rc);
49 funcs->C_Logout(session);
50 funcs->C_CloseSession(session);
51 return rc;
52 }
53
54 printf("Success.\n");
55
56 rc = funcs->C_Logout(session);
57 if (rc != CKR_OK) {
58 show_error("C_Logout", rc);
59 return rc;
60 }
61
62 printf("Logged out.\n");
63
64 rc = funcs->C_CloseSession(session);
65 if (rc != CKR_OK) {
66 show_error("C_CloseSession", rc);
67 return rc;
68 }
69
70 return rc;
71 }
72
73 void init_pin_usage(char *argv0)
74 {
75 printf("usage: %s [-slot <num>] [-h] -sopass pass -userpass pass\n\n",
76 argv0);
77 printf("By default, Slot #%lu is used, as user\n\n", SLOT_ID);
78 exit(-1);
79 }
80
81 int main(int argc, char **argv)
82 {
83 CK_C_INITIALIZE_ARGS cinit_args;
84 CK_RV rc = 0;
85 CK_SLOT_ID slot_id = 0;
86 char *sopass = NULL, *userpass = NULL;
87 int i;
88
89 for (i = 1; i < argc; i++) {
90 if (strcmp(argv[i], "-sopass") == 0) {
91 ++i;
92 if (i >= argc) {
93 printf("PIN argument missing\n");
94 return -1;
95 }
96 sopass = argv[i];
97 } else if (strcmp(argv[i], "-userpass") == 0) {
98 ++i;
99 if (i >= argc) {
100 printf("PIN argument missing\n");
101 return -1;
102 }
103 userpass = argv[i];
104 } else if (strcmp(argv[i], "-slot") == 0) {
105 ++i;
106 if (i >= argc) {
107 printf("Slot number missing\n");
108 return -1;
109 }
110 slot_id = atoi(argv[i]);
111 } else {
112 init_pin_usage(argv[0]);
113 }
114 }
115
116 if (!sopass || !userpass)
117 init_pin_usage(argv[0]);
118
119 if (slot_id != SLOT_ID)
120 printf("Using user specified slot %lu.\n", slot_id);
121
122 rc = do_GetFunctionList();
123 if (funcs == NULL)
124 return -1;
125
126 memset(&cinit_args, 0, sizeof(cinit_args));
127 cinit_args.flags = CKF_OS_LOCKING_OK;
128
129 rc = funcs->C_Initialize(&cinit_args);
130 if (rc != CKR_OK) {
131 show_error("C_Initialize", rc);
132 return -1;
133 }
134
135 rc = do_InitPIN(funcs, slot_id, sopass, userpass);
136
137 funcs->C_Finalize(NULL);
138
139 return rc;
140 }