xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softDigest.c (revision 0e986b9d87352cd82909c748e7f684afe0ed579f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5588a1af0SAlexandr Nedvedicky  * Common Development and Distribution License (the "License").
6588a1af0SAlexandr Nedvedicky  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22588a1af0SAlexandr Nedvedicky  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*0e986b9dSJason King  * Copyright 2018, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <pthread.h>
287c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
297c478bd9Sstevel@tonic-gate #include "softGlobal.h"
307c478bd9Sstevel@tonic-gate #include "softOps.h"
317c478bd9Sstevel@tonic-gate #include "softSession.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate CK_RV
C_DigestInit(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism)357c478bd9Sstevel@tonic-gate C_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	CK_RV		rv;
397c478bd9Sstevel@tonic-gate 	soft_session_t	*session_p;
407c478bd9Sstevel@tonic-gate 	boolean_t lock_held = B_TRUE;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
437c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	/*
467c478bd9Sstevel@tonic-gate 	 * Obtain the session pointer. Also, increment the session
477c478bd9Sstevel@tonic-gate 	 * reference count.
487c478bd9Sstevel@tonic-gate 	 */
497c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
507c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
517c478bd9Sstevel@tonic-gate 		return (rv);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (pMechanism == NULL) {
547c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
557c478bd9Sstevel@tonic-gate 		goto clean_exit;
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	/* Acquire the session lock */
597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/* Check to see if digest operation is already active */
627c478bd9Sstevel@tonic-gate 	if (session_p->digest.flags & CRYPTO_OPERATION_ACTIVE) {
637c478bd9Sstevel@tonic-gate 		/*
647c478bd9Sstevel@tonic-gate 		 * Free the memory to avoid memory leak.
657c478bd9Sstevel@tonic-gate 		 * digest.context is only a flat structure.
667c478bd9Sstevel@tonic-gate 		 */
67588a1af0SAlexandr Nedvedicky 		soft_digest_cleanup(session_p, lock_held);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	/*
717c478bd9Sstevel@tonic-gate 	 * This active flag will remain ON until application calls either
727c478bd9Sstevel@tonic-gate 	 * C_Digest or C_DigestFinal to actually obtain the value of
737c478bd9Sstevel@tonic-gate 	 * the message digest.
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 	session_p->digest.flags = CRYPTO_OPERATION_ACTIVE;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	rv = soft_digest_init(session_p, pMechanism);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
827c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
837c478bd9Sstevel@tonic-gate 		session_p->digest.flags &= ~CRYPTO_OPERATION_ACTIVE;
847c478bd9Sstevel@tonic-gate 		/*
857c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
867c478bd9Sstevel@tonic-gate 		 * We hold the session lock, and SES_REFRELE()
877c478bd9Sstevel@tonic-gate 		 * will release the session lock for us.
887c478bd9Sstevel@tonic-gate 		 */
897c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
907c478bd9Sstevel@tonic-gate 		return (rv);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate clean_exit:
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * Decrement the session reference count.
967c478bd9Sstevel@tonic-gate 	 * We do not hold the session lock.
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	lock_held = B_FALSE;
997c478bd9Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
1007c478bd9Sstevel@tonic-gate 	return (rv);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate CK_RV
C_Digest(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pData,CK_ULONG ulDataLen,CK_BYTE_PTR pDigest,CK_ULONG_PTR pulDigestLen)1057c478bd9Sstevel@tonic-gate C_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
1067c478bd9Sstevel@tonic-gate     CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	CK_RV		rv;
1107c478bd9Sstevel@tonic-gate 	soft_session_t	*session_p;
1117c478bd9Sstevel@tonic-gate 	boolean_t lock_held = B_TRUE;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
1147c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * Obtain the session pointer. Also, increment the session
1187c478bd9Sstevel@tonic-gate 	 * reference count.
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
1217c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
1227c478bd9Sstevel@tonic-gate 		return (rv);
1237c478bd9Sstevel@tonic-gate 
124*0e986b9dSJason King 	if ((pData == NULL && ulDataLen != 0) || pulDigestLen == NULL) {
1257c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
1267c478bd9Sstevel@tonic-gate 		goto clean_exit;
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* Acquire the session lock */
1307c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/* Application must call C_DigestInit before calling C_Digest */
1337c478bd9Sstevel@tonic-gate 	if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
1347c478bd9Sstevel@tonic-gate 		/*
1357c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
1367c478bd9Sstevel@tonic-gate 		 * We hold the session lock, and SES_REFRELE()
1377c478bd9Sstevel@tonic-gate 		 * will release the session lock for us.
1387c478bd9Sstevel@tonic-gate 		 */
1397c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
1407c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * C_Digest must be called without intervening C_DigestUpdate
1457c478bd9Sstevel@tonic-gate 	 * calls.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	if (session_p->digest.flags & CRYPTO_OPERATION_UPDATE) {
1487c478bd9Sstevel@tonic-gate 		/*
1497c478bd9Sstevel@tonic-gate 		 * C_Digest can not be used to terminate a multi-part
1507c478bd9Sstevel@tonic-gate 		 * operation, so we'll leave the active digest operation
1517c478bd9Sstevel@tonic-gate 		 * flag on and let the application continue with the
1527c478bd9Sstevel@tonic-gate 		 * digest update operation.
1537c478bd9Sstevel@tonic-gate 		 *
1547c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
1557c478bd9Sstevel@tonic-gate 		 * We hold the session lock, and SES_REFRELE()
1567c478bd9Sstevel@tonic-gate 		 * will release the session lock for us.
1577c478bd9Sstevel@tonic-gate 		 */
1587c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
1597c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	rv = soft_digest(session_p, pData, ulDataLen, pDigest, pulDigestLen);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if ((rv == CKR_BUFFER_TOO_SMALL) ||
1677c478bd9Sstevel@tonic-gate 	    (pDigest == NULL && rv == CKR_OK)) {
1687c478bd9Sstevel@tonic-gate 		/*
1697c478bd9Sstevel@tonic-gate 		 * We will not terminate the active digest operation flag,
1707c478bd9Sstevel@tonic-gate 		 * when the application-supplied buffer is too small, or
1717c478bd9Sstevel@tonic-gate 		 * the application asks for the length of buffer to hold
1727c478bd9Sstevel@tonic-gate 		 * the message digest.
1737c478bd9Sstevel@tonic-gate 		 *
1747c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
1757c478bd9Sstevel@tonic-gate 		 * We do not hold the session lock.
1767c478bd9Sstevel@tonic-gate 		 */
1777c478bd9Sstevel@tonic-gate 		lock_held = B_FALSE;
1787c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
1797c478bd9Sstevel@tonic-gate 		return (rv);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate clean_exit:
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Terminates the active digest operation.
1857c478bd9Sstevel@tonic-gate 	 * Application needs to call C_DigestInit again for next
1867c478bd9Sstevel@tonic-gate 	 * digest operation.
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
189588a1af0SAlexandr Nedvedicky 
190588a1af0SAlexandr Nedvedicky 	soft_digest_cleanup(session_p, lock_held);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * Decrement the session reference count.
1947c478bd9Sstevel@tonic-gate 	 * We hold the session lock, and SES_REFRELE()
1957c478bd9Sstevel@tonic-gate 	 * will release the session lock for us.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	return (rv);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate CK_RV
C_DigestUpdate(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pPart,CK_ULONG ulPartLen)2047c478bd9Sstevel@tonic-gate C_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
2057c478bd9Sstevel@tonic-gate     CK_ULONG ulPartLen)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	CK_RV		rv;
2097c478bd9Sstevel@tonic-gate 	soft_session_t	*session_p;
2107c478bd9Sstevel@tonic-gate 	boolean_t lock_held = B_TRUE;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
2137c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * Obtain the session pointer. Also, increment the session
2177c478bd9Sstevel@tonic-gate 	 * reference count.
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
2207c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
2217c478bd9Sstevel@tonic-gate 		return (rv);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (pPart == NULL) {
2247c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
2257c478bd9Sstevel@tonic-gate 		goto clean_exit;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/* Acquire the session lock */
2297c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Application must call C_DigestInit before calling
2337c478bd9Sstevel@tonic-gate 	 * C_DigestUpdate.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
2367c478bd9Sstevel@tonic-gate 		/*
2377c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
2387c478bd9Sstevel@tonic-gate 		 * We hold the session lock, and SES_REFRELE()
2397c478bd9Sstevel@tonic-gate 		 * will release the session lock for us.
2407c478bd9Sstevel@tonic-gate 		 */
2417c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
2427c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* Set update flag to protect C_Digest */
2467c478bd9Sstevel@tonic-gate 	session_p->digest.flags |= CRYPTO_OPERATION_UPDATE;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	rv = soft_digest_update(session_p, pPart, ulPartLen);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK) {
2537c478bd9Sstevel@tonic-gate 		/*
2547c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
2557c478bd9Sstevel@tonic-gate 		 * We do not hold the session lock.
2567c478bd9Sstevel@tonic-gate 		 */
2577c478bd9Sstevel@tonic-gate 		lock_held = B_FALSE;
2587c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
2597c478bd9Sstevel@tonic-gate 		return (CKR_OK);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate clean_exit:
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * After an error occurred, terminate the current digest
2657c478bd9Sstevel@tonic-gate 	 * operation by resetting the active and update flags.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
268588a1af0SAlexandr Nedvedicky 
269588a1af0SAlexandr Nedvedicky 	soft_digest_cleanup(session_p, lock_held);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Decrement the session reference count.
2737c478bd9Sstevel@tonic-gate 	 * We hold the session lock, and SES_REFRELE()
2747c478bd9Sstevel@tonic-gate 	 * will release the session lock for us.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	return (rv);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate CK_RV
C_DigestKey(CK_SESSION_HANDLE hSession,CK_OBJECT_HANDLE hKey)2837c478bd9Sstevel@tonic-gate C_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	CK_RV		rv;
2877c478bd9Sstevel@tonic-gate 	soft_session_t	*session_p;
2887c478bd9Sstevel@tonic-gate 	soft_object_t	*key_p;
2897c478bd9Sstevel@tonic-gate 	boolean_t lock_held = B_TRUE;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
2927c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * Obtain the session pointer. Also, increment the session
2967c478bd9Sstevel@tonic-gate 	 * reference count.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
2997c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
3007c478bd9Sstevel@tonic-gate 		return (rv);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/* Obtain the object pointer. */
3037c478bd9Sstevel@tonic-gate 	HANDLE2OBJECT(hKey, key_p, rv);
3047c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
3057c478bd9Sstevel@tonic-gate 		goto clean_exit;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * Application must call C_DigestInit before calling
3117c478bd9Sstevel@tonic-gate 	 * C_DigestKey.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
3167c478bd9Sstevel@tonic-gate 		 * We hold the session lock, and SES_REFRELE()
3177c478bd9Sstevel@tonic-gate 		 * will release the session lock for us.
3187c478bd9Sstevel@tonic-gate 		 */
3197c478bd9Sstevel@tonic-gate 		OBJ_REFRELE(key_p);
3207c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
3217c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * Remember the fact that a key was thrown into the mix, so that
3267c478bd9Sstevel@tonic-gate 	 * C_DigestFinal bzero()'s the digest context before freeing it.
3277c478bd9Sstevel@tonic-gate 	 */
3287c478bd9Sstevel@tonic-gate 	session_p->digest.flags |= (CRYPTO_KEY_DIGESTED |
3297c478bd9Sstevel@tonic-gate 	    CRYPTO_OPERATION_UPDATE);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	rv = soft_digest_key(session_p, key_p);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK) {
3367c478bd9Sstevel@tonic-gate 		/*
3377c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
3387c478bd9Sstevel@tonic-gate 		 * We do not hold the session lock.
3397c478bd9Sstevel@tonic-gate 		 */
3407c478bd9Sstevel@tonic-gate 		lock_held = B_FALSE;
3417c478bd9Sstevel@tonic-gate 		OBJ_REFRELE(key_p);
3427c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
3437c478bd9Sstevel@tonic-gate 		return (CKR_OK);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	OBJ_REFRELE(key_p);
3477c478bd9Sstevel@tonic-gate clean_exit:
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * After an error occurred, terminate the current digest
3507c478bd9Sstevel@tonic-gate 	 * operation by resetting the active and update flags.
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
353588a1af0SAlexandr Nedvedicky 
354588a1af0SAlexandr Nedvedicky 	soft_digest_cleanup(session_p, lock_held);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * Decrement the session reference count.
3587c478bd9Sstevel@tonic-gate 	 * We hold the session lock, and SES_REFRELE()
3597c478bd9Sstevel@tonic-gate 	 * will release the session lock for us.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
3627c478bd9Sstevel@tonic-gate 	return (rv);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate CK_RV
C_DigestFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pDigest,CK_ULONG_PTR pulDigestLen)3677c478bd9Sstevel@tonic-gate C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
3687c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulDigestLen)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	CK_RV		rv;
3727c478bd9Sstevel@tonic-gate 	soft_session_t	*session_p;
3737c478bd9Sstevel@tonic-gate 	boolean_t lock_held = B_TRUE;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
3767c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * Obtain the session pointer. Also, increment the session
3807c478bd9Sstevel@tonic-gate 	 * reference count.
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
3837c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
3847c478bd9Sstevel@tonic-gate 		return (rv);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (pulDigestLen == NULL) {
3877c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3887c478bd9Sstevel@tonic-gate 		goto clean_exit;
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/* Acquire the session lock */
3927c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * Application must call C_DigestInit before calling
3967c478bd9Sstevel@tonic-gate 	 * C_DigestFinal.
3977c478bd9Sstevel@tonic-gate 	 */
3987c478bd9Sstevel@tonic-gate 	if (!(session_p->digest.flags & CRYPTO_OPERATION_ACTIVE)) {
3997c478bd9Sstevel@tonic-gate 		/*
4007c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
4017c478bd9Sstevel@tonic-gate 		 * We hold the session lock, and SES_REFRELE()
4027c478bd9Sstevel@tonic-gate 		 * will release the session lock for us.
4037c478bd9Sstevel@tonic-gate 		 */
4047c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
4057c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	rv = soft_digest_final(session_p, pDigest, pulDigestLen);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if ((rv == CKR_BUFFER_TOO_SMALL) ||
4137c478bd9Sstevel@tonic-gate 	    (pDigest == NULL && rv == CKR_OK)) {
4147c478bd9Sstevel@tonic-gate 		/*
4157c478bd9Sstevel@tonic-gate 		 * We will not terminate the active digest operation flag,
4167c478bd9Sstevel@tonic-gate 		 * when the application-supplied buffer is too small, or
4177c478bd9Sstevel@tonic-gate 		 * the application asks for the length of buffer to hold
4187c478bd9Sstevel@tonic-gate 		 * the message digest.
4197c478bd9Sstevel@tonic-gate 		 *
4207c478bd9Sstevel@tonic-gate 		 * Decrement the session reference count.
4217c478bd9Sstevel@tonic-gate 		 * We do not hold the session lock.
4227c478bd9Sstevel@tonic-gate 		 */
4237c478bd9Sstevel@tonic-gate 		lock_held = B_FALSE;
4247c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_held);
4257c478bd9Sstevel@tonic-gate 		return (rv);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate clean_exit:
4297c478bd9Sstevel@tonic-gate 	/* Terminates the active digest operation */
4307c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
431588a1af0SAlexandr Nedvedicky 
432588a1af0SAlexandr Nedvedicky 	soft_digest_cleanup(session_p, lock_held);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * Decrement the session reference count.
4367c478bd9Sstevel@tonic-gate 	 * We hold the session lock, and SES_REFRELE()
4377c478bd9Sstevel@tonic-gate 	 * will release the session lock for us.
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	return (rv);
4427c478bd9Sstevel@tonic-gate }
443