#include <pthread.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/crypto/ioctl.h>
#include <security/cryptoki.h>
#include "kernelGlobal.h"
#include "kernelSession.h"
#include "kernelSlot.h"
#include "kernelEmulate.h"
static pthread_mutex_t delete_sessions_mutex = PTHREAD_MUTEX_INITIALIZER;
void
kernel_delete_all_sessions(CK_SLOT_ID slotID, boolean_t wrapper_only)
{
kernel_session_t *session_p;
kernel_slot_t *pslot;
(void) pthread_mutex_lock(&delete_sessions_mutex);
pslot = slot_table[slotID];
for (;;) {
(void) pthread_mutex_lock(&pslot->sl_mutex);
if (pslot->sl_sess_list == NULL)
break;
session_p = pslot->sl_sess_list;
(void) pthread_mutex_lock(&session_p->session_mutex);
if (session_p->ses_close_sync & SESSION_IS_CLOSING) {
(void) pthread_mutex_unlock(&session_p->session_mutex);
continue;
}
session_p->ses_close_sync |= SESSION_IS_CLOSING;
(void) pthread_mutex_unlock(&session_p->session_mutex);
(void) pthread_mutex_unlock(&pslot->sl_mutex);
kernel_delete_session(slotID, session_p, B_FALSE, wrapper_only);
}
(void) pthread_mutex_unlock(&pslot->sl_mutex);
(void) pthread_mutex_unlock(&delete_sessions_mutex);
}
CK_RV
kernel_add_session(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
CK_NOTIFY notify, CK_ULONG *sessionhandle_p)
{
CK_RV rv = CKR_OK;
kernel_session_t *new_sp = NULL;
crypto_open_session_t open_session;
kernel_slot_t *pslot;
int r;
new_sp = calloc(1, sizeof (kernel_session_t));
if (new_sp == NULL) {
return (CKR_HOST_MEMORY);
}
new_sp->magic_marker = KERNELTOKEN_SESSION_MAGIC;
new_sp->pApplication = pApplication;
new_sp->Notify = notify;
new_sp->flags = flags;
new_sp->ses_RO = (flags & CKF_RW_SESSION) ? B_FALSE : B_TRUE;
new_sp->ses_slotid = slotID;
new_sp->object_list = NULL;
new_sp->ses_refcnt = 0;
new_sp->ses_close_sync = 0;
if (pthread_mutex_init(&new_sp->session_mutex, NULL) != 0) {
free(new_sp);
return (CKR_CANT_LOCK);
}
pslot = slot_table[slotID];
open_session.os_provider_id = pslot->sl_provider_id;
open_session.os_flags = flags;
while ((r = ioctl(kernel_fd, CRYPTO_OPEN_SESSION, &open_session)) < 0) {
if (errno != EINTR)
break;
}
if (r < 0) {
rv = CKR_FUNCTION_FAILED;
} else {
rv = crypto2pkcs11_error_number(open_session.os_return_value);
}
if (rv != CKR_OK) {
(void) pthread_mutex_destroy(&new_sp->session_mutex);
free(new_sp);
return (rv);
}
new_sp->k_session = open_session.os_session;
(void) pthread_mutex_init(&new_sp->ses_free_mutex, NULL);
(void) pthread_cond_init(&new_sp->ses_free_cond, NULL);
if (pslot->sl_sess_list == NULL) {
pslot->sl_sess_list = new_sp;
new_sp->prev = NULL;
new_sp->next = NULL;
} else {
pslot->sl_sess_list->prev = new_sp;
new_sp->next = pslot->sl_sess_list;
new_sp->prev = NULL;
pslot->sl_sess_list = new_sp;
}
*sessionhandle_p = (CK_ULONG)new_sp;
return (CKR_OK);
}
void
kernel_delete_session(CK_SLOT_ID slotID, kernel_session_t *session_p,
boolean_t slot_lock_held, boolean_t wrapper_only)
{
crypto_session_id_t k_session;
crypto_close_session_t close_session;
kernel_slot_t *pslot;
kernel_object_t *objp;
kernel_object_t *objp1;
pslot = slot_table[slotID];
if (!slot_lock_held) {
(void) pthread_mutex_lock(&pslot->sl_mutex);
}
if (pslot->sl_sess_list == session_p) {
if (session_p->next) {
pslot->sl_sess_list = session_p->next;
session_p->next->prev = NULL;
} else {
pslot->sl_sess_list = NULL;
}
} else {
if (session_p->next) {
session_p->prev->next = session_p->next;
session_p->next->prev = session_p->prev;
} else {
session_p->prev->next = NULL;
}
}
if (!slot_lock_held) {
(void) pthread_mutex_unlock(&pslot->sl_mutex);
}
(void) pthread_mutex_lock(&session_p->session_mutex);
if (session_p->magic_marker != KERNELTOKEN_SESSION_MAGIC) {
(void) pthread_mutex_unlock(&session_p->session_mutex);
return;
}
(void) pthread_mutex_lock(&session_p->ses_free_mutex);
if (wrapper_only) {
session_p->ses_refcnt = 0;
}
while (session_p->ses_refcnt != 0) {
session_p->ses_close_sync |= SESSION_REFCNT_WAITING;
(void) pthread_mutex_unlock(&session_p->session_mutex);
(void) pthread_cond_wait(&session_p->ses_free_cond,
&session_p->ses_free_mutex);
(void) pthread_mutex_lock(&session_p->session_mutex);
}
session_p->ses_close_sync &= ~SESSION_REFCNT_WAITING;
session_p->magic_marker = 0;
(void) pthread_mutex_unlock(&session_p->ses_free_mutex);
(void) pthread_mutex_destroy(&session_p->ses_free_mutex);
(void) pthread_cond_destroy(&session_p->ses_free_cond);
kernel_delete_all_objects_in_session(session_p, wrapper_only);
if (session_p->digest.context != NULL) {
digest_buf_t *bufp = session_p->digest.context;
if (bufp->buf != NULL) {
free_soft_ctx(get_sp(&session_p->digest), OP_DIGEST);
bzero(bufp->buf, bufp->indata_len);
free(bufp->buf);
}
free(bufp);
}
if (session_p->encrypt.context != NULL)
free(session_p->encrypt.context);
if (session_p->decrypt.context != NULL)
free(session_p->decrypt.context);
if (session_p->sign.context != NULL) {
digest_buf_t *bufp = session_p->sign.context;
if (bufp->buf != NULL) {
free_soft_ctx(get_sp(&session_p->sign), OP_SIGN);
bzero(bufp->buf, bufp->indata_len);
free(bufp->buf);
}
free(bufp);
}
if (session_p->verify.context != NULL) {
digest_buf_t *bufp = session_p->verify.context;
if (bufp->buf != NULL) {
free_soft_ctx(get_sp(&session_p->verify), OP_VERIFY);
bzero(bufp->buf, bufp->indata_len);
free(bufp->buf);
}
free(bufp);
}
k_session = session_p->k_session;
session_p->ses_close_sync &= ~SESSION_IS_CLOSING;
(void) pthread_mutex_unlock(&session_p->session_mutex);
(void) pthread_mutex_destroy(&session_p->session_mutex);
if (!wrapper_only) {
close_session.cs_session = k_session;
while (ioctl(kernel_fd, CRYPTO_CLOSE_SESSION,
&close_session) < 0) {
if (errno != EINTR)
break;
}
}
kernel_session_delay_free(session_p);
if (!slot_lock_held) {
(void) pthread_mutex_lock(&pslot->sl_mutex);
}
if (pslot->sl_sess_list == NULL) {
pslot->sl_state = CKU_PUBLIC;
objp = pslot->sl_tobj_list;
while (objp) {
objp1 = objp->next;
(void) pthread_mutex_destroy(&objp->object_mutex);
(void) kernel_object_delay_free(objp);
objp = objp1;
}
pslot->sl_tobj_list = NULL;
}
if (!slot_lock_held) {
(void) pthread_mutex_unlock(&pslot->sl_mutex);
}
}
CK_RV
handle2session(CK_SESSION_HANDLE hSession, kernel_session_t **session_p)
{
kernel_session_t *sp = (kernel_session_t *)(hSession);
CK_RV rv;
if ((sp == NULL) ||
(sp->magic_marker != KERNELTOKEN_SESSION_MAGIC)) {
return (CKR_SESSION_HANDLE_INVALID);
} else {
(void) pthread_mutex_lock(&sp->session_mutex);
if (sp->ses_close_sync & SESSION_IS_CLOSING) {
rv = CKR_SESSION_CLOSED;
} else {
sp->ses_refcnt++;
rv = CKR_OK;
}
(void) pthread_mutex_unlock(&sp->session_mutex);
}
if (rv == CKR_OK)
*session_p = sp;
return (rv);
}
void
kernel_session_delay_free(kernel_session_t *sp)
{
kernel_session_t *tmp;
(void) pthread_mutex_lock(&ses_delay_freed.ses_to_be_free_mutex);
sp->next = NULL;
if (ses_delay_freed.first == NULL) {
ses_delay_freed.last = sp;
ses_delay_freed.first = sp;
} else {
ses_delay_freed.last->next = sp;
ses_delay_freed.last = sp;
}
if (++ses_delay_freed.count >= MAX_SES_TO_BE_FREED) {
ses_delay_freed.count--;
tmp = ses_delay_freed.first->next;
free(ses_delay_freed.first);
ses_delay_freed.first = tmp;
}
(void) pthread_mutex_unlock(&ses_delay_freed.ses_to_be_free_mutex);
}
void
kernel_acquire_all_slots_mutexes()
{
int slotID;
kernel_slot_t *pslot;
kernel_session_t *session_p;
(void) pthread_mutex_lock(&delete_sessions_mutex);
for (slotID = 0; slotID < slot_count; slotID++) {
pslot = slot_table[slotID];
(void) pthread_mutex_lock(&pslot->sl_mutex);
session_p = pslot->sl_sess_list;
while (session_p) {
struct object *objp;
(void) pthread_mutex_lock(&session_p->session_mutex);
(void) pthread_mutex_lock(&session_p->ses_free_mutex);
objp = session_p->object_list;
while (objp) {
(void) pthread_mutex_lock(&objp->object_mutex);
objp = objp->next;
}
session_p = session_p->next;
}
}
}
void
kernel_release_all_slots_mutexes()
{
int slotID;
kernel_slot_t *pslot;
kernel_session_t *session_p;
for (slotID = 0; slotID < slot_count; slotID++) {
pslot = slot_table[slotID];
session_p = pslot->sl_sess_list;
while (session_p) {
struct object *objp;
objp = session_p->object_list;
while (objp) {
(void) pthread_mutex_unlock(
&objp->object_mutex);
objp = objp->next;
}
(void) pthread_mutex_unlock(&session_p->ses_free_mutex);
(void) pthread_mutex_unlock(&session_p->session_mutex);
session_p = session_p->next;
}
(void) pthread_mutex_unlock(&pslot->sl_mutex);
}
(void) pthread_mutex_unlock(&delete_sessions_mutex);
}