| 1 |
/* |
| 2 |
* COPYRIGHT (c) International Business Machines Corp. 2002-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 |
* openCryptoki testcase |
| 13 |
* - Tests the new login flags for v2.11 |
| 14 |
* |
| 15 |
* Feb 12, 2002 |
| 16 |
* Kent Yoder <[email protected]> |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include <dlfcn.h> |
| 24 |
#include <sys/types.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <fcntl.h> |
| 28 |
|
| 29 |
#include "pkcs11types.h" |
| 30 |
#include "regress.h" |
| 31 |
|
| 32 |
#define BAD_USER_PIN "534566346" |
| 33 |
#define BAD_USER_PIN_LEN strlen(BAD_USER_PIN) |
| 34 |
|
| 35 |
int clean_up(void); |
| 36 |
|
| 37 |
CK_SLOT_ID slot_id; |
| 38 |
CK_SESSION_HANDLE session_handle; |
| 39 |
CK_SESSION_INFO si; |
| 40 |
CK_TOKEN_INFO ti; |
| 41 |
|
| 42 |
void *dl_handle; |
| 43 |
|
| 44 |
int main(int argc, char **argv) |
| 45 |
{ |
| 46 |
int i; |
| 47 |
CK_RV rc; |
| 48 |
CK_C_INITIALIZE_ARGS initialize_args; |
| 49 |
CK_BYTE user_pin[PKCS11_MAX_PIN_LEN]; |
| 50 |
CK_ULONG user_pin_len; |
| 51 |
|
| 52 |
/* Set default slot to 0 */ |
| 53 |
slot_id = 0; |
| 54 |
|
| 55 |
/* Parse the command line */ |
| 56 |
for (i = 1; i < argc; i++) { |
| 57 |
if (strncmp(argv[i], "-slot", 5) == 0) { |
| 58 |
if (i + 1 >= argc) { |
| 59 |
printf("Slot number missing\n"); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
slot_id = atoi(argv[i + 1]); |
| 63 |
i++; |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
printf("Using slot %lu...\n\n", slot_id); |
| 69 |
|
| 70 |
if (do_GetFunctionList()) |
| 71 |
return -1; |
| 72 |
|
| 73 |
/* There will be no multi-threaded Cryptoki access in this app */ |
| 74 |
memset(&initialize_args, 0, sizeof(initialize_args)); |
| 75 |
memset(&si, 0, sizeof(CK_SESSION_INFO)); |
| 76 |
|
| 77 |
if ((rc = funcs->C_Initialize(&initialize_args)) != CKR_OK) { |
| 78 |
show_error("C_Initialize", rc); |
| 79 |
return -1; |
| 80 |
} |
| 81 |
|
| 82 |
if (get_user_pin(user_pin)) |
| 83 |
return -1; |
| 84 |
user_pin_len = (CK_ULONG) strlen((char *) user_pin); |
| 85 |
|
| 86 |
// |
| 87 |
// Tests: |
| 88 |
// |
| 89 |
// 1. Open Session |
| 90 |
// 2. Check that the session looks normal |
| 91 |
// 3. Login/Logout as USER with correct PIN |
| 92 |
// 4. Login as USER with an incorrect PIN |
| 93 |
// 5. Check that USER PIN COUNT LOW set |
| 94 |
// 6. Login as USER with an incorrect PIN |
| 95 |
// 7. Check that USER PIN LAST TRY set |
| 96 |
// 8. Login correctly |
| 97 |
// 9. Check that flags are reset |
| 98 |
// 10. Try to set a new PIN, but with newPIN == oldPIN |
| 99 |
// 11. Check that we get CKR_PIN_INVALID |
| 100 |
// 12. Login as USER with an incorrect PIN |
| 101 |
// 13. Check that USER PIN COUNT LOW set |
| 102 |
// 14. Login as USER with an incorrect PIN |
| 103 |
// 15. Check that USER PIN LAST TRY set |
| 104 |
// 16. Login as USER with incorrect PIN |
| 105 |
// 17. Check that USER PIN LOCKED set |
| 106 |
// |
| 107 |
|
| 108 |
/* 1. Open a session with the token */ |
| 109 |
if ((rc = funcs->C_OpenSession(slot_id, |
| 110 |
(CKF_SERIAL_SESSION | CKF_RW_SESSION), |
| 111 |
NULL_PTR, |
| 112 |
NULL_PTR, &session_handle)) != CKR_OK) { |
| 113 |
show_error("C_OpenSession #1", rc); |
| 114 |
goto done; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
if ((rc = funcs->C_GetSessionInfo(session_handle, &si)) != CKR_OK) { |
| 119 |
show_error("C_GetSessionInfo #1", rc); |
| 120 |
goto session_close; |
| 121 |
} |
| 122 |
|
| 123 |
/* 2. Test the slot_id change. This used to be hard coded to 1. |
| 124 |
* It should now be the slot number of the token we're using |
| 125 |
*/ |
| 126 |
if (si.slotID != slot_id) { |
| 127 |
printf("Test #2 failed. Slot ID was %lu, expected %lu\n", si.slotID, |
| 128 |
slot_id); |
| 129 |
goto session_close; |
| 130 |
} |
| 131 |
|
| 132 |
if ((rc = funcs->C_GetTokenInfo(slot_id, &ti)) != CKR_OK) { |
| 133 |
show_error("C_GetTokenInfo #2", rc); |
| 134 |
goto session_close; |
| 135 |
} |
| 136 |
|
| 137 |
if (ti.flags & CKF_USER_PIN_LOCKED) { |
| 138 |
printf("The USER's PIN is locked for the token in slot %lu.\n" |
| 139 |
"Please reset the USER's PIN and re-run this test.\n", slot_id); |
| 140 |
goto session_close; |
| 141 |
} |
| 142 |
|
| 143 |
if (!(ti.flags & CKF_TOKEN_INITIALIZED)) { |
| 144 |
printf("The token in slot %lu is uninitialized.\n", slot_id); |
| 145 |
goto session_close; |
| 146 |
} |
| 147 |
// 3. Login/Logout with correct USER PIN |
| 148 |
rc = funcs->C_Login(session_handle, CKU_USER, user_pin, user_pin_len); |
| 149 |
if (rc != CKR_OK) { |
| 150 |
show_error("C_Login #3", rc); |
| 151 |
goto session_close; |
| 152 |
} |
| 153 |
|
| 154 |
rc = funcs->C_Logout(session_handle); |
| 155 |
if (rc != CKR_OK) { |
| 156 |
show_error("C_Logout #3", rc); |
| 157 |
goto session_close; |
| 158 |
} |
| 159 |
// 4. Login as USER with an incorrect PIN |
| 160 |
rc = funcs->C_Login(session_handle, CKU_USER, (CK_CHAR_PTR) BAD_USER_PIN, |
| 161 |
BAD_USER_PIN_LEN); |
| 162 |
if (rc != CKR_PIN_INCORRECT) { |
| 163 |
show_error("Test #4", rc); |
| 164 |
goto session_close; |
| 165 |
} |
| 166 |
|
| 167 |
if ((rc = funcs->C_GetTokenInfo(slot_id, &ti)) != CKR_OK) { |
| 168 |
show_error("C_GetTokenInfo #4", rc); |
| 169 |
goto session_close; |
| 170 |
} |
| 171 |
// 5. Check that USER PIN COUNT LOW set |
| 172 |
if (((ti.flags & CKF_USER_PIN_COUNT_LOW) == 0) || |
| 173 |
(ti.flags & CKF_USER_PIN_FINAL_TRY) || |
| 174 |
(ti.flags & CKF_USER_PIN_LOCKED)) { |
| 175 |
printf("Test #5 failed. Token flags: %p.\n", (void *) ti.flags); |
| 176 |
goto session_close; |
| 177 |
} |
| 178 |
// 6. Login as USER with an incorrect PIN |
| 179 |
rc = funcs->C_Login(session_handle, CKU_USER, (CK_CHAR_PTR) BAD_USER_PIN, |
| 180 |
BAD_USER_PIN_LEN); |
| 181 |
if (rc != CKR_PIN_INCORRECT) { |
| 182 |
show_error("C_Login #6", rc); |
| 183 |
goto session_close; |
| 184 |
} |
| 185 |
|
| 186 |
if ((rc = funcs->C_GetTokenInfo(slot_id, &ti)) != CKR_OK) { |
| 187 |
show_error("C_GetTokenInfo #6", rc); |
| 188 |
goto session_close; |
| 189 |
} |
| 190 |
// 7. Check that USER PIN LAST TRY set |
| 191 |
if ((ti.flags & CKF_USER_PIN_COUNT_LOW) || |
| 192 |
((ti.flags & CKF_USER_PIN_FINAL_TRY) == 0) || |
| 193 |
(ti.flags & CKF_USER_PIN_LOCKED)) { |
| 194 |
printf("Test #7 failed. Token flags: %p.\n", (void *) ti.flags); |
| 195 |
goto session_close; |
| 196 |
} |
| 197 |
// 8. Login correctly |
| 198 |
rc = funcs->C_Login(session_handle, CKU_USER, user_pin, user_pin_len); |
| 199 |
if (rc != CKR_OK) { |
| 200 |
show_error("C_Login #8", rc); |
| 201 |
goto session_close; |
| 202 |
} |
| 203 |
|
| 204 |
if ((rc = funcs->C_GetTokenInfo(slot_id, &ti)) != CKR_OK) { |
| 205 |
show_error("C_GetTokenInfo #8", rc); |
| 206 |
goto session_close; |
| 207 |
} |
| 208 |
// 9. Check that flags are reset |
| 209 |
if ((ti.flags & CKF_USER_PIN_COUNT_LOW) || |
| 210 |
(ti.flags & CKF_USER_PIN_FINAL_TRY) || |
| 211 |
(ti.flags & CKF_USER_PIN_LOCKED)) { |
| 212 |
|
| 213 |
printf("Test #9 failed. Token flags: %p.\n", (void *) ti.flags); |
| 214 |
goto session_close; |
| 215 |
} |
| 216 |
// 10. Try to set a new PIN, but with newPIN == oldPIN |
| 217 |
// 11. Check that we get CKR_PIN_INVALID |
| 218 |
rc = funcs->C_SetPIN(session_handle, user_pin, user_pin_len, |
| 219 |
user_pin, user_pin_len); |
| 220 |
if (rc != CKR_PIN_INVALID) { |
| 221 |
show_error("Test #10", rc); |
| 222 |
goto session_close; |
| 223 |
} |
| 224 |
// 12. Login as USER with an incorrect PIN |
| 225 |
rc = funcs->C_Login(session_handle, CKU_USER, (CK_CHAR_PTR) BAD_USER_PIN, |
| 226 |
BAD_USER_PIN_LEN); |
| 227 |
if (rc != CKR_PIN_INCORRECT) { |
| 228 |
show_error("C_Login #12", rc); |
| 229 |
goto session_close; |
| 230 |
} |
| 231 |
|
| 232 |
if ((rc = funcs->C_GetTokenInfo(slot_id, &ti)) != CKR_OK) { |
| 233 |
show_error("C_GetTokenInfo #12", rc); |
| 234 |
goto session_close; |
| 235 |
} |
| 236 |
// 13. Check that USER PIN COUNT LOW set |
| 237 |
if (((ti.flags & CKF_USER_PIN_COUNT_LOW) == 0) || |
| 238 |
(ti.flags & CKF_USER_PIN_FINAL_TRY) || |
| 239 |
(ti.flags & CKF_USER_PIN_LOCKED)) { |
| 240 |
printf("Test #13 failed. Token flags: %p.\n", (void *) ti.flags); |
| 241 |
goto session_close; |
| 242 |
} |
| 243 |
// 14. Login as USER with an incorrect PIN |
| 244 |
rc = funcs->C_Login(session_handle, CKU_USER, (CK_CHAR_PTR) BAD_USER_PIN, |
| 245 |
BAD_USER_PIN_LEN); |
| 246 |
if (rc != CKR_PIN_INCORRECT) { |
| 247 |
show_error("C_Login #14", rc); |
| 248 |
goto session_close; |
| 249 |
} |
| 250 |
|
| 251 |
if ((rc = funcs->C_GetTokenInfo(slot_id, &ti)) != CKR_OK) { |
| 252 |
show_error("C_GetTokenInfo #14", rc); |
| 253 |
goto session_close; |
| 254 |
} |
| 255 |
// 15. Check that USER PIN LAST TRY set |
| 256 |
if ((ti.flags & CKF_USER_PIN_COUNT_LOW) || |
| 257 |
((ti.flags & CKF_USER_PIN_FINAL_TRY) == 0) || |
| 258 |
(ti.flags & CKF_USER_PIN_LOCKED)) { |
| 259 |
printf("Test #15 failed. Token flags: %p.\n", (void *) ti.flags); |
| 260 |
goto session_close; |
| 261 |
} |
| 262 |
|
| 263 |
// 16. Login as USER with incorrect PIN |
| 264 |
rc = funcs->C_Login(session_handle, CKU_USER, (CK_CHAR_PTR) BAD_USER_PIN, |
| 265 |
BAD_USER_PIN_LEN); |
| 266 |
if (rc != CKR_PIN_INCORRECT) { |
| 267 |
show_error("C_Login #16", rc); |
| 268 |
goto session_close; |
| 269 |
} |
| 270 |
|
| 271 |
if ((rc = funcs->C_GetTokenInfo(slot_id, &ti)) != CKR_OK) { |
| 272 |
show_error("C_GetTokenInfo #16", rc); |
| 273 |
goto session_close; |
| 274 |
} |
| 275 |
// 17. Check that USER PIN LOCKED set |
| 276 |
if ((ti.flags & CKF_USER_PIN_COUNT_LOW) || |
| 277 |
(ti.flags & CKF_USER_PIN_FINAL_TRY) || |
| 278 |
((ti.flags & CKF_USER_PIN_LOCKED) == 0)) { |
| 279 |
|
| 280 |
printf("Test #17 failed. Token flags: %p.\n", (void *) ti.flags); |
| 281 |
goto session_close; |
| 282 |
} |
| 283 |
|
| 284 |
printf("Tests succeeded. USER PIN is now locked for slot %lu.\n" |
| 285 |
"Re-running this test should return CKR_PIN_LOCKED.\n" |
| 286 |
"To unlock this slot, run the init_tok testcase on the slot.\n", |
| 287 |
slot_id); |
| 288 |
|
| 289 |
session_close: |
| 290 |
|
| 291 |
/* Close the session */ |
| 292 |
if ((rc = funcs->C_CloseSession(session_handle)) != CKR_OK) |
| 293 |
show_error("C_CloseSession", rc); |
| 294 |
|
| 295 |
done: |
| 296 |
/* Call C_Finalize and dlclose the library */ |
| 297 |
return clean_up(); |
| 298 |
} |
| 299 |
|
| 300 |
int clean_up(void) |
| 301 |
{ |
| 302 |
CK_RV rc; |
| 303 |
|
| 304 |
if ((rc = funcs->C_Finalize(NULL)) != CKR_OK) |
| 305 |
show_error("C_Finalize", rc); |
| 306 |
|
| 307 |
/* Decrement the reference count to libopencryptoki.so */ |
| 308 |
#ifndef WITH_SANITIZER |
| 309 |
dlclose(dl_handle); |
| 310 |
#endif |
| 311 |
|
| 312 |
return rc; |
| 313 |
} |