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 (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <pthread.h>
2N/A#include <security/cryptoki.h>
2N/A#include "softGlobal.h"
2N/A#include "softObject.h"
2N/A#include "softOps.h"
2N/A#include "softSession.h"
2N/A
2N/A
2N/ACK_RV
2N/AC_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
2N/A 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_FALSE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /* Obtain the session pointer. */
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 /* 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
2N/A /* Check to see if key object supports signature. */
2N/A if (!(key_p->bool_attr_mask & SIGN_BOOL_ON)) {
2N/A rv = CKR_KEY_FUNCTION_NOT_PERMITTED;
2N/A goto clean_exit1;
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A lock_held = B_TRUE;
2N/A
2N/A /* Check to see if sign operation is already active. */
2N/A if (session_p->sign.flags & CRYPTO_OPERATION_ACTIVE) {
2N/A /* free the memory to avoid memory leak */
2N/A soft_sign_verify_cleanup(session_p, B_TRUE, B_TRUE);
2N/A }
2N/A
2N/A /*
2N/A * This active flag will remain ON until application calls either
2N/A * C_Sign or C_SignFinal to actually obtain the signature.
2N/A */
2N/A session_p->sign.flags = CRYPTO_OPERATION_ACTIVE;
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A lock_held = B_FALSE;
2N/A
2N/A rv = soft_sign_init(session_p, pMechanism, key_p);
2N/A
2N/A if (rv != CKR_OK) {
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A session_p->sign.flags &= ~CRYPTO_OPERATION_ACTIVE;
2N/A lock_held = B_TRUE;
2N/A }
2N/A
2N/Aclean_exit1:
2N/A OBJ_REFRELE(key_p);
2N/Aclean_exit:
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_Sign(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
2N/A CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A boolean_t lock_held = B_FALSE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /* Obtain the session pointer */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if ((pData == NULL) || (pulSignatureLen == NULL)) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A lock_held = B_TRUE;
2N/A
2N/A /* Application must call C_SignInit before calling C_Sign. */
2N/A if (!(session_p->sign.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A /*
2N/A * C_Sign must be called without intervening C_SignUpdate
2N/A * calls.
2N/A */
2N/A if (session_p->sign.flags & CRYPTO_OPERATION_UPDATE) {
2N/A /*
2N/A * C_Sign can not be used to terminate a multi-part
2N/A * operation, so we'll leave the active sign operation
2N/A * flag on and let the application continue with the
2N/A * sign update operation.
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 lock_held = B_FALSE;
2N/A
2N/A rv = soft_sign(session_p, pData, ulDataLen, pSignature,
2N/A pulSignatureLen);
2N/A
2N/A if ((rv == CKR_BUFFER_TOO_SMALL) ||
2N/A (pSignature == NULL && rv == CKR_OK)) {
2N/A /*
2N/A * We will not terminate the active sign 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 signature.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /* Clear contexts, free key, and release session counter */
2N/A soft_sign_verify_cleanup(session_p, B_TRUE, B_FALSE);
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_SignUpdate(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_FALSE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /* Obtain the session pointer */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if (ulPartLen == 0) {
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OK);
2N/A }
2N/A
2N/A if (pPart == NULL) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A lock_held = B_TRUE;
2N/A
2N/A /*
2N/A * Application must call C_SignInit before calling
2N/A * C_SignUpdate.
2N/A */
2N/A if (!(session_p->sign.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A session_p->sign.flags |= CRYPTO_OPERATION_UPDATE;
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A lock_held = B_FALSE;
2N/A
2N/A rv = soft_sign_update(session_p, pPart, ulPartLen);
2N/A
2N/A if (rv == CKR_OK) {
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /* After error, clear context, free key, & release session counter */
2N/A soft_sign_verify_cleanup(session_p, B_TRUE, B_FALSE);
2N/A return (rv);
2N/A
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
2N/A CK_ULONG_PTR pulSignatureLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A boolean_t lock_held = B_FALSE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /* Obtain the session pointer */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if (pulSignatureLen == NULL) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A lock_held = B_TRUE;
2N/A
2N/A /*
2N/A * Application must call C_SignInit before calling
2N/A * C_SignFinal.
2N/A */
2N/A if (!(session_p->sign.flags & CRYPTO_OPERATION_ACTIVE)) {
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 lock_held = B_FALSE;
2N/A
2N/A rv = soft_sign_final(session_p, pSignature, pulSignatureLen);
2N/A
2N/A if ((rv == CKR_BUFFER_TOO_SMALL) ||
2N/A (pSignature == NULL && rv == CKR_OK)) {
2N/A /*
2N/A * We will not terminate the active sign 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 signature.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /* Clear contexts, free key, and release session counter */
2N/A soft_sign_verify_cleanup(session_p, B_TRUE, B_FALSE);
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_SignRecoverInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
2N/A 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_FALSE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /* Obtain the session pointer. */
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 /* 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
2N/A /* Check to see if key object supports sign_recover. */
2N/A if (!(key_p->bool_attr_mask & SIGN_RECOVER_BOOL_ON)) {
2N/A rv = CKR_KEY_FUNCTION_NOT_PERMITTED;
2N/A goto clean_exit1;
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A lock_held = B_TRUE;
2N/A
2N/A /* Check to see if sign operation is already active. */
2N/A if (session_p->sign.flags & CRYPTO_OPERATION_ACTIVE) {
2N/A /* free the memory to avoid memory leak */
2N/A soft_sign_verify_cleanup(session_p, B_TRUE, B_TRUE);
2N/A }
2N/A
2N/A /*
2N/A * This active flag will remain ON until application calls either
2N/A * C_SignRecover to actually obtain the signature.
2N/A */
2N/A session_p->sign.flags = CRYPTO_OPERATION_ACTIVE;
2N/A
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A lock_held = B_FALSE;
2N/A
2N/A rv = soft_sign_recover_init(session_p, pMechanism, key_p);
2N/A
2N/A if (rv != CKR_OK) {
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A session_p->sign.flags &= ~CRYPTO_OPERATION_ACTIVE;
2N/A lock_held = B_TRUE;
2N/A }
2N/A
2N/Aclean_exit1:
2N/A OBJ_REFRELE(key_p);
2N/Aclean_exit:
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
2N/A CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A soft_session_t *session_p;
2N/A boolean_t lock_held = B_FALSE;
2N/A
2N/A if (!softtoken_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A /* Obtain the session pointer */
2N/A rv = handle2session(hSession, &session_p);
2N/A if (rv != CKR_OK)
2N/A return (rv);
2N/A
2N/A if ((pData == NULL) || (pulSignatureLen == NULL)) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A lock_held = B_TRUE;
2N/A
2N/A /* Application must call C_SignRecoverInit before C_SignRecover. */
2N/A if (!(session_p->sign.flags & CRYPTO_OPERATION_ACTIVE)) {
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 lock_held = B_FALSE;
2N/A
2N/A rv = soft_sign_recover(session_p, pData, ulDataLen, pSignature,
2N/A pulSignatureLen);
2N/A
2N/A if ((rv == CKR_BUFFER_TOO_SMALL) ||
2N/A (pSignature == NULL && rv == CKR_OK)) {
2N/A /*
2N/A * We will not terminate the active sign 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 signature.
2N/A */
2N/A SES_REFRELE(session_p, lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /* Clear contexts, free key, and release session counter */
2N/A soft_sign_verify_cleanup(session_p, B_TRUE, B_FALSE);
2N/A return (rv);
2N/A}
2N/A