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