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 2009 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 <stdlib.h>
2N/A#include <errno.h>
2N/A#include <sys/crypto/ioctl.h>
2N/A#include <security/cryptoki.h>
2N/A#include "kernelGlobal.h"
2N/A#include "kernelSession.h"
2N/A#include "kernelObject.h"
2N/A
2N/A
2N/ACK_RV
2N/AC_EncryptInit(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 kernel_session_t *session_p;
2N/A kernel_object_t *key_p;
2N/A boolean_t ses_lock_held = B_FALSE;
2N/A crypto_encrypt_init_t encrypt_init;
2N/A crypto_mech_type_t k_mech_type;
2N/A int r;
2N/A
2N/A if (!kernel_initialized)
2N/A return (CKR_CRYPTOKI_NOT_INITIALIZED);
2N/A
2N/A if (pMechanism == NULL) {
2N/A return (CKR_ARGUMENTS_BAD);
2N/A }
2N/A
2N/A /* Get the kernel's internal mechanism number. */
2N/A rv = kernel_mech(pMechanism->mechanism, &k_mech_type);
2N/A if (rv != CKR_OK)
2N/A return (rv);
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 /* Obtain the object pointer. */
2N/A HANDLE2OBJECT(hKey, key_p, rv);
2N/A if (rv != CKR_OK) {
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/A /* Check to see if key object allows for encryption. */
2N/A if (key_p->is_lib_obj && !(key_p->bool_attr_mask & ENCRYPT_BOOL_ON)) {
2N/A rv = CKR_KEY_TYPE_INCONSISTENT;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A ses_lock_held = B_TRUE;
2N/A
2N/A /*
2N/A * This active flag will remain ON until application calls either
2N/A * C_Encrypt or C_EncryptFinal to actually obtain the final piece
2N/A * of ciphertext.
2N/A */
2N/A session_p->encrypt.flags = CRYPTO_OPERATION_ACTIVE;
2N/A
2N/A /* set up key data */
2N/A if (!key_p->is_lib_obj) {
2N/A encrypt_init.ei_key.ck_format = CRYPTO_KEY_REFERENCE;
2N/A encrypt_init.ei_key.ck_obj_id = key_p->k_handle;
2N/A } else {
2N/A if (key_p->class == CKO_SECRET_KEY) {
2N/A encrypt_init.ei_key.ck_format = CRYPTO_KEY_RAW;
2N/A encrypt_init.ei_key.ck_data =
2N/A get_symmetric_key_value(key_p);
2N/A if (encrypt_init.ei_key.ck_data == NULL) {
2N/A rv = CKR_HOST_MEMORY;
2N/A goto clean_exit;
2N/A }
2N/A encrypt_init.ei_key.ck_length =
2N/A OBJ_SEC(key_p)->sk_value_len << 3;
2N/A
2N/A } else if (key_p->key_type == CKK_RSA) {
2N/A if (get_rsa_public_key(key_p, &encrypt_init.ei_key) !=
2N/A CKR_OK) {
2N/A rv = CKR_HOST_MEMORY;
2N/A goto clean_exit;
2N/A }
2N/A } else {
2N/A rv = CKR_KEY_TYPE_INCONSISTENT;
2N/A goto clean_exit;
2N/A }
2N/A }
2N/A
2N/A encrypt_init.ei_session = session_p->k_session;
2N/A session_p->encrypt.mech = *pMechanism;
2N/A
2N/A /* Cache this capability value for efficiency */
2N/A if (INPLACE_MECHANISM(session_p->encrypt.mech.mechanism)) {
2N/A session_p->encrypt.flags |= CRYPTO_OPERATION_INPLACE_OK;
2N/A }
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A
2N/A ses_lock_held = B_FALSE;
2N/A encrypt_init.ei_mech.cm_type = k_mech_type;
2N/A encrypt_init.ei_mech.cm_param = pMechanism->pParameter;
2N/A encrypt_init.ei_mech.cm_param_len = pMechanism->ulParameterLen;
2N/A
2N/A while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_INIT, &encrypt_init)) < 0) {
2N/A if (errno != EINTR)
2N/A break;
2N/A }
2N/A if (r < 0) {
2N/A rv = CKR_FUNCTION_FAILED;
2N/A } else {
2N/A if (encrypt_init.ei_return_value != CRYPTO_SUCCESS) {
2N/A rv = crypto2pkcs11_error_number(
2N/A encrypt_init.ei_return_value);
2N/A }
2N/A }
2N/A
2N/A /* Free memory allocated for decrypt_init.di_key */
2N/A if (key_p->is_lib_obj) {
2N/A if (key_p->class == CKO_SECRET_KEY) {
2N/A free(encrypt_init.ei_key.ck_data);
2N/A } else if (key_p->key_type == CKK_RSA) {
2N/A free_key_attributes(&encrypt_init.ei_key);
2N/A }
2N/A }
2N/A
2N/A if (rv != CKR_OK) {
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A session_p->encrypt.flags &= ~CRYPTO_OPERATION_ACTIVE;
2N/A ses_lock_held = B_TRUE;
2N/A }
2N/A
2N/Aclean_exit:
2N/A OBJ_REFRELE(key_p);
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_Encrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
2N/A CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A kernel_session_t *session_p;
2N/A boolean_t ses_lock_held = B_FALSE;
2N/A boolean_t inplace;
2N/A crypto_encrypt_t encrypt;
2N/A int r;
2N/A
2N/A if (!kernel_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) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A /*
2N/A * Only check if pulEncryptedDataLen is NULL.
2N/A * No need to check if pEncryptedData is NULL because
2N/A * application might just ask for the length of buffer to hold
2N/A * the ciphertext.
2N/A */
2N/A if (pulEncryptedDataLen == 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 ses_lock_held = B_TRUE;
2N/A
2N/A /* Application must call C_EncryptInit before calling C_Encrypt. */
2N/A if (!(session_p->encrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A /*
2N/A * C_Encrypt must be called without intervening C_EncryptUpdate
2N/A * calls.
2N/A */
2N/A if (session_p->encrypt.flags & CRYPTO_OPERATION_UPDATE) {
2N/A /*
2N/A * C_Encrypt can not be used to terminate a multi-part
2N/A * operation, so we'll leave the active encrypt operation
2N/A * flag on and let the application continue with the
2N/A * encrypt update operation.
2N/A */
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (CKR_FUNCTION_FAILED);
2N/A }
2N/A
2N/A encrypt.ce_session = session_p->k_session;
2N/A
2N/A /*
2N/A * Certain mechanisms, where the length of the ciphertext is
2N/A * same as the transformed plaintext, can be optimized
2N/A * by the kernel into an in-place operation. Unfortunately,
2N/A * some applications use a ciphertext buffer that is larger
2N/A * than it needs to be. We fix that here.
2N/A */
2N/A inplace = (session_p->encrypt.flags & CRYPTO_OPERATION_INPLACE_OK) != 0;
2N/A if (ulDataLen < *pulEncryptedDataLen && inplace) {
2N/A encrypt.ce_encrlen = ulDataLen;
2N/A } else {
2N/A encrypt.ce_encrlen = *pulEncryptedDataLen;
2N/A }
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A ses_lock_held = B_FALSE;
2N/A
2N/A encrypt.ce_datalen = ulDataLen;
2N/A encrypt.ce_databuf = (char *)pData;
2N/A encrypt.ce_encrbuf = (char *)pEncryptedData;
2N/A encrypt.ce_flags =
2N/A ((inplace && (pEncryptedData != NULL)) ||
2N/A (pData == pEncryptedData)) &&
2N/A (encrypt.ce_encrlen == encrypt.ce_datalen) ?
2N/A CRYPTO_INPLACE_OPERATION : 0;
2N/A
2N/A while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT, &encrypt)) < 0) {
2N/A if (errno != EINTR)
2N/A break;
2N/A }
2N/A if (r < 0) {
2N/A rv = CKR_FUNCTION_FAILED;
2N/A } else {
2N/A rv = crypto2pkcs11_error_number(encrypt.ce_return_value);
2N/A }
2N/A
2N/A if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
2N/A *pulEncryptedDataLen = encrypt.ce_encrlen;
2N/A
2N/A if ((rv == CKR_BUFFER_TOO_SMALL) ||
2N/A (rv == CKR_OK && pEncryptedData == NULL)) {
2N/A /*
2N/A * We will not terminate the active encrypt 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 ciphertext.
2N/A */
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /*
2N/A * Terminates the active encrypt operation.
2N/A * Application needs to call C_EncryptInit again for next
2N/A * encrypt operation.
2N/A */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A session_p->encrypt.flags = 0;
2N/A ses_lock_held = B_TRUE;
2N/A REFRELE(session_p, ses_lock_held);
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_EncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
2N/A CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
2N/A CK_ULONG_PTR pulEncryptedPartLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A kernel_session_t *session_p;
2N/A boolean_t ses_lock_held = B_FALSE;
2N/A boolean_t inplace;
2N/A crypto_encrypt_update_t encrypt_update;
2N/A int r;
2N/A
2N/A if (!kernel_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 (pPart == NULL) {
2N/A rv = CKR_ARGUMENTS_BAD;
2N/A goto clean_exit;
2N/A }
2N/A
2N/A /*
2N/A * Only check if pulEncryptedPartLen is NULL.
2N/A * No need to check if pEncryptedPart is NULL because
2N/A * application might just ask for the length of buffer to hold
2N/A * the ciphertext.
2N/A */
2N/A if (pulEncryptedPartLen == 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 ses_lock_held = B_TRUE;
2N/A
2N/A /*
2N/A * Application must call C_EncryptInit before calling
2N/A * C_EncryptUpdate.
2N/A */
2N/A if (!(session_p->encrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A session_p->encrypt.flags |= CRYPTO_OPERATION_UPDATE;
2N/A
2N/A encrypt_update.eu_session = session_p->k_session;
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A ses_lock_held = B_FALSE;
2N/A
2N/A encrypt_update.eu_datalen = ulPartLen;
2N/A encrypt_update.eu_databuf = (char *)pPart;
2N/A encrypt_update.eu_encrlen = *pulEncryptedPartLen;
2N/A encrypt_update.eu_encrbuf = (char *)pEncryptedPart;
2N/A
2N/A inplace = (session_p->encrypt.flags & CRYPTO_OPERATION_INPLACE_OK) != 0;
2N/A encrypt_update.eu_flags =
2N/A ((inplace && (pEncryptedPart != NULL)) ||
2N/A (pPart == pEncryptedPart)) &&
2N/A (encrypt_update.eu_encrlen == encrypt_update.eu_datalen) ?
2N/A CRYPTO_INPLACE_OPERATION : 0;
2N/A
2N/A while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_UPDATE,
2N/A &encrypt_update)) < 0) {
2N/A if (errno != EINTR)
2N/A break;
2N/A }
2N/A if (r < 0) {
2N/A rv = CKR_FUNCTION_FAILED;
2N/A } else {
2N/A rv = crypto2pkcs11_error_number(
2N/A encrypt_update.eu_return_value);
2N/A }
2N/A
2N/A /*
2N/A * If CKR_OK or CKR_BUFFER_TOO_SMALL, set the output length.
2N/A * We don't terminate the current encryption operation.
2N/A */
2N/A if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL) {
2N/A *pulEncryptedPartLen = encrypt_update.eu_encrlen;
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /*
2N/A * After an error occurred, terminate the current encrypt
2N/A * operation by resetting the active and update flags.
2N/A */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A session_p->encrypt.flags = 0;
2N/A ses_lock_held = B_TRUE;
2N/A REFRELE(session_p, ses_lock_held);
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/A
2N/ACK_RV
2N/AC_EncryptFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastEncryptedPart,
2N/A CK_ULONG_PTR pulLastEncryptedPartLen)
2N/A{
2N/A
2N/A CK_RV rv;
2N/A kernel_session_t *session_p;
2N/A boolean_t ses_lock_held = B_FALSE;
2N/A crypto_encrypt_final_t encrypt_final;
2N/A int r;
2N/A
2N/A if (!kernel_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 (pulLastEncryptedPartLen == 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 ses_lock_held = B_TRUE;
2N/A
2N/A /*
2N/A * Application must call C_EncryptInit before calling
2N/A * C_EncryptFinal.
2N/A */
2N/A if (!(session_p->encrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (CKR_OPERATION_NOT_INITIALIZED);
2N/A }
2N/A
2N/A encrypt_final.ef_session = session_p->k_session;
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A ses_lock_held = B_FALSE;
2N/A
2N/A encrypt_final.ef_encrlen = *pulLastEncryptedPartLen;
2N/A encrypt_final.ef_encrbuf = (char *)pLastEncryptedPart;
2N/A
2N/A while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_FINAL,
2N/A &encrypt_final)) < 0) {
2N/A if (errno != EINTR)
2N/A break;
2N/A }
2N/A if (r < 0) {
2N/A rv = CKR_FUNCTION_FAILED;
2N/A } else {
2N/A rv = crypto2pkcs11_error_number(encrypt_final.ef_return_value);
2N/A }
2N/A
2N/A if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
2N/A *pulLastEncryptedPartLen = encrypt_final.ef_encrlen;
2N/A
2N/A if ((rv == CKR_BUFFER_TOO_SMALL) ||
2N/A (rv == CKR_OK && pLastEncryptedPart == NULL)) {
2N/A /*
2N/A * We will not terminate the active encrypt 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 ciphertext.
2N/A */
2N/A REFRELE(session_p, ses_lock_held);
2N/A return (rv);
2N/A }
2N/A
2N/Aclean_exit:
2N/A /* Terminates the active encrypt operation. */
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A session_p->encrypt.flags = 0;
2N/A ses_lock_held = B_TRUE;
2N/A REFRELE(session_p, ses_lock_held);
2N/A
2N/A return (rv);
2N/A}