Index: test.c ================================================================== --- test.c +++ test.c @@ -108,10 +108,11 @@ CK_OBJECT_CLASS objectClass; CK_BYTE signature[1024], encrypted_buf[16384], decrypted_buf[16384]; CK_ULONG signature_len, encrypted_buflen, decrypted_buflen; CK_MECHANISM mechanism = {CKM_RSA_PKCS, NULL, 0}; CK_RV chk_rv; + char *fgets_ret; int i; privateKeyObjects = malloc(sizeof(*privateKeyObjects) * 1024); privateKeyObjects_root = privateKeyObjects; for (i = 0; i < 1024; i++) { @@ -263,16 +264,23 @@ } chk_rv = C_OpenSession(slots[0], CKF_SERIAL_SESSION, NULL, NULL, &hSession); if (chk_rv == CKR_OK) { if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED) { - printf("** ENTER PIN: "); - fflush(stdout); + fgets_ret = NULL; + + while (fgets_ret == NULL) { + printf("** ENTER PIN: "); + fflush(stdout); + + fgets_ret = fgets((char *) user_pin, sizeof(user_pin), stdin); + } - fgets((char *) user_pin, sizeof(user_pin), stdin); - while (user_pin[strlen((char *) user_pin) - 1] < ' ') { - user_pin[strlen((char *) user_pin) - 1] = '\0'; + if (strlen(user_pin) >= 1) { + while (user_pin[strlen((char *) user_pin) - 1] < ' ') { + user_pin[strlen((char *) user_pin) - 1] = '\0'; + } } chk_rv = C_Login(hSession, CKU_USER, user_pin, strlen((char *) user_pin)); } else { chk_rv = C_Login(hSession, CKU_USER, NULL, 0);