4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <security/cryptoki.h>
28 #include "softGlobal.h"
29 #include "softSession.h"
30 #include "softObject.h"
35 C_DecryptInit(CK_SESSION_HANDLE hSession
, CK_MECHANISM_PTR pMechanism
,
36 CK_OBJECT_HANDLE hKey
)
40 soft_session_t
*session_p
;
42 boolean_t lock_held
= B_FALSE
;
44 if (!softtoken_initialized
)
45 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
47 /* Obtain the session pointer. */
48 rv
= handle2session(hSession
, &session_p
);
52 if (pMechanism
== NULL
) {
53 rv
= CKR_ARGUMENTS_BAD
;
57 /* Obtain the object pointer. */
58 HANDLE2OBJECT(hKey
, key_p
, rv
);
62 /* Check to see if key object allows for decryption. */
63 if (!(key_p
->bool_attr_mask
& DECRYPT_BOOL_ON
)) {
64 rv
= CKR_KEY_FUNCTION_NOT_PERMITTED
;
68 (void) pthread_mutex_lock(&session_p
->session_mutex
);
71 /* Check to see if decrypt operation is already active. */
72 if (session_p
->decrypt
.flags
& CRYPTO_OPERATION_ACTIVE
) {
73 /* free the memory to avoid memory leak */
74 soft_crypt_cleanup(session_p
, B_FALSE
, lock_held
);
78 * This active flag will remain ON until application calls either
79 * C_Decrypt or C_DecryptFinal to actually obtain the final piece
82 session_p
->decrypt
.flags
= CRYPTO_OPERATION_ACTIVE
;
84 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
87 rv
= soft_decrypt_init(session_p
, pMechanism
, key_p
);
90 (void) pthread_mutex_lock(&session_p
->session_mutex
);
91 session_p
->decrypt
.flags
&= ~CRYPTO_OPERATION_ACTIVE
;
98 SES_REFRELE(session_p
, lock_held
);
104 C_Decrypt(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pEncryptedData
,
105 CK_ULONG ulEncryptedDataLen
, CK_BYTE_PTR pData
, CK_ULONG_PTR pulDataLen
)
109 soft_session_t
*session_p
;
110 boolean_t lock_held
= B_FALSE
;
112 if (!softtoken_initialized
)
113 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
115 /* Obatin the session pointer. */
116 rv
= handle2session(hSession
, &session_p
);
121 * Only check if input buffer is null. How to handle zero input
122 * length depents on the mechanism in use. For secret key mechanisms,
123 * unpadded ones yield zero length output, but padded ones always
124 * result in smaller than original, possibly zero, length output.
126 if (pEncryptedData
== NULL
) {
127 rv
= CKR_ARGUMENTS_BAD
;
132 * No need to check pData because application might
133 * just want to know the length of decrypted data.
135 if (pulDataLen
== NULL
) {
136 rv
= CKR_ARGUMENTS_BAD
;
140 (void) pthread_mutex_lock(&session_p
->session_mutex
);
143 /* Application must call C_DecryptInit before calling C_Decrypt. */
144 if (!(session_p
->decrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
145 SES_REFRELE(session_p
, lock_held
);
146 return (CKR_OPERATION_NOT_INITIALIZED
);
150 * C_Decrypt must be called without intervening C_DecryptUpdate
153 if (session_p
->decrypt
.flags
& CRYPTO_OPERATION_UPDATE
) {
155 * C_Decrypt can not be used to terminate a multi-part
156 * operation, so we'll leave the active decrypt operation
157 * flag on and let the application continue with the
158 * decrypt update operation.
160 SES_REFRELE(session_p
, lock_held
);
161 return (CKR_FUNCTION_FAILED
);
164 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
167 rv
= soft_decrypt(session_p
, pEncryptedData
, ulEncryptedDataLen
,
170 if ((rv
== CKR_BUFFER_TOO_SMALL
) ||
171 (pData
== NULL
&& rv
== CKR_OK
)) {
173 * We will not terminate the active decrypt operation flag,
174 * when the application-supplied buffer is too small, or
175 * the application asks for the length of buffer to hold
178 SES_REFRELE(session_p
, lock_held
);
183 /* Clear context, free key, and release session counter */
184 soft_crypt_cleanup(session_p
, B_FALSE
, B_FALSE
);
191 C_DecryptUpdate(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pEncryptedPart
,
192 CK_ULONG ulEncryptedPartLen
, CK_BYTE_PTR pPart
,
193 CK_ULONG_PTR pulPartLen
)
197 soft_session_t
*session_p
;
198 boolean_t lock_held
= B_FALSE
;
200 if (!softtoken_initialized
)
201 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
203 /* Obtain the session pointer. */
204 rv
= handle2session(hSession
, &session_p
);
209 * Only check if input buffer is null. How to handle zero input
210 * length depents on the mechanism in use. For secret key mechanisms,
211 * unpadded ones yeild zero length output, but padded ones always
212 * result in smaller than original, possibly zero, length output.
214 if (pEncryptedPart
== NULL
) {
215 rv
= CKR_ARGUMENTS_BAD
;
220 * Only check if pulPartLen is NULL.
221 * No need to check if pPart is NULL because application
222 * might just ask for the length of buffer to hold the
225 if (pulPartLen
== NULL
) {
226 rv
= CKR_ARGUMENTS_BAD
;
230 (void) pthread_mutex_lock(&session_p
->session_mutex
);
234 * Application must call C_DecryptInit before calling
237 if (!(session_p
->decrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
238 SES_REFRELE(session_p
, lock_held
);
239 return (CKR_OPERATION_NOT_INITIALIZED
);
242 session_p
->decrypt
.flags
|= CRYPTO_OPERATION_UPDATE
;
244 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
247 rv
= soft_decrypt_update(session_p
, pEncryptedPart
,
248 ulEncryptedPartLen
, pPart
, pulPartLen
);
251 * If CKR_OK or CKR_BUFFER_TOO_SMALL, don't terminate the
252 * current decryption operation.
254 if ((rv
== CKR_OK
) || (rv
== CKR_BUFFER_TOO_SMALL
)) {
255 SES_REFRELE(session_p
, lock_held
);
261 * After an error occurred, terminate the current decrypt
262 * operation by resetting the active and update flags.
264 soft_crypt_cleanup(session_p
, B_FALSE
, lock_held
);
270 C_DecryptFinal(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pLastPart
,
271 CK_ULONG_PTR pulLastPartLen
)
275 soft_session_t
*session_p
;
276 boolean_t lock_held
= B_FALSE
;
278 if (!softtoken_initialized
)
279 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
281 /* Obtain the session pointer. */
282 rv
= handle2session(hSession
, &session_p
);
286 if (pulLastPartLen
== NULL
) {
287 rv
= CKR_ARGUMENTS_BAD
;
291 (void) pthread_mutex_lock(&session_p
->session_mutex
);
295 * Application must call C_DecryptInit before calling
298 if (!(session_p
->decrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
299 SES_REFRELE(session_p
, lock_held
);
300 return (CKR_OPERATION_NOT_INITIALIZED
);
303 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
306 rv
= soft_decrypt_final(session_p
, pLastPart
, pulLastPartLen
);
308 if ((rv
== CKR_BUFFER_TOO_SMALL
) ||
309 (pLastPart
== NULL
&& rv
== CKR_OK
)) {
311 * We will not terminate the active decrypt operation flag,
312 * when the application-supplied buffer is too small, or
313 * the application asks for the length of buffer to hold
316 SES_REFRELE(session_p
, lock_held
);
320 /* Terminates the active encrypt operation. */
321 (void) pthread_mutex_lock(&session_p
->session_mutex
);
322 session_p
->decrypt
.flags
= 0;
324 SES_REFRELE(session_p
, lock_held
);
328 /* Terminates the active decrypt operation */
329 soft_crypt_cleanup(session_p
, B_FALSE
, lock_held
);