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