NingSun | 0c89b3c | 2018-02-08 08:34:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | /***************************************************************************** |
| 28 | UserTests.cpp |
| 29 | |
| 30 | Contains test cases to C_InitPIN, C_SetPIN, C_Login, and C_Logout |
| 31 | *****************************************************************************/ |
| 32 | |
| 33 | #include <config.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include "UserTests.h" |
| 37 | |
| 38 | CPPUNIT_TEST_SUITE_REGISTRATION(UserTests); |
| 39 | |
| 40 | void UserTests::testInitPIN() |
| 41 | { |
| 42 | CK_RV rv; |
| 43 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
| 44 | |
| 45 | // Just make sure that we finalize any previous tests |
| 46 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 47 | |
| 48 | rv = CRYPTOKI_F_PTR( C_InitPIN(hSession, m_userPin1, m_userPin1Length) ); |
| 49 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 50 | |
| 51 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 52 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 53 | |
| 54 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 55 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 56 | |
| 57 | rv = CRYPTOKI_F_PTR( C_InitPIN(hSession, m_userPin1, m_userPin1Length) ); |
| 58 | CPPUNIT_ASSERT(rv == CKR_USER_NOT_LOGGED_IN); |
| 59 | |
| 60 | rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_SO, m_soPin1, m_soPin1Length) ); |
| 61 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 62 | |
| 63 | rv = CRYPTOKI_F_PTR( C_InitPIN(CK_INVALID_HANDLE, m_userPin1, m_userPin1Length) ); |
| 64 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 65 | |
| 66 | rv = CRYPTOKI_F_PTR( C_InitPIN(hSession, m_userPin1, 0) ); |
| 67 | CPPUNIT_ASSERT(rv == CKR_PIN_LEN_RANGE); |
| 68 | |
| 69 | rv = CRYPTOKI_F_PTR( C_InitPIN(hSession, m_userPin1, m_userPin1Length) ); |
| 70 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 71 | } |
| 72 | |
| 73 | void UserTests::testLogin() |
| 74 | { |
| 75 | CK_RV rv; |
| 76 | CK_SESSION_HANDLE hSession[2]; |
| 77 | |
| 78 | // Just make sure that we finalize any previous tests |
| 79 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 80 | |
| 81 | // Set up user PIN |
| 82 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 83 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 84 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession[0]) ); |
| 85 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 86 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_USER, m_userPin1, m_userPin1Length) ); |
| 87 | CPPUNIT_ASSERT(rv == CKR_USER_PIN_NOT_INITIALIZED); |
| 88 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, m_soPin1, m_soPin1Length) ); |
| 89 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 90 | rv = CRYPTOKI_F_PTR( C_InitPIN(hSession[0], m_userPin1, m_userPin1Length) ); |
| 91 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 92 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 93 | |
| 94 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, m_soPin1, m_soPin1Length) ); |
| 95 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 96 | |
| 97 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 98 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 99 | |
| 100 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession[0]) ); |
| 101 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 102 | |
| 103 | rv = CRYPTOKI_F_PTR( C_Login(CK_INVALID_HANDLE, CKU_SO, m_soPin1, m_soPin1Length) ); |
| 104 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 105 | |
| 106 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, NULL_PTR, m_soPin1Length) ); |
| 107 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 108 | |
| 109 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, m_soPin1, 0) ); |
| 110 | CPPUNIT_ASSERT(rv == CKR_PIN_INCORRECT); |
| 111 | |
| 112 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession[1]) ); |
| 113 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 114 | |
| 115 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, m_soPin1, m_soPin1Length) ); |
| 116 | CPPUNIT_ASSERT(rv == CKR_SESSION_READ_ONLY_EXISTS); |
| 117 | |
| 118 | rv = CRYPTOKI_F_PTR( C_CloseSession(hSession[1]) ); |
| 119 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 120 | |
| 121 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_USER, m_userPin1, m_userPin1Length) ); |
| 122 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 123 | |
| 124 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, m_soPin1, m_soPin1Length) ); |
| 125 | CPPUNIT_ASSERT(rv == CKR_USER_ANOTHER_ALREADY_LOGGED_IN); |
| 126 | |
| 127 | rv = CRYPTOKI_F_PTR( C_Logout(hSession[0]) ); |
| 128 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 129 | |
| 130 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, m_soPin1, m_soPin1Length) ); |
| 131 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 132 | |
| 133 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_SO, m_soPin1, m_soPin1Length) ); |
| 134 | CPPUNIT_ASSERT(rv == CKR_USER_ALREADY_LOGGED_IN); |
| 135 | |
| 136 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_USER, m_userPin1, m_userPin1Length) ); |
| 137 | CPPUNIT_ASSERT(rv == CKR_USER_ANOTHER_ALREADY_LOGGED_IN); |
| 138 | |
| 139 | rv = CRYPTOKI_F_PTR( C_Logout(hSession[0]) ); |
| 140 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 141 | |
| 142 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_USER, m_userPin1, m_userPin1Length - 1) ); |
| 143 | CPPUNIT_ASSERT(rv == CKR_PIN_INCORRECT); |
| 144 | |
| 145 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_USER, m_userPin1, m_userPin1Length) ); |
| 146 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 147 | |
| 148 | rv = CRYPTOKI_F_PTR( C_Login(hSession[0], CKU_USER, m_userPin1, m_userPin1Length) ); |
| 149 | CPPUNIT_ASSERT(rv == CKR_USER_ALREADY_LOGGED_IN); |
| 150 | } |
| 151 | |
| 152 | void UserTests::testLogout() |
| 153 | { |
| 154 | CK_RV rv; |
| 155 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
| 156 | |
| 157 | // Just make sure that we finalize any previous tests |
| 158 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 159 | |
| 160 | rv = CRYPTOKI_F_PTR( C_Logout(hSession) ); |
| 161 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 162 | |
| 163 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 164 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 165 | |
| 166 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 167 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 168 | |
| 169 | rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_SO, m_soPin1, m_soPin1Length) ); |
| 170 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 171 | |
| 172 | rv = CRYPTOKI_F_PTR( C_Logout(CK_INVALID_HANDLE) ); |
| 173 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 174 | |
| 175 | rv = CRYPTOKI_F_PTR( C_Logout(hSession) ); |
| 176 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 177 | |
| 178 | rv = CRYPTOKI_F_PTR( C_Logout(hSession) ); |
| 179 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 180 | } |
| 181 | |
| 182 | void UserTests::testSetPIN() |
| 183 | { |
| 184 | CK_RV rv; |
| 185 | const CK_UTF8CHAR_PTR pin2((CK_UTF8CHAR_PTR)"12345"); |
| 186 | const CK_ULONG pin2Length(strlen((char*)pin2)); |
| 187 | const CK_UTF8CHAR_PTR so2pin((CK_UTF8CHAR_PTR)"123456789"); |
| 188 | const CK_ULONG so2pinLength(strlen((char*)so2pin)); |
| 189 | CK_SESSION_HANDLE hSession; |
| 190 | |
| 191 | // Just make sure that we finalize any previous tests |
| 192 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 193 | |
| 194 | // Set up user PIN |
| 195 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 196 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 197 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 198 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 199 | rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_SO, m_soPin1, m_soPin1Length) ); |
| 200 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 201 | rv = CRYPTOKI_F_PTR( C_InitPIN(hSession, m_userPin1, m_userPin1Length) ); |
| 202 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 203 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 204 | |
| 205 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_userPin1, m_userPin1Length, pin2, pin2Length) ); |
| 206 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 207 | |
| 208 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 209 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 210 | |
| 211 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 212 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 213 | |
| 214 | rv = CRYPTOKI_F_PTR( C_SetPIN(CK_INVALID_HANDLE, m_userPin1, m_userPin1Length, pin2, pin2Length) ); |
| 215 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 216 | |
| 217 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_userPin1, m_userPin1Length, pin2, pin2Length) ); |
| 218 | CPPUNIT_ASSERT(rv == CKR_SESSION_READ_ONLY); |
| 219 | |
| 220 | rv = CRYPTOKI_F_PTR( C_CloseSession(hSession) ); |
| 221 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 222 | |
| 223 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 224 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 225 | |
| 226 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, NULL_PTR, m_userPin1Length, pin2, pin2Length) ); |
| 227 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 228 | |
| 229 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_userPin1, m_userPin1Length, NULL_PTR, pin2Length) ); |
| 230 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 231 | |
| 232 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_userPin1, m_userPin1Length, pin2, 0) ); |
| 233 | CPPUNIT_ASSERT(rv == CKR_PIN_LEN_RANGE); |
| 234 | |
| 235 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, pin2, pin2Length, pin2, pin2Length) ); |
| 236 | CPPUNIT_ASSERT(rv == CKR_PIN_INCORRECT); |
| 237 | |
| 238 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_userPin1, m_userPin1Length, pin2, pin2Length) ); |
| 239 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 240 | |
| 241 | rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_USER, pin2, pin2Length) ); |
| 242 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 243 | |
| 244 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_userPin1, m_userPin1Length, pin2, pin2Length) ); |
| 245 | CPPUNIT_ASSERT(rv == CKR_PIN_INCORRECT); |
| 246 | |
| 247 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, pin2, pin2Length, m_userPin1, m_userPin1Length) ); |
| 248 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 249 | |
| 250 | rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_SO, m_soPin1, m_soPin1Length) ); |
| 251 | CPPUNIT_ASSERT(rv == CKR_USER_ANOTHER_ALREADY_LOGGED_IN); |
| 252 | |
| 253 | rv = CRYPTOKI_F_PTR( C_Logout(hSession) ); |
| 254 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 255 | |
| 256 | rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_SO, m_soPin1, m_soPin1Length) ); |
| 257 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 258 | |
| 259 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, so2pin, so2pinLength, so2pin, so2pinLength) ); |
| 260 | CPPUNIT_ASSERT(rv == CKR_PIN_INCORRECT); |
| 261 | |
| 262 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_soPin1, m_soPin1Length, so2pin, so2pinLength) ); |
| 263 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 264 | |
| 265 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, m_soPin1, m_soPin1Length, m_soPin1, m_soPin1Length) ); |
| 266 | CPPUNIT_ASSERT(rv == CKR_PIN_INCORRECT); |
| 267 | |
| 268 | rv = CRYPTOKI_F_PTR( C_SetPIN(hSession, so2pin, so2pinLength, m_soPin1, m_soPin1Length) ); |
| 269 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 270 | } |