1 /* ****************************************************************************
2
3 * eID Middleware Project.
4 * Copyright (C) 2014 FedICT.
5 *
6 * This is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version
8 * 3.0 as published by the Free Software Foundation.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this software; if not, see
17 * http://www.gnu.org/licenses/.
18
19 **************************************************************************** */
20 #ifdef WIN32
21 #include <win32.h>
22 #pragma pack(push, cryptoki, 1)
23 #include "pkcs11.h"
24 #pragma pack(pop, cryptoki)
25 #else
26 #include <unix.h>
27 #include <pkcs11.h>
28 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "testlib.h"
33
notify_login(CK_SESSION_HANDLE handle EIDT_UNUSED,CK_NOTIFICATION event EIDT_UNUSED,CK_VOID_PTR ptr EIDT_UNUSED)34 static CK_RV notify_login(CK_SESSION_HANDLE handle EIDT_UNUSED, CK_NOTIFICATION event EIDT_UNUSED, CK_VOID_PTR ptr EIDT_UNUSED) {
35 printf("INFO: notification called\n");
36 return CKR_OK;
37 }
38
TEST_FUNC(slogin)39 TEST_FUNC(slogin) {
40 CK_SLOT_ID slot;
41 CK_SESSION_HANDLE handle = 0;
42 CK_SESSION_INFO sinfo;
43 int ret;
44 ckrv_mod m[] = {
45 { CKR_PIN_INCORRECT, TEST_RV_SKIP },
46 { CKR_FUNCTION_CANCELED, TEST_RV_SKIP },
47 };
48 ckrv_mod m_nlogin[] = {
49 { CKR_USER_NOT_LOGGED_IN, TEST_RV_OK },
50 { CKR_OK, TEST_RV_FAIL },
51 };
52 ckrv_mod m_nsession[] = {
53 { CKR_SESSION_HANDLE_INVALID, TEST_RV_OK },
54 { CKR_OK, TEST_RV_FAIL },
55 };
56
57 check_rv_long(C_Login(handle, CKU_USER, NULL_PTR, 0), m_p11_noinit);
58 check_rv_long(C_Logout(handle), m_p11_noinit);
59
60 check_rv(C_Initialize(NULL_PTR));
61
62 if((ret = find_slot(CK_TRUE, &slot)) != TEST_RV_OK) {
63 check_rv(C_Finalize(NULL_PTR));
64 return ret;
65 }
66
67 check_rv_long(C_Logout(handle), m_nsession);
68
69 check_rv(C_OpenSession(slot, CKF_SERIAL_SESSION, NULL_PTR, notify_login, &handle));
70
71 check_rv(C_GetSessionInfo(handle, &sinfo));
72 printf("State: %lu\n", sinfo.state);
73 printf("Flags: %#08lx\n", sinfo.flags);
74
75 if(!have_pin() || !can_enter_pin(slot)) {
76 fprintf(stderr, "cannot test login without the ability to enter a pin code\n");
77 check_rv(C_Finalize(NULL_PTR));
78 return TEST_RV_SKIP;
79 }
80
81 check_rv_long(C_Logout(handle), m_nlogin);
82
83 check_rv_long(C_Login(handle, CKU_USER, NULL_PTR, 0), m);
84
85 check_rv(C_GetSessionInfo(handle, &sinfo));
86
87 printf("State: %lu\n", sinfo.state);
88 printf("Flags: %#08lx\n", sinfo.flags);
89
90 check_rv(C_Logout(handle));
91 check_rv_long(C_Logout(handle), m_nlogin);
92 check_rv(C_CloseSession(handle));
93
94 if(have_robot()) {
95 check_rv(C_OpenSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &handle));
96 check_rv_long(C_Login(handle, CKU_USER, NULL_PTR, 0), m);
97 robot_remove_card();
98 check_rv_long(C_Logout(handle), m_p11_nocard);
99 C_CloseSession(handle);
100 }
101
102 check_rv(C_Finalize(NULL_PTR));
103
104 return TEST_RV_OK;
105 }
106