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