| 1 |
/* |
| 2 |
* COPYRIGHT (c) International Business Machines Corp. 2001-2023 |
| 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 |
#define OCK_NO_EP11_DEFINES |
| 12 |
#include "pkcs11types.h" |
| 13 |
#include "defs.h" |
| 14 |
#include "host_defs.h" |
| 15 |
#include "h_extern.h" |
| 16 |
#include "attributes.h" |
| 17 |
#include "trace.h" |
| 18 |
#include "ock_syslog.h" |
| 19 |
#include "stdll.h" |
| 20 |
|
| 21 |
#include <sys/time.h> |
| 22 |
#include <time.h> |
| 23 |
#include <stdint.h> |
| 24 |
|
| 25 |
#include "ep11_specific.h" |
| 26 |
|
| 27 |
CK_RV SC_CreateObject(STDLL_TokData_t *tokdata, |
| 28 |
ST_SESSION_HANDLE *sSession, CK_ATTRIBUTE_PTR pTemplate, |
| 29 |
CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject); |
| 30 |
CK_RV SC_DestroyObject(STDLL_TokData_t *tokdata, |
| 31 |
ST_SESSION_HANDLE *sSession, CK_OBJECT_HANDLE hObject); |
| 32 |
CK_RV SC_FindObjectsInit(STDLL_TokData_t *tokdata, |
| 33 |
ST_SESSION_HANDLE *sSession, |
| 34 |
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount); |
| 35 |
CK_RV SC_FindObjects(STDLL_TokData_t *tokdata, |
| 36 |
ST_SESSION_HANDLE *sSession, |
| 37 |
CK_OBJECT_HANDLE_PTR phObject, CK_ULONG ulMaxObjectCount, |
| 38 |
CK_ULONG_PTR pulObjectCount); |
| 39 |
CK_RV SC_FindObjectsFinal(STDLL_TokData_t *tokdata, |
| 40 |
ST_SESSION_HANDLE *sSession); |
| 41 |
CK_RV SC_GetAttributeValue(STDLL_TokData_t *tokdata, |
| 42 |
ST_SESSION_HANDLE *sSession, |
| 43 |
CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, |
| 44 |
CK_ULONG ulCount); |
| 45 |
CK_RV SC_SetAttributeValue(STDLL_TokData_t *tokdata, |
| 46 |
ST_SESSION_HANDLE *sSession, |
| 47 |
CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, |
| 48 |
CK_ULONG ulCount); |
| 49 |
CK_RV SC_OpenSession(STDLL_TokData_t *tokdata, CK_SLOT_ID sid, CK_FLAGS flags, |
| 50 |
CK_SESSION_HANDLE_PTR phSession); |
| 51 |
CK_RV SC_CloseSession(STDLL_TokData_t *tokdata, ST_SESSION_HANDLE *sSession, |
| 52 |
CK_BBOOL in_fork_initializer); |
| 53 |
|
| 54 |
static CK_RV ep11_open_helper_session(STDLL_TokData_t *tokdata, SESSION *sess, |
| 55 |
CK_SESSION_HANDLE_PTR phSession); |
| 56 |
static CK_RV ep11_close_helper_session(STDLL_TokData_t *tokdata, |
| 57 |
ST_SESSION_HANDLE *sSession, |
| 58 |
CK_BBOOL in_fork_initializer); |
| 59 |
static CK_RV update_ep11_object(STDLL_TokData_t *tokdata, |
| 60 |
ST_SESSION_HANDLE *handle, |
| 61 |
CK_OBJECT_HANDLE obj, |
| 62 |
ep11_serialno_t *serial_numbers, |
| 63 |
CK_ULONG num_serial_numbers); |
| 64 |
|
| 65 |
CK_BOOL ep11_is_session_object(CK_ATTRIBUTE_PTR attrs, CK_ULONG attrs_len) |
| 66 |
{ |
| 67 |
CK_ATTRIBUTE_PTR attr; |
| 68 |
|
| 69 |
attr = get_attribute_by_type(attrs, attrs_len, CKA_TOKEN); |
| 70 |
if (attr == NULL) |
| 71 |
return TRUE; |
| 72 |
|
| 73 |
if (attr->pValue == NULL) |
| 74 |
return TRUE; |
| 75 |
|
| 76 |
if (*((CK_BBOOL *)attr->pValue) == FALSE) |
| 77 |
return TRUE; |
| 78 |
|
| 79 |
return FALSE; |
| 80 |
} |
| 81 |
|
| 82 |
CK_BOOL ep11_is_private_object(CK_ATTRIBUTE_PTR attrs, CK_ULONG attrs_len) |
| 83 |
{ |
| 84 |
CK_ATTRIBUTE_PTR attr; |
| 85 |
|
| 86 |
attr = get_attribute_by_type(attrs, attrs_len, CKA_PRIVATE); |
| 87 |
if (attr == NULL) |
| 88 |
return FALSE; |
| 89 |
|
| 90 |
if (attr->pValue == NULL) |
| 91 |
return FALSE; |
| 92 |
|
| 93 |
return *((CK_BBOOL *)attr->pValue); |
| 94 |
} |
| 95 |
|
| 96 |
CK_RV ep11tok_relogin_session(STDLL_TokData_t *tokdata, SESSION *session) |
| 97 |
{ |
| 98 |
ep11_private_data_t *ep11_data = tokdata->private_data; |
| 99 |
ep11_session_t *ep11_session = (ep11_session_t *)session->private_data; |
| 100 |
login_logout_data_t data = { 0 }; |
| 101 |
ST_SESSION_HANDLE handle = { |
| 102 |
.slotID = session->session_info.slotID, |
| 103 |
.sessionh = session->handle |
| 104 |
}; |
| 105 |
CK_SESSION_HANDLE helper_session = CK_INVALID_HANDLE; |
| 106 |
CK_ULONG num_serial_numbers_before = 0; |
| 107 |
CK_RV rc, rc2; |
| 108 |
|
| 109 |
TRACE_INFO("%s session=%lu\n", __func__, session->handle); |
| 110 |
|
| 111 |
if (!ep11_data->strict_mode && !ep11_data->vhsm_mode && |
| 112 |
!ep11_data->fips_session_mode) |
| 113 |
return CKR_OK; |
| 114 |
|
| 115 |
if (ep11_data->strict_mode && ep11_session == NULL) { |
| 116 |
TRACE_INFO("%s Session not yet logged in\n", __func__); |
| 117 |
return CKR_USER_NOT_LOGGED_IN; |
| 118 |
} |
| 119 |
|
| 120 |
if (ep11_data->vhsm_mode || ep11_data->fips_session_mode) { |
| 121 |
if (pthread_mutex_lock(&ep11_data->session_mutex)) { |
| 122 |
TRACE_ERROR("%s Failed to lock session lock\n", __func__); |
| 123 |
return CKR_CANT_LOCK; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (ep11_data->strict_mode) |
| 128 |
num_serial_numbers_before = ep11_session->num_serial_numbers; |
| 129 |
|
| 130 |
data.tokdata = tokdata; |
| 131 |
data.ep11_session = ep11_session; |
| 132 |
data.relogin = TRUE; |
| 133 |
rc = handle_all_ep11_cards(&ep11_data->target_list, ep11_login_handler, |
| 134 |
&data); |
| 135 |
if (rc != CKR_OK) { |
| 136 |
TRACE_ERROR("%s handle_all_ep11_cards failed: 0x%lx\n", __func__, rc); |
| 137 |
goto unlock; |
| 138 |
} |
| 139 |
|
| 140 |
/* Update Session object if list of serial numbers changed */ |
| 141 |
if (ep11_data->strict_mode && |
| 142 |
num_serial_numbers_before != ep11_session->num_serial_numbers) { |
| 143 |
switch (session->session_info.state) { |
| 144 |
case CKS_RW_SO_FUNCTIONS: |
| 145 |
case CKS_RO_PUBLIC_SESSION: |
| 146 |
case CKS_RW_PUBLIC_SESSION: |
| 147 |
TRACE_INFO("%s Public or SO session\n", __func__); |
| 148 |
rc = CKR_OK; |
| 149 |
goto unlock; |
| 150 |
case CKS_RO_USER_FUNCTIONS: |
| 151 |
rc = ep11_open_helper_session(tokdata, session, &helper_session); |
| 152 |
if (rc != CKR_OK) |
| 153 |
goto unlock; |
| 154 |
handle.sessionh = helper_session; |
| 155 |
break; |
| 156 |
default: |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
rc = update_ep11_object(tokdata, &handle, ep11_session->session_object, |
| 161 |
ep11_session->serial_numbers, |
| 162 |
ep11_session->num_serial_numbers); |
| 163 |
if (rc != CKR_OK) { |
| 164 |
TRACE_ERROR("%s update_ep11_object failed: 0x%lx\n", __func__, rc); |
| 165 |
goto unlock; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
unlock: |
| 170 |
if (ep11_data->vhsm_mode || ep11_data->fips_session_mode) { |
| 171 |
if (pthread_mutex_unlock(&ep11_data->session_mutex)) { |
| 172 |
TRACE_ERROR("%s Failed to unlock session lock\n", __func__); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (helper_session != CK_INVALID_HANDLE) { |
| 177 |
rc2 = ep11_close_helper_session(tokdata, &handle, FALSE); |
| 178 |
if (rc2 != CKR_OK) |
| 179 |
TRACE_ERROR("%s ep11_close_helper_session failed: 0x%lx\n", |
| 180 |
__func__, rc2); |
| 181 |
} |
| 182 |
|
| 183 |
return rc; |
| 184 |
} |
| 185 |
|
| 186 |
void ep11_get_pin_blob(STDLL_TokData_t *tokdata, ep11_session_t *ep11_session, |
| 187 |
CK_BOOL is_session_obj, CK_BOOL is_private_obj, |
| 188 |
CK_BYTE **pin_blob, CK_ULONG *pin_blob_len) |
| 189 |
{ |
| 190 |
ep11_private_data_t *ep11_data = tokdata->private_data; |
| 191 |
|
| 192 |
if (ep11_data->strict_mode && ep11_session != NULL && |
| 193 |
ep11_session->pin_blob_valid && is_session_obj && is_private_obj) { |
| 194 |
*pin_blob = ep11_session->session_pin_blob; |
| 195 |
*pin_blob_len = sizeof(ep11_session->session_pin_blob); |
| 196 |
TRACE_DEVEL("%s Strict mode with CKA_TOKEN=FALSE & CKA_PRIVATE=TRUE " |
| 197 |
"-> pass session pin_blob\n", __func__); |
| 198 |
} else if (ep11_data->vhsm_mode && ep11_data->vhsm_pin_blob_valid && |
| 199 |
is_private_obj) { |
| 200 |
*pin_blob = ep11_data->vhsm_pin_blob; |
| 201 |
*pin_blob_len = sizeof(ep11_data->vhsm_pin_blob); |
| 202 |
TRACE_DEVEL("%s VHSM mode with CKA_PRIVATE=TRUE -> pass VHSM pin_blob\n", |
| 203 |
__func__); |
| 204 |
} else if (ep11_data->fips_session_mode && ep11_data->fips_pin_blob_valid) { |
| 205 |
*pin_blob = ep11_data->fips_pin_blob; |
| 206 |
*pin_blob_len = sizeof(ep11_data->fips_pin_blob); |
| 207 |
TRACE_DEVEL("%s FIPS session mode -> pass FIPS pin_blob\n", __func__); |
| 208 |
} else { |
| 209 |
*pin_blob = NULL; |
| 210 |
*pin_blob_len = 0; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
static CK_RV ep11_open_helper_session(STDLL_TokData_t *tokdata, SESSION *sess, |
| 215 |
CK_SESSION_HANDLE_PTR phSession) |
| 216 |
{ |
| 217 |
CK_RV rc; |
| 218 |
|
| 219 |
TRACE_INFO("%s\n", __func__); |
| 220 |
|
| 221 |
rc = SC_OpenSession(tokdata, sess->session_info.slotID, |
| 222 |
CKF_RW_SESSION | CKF_SERIAL_SESSION | |
| 223 |
CKF_EP11_HELPER_SESSION, phSession); |
| 224 |
if (rc != CKR_OK) |
| 225 |
TRACE_ERROR("%s SC_OpenSession failed: 0x%lx\n", __func__, rc); |
| 226 |
|
| 227 |
return rc; |
| 228 |
} |
| 229 |
|
| 230 |
static CK_RV ep11_close_helper_session(STDLL_TokData_t *tokdata, |
| 231 |
ST_SESSION_HANDLE *sSession, |
| 232 |
CK_BBOOL in_fork_initializer) |
| 233 |
{ |
| 234 |
CK_RV rc; |
| 235 |
|
| 236 |
TRACE_INFO("%s\n", __func__); |
| 237 |
|
| 238 |
rc = SC_CloseSession(tokdata, sSession, in_fork_initializer); |
| 239 |
if (rc != CKR_OK) |
| 240 |
TRACE_ERROR("%s SC_CloseSession failed: 0x%lx\n", __func__, rc); |
| 241 |
|
| 242 |
return rc; |
| 243 |
} |
| 244 |
|
| 245 |
static CK_RV generate_ep11_session_id(STDLL_TokData_t *tokdata, |
| 246 |
SESSION *session, |
| 247 |
ep11_session_t *ep11_session) |
| 248 |
{ |
| 249 |
ep11_private_data_t *ep11_data = tokdata->private_data; |
| 250 |
CK_RV rc; |
| 251 |
struct { |
| 252 |
CK_SESSION_HANDLE handle; |
| 253 |
CK_SLOT_ID slot; |
| 254 |
struct timeval timeofday; |
| 255 |
clock_t clock; |
| 256 |
pid_t pid; |
| 257 |
CK_BYTE nonce[32]; |
| 258 |
} session_id_data; |
| 259 |
CK_MECHANISM mech; |
| 260 |
CK_ULONG len; |
| 261 |
libica_sha_context_t ctx; |
| 262 |
ep11_target_info_t* target_info; |
| 263 |
|
| 264 |
session_id_data.handle = session->handle; |
| 265 |
session_id_data.slot = session->session_info.slotID; |
| 266 |
gettimeofday(&session_id_data.timeofday, NULL); |
| 267 |
session_id_data.clock = clock(); |
| 268 |
session_id_data.pid = tokdata->real_pid; |
| 269 |
rc = rng_generate(tokdata, session_id_data.nonce, |
| 270 |
sizeof(session_id_data.nonce)); |
| 271 |
if (rc != CKR_OK) { |
| 272 |
TRACE_ERROR("%s rng_generate failed: 0x%lx\n", __func__, rc); |
| 273 |
return rc; |
| 274 |
} |
| 275 |
|
| 276 |
target_info = get_target_info(tokdata); |
| 277 |
if (target_info == NULL) |
| 278 |
return CKR_FUNCTION_FAILED; |
| 279 |
|
| 280 |
mech.mechanism = CKM_SHA256; |
| 281 |
mech.pParameter = NULL; |
| 282 |
mech.ulParameterLen = 0; |
| 283 |
|
| 284 |
len = sizeof(ep11_session->session_id); |
| 285 |
if (ep11tok_libica_digest_available(tokdata, ep11_data, mech.mechanism)) { |
| 286 |
rc = ep11tok_libica_digest(tokdata, ep11_data, mech.mechanism, &ctx, |
| 287 |
(CK_BYTE_PTR)&session_id_data, |
| 288 |
sizeof(session_id_data), |
| 289 |
ep11_session->session_id, &len, |
| 290 |
SHA_MSG_PART_ONLY); |
| 291 |
} else { |
| 292 |
RETRY_SINGLE_APQN_START(tokdata, rc) |
| 293 |
rc = dll_m_DigestSingle(&mech, (CK_BYTE_PTR)&session_id_data, |
| 294 |
sizeof(session_id_data), |
| 295 |
ep11_session->session_id, &len, |
| 296 |
target_info->target); |
| 297 |
RETRY_SINGLE_APQN_END(rc, tokdata, target_info) |
| 298 |
} |
| 299 |
|
| 300 |
put_target_info(tokdata, target_info); |
| 301 |
|
| 302 |
if (rc != CKR_OK) { |
| 303 |
rc = ep11_error_to_pkcs11_error(rc, session); |
| 304 |
TRACE_ERROR("%s Digest failed: 0x%lx\n", __func__, rc); |
| 305 |
return rc; |
| 306 |
} |
| 307 |
|
| 308 |
return CKR_OK; |
| 309 |
} |
| 310 |
|
| 311 |
static CK_RV create_ep11_object(STDLL_TokData_t *tokdata, |
| 312 |
ST_SESSION_HANDLE *handle, |
| 313 |
CK_BYTE *session_id, |
| 314 |
CK_ULONG session_id_len, |
| 315 |
CK_BYTE *pin_blob, CK_ULONG pin_blob_len, |
| 316 |
ep11_serialno_t *serial_numbers, |
| 317 |
CK_ULONG num_serial_numbers, |
| 318 |
CK_OBJECT_HANDLE *obj) |
| 319 |
{ |
| 320 |
ep11_private_data_t *ep11_data = tokdata->private_data; |
| 321 |
CK_RV rc; |
| 322 |
CK_OBJECT_CLASS class = CKO_HW_FEATURE; |
| 323 |
CK_HW_FEATURE_TYPE type = CKH_IBM_EP11_SESSION; |
| 324 |
CK_BYTE subject[] = "EP11 Session Object"; |
| 325 |
pid_t pid; |
| 326 |
CK_DATE date; |
| 327 |
CK_BYTE cktrue = TRUE; |
| 328 |
time_t t; |
| 329 |
struct tm *tm; |
| 330 |
char tmp[40]; |
| 331 |
CK_BYTE *app_data = NULL; |
| 332 |
CK_ULONG app_data_len; |
| 333 |
|
| 334 |
CK_ATTRIBUTE attrs[] = { |
| 335 |
{ CKA_CLASS, &class, sizeof(class) }, |
| 336 |
{ CKA_TOKEN, &cktrue, sizeof(cktrue) }, |
| 337 |
{ CKA_PRIVATE, &cktrue, sizeof(cktrue) }, |
| 338 |
{ CKA_HIDDEN, &cktrue, sizeof(cktrue) }, |
| 339 |
{ CKA_HW_FEATURE_TYPE, &type, sizeof(type) }, |
| 340 |
{ CKA_SUBJECT, &subject, sizeof(subject) }, |
| 341 |
{ CKA_VALUE, pin_blob, pin_blob_len }, |
| 342 |
{ CKA_ID, session_id, session_id_len }, |
| 343 |
{ CKA_APPLICATION, NULL, 0 }, // Keep at index 8 |
| 344 |
{ CKA_OWNER, &pid, sizeof(pid) }, |
| 345 |
{ CKA_START_DATE, &date, sizeof(date) } |
| 346 |
}; |
| 347 |
|
| 348 |
app_data_len = sizeof(ep11_target_t) + |
| 349 |
num_serial_numbers * sizeof(ep11_serialno_t); |
| 350 |
app_data = malloc(app_data_len); |
| 351 |
if (app_data == NULL) { |
| 352 |
TRACE_ERROR("%s Failed to allocate memory\n", __func__); |
| 353 |
rc = CKR_HOST_MEMORY; |
| 354 |
goto out; |
| 355 |
} |
| 356 |
|
| 357 |
memcpy(app_data, &ep11_data->target_list, sizeof(ep11_target_t)); |
| 358 |
if (num_serial_numbers > 0) |
| 359 |
memcpy(app_data + sizeof(ep11_target_t), serial_numbers, |
| 360 |
num_serial_numbers * sizeof(ep11_serialno_t)); |
| 361 |
attrs[8].pValue = app_data; |
| 362 |
attrs[8].ulValueLen = app_data_len; |
| 363 |
|
| 364 |
pid = tokdata->real_pid; |
| 365 |
time(&t); |
| 366 |
tm = localtime(&t); |
| 367 |
sprintf(tmp, "%04d%02d%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); |
| 368 |
memcpy(date.year, tmp, 4); |
| 369 |
memcpy(date.month, tmp + 4, 2); |
| 370 |
memcpy(date.day, tmp + 4 + 2, 2); |
| 371 |
|
| 372 |
rc = SC_CreateObject(tokdata, handle, |
| 373 |
attrs, sizeof(attrs) / sizeof(CK_ATTRIBUTE), obj); |
| 374 |
if (rc != CKR_OK) { |
| 375 |
TRACE_ERROR("%s SC_CreateObject failed: 0x%lx\n", __func__, rc); |
| 376 |
} |
| 377 |
|
| 378 |
out: |
| 379 |
if (app_data != NULL) |
| 380 |
free(app_data); |
| 381 |
|
| 382 |
return rc; |
| 383 |
} |
| 384 |
|
| 385 |
static CK_RV update_ep11_object(STDLL_TokData_t *tokdata, |
| 386 |
ST_SESSION_HANDLE *handle, |
| 387 |
CK_OBJECT_HANDLE obj, |
| 388 |
ep11_serialno_t *serial_numbers, |
| 389 |
CK_ULONG num_serial_numbers) |
| 390 |
{ |
| 391 |
ep11_private_data_t *ep11_data = tokdata->private_data; |
| 392 |
CK_RV rc; |
| 393 |
CK_BYTE *app_data = NULL; |
| 394 |
CK_ULONG app_data_len; |
| 395 |
CK_ATTRIBUTE attr = { CKA_APPLICATION, NULL, 0 }; |
| 396 |
|
| 397 |
app_data_len = sizeof(ep11_target_t) + |
| 398 |
num_serial_numbers * sizeof(ep11_serialno_t); |
| 399 |
app_data = malloc(app_data_len); |
| 400 |
if (app_data == NULL) { |
| 401 |
TRACE_ERROR("%s Failed to allocate memory\n", __func__); |
| 402 |
rc = CKR_HOST_MEMORY; |
| 403 |
goto out; |
| 404 |
} |
| 405 |
|
| 406 |
memcpy(app_data, &ep11_data->target_list, sizeof(ep11_target_t)); |
| 407 |
if (num_serial_numbers > 0) |
| 408 |
memcpy(app_data + sizeof(ep11_target_t), serial_numbers, |
| 409 |
num_serial_numbers * sizeof(ep11_serialno_t)); |
| 410 |
attr.pValue = app_data; |
| 411 |
attr.ulValueLen = app_data_len; |
| 412 |
|
| 413 |
rc = SC_SetAttributeValue(tokdata, handle, obj, &attr, 1); |
| 414 |
if (rc != CKR_OK) { |
| 415 |
TRACE_ERROR("%s SC_SetAttributeValue failed: 0x%lx\n", __func__, rc); |
| 416 |
} |
| 417 |
|
| 418 |
out: |
| 419 |
if (app_data != NULL) |
| 420 |
free(app_data); |
| 421 |
|
| 422 |
return rc; |
| 423 |
} |
| 424 |
|
| 425 |
static CK_RV get_pin(STDLL_TokData_t *tokdata, SESSION *session, |
| 426 |
CK_HW_FEATURE_TYPE type, CK_BYTE *pin, CK_ULONG pin_len) |
| 427 |
{ |
| 428 |
CK_RV rc; |
| 429 |
ST_SESSION_HANDLE handle = { |
| 430 |
.slotID = session->session_info.slotID, |
| 431 |
.sessionh = session->handle |
| 432 |
}; |
| 433 |
CK_OBJECT_HANDLE obj_store[16]; |
| 434 |
CK_ULONG objs_found = 0; |
| 435 |
CK_OBJECT_CLASS class = CKO_HW_FEATURE; |
| 436 |
CK_BBOOL cktrue = TRUE; |
| 437 |
CK_ATTRIBUTE pinobj_template[] = { |
| 438 |
{ CKA_CLASS, &class, sizeof(class) }, |
| 439 |
{ CKA_TOKEN, &cktrue, sizeof(cktrue) }, |
| 440 |
{ CKA_PRIVATE, &cktrue, sizeof(cktrue) }, |
| 441 |
{ CKA_HIDDEN, &cktrue, sizeof(cktrue) }, |
| 442 |
{ CKA_HW_FEATURE_TYPE, &type, sizeof(type) }, |
| 443 |
}; |
| 444 |
CK_ATTRIBUTE attrs[] = { |
| 445 |
{ CKA_VALUE, pin, pin_len }, |
| 446 |
}; |
| 447 |
|
| 448 |
rc = SC_FindObjectsInit(tokdata, &handle, pinobj_template, |
| 449 |
sizeof(pinobj_template) / sizeof(CK_ATTRIBUTE)); |
| 450 |
if (rc != CKR_OK) { |
| 451 |
TRACE_ERROR("%s SC_FindObjectsInit failed: 0x%lx\n", __func__, rc); |
| 452 |
goto out; |
| 453 |
} |
| 454 |
|
| 455 |
rc = SC_FindObjects(tokdata, &handle, obj_store, 16, &objs_found); |
| 456 |
if (rc != CKR_OK) { |
| 457 |
TRACE_ERROR("%s SC_FindObjects failed: 0x%lx\n", __func__, rc); |
| 458 |
goto out; |
| 459 |
} |
| 460 |
|
| 461 |
if (objs_found == 0) { |
| 462 |
rc = CKR_FUNCTION_FAILED; |
| 463 |
TRACE_ERROR("%s No VHSMPIN object found\n", __func__); |
| 464 |
goto out; |
| 465 |
} |
| 466 |
|
| 467 |
rc = SC_GetAttributeValue(tokdata, &handle, obj_store[0], |
| 468 |
attrs, sizeof(attrs) / sizeof(CK_ATTRIBUTE)); |
| 469 |
if (rc != CKR_OK) { |
| 470 |
TRACE_ERROR("%s SC_GetAttributeValue failed: 0x%lx\n", __func__, rc); |
| 471 |
goto out; |
| 472 |
} |
| 473 |
|
| 474 |
out: |
| 475 |
SC_FindObjectsFinal(tokdata, &handle); |
| 476 |
return rc; |
| 477 |
} |
| 478 |
|
| 479 |
static CK_RV get_serial_number(STDLL_TokData_t *tokdata, target_t target, |
| 480 |
uint_32 adapter, uint_32 domain, |
| 481 |
ep11_serialno_t serial_number) |
| 482 |
{ |
| 483 |
CK_IBM_XCP_INFO xcp_info; |
| 484 |
CK_ULONG xcp_info_len = sizeof(xcp_info); |
| 485 |
CK_RV rc; |
| 486 |
|
| 487 |
UNUSED(tokdata); |
| 488 |
|
| 489 |
rc = dll_m_get_xcp_info(&xcp_info, &xcp_info_len, CK_IBM_XCPQ_MODULE, 0, |
| 490 |
target); |
| 491 |
if (rc != CKR_OK) { |
| 492 |
TRACE_ERROR("%s Failed to query module version from adapter %02X.%04X\n", |
| 493 |
__func__, adapter, domain); |
| 494 |
/* card may no longer be online, so ignore this error situation */ |
| 495 |
return rc; |
| 496 |
} |
| 497 |
|
| 498 |
memcpy(serial_number, xcp_info.serialNumber, sizeof(ep11_serialno_t)); |
| 499 |
|
| 500 |
TRACE_DEVEL("%s serial number of adapter %02X.%04X: %.16s\n", |
| 501 |
__func__, adapter, domain, serial_number); |
| 502 |
|
| 503 |
return CKR_OK; |
| 504 |
} |
| 505 |
|
| 506 |
static CK_RV add_serial_number(STDLL_TokData_t *tokdata, |
| 507 |
ep11_session_t *ep11_session, |
| 508 |
ep11_serialno_t serial_number) |
| 509 |
{ |
| 510 |
CK_ULONG i; |
| 511 |
ep11_serialno_t *tmp; |
| 512 |
|
| 513 |
UNUSED(tokdata); |
| 514 |
|
| 515 |
for (i = 0; i < ep11_session->num_serial_numbers; i++) { |
| 516 |
if (memcmp(ep11_session->serial_numbers[i], serial_number, 16) == 0) |
| 517 |
return CKR_OK; |
| 518 |
} |
| 519 |
|
| 520 |
TRACE_DEVEL("%s adding serial number : %.16s\n", __func__, serial_number); |
| 521 |
|
| 522 |
tmp = realloc(ep11_session->serial_numbers, sizeof(ep11_serialno_t) * |
| 523 |
(ep11_session->num_serial_numbers + 1)); |
| 524 |
if (tmp == NULL) { |
| 525 |
TRACE_ERROR("%s Failed allocate serial number list\n",__func__); |
| 526 |
return CKR_HOST_MEMORY; |
| 527 |
} |
| 528 |
|
| 529 |
memcpy(tmp[ep11_session->num_serial_numbers], serial_number, |
| 530 |
sizeof(ep11_serialno_t)); |
| 531 |
|
| 532 |
ep11_session->num_serial_numbers++; |
| 533 |
ep11_session->serial_numbers = tmp; |
| 534 |
|
| 535 |
return CKR_OK; |
| 536 |
} |
| 537 |
|
| 538 |
static void remove_serial_number(STDLL_TokData_t *tokdata, |
| 539 |
ep11_session_t *ep11_session, |
| 540 |
ep11_serialno_t serial_number) |
| 541 |
{ |
| 542 |
CK_ULONG i; |
| 543 |
CK_BOOL found = FALSE; |
| 544 |
|
| 545 |
UNUSED(tokdata); |
| 546 |
|
| 547 |
for (i = 0; i < ep11_session->num_serial_numbers; i++) { |
| 548 |
if (memcmp(ep11_session->serial_numbers[i], serial_number, |
| 549 |
sizeof(ep11_serialno_t)) == 0) { |
| 550 |
found = TRUE; |
| 551 |
break; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
if (!found) |
| 556 |
return; |
| 557 |
|
| 558 |
TRACE_DEVEL("%s removing serial number : %.16s\n", __func__, serial_number); |
| 559 |
|
| 560 |
memmove(ep11_session->serial_numbers[i], |
| 561 |
ep11_session->serial_numbers[i + 1], |
| 562 |
(ep11_session->num_serial_numbers - i) * sizeof(ep11_serialno_t)); |
| 563 |
memset(ep11_session->serial_numbers[ep11_session->num_serial_numbers - 1], |
| 564 |
0, sizeof(ep11_serialno_t)); |
| 565 |
|
| 566 |
/* Don't shrink the area, just leave it allocated as is */ |
| 567 |
ep11_session->num_serial_numbers--; |
| 568 |
|
| 569 |
return; |
| 570 |
} |
| 571 |
|
| 572 |
static void free_ep11_session(ep11_session_t *ep11_session) |
| 573 |
{ |
| 574 |
memset(ep11_session->session_pin_blob, 0, |
| 575 |
sizeof(ep11_session->session_pin_blob)); |
| 576 |
if (ep11_session->serial_numbers != NULL) |
| 577 |
free(ep11_session->serial_numbers); |
| 578 |
free(ep11_session); |
| 579 |
} |
| 580 |
|
| 581 |
CK_RV ep11_login_handler(uint_32 adapter, uint_32 domain, void *handler_data) |
| 582 |
{ |
| 583 |
login_logout_data_t *data = (login_logout_data_t *)handler_data; |
| 584 |
ep11_private_data_t *ep11_data = data->tokdata->private_data; |
| 585 |
target_t target; |
| 586 |
CK_RV rc; |
| 587 |
CK_BYTE pin_blob[MAX(sizeof(ep11_data->fips_pin_blob), |
| 588 |
MAX(sizeof(ep11_data->vhsm_pin_blob), |
| 589 |
sizeof(data->ep11_session->session_pin_blob)))]; |
| 590 |
CK_ULONG pin_blob_len; |
| 591 |
CK_BYTE *pin = (CK_BYTE *)DEFAULT_EP11_PIN; |
| 592 |
CK_ULONG pin_len = strlen(DEFAULT_EP11_PIN); |
| 593 |
CK_BYTE *nonce = NULL; |
| 594 |
CK_ULONG nonce_len = 0; |
| 595 |
session_nonce_t session_nonce; |
| 596 |
ep11_serialno_t serial_number; |
| 597 |
|
| 598 |
TRACE_INFO("Logging in adapter %02X.%04X\n", adapter, domain); |
| 599 |
|
| 600 |
rc = get_ep11_target_for_apqn(adapter, domain, &target, 0); |
| 601 |
if (rc != CKR_OK) |
| 602 |
return rc; |
| 603 |
|
| 604 |
if (ep11_data->fips_session_mode && |
| 605 |
(ep11_data->session_refcount == 0 || data->relogin)) { |
| 606 |
session_nonce.slot_id = htobe32(data->tokdata->slot_id); |
| 607 |
memcpy(session_nonce.purpose, FIPS_NONCE_PURPOSE, 12); |
| 608 |
nonce = (CK_BYTE *)&session_nonce; |
| 609 |
nonce_len = sizeof(session_nonce); |
| 610 |
|
| 611 |
pin_blob_len = sizeof(ep11_data->fips_pin_blob); |
| 612 |
rc = do_LoginExtended(XCP_LOGIN_ALG_F2021, XCP_LOGIN_IMPR_EC_P521, |
| 613 |
ep11_data->fips_pin, sizeof(ep11_data->fips_pin), |
| 614 |
nonce, nonce_len, pin_blob, &pin_blob_len, |
| 615 |
NULL, target); |
| 616 |
if (rc != CKR_OK || pin_blob_len != sizeof(ep11_data->fips_pin_blob)) { |
| 617 |
rc = ep11_error_to_pkcs11_error(rc, NULL); |
| 618 |
TRACE_ERROR("%s do_LoginExtended failed: 0x%lx pin_blob_len: %lu\n", |
| 619 |
__func__, rc, pin_blob_len); |
| 620 |
/* ignore the error here, the adapter may not be able to perform |
| 621 |
* m_Login at this moment */ |
| 622 |
rc = CKR_OK; |
| 623 |
goto vhsm_mode; |
| 624 |
} |
| 625 |
|
| 626 |
#ifdef DEBUG |
| 627 |
TRACE_DEBUG("EP11 FIPS Pin blob:\n"); |
| 628 |
TRACE_DEBUG_DUMP(" ", pin_blob, pin_blob_len); |
| 629 |
#endif |
| 630 |
|
| 631 |
if (ep11_data->fips_pin_blob_valid) { |
| 632 |
/* First part of pin-blob (keypart and session) must be equal */ |
| 633 |
if (memcmp(ep11_data->fips_pin_blob, pin_blob, XCP_WK_BYTES) != |
| 634 |
0) { |
| 635 |
TRACE_ERROR("%s FIPS-Pin blob not equal to previous one\n", |
| 636 |
__func__); |
| 637 |
OCK_SYSLOG(LOG_ERR, |
| 638 |
"%s: Error: FIPS-Pin blob of adapter %02X.%04X is " |
| 639 |
"not equal to other adapters for same session\n", |
| 640 |
__func__, adapter, domain); |
| 641 |
rc = CKR_DEVICE_ERROR; |
| 642 |
goto out; |
| 643 |
} |
| 644 |
} else { |
| 645 |
memcpy(ep11_data->fips_pin_blob, pin_blob, pin_blob_len); |
| 646 |
ep11_data->fips_pin_blob_valid = TRUE; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
vhsm_mode: |
| 651 |
if (ep11_data->vhsm_mode) { |
| 652 |
pin = ep11_data->vhsm_pin; |
| 653 |
pin_len = sizeof(ep11_data->vhsm_pin); |
| 654 |
} |
| 655 |
|
| 656 |
if (ep11_data->vhsm_mode && |
| 657 |
(ep11_data->session_refcount == 0 || data->relogin)) { |
| 658 |
session_nonce.slot_id = htobe32(data->tokdata->slot_id); |
| 659 |
memcpy(session_nonce.purpose, VHSM_NONCE_PURPOSE, 12); |
| 660 |
nonce = (CK_BYTE *)&session_nonce; |
| 661 |
nonce_len = sizeof(session_nonce); |
| 662 |
|
| 663 |
pin_blob_len = sizeof(ep11_data->vhsm_pin_blob); |
| 664 |
rc = dll_m_Login(pin, pin_len, nonce, nonce_len, |
| 665 |
pin_blob, &pin_blob_len, target); |
| 666 |
if (rc != CKR_OK || pin_blob_len != sizeof(ep11_data->vhsm_pin_blob)) { |
| 667 |
rc = ep11_error_to_pkcs11_error(rc, NULL); |
| 668 |
TRACE_ERROR("%s dll_m_Login failed: 0x%lx pin_blob_len: %lu\n", |
| 669 |
__func__, rc, pin_blob_len); |
| 670 |
/* ignore the error here, the adapter may not be able to perform |
| 671 |
* m_Login at this moment */ |
| 672 |
rc = CKR_OK; |
| 673 |
goto strict_mode; |
| 674 |
} |
| 675 |
#ifdef DEBUG |
| 676 |
TRACE_DEBUG("EP11 VHSM Pin blob:\n"); |
| 677 |
TRACE_DEBUG_DUMP(" ", pin_blob, pin_blob_len); |
| 678 |
#endif |
| 679 |
|
| 680 |
if (ep11_data->vhsm_pin_blob_valid) { |
| 681 |
/* First part of pin-blob (keypart and session) must be equal */ |
| 682 |
if (memcmp(ep11_data->vhsm_pin_blob, pin_blob, XCP_WK_BYTES) != |
| 683 |
0) { |
| 684 |
TRACE_ERROR("%s VHSM-Pin blob not equal to previous one\n", |
| 685 |
__func__); |
| 686 |
OCK_SYSLOG(LOG_ERR, |
| 687 |
"%s: Error: VHSM-Pin blob of adapter %02X.%04X is " |
| 688 |
"not equal to other adapters for same session\n", |
| 689 |
__func__, adapter, domain); |
| 690 |
rc = CKR_DEVICE_ERROR; |
| 691 |
goto out; |
| 692 |
} |
| 693 |
} else { |
| 694 |
memcpy(ep11_data->vhsm_pin_blob, pin_blob, |
| 695 |
sizeof(ep11_data->vhsm_pin_blob)); |
| 696 |
ep11_data->vhsm_pin_blob_valid = TRUE; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
strict_mode: |
| 701 |
if (ep11_data->strict_mode && data->ep11_session != NULL) { |
| 702 |
rc = get_serial_number(data->tokdata, target, adapter, domain, |
| 703 |
serial_number); |
| 704 |
if (rc != CKR_OK) { |
| 705 |
/* ignore the error here, the adapter may not be able to perform |
| 706 |
* the query at this moment */ |
| 707 |
rc = CKR_OK; |
| 708 |
goto out; |
| 709 |
} |
| 710 |
|
| 711 |
nonce = data->ep11_session->session_id; |
| 712 |
nonce_len = sizeof(data->ep11_session->session_id); |
| 713 |
/* pin is already set to default pin or vhsm pin (if VHSM mode) */ |
| 714 |
|
| 715 |
pin_blob_len = sizeof(data->ep11_session->session_pin_blob); |
| 716 |
rc = dll_m_Login(pin, pin_len, nonce, nonce_len, |
| 717 |
pin_blob, &pin_blob_len, target); |
| 718 |
if (rc != CKR_OK || |
| 719 |
pin_blob_len != sizeof(data->ep11_session->session_pin_blob)) { |
| 720 |
rc = ep11_error_to_pkcs11_error(rc, NULL); |
| 721 |
TRACE_ERROR("%s dll_m_Login failed: 0x%lx pin_blob_len: %lu\n", |
| 722 |
__func__, rc, pin_blob_len); |
| 723 |
/* ignore the error here, the adapter may not be able to perform |
| 724 |
* m_Login at this moment */ |
| 725 |
rc = CKR_OK; |
| 726 |
goto out; |
| 727 |
} |
| 728 |
#ifdef DEBUG |
| 729 |
TRACE_DEBUG("EP11 Session Pin blob:\n"); |
| 730 |
TRACE_DEBUG_DUMP(" ", pin_blob, pin_blob_len); |
| 731 |
#endif |
| 732 |
|
| 733 |
if (data->ep11_session->pin_blob_valid) { |
| 734 |
/* First part of pin-blob (keypart and session) must be equal */ |
| 735 |
if (memcmp(data->ep11_session->session_pin_blob, pin_blob, |
| 736 |
XCP_WK_BYTES) |
| 737 |
!= 0) { |
| 738 |
TRACE_ERROR("%s Pin blob not equal to previous one\n", |
| 739 |
__func__); |
| 740 |
OCK_SYSLOG(LOG_ERR, |
| 741 |
"%s: Error: Pin blob of adapter %02X.%04X is not " |
| 742 |
"equal to other adapters for same session\n", |
| 743 |
__func__, adapter, domain); |
| 744 |
rc = CKR_DEVICE_ERROR; |
| 745 |
goto out; |
| 746 |
} |
| 747 |
} else { |
| 748 |
memcpy(data->ep11_session->session_pin_blob, pin_blob, |
| 749 |
sizeof(data->ep11_session->session_pin_blob)); |
| 750 |
data->ep11_session->pin_blob_valid = TRUE; |
| 751 |
} |
| 752 |
|
| 753 |
rc = add_serial_number(data->tokdata, data->ep11_session, |
| 754 |
serial_number); |
| 755 |
} |
| 756 |
|
| 757 |
out: |
| 758 |
free_ep11_target_for_apqn(target); |
| 759 |
return rc; |
| 760 |
} |
| 761 |
|
| 762 |
static CK_RV ep11_logout_handler(uint_32 adapter, uint_32 domain, |
| 763 |
void *handler_data) |
| 764 |
{ |
| 765 |
login_logout_data_t *data = (login_logout_data_t *)handler_data; |
| 766 |
ep11_private_data_t *ep11_data = data->tokdata->private_data; |
| 767 |
target_t target; |
| 768 |
session_nonce_t session_nonce; |
| 769 |
ep11_serialno_t serial_number; |
| 770 |
CK_RV rc; |
| 771 |
|
| 772 |
TRACE_INFO("Logging out adapter %02X.%04X\n", adapter, domain); |
| 773 |
|
| 774 |
rc = get_ep11_target_for_apqn(adapter, domain, &target, 0); |
| 775 |
if (rc != CKR_OK) |
| 776 |
return rc; |
| 777 |
|
| 778 |
if (ep11_data->strict_mode && data->ep11_session != NULL && |
| 779 |
data->ep11_session->pin_blob_valid) { |
| 780 |
rc = get_serial_number(data->tokdata, target, adapter, domain, |
| 781 |
serial_number); |
| 782 |
if (rc != CKR_OK) { |
| 783 |
/* ignore the error here, the adapter may not be able to perform |
| 784 |
* the query at this moment */ |
| 785 |
rc = CKR_OK; |
| 786 |
goto vhsm_mode; |
| 787 |
} |
| 788 |
|
| 789 |
#ifdef DEBUG |
| 790 |
TRACE_DEBUG("EP11 Session Pin blob\n"); |
| 791 |
TRACE_DEBUG_DUMP(" ", data->ep11_session->session_pin_blob, |
| 792 |
sizeof(data->ep11_session->session_pin_blob)); |
| 793 |
#endif |
| 794 |
|
| 795 |
rc = dll_m_Logout(data->ep11_session->session_pin_blob, |
| 796 |
sizeof(data->ep11_session->session_pin_blob), |
| 797 |
target); |
| 798 |
if (rc != CKR_OK) { |
| 799 |
rc = ep11_error_to_pkcs11_error(rc, NULL); |
| 800 |
TRACE_ERROR("%s dll_m_Logout failed: 0x%lx\n", __func__, rc); |
| 801 |
/* ignore any errors during m_logout */ |
| 802 |
} else { |
| 803 |
remove_serial_number(data->tokdata, data->ep11_session, |
| 804 |
serial_number); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
vhsm_mode: |
| 809 |
if (ep11_data->vhsm_mode && |
| 810 |
ep11_data->get_session_refcount(data->tokdata) == 0 && |
| 811 |
ep11_data->vhsm_pin_blob_valid) { |
| 812 |
#ifdef DEBUG |
| 813 |
TRACE_DEBUG("EP11 VHSM Pin blob:\n"); |
| 814 |
TRACE_DEBUG_DUMP(" ", ep11_data->vhsm_pin_blob, |
| 815 |
sizeof(ep11_data->vhsm_pin_blob)); |
| 816 |
#endif |
| 817 |
|
| 818 |
rc = dll_m_Logout(ep11_data->vhsm_pin_blob, |
| 819 |
sizeof(ep11_data->vhsm_pin_blob), |
| 820 |
target); |
| 821 |
if (rc != CKR_OK) { |
| 822 |
rc = ep11_error_to_pkcs11_error(rc, NULL); |
| 823 |
TRACE_ERROR("%s dll_m_Logout failed: 0x%lx\n", __func__, rc); |
| 824 |
/* ignore any errors during m_logout */ |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
if (ep11_data->fips_session_mode && |
| 829 |
ep11_data->get_session_refcount(data->tokdata) == 0 && |
| 830 |
ep11_data->fips_pin_blob_valid) { |
| 831 |
#ifdef DEBUG |
| 832 |
TRACE_DEBUG("EP11 FIPS Pin blob:\n"); |
| 833 |
TRACE_DEBUG_DUMP(" ", ep11_data->fips_pin_blob, |
| 834 |
sizeof(ep11_data->fips_pin_blob)); |
| 835 |
#endif |
| 836 |
|
| 837 |
session_nonce.slot_id = htobe32(data->tokdata->slot_id); |
| 838 |
memcpy(session_nonce.purpose, FIPS_NONCE_PURPOSE, 12); |
| 839 |
|
| 840 |
rc = do_LogoutExtended(XCP_LOGIN_ALG_F2021, XCP_LOGIN_IMPR_EC_P521, |
| 841 |
ep11_data->fips_pin, sizeof(ep11_data->fips_pin), |
| 842 |
(CK_BYTE *)&session_nonce, sizeof(session_nonce), |
| 843 |
target); |
| 844 |
if (rc != CKR_OK) { |
| 845 |
rc = ep11_error_to_pkcs11_error(rc, NULL); |
| 846 |
TRACE_ERROR("%s do_LogoutExtended failed: 0x%lx\n", |
| 847 |
__func__, rc); |
| 848 |
/* ignore any errors during m_logout */ |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
free_ep11_target_for_apqn(target); |
| 853 |
return CKR_OK; |
| 854 |
} |
| 855 |
|
| 856 |
CK_RV ep11tok_login_session(STDLL_TokData_t *tokdata, SESSION *session) |
| 857 |
{ |
| 858 |
ep11_private_data_t *ep11_data = tokdata->private_data; |
| 859 |
ep11_session_t *ep11_session = NULL; |
| 860 |
CK_RV rc; |
| 861 |
CK_RV rc2; |
| 862 |
ST_SESSION_HANDLE handle = { |
| 863 |
.slotID = session->session_info.slotID, |
| 864 |
.sessionh = session->handle |
| 865 |
}; |
| 866 |
CK_SESSION_HANDLE helper_session = CK_INVALID_HANDLE; |
| 867 |
login_logout_data_t data = { 0 }; |
| 868 |
CK_BBOOL cnt_incr = FALSE; |
| 869 |
|
| 870 |
TRACE_INFO("%s session=%lu\n", __func__, session->handle); |
| 871 |
|
| 872 |
if (!ep11_data->strict_mode && !ep11_data->vhsm_mode && |
| 873 |
!ep11_data->fips_session_mode) |
| 874 |
return CKR_OK; |
| 875 |
|
| 876 |
if (session->session_info.flags & CKF_EP11_HELPER_SESSION) |
| 877 |
return CKR_OK; |
| 878 |
|
| 879 |
switch (session->session_info.state) { |
| 880 |
case CKS_RW_SO_FUNCTIONS: |
| 881 |
case CKS_RO_PUBLIC_SESSION: |
| 882 |
case CKS_RW_PUBLIC_SESSION: |
| 883 |
TRACE_INFO("%s Public or SO session\n", __func__); |
| 884 |
return CKR_OK; |
| 885 |
case CKS_RO_USER_FUNCTIONS: |
| 886 |
rc = ep11_open_helper_session(tokdata, session, &helper_session); |
| 887 |
if (rc != CKR_OK) |
| 888 |
return rc; |
| 889 |
handle.sessionh = helper_session; |
| 890 |
break; |
| 891 |
default: |
| 892 |
break; |
| 893 |
} |
| 894 |
|
| 895 |
if (session->private_data != NULL) { |
| 896 |
TRACE_INFO("%s Session already logged in\n", __func__); |
| 897 |
rc = CKR_USER_ALREADY_LOGGED_IN; |
| 898 |
goto done_no_lock; |
| 899 |
} |
| 900 |
|
| 901 |
if (ep11_data->vhsm_mode || ep11_data->fips_session_mode) { |
| 902 |
if (pthread_mutex_lock(&ep11_data->session_mutex)) { |
| 903 |
TRACE_ERROR("%s Failed to lock session lock\n", __func__); |
| 904 |
rc = CKR_CANT_LOCK; |
| 905 |
goto done_no_lock; |
| 906 |
} |
| 907 |
|
| 908 |
TRACE_DEVEL("%s global session refcount: %u\n", __func__, |
| 909 |
ep11_data->get_session_refcount(tokdata)); |
| 910 |
TRACE_DEVEL("%s session refcount: %u\n", __func__, |
| 911 |
ep11_data->session_refcount); |
| 912 |
} |
| 913 |
|
| 914 |
if (ep11_data->strict_mode) { |
| 915 |
ep11_session = calloc(1, sizeof(ep11_session_t)); |
| 916 |
if (ep11_session == NULL) { |
| 917 |
TRACE_ERROR("%s Memory allocation failed\n", __func__); |
| 918 |
rc = CKR_HOST_MEMORY; |
| 919 |
goto done; |
| 920 |
} |
| 921 |
ep11_session->session = session; |
| 922 |
ep11_session->session_object = CK_INVALID_HANDLE; |
| 923 |
session->private_data = ep11_session; |
| 924 |
|
| 925 |
rc = generate_ep11_session_id(tokdata, session, ep11_session); |
| 926 |
if (rc != CKR_OK) { |
| 927 |
TRACE_ERROR("%s _generate_ep11_session_id failed: 0x%lx\n", |
| 928 |
__func__, rc); |
| 929 |
goto done; |
| 930 |
} |
| 931 |
#ifdef DEBUG |
| 932 |
TRACE_DEBUG("EP11 Session-ID for PKCS#11 session %lu:\n", |
| 933 |
session->handle); |
| 934 |
TRACE_DEBUG_DUMP(" ", ep11_session->session_id, |
| 935 |
sizeof(ep11_session->session_id)); |
| 936 |
#endif |
| 937 |
} |
| 938 |
|
| 939 |
if (ep11_data->fips_session_mode && !ep11_data->fips_pin_valid) { |
| 940 |
rc = get_pin(tokdata, session, CKH_IBM_EP11_FIPSPIN, |
| 941 |
ep11_data->fips_pin, sizeof(ep11_data->fips_pin)); |
| 942 |
if (rc != CKR_OK) { |
| 943 |
TRACE_ERROR("%s get_pin(FIPS) failed: 0x%lx\n", __func__, rc); |
| 944 |
OCK_SYSLOG(LOG_ERR, |
| 945 |
"%s: Error: A FIPS-PIN is required for FIPS_SESSION_MODE.\n", |
| 946 |
__func__); |
| 947 |
goto done; |
| 948 |
} |
| 949 |
ep11_data->fips_pin_valid = TRUE; |
| 950 |
} |
| 951 |
|
| 952 |
if (ep11_data->vhsm_mode && !ep11_data->vhsm_pin_valid) { |
| 953 |
rc = get_pin(tokdata, session, CKH_IBM_EP11_VHSMPIN, |
| 954 |
ep11_data->vhsm_pin, sizeof(ep11_data->vhsm_pin)); |
| 955 |
if (rc != CKR_OK) { |
| 956 |
TRACE_ERROR("%s get_pin(VHSM) failed: 0x%lx\n", __func__, rc); |
| 957 |
OCK_SYSLOG(LOG_ERR, |
| 958 |
"%s: Error: A VHSM-PIN is required for VHSM_MODE.\n", |
| 959 |
__func__); |
| 960 |
goto done; |
| 961 |
} |
| 962 |
ep11_data->vhsm_pin_valid = TRUE; |
| 963 |
} |
| 964 |
|
| 965 |
data.tokdata = tokdata; |
| 966 |
data.ep11_session = ep11_session; |
| 967 |
rc = handle_all_ep11_cards(&ep11_data->target_list, ep11_login_handler, |
| 968 |
&data); |
| 969 |
if (rc != CKR_OK) { |
| 970 |
TRACE_ERROR("%s handle_all_ep11_cards failed: 0x%lx\n", __func__, rc); |
| 971 |
goto done; |
| 972 |
} |
| 973 |
|
| 974 |
if (ep11_data->vhsm_mode || ep11_data->fips_session_mode) { |
| 975 |
ep11_data->session_refcount++; |
| 976 |
ep11_data->incr_session_refcount(tokdata); |
| 977 |
|
| 978 |
TRACE_DEVEL("%s global session refcount: %u\n", __func__, |
| 979 |
ep11_data->get_session_refcount(tokdata)); |
| 980 |
TRACE_DEVEL("%s session refcount: %u\n", __func__, |
| 981 |
ep11_data->session_refcount); |
| 982 |
|
| 983 |
cnt_incr = TRUE; |
| 984 |
} |
| 985 |
|
| 986 |
if (ep11_data->strict_mode) { |
| 987 |
if (ep11_session == NULL || !ep11_session->pin_blob_valid) { |
| 988 |
rc = CKR_DEVICE_ERROR; |
| 989 |
TRACE_ERROR("%s no pinblob available\n", __func__); |
| 990 |
goto done; |
| 991 |
} |
| 992 |
|
| 993 |
rc = create_ep11_object(tokdata, &handle, ep11_session->session_id, |
| 994 |
PUBLIC_SESSION_ID_LENGTH, |
| 995 |
ep11_session->session_pin_blob, |
| 996 |
sizeof(ep11_session->session_pin_blob), |
| 997 |
ep11_session->serial_numbers, |
| 998 |
ep11_session->num_serial_numbers, |
| 999 |
&ep11_session->session_object); |
| 1000 |
if (rc != CKR_OK) { |
| 1001 |
TRACE_ERROR("%s _create_ep11_object failed: 0x%lx\n", __func__, rc); |
| 1002 |
goto done; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
if (ep11_data->vhsm_mode) { |
| 1007 |
if (!ep11_data->vhsm_pin_blob_valid) { |
| 1008 |
rc = CKR_DEVICE_ERROR; |
| 1009 |
TRACE_ERROR("%s no VHSM pinblob available\n", __func__); |
| 1010 |
goto done; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
if (ep11_data->fips_session_mode) { |
| 1015 |
if (!ep11_data->fips_pin_blob_valid) { |
| 1016 |
rc = CKR_DEVICE_ERROR; |
| 1017 |
TRACE_ERROR("%s no FIPS pinblob available\n", __func__); |
| 1018 |
goto done; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
done: |
| 1023 |
if (rc != CKR_OK) { |
| 1024 |
if ((ep11_session != NULL && ep11_session->pin_blob_valid) || |
| 1025 |
ep11_data->vhsm_pin_blob_valid || |
| 1026 |
ep11_data->fips_pin_blob_valid) { |
| 1027 |
if (cnt_incr) { |
| 1028 |
if (ep11_data->session_refcount > 0) |
| 1029 |
ep11_data->session_refcount--; |
| 1030 |
ep11_data->decr_session_refcount(tokdata); |
| 1031 |
} |
| 1032 |
|
| 1033 |
data.tokdata = tokdata; |
| 1034 |
data.ep11_session = ep11_session; |
| 1035 |
rc2 = handle_all_ep11_cards(&ep11_data->target_list, |
| 1036 |
ep11_logout_handler, &data); |
| 1037 |
if (rc2 != CKR_OK) |
| 1038 |
TRACE_ERROR("%s handle_all_ep11_cards failed: 0x%lx\n", |
| 1039 |
__func__, rc2); |
| 1040 |
} |
| 1041 |
|
| 1042 |
if (ep11_data->strict_mode && ep11_session != NULL && |
| 1043 |
ep11_session->session_object != CK_INVALID_HANDLE && |
| 1044 |
ep11_session->num_serial_numbers == 0) { |
| 1045 |
rc2 = SC_DestroyObject(tokdata, &handle, |
| 1046 |
ep11_session->session_object); |
| 1047 |
if (rc2 != CKR_OK) |
| 1048 |
TRACE_ERROR("%s SC_DestroyObject failed: 0x%lx\n", __func__, |
| 1049 |
rc2); |
| 1050 |
} |
| 1051 |
|
| 1052 |
if (ep11_session != NULL) { |
| 1053 |
free_ep11_session(ep11_session); |
| 1054 |
session->private_data = NULL; |
| 1055 |
} |
| 1056 |
|
| 1057 |
TRACE_ERROR("%s: failed: 0x%lx\n", __func__, rc); |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (ep11_data->vhsm_mode || ep11_data->fips_session_mode) { |
| 1061 |
if (pthread_mutex_unlock(&ep11_data->session_mutex)) { |
| 1062 |
TRACE_ERROR("%s Failed to unlock session lock\n", __func__); |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
done_no_lock: |
| 1067 |
if (helper_session != CK_INVALID_HANDLE) { |
| 1068 |
rc2 = ep11_close_helper_session(tokdata, &handle, FALSE); |
| 1069 |
if (rc2 != CKR_OK) |
| 1070 |
TRACE_ERROR("%s ep11_close_helper_session failed: 0x%lx\n", |
| 1071 |
__func__, rc2); |
| 1072 |
} |
| 1073 |
|
| 1074 |
return rc; |
| 1075 |
} |
| 1076 |
|
| 1077 |
CK_RV ep11tok_logout_session(STDLL_TokData_t *tokdata, SESSION *session, |
| 1078 |
CK_BBOOL in_fork_initializer) |
| 1079 |
{ |
| 1080 |
ep11_private_data_t *ep11_data = tokdata->private_data; |
| 1081 |
ep11_session_t *ep11_session = (ep11_session_t *)session->private_data; |
| 1082 |
CK_RV rc = CKR_OK, rc2; |
| 1083 |
ST_SESSION_HANDLE handle = { |
| 1084 |
.slotID = session->session_info.slotID, |
| 1085 |
.sessionh = session->handle |
| 1086 |
}; |
| 1087 |
CK_SESSION_HANDLE helper_session = CK_INVALID_HANDLE; |
| 1088 |
login_logout_data_t data = { 0 }; |
| 1089 |
|
| 1090 |
TRACE_INFO("%s session=%lu\n", __func__, session->handle); |
| 1091 |
|
| 1092 |
if (!ep11_data->strict_mode && !ep11_data->vhsm_mode && |
| 1093 |
!ep11_data->fips_session_mode) |
| 1094 |
return CKR_OK; |
| 1095 |
|
| 1096 |
if (session->session_info.flags & CKF_EP11_HELPER_SESSION) |
| 1097 |
return CKR_OK; |
| 1098 |
|
| 1099 |
if (in_fork_initializer) |
| 1100 |
goto free_session; |
| 1101 |
|
| 1102 |
switch (session->session_info.state) { |
| 1103 |
case CKS_RW_SO_FUNCTIONS: |
| 1104 |
case CKS_RO_PUBLIC_SESSION: |
| 1105 |
case CKS_RW_PUBLIC_SESSION: |
| 1106 |
TRACE_INFO("%s Public or SO session\n", __func__); |
| 1107 |
return CKR_OK; |
| 1108 |
case CKS_RO_USER_FUNCTIONS: |
| 1109 |
rc = ep11_open_helper_session(tokdata, session, &helper_session); |
| 1110 |
if (rc != CKR_OK) |
| 1111 |
return rc; |
| 1112 |
handle.sessionh = helper_session; |
| 1113 |
break; |
| 1114 |
default: |
| 1115 |
break; |
| 1116 |
} |
| 1117 |
|
| 1118 |
if (ep11_data->strict_mode && ep11_session == NULL) { |
| 1119 |
TRACE_INFO("%s CKR_USER_NOT_LOGGED_IN\n", __func__); |
| 1120 |
rc = CKR_USER_NOT_LOGGED_IN; |
| 1121 |
goto done_no_lock; |
| 1122 |
} |
| 1123 |
|
| 1124 |
if (ep11_data->vhsm_mode || ep11_data->fips_session_mode) { |
| 1125 |
if (pthread_mutex_lock(&ep11_data->session_mutex)) { |
| 1126 |
TRACE_ERROR("%s Failed to lock session lock\n", __func__); |
| 1127 |
rc = CKR_CANT_LOCK; |
| 1128 |
goto done_no_lock; |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (ep11_data->session_refcount > 0) |
| 1132 |
ep11_data->session_refcount--; |
| 1133 |
ep11_data->decr_session_refcount(tokdata); |
| 1134 |
|
| 1135 |
TRACE_DEVEL("%s global session refcount: %u\n", __func__, |
| 1136 |
ep11_data->get_session_refcount(tokdata)); |
| 1137 |
TRACE_DEVEL("%s session refcount: %u\n", __func__, |
| 1138 |
ep11_data->session_refcount); |
| 1139 |
} |
| 1140 |
|
| 1141 |
data.tokdata = tokdata; |
| 1142 |
data.ep11_session = ep11_session; |
| 1143 |
rc = handle_all_ep11_cards(&ep11_data->target_list, ep11_logout_handler, |
| 1144 |
&data); |
| 1145 |
if (rc != CKR_OK) |
| 1146 |
TRACE_ERROR("%s handle_all_ep11_cards failed: 0x%lx\n", __func__, rc); |
| 1147 |
|
| 1148 |
if (ep11_data->strict_mode && ep11_session != NULL && |
| 1149 |
ep11_session->session_object != CK_INVALID_HANDLE) { |
| 1150 |
if (ep11_session->num_serial_numbers == 0) { |
| 1151 |
rc = SC_DestroyObject(tokdata, &handle, |
| 1152 |
ep11_session->session_object); |
| 1153 |
if (rc != CKR_OK) |
| 1154 |
TRACE_ERROR("%s SC_DestroyObject failed: 0x%lx\n", |
| 1155 |
__func__, rc); |
| 1156 |
} else { |
| 1157 |
rc = update_ep11_object(tokdata, &handle, |
| 1158 |
ep11_session->session_object, |
| 1159 |
ep11_session->serial_numbers, |
| 1160 |
ep11_session->num_serial_numbers); |
| 1161 |
if (rc != CKR_OK) |
| 1162 |
TRACE_ERROR("%s update_ep11_object failed: 0x%lx\n", |
| 1163 |
__func__, rc); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
free_session: |
| 1168 |
if (ep11_session != NULL) { |
| 1169 |
free_ep11_session(ep11_session); |
| 1170 |
session->private_data = NULL; |
| 1171 |
} |
| 1172 |
|
| 1173 |
if (ep11_data->vhsm_mode || ep11_data->fips_session_mode) { |
| 1174 |
if (ep11_data->session_refcount == 0) { |
| 1175 |
ep11_data->vhsm_pin_blob_valid = FALSE; |
| 1176 |
memset(ep11_data->vhsm_pin_blob, 0, |
| 1177 |
sizeof(ep11_data->vhsm_pin_blob)); |
| 1178 |
ep11_data->fips_pin_blob_valid = FALSE; |
| 1179 |
memset(ep11_data->fips_pin_blob, 0, |
| 1180 |
sizeof(ep11_data->fips_pin_blob)); |
| 1181 |
} |
| 1182 |
|
| 1183 |
if (pthread_mutex_unlock(&ep11_data->session_mutex)) { |
| 1184 |
TRACE_ERROR("%s Failed to unlock session lock\n", __func__); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
done_no_lock: |
| 1189 |
if (helper_session != CK_INVALID_HANDLE) { |
| 1190 |
rc2 = ep11_close_helper_session(tokdata, &handle, in_fork_initializer); |
| 1191 |
if (rc2 != CKR_OK) |
| 1192 |
TRACE_ERROR("%s ep11_close_helper_session failed: 0x%lx\n", |
| 1193 |
__func__, rc2); |
| 1194 |
} |
| 1195 |
|
| 1196 |
return rc; |
| 1197 |
} |