2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <pthread.h>
2N/A#include <security/cryptoki.h>
2N/A#include "softGlobal.h"
2N/A#include "softOps.h"
2N/A#include "softSession.h"
2N/A
2N/A
2N/ACK_RV
2N/AC_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A boolean_t lock_held = B_TRUE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /*
2N/A * Obtain the session pointer. Also, increment the session
2N/A * reference count.
2N/A */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if (pMechanism == NULL) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A /* Acquire the session lock */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A /* Check to see if digest operation is already active */
2N/A if (session_p->digest.flags & CRYPTO_OPERATION_ACTIVE) {
2N/A /*
2N/A * Free the memory to avoid memory leak.
2N/A * digest.context is only a flat structure.
2N/A */
2N/A soft_digest_cleanup(session_p, lock_held);
2N/A }
2N/A
2N/A /*
2N/A * This active flag will remain ON until application calls either
2N/A * C_Digest or C_DigestFinal to actually obtain the value of
2N/A * the message digest.
2N/A */
2N/A session_p->digest.flags = CRYPTO_OPERATION_ACTIVE;
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A
2N/A rv = soft_digest_init(session_p, pMechanism);
2N/A
2N/A if (rv != CKR_OK) {
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A session_p->digest.flags &= ~CRYPTO_OPERATION_ACTIVE;
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We do not hold the session lock.
2N/A */
2N/A lock_held = B_FALSE;
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
2N/A CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A boolean_t lock_held = B_TRUE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /*
2N/A * Obtain the session pointer. Also, increment the session
2N/A * reference count.
2N/A */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if (pData == NULL || pulDigestLen == NULL) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A /* Acquire the session lock */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A /* Application must call C_DigestInit before calling C_Digest */
2N/A if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A /*
2N/A * C_Digest must be called without intervening C_DigestUpdate
2N/A * calls.
2N/A */
2N/A if (session_p->digest.flags & CRYPTO_OPERATION_UPDATE) {
2N/A /*
2N/A * C_Digest can not be used to terminate a multi-part
2N/A * operation, so we'll leave the active digest operation
2N/A * flag on and let the application continue with the
2N/A * digest update operation.
2N/A *
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_FUNCTION_FAILED);
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A
2N/A rv = soft_digest(session_p, pData, ulDataLen, pDigest, pulDigestLen);
2N/A
2N/A if ((rv == CKR_BUFFER_TOO_SMALL) ||
2N/A (pDigest == NULL && rv == CKR_OK)) {
2N/A /*
2N/A * We will not terminate the active digest operation flag,
2N/A * when the application-supplied buffer is too small, or
2N/A * the application asks for the length of buffer to hold
2N/A * the message digest.
2N/A *
2N/A * Decrement the session reference count.
2N/A * We do not hold the session lock.
2N/A */
2N/A lock_held = B_FALSE;
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /*
2N/A * Terminates the active digest operation.
2N/A * Application needs to call C_DigestInit again for next
2N/A * digest operation.
2N/A */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A soft_digest_cleanup(session_p, lock_held);
2N/A
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
2N/A CK_ULONG ulPartLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A boolean_t lock_held = B_TRUE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /*
2N/A * Obtain the session pointer. Also, increment the session
2N/A * reference count.
2N/A */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if (pPart == NULL) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A /* Acquire the session lock */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A /*
2N/A * Application must call C_DigestInit before calling
2N/A * C_DigestUpdate.
2N/A */
2N/A if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A /* Set update flag to protect C_Digest */
2N/A session_p->digest.flags |= CRYPTO_OPERATION_UPDATE;
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A
2N/A rv = soft_digest_update(session_p, pPart, ulPartLen);
2N/A
2N/A if (rv == CKR_OK) {
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We do not hold the session lock.
2N/A */
2N/A lock_held = B_FALSE;
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OK);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /*
2N/A * After an error occurred, terminate the current digest
2N/A * operation by resetting the active and update flags.
2N/A */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A soft_digest_cleanup(session_p, lock_held);
2N/A
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A soft_object_t *key_p;
2N/A boolean_t lock_held = B_TRUE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /*
2N/A * Obtain the session pointer. Also, increment the session
2N/A * reference count.
2N/A */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A /* Obtain the object pointer. */
2N/A HANDLE2OBJECT(hKey, key_p, rv);
2N/A if (rv != CKR_OK)
2N/A goto clean_exit;
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A /*
2N/A * Application must call C_DigestInit before calling
2N/A * C_DigestKey.
2N/A */
2N/A if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A OBJ_REFRELE(key_p);
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A /*
2N/A * Remember the fact that a key was thrown into the mix, so that
2N/A * C_DigestFinal bzero()'s the digest context before freeing it.
2N/A */
2N/A session_p->digest.flags |= (CRYPTO_KEY_DIGESTED |
2N/A CRYPTO_OPERATION_UPDATE);
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A
2N/A rv = soft_digest_key(session_p, key_p);
2N/A
2N/A if (rv == CKR_OK) {
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We do not hold the session lock.
2N/A */
2N/A lock_held = B_FALSE;
2N/A OBJ_REFRELE(key_p);
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OK);
2N/A }
2N/A
2N/A OBJ_REFRELE(key_p);
2N/Aclean_exit:
2N/A /*
2N/A * After an error occurred, terminate the current digest
2N/A * operation by resetting the active and update flags.
2N/A */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A soft_digest_cleanup(session_p, lock_held);
2N/A
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
2N/A CK_ULONG_PTR pulDigestLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A boolean_t lock_held = B_TRUE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /*
2N/A * Obtain the session pointer. Also, increment the session
2N/A * reference count.
2N/A */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if (pulDigestLen == NULL) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A /* Acquire the session lock */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A /*
2N/A * Application must call C_DigestInit before calling
2N/A * C_DigestFinal.
2N/A */
2N/A if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A
2N/A rv = soft_digest_final(session_p, pDigest, pulDigestLen);
2N/A
2N/A if ((rv == CKR_BUFFER_TOO_SMALL) ||
2N/A (pDigest == NULL && rv == CKR_OK)) {
2N/A /*
2N/A * We will not terminate the active digest operation flag,
2N/A * when the application-supplied buffer is too small, or
2N/A * the application asks for the length of buffer to hold
2N/A * the message digest.
2N/A *
2N/A * Decrement the session reference count.
2N/A * We do not hold the session lock.
2N/A */
2N/A lock_held = B_FALSE;
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /* Terminates the active digest operation */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A
2N/A soft_digest_cleanup(session_p, lock_held);
2N/A
2N/A /*
2N/A * Decrement the session reference count.
2N/A * We hold the session lock, and SES_REFRELE()
2N/A * will release the session lock for us.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A
2N/A return (rv);
2N/A}