xref: /titanic_41/usr/src/lib/pkcs11/pkcs11_softtoken/common/softDecrypt.c (revision d29b2c4438482eb00488be49a1f5d6835f455546)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <pthread.h>
29 #include <security/cryptoki.h>
30 #include "softGlobal.h"
31 #include "softSession.h"
32 #include "softObject.h"
33 #include "softOps.h"
34 
35 
36 CK_RV
37 C_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
38     CK_OBJECT_HANDLE hKey)
39 {
40 
41 	CK_RV		rv;
42 	soft_session_t	*session_p;
43 	soft_object_t	*key_p;
44 	boolean_t	lock_held = B_FALSE;
45 
46 	if (!softtoken_initialized)
47 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
48 
49 	/* Obtain the session pointer. */
50 	rv = handle2session(hSession, &session_p);
51 	if (rv != CKR_OK)
52 		return (rv);
53 
54 	if (pMechanism == NULL) {
55 		rv = CKR_ARGUMENTS_BAD;
56 		goto clean_exit;
57 	}
58 
59 	/* Obtain the object pointer. */
60 	HANDLE2OBJECT(hKey, key_p, rv);
61 	if (rv != CKR_OK)
62 		goto clean_exit;
63 
64 	/* Check to see if key object allows for decryption. */
65 	if (!(key_p->bool_attr_mask & DECRYPT_BOOL_ON)) {
66 		rv = CKR_KEY_TYPE_INCONSISTENT;
67 		goto clean_exit1;
68 	}
69 
70 	(void) pthread_mutex_lock(&session_p->session_mutex);
71 	lock_held = B_TRUE;
72 
73 	/* Check to see if decrypt operation is already active. */
74 	if (session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE) {
75 		/* free the memory to avoid memory leak */
76 		soft_crypt_cleanup(session_p, B_FALSE, lock_held);
77 	}
78 
79 	/*
80 	 * This active flag will remain ON until application calls either
81 	 * C_Decrypt or C_DecryptFinal to actually obtain the final piece
82 	 * of plaintext.
83 	 */
84 	session_p->decrypt.flags = CRYPTO_OPERATION_ACTIVE;
85 
86 	(void) pthread_mutex_unlock(&session_p->session_mutex);
87 	lock_held = B_FALSE;
88 
89 	rv = soft_decrypt_init(session_p, pMechanism, key_p);
90 
91 	if (rv != CKR_OK) {
92 		(void) pthread_mutex_lock(&session_p->session_mutex);
93 		session_p->decrypt.flags &= ~CRYPTO_OPERATION_ACTIVE;
94 		lock_held = B_TRUE;
95 	}
96 
97 clean_exit1:
98 	OBJ_REFRELE(key_p);
99 clean_exit:
100 	SES_REFRELE(session_p, lock_held);
101 	return (rv);
102 }
103 
104 
105 CK_RV
106 C_Decrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData,
107     CK_ULONG ulEncryptedDataLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
108 {
109 
110 	CK_RV		rv;
111 	soft_session_t	*session_p;
112 	boolean_t	lock_held = B_FALSE;
113 
114 	if (!softtoken_initialized)
115 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
116 
117 	/* Obatin the session pointer. */
118 	rv = handle2session(hSession, &session_p);
119 	if (rv != CKR_OK)
120 		return (rv);
121 
122 	/*
123 	 * Only check if input buffer is null.  How to handle zero input
124 	 * length depents on the mechanism in use.  For secret key mechanisms,
125 	 * unpadded ones yield zero length output, but padded ones always
126 	 * result in smaller than original, possibly zero, length output.
127 	 */
128 	if (pEncryptedData == NULL) {
129 		rv = CKR_ARGUMENTS_BAD;
130 		goto clean_exit;
131 	}
132 
133 	/*
134 	 * No need to check pData because application might
135 	 * just want to know the length of decrypted data.
136 	 */
137 	if (pulDataLen == NULL) {
138 		rv = CKR_ARGUMENTS_BAD;
139 		goto clean_exit;
140 	}
141 
142 	(void) pthread_mutex_lock(&session_p->session_mutex);
143 	lock_held = B_TRUE;
144 
145 	/* Application must call C_DecryptInit before calling C_Decrypt. */
146 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
147 		SES_REFRELE(session_p, lock_held);
148 		return (CKR_OPERATION_NOT_INITIALIZED);
149 	}
150 
151 	/*
152 	 * C_Decrypt must be called without intervening C_DecryptUpdate
153 	 * calls.
154 	 */
155 	if (session_p->decrypt.flags & CRYPTO_OPERATION_UPDATE) {
156 		/*
157 		 * C_Decrypt can not be used to terminate a multi-part
158 		 * operation, so we'll leave the active decrypt operation
159 		 * flag on and let the application continue with the
160 		 * decrypt update operation.
161 		 */
162 		SES_REFRELE(session_p, lock_held);
163 		return (CKR_FUNCTION_FAILED);
164 	}
165 
166 	(void) pthread_mutex_unlock(&session_p->session_mutex);
167 	lock_held = B_FALSE;
168 
169 	rv = soft_decrypt(session_p, pEncryptedData, ulEncryptedDataLen,
170 	    pData, pulDataLen);
171 
172 	if ((rv == CKR_BUFFER_TOO_SMALL) ||
173 	    (pData == NULL && rv == CKR_OK)) {
174 		/*
175 		 * We will not terminate the active decrypt operation flag,
176 		 * when the application-supplied buffer is too small, or
177 		 * the application asks for the length of buffer to hold
178 		 * the plaintext.
179 		 */
180 		SES_REFRELE(session_p, lock_held);
181 		return (rv);
182 	}
183 
184 clean_exit:
185 	/* Clear context, free key, and release session counter */
186 	soft_crypt_cleanup(session_p, B_FALSE, B_FALSE);
187 
188 	return (rv);
189 }
190 
191 
192 CK_RV
193 C_DecryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
194     CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart,
195     CK_ULONG_PTR pulPartLen)
196 {
197 
198 	CK_RV		rv;
199 	soft_session_t	*session_p;
200 	boolean_t	lock_held = B_FALSE;
201 
202 	if (!softtoken_initialized)
203 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
204 
205 	/* Obtain the session pointer. */
206 	rv = handle2session(hSession, &session_p);
207 	if (rv != CKR_OK)
208 		return (rv);
209 
210 	/*
211 	 * Only check if input buffer is null.  How to handle zero input
212 	 * length depents on the mechanism in use.  For secret key mechanisms,
213 	 * unpadded ones yeild zero length output, but padded ones always
214 	 * result in smaller than original, possibly zero, length output.
215 	 */
216 	if (pEncryptedPart == NULL) {
217 		rv = CKR_ARGUMENTS_BAD;
218 		goto clean_exit;
219 	}
220 
221 	/*
222 	 * Only check if pulPartLen is NULL.
223 	 * No need to check if pPart is NULL because application
224 	 * might just ask for the length of buffer to hold the
225 	 * recovered data.
226 	 */
227 	if (pulPartLen == NULL) {
228 		rv = CKR_ARGUMENTS_BAD;
229 		goto clean_exit;
230 	}
231 
232 	(void) pthread_mutex_lock(&session_p->session_mutex);
233 	lock_held = B_TRUE;
234 
235 	/*
236 	 * Application must call C_DecryptInit before calling
237 	 * C_DecryptUpdate.
238 	 */
239 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
240 		SES_REFRELE(session_p, lock_held);
241 		return (CKR_OPERATION_NOT_INITIALIZED);
242 	}
243 
244 	session_p->decrypt.flags |= CRYPTO_OPERATION_UPDATE;
245 
246 	(void) pthread_mutex_unlock(&session_p->session_mutex);
247 	lock_held = B_FALSE;
248 
249 	rv = soft_decrypt_update(session_p, pEncryptedPart,
250 	    ulEncryptedPartLen, pPart, pulPartLen);
251 
252 	/*
253 	 * If CKR_OK or CKR_BUFFER_TOO_SMALL, don't terminate the
254 	 * current decryption operation.
255 	 */
256 	if ((rv == CKR_OK) || (rv == CKR_BUFFER_TOO_SMALL)) {
257 		SES_REFRELE(session_p, lock_held);
258 		return (rv);
259 	}
260 
261 clean_exit:
262 	/*
263 	 * After an error occurred, terminate the current decrypt
264 	 * operation by resetting the active and update flags.
265 	 */
266 	soft_crypt_cleanup(session_p, B_FALSE, lock_held);
267 
268 	return (rv);
269 }
270 
271 CK_RV
272 C_DecryptFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastPart,
273     CK_ULONG_PTR pulLastPartLen)
274 {
275 
276 	CK_RV		rv;
277 	soft_session_t	*session_p;
278 	boolean_t	lock_held = B_FALSE;
279 
280 	if (!softtoken_initialized)
281 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
282 
283 	/* Obtain the session pointer. */
284 	rv = handle2session(hSession, &session_p);
285 	if (rv != CKR_OK)
286 		return (rv);
287 
288 	if (pulLastPartLen == NULL) {
289 		rv = CKR_ARGUMENTS_BAD;
290 		goto clean_exit;
291 	}
292 
293 	(void) pthread_mutex_lock(&session_p->session_mutex);
294 	lock_held = B_TRUE;
295 
296 	/*
297 	 * Application must call C_DecryptInit before calling
298 	 * C_DecryptFinal.
299 	 */
300 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
301 		SES_REFRELE(session_p, lock_held);
302 		return (CKR_OPERATION_NOT_INITIALIZED);
303 	}
304 
305 	(void) pthread_mutex_unlock(&session_p->session_mutex);
306 	lock_held = B_FALSE;
307 
308 	rv = soft_decrypt_final(session_p, pLastPart, pulLastPartLen);
309 
310 	if ((rv == CKR_BUFFER_TOO_SMALL) ||
311 	    (pLastPart == NULL && rv == CKR_OK)) {
312 		/*
313 		 * We will not terminate the active decrypt operation flag,
314 		 * when the application-supplied buffer is too small, or
315 		 * the application asks for the length of buffer to hold
316 		 * the plaintext.
317 		 */
318 		SES_REFRELE(session_p, lock_held);
319 		return (rv);
320 	}
321 
322 	/* Terminates the active encrypt operation. */
323 	(void) pthread_mutex_lock(&session_p->session_mutex);
324 	session_p->decrypt.flags = 0;
325 	lock_held = B_TRUE;
326 	SES_REFRELE(session_p, lock_held);
327 	return (rv);
328 
329 clean_exit:
330 	/* Terminates the active decrypt operation */
331 	soft_crypt_cleanup(session_p, B_FALSE, lock_held);
332 
333 	return (rv);
334 }
335