1 /* |
|
2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * |
|
4 */ |
|
5 |
|
6 /* crypto/engine/e_pk11.c */ |
|
7 /* |
|
8 * This product includes software developed by the OpenSSL Project for |
|
9 * use in the OpenSSL Toolkit (http://www.openssl.org/). |
|
10 * |
|
11 * This project also referenced hw_pkcs11-0.9.7b.patch written by |
|
12 * Afchine Madjlessi. |
|
13 */ |
|
14 /* |
|
15 * ==================================================================== |
|
16 * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved. |
|
17 * |
|
18 * Redistribution and use in source and binary forms, with or without |
|
19 * modification, are permitted provided that the following conditions |
|
20 * are met: |
|
21 * |
|
22 * 1. Redistributions of source code must retain the above copyright |
|
23 * notice, this list of conditions and the following disclaimer. |
|
24 * |
|
25 * 2. Redistributions in binary form must reproduce the above copyright |
|
26 * notice, this list of conditions and the following disclaimer in |
|
27 * the documentation and/or other materials provided with the |
|
28 * distribution. |
|
29 * |
|
30 * 3. All advertising materials mentioning features or use of this |
|
31 * software must display the following acknowledgment: |
|
32 * "This product includes software developed by the OpenSSL Project |
|
33 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
|
34 * |
|
35 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
|
36 * endorse or promote products derived from this software without |
|
37 * prior written permission. For written permission, please contact |
|
38 * [email protected]. |
|
39 * |
|
40 * 5. Products derived from this software may not be called "OpenSSL" |
|
41 * nor may "OpenSSL" appear in their names without prior written |
|
42 * permission of the OpenSSL Project. |
|
43 * |
|
44 * 6. Redistributions of any form whatsoever must retain the following |
|
45 * acknowledgment: |
|
46 * "This product includes software developed by the OpenSSL Project |
|
47 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
|
48 * |
|
49 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
|
50 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
|
53 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
60 * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
61 * ==================================================================== |
|
62 * |
|
63 * This product includes cryptographic software written by Eric Young |
|
64 * ([email protected]). This product includes software written by Tim |
|
65 * Hudson ([email protected]). |
|
66 * |
|
67 */ |
|
68 |
|
69 #include <stdio.h> |
|
70 #include <stdlib.h> |
|
71 #include <string.h> |
|
72 #include <sys/types.h> |
|
73 #include <unistd.h> |
|
74 #include <strings.h> |
|
75 |
|
76 #include <openssl/e_os2.h> |
|
77 #include <openssl/crypto.h> |
|
78 #include <openssl/engine.h> |
|
79 #include <openssl/dso.h> |
|
80 #include <openssl/err.h> |
|
81 #include <openssl/bn.h> |
|
82 #include <openssl/md5.h> |
|
83 #include <openssl/pem.h> |
|
84 #ifndef OPENSSL_NO_RSA |
|
85 #include <openssl/rsa.h> |
|
86 #endif |
|
87 #ifndef OPENSSL_NO_DSA |
|
88 #include <openssl/dsa.h> |
|
89 #endif |
|
90 #ifndef OPENSSL_NO_DH |
|
91 #include <openssl/dh.h> |
|
92 #endif |
|
93 #include <openssl/rand.h> |
|
94 #include <openssl/objects.h> |
|
95 #include <openssl/x509.h> |
|
96 #include <openssl/aes.h> |
|
97 #include <dlfcn.h> |
|
98 #include <pthread.h> |
|
99 |
|
100 #ifndef OPENSSL_NO_HW |
|
101 #ifndef OPENSSL_NO_HW_PK11 |
|
102 |
|
103 /* label for debug messages printed on stderr */ |
|
104 #define PK11_DBG "PKCS#11 ENGINE DEBUG" |
|
105 /* prints a lot of debug messages on stderr about slot selection process */ |
|
106 #undef DEBUG_SLOT_SELECTION |
|
107 /* |
|
108 * Solaris specific code. See comment at check_hw_mechanisms() for more |
|
109 * information. |
|
110 */ |
|
111 #if defined(__SVR4) && defined(__sun) |
|
112 #define SOLARIS_HW_SLOT_SELECTION |
|
113 #endif |
|
114 |
|
115 #ifdef DEBUG_SLOT_SELECTION |
|
116 #define DEBUG_SLOT_SEL(...) fprintf(stderr, __VA_ARGS__) |
|
117 #else |
|
118 #define DEBUG_SLOT_SEL(...) |
|
119 #endif |
|
120 |
|
121 #include <security/cryptoki.h> |
|
122 #include <security/pkcs11.h> |
|
123 #include "e_pk11.h" |
|
124 #include "e_pk11_uri.h" |
|
125 |
|
126 static CK_BBOOL pk11_true = CK_TRUE; |
|
127 static CK_BBOOL pk11_false = CK_FALSE; |
|
128 #define PK11_ENGINE_LIB_NAME "PKCS#11 engine" |
|
129 #include "e_pk11_err.c" |
|
130 #include "e_pk11_uri.c" |
|
131 #include "e_pk11_pub.c" |
|
132 |
|
133 /* |
|
134 * We use this lock to prevent multiple C_Login()s, guard getpassphrase(), |
|
135 * uri_struct manipulation, and static token info. All of that is used by the |
|
136 * RSA keys by reference feature. |
|
137 */ |
|
138 pthread_mutex_t *uri_lock = NULL; |
|
139 |
|
140 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
141 /* |
|
142 * Tables for symmetric ciphers and digest mechs found in the pkcs11_kernel |
|
143 * library. See comment at check_hw_mechanisms() for more information. |
|
144 */ |
|
145 int *hw_cnids; |
|
146 int *hw_dnids; |
|
147 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
148 |
|
149 /* PKCS#11 session caches and their locks for all operation types */ |
|
150 static PK11_CACHE session_cache[OP_MAX]; |
|
151 |
|
152 /* |
|
153 * We cache the flags so that we do not have to run C_GetTokenInfo() again when |
|
154 * logging into the token. |
|
155 */ |
|
156 CK_FLAGS pubkey_token_flags; |
|
157 |
|
158 /* |
|
159 * As stated in v2.20, 11.7 Object Management Function, in section for |
|
160 * C_FindObjectsInit(), at most one search operation may be active at a given |
|
161 * time in a given session. Therefore, C_Find{,Init,Final}Objects() should be |
|
162 * grouped together to form one atomic search operation. This is already |
|
163 * ensured by the property of unique PKCS#11 session handle used for each |
|
164 * PK11_SESSION object. |
|
165 * |
|
166 * This is however not the biggest concern - maintaining consistency of the |
|
167 * underlying object store is more important. The same section of the spec also |
|
168 * says that one thread can be in the middle of a search operation while another |
|
169 * thread destroys the object matching the search template which would result in |
|
170 * invalid handle returned from the search operation. |
|
171 * |
|
172 * Hence, the following locks are used for both protection of the object stores. |
|
173 * They are also used for active list protection. |
|
174 */ |
|
175 pthread_mutex_t *find_lock[OP_MAX] = { NULL }; |
|
176 |
|
177 /* |
|
178 * lists of asymmetric key handles which are active (referenced by at least one |
|
179 * PK11_SESSION structure, either held by a thread or present in free_session |
|
180 * list) for given algorithm type |
|
181 */ |
|
182 PK11_active *active_list[OP_MAX] = { NULL }; |
|
183 |
|
184 /* |
|
185 * Create all secret key objects in a global session so that they are available |
|
186 * to use for other sessions. These other sessions may be opened or closed |
|
187 * without losing the secret key objects. |
|
188 */ |
|
189 static CK_SESSION_HANDLE global_session = CK_INVALID_HANDLE; |
|
190 |
|
191 /* Index for the supported ciphers */ |
|
192 enum pk11_cipher_id { |
|
193 PK11_DES_CBC, |
|
194 PK11_DES3_CBC, |
|
195 PK11_DES_ECB, |
|
196 PK11_DES3_ECB, |
|
197 PK11_RC4, |
|
198 PK11_AES_128_CBC, |
|
199 PK11_AES_192_CBC, |
|
200 PK11_AES_256_CBC, |
|
201 PK11_AES_128_ECB, |
|
202 PK11_AES_192_ECB, |
|
203 PK11_AES_256_ECB, |
|
204 PK11_BLOWFISH_CBC, |
|
205 PK11_AES_128_CTR, |
|
206 PK11_AES_192_CTR, |
|
207 PK11_AES_256_CTR, |
|
208 PK11_CIPHER_MAX |
|
209 }; |
|
210 |
|
211 /* Index for the supported digests */ |
|
212 enum pk11_digest_id { |
|
213 PK11_MD5, |
|
214 PK11_SHA1, |
|
215 PK11_SHA224, |
|
216 PK11_SHA256, |
|
217 PK11_SHA384, |
|
218 PK11_SHA512, |
|
219 PK11_DIGEST_MAX |
|
220 }; |
|
221 |
|
222 typedef struct PK11_CIPHER_st |
|
223 { |
|
224 enum pk11_cipher_id id; |
|
225 int nid; |
|
226 int iv_len; |
|
227 int min_key_len; |
|
228 int max_key_len; |
|
229 CK_KEY_TYPE key_type; |
|
230 CK_MECHANISM_TYPE mech_type; |
|
231 } PK11_CIPHER; |
|
232 |
|
233 typedef struct PK11_DIGEST_st |
|
234 { |
|
235 enum pk11_digest_id id; |
|
236 int nid; |
|
237 CK_MECHANISM_TYPE mech_type; |
|
238 } PK11_DIGEST; |
|
239 |
|
240 /* ENGINE level stuff */ |
|
241 static int pk11_init(ENGINE *e); |
|
242 static int pk11_library_init(ENGINE *e); |
|
243 static int pk11_finish(ENGINE *e); |
|
244 static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); |
|
245 static int pk11_destroy(ENGINE *e); |
|
246 |
|
247 /* RAND stuff */ |
|
248 static void pk11_rand_seed(const void *buf, int num); |
|
249 static void pk11_rand_add(const void *buf, int num, double add_entropy); |
|
250 static void pk11_rand_cleanup(void); |
|
251 static int pk11_rand_bytes(unsigned char *buf, int num); |
|
252 static int pk11_rand_status(void); |
|
253 |
|
254 /* These functions are also used in other files */ |
|
255 PK11_SESSION *pk11_get_session(PK11_OPTYPE optype); |
|
256 void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype); |
|
257 |
|
258 /* active list manipulation functions used in this file */ |
|
259 extern int pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type); |
|
260 extern void pk11_free_active_list(PK11_OPTYPE type); |
|
261 |
|
262 #ifndef OPENSSL_NO_RSA |
|
263 int pk11_destroy_rsa_key_objects(PK11_SESSION *session); |
|
264 int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock); |
|
265 int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock); |
|
266 #endif |
|
267 #ifndef OPENSSL_NO_DSA |
|
268 int pk11_destroy_dsa_key_objects(PK11_SESSION *session); |
|
269 int pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock); |
|
270 int pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock); |
|
271 #endif |
|
272 #ifndef OPENSSL_NO_DH |
|
273 int pk11_destroy_dh_key_objects(PK11_SESSION *session); |
|
274 int pk11_destroy_dh_object(PK11_SESSION *session, CK_BBOOL uselock); |
|
275 #endif |
|
276 |
|
277 /* Local helper functions */ |
|
278 static int pk11_free_all_sessions(void); |
|
279 static int pk11_free_session_list(PK11_OPTYPE optype); |
|
280 static int pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype); |
|
281 static int pk11_destroy_cipher_key_objects(PK11_SESSION *session); |
|
282 static int pk11_destroy_object(CK_SESSION_HANDLE handle, CK_OBJECT_HANDLE oh, |
|
283 CK_BBOOL persistent); |
|
284 static const char *get_PK11_LIBNAME(void); |
|
285 static void free_PK11_LIBNAME(void); |
|
286 static long set_PK11_LIBNAME(const char *name); |
|
287 |
|
288 /* Symmetric cipher and digest support functions */ |
|
289 static int cipher_nid_to_pk11(int nid); |
|
290 static int pk11_usable_ciphers(const int **nids); |
|
291 static int pk11_usable_digests(const int **nids); |
|
292 static int pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
|
293 const unsigned char *iv, int enc); |
|
294 static int pk11_cipher_final(PK11_SESSION *sp); |
|
295 static int pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
|
296 const unsigned char *in, size_t inl); |
|
297 static int pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx); |
|
298 static int pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, |
|
299 const int **nids, int nid); |
|
300 static int pk11_engine_digests(ENGINE *e, const EVP_MD **digest, |
|
301 const int **nids, int nid); |
|
302 static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx, |
|
303 const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp); |
|
304 static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key, |
|
305 int key_len); |
|
306 static int md_nid_to_pk11(int nid); |
|
307 static int pk11_digest_init(EVP_MD_CTX *ctx); |
|
308 static int pk11_digest_update(EVP_MD_CTX *ctx, const void *data, |
|
309 size_t count); |
|
310 static int pk11_digest_final(EVP_MD_CTX *ctx, unsigned char *md); |
|
311 static int pk11_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from); |
|
312 static int pk11_digest_cleanup(EVP_MD_CTX *ctx); |
|
313 |
|
314 static int pk11_choose_slots(int *any_slot_found); |
|
315 static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist, |
|
316 CK_SLOT_ID current_slot, int *current_slot_n_cipher, |
|
317 int *local_cipher_nids); |
|
318 static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist, |
|
319 CK_SLOT_ID current_slot, int *current_slot_n_digest, |
|
320 int *local_digest_nids); |
|
321 static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR, int slot_id, |
|
322 int *current_slot_n_cipher, int *local_cipher_nids, |
|
323 PK11_CIPHER *cipher); |
|
324 static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id, |
|
325 int *current_slot_n_digest, int *local_digest_nids, |
|
326 PK11_DIGEST *digest); |
|
327 |
|
328 static int pk11_init_all_locks(void); |
|
329 static void pk11_free_all_locks(void); |
|
330 |
|
331 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
332 static int check_hw_mechanisms(void); |
|
333 static int nid_in_table(int nid, int *nid_table); |
|
334 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
335 |
|
336 #define TRY_OBJ_DESTROY(sp, obj_hdl, retval, uselock, alg_type) \ |
|
337 { \ |
|
338 if (uselock) \ |
|
339 LOCK_OBJSTORE(alg_type); \ |
|
340 if (pk11_active_delete(obj_hdl, alg_type) == 1) \ |
|
341 { \ |
|
342 retval = pk11_destroy_object(sp->session, obj_hdl, \ |
|
343 sp->persistent); \ |
|
344 } \ |
|
345 if (uselock) \ |
|
346 UNLOCK_OBJSTORE(alg_type); \ |
|
347 } |
|
348 |
|
349 static int cipher_nids[PK11_CIPHER_MAX]; |
|
350 static int digest_nids[PK11_DIGEST_MAX]; |
|
351 static int cipher_count = 0; |
|
352 static int digest_count = 0; |
|
353 static CK_BBOOL pk11_have_rsa = CK_FALSE; |
|
354 static CK_BBOOL pk11_have_dsa = CK_FALSE; |
|
355 static CK_BBOOL pk11_have_dh = CK_FALSE; |
|
356 static CK_BBOOL pk11_have_random = CK_FALSE; |
|
357 |
|
358 /* |
|
359 * Static list of ciphers. |
|
360 * Note, that ciphers array is indexed by member PK11_CIPHER.id, |
|
361 * thus ciphers[i].id == i |
|
362 * Rows must be kept in sync with enum pk11_cipher_id. |
|
363 */ |
|
364 static PK11_CIPHER ciphers[] = |
|
365 { |
|
366 { PK11_DES_CBC, NID_des_cbc, 8, 8, 8, |
|
367 CKK_DES, CKM_DES_CBC, }, |
|
368 { PK11_DES3_CBC, NID_des_ede3_cbc, 8, 24, 24, |
|
369 CKK_DES3, CKM_DES3_CBC, }, |
|
370 { PK11_DES_ECB, NID_des_ecb, 0, 8, 8, |
|
371 CKK_DES, CKM_DES_ECB, }, |
|
372 { PK11_DES3_ECB, NID_des_ede3_ecb, 0, 24, 24, |
|
373 CKK_DES3, CKM_DES3_ECB, }, |
|
374 { PK11_RC4, NID_rc4, 0, 16, 256, |
|
375 CKK_RC4, CKM_RC4, }, |
|
376 { PK11_AES_128_CBC, NID_aes_128_cbc, 16, 16, 16, |
|
377 CKK_AES, CKM_AES_CBC, }, |
|
378 { PK11_AES_192_CBC, NID_aes_192_cbc, 16, 24, 24, |
|
379 CKK_AES, CKM_AES_CBC, }, |
|
380 { PK11_AES_256_CBC, NID_aes_256_cbc, 16, 32, 32, |
|
381 CKK_AES, CKM_AES_CBC, }, |
|
382 { PK11_AES_128_ECB, NID_aes_128_ecb, 0, 16, 16, |
|
383 CKK_AES, CKM_AES_ECB, }, |
|
384 { PK11_AES_192_ECB, NID_aes_192_ecb, 0, 24, 24, |
|
385 CKK_AES, CKM_AES_ECB, }, |
|
386 { PK11_AES_256_ECB, NID_aes_256_ecb, 0, 32, 32, |
|
387 CKK_AES, CKM_AES_ECB, }, |
|
388 { PK11_BLOWFISH_CBC, NID_bf_cbc, 8, 16, 16, |
|
389 CKK_BLOWFISH, CKM_BLOWFISH_CBC, }, |
|
390 /* we don't know the correct NIDs until the engine is initialized */ |
|
391 { PK11_AES_128_CTR, NID_aes_128_ctr, 16, 16, 16, |
|
392 CKK_AES, CKM_AES_CTR, }, |
|
393 { PK11_AES_192_CTR, NID_aes_192_ctr, 16, 24, 24, |
|
394 CKK_AES, CKM_AES_CTR, }, |
|
395 { PK11_AES_256_CTR, NID_aes_256_ctr, 16, 32, 32, |
|
396 CKK_AES, CKM_AES_CTR, }, |
|
397 }; |
|
398 |
|
399 /* |
|
400 * Static list of digests. |
|
401 * Note, that digests array is indexed by member PK11_DIGEST.id, |
|
402 * thus digests[i].id == i |
|
403 * Rows must be kept in sync with enum pk11_digest_id. |
|
404 */ |
|
405 static PK11_DIGEST digests[] = |
|
406 { |
|
407 {PK11_MD5, NID_md5, CKM_MD5, }, |
|
408 {PK11_SHA1, NID_sha1, CKM_SHA_1, }, |
|
409 {PK11_SHA224, NID_sha224, CKM_SHA224, }, |
|
410 {PK11_SHA256, NID_sha256, CKM_SHA256, }, |
|
411 {PK11_SHA384, NID_sha384, CKM_SHA384, }, |
|
412 {PK11_SHA512, NID_sha512, CKM_SHA512, }, |
|
413 {0, NID_undef, 0xFFFF, }, |
|
414 }; |
|
415 |
|
416 /* |
|
417 * Structure to be used for the cipher_data/md_data in |
|
418 * EVP_CIPHER_CTX/EVP_MD_CTX structures in order to use the same pk11 |
|
419 * session in multiple cipher_update calls |
|
420 */ |
|
421 typedef struct PK11_CIPHER_STATE_st |
|
422 { |
|
423 PK11_SESSION *sp; |
|
424 } PK11_CIPHER_STATE; |
|
425 |
|
426 |
|
427 /* |
|
428 * libcrypto EVP stuff - this is how we get wired to EVP so the engine gets |
|
429 * called when libcrypto requests a cipher NID. |
|
430 * |
|
431 * Note how the PK11_CIPHER_STATE is used here. |
|
432 */ |
|
433 |
|
434 /* DES CBC EVP */ |
|
435 static const EVP_CIPHER pk11_des_cbc = |
|
436 { |
|
437 NID_des_cbc, |
|
438 8, 8, 8, |
|
439 EVP_CIPH_CBC_MODE, |
|
440 pk11_cipher_init, |
|
441 pk11_cipher_do_cipher, |
|
442 pk11_cipher_cleanup, |
|
443 sizeof (PK11_CIPHER_STATE), |
|
444 EVP_CIPHER_set_asn1_iv, |
|
445 EVP_CIPHER_get_asn1_iv, |
|
446 NULL |
|
447 }; |
|
448 |
|
449 /* 3DES CBC EVP */ |
|
450 static const EVP_CIPHER pk11_3des_cbc = |
|
451 { |
|
452 NID_des_ede3_cbc, |
|
453 8, 24, 8, |
|
454 EVP_CIPH_CBC_MODE, |
|
455 pk11_cipher_init, |
|
456 pk11_cipher_do_cipher, |
|
457 pk11_cipher_cleanup, |
|
458 sizeof (PK11_CIPHER_STATE), |
|
459 EVP_CIPHER_set_asn1_iv, |
|
460 EVP_CIPHER_get_asn1_iv, |
|
461 NULL |
|
462 }; |
|
463 |
|
464 /* |
|
465 * ECB modes don't use an Initial Vector so that's why set_asn1_parameters and |
|
466 * get_asn1_parameters fields are set to NULL. |
|
467 */ |
|
468 static const EVP_CIPHER pk11_des_ecb = |
|
469 { |
|
470 NID_des_ecb, |
|
471 8, 8, 8, |
|
472 EVP_CIPH_ECB_MODE, |
|
473 pk11_cipher_init, |
|
474 pk11_cipher_do_cipher, |
|
475 pk11_cipher_cleanup, |
|
476 sizeof (PK11_CIPHER_STATE), |
|
477 NULL, |
|
478 NULL, |
|
479 NULL |
|
480 }; |
|
481 |
|
482 static const EVP_CIPHER pk11_3des_ecb = |
|
483 { |
|
484 NID_des_ede3_ecb, |
|
485 8, 24, 8, |
|
486 EVP_CIPH_ECB_MODE, |
|
487 pk11_cipher_init, |
|
488 pk11_cipher_do_cipher, |
|
489 pk11_cipher_cleanup, |
|
490 sizeof (PK11_CIPHER_STATE), |
|
491 NULL, |
|
492 NULL, |
|
493 NULL |
|
494 }; |
|
495 |
|
496 |
|
497 static const EVP_CIPHER pk11_aes_128_cbc = |
|
498 { |
|
499 NID_aes_128_cbc, |
|
500 16, 16, 16, |
|
501 EVP_CIPH_CBC_MODE, |
|
502 pk11_cipher_init, |
|
503 pk11_cipher_do_cipher, |
|
504 pk11_cipher_cleanup, |
|
505 sizeof (PK11_CIPHER_STATE), |
|
506 EVP_CIPHER_set_asn1_iv, |
|
507 EVP_CIPHER_get_asn1_iv, |
|
508 NULL |
|
509 }; |
|
510 |
|
511 static const EVP_CIPHER pk11_aes_192_cbc = |
|
512 { |
|
513 NID_aes_192_cbc, |
|
514 16, 24, 16, |
|
515 EVP_CIPH_CBC_MODE, |
|
516 pk11_cipher_init, |
|
517 pk11_cipher_do_cipher, |
|
518 pk11_cipher_cleanup, |
|
519 sizeof (PK11_CIPHER_STATE), |
|
520 EVP_CIPHER_set_asn1_iv, |
|
521 EVP_CIPHER_get_asn1_iv, |
|
522 NULL |
|
523 }; |
|
524 |
|
525 static const EVP_CIPHER pk11_aes_256_cbc = |
|
526 { |
|
527 NID_aes_256_cbc, |
|
528 16, 32, 16, |
|
529 EVP_CIPH_CBC_MODE, |
|
530 pk11_cipher_init, |
|
531 pk11_cipher_do_cipher, |
|
532 pk11_cipher_cleanup, |
|
533 sizeof (PK11_CIPHER_STATE), |
|
534 EVP_CIPHER_set_asn1_iv, |
|
535 EVP_CIPHER_get_asn1_iv, |
|
536 NULL |
|
537 }; |
|
538 |
|
539 /* |
|
540 * ECB modes don't use IV so that's why set_asn1_parameters and |
|
541 * get_asn1_parameters are set to NULL. |
|
542 */ |
|
543 static const EVP_CIPHER pk11_aes_128_ecb = |
|
544 { |
|
545 NID_aes_128_ecb, |
|
546 16, 16, 0, |
|
547 EVP_CIPH_ECB_MODE, |
|
548 pk11_cipher_init, |
|
549 pk11_cipher_do_cipher, |
|
550 pk11_cipher_cleanup, |
|
551 sizeof (PK11_CIPHER_STATE), |
|
552 NULL, |
|
553 NULL, |
|
554 NULL |
|
555 }; |
|
556 |
|
557 static const EVP_CIPHER pk11_aes_192_ecb = |
|
558 { |
|
559 NID_aes_192_ecb, |
|
560 16, 24, 0, |
|
561 EVP_CIPH_ECB_MODE, |
|
562 pk11_cipher_init, |
|
563 pk11_cipher_do_cipher, |
|
564 pk11_cipher_cleanup, |
|
565 sizeof (PK11_CIPHER_STATE), |
|
566 NULL, |
|
567 NULL, |
|
568 NULL |
|
569 }; |
|
570 |
|
571 static const EVP_CIPHER pk11_aes_256_ecb = |
|
572 { |
|
573 NID_aes_256_ecb, |
|
574 16, 32, 0, |
|
575 EVP_CIPH_ECB_MODE, |
|
576 pk11_cipher_init, |
|
577 pk11_cipher_do_cipher, |
|
578 pk11_cipher_cleanup, |
|
579 sizeof (PK11_CIPHER_STATE), |
|
580 NULL, |
|
581 NULL, |
|
582 NULL |
|
583 }; |
|
584 |
|
585 static EVP_CIPHER pk11_aes_128_ctr = |
|
586 { |
|
587 NID_aes_128_ctr, |
|
588 16, 16, 16, |
|
589 EVP_CIPH_CTR_MODE, |
|
590 pk11_cipher_init, |
|
591 pk11_cipher_do_cipher, |
|
592 pk11_cipher_cleanup, |
|
593 sizeof (PK11_CIPHER_STATE), |
|
594 EVP_CIPHER_set_asn1_iv, |
|
595 EVP_CIPHER_get_asn1_iv, |
|
596 NULL |
|
597 }; |
|
598 |
|
599 static EVP_CIPHER pk11_aes_192_ctr = |
|
600 { |
|
601 NID_aes_192_ctr, |
|
602 16, 24, 16, |
|
603 EVP_CIPH_CTR_MODE, |
|
604 pk11_cipher_init, |
|
605 pk11_cipher_do_cipher, |
|
606 pk11_cipher_cleanup, |
|
607 sizeof (PK11_CIPHER_STATE), |
|
608 EVP_CIPHER_set_asn1_iv, |
|
609 EVP_CIPHER_get_asn1_iv, |
|
610 NULL |
|
611 }; |
|
612 |
|
613 static EVP_CIPHER pk11_aes_256_ctr = |
|
614 { |
|
615 NID_aes_256_ctr, |
|
616 16, 32, 16, |
|
617 EVP_CIPH_CTR_MODE, |
|
618 pk11_cipher_init, |
|
619 pk11_cipher_do_cipher, |
|
620 pk11_cipher_cleanup, |
|
621 sizeof (PK11_CIPHER_STATE), |
|
622 EVP_CIPHER_set_asn1_iv, |
|
623 EVP_CIPHER_get_asn1_iv, |
|
624 NULL |
|
625 }; |
|
626 |
|
627 static const EVP_CIPHER pk11_bf_cbc = |
|
628 { |
|
629 NID_bf_cbc, |
|
630 8, 16, 8, |
|
631 EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CBC_MODE, |
|
632 pk11_cipher_init, |
|
633 pk11_cipher_do_cipher, |
|
634 pk11_cipher_cleanup, |
|
635 sizeof (PK11_CIPHER_STATE), |
|
636 EVP_CIPHER_set_asn1_iv, |
|
637 EVP_CIPHER_get_asn1_iv, |
|
638 NULL |
|
639 }; |
|
640 |
|
641 static const EVP_CIPHER pk11_rc4 = |
|
642 { |
|
643 NID_rc4, |
|
644 1, 16, 0, |
|
645 EVP_CIPH_VARIABLE_LENGTH, |
|
646 pk11_cipher_init, |
|
647 pk11_cipher_do_cipher, |
|
648 pk11_cipher_cleanup, |
|
649 sizeof (PK11_CIPHER_STATE), |
|
650 NULL, |
|
651 NULL, |
|
652 NULL |
|
653 }; |
|
654 |
|
655 static const EVP_MD pk11_md5 = |
|
656 { |
|
657 NID_md5, |
|
658 NID_md5WithRSAEncryption, |
|
659 MD5_DIGEST_LENGTH, |
|
660 0, |
|
661 pk11_digest_init, |
|
662 pk11_digest_update, |
|
663 pk11_digest_final, |
|
664 pk11_digest_copy, |
|
665 pk11_digest_cleanup, |
|
666 EVP_PKEY_RSA_method, |
|
667 MD5_CBLOCK, |
|
668 sizeof (PK11_CIPHER_STATE), |
|
669 }; |
|
670 |
|
671 static const EVP_MD pk11_sha1 = |
|
672 { |
|
673 NID_sha1, |
|
674 NID_sha1WithRSAEncryption, |
|
675 SHA_DIGEST_LENGTH, |
|
676 EVP_MD_FLAG_FIPS, |
|
677 pk11_digest_init, |
|
678 pk11_digest_update, |
|
679 pk11_digest_final, |
|
680 pk11_digest_copy, |
|
681 pk11_digest_cleanup, |
|
682 EVP_PKEY_RSA_method, |
|
683 SHA_CBLOCK, |
|
684 sizeof (PK11_CIPHER_STATE), |
|
685 }; |
|
686 |
|
687 static const EVP_MD pk11_sha224 = |
|
688 { |
|
689 NID_sha224, |
|
690 NID_sha224WithRSAEncryption, |
|
691 SHA224_DIGEST_LENGTH, |
|
692 EVP_MD_FLAG_FIPS, |
|
693 pk11_digest_init, |
|
694 pk11_digest_update, |
|
695 pk11_digest_final, |
|
696 pk11_digest_copy, |
|
697 pk11_digest_cleanup, |
|
698 EVP_PKEY_RSA_method, |
|
699 /* SHA-224 uses the same cblock size as SHA-256 */ |
|
700 SHA256_CBLOCK, |
|
701 sizeof (PK11_CIPHER_STATE), |
|
702 }; |
|
703 |
|
704 static const EVP_MD pk11_sha256 = |
|
705 { |
|
706 NID_sha256, |
|
707 NID_sha256WithRSAEncryption, |
|
708 SHA256_DIGEST_LENGTH, |
|
709 EVP_MD_FLAG_FIPS, |
|
710 pk11_digest_init, |
|
711 pk11_digest_update, |
|
712 pk11_digest_final, |
|
713 pk11_digest_copy, |
|
714 pk11_digest_cleanup, |
|
715 EVP_PKEY_RSA_method, |
|
716 SHA256_CBLOCK, |
|
717 sizeof (PK11_CIPHER_STATE), |
|
718 }; |
|
719 |
|
720 static const EVP_MD pk11_sha384 = |
|
721 { |
|
722 NID_sha384, |
|
723 NID_sha384WithRSAEncryption, |
|
724 SHA384_DIGEST_LENGTH, |
|
725 EVP_MD_FLAG_FIPS, |
|
726 pk11_digest_init, |
|
727 pk11_digest_update, |
|
728 pk11_digest_final, |
|
729 pk11_digest_copy, |
|
730 pk11_digest_cleanup, |
|
731 EVP_PKEY_RSA_method, |
|
732 /* SHA-384 uses the same cblock size as SHA-512 */ |
|
733 SHA512_CBLOCK, |
|
734 sizeof (PK11_CIPHER_STATE), |
|
735 }; |
|
736 |
|
737 static const EVP_MD pk11_sha512 = |
|
738 { |
|
739 NID_sha512, |
|
740 NID_sha512WithRSAEncryption, |
|
741 SHA512_DIGEST_LENGTH, |
|
742 EVP_MD_FLAG_FIPS, |
|
743 pk11_digest_init, |
|
744 pk11_digest_update, |
|
745 pk11_digest_final, |
|
746 pk11_digest_copy, |
|
747 pk11_digest_cleanup, |
|
748 EVP_PKEY_RSA_method, |
|
749 SHA512_CBLOCK, |
|
750 sizeof (PK11_CIPHER_STATE), |
|
751 }; |
|
752 |
|
753 /* |
|
754 * Initialization function. Sets up various PKCS#11 library components. |
|
755 * The definitions for control commands specific to this engine |
|
756 */ |
|
757 #define PK11_CMD_SO_PATH ENGINE_CMD_BASE |
|
758 static const ENGINE_CMD_DEFN pk11_cmd_defns[] = |
|
759 { |
|
760 { |
|
761 PK11_CMD_SO_PATH, |
|
762 "SO_PATH", |
|
763 "Specifies the path to the 'pkcs#11' shared library", |
|
764 ENGINE_CMD_FLAG_STRING |
|
765 }, |
|
766 {0, NULL, NULL, 0} |
|
767 }; |
|
768 |
|
769 |
|
770 static RAND_METHOD pk11_random = |
|
771 { |
|
772 pk11_rand_seed, |
|
773 pk11_rand_bytes, |
|
774 pk11_rand_cleanup, |
|
775 pk11_rand_add, |
|
776 pk11_rand_bytes, |
|
777 pk11_rand_status |
|
778 }; |
|
779 |
|
780 |
|
781 /* Constants used when creating the ENGINE */ |
|
782 static const char *engine_pk11_id = "pkcs11"; |
|
783 static const char *engine_pk11_name = "PKCS #11 engine support"; |
|
784 |
|
785 CK_FUNCTION_LIST_PTR pFuncList = NULL; |
|
786 static const char PK11_GET_FUNCTION_LIST[] = "C_GetFunctionList"; |
|
787 |
|
788 /* |
|
789 * This is a static string constant for the DSO file name and the function |
|
790 * symbol names to bind to. We set it in the Configure script based on whether |
|
791 * this is 32 or 64 bit build. |
|
792 */ |
|
793 static const char def_PK11_LIBNAME[] = PK11_LIB_LOCATION; |
|
794 |
|
795 /* Needed in e_pk11_pub.c as well so that's why it is not static. */ |
|
796 CK_SLOT_ID pubkey_SLOTID = 0; |
|
797 static CK_SLOT_ID rand_SLOTID = 0; |
|
798 static CK_SLOT_ID SLOTID = 0; |
|
799 static CK_BBOOL pk11_library_initialized = CK_FALSE; |
|
800 static CK_BBOOL pk11_atfork_initialized = CK_FALSE; |
|
801 static int pk11_pid = 0; |
|
802 static ENGINE* pk11_engine = NULL; |
|
803 |
|
804 static DSO *pk11_dso = NULL; |
|
805 |
|
806 /* allocate and initialize all locks used by the engine itself */ |
|
807 static int pk11_init_all_locks(void) |
|
808 { |
|
809 int type; |
|
810 |
|
811 #ifndef OPENSSL_NO_RSA |
|
812 find_lock[OP_RSA] = OPENSSL_malloc(sizeof (pthread_mutex_t)); |
|
813 if (find_lock[OP_RSA] == NULL) |
|
814 goto malloc_err; |
|
815 (void) pthread_mutex_init(find_lock[OP_RSA], NULL); |
|
816 #endif /* OPENSSL_NO_RSA */ |
|
817 |
|
818 if ((uri_lock = OPENSSL_malloc(sizeof (pthread_mutex_t))) == NULL) |
|
819 goto malloc_err; |
|
820 (void) pthread_mutex_init(uri_lock, NULL); |
|
821 |
|
822 #ifndef OPENSSL_NO_DSA |
|
823 find_lock[OP_DSA] = OPENSSL_malloc(sizeof (pthread_mutex_t)); |
|
824 if (find_lock[OP_DSA] == NULL) |
|
825 goto malloc_err; |
|
826 (void) pthread_mutex_init(find_lock[OP_DSA], NULL); |
|
827 #endif /* OPENSSL_NO_DSA */ |
|
828 |
|
829 #ifndef OPENSSL_NO_DH |
|
830 find_lock[OP_DH] = OPENSSL_malloc(sizeof (pthread_mutex_t)); |
|
831 if (find_lock[OP_DH] == NULL) |
|
832 goto malloc_err; |
|
833 (void) pthread_mutex_init(find_lock[OP_DH], NULL); |
|
834 #endif /* OPENSSL_NO_DH */ |
|
835 |
|
836 for (type = 0; type < OP_MAX; type++) |
|
837 { |
|
838 session_cache[type].lock = |
|
839 OPENSSL_malloc(sizeof (pthread_mutex_t)); |
|
840 if (session_cache[type].lock == NULL) |
|
841 goto malloc_err; |
|
842 (void) pthread_mutex_init(session_cache[type].lock, NULL); |
|
843 } |
|
844 |
|
845 return (1); |
|
846 |
|
847 malloc_err: |
|
848 pk11_free_all_locks(); |
|
849 PK11err(PK11_F_INIT_ALL_LOCKS, PK11_R_MALLOC_FAILURE); |
|
850 return (0); |
|
851 } |
|
852 |
|
853 static void pk11_free_all_locks(void) |
|
854 { |
|
855 int type; |
|
856 |
|
857 #ifndef OPENSSL_NO_RSA |
|
858 if (find_lock[OP_RSA] != NULL) |
|
859 { |
|
860 (void) pthread_mutex_destroy(find_lock[OP_RSA]); |
|
861 OPENSSL_free(find_lock[OP_RSA]); |
|
862 find_lock[OP_RSA] = NULL; |
|
863 } |
|
864 #endif /* OPENSSL_NO_RSA */ |
|
865 #ifndef OPENSSL_NO_DSA |
|
866 if (find_lock[OP_DSA] != NULL) |
|
867 { |
|
868 (void) pthread_mutex_destroy(find_lock[OP_DSA]); |
|
869 OPENSSL_free(find_lock[OP_DSA]); |
|
870 find_lock[OP_DSA] = NULL; |
|
871 } |
|
872 #endif /* OPENSSL_NO_DSA */ |
|
873 #ifndef OPENSSL_NO_DH |
|
874 if (find_lock[OP_DH] != NULL) |
|
875 { |
|
876 (void) pthread_mutex_destroy(find_lock[OP_DH]); |
|
877 OPENSSL_free(find_lock[OP_DH]); |
|
878 find_lock[OP_DH] = NULL; |
|
879 } |
|
880 #endif /* OPENSSL_NO_DH */ |
|
881 |
|
882 for (type = 0; type < OP_MAX; type++) |
|
883 { |
|
884 if (session_cache[type].lock != NULL) |
|
885 { |
|
886 (void) pthread_mutex_destroy(session_cache[type].lock); |
|
887 OPENSSL_free(session_cache[type].lock); |
|
888 session_cache[type].lock = NULL; |
|
889 } |
|
890 } |
|
891 /* Free uri_lock */ |
|
892 (void) pthread_mutex_destroy(uri_lock); |
|
893 OPENSSL_free(uri_lock); |
|
894 uri_lock = NULL; |
|
895 } |
|
896 |
|
897 /* |
|
898 * This internal function is used by ENGINE_pk11() and "dynamic" ENGINE support. |
|
899 */ |
|
900 static int bind_pk11(ENGINE *e) |
|
901 { |
|
902 #ifndef OPENSSL_NO_RSA |
|
903 const RSA_METHOD *rsa = NULL; |
|
904 RSA_METHOD *pk11_rsa = PK11_RSA(); |
|
905 #endif /* OPENSSL_NO_RSA */ |
|
906 if (!pk11_library_initialized) |
|
907 if (!pk11_library_init(e)) |
|
908 return (0); |
|
909 |
|
910 if (!ENGINE_set_id(e, engine_pk11_id) || |
|
911 !ENGINE_set_name(e, engine_pk11_name) || |
|
912 !ENGINE_set_ciphers(e, pk11_engine_ciphers) || |
|
913 !ENGINE_set_digests(e, pk11_engine_digests)) |
|
914 return (0); |
|
915 |
|
916 if (!ENGINE_set_pkey_meths(e, pk11_engine_pkey_methods)) |
|
917 return (0); |
|
918 |
|
919 #ifndef OPENSSL_NO_RSA |
|
920 if (pk11_have_rsa == CK_TRUE) |
|
921 { |
|
922 if (!ENGINE_set_RSA(e, PK11_RSA()) || |
|
923 !ENGINE_set_load_privkey_function(e, pk11_load_privkey) || |
|
924 !ENGINE_set_load_pubkey_function(e, pk11_load_pubkey)) |
|
925 return (0); |
|
926 DEBUG_SLOT_SEL("%s: registered RSA\n", PK11_DBG); |
|
927 } |
|
928 #endif /* OPENSSL_NO_RSA */ |
|
929 #ifndef OPENSSL_NO_DSA |
|
930 if (pk11_have_dsa == CK_TRUE) |
|
931 { |
|
932 if (!ENGINE_set_DSA(e, PK11_DSA())) |
|
933 return (0); |
|
934 DEBUG_SLOT_SEL("%s: registered DSA\n", PK11_DBG); |
|
935 } |
|
936 #endif /* OPENSSL_NO_DSA */ |
|
937 #ifndef OPENSSL_NO_DH |
|
938 if (pk11_have_dh == CK_TRUE) |
|
939 { |
|
940 if (!ENGINE_set_DH(e, PK11_DH())) |
|
941 return (0); |
|
942 DEBUG_SLOT_SEL("%s: registered DH\n", PK11_DBG); |
|
943 } |
|
944 #endif /* OPENSSL_NO_DH */ |
|
945 if (pk11_have_random) |
|
946 { |
|
947 if (!ENGINE_set_RAND(e, &pk11_random)) |
|
948 return (0); |
|
949 DEBUG_SLOT_SEL("%s: registered random\n", PK11_DBG); |
|
950 } |
|
951 if (!ENGINE_set_init_function(e, pk11_init) || |
|
952 !ENGINE_set_destroy_function(e, pk11_destroy) || |
|
953 !ENGINE_set_finish_function(e, pk11_finish) || |
|
954 !ENGINE_set_ctrl_function(e, pk11_ctrl) || |
|
955 !ENGINE_set_cmd_defns(e, pk11_cmd_defns)) |
|
956 return (0); |
|
957 |
|
958 /* |
|
959 * Apache calls OpenSSL function RSA_blinding_on() once during startup |
|
960 * which in turn calls bn_mod_exp. Since we do not implement bn_mod_exp |
|
961 * here, we wire it back to the OpenSSL software implementation. |
|
962 * Since it is used only once, performance is not a concern. |
|
963 */ |
|
964 #ifndef OPENSSL_NO_RSA |
|
965 rsa = RSA_PKCS1_SSLeay(); |
|
966 pk11_rsa->rsa_mod_exp = rsa->rsa_mod_exp; |
|
967 pk11_rsa->bn_mod_exp = rsa->bn_mod_exp; |
|
968 #endif /* OPENSSL_NO_RSA */ |
|
969 |
|
970 /* Ensure the pk11 error handling is set up */ |
|
971 ERR_load_pk11_strings(); |
|
972 |
|
973 return (1); |
|
974 } |
|
975 |
|
976 static int bind_helper(ENGINE *e, const char *id) |
|
977 { |
|
978 if (id && (strcmp(id, engine_pk11_id) != 0)) |
|
979 return (0); |
|
980 |
|
981 if (!bind_pk11(e)) |
|
982 return (0); |
|
983 |
|
984 return (1); |
|
985 } |
|
986 |
|
987 IMPLEMENT_DYNAMIC_CHECK_FN() |
|
988 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) |
|
989 |
|
990 /* |
|
991 * These are the static string constants for the DSO file name and |
|
992 * the function symbol names to bind to. |
|
993 */ |
|
994 static const char *PK11_LIBNAME = NULL; |
|
995 |
|
996 static const char *get_PK11_LIBNAME(void) |
|
997 { |
|
998 if (PK11_LIBNAME) |
|
999 return (PK11_LIBNAME); |
|
1000 |
|
1001 return (def_PK11_LIBNAME); |
|
1002 } |
|
1003 |
|
1004 static void free_PK11_LIBNAME(void) |
|
1005 { |
|
1006 if (PK11_LIBNAME) |
|
1007 OPENSSL_free((void*)PK11_LIBNAME); |
|
1008 |
|
1009 PK11_LIBNAME = NULL; |
|
1010 } |
|
1011 |
|
1012 static long set_PK11_LIBNAME(const char *name) |
|
1013 { |
|
1014 free_PK11_LIBNAME(); |
|
1015 |
|
1016 return ((PK11_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0); |
|
1017 } |
|
1018 |
|
1019 /* acquire all engine specific mutexes before fork */ |
|
1020 static void pk11_fork_prepare(void) |
|
1021 { |
|
1022 int i; |
|
1023 |
|
1024 if (!pk11_library_initialized) |
|
1025 return; |
|
1026 |
|
1027 LOCK_OBJSTORE(OP_RSA); |
|
1028 LOCK_OBJSTORE(OP_DSA); |
|
1029 LOCK_OBJSTORE(OP_DH); |
|
1030 (void) pthread_mutex_lock(uri_lock); |
|
1031 for (i = 0; i < OP_MAX; i++) |
|
1032 { |
|
1033 (void) pthread_mutex_lock(session_cache[i].lock); |
|
1034 } |
|
1035 } |
|
1036 |
|
1037 /* release all engine specific mutexes */ |
|
1038 static void pk11_fork_parent(void) |
|
1039 { |
|
1040 int i; |
|
1041 |
|
1042 if (!pk11_library_initialized) |
|
1043 return; |
|
1044 |
|
1045 for (i = OP_MAX - 1; i >= 0; i--) |
|
1046 { |
|
1047 (void) pthread_mutex_unlock(session_cache[i].lock); |
|
1048 } |
|
1049 UNLOCK_OBJSTORE(OP_DH); |
|
1050 UNLOCK_OBJSTORE(OP_DSA); |
|
1051 UNLOCK_OBJSTORE(OP_RSA); |
|
1052 (void) pthread_mutex_unlock(uri_lock); |
|
1053 } |
|
1054 |
|
1055 /* |
|
1056 * same situation as in parent - we need to unlock all locks to make them |
|
1057 * accessible to all threads. |
|
1058 */ |
|
1059 static void pk11_fork_child(void) |
|
1060 { |
|
1061 int i; |
|
1062 |
|
1063 if (!pk11_library_initialized) |
|
1064 return; |
|
1065 |
|
1066 for (i = OP_MAX - 1; i >= 0; i--) |
|
1067 { |
|
1068 (void) pthread_mutex_unlock(session_cache[i].lock); |
|
1069 } |
|
1070 UNLOCK_OBJSTORE(OP_DH); |
|
1071 UNLOCK_OBJSTORE(OP_DSA); |
|
1072 UNLOCK_OBJSTORE(OP_RSA); |
|
1073 (void) pthread_mutex_unlock(uri_lock); |
|
1074 } |
|
1075 |
|
1076 /* Initialization function for the pk11 engine */ |
|
1077 static int pk11_init(ENGINE *e) |
|
1078 { |
|
1079 return (pk11_library_init(e)); |
|
1080 } |
|
1081 |
|
1082 |
|
1083 /* |
|
1084 * Helper function that unsets reference to current engine (pk11_engine = NULL). |
|
1085 * |
|
1086 * Use of local variable only seems clumsy, it needs to be this way! |
|
1087 * This is to prevent double free in the unlucky scenario: |
|
1088 * ENGINE_free calls pk11_destroy calls pk11_finish calls ENGINE_free |
|
1089 * Setting pk11_engine to NULL prior to ENGINE_free() avoids this. |
|
1090 */ |
|
1091 static void pk11_engine_free() |
|
1092 { |
|
1093 ENGINE* old_engine = pk11_engine; |
|
1094 |
|
1095 if (old_engine) |
|
1096 { |
|
1097 pk11_engine = NULL; |
|
1098 } |
|
1099 } |
|
1100 |
|
1101 |
|
1102 /* |
|
1103 * Initialization function. Sets up various PKCS#11 library components. |
|
1104 * It selects a slot based on predefined critiera. In the process, it also |
|
1105 * count how many ciphers and digests to support. Since the cipher and |
|
1106 * digest information is needed when setting default engine, this function |
|
1107 * needs to be called before calling ENGINE_set_default. |
|
1108 */ |
|
1109 /* ARGSUSED */ |
|
1110 static int pk11_library_init(ENGINE *e) |
|
1111 { |
|
1112 CK_C_GetFunctionList p; |
|
1113 CK_RV rv = CKR_OK; |
|
1114 CK_INFO info; |
|
1115 CK_ULONG ul_state_len; |
|
1116 int any_slot_found; |
|
1117 int i; |
|
1118 |
|
1119 if (e != pk11_engine) |
|
1120 { |
|
1121 pk11_engine_free(); |
|
1122 pk11_engine = e; |
|
1123 } |
|
1124 |
|
1125 /* |
|
1126 * pk11_library_initialized is set to 0 in pk11_finish() which is called |
|
1127 * from ENGINE_finish(). However, if there is still at least one |
|
1128 * existing functional reference to the engine (see engine(3) for more |
|
1129 * information), pk11_finish() is skipped. For example, this can happen |
|
1130 * if an application forgets to clear one cipher context. In case of a |
|
1131 * fork() when the application is finishing the engine so that it can be |
|
1132 * reinitialized in the child, forgotten functional reference causes |
|
1133 * pk11_library_initialized to stay 1. In that case we need the PID |
|
1134 * check so that we properly initialize the engine again. |
|
1135 */ |
|
1136 if (pk11_library_initialized) |
|
1137 { |
|
1138 if (pk11_pid == getpid()) |
|
1139 { |
|
1140 return (1); |
|
1141 } |
|
1142 else |
|
1143 { |
|
1144 global_session = CK_INVALID_HANDLE; |
|
1145 /* |
|
1146 * free the locks first to prevent memory leak in case |
|
1147 * the application calls fork() without finishing the |
|
1148 * engine first. |
|
1149 */ |
|
1150 pk11_free_all_locks(); |
|
1151 } |
|
1152 } |
|
1153 |
|
1154 /* |
|
1155 * If initialization of the locks fails pk11_init_all_locks() |
|
1156 * will do the cleanup. |
|
1157 */ |
|
1158 if (!pk11_init_all_locks()) |
|
1159 goto err; |
|
1160 for (i = 0; i < OP_MAX; i++) |
|
1161 session_cache[i].head = NULL; |
|
1162 /* |
|
1163 * Initialize active lists. We only use active lists |
|
1164 * for asymmetric ciphers. |
|
1165 */ |
|
1166 for (i = 0; i < OP_MAX; i++) |
|
1167 active_list[i] = NULL; |
|
1168 |
|
1169 /* Attempt to load PKCS#11 library. */ |
|
1170 if (!pk11_dso) |
|
1171 { |
|
1172 pk11_dso = DSO_load(NULL, get_PK11_LIBNAME(), NULL, 0); |
|
1173 if (pk11_dso == NULL) |
|
1174 { |
|
1175 PK11err(PK11_F_LOAD, PK11_R_DSO_FAILURE); |
|
1176 goto err; |
|
1177 } |
|
1178 } |
|
1179 |
|
1180 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
1181 if (check_hw_mechanisms() == 0) |
|
1182 goto err; |
|
1183 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
1184 |
|
1185 /* get the C_GetFunctionList function from the loaded library */ |
|
1186 p = (CK_C_GetFunctionList)DSO_bind_func(pk11_dso, |
|
1187 PK11_GET_FUNCTION_LIST); |
|
1188 if (!p) |
|
1189 { |
|
1190 PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE); |
|
1191 goto err; |
|
1192 } |
|
1193 |
|
1194 /* get the full function list from the loaded library */ |
|
1195 rv = p(&pFuncList); |
|
1196 if (rv != CKR_OK) |
|
1197 { |
|
1198 PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE, rv); |
|
1199 goto err; |
|
1200 } |
|
1201 |
|
1202 rv = pFuncList->C_Initialize(NULL_PTR); |
|
1203 if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) |
|
1204 { |
|
1205 PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_INITIALIZE, rv); |
|
1206 goto err; |
|
1207 } |
|
1208 |
|
1209 rv = pFuncList->C_GetInfo(&info); |
|
1210 if (rv != CKR_OK) |
|
1211 { |
|
1212 PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_GETINFO, rv); |
|
1213 goto err; |
|
1214 } |
|
1215 |
|
1216 if (pk11_choose_slots(&any_slot_found) == 0) |
|
1217 goto err; |
|
1218 |
|
1219 /* |
|
1220 * The library we use, set in def_PK11_LIBNAME, may not offer any |
|
1221 * slot(s). In that case, we must not proceed but we must not return an |
|
1222 * error. The reason is that applications that try to set up the PKCS#11 |
|
1223 * engine don't exit on error during the engine initialization just |
|
1224 * because no slot was present. |
|
1225 */ |
|
1226 if (any_slot_found == 0) |
|
1227 return (1); |
|
1228 |
|
1229 if (global_session == CK_INVALID_HANDLE) |
|
1230 { |
|
1231 /* Open the global_session for the new process */ |
|
1232 rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, |
|
1233 NULL_PTR, NULL_PTR, &global_session); |
|
1234 if (rv != CKR_OK) |
|
1235 { |
|
1236 PK11err_add_data(PK11_F_LIBRARY_INIT, |
|
1237 PK11_R_OPENSESSION, rv); |
|
1238 goto err; |
|
1239 } |
|
1240 } |
|
1241 |
|
1242 /* |
|
1243 * Disable digest if C_GetOperationState is not supported since |
|
1244 * this function is required by OpenSSL digest copy function |
|
1245 */ |
|
1246 if (pFuncList->C_GetOperationState(global_session, NULL, &ul_state_len) |
|
1247 == CKR_FUNCTION_NOT_SUPPORTED) { |
|
1248 DEBUG_SLOT_SEL("%s: C_GetOperationState() not supported, " |
|
1249 "setting digest_count to 0\n", PK11_DBG); |
|
1250 digest_count = 0; |
|
1251 } |
|
1252 |
|
1253 pk11_library_initialized = CK_TRUE; |
|
1254 pk11_pid = getpid(); |
|
1255 |
|
1256 if (!pk11_atfork_initialized) |
|
1257 { |
|
1258 if (pthread_atfork(pk11_fork_prepare, pk11_fork_parent, |
|
1259 pk11_fork_child) != 0) |
|
1260 { |
|
1261 PK11err(PK11_F_LIBRARY_INIT, PK11_R_ATFORK_FAILED); |
|
1262 goto err; |
|
1263 } |
|
1264 pk11_atfork_initialized = CK_TRUE; |
|
1265 } |
|
1266 |
|
1267 return (1); |
|
1268 |
|
1269 err: |
|
1270 return (0); |
|
1271 } |
|
1272 |
|
1273 /* Destructor (complements the "ENGINE_pk11()" constructor) */ |
|
1274 /* ARGSUSED */ |
|
1275 static int pk11_destroy(ENGINE *e) |
|
1276 { |
|
1277 int rtn = 1; |
|
1278 |
|
1279 free_PK11_LIBNAME(); |
|
1280 ERR_unload_pk11_strings(); |
|
1281 |
|
1282 if (pk11_library_initialized == CK_TRUE) |
|
1283 rtn = pk11_finish(e); |
|
1284 |
|
1285 return (rtn); |
|
1286 } |
|
1287 |
|
1288 /* |
|
1289 * Termination function to clean up the session, the token, and the pk11 |
|
1290 * library. |
|
1291 */ |
|
1292 /* ARGSUSED */ |
|
1293 static int pk11_finish(ENGINE *e) |
|
1294 { |
|
1295 int i; |
|
1296 |
|
1297 /* |
|
1298 * Make sure, right engine instance is being destroyed. |
|
1299 * Engine e may be the wrong instance if |
|
1300 * 1) either someone calls ENGINE_load_pk11 twice |
|
1301 * 2) or last ref. to an already finished engine is being destroyed |
|
1302 */ |
|
1303 if (e != pk11_engine) |
|
1304 goto err; |
|
1305 |
|
1306 if (pk11_dso == NULL) |
|
1307 { |
|
1308 PK11err(PK11_F_FINISH, PK11_R_NOT_LOADED); |
|
1309 goto err; |
|
1310 } |
|
1311 |
|
1312 OPENSSL_assert(pFuncList != NULL); |
|
1313 |
|
1314 if (pk11_free_all_sessions() == 0) |
|
1315 goto err; |
|
1316 |
|
1317 /* free all active lists */ |
|
1318 for (i = 0; i < OP_MAX; i++) |
|
1319 pk11_free_active_list(i); |
|
1320 |
|
1321 /* Global session is not present when there are no slots. */ |
|
1322 if (global_session != CK_INVALID_HANDLE) |
|
1323 { |
|
1324 pFuncList->C_CloseSession(global_session); |
|
1325 global_session = CK_INVALID_HANDLE; |
|
1326 } |
|
1327 |
|
1328 /* |
|
1329 * Since we are part of a library (libcrypto.so), calling this function |
|
1330 * may have side-effects. |
|
1331 */ |
|
1332 #if 0 |
|
1333 pFuncList->C_Finalize(NULL); |
|
1334 #endif |
|
1335 |
|
1336 if (!DSO_free(pk11_dso)) |
|
1337 { |
|
1338 PK11err(PK11_F_FINISH, PK11_R_DSO_FAILURE); |
|
1339 goto err; |
|
1340 } |
|
1341 pk11_dso = NULL; |
|
1342 pFuncList = NULL; |
|
1343 pk11_library_initialized = CK_FALSE; |
|
1344 pk11_pid = 0; |
|
1345 pk11_engine_free(); |
|
1346 /* |
|
1347 * There is no way how to unregister atfork handlers (other than |
|
1348 * unloading the library) so we just free the locks. For this reason |
|
1349 * the atfork handlers check if the engine is initialized and bail out |
|
1350 * immediately if not. This is necessary in case a process finishes |
|
1351 * the engine before calling fork(). |
|
1352 */ |
|
1353 pk11_free_all_locks(); |
|
1354 |
|
1355 return (1); |
|
1356 |
|
1357 err: |
|
1358 return (0); |
|
1359 } |
|
1360 |
|
1361 /* Standard engine interface function to set the dynamic library path */ |
|
1362 /* ARGSUSED */ |
|
1363 static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()) |
|
1364 { |
|
1365 int initialized = ((pk11_dso == NULL) ? 0 : 1); |
|
1366 |
|
1367 switch (cmd) |
|
1368 { |
|
1369 case PK11_CMD_SO_PATH: |
|
1370 if (p == NULL) |
|
1371 { |
|
1372 PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER); |
|
1373 return (0); |
|
1374 } |
|
1375 |
|
1376 if (initialized) |
|
1377 { |
|
1378 PK11err(PK11_F_CTRL, PK11_R_ALREADY_LOADED); |
|
1379 return (0); |
|
1380 } |
|
1381 |
|
1382 return (set_PK11_LIBNAME((const char *)p)); |
|
1383 default: |
|
1384 break; |
|
1385 } |
|
1386 |
|
1387 PK11err(PK11_F_CTRL, PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED); |
|
1388 |
|
1389 return (0); |
|
1390 } |
|
1391 |
|
1392 |
|
1393 /* Required function by the engine random interface. It does nothing here */ |
|
1394 static void pk11_rand_cleanup(void) |
|
1395 { |
|
1396 return; |
|
1397 } |
|
1398 |
|
1399 /* ARGSUSED */ |
|
1400 static void pk11_rand_add(const void *buf, int num, double add) |
|
1401 { |
|
1402 PK11_SESSION *sp; |
|
1403 |
|
1404 if ((sp = pk11_get_session(OP_RAND)) == NULL) |
|
1405 return; |
|
1406 |
|
1407 /* |
|
1408 * Ignore any errors (e.g. CKR_RANDOM_SEED_NOT_SUPPORTED) since |
|
1409 * the calling functions do not care anyway |
|
1410 */ |
|
1411 pFuncList->C_SeedRandom(sp->session, (unsigned char *) buf, num); |
|
1412 pk11_return_session(sp, OP_RAND); |
|
1413 |
|
1414 return; |
|
1415 } |
|
1416 |
|
1417 static void pk11_rand_seed(const void *buf, int num) |
|
1418 { |
|
1419 pk11_rand_add(buf, num, 0); |
|
1420 } |
|
1421 |
|
1422 static int pk11_rand_bytes(unsigned char *buf, int num) |
|
1423 { |
|
1424 CK_RV rv; |
|
1425 PK11_SESSION *sp; |
|
1426 |
|
1427 if ((sp = pk11_get_session(OP_RAND)) == NULL) |
|
1428 return (0); |
|
1429 |
|
1430 rv = pFuncList->C_GenerateRandom(sp->session, buf, num); |
|
1431 if (rv != CKR_OK) |
|
1432 { |
|
1433 PK11err_add_data(PK11_F_RAND_BYTES, PK11_R_GENERATERANDOM, rv); |
|
1434 pk11_return_session(sp, OP_RAND); |
|
1435 return (0); |
|
1436 } |
|
1437 |
|
1438 pk11_return_session(sp, OP_RAND); |
|
1439 return (1); |
|
1440 } |
|
1441 |
|
1442 /* Required function by the engine random interface. It does nothing here */ |
|
1443 static int pk11_rand_status(void) |
|
1444 { |
|
1445 return (1); |
|
1446 } |
|
1447 |
|
1448 /* Free all BIGNUM structures from PK11_SESSION. */ |
|
1449 static void pk11_free_nums(PK11_SESSION *sp, PK11_OPTYPE optype) |
|
1450 { |
|
1451 switch (optype) |
|
1452 { |
|
1453 #ifndef OPENSSL_NO_RSA |
|
1454 case OP_RSA: |
|
1455 if (sp->opdata_rsa_n_num != NULL) |
|
1456 { |
|
1457 BN_free(sp->opdata_rsa_n_num); |
|
1458 sp->opdata_rsa_n_num = NULL; |
|
1459 } |
|
1460 if (sp->opdata_rsa_e_num != NULL) |
|
1461 { |
|
1462 BN_free(sp->opdata_rsa_e_num); |
|
1463 sp->opdata_rsa_e_num = NULL; |
|
1464 } |
|
1465 if (sp->opdata_rsa_d_num != NULL) |
|
1466 { |
|
1467 BN_free(sp->opdata_rsa_d_num); |
|
1468 sp->opdata_rsa_d_num = NULL; |
|
1469 } |
|
1470 break; |
|
1471 #endif |
|
1472 #ifndef OPENSSL_NO_DSA |
|
1473 case OP_DSA: |
|
1474 if (sp->opdata_dsa_pub_num != NULL) |
|
1475 { |
|
1476 BN_free(sp->opdata_dsa_pub_num); |
|
1477 sp->opdata_dsa_pub_num = NULL; |
|
1478 } |
|
1479 if (sp->opdata_dsa_priv_num != NULL) |
|
1480 { |
|
1481 BN_free(sp->opdata_dsa_priv_num); |
|
1482 sp->opdata_dsa_priv_num = NULL; |
|
1483 } |
|
1484 break; |
|
1485 #endif |
|
1486 #ifndef OPENSSL_NO_DH |
|
1487 case OP_DH: |
|
1488 if (sp->opdata_dh_priv_num != NULL) |
|
1489 { |
|
1490 BN_free(sp->opdata_dh_priv_num); |
|
1491 sp->opdata_dh_priv_num = NULL; |
|
1492 } |
|
1493 break; |
|
1494 #endif |
|
1495 default: |
|
1496 break; |
|
1497 } |
|
1498 } |
|
1499 |
|
1500 /* |
|
1501 * Get new PK11_SESSION structure ready for use. Every process must have |
|
1502 * its own freelist of PK11_SESSION structures so handle fork() here |
|
1503 * by destroying the old and creating new freelist. |
|
1504 * The returned PK11_SESSION structure is disconnected from the freelist. |
|
1505 */ |
|
1506 PK11_SESSION * |
|
1507 pk11_get_session(PK11_OPTYPE optype) |
|
1508 { |
|
1509 PK11_SESSION *sp = NULL, *sp1, *freelist; |
|
1510 pthread_mutex_t *freelist_lock; |
|
1511 static pid_t pid = 0; |
|
1512 pid_t new_pid; |
|
1513 CK_RV rv; |
|
1514 |
|
1515 switch (optype) |
|
1516 { |
|
1517 case OP_RSA: |
|
1518 case OP_DSA: |
|
1519 case OP_DH: |
|
1520 case OP_RAND: |
|
1521 case OP_DIGEST: |
|
1522 case OP_CIPHER: |
|
1523 freelist_lock = session_cache[optype].lock; |
|
1524 break; |
|
1525 default: |
|
1526 PK11err(PK11_F_GET_SESSION, |
|
1527 PK11_R_INVALID_OPERATION_TYPE); |
|
1528 return (NULL); |
|
1529 } |
|
1530 (void) pthread_mutex_lock(freelist_lock); |
|
1531 |
|
1532 /* |
|
1533 * Will use it to find out if we forked. We cannot use the PID field in |
|
1534 * the session structure because we could get a newly allocated session |
|
1535 * here, with no PID information. |
|
1536 */ |
|
1537 if (pid == 0) |
|
1538 pid = getpid(); |
|
1539 |
|
1540 freelist = session_cache[optype].head; |
|
1541 sp = freelist; |
|
1542 |
|
1543 /* |
|
1544 * If the free list is empty, allocate new uninitialized (filled |
|
1545 * with zeroes) PK11_SESSION structure otherwise return first |
|
1546 * structure from the freelist. |
|
1547 */ |
|
1548 if (sp == NULL) |
|
1549 { |
|
1550 if ((sp = OPENSSL_malloc(sizeof (PK11_SESSION))) == NULL) |
|
1551 { |
|
1552 PK11err(PK11_F_GET_SESSION, |
|
1553 PK11_R_MALLOC_FAILURE); |
|
1554 goto err; |
|
1555 } |
|
1556 (void) memset(sp, 0, sizeof (PK11_SESSION)); |
|
1557 |
|
1558 /* |
|
1559 * It is a new session so it will look like a cache miss to the |
|
1560 * code below. So, we must not try to to destroy its members so |
|
1561 * mark them as unused. |
|
1562 */ |
|
1563 sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; |
|
1564 sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; |
|
1565 } |
|
1566 else |
|
1567 freelist = sp->next; |
|
1568 |
|
1569 /* |
|
1570 * Check whether we have forked. In that case, we must get rid of all |
|
1571 * inherited sessions and start allocating new ones. |
|
1572 */ |
|
1573 if (pid != (new_pid = getpid())) |
|
1574 { |
|
1575 pid = new_pid; |
|
1576 |
|
1577 /* |
|
1578 * We are a new process and thus need to free any inherited |
|
1579 * PK11_SESSION objects aside from the first session (sp) which |
|
1580 * is the only PK11_SESSION structure we will reuse (for the |
|
1581 * head of the list). |
|
1582 */ |
|
1583 while ((sp1 = freelist) != NULL) |
|
1584 { |
|
1585 freelist = sp1->next; |
|
1586 /* |
|
1587 * NOTE: we do not want to call pk11_free_all_sessions() |
|
1588 * here because it would close underlying PKCS#11 |
|
1589 * sessions and destroy all objects. |
|
1590 */ |
|
1591 pk11_free_nums(sp1, optype); |
|
1592 OPENSSL_free(sp1); |
|
1593 } |
|
1594 |
|
1595 /* we have to free the active list as well. */ |
|
1596 pk11_free_active_list(optype); |
|
1597 |
|
1598 /* Initialize the process */ |
|
1599 rv = pFuncList->C_Initialize(NULL_PTR); |
|
1600 if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) |
|
1601 { |
|
1602 PK11err_add_data(PK11_F_GET_SESSION, PK11_R_INITIALIZE, |
|
1603 rv); |
|
1604 OPENSSL_free(sp); |
|
1605 sp = NULL; |
|
1606 goto err; |
|
1607 } |
|
1608 |
|
1609 /* |
|
1610 * Choose slot here since the slot table is different on this |
|
1611 * process. If we are here then we must have found at least one |
|
1612 * usable slot before so we don't need to check any_slot_found. |
|
1613 * See pk11_library_init()'s usage of this function for more |
|
1614 * information. |
|
1615 */ |
|
1616 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
1617 if (check_hw_mechanisms() == 0) |
|
1618 goto err; |
|
1619 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
1620 if (pk11_choose_slots(NULL) == 0) |
|
1621 goto err; |
|
1622 |
|
1623 /* Open the global_session for the new process */ |
|
1624 rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, |
|
1625 NULL_PTR, NULL_PTR, &global_session); |
|
1626 if (rv != CKR_OK) |
|
1627 { |
|
1628 PK11err_add_data(PK11_F_GET_SESSION, PK11_R_OPENSESSION, |
|
1629 rv); |
|
1630 OPENSSL_free(sp); |
|
1631 sp = NULL; |
|
1632 goto err; |
|
1633 } |
|
1634 |
|
1635 /* |
|
1636 * It is an inherited session from our parent so it needs |
|
1637 * re-initialization. |
|
1638 */ |
|
1639 if (pk11_setup_session(sp, optype) == 0) |
|
1640 { |
|
1641 OPENSSL_free(sp); |
|
1642 sp = NULL; |
|
1643 goto err; |
|
1644 } |
|
1645 if (pk11_token_relogin(sp->session) == 0) |
|
1646 { |
|
1647 /* |
|
1648 * We will keep the session in the cache list and let |
|
1649 * the caller cope with the situation. |
|
1650 */ |
|
1651 freelist = sp; |
|
1652 sp = NULL; |
|
1653 goto err; |
|
1654 } |
|
1655 } |
|
1656 |
|
1657 if (sp->pid == 0) |
|
1658 { |
|
1659 /* It is a new session and needs initialization. */ |
|
1660 if (pk11_setup_session(sp, optype) == 0) |
|
1661 { |
|
1662 OPENSSL_free(sp); |
|
1663 sp = NULL; |
|
1664 } |
|
1665 } |
|
1666 |
|
1667 /* set new head for the list of PK11_SESSION objects */ |
|
1668 session_cache[optype].head = freelist; |
|
1669 |
|
1670 err: |
|
1671 if (sp != NULL) |
|
1672 sp->next = NULL; |
|
1673 |
|
1674 (void) pthread_mutex_unlock(freelist_lock); |
|
1675 |
|
1676 return (sp); |
|
1677 } |
|
1678 |
|
1679 |
|
1680 void |
|
1681 pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype) |
|
1682 { |
|
1683 pthread_mutex_t *freelist_lock; |
|
1684 PK11_SESSION *freelist; |
|
1685 |
|
1686 /* |
|
1687 * If this is a session from the parent it will be taken care of and |
|
1688 * freed in pk11_get_session() as part of the post-fork clean up the |
|
1689 * next time we will ask for a new session. |
|
1690 */ |
|
1691 if (sp == NULL || sp->pid != getpid()) |
|
1692 return; |
|
1693 |
|
1694 switch (optype) |
|
1695 { |
|
1696 case OP_RSA: |
|
1697 case OP_DSA: |
|
1698 case OP_DH: |
|
1699 case OP_RAND: |
|
1700 case OP_DIGEST: |
|
1701 case OP_CIPHER: |
|
1702 freelist_lock = session_cache[optype].lock; |
|
1703 break; |
|
1704 default: |
|
1705 PK11err(PK11_F_RETURN_SESSION, |
|
1706 PK11_R_INVALID_OPERATION_TYPE); |
|
1707 return; |
|
1708 } |
|
1709 |
|
1710 (void) pthread_mutex_lock(freelist_lock); |
|
1711 freelist = session_cache[optype].head; |
|
1712 sp->next = freelist; |
|
1713 session_cache[optype].head = sp; |
|
1714 (void) pthread_mutex_unlock(freelist_lock); |
|
1715 } |
|
1716 |
|
1717 |
|
1718 /* Destroy all objects. This function is called when the engine is finished */ |
|
1719 static int pk11_free_all_sessions() |
|
1720 { |
|
1721 int ret = 1; |
|
1722 int type; |
|
1723 |
|
1724 #ifndef OPENSSL_NO_RSA |
|
1725 (void) pk11_destroy_rsa_key_objects(NULL); |
|
1726 #endif /* OPENSSL_NO_RSA */ |
|
1727 #ifndef OPENSSL_NO_DSA |
|
1728 (void) pk11_destroy_dsa_key_objects(NULL); |
|
1729 #endif /* OPENSSL_NO_DSA */ |
|
1730 #ifndef OPENSSL_NO_DH |
|
1731 (void) pk11_destroy_dh_key_objects(NULL); |
|
1732 #endif /* OPENSSL_NO_DH */ |
|
1733 (void) pk11_destroy_cipher_key_objects(NULL); |
|
1734 |
|
1735 /* |
|
1736 * We try to release as much as we can but any error means that we will |
|
1737 * return 0 on exit. |
|
1738 */ |
|
1739 for (type = 0; type < OP_MAX; type++) |
|
1740 { |
|
1741 if (pk11_free_session_list(type) == 0) |
|
1742 ret = 0; |
|
1743 } |
|
1744 |
|
1745 return (ret); |
|
1746 } |
|
1747 |
|
1748 /* |
|
1749 * Destroy session structures from the linked list specified. Free as many |
|
1750 * sessions as possible but any failure in C_CloseSession() means that we |
|
1751 * return an error on return. |
|
1752 */ |
|
1753 static int pk11_free_session_list(PK11_OPTYPE optype) |
|
1754 { |
|
1755 CK_RV rv; |
|
1756 PK11_SESSION *sp = NULL; |
|
1757 PK11_SESSION *freelist = NULL; |
|
1758 pid_t mypid = getpid(); |
|
1759 pthread_mutex_t *freelist_lock; |
|
1760 int ret = 1; |
|
1761 |
|
1762 switch (optype) |
|
1763 { |
|
1764 case OP_RSA: |
|
1765 case OP_DSA: |
|
1766 case OP_DH: |
|
1767 case OP_RAND: |
|
1768 case OP_DIGEST: |
|
1769 case OP_CIPHER: |
|
1770 freelist_lock = session_cache[optype].lock; |
|
1771 break; |
|
1772 default: |
|
1773 PK11err(PK11_F_FREE_ALL_SESSIONS, |
|
1774 PK11_R_INVALID_OPERATION_TYPE); |
|
1775 return (0); |
|
1776 } |
|
1777 |
|
1778 (void) pthread_mutex_lock(freelist_lock); |
|
1779 freelist = session_cache[optype].head; |
|
1780 while ((sp = freelist) != NULL) |
|
1781 { |
|
1782 if (sp->session != CK_INVALID_HANDLE && sp->pid == mypid) |
|
1783 { |
|
1784 rv = pFuncList->C_CloseSession(sp->session); |
|
1785 if (rv != CKR_OK) |
|
1786 { |
|
1787 PK11err_add_data(PK11_F_FREE_ALL_SESSIONS, |
|
1788 PK11_R_CLOSESESSION, rv); |
|
1789 ret = 0; |
|
1790 } |
|
1791 } |
|
1792 freelist = sp->next; |
|
1793 pk11_free_nums(sp, optype); |
|
1794 OPENSSL_free(sp); |
|
1795 } |
|
1796 |
|
1797 (void) pthread_mutex_unlock(freelist_lock); |
|
1798 return (ret); |
|
1799 } |
|
1800 |
|
1801 |
|
1802 static int |
|
1803 pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype) |
|
1804 { |
|
1805 CK_RV rv; |
|
1806 CK_SLOT_ID myslot; |
|
1807 |
|
1808 switch (optype) |
|
1809 { |
|
1810 case OP_RSA: |
|
1811 case OP_DSA: |
|
1812 case OP_DH: |
|
1813 myslot = pubkey_SLOTID; |
|
1814 break; |
|
1815 case OP_RAND: |
|
1816 myslot = rand_SLOTID; |
|
1817 break; |
|
1818 case OP_DIGEST: |
|
1819 case OP_CIPHER: |
|
1820 myslot = SLOTID; |
|
1821 break; |
|
1822 default: |
|
1823 PK11err(PK11_F_SETUP_SESSION, |
|
1824 PK11_R_INVALID_OPERATION_TYPE); |
|
1825 return (0); |
|
1826 } |
|
1827 |
|
1828 sp->session = CK_INVALID_HANDLE; |
|
1829 DEBUG_SLOT_SEL("%s: myslot=%d optype=%d\n", PK11_DBG, myslot, optype); |
|
1830 rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION, |
|
1831 NULL_PTR, NULL_PTR, &sp->session); |
|
1832 if (rv == CKR_CRYPTOKI_NOT_INITIALIZED) |
|
1833 { |
|
1834 /* |
|
1835 * We are probably a child process so force the |
|
1836 * reinitialize of the session |
|
1837 */ |
|
1838 pk11_library_initialized = CK_FALSE; |
|
1839 if (!pk11_library_init(NULL)) |
|
1840 return (0); |
|
1841 rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION, |
|
1842 NULL_PTR, NULL_PTR, &sp->session); |
|
1843 } |
|
1844 if (rv != CKR_OK) |
|
1845 { |
|
1846 PK11err_add_data(PK11_F_SETUP_SESSION, PK11_R_OPENSESSION, rv); |
|
1847 return (0); |
|
1848 } |
|
1849 |
|
1850 sp->pid = getpid(); |
|
1851 |
|
1852 switch (optype) |
|
1853 { |
|
1854 #ifndef OPENSSL_NO_RSA |
|
1855 case OP_RSA: |
|
1856 sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; |
|
1857 sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; |
|
1858 sp->opdata_rsa_pub = NULL; |
|
1859 sp->opdata_rsa_n_num = NULL; |
|
1860 sp->opdata_rsa_e_num = NULL; |
|
1861 sp->opdata_rsa_priv = NULL; |
|
1862 sp->opdata_rsa_d_num = NULL; |
|
1863 break; |
|
1864 #endif /* OPENSSL_NO_RSA */ |
|
1865 #ifndef OPENSSL_NO_DSA |
|
1866 case OP_DSA: |
|
1867 sp->opdata_dsa_pub_key = CK_INVALID_HANDLE; |
|
1868 sp->opdata_dsa_priv_key = CK_INVALID_HANDLE; |
|
1869 sp->opdata_dsa_pub = NULL; |
|
1870 sp->opdata_dsa_pub_num = NULL; |
|
1871 sp->opdata_dsa_priv = NULL; |
|
1872 sp->opdata_dsa_priv_num = NULL; |
|
1873 break; |
|
1874 #endif /* OPENSSL_NO_DSA */ |
|
1875 #ifndef OPENSSL_NO_DH |
|
1876 case OP_DH: |
|
1877 sp->opdata_dh_key = CK_INVALID_HANDLE; |
|
1878 sp->opdata_dh = NULL; |
|
1879 sp->opdata_dh_priv_num = NULL; |
|
1880 break; |
|
1881 #endif /* OPENSSL_NO_DH */ |
|
1882 case OP_CIPHER: |
|
1883 sp->opdata_cipher_key = CK_INVALID_HANDLE; |
|
1884 sp->opdata_encrypt = -1; |
|
1885 break; |
|
1886 } |
|
1887 |
|
1888 /* |
|
1889 * We always initialize the session as containing a non-persistent |
|
1890 * object. The key load functions set it to persistent if that is so. |
|
1891 */ |
|
1892 sp->persistent = CK_FALSE; |
|
1893 return (1); |
|
1894 } |
|
1895 |
|
1896 #ifndef OPENSSL_NO_RSA |
|
1897 /* |
|
1898 * Destroy all non-NULL RSA parameters. For the RSA keys by reference code, |
|
1899 * public components 'n'/'e' are the key components we use to check for the |
|
1900 * cache hit even for the private keys. So, no matter whether we are destroying |
|
1901 * a public or a private key, we always free what we can. |
|
1902 */ |
|
1903 static void |
|
1904 destroy_all_rsa_params(PK11_SESSION *sp) |
|
1905 { |
|
1906 if (sp->opdata_rsa_n_num != NULL) |
|
1907 { |
|
1908 BN_free(sp->opdata_rsa_n_num); |
|
1909 sp->opdata_rsa_n_num = NULL; |
|
1910 } |
|
1911 if (sp->opdata_rsa_e_num != NULL) |
|
1912 { |
|
1913 BN_free(sp->opdata_rsa_e_num); |
|
1914 sp->opdata_rsa_e_num = NULL; |
|
1915 } |
|
1916 if (sp->opdata_rsa_d_num != NULL) |
|
1917 { |
|
1918 BN_free(sp->opdata_rsa_d_num); |
|
1919 sp->opdata_rsa_d_num = NULL; |
|
1920 } |
|
1921 } |
|
1922 |
|
1923 /* Destroy RSA public key from single session. */ |
|
1924 int |
|
1925 pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock) |
|
1926 { |
|
1927 int ret = 0; |
|
1928 |
|
1929 if (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE) |
|
1930 { |
|
1931 TRY_OBJ_DESTROY(sp, sp->opdata_rsa_pub_key, |
|
1932 ret, uselock, OP_RSA); |
|
1933 sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; |
|
1934 sp->opdata_rsa_pub = NULL; |
|
1935 destroy_all_rsa_params(sp); |
|
1936 } |
|
1937 |
|
1938 return (ret); |
|
1939 } |
|
1940 |
|
1941 /* Destroy RSA private key from single session. */ |
|
1942 int |
|
1943 pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock) |
|
1944 { |
|
1945 int ret = 0; |
|
1946 |
|
1947 if (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE) |
|
1948 { |
|
1949 TRY_OBJ_DESTROY(sp, sp->opdata_rsa_priv_key, |
|
1950 ret, uselock, OP_RSA); |
|
1951 sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; |
|
1952 sp->opdata_rsa_priv = NULL; |
|
1953 destroy_all_rsa_params(sp); |
|
1954 } |
|
1955 |
|
1956 return (ret); |
|
1957 } |
|
1958 |
|
1959 /* |
|
1960 * Destroy RSA key object wrapper. If session is NULL, try to destroy all |
|
1961 * objects in the free list. |
|
1962 */ |
|
1963 int |
|
1964 pk11_destroy_rsa_key_objects(PK11_SESSION *session) |
|
1965 { |
|
1966 int ret = 1; |
|
1967 PK11_SESSION *sp = NULL; |
|
1968 PK11_SESSION *local_free_session; |
|
1969 CK_BBOOL uselock = CK_TRUE; |
|
1970 |
|
1971 if (session != NULL) |
|
1972 local_free_session = session; |
|
1973 else |
|
1974 { |
|
1975 (void) pthread_mutex_lock(session_cache[OP_RSA].lock); |
|
1976 local_free_session = session_cache[OP_RSA].head; |
|
1977 uselock = CK_FALSE; |
|
1978 } |
|
1979 |
|
1980 /* |
|
1981 * go through the list of sessions and delete key objects |
|
1982 */ |
|
1983 while ((sp = local_free_session) != NULL) |
|
1984 { |
|
1985 local_free_session = sp->next; |
|
1986 |
|
1987 /* |
|
1988 * Do not terminate list traversal if one of the |
|
1989 * destroy operations fails. |
|
1990 */ |
|
1991 if (pk11_destroy_rsa_object_pub(sp, uselock) == 0) |
|
1992 { |
|
1993 ret = 0; |
|
1994 continue; |
|
1995 } |
|
1996 if (pk11_destroy_rsa_object_priv(sp, uselock) == 0) |
|
1997 { |
|
1998 ret = 0; |
|
1999 continue; |
|
2000 } |
|
2001 } |
|
2002 |
|
2003 if (session == NULL) |
|
2004 (void) pthread_mutex_unlock(session_cache[OP_RSA].lock); |
|
2005 |
|
2006 return (ret); |
|
2007 } |
|
2008 #endif /* OPENSSL_NO_RSA */ |
|
2009 |
|
2010 #ifndef OPENSSL_NO_DSA |
|
2011 /* Destroy DSA public key from single session. */ |
|
2012 int |
|
2013 pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock) |
|
2014 { |
|
2015 int ret = 0; |
|
2016 |
|
2017 if (sp->opdata_dsa_pub_key != CK_INVALID_HANDLE) |
|
2018 { |
|
2019 TRY_OBJ_DESTROY(sp, sp->opdata_dsa_pub_key, |
|
2020 ret, uselock, OP_DSA); |
|
2021 sp->opdata_dsa_pub_key = CK_INVALID_HANDLE; |
|
2022 sp->opdata_dsa_pub = NULL; |
|
2023 if (sp->opdata_dsa_pub_num != NULL) |
|
2024 { |
|
2025 BN_free(sp->opdata_dsa_pub_num); |
|
2026 sp->opdata_dsa_pub_num = NULL; |
|
2027 } |
|
2028 } |
|
2029 |
|
2030 return (ret); |
|
2031 } |
|
2032 |
|
2033 /* Destroy DSA private key from single session. */ |
|
2034 int |
|
2035 pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock) |
|
2036 { |
|
2037 int ret = 0; |
|
2038 |
|
2039 if (sp->opdata_dsa_priv_key != CK_INVALID_HANDLE) |
|
2040 { |
|
2041 TRY_OBJ_DESTROY(sp, sp->opdata_dsa_priv_key, |
|
2042 ret, uselock, OP_DSA); |
|
2043 sp->opdata_dsa_priv_key = CK_INVALID_HANDLE; |
|
2044 sp->opdata_dsa_priv = NULL; |
|
2045 if (sp->opdata_dsa_priv_num != NULL) |
|
2046 { |
|
2047 BN_free(sp->opdata_dsa_priv_num); |
|
2048 sp->opdata_dsa_priv_num = NULL; |
|
2049 } |
|
2050 } |
|
2051 |
|
2052 return (ret); |
|
2053 } |
|
2054 |
|
2055 /* |
|
2056 * Destroy DSA key object wrapper. If session is NULL, try to destroy all |
|
2057 * objects in the free list. |
|
2058 */ |
|
2059 int |
|
2060 pk11_destroy_dsa_key_objects(PK11_SESSION *session) |
|
2061 { |
|
2062 int ret = 1; |
|
2063 PK11_SESSION *sp = NULL; |
|
2064 PK11_SESSION *local_free_session; |
|
2065 CK_BBOOL uselock = CK_TRUE; |
|
2066 |
|
2067 if (session != NULL) |
|
2068 local_free_session = session; |
|
2069 else |
|
2070 { |
|
2071 (void) pthread_mutex_lock(session_cache[OP_DSA].lock); |
|
2072 local_free_session = session_cache[OP_DSA].head; |
|
2073 uselock = CK_FALSE; |
|
2074 } |
|
2075 |
|
2076 /* |
|
2077 * go through the list of sessions and delete key objects |
|
2078 */ |
|
2079 while ((sp = local_free_session) != NULL) |
|
2080 { |
|
2081 local_free_session = sp->next; |
|
2082 |
|
2083 /* |
|
2084 * Do not terminate list traversal if one of the |
|
2085 * destroy operations fails. |
|
2086 */ |
|
2087 if (pk11_destroy_dsa_object_pub(sp, uselock) == 0) |
|
2088 { |
|
2089 ret = 0; |
|
2090 continue; |
|
2091 } |
|
2092 if (pk11_destroy_dsa_object_priv(sp, uselock) == 0) |
|
2093 { |
|
2094 ret = 0; |
|
2095 continue; |
|
2096 } |
|
2097 } |
|
2098 |
|
2099 if (session == NULL) |
|
2100 (void) pthread_mutex_unlock(session_cache[OP_DSA].lock); |
|
2101 |
|
2102 return (ret); |
|
2103 } |
|
2104 #endif /* OPENSSL_NO_DSA */ |
|
2105 |
|
2106 #ifndef OPENSSL_NO_DH |
|
2107 /* Destroy DH key from single session. */ |
|
2108 int |
|
2109 pk11_destroy_dh_object(PK11_SESSION *sp, CK_BBOOL uselock) |
|
2110 { |
|
2111 int ret = 0; |
|
2112 |
|
2113 if (sp->opdata_dh_key != CK_INVALID_HANDLE) |
|
2114 { |
|
2115 TRY_OBJ_DESTROY(sp, sp->opdata_dh_key, |
|
2116 ret, uselock, OP_DH); |
|
2117 sp->opdata_dh_key = CK_INVALID_HANDLE; |
|
2118 sp->opdata_dh = NULL; |
|
2119 if (sp->opdata_dh_priv_num != NULL) |
|
2120 { |
|
2121 BN_free(sp->opdata_dh_priv_num); |
|
2122 sp->opdata_dh_priv_num = NULL; |
|
2123 } |
|
2124 } |
|
2125 |
|
2126 return (ret); |
|
2127 } |
|
2128 |
|
2129 /* |
|
2130 * Destroy DH key object wrapper. |
|
2131 * |
|
2132 * arg0: pointer to PKCS#11 engine session structure |
|
2133 * if session is NULL, try to destroy all objects in the free list |
|
2134 */ |
|
2135 int |
|
2136 pk11_destroy_dh_key_objects(PK11_SESSION *session) |
|
2137 { |
|
2138 int ret = 1; |
|
2139 PK11_SESSION *sp = NULL; |
|
2140 PK11_SESSION *local_free_session; |
|
2141 CK_BBOOL uselock = CK_TRUE; |
|
2142 |
|
2143 if (session != NULL) |
|
2144 local_free_session = session; |
|
2145 else |
|
2146 { |
|
2147 (void) pthread_mutex_lock(session_cache[OP_DH].lock); |
|
2148 local_free_session = session_cache[OP_DH].head; |
|
2149 uselock = CK_FALSE; |
|
2150 } |
|
2151 |
|
2152 while ((sp = local_free_session) != NULL) |
|
2153 { |
|
2154 local_free_session = sp->next; |
|
2155 |
|
2156 /* |
|
2157 * Do not terminate list traversal if one of the |
|
2158 * destroy operations fails. |
|
2159 */ |
|
2160 if (pk11_destroy_dh_object(sp, uselock) == 0) |
|
2161 { |
|
2162 ret = 0; |
|
2163 continue; |
|
2164 } |
|
2165 } |
|
2166 if (session == NULL) |
|
2167 (void) pthread_mutex_unlock(session_cache[OP_DH].lock); |
|
2168 |
|
2169 return (ret); |
|
2170 } |
|
2171 #endif /* OPENSSL_NO_DH */ |
|
2172 |
|
2173 static int |
|
2174 pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh, |
|
2175 CK_BBOOL persistent) |
|
2176 { |
|
2177 CK_RV rv; |
|
2178 |
|
2179 /* |
|
2180 * We never try to destroy persistent objects which are the objects |
|
2181 * stored in the keystore. Also, we always use read-only sessions so |
|
2182 * C_DestroyObject() would be returning CKR_SESSION_READ_ONLY here. |
|
2183 */ |
|
2184 if (persistent == CK_TRUE) |
|
2185 return (1); |
|
2186 |
|
2187 rv = pFuncList->C_DestroyObject(session, oh); |
|
2188 if (rv != CKR_OK) |
|
2189 { |
|
2190 PK11err_add_data(PK11_F_DESTROY_OBJECT, PK11_R_DESTROYOBJECT, |
|
2191 rv); |
|
2192 return (0); |
|
2193 } |
|
2194 |
|
2195 return (1); |
|
2196 } |
|
2197 |
|
2198 |
|
2199 /* Symmetric ciphers and digests support functions */ |
|
2200 |
|
2201 static int |
|
2202 cipher_nid_to_pk11(int nid) |
|
2203 { |
|
2204 int i; |
|
2205 |
|
2206 for (i = 0; i < PK11_CIPHER_MAX; i++) |
|
2207 if (ciphers[i].nid == nid) |
|
2208 return (ciphers[i].id); |
|
2209 return (-1); |
|
2210 } |
|
2211 |
|
2212 static int |
|
2213 pk11_usable_ciphers(const int **nids) |
|
2214 { |
|
2215 if (cipher_count > 0) |
|
2216 *nids = cipher_nids; |
|
2217 else |
|
2218 *nids = NULL; |
|
2219 return (cipher_count); |
|
2220 } |
|
2221 |
|
2222 static int |
|
2223 pk11_usable_digests(const int **nids) |
|
2224 { |
|
2225 if (digest_count > 0) |
|
2226 *nids = digest_nids; |
|
2227 else |
|
2228 *nids = NULL; |
|
2229 return (digest_count); |
|
2230 } |
|
2231 |
|
2232 /* |
|
2233 * Init context for encryption or decryption using a symmetric key. |
|
2234 */ |
|
2235 static int pk11_init_symmetric(EVP_CIPHER_CTX *ctx, PK11_CIPHER *pcipher, |
|
2236 PK11_SESSION *sp, CK_MECHANISM_PTR pmech) |
|
2237 { |
|
2238 CK_RV rv; |
|
2239 CK_AES_CTR_PARAMS ctr_params; |
|
2240 |
|
2241 /* |
|
2242 * We expect pmech->mechanism to be already set and |
|
2243 * pParameter/ulParameterLen initialized to NULL/0 before |
|
2244 * pk11_init_symmetric() is called. |
|
2245 */ |
|
2246 OPENSSL_assert(pmech->mechanism != NULL); |
|
2247 OPENSSL_assert(pmech->pParameter == NULL); |
|
2248 OPENSSL_assert(pmech->ulParameterLen == 0); |
|
2249 |
|
2250 if (ctx->cipher->nid == NID_aes_128_ctr || |
|
2251 ctx->cipher->nid == NID_aes_192_ctr || |
|
2252 ctx->cipher->nid == NID_aes_256_ctr) |
|
2253 { |
|
2254 pmech->pParameter = (void *)(&ctr_params); |
|
2255 pmech->ulParameterLen = sizeof (ctr_params); |
|
2256 /* |
|
2257 * For now, we are limited to the fixed length of the counter, |
|
2258 * it covers the whole counter block. That's what RFC 4344 |
|
2259 * needs. For more information on internal structure of the |
|
2260 * counter block, see RFC 3686. If needed in the future, we can |
|
2261 * add code so that the counter length can be set via |
|
2262 * ENGINE_ctrl() function. |
|
2263 */ |
|
2264 ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8; |
|
2265 OPENSSL_assert(pcipher->iv_len == AES_BLOCK_SIZE); |
|
2266 (void) memcpy(ctr_params.cb, ctx->iv, AES_BLOCK_SIZE); |
|
2267 } |
|
2268 else |
|
2269 { |
|
2270 if (pcipher->iv_len > 0) |
|
2271 { |
|
2272 pmech->pParameter = (void *)ctx->iv; |
|
2273 pmech->ulParameterLen = pcipher->iv_len; |
|
2274 } |
|
2275 } |
|
2276 |
|
2277 /* if we get here, the encryption needs to be reinitialized */ |
|
2278 if (ctx->encrypt) |
|
2279 rv = pFuncList->C_EncryptInit(sp->session, pmech, |
|
2280 sp->opdata_cipher_key); |
|
2281 else |
|
2282 rv = pFuncList->C_DecryptInit(sp->session, pmech, |
|
2283 sp->opdata_cipher_key); |
|
2284 |
|
2285 if (rv != CKR_OK) |
|
2286 { |
|
2287 PK11err_add_data(PK11_F_CIPHER_INIT, ctx->encrypt ? |
|
2288 PK11_R_ENCRYPTINIT : PK11_R_DECRYPTINIT, rv); |
|
2289 pk11_return_session(sp, OP_CIPHER); |
|
2290 return (0); |
|
2291 } |
|
2292 |
|
2293 return (1); |
|
2294 } |
|
2295 |
|
2296 /* ARGSUSED */ |
|
2297 static int |
|
2298 pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
|
2299 const unsigned char *iv, int enc) |
|
2300 { |
|
2301 CK_MECHANISM mech; |
|
2302 int index; |
|
2303 PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data; |
|
2304 PK11_SESSION *sp; |
|
2305 PK11_CIPHER *p_ciph_table_row; |
|
2306 |
|
2307 state->sp = NULL; |
|
2308 |
|
2309 index = cipher_nid_to_pk11(ctx->cipher->nid); |
|
2310 if (index < 0 || index >= PK11_CIPHER_MAX) |
|
2311 return (0); |
|
2312 |
|
2313 p_ciph_table_row = &ciphers[index]; |
|
2314 /* |
|
2315 * iv_len in the ctx->cipher structure is the maximum IV length for the |
|
2316 * current cipher and it must be less or equal to the IV length in our |
|
2317 * ciphers table. The key length must be in the allowed interval. From |
|
2318 * all cipher modes that the PKCS#11 engine supports only RC4 allows a |
|
2319 * key length to be in some range, all other NIDs have a precise key |
|
2320 * length. Every application can define its own EVP functions so this |
|
2321 * code serves as a sanity check. |
|
2322 * |
|
2323 * Note that the reason why the IV length in ctx->cipher might be |
|
2324 * greater than the actual length is that OpenSSL uses BLOCK_CIPHER_defs |
|
2325 * macro to define functions that return EVP structures for all DES |
|
2326 * modes. So, even ECB modes get 8 byte IV. |
|
2327 */ |
|
2328 if (ctx->cipher->iv_len < p_ciph_table_row->iv_len || |
|
2329 ctx->key_len < p_ciph_table_row->min_key_len || |
|
2330 ctx->key_len > p_ciph_table_row->max_key_len) { |
|
2331 PK11err(PK11_F_CIPHER_INIT, PK11_R_KEY_OR_IV_LEN_PROBLEM); |
|
2332 return (0); |
|
2333 } |
|
2334 |
|
2335 if ((sp = pk11_get_session(OP_CIPHER)) == NULL) |
|
2336 return (0); |
|
2337 |
|
2338 /* if applicable, the mechanism parameter is used for IV */ |
|
2339 mech.mechanism = p_ciph_table_row->mech_type; |
|
2340 mech.pParameter = NULL; |
|
2341 mech.ulParameterLen = 0; |
|
2342 |
|
2343 /* The key object is destroyed here if it is not the current key. */ |
|
2344 (void) check_new_cipher_key(sp, key, ctx->key_len); |
|
2345 |
|
2346 /* |
|
2347 * If the key is the same and the encryption is also the same, then |
|
2348 * just reuse it. However, we must not forget to reinitialize the |
|
2349 * context that was finalized in pk11_cipher_cleanup(). |
|
2350 */ |
|
2351 if (sp->opdata_cipher_key != CK_INVALID_HANDLE && |
|
2352 sp->opdata_encrypt == ctx->encrypt) |
|
2353 { |
|
2354 state->sp = sp; |
|
2355 if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0) |
|
2356 return (0); |
|
2357 |
|
2358 return (1); |
|
2359 } |
|
2360 |
|
2361 /* |
|
2362 * Check if the key has been invalidated. If so, a new key object |
|
2363 * needs to be created. |
|
2364 */ |
|
2365 if (sp->opdata_cipher_key == CK_INVALID_HANDLE) |
|
2366 { |
|
2367 sp->opdata_cipher_key = pk11_get_cipher_key( |
|
2368 ctx, key, p_ciph_table_row->key_type, sp); |
|
2369 } |
|
2370 |
|
2371 if (sp->opdata_encrypt != ctx->encrypt && sp->opdata_encrypt != -1) |
|
2372 { |
|
2373 /* |
|
2374 * The previous encryption/decryption is different. Need to |
|
2375 * terminate the previous * active encryption/decryption here. |
|
2376 */ |
|
2377 if (!pk11_cipher_final(sp)) |
|
2378 { |
|
2379 pk11_return_session(sp, OP_CIPHER); |
|
2380 return (0); |
|
2381 } |
|
2382 } |
|
2383 |
|
2384 if (sp->opdata_cipher_key == CK_INVALID_HANDLE) |
|
2385 { |
|
2386 pk11_return_session(sp, OP_CIPHER); |
|
2387 return (0); |
|
2388 } |
|
2389 |
|
2390 /* now initialize the context with a new key */ |
|
2391 if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0) |
|
2392 return (0); |
|
2393 |
|
2394 sp->opdata_encrypt = ctx->encrypt; |
|
2395 state->sp = sp; |
|
2396 |
|
2397 return (1); |
|
2398 } |
|
2399 |
|
2400 /* |
|
2401 * When reusing the same key in an encryption/decryption session for a |
|
2402 * decryption/encryption session, we need to close the active session |
|
2403 * and recreate a new one. Note that the key is in the global session so |
|
2404 * that it needs not be recreated. |
|
2405 * |
|
2406 * It is more appropriate to use C_En/DecryptFinish here. At the time of this |
|
2407 * development, these two functions in the PKCS#11 libraries used return |
|
2408 * unexpected errors when passing in 0 length output. It may be a good |
|
2409 * idea to try them again if performance is a problem here and fix |
|
2410 * C_En/DecryptFinial if there are bugs there causing the problem. |
|
2411 */ |
|
2412 static int |
|
2413 pk11_cipher_final(PK11_SESSION *sp) |
|
2414 { |
|
2415 CK_RV rv; |
|
2416 |
|
2417 rv = pFuncList->C_CloseSession(sp->session); |
|
2418 if (rv != CKR_OK) |
|
2419 { |
|
2420 PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_CLOSESESSION, rv); |
|
2421 return (0); |
|
2422 } |
|
2423 |
|
2424 rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, |
|
2425 NULL_PTR, NULL_PTR, &sp->session); |
|
2426 if (rv != CKR_OK) |
|
2427 { |
|
2428 PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_OPENSESSION, rv); |
|
2429 return (0); |
|
2430 } |
|
2431 |
|
2432 return (1); |
|
2433 } |
|
2434 |
|
2435 /* |
|
2436 * An engine interface function. The calling function allocates sufficient |
|
2437 * memory for the output buffer "out" to hold the results. |
|
2438 */ |
|
2439 static int |
|
2440 pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
|
2441 const unsigned char *in, size_t inl) |
|
2442 { |
|
2443 PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data; |
|
2444 PK11_SESSION *sp; |
|
2445 CK_RV rv; |
|
2446 unsigned long outl = inl; |
|
2447 |
|
2448 if (state == NULL || state->sp == NULL) |
|
2449 return (0); |
|
2450 |
|
2451 sp = (PK11_SESSION *) state->sp; |
|
2452 |
|
2453 if (!inl) |
|
2454 return (1); |
|
2455 |
|
2456 /* RC4 is the only stream cipher we support */ |
|
2457 if (ctx->cipher->nid != NID_rc4 && (inl % ctx->cipher->block_size) != 0) |
|
2458 return (0); |
|
2459 |
|
2460 if (ctx->encrypt) |
|
2461 { |
|
2462 rv = pFuncList->C_EncryptUpdate(sp->session, |
|
2463 (unsigned char *)in, inl, out, &outl); |
|
2464 |
|
2465 if (rv != CKR_OK) |
|
2466 { |
|
2467 PK11err_add_data(PK11_F_CIPHER_DO_CIPHER, |
|
2468 PK11_R_ENCRYPTUPDATE, rv); |
|
2469 return (0); |
|
2470 } |
|
2471 } |
|
2472 else |
|
2473 { |
|
2474 rv = pFuncList->C_DecryptUpdate(sp->session, |
|
2475 (unsigned char *)in, inl, out, &outl); |
|
2476 |
|
2477 if (rv != CKR_OK) |
|
2478 { |
|
2479 PK11err_add_data(PK11_F_CIPHER_DO_CIPHER, |
|
2480 PK11_R_DECRYPTUPDATE, rv); |
|
2481 return (0); |
|
2482 } |
|
2483 } |
|
2484 |
|
2485 /* |
|
2486 * For DES_CBC, DES3_CBC, AES_CBC, and RC4, the output size is always |
|
2487 * the same size of input. |
|
2488 * The application has guaranteed to call the block ciphers with |
|
2489 * correctly aligned buffers. |
|
2490 */ |
|
2491 if (inl != outl) |
|
2492 return (0); |
|
2493 |
|
2494 return (1); |
|
2495 } |
|
2496 |
|
2497 /* |
|
2498 * Return the session to the pool. Calling C_EncryptFinal() and C_DecryptFinal() |
|
2499 * here is the right thing because in EVP_DecryptFinal_ex(), engine's |
|
2500 * do_cipher() is not even called, and in EVP_EncryptFinal_ex() it is called but |
|
2501 * the engine can't find out that it's the finalizing call. We wouldn't |
|
2502 * necessarily have to finalize the context here since reinitializing it with |
|
2503 * C_(Encrypt|Decrypt)Init() should be fine but for the sake of correctness, |
|
2504 * let's do it. Some implementations might leak memory if the previously used |
|
2505 * context is initialized without finalizing it first. |
|
2506 */ |
|
2507 static int |
|
2508 pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx) |
|
2509 { |
|
2510 CK_RV rv; |
|
2511 CK_ULONG len = EVP_MAX_BLOCK_LENGTH; |
|
2512 CK_BYTE buf[EVP_MAX_BLOCK_LENGTH]; |
|
2513 PK11_CIPHER_STATE *state = ctx->cipher_data; |
|
2514 |
|
2515 if (state != NULL && state->sp != NULL) |
|
2516 { |
|
2517 /* |
|
2518 * We are not interested in the data here, we just need to get |
|
2519 * rid of the context. |
|
2520 */ |
|
2521 if (ctx->encrypt) |
|
2522 rv = pFuncList->C_EncryptFinal( |
|
2523 state->sp->session, buf, &len); |
|
2524 else |
|
2525 rv = pFuncList->C_DecryptFinal( |
|
2526 state->sp->session, buf, &len); |
|
2527 |
|
2528 if (rv != CKR_OK) |
|
2529 { |
|
2530 PK11err_add_data(PK11_F_CIPHER_CLEANUP, ctx->encrypt ? |
|
2531 PK11_R_ENCRYPTFINAL : PK11_R_DECRYPTFINAL, rv); |
|
2532 pk11_return_session(state->sp, OP_CIPHER); |
|
2533 return (0); |
|
2534 } |
|
2535 |
|
2536 pk11_return_session(state->sp, OP_CIPHER); |
|
2537 state->sp = NULL; |
|
2538 } |
|
2539 |
|
2540 return (1); |
|
2541 } |
|
2542 |
|
2543 /* |
|
2544 * Registered by the ENGINE when used to find out how to deal with |
|
2545 * a particular NID in the ENGINE. This says what we'll do at the |
|
2546 * top level - note, that list is restricted by what we answer with |
|
2547 */ |
|
2548 /* ARGSUSED */ |
|
2549 static int |
|
2550 pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, |
|
2551 const int **nids, int nid) |
|
2552 { |
|
2553 if (!cipher) |
|
2554 return (pk11_usable_ciphers(nids)); |
|
2555 |
|
2556 switch (nid) |
|
2557 { |
|
2558 case NID_des_ede3_cbc: |
|
2559 *cipher = &pk11_3des_cbc; |
|
2560 break; |
|
2561 case NID_des_cbc: |
|
2562 *cipher = &pk11_des_cbc; |
|
2563 break; |
|
2564 case NID_des_ede3_ecb: |
|
2565 *cipher = &pk11_3des_ecb; |
|
2566 break; |
|
2567 case NID_des_ecb: |
|
2568 *cipher = &pk11_des_ecb; |
|
2569 break; |
|
2570 case NID_aes_128_cbc: |
|
2571 *cipher = &pk11_aes_128_cbc; |
|
2572 break; |
|
2573 case NID_aes_192_cbc: |
|
2574 *cipher = &pk11_aes_192_cbc; |
|
2575 break; |
|
2576 case NID_aes_256_cbc: |
|
2577 *cipher = &pk11_aes_256_cbc; |
|
2578 break; |
|
2579 case NID_aes_128_ecb: |
|
2580 *cipher = &pk11_aes_128_ecb; |
|
2581 break; |
|
2582 case NID_aes_192_ecb: |
|
2583 *cipher = &pk11_aes_192_ecb; |
|
2584 break; |
|
2585 case NID_aes_256_ecb: |
|
2586 *cipher = &pk11_aes_256_ecb; |
|
2587 break; |
|
2588 case NID_aes_128_ctr: |
|
2589 *cipher = &pk11_aes_128_ctr; |
|
2590 break; |
|
2591 case NID_aes_192_ctr: |
|
2592 *cipher = &pk11_aes_192_ctr; |
|
2593 break; |
|
2594 case NID_aes_256_ctr: |
|
2595 *cipher = &pk11_aes_256_ctr; |
|
2596 break; |
|
2597 case NID_bf_cbc: |
|
2598 *cipher = &pk11_bf_cbc; |
|
2599 break; |
|
2600 case NID_rc4: |
|
2601 *cipher = &pk11_rc4; |
|
2602 break; |
|
2603 default: |
|
2604 *cipher = NULL; |
|
2605 break; |
|
2606 } |
|
2607 return (*cipher != NULL); |
|
2608 } |
|
2609 |
|
2610 /* ARGSUSED */ |
|
2611 static int |
|
2612 pk11_engine_digests(ENGINE *e, const EVP_MD **digest, |
|
2613 const int **nids, int nid) |
|
2614 { |
|
2615 if (!digest) |
|
2616 return (pk11_usable_digests(nids)); |
|
2617 |
|
2618 switch (nid) |
|
2619 { |
|
2620 case NID_md5: |
|
2621 *digest = &pk11_md5; |
|
2622 break; |
|
2623 /* |
|
2624 * A special case. For "openssl dgst -dss1 -engine pkcs11 ...", |
|
2625 * OpenSSL calls EVP_get_digestbyname() on "dss1" which ends up |
|
2626 * calling pk11_engine_digests() for NID_dsa. Internally, if an |
|
2627 * engine is not used, OpenSSL uses SHA1_Init() as expected for |
|
2628 * DSA. So, we must return pk11_sha1() for NID_dsa as well. Note |
|
2629 * that this must have changed between 0.9.8 and 1.0.0 since we |
|
2630 * did not have the problem with the 0.9.8 version. |
|
2631 */ |
|
2632 case NID_sha1: |
|
2633 case NID_dsa: |
|
2634 *digest = &pk11_sha1; |
|
2635 break; |
|
2636 case NID_sha224: |
|
2637 *digest = &pk11_sha224; |
|
2638 break; |
|
2639 case NID_sha256: |
|
2640 *digest = &pk11_sha256; |
|
2641 break; |
|
2642 case NID_sha384: |
|
2643 *digest = &pk11_sha384; |
|
2644 break; |
|
2645 case NID_sha512: |
|
2646 *digest = &pk11_sha512; |
|
2647 break; |
|
2648 default: |
|
2649 *digest = NULL; |
|
2650 break; |
|
2651 } |
|
2652 return (*digest != NULL); |
|
2653 } |
|
2654 |
|
2655 |
|
2656 /* Create a secret key object in a PKCS#11 session */ |
|
2657 static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx, |
|
2658 const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp) |
|
2659 { |
|
2660 CK_RV rv; |
|
2661 CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; |
|
2662 CK_OBJECT_CLASS obj_key = CKO_SECRET_KEY; |
|
2663 CK_ULONG ul_key_attr_count = 6; |
|
2664 |
|
2665 CK_ATTRIBUTE a_key_template[] = |
|
2666 { |
|
2667 {CKA_CLASS, (void*) NULL, sizeof (CK_OBJECT_CLASS)}, |
|
2668 {CKA_KEY_TYPE, (void*) NULL, sizeof (CK_KEY_TYPE)}, |
|
2669 {CKA_TOKEN, &pk11_false, sizeof (pk11_false)}, |
|
2670 {CKA_ENCRYPT, &pk11_true, sizeof (pk11_true)}, |
|
2671 {CKA_DECRYPT, &pk11_true, sizeof (pk11_true)}, |
|
2672 {CKA_VALUE, (void*) NULL, 0}, |
|
2673 }; |
|
2674 |
|
2675 /* |
|
2676 * Create secret key object in global_session. All other sessions |
|
2677 * can use the key handles. Here is why: |
|
2678 * OpenSSL will call EncryptInit and EncryptUpdate using a secret key. |
|
2679 * It may then call DecryptInit and DecryptUpdate using the same key. |
|
2680 * To use the same key object, we need to call EncryptFinal with |
|
2681 * a 0 length message. Currently, this does not work for 3DES |
|
2682 * mechanism. To get around this problem, we close the session and |
|
2683 * then create a new session to use the same key object. When a session |
|
2684 * is closed, all the object handles will be invalid. Thus, create key |
|
2685 * objects in a global session, an individual session may be closed to |
|
2686 * terminate the active operation. |
|
2687 */ |
|
2688 CK_SESSION_HANDLE session = global_session; |
|
2689 a_key_template[0].pValue = &obj_key; |
|
2690 a_key_template[1].pValue = &key_type; |
|
2691 a_key_template[5].pValue = (void *) key; |
|
2692 a_key_template[5].ulValueLen = (unsigned long) ctx->key_len; |
|
2693 |
|
2694 rv = pFuncList->C_CreateObject(session, |
|
2695 a_key_template, ul_key_attr_count, &h_key); |
|
2696 if (rv != CKR_OK) |
|
2697 { |
|
2698 PK11err_add_data(PK11_F_GET_CIPHER_KEY, PK11_R_CREATEOBJECT, |
|
2699 rv); |
|
2700 goto err; |
|
2701 } |
|
2702 |
|
2703 /* |
|
2704 * Save the key information used in this session. |
|
2705 * The max can be saved is PK11_KEY_LEN_MAX. |
|
2706 */ |
|
2707 sp->opdata_key_len = ctx->key_len > PK11_KEY_LEN_MAX ? |
|
2708 PK11_KEY_LEN_MAX : ctx->key_len; |
|
2709 (void) memcpy(sp->opdata_key, key, sp->opdata_key_len); |
|
2710 err: |
|
2711 |
|
2712 return (h_key); |
|
2713 } |
|
2714 |
|
2715 static int |
|
2716 md_nid_to_pk11(int nid) |
|
2717 { |
|
2718 int i; |
|
2719 |
|
2720 for (i = 0; i < PK11_DIGEST_MAX; i++) |
|
2721 if (digests[i].nid == nid) |
|
2722 return (digests[i].id); |
|
2723 return (-1); |
|
2724 } |
|
2725 |
|
2726 static int |
|
2727 pk11_digest_init(EVP_MD_CTX *ctx) |
|
2728 { |
|
2729 CK_RV rv; |
|
2730 CK_MECHANISM mech; |
|
2731 int index; |
|
2732 PK11_SESSION *sp; |
|
2733 PK11_DIGEST *pdp; |
|
2734 PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data; |
|
2735 |
|
2736 state->sp = NULL; |
|
2737 |
|
2738 index = md_nid_to_pk11(ctx->digest->type); |
|
2739 if (index < 0 || index >= PK11_DIGEST_MAX) |
|
2740 return (0); |
|
2741 |
|
2742 pdp = &digests[index]; |
|
2743 if ((sp = pk11_get_session(OP_DIGEST)) == NULL) |
|
2744 return (0); |
|
2745 |
|
2746 /* at present, no parameter is needed for supported digests */ |
|
2747 mech.mechanism = pdp->mech_type; |
|
2748 mech.pParameter = NULL; |
|
2749 mech.ulParameterLen = 0; |
|
2750 |
|
2751 rv = pFuncList->C_DigestInit(sp->session, &mech); |
|
2752 |
|
2753 if (rv != CKR_OK) |
|
2754 { |
|
2755 PK11err_add_data(PK11_F_DIGEST_INIT, PK11_R_DIGESTINIT, rv); |
|
2756 pk11_return_session(sp, OP_DIGEST); |
|
2757 return (0); |
|
2758 } |
|
2759 |
|
2760 state->sp = sp; |
|
2761 |
|
2762 return (1); |
|
2763 } |
|
2764 |
|
2765 static int |
|
2766 pk11_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count) |
|
2767 { |
|
2768 CK_RV rv; |
|
2769 PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data; |
|
2770 |
|
2771 /* 0 length message will cause a failure in C_DigestFinal */ |
|
2772 if (count == 0) |
|
2773 return (1); |
|
2774 |
|
2775 if (state == NULL || state->sp == NULL) |
|
2776 return (0); |
|
2777 |
|
2778 rv = pFuncList->C_DigestUpdate(state->sp->session, (CK_BYTE *) data, |
|
2779 count); |
|
2780 |
|
2781 if (rv != CKR_OK) |
|
2782 { |
|
2783 PK11err_add_data(PK11_F_DIGEST_UPDATE, PK11_R_DIGESTUPDATE, rv); |
|
2784 pk11_return_session(state->sp, OP_DIGEST); |
|
2785 state->sp = NULL; |
|
2786 return (0); |
|
2787 } |
|
2788 |
|
2789 return (1); |
|
2790 } |
|
2791 |
|
2792 static int |
|
2793 pk11_digest_final(EVP_MD_CTX *ctx, unsigned char *md) |
|
2794 { |
|
2795 CK_RV rv; |
|
2796 unsigned long len; |
|
2797 PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data; |
|
2798 len = ctx->digest->md_size; |
|
2799 |
|
2800 if (state == NULL || state->sp == NULL) |
|
2801 return (0); |
|
2802 |
|
2803 rv = pFuncList->C_DigestFinal(state->sp->session, md, &len); |
|
2804 |
|
2805 if (rv != CKR_OK) |
|
2806 { |
|
2807 PK11err_add_data(PK11_F_DIGEST_FINAL, PK11_R_DIGESTFINAL, rv); |
|
2808 pk11_return_session(state->sp, OP_DIGEST); |
|
2809 state->sp = NULL; |
|
2810 return (0); |
|
2811 } |
|
2812 |
|
2813 if (ctx->digest->md_size != len) |
|
2814 return (0); |
|
2815 |
|
2816 /* |
|
2817 * Final is called and digest is returned, so return the session |
|
2818 * to the pool |
|
2819 */ |
|
2820 pk11_return_session(state->sp, OP_DIGEST); |
|
2821 state->sp = NULL; |
|
2822 |
|
2823 return (1); |
|
2824 } |
|
2825 |
|
2826 static int |
|
2827 pk11_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from) |
|
2828 { |
|
2829 CK_RV rv; |
|
2830 int ret = 0; |
|
2831 PK11_CIPHER_STATE *state, *state_to; |
|
2832 CK_BYTE_PTR pstate = NULL; |
|
2833 CK_ULONG ul_state_len; |
|
2834 |
|
2835 if (from->md_data == NULL || to->digest->ctx_size == 0) |
|
2836 return (1); |
|
2837 |
|
2838 /* The copy-from state */ |
|
2839 state = (PK11_CIPHER_STATE *) from->md_data; |
|
2840 if (state->sp == NULL) |
|
2841 goto err; |
|
2842 |
|
2843 /* Initialize the copy-to state */ |
|
2844 if (!pk11_digest_init(to)) |
|
2845 goto err; |
|
2846 state_to = (PK11_CIPHER_STATE *) to->md_data; |
|
2847 |
|
2848 /* Get the size of the operation state of the copy-from session */ |
|
2849 rv = pFuncList->C_GetOperationState(state->sp->session, NULL, |
|
2850 &ul_state_len); |
|
2851 |
|
2852 if (rv != CKR_OK) |
|
2853 { |
|
2854 PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE, |
|
2855 rv); |
|
2856 goto err; |
|
2857 } |
|
2858 if (ul_state_len == 0) |
|
2859 { |
|
2860 goto err; |
|
2861 } |
|
2862 |
|
2863 pstate = OPENSSL_malloc(ul_state_len); |
|
2864 if (pstate == NULL) |
|
2865 { |
|
2866 PK11err(PK11_F_DIGEST_COPY, PK11_R_MALLOC_FAILURE); |
|
2867 goto err; |
|
2868 } |
|
2869 |
|
2870 /* Get the operation state of the copy-from session */ |
|
2871 rv = pFuncList->C_GetOperationState(state->sp->session, pstate, |
|
2872 &ul_state_len); |
|
2873 |
|
2874 if (rv != CKR_OK) |
|
2875 { |
|
2876 PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE, |
|
2877 rv); |
|
2878 goto err; |
|
2879 } |
|
2880 |
|
2881 /* Set the operation state of the copy-to session */ |
|
2882 rv = pFuncList->C_SetOperationState(state_to->sp->session, pstate, |
|
2883 ul_state_len, 0, 0); |
|
2884 |
|
2885 if (rv != CKR_OK) |
|
2886 { |
|
2887 PK11err_add_data(PK11_F_DIGEST_COPY, |
|
2888 PK11_R_SET_OPERATION_STATE, rv); |
|
2889 goto err; |
|
2890 } |
|
2891 |
|
2892 ret = 1; |
|
2893 err: |
|
2894 if (pstate != NULL) |
|
2895 OPENSSL_free(pstate); |
|
2896 |
|
2897 return (ret); |
|
2898 } |
|
2899 |
|
2900 /* Return any pending session state to the pool */ |
|
2901 static int |
|
2902 pk11_digest_cleanup(EVP_MD_CTX *ctx) |
|
2903 { |
|
2904 PK11_CIPHER_STATE *state = ctx->md_data; |
|
2905 unsigned char buf[EVP_MAX_MD_SIZE]; |
|
2906 |
|
2907 if (state != NULL && state->sp != NULL) |
|
2908 { |
|
2909 /* |
|
2910 * If state->sp is not NULL then pk11_digest_final() has not |
|
2911 * been called yet. We must call it now to free any memory |
|
2912 * that might have been allocated in the token when |
|
2913 * pk11_digest_init() was called. pk11_digest_final() |
|
2914 * will return the session to the cache. |
|
2915 */ |
|
2916 if (!pk11_digest_final(ctx, buf)) |
|
2917 return (0); |
|
2918 } |
|
2919 |
|
2920 return (1); |
|
2921 } |
|
2922 |
|
2923 /* |
|
2924 * Check if the new key is the same as the key object in the session. If the key |
|
2925 * is the same, no need to create a new key object. Otherwise, the old key |
|
2926 * object needs to be destroyed and a new one will be created. Return 1 for |
|
2927 * cache hit, 0 for cache miss. Note that we must check the key length first |
|
2928 * otherwise we could end up reusing a different, longer key with the same |
|
2929 * prefix. |
|
2930 */ |
|
2931 static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key, |
|
2932 int key_len) |
|
2933 { |
|
2934 if (sp->opdata_key_len != key_len || |
|
2935 memcmp(sp->opdata_key, key, key_len) != 0) |
|
2936 { |
|
2937 (void) pk11_destroy_cipher_key_objects(sp); |
|
2938 return (0); |
|
2939 } |
|
2940 return (1); |
|
2941 } |
|
2942 |
|
2943 /* Destroy one or more secret key objects. */ |
|
2944 static int pk11_destroy_cipher_key_objects(PK11_SESSION *session) |
|
2945 { |
|
2946 int ret = 0; |
|
2947 PK11_SESSION *sp = NULL; |
|
2948 PK11_SESSION *local_free_session; |
|
2949 |
|
2950 if (session != NULL) |
|
2951 local_free_session = session; |
|
2952 else |
|
2953 { |
|
2954 (void) pthread_mutex_lock(session_cache[OP_CIPHER].lock); |
|
2955 local_free_session = session_cache[OP_CIPHER].head; |
|
2956 } |
|
2957 |
|
2958 while ((sp = local_free_session) != NULL) |
|
2959 { |
|
2960 local_free_session = sp->next; |
|
2961 |
|
2962 if (sp->opdata_cipher_key != CK_INVALID_HANDLE) |
|
2963 { |
|
2964 /* |
|
2965 * The secret key object is created in the |
|
2966 * global_session. See pk11_get_cipher_key(). |
|
2967 */ |
|
2968 if (pk11_destroy_object(global_session, |
|
2969 sp->opdata_cipher_key, CK_FALSE) == 0) |
|
2970 goto err; |
|
2971 sp->opdata_cipher_key = CK_INVALID_HANDLE; |
|
2972 } |
|
2973 } |
|
2974 ret = 1; |
|
2975 err: |
|
2976 |
|
2977 if (session == NULL) |
|
2978 (void) pthread_mutex_unlock(session_cache[OP_CIPHER].lock); |
|
2979 |
|
2980 return (ret); |
|
2981 } |
|
2982 |
|
2983 |
|
2984 /* |
|
2985 * Public key mechanisms optionally supported |
|
2986 * |
|
2987 * CKM_RSA_X_509 |
|
2988 * CKM_RSA_PKCS |
|
2989 * CKM_DSA |
|
2990 * |
|
2991 * The first slot that supports at least one of those mechanisms is chosen as a |
|
2992 * public key slot. |
|
2993 * |
|
2994 * Symmetric ciphers optionally supported |
|
2995 * |
|
2996 * CKM_DES3_CBC |
|
2997 * CKM_DES_CBC |
|
2998 * CKM_AES_CBC |
|
2999 * CKM_DES3_ECB |
|
3000 * CKM_DES_ECB |
|
3001 * CKM_AES_ECB |
|
3002 * CKM_AES_CTR |
|
3003 * CKM_RC4 |
|
3004 * CKM_BLOWFISH_CBC |
|
3005 * |
|
3006 * Digests optionally supported |
|
3007 * |
|
3008 * CKM_MD5 |
|
3009 * CKM_SHA_1 |
|
3010 * CKM_SHA224 |
|
3011 * CKM_SHA256 |
|
3012 * CKM_SHA384 |
|
3013 * CKM_SHA512 |
|
3014 * |
|
3015 * The output of this function is a set of global variables indicating which |
|
3016 * mechanisms from RSA, DSA, DH and RAND are present, and also two arrays of |
|
3017 * mechanisms, one for symmetric ciphers and one for digests. Also, 3 global |
|
3018 * variables carry information about which slot was chosen for (a) public key |
|
3019 * mechanisms, (b) random operations, and (c) symmetric ciphers and digests. |
|
3020 */ |
|
3021 static int |
|
3022 pk11_choose_slots(int *any_slot_found) |
|
3023 { |
|
3024 CK_SLOT_ID_PTR pSlotList = NULL_PTR; |
|
3025 CK_ULONG ulSlotCount = 0; |
|
3026 CK_MECHANISM_INFO mech_info; |
|
3027 CK_TOKEN_INFO token_info; |
|
3028 int i; |
|
3029 CK_RV rv; |
|
3030 CK_SLOT_ID best_slot_sofar; |
|
3031 CK_BBOOL found_candidate_slot = CK_FALSE; |
|
3032 int slot_n_cipher = 0; |
|
3033 int slot_n_digest = 0; |
|
3034 CK_SLOT_ID current_slot = 0; |
|
3035 int current_slot_n_cipher = 0; |
|
3036 int current_slot_n_digest = 0; |
|
3037 |
|
3038 int local_cipher_nids[PK11_CIPHER_MAX]; |
|
3039 int local_digest_nids[PK11_DIGEST_MAX]; |
|
3040 |
|
3041 /* let's initialize the output parameter */ |
|
3042 if (any_slot_found != NULL) |
|
3043 *any_slot_found = 0; |
|
3044 |
|
3045 /* Get slot list for memory allocation */ |
|
3046 rv = pFuncList->C_GetSlotList(CK_FALSE, NULL_PTR, &ulSlotCount); |
|
3047 |
|
3048 if (rv != CKR_OK) |
|
3049 { |
|
3050 PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv); |
|
3051 return (0); |
|
3052 } |
|
3053 |
|
3054 /* it's not an error if we didn't find any providers */ |
|
3055 if (ulSlotCount == 0) |
|
3056 { |
|
3057 DEBUG_SLOT_SEL("%s: no crypto providers found\n", PK11_DBG); |
|
3058 return (1); |
|
3059 } |
|
3060 |
|
3061 pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID)); |
|
3062 |
|
3063 if (pSlotList == NULL) |
|
3064 { |
|
3065 PK11err(PK11_F_CHOOSE_SLOT, PK11_R_MALLOC_FAILURE); |
|
3066 return (0); |
|
3067 } |
|
3068 |
|
3069 /* Get the slot list for processing */ |
|
3070 rv = pFuncList->C_GetSlotList(CK_FALSE, pSlotList, &ulSlotCount); |
|
3071 if (rv != CKR_OK) |
|
3072 { |
|
3073 PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv); |
|
3074 OPENSSL_free(pSlotList); |
|
3075 return (0); |
|
3076 } |
|
3077 |
|
3078 DEBUG_SLOT_SEL("%s: provider: %s\n", PK11_DBG, def_PK11_LIBNAME); |
|
3079 DEBUG_SLOT_SEL("%s: number of slots: %d\n", PK11_DBG, ulSlotCount); |
|
3080 |
|
3081 DEBUG_SLOT_SEL("%s: == checking rand slots ==\n", PK11_DBG); |
|
3082 for (i = 0; i < ulSlotCount; i++) |
|
3083 { |
|
3084 current_slot = pSlotList[i]; |
|
3085 |
|
3086 DEBUG_SLOT_SEL("%s: checking slot: %d\n", PK11_DBG, i); |
|
3087 /* Check if slot has random support. */ |
|
3088 rv = pFuncList->C_GetTokenInfo(current_slot, &token_info); |
|
3089 if (rv != CKR_OK) |
|
3090 continue; |
|
3091 |
|
3092 DEBUG_SLOT_SEL("%s: token label: %.32s\n", PK11_DBG, |
|
3093 token_info.label); |
|
3094 |
|
3095 if (token_info.flags & CKF_RNG) |
|
3096 { |
|
3097 DEBUG_SLOT_SEL( |
|
3098 "%s: this token has CKF_RNG flag\n", PK11_DBG); |
|
3099 pk11_have_random = CK_TRUE; |
|
3100 rand_SLOTID = current_slot; |
|
3101 break; |
|
3102 } |
|
3103 } |
|
3104 |
|
3105 DEBUG_SLOT_SEL("%s: == checking pubkey slots ==\n", PK11_DBG); |
|
3106 |
|
3107 pubkey_SLOTID = pSlotList[0]; |
|
3108 for (i = 0; i < ulSlotCount; i++) |
|
3109 { |
|
3110 CK_BBOOL slot_has_rsa = CK_FALSE; |
|
3111 CK_BBOOL slot_has_dsa = CK_FALSE; |
|
3112 CK_BBOOL slot_has_dh = CK_FALSE; |
|
3113 current_slot = pSlotList[i]; |
|
3114 |
|
3115 DEBUG_SLOT_SEL("%s: checking slot: %d\n", PK11_DBG, i); |
|
3116 rv = pFuncList->C_GetTokenInfo(current_slot, &token_info); |
|
3117 if (rv != CKR_OK) |
|
3118 continue; |
|
3119 |
|
3120 DEBUG_SLOT_SEL("%s: token label: %.32s\n", PK11_DBG, |
|
3121 token_info.label); |
|
3122 |
|
3123 #ifndef OPENSSL_NO_RSA |
|
3124 /* |
|
3125 * Check if this slot is capable of signing and |
|
3126 * verifying with CKM_RSA_PKCS. |
|
3127 */ |
|
3128 rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_RSA_PKCS, |
|
3129 &mech_info); |
|
3130 |
|
3131 if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) && |
|
3132 (mech_info.flags & CKF_VERIFY))) |
|
3133 { |
|
3134 /* |
|
3135 * Check if this slot is capable of encryption, |
|
3136 * decryption, sign, and verify with CKM_RSA_X_509. |
|
3137 */ |
|
3138 rv = pFuncList->C_GetMechanismInfo(current_slot, |
|
3139 CKM_RSA_X_509, &mech_info); |
|
3140 |
|
3141 if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) && |
|
3142 (mech_info.flags & CKF_VERIFY) && |
|
3143 (mech_info.flags & CKF_ENCRYPT) && |
|
3144 (mech_info.flags & CKF_VERIFY_RECOVER) && |
|
3145 (mech_info.flags & CKF_DECRYPT))) |
|
3146 { |
|
3147 slot_has_rsa = CK_TRUE; |
|
3148 } |
|
3149 } |
|
3150 #endif /* OPENSSL_NO_RSA */ |
|
3151 |
|
3152 #ifndef OPENSSL_NO_DSA |
|
3153 /* |
|
3154 * Check if this slot is capable of signing and |
|
3155 * verifying with CKM_DSA. |
|
3156 */ |
|
3157 rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_DSA, |
|
3158 &mech_info); |
|
3159 if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) && |
|
3160 (mech_info.flags & CKF_VERIFY))) |
|
3161 { |
|
3162 slot_has_dsa = CK_TRUE; |
|
3163 } |
|
3164 |
|
3165 #endif /* OPENSSL_NO_DSA */ |
|
3166 |
|
3167 #ifndef OPENSSL_NO_DH |
|
3168 /* |
|
3169 * Check if this slot is capable of DH key generataion and |
|
3170 * derivation. |
|
3171 */ |
|
3172 rv = pFuncList->C_GetMechanismInfo(current_slot, |
|
3173 CKM_DH_PKCS_KEY_PAIR_GEN, &mech_info); |
|
3174 |
|
3175 if (rv == CKR_OK && (mech_info.flags & CKF_GENERATE_KEY_PAIR)) |
|
3176 { |
|
3177 rv = pFuncList->C_GetMechanismInfo(current_slot, |
|
3178 CKM_DH_PKCS_DERIVE, &mech_info); |
|
3179 if (rv == CKR_OK && (mech_info.flags & CKF_DERIVE)) |
|
3180 { |
|
3181 slot_has_dh = CK_TRUE; |
|
3182 } |
|
3183 } |
|
3184 #endif /* OPENSSL_NO_DH */ |
|
3185 |
|
3186 if (!found_candidate_slot && |
|
3187 (slot_has_rsa || slot_has_dsa || slot_has_dh)) |
|
3188 { |
|
3189 DEBUG_SLOT_SEL( |
|
3190 "%s: potential slot: %d\n", PK11_DBG, current_slot); |
|
3191 best_slot_sofar = current_slot; |
|
3192 pk11_have_rsa = slot_has_rsa; |
|
3193 pk11_have_dsa = slot_has_dsa; |
|
3194 pk11_have_dh = slot_has_dh; |
|
3195 found_candidate_slot = CK_TRUE; |
|
3196 /* |
|
3197 * Cache the flags for later use. We might need those if |
|
3198 * RSA keys by reference feature is used. |
|
3199 */ |
|
3200 pubkey_token_flags = token_info.flags; |
|
3201 DEBUG_SLOT_SEL( |
|
3202 "%s: setting found_candidate_slot to CK_TRUE\n", |
|
3203 PK11_DBG); |
|
3204 DEBUG_SLOT_SEL("%s: best slot so far: %d\n", PK11_DBG, |
|
3205 best_slot_sofar); |
|
3206 DEBUG_SLOT_SEL("%s: pubkey flags changed to " |
|
3207 "%lu.\n", PK11_DBG, pubkey_token_flags); |
|
3208 } |
|
3209 else |
|
3210 { |
|
3211 DEBUG_SLOT_SEL("%s: no rsa/dsa/dh\n", PK11_DBG); |
|
3212 } |
|
3213 } /* for */ |
|
3214 |
|
3215 if (found_candidate_slot == CK_TRUE) |
|
3216 { |
|
3217 pubkey_SLOTID = best_slot_sofar; |
|
3218 } |
|
3219 |
|
3220 found_candidate_slot = CK_FALSE; |
|
3221 best_slot_sofar = 0; |
|
3222 |
|
3223 DEBUG_SLOT_SEL("%s: == checking cipher/digest ==\n", PK11_DBG); |
|
3224 |
|
3225 SLOTID = pSlotList[0]; |
|
3226 for (i = 0; i < ulSlotCount; i++) |
|
3227 { |
|
3228 DEBUG_SLOT_SEL("%s: checking slot: %d\n", PK11_DBG, i); |
|
3229 |
|
3230 current_slot = pSlotList[i]; |
|
3231 current_slot_n_cipher = 0; |
|
3232 current_slot_n_digest = 0; |
|
3233 (void) memset(local_cipher_nids, 0, sizeof (local_cipher_nids)); |
|
3234 (void) memset(local_digest_nids, 0, sizeof (local_digest_nids)); |
|
3235 |
|
3236 pk11_find_symmetric_ciphers(pFuncList, current_slot, |
|
3237 ¤t_slot_n_cipher, local_cipher_nids); |
|
3238 |
|
3239 pk11_find_digests(pFuncList, current_slot, |
|
3240 ¤t_slot_n_digest, local_digest_nids); |
|
3241 |
|
3242 DEBUG_SLOT_SEL("%s: current_slot_n_cipher %d\n", PK11_DBG, |
|
3243 current_slot_n_cipher); |
|
3244 DEBUG_SLOT_SEL("%s: current_slot_n_digest %d\n", PK11_DBG, |
|
3245 current_slot_n_digest); |
|
3246 DEBUG_SLOT_SEL("%s: best cipher/digest slot so far: %d\n", |
|
3247 PK11_DBG, best_slot_sofar); |
|
3248 |
|
3249 /* |
|
3250 * If the current slot supports more ciphers/digests than |
|
3251 * the previous best one we change the current best to this one, |
|
3252 * otherwise leave it where it is. |
|
3253 */ |
|
3254 if ((current_slot_n_cipher + current_slot_n_digest) > |
|
3255 (slot_n_cipher + slot_n_digest)) |
|
3256 { |
|
3257 DEBUG_SLOT_SEL("%s: changing best slot to %d\n", |
|
3258 PK11_DBG, current_slot); |
|
3259 best_slot_sofar = SLOTID = current_slot; |
|
3260 cipher_count = slot_n_cipher = current_slot_n_cipher; |
|
3261 digest_count = slot_n_digest = current_slot_n_digest; |
|
3262 (void) memcpy(cipher_nids, local_cipher_nids, |
|
3263 sizeof (local_cipher_nids)); |
|
3264 (void) memcpy(digest_nids, local_digest_nids, |
|
3265 sizeof (local_digest_nids)); |
|
3266 } |
|
3267 } |
|
3268 |
|
3269 DEBUG_SLOT_SEL("%s: chosen pubkey slot: %d\n", PK11_DBG, pubkey_SLOTID); |
|
3270 DEBUG_SLOT_SEL("%s: chosen rand slot: %d\n", PK11_DBG, rand_SLOTID); |
|
3271 DEBUG_SLOT_SEL("%s: chosen cipher/digest slot: %d\n", PK11_DBG, SLOTID); |
|
3272 DEBUG_SLOT_SEL("%s: pk11_have_rsa %d\n", PK11_DBG, pk11_have_rsa); |
|
3273 DEBUG_SLOT_SEL("%s: pk11_have_dsa %d\n", PK11_DBG, pk11_have_dsa); |
|
3274 DEBUG_SLOT_SEL("%s: pk11_have_dh %d\n", PK11_DBG, pk11_have_dh); |
|
3275 DEBUG_SLOT_SEL("%s: pk11_have_random %d\n", PK11_DBG, pk11_have_random); |
|
3276 DEBUG_SLOT_SEL("%s: cipher_count %d\n", PK11_DBG, cipher_count); |
|
3277 DEBUG_SLOT_SEL("%s: digest_count %d\n", PK11_DBG, digest_count); |
|
3278 |
|
3279 if (pSlotList != NULL) |
|
3280 OPENSSL_free(pSlotList); |
|
3281 |
|
3282 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
3283 OPENSSL_free(hw_cnids); |
|
3284 OPENSSL_free(hw_dnids); |
|
3285 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
3286 |
|
3287 if (any_slot_found != NULL) |
|
3288 *any_slot_found = 1; |
|
3289 return (1); |
|
3290 } |
|
3291 |
|
3292 static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR pflist, |
|
3293 int slot_id, int *current_slot_n_cipher, int *local_cipher_nids, |
|
3294 PK11_CIPHER *cipher) |
|
3295 { |
|
3296 CK_MECHANISM_INFO mech_info; |
|
3297 CK_RV rv; |
|
3298 |
|
3299 DEBUG_SLOT_SEL("%s: checking mech: %x", PK11_DBG, cipher->mech_type); |
|
3300 rv = pflist->C_GetMechanismInfo(slot_id, cipher->mech_type, &mech_info); |
|
3301 |
|
3302 if (rv != CKR_OK) |
|
3303 { |
|
3304 DEBUG_SLOT_SEL(" not found\n"); |
|
3305 return; |
|
3306 } |
|
3307 |
|
3308 if ((mech_info.flags & CKF_ENCRYPT) && |
|
3309 (mech_info.flags & CKF_DECRYPT)) |
|
3310 { |
|
3311 if (mech_info.ulMinKeySize > cipher->min_key_len || |
|
3312 mech_info.ulMaxKeySize < cipher->max_key_len) |
|
3313 { |
|
3314 DEBUG_SLOT_SEL(" engine key size range <%i-%i> does not" |
|
3315 " match mech range <%lu-%lu>\n", |
|
3316 cipher->min_key_len, cipher->max_key_len, |
|
3317 mech_info.ulMinKeySize, mech_info.ulMaxKeySize); |
|
3318 return; |
|
3319 } |
|
3320 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
3321 if (nid_in_table(cipher->nid, hw_cnids)) |
|
3322 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
3323 { |
|
3324 DEBUG_SLOT_SEL(" usable\n"); |
|
3325 local_cipher_nids[(*current_slot_n_cipher)++] = |
|
3326 cipher->nid; |
|
3327 } |
|
3328 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
3329 else |
|
3330 { |
|
3331 DEBUG_SLOT_SEL( |
|
3332 " rejected, software implementation only\n"); |
|
3333 } |
|
3334 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
3335 } |
|
3336 else |
|
3337 { |
|
3338 DEBUG_SLOT_SEL(" unusable\n"); |
|
3339 } |
|
3340 |
|
3341 return; |
|
3342 } |
|
3343 |
|
3344 static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id, |
|
3345 int *current_slot_n_digest, int *local_digest_nids, PK11_DIGEST *digest) |
|
3346 { |
|
3347 CK_MECHANISM_INFO mech_info; |
|
3348 CK_RV rv; |
|
3349 |
|
3350 DEBUG_SLOT_SEL("%s: checking mech: %x", PK11_DBG, digest->mech_type); |
|
3351 rv = pflist->C_GetMechanismInfo(slot_id, digest->mech_type, &mech_info); |
|
3352 |
|
3353 if (rv != CKR_OK) |
|
3354 { |
|
3355 DEBUG_SLOT_SEL(" not found\n"); |
|
3356 return; |
|
3357 } |
|
3358 |
|
3359 if (mech_info.flags & CKF_DIGEST) |
|
3360 { |
|
3361 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
3362 if (nid_in_table(digest->nid, hw_dnids)) |
|
3363 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
3364 { |
|
3365 DEBUG_SLOT_SEL(" usable\n"); |
|
3366 local_digest_nids[(*current_slot_n_digest)++] = |
|
3367 digest->nid; |
|
3368 } |
|
3369 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
3370 else |
|
3371 { |
|
3372 DEBUG_SLOT_SEL( |
|
3373 " rejected, software implementation only\n"); |
|
3374 } |
|
3375 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
3376 } |
|
3377 else |
|
3378 { |
|
3379 DEBUG_SLOT_SEL(" unusable\n"); |
|
3380 } |
|
3381 |
|
3382 return; |
|
3383 } |
|
3384 |
|
3385 /* Find what symmetric ciphers this slot supports. */ |
|
3386 static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist, |
|
3387 CK_SLOT_ID current_slot, int *current_slot_n_cipher, int *local_cipher_nids) |
|
3388 { |
|
3389 int i; |
|
3390 |
|
3391 for (i = 0; i < PK11_CIPHER_MAX; ++i) |
|
3392 { |
|
3393 pk11_get_symmetric_cipher(pflist, current_slot, |
|
3394 current_slot_n_cipher, local_cipher_nids, &ciphers[i]); |
|
3395 } |
|
3396 } |
|
3397 |
|
3398 /* Find what digest algorithms this slot supports. */ |
|
3399 static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist, |
|
3400 CK_SLOT_ID current_slot, int *current_slot_n_digest, int *local_digest_nids) |
|
3401 { |
|
3402 int i; |
|
3403 |
|
3404 for (i = 0; i < PK11_DIGEST_MAX; ++i) |
|
3405 { |
|
3406 pk11_get_digest(pflist, current_slot, current_slot_n_digest, |
|
3407 local_digest_nids, &digests[i]); |
|
3408 } |
|
3409 } |
|
3410 |
|
3411 #ifdef SOLARIS_HW_SLOT_SELECTION |
|
3412 /* |
|
3413 * It would be great if we could use pkcs11_kernel directly since this library |
|
3414 * offers hardware slots only. That's the easiest way to achieve the situation |
|
3415 * where we use the hardware accelerators when present and OpenSSL native code |
|
3416 * otherwise. That presumes the fact that OpenSSL native code is faster than the |
|
3417 * code in the soft token. It's a logical assumption - Crypto Framework has some |
|
3418 * inherent overhead so going there for the software implementation of a |
|
3419 * mechanism should be logically slower in contrast to the OpenSSL native code, |
|
3420 * presuming that both implementations are of similar speed. For example, the |
|
3421 * soft token for AES is roughly three times slower than OpenSSL for 64 byte |
|
3422 * blocks and still 20% slower for 8KB blocks. So, if we want to ship products |
|
3423 * that use the PKCS#11 engine by default, we must somehow avoid that regression |
|
3424 * on machines without hardware acceleration. That's why switching to the |
|
3425 * pkcs11_kernel library seems like a very good idea. |
|
3426 * |
|
3427 * The problem is that OpenSSL built with SunStudio is roughly 2x slower for |
|
3428 * asymmetric operations (RSA/DSA/DH) than the soft token built with the same |
|
3429 * compiler. That means that if we switched to pkcs11_kernel from the libpkcs11 |
|
3430 * library, we would have had a performance regression on machines without |
|
3431 * hardware acceleration for asymmetric operations for all applications that use |
|
3432 * the PKCS#11 engine. There is one such application - Apache web server since |
|
3433 * it's shipped configured to use the PKCS#11 engine by default. Having said |
|
3434 * that, we can't switch to the pkcs11_kernel library now and have to come with |
|
3435 * a solution that, on non-accelerated machines, uses the OpenSSL native code |
|
3436 * for all symmetric ciphers and digests while it uses the soft token for |
|
3437 * asymmetric operations. |
|
3438 * |
|
3439 * This is the idea: dlopen() pkcs11_kernel directly and find out what |
|
3440 * mechanisms are there. We don't care about duplications (more slots can |
|
3441 * support the same mechanism), we just want to know what mechanisms can be |
|
3442 * possibly supported in hardware on that particular machine. As said before, |
|
3443 * pkcs11_kernel will show you hardware providers only. |
|
3444 * |
|
3445 * Then, we rely on the fact that since we use libpkcs11 library we will find |
|
3446 * the metaslot. When we go through the metaslot's mechanisms for symmetric |
|
3447 * ciphers and digests, we check that any found mechanism is in the table |
|
3448 * created using the pkcs11_kernel library. So, as a result we have two arrays |
|
3449 * of mechanisms that were advertised as supported in hardware which was the |
|
3450 * goal of that whole exercise. Thus, we can use libpkcs11 but avoid soft token |
|
3451 * code for symmetric ciphers and digests. See pk11_choose_slots() for more |
|
3452 * information. |
|
3453 * |
|
3454 * This is Solaris specific code, if SOLARIS_HW_SLOT_SELECTION is not defined |
|
3455 * the code won't be used. |
|
3456 */ |
|
3457 #if defined(__sparcv9) || defined(__x86_64) || defined(__amd64) |
|
3458 static const char pkcs11_kernel[] = "/usr/lib/security/64/pkcs11_kernel.so.1"; |
|
3459 #else |
|
3460 static const char pkcs11_kernel[] = "/usr/lib/security/pkcs11_kernel.so.1"; |
|
3461 #endif |
|
3462 |
|
3463 /* |
|
3464 * Check hardware capabilities of the machines. The output are two lists, |
|
3465 * hw_cnids and hw_dnids, that contain hardware mechanisms found in all hardware |
|
3466 * providers together. They are not sorted and may contain duplicate mechanisms. |
|
3467 */ |
|
3468 static int check_hw_mechanisms(void) |
|
3469 { |
|
3470 int i; |
|
3471 CK_RV rv; |
|
3472 void *handle; |
|
3473 CK_C_GetFunctionList p; |
|
3474 CK_TOKEN_INFO token_info; |
|
3475 CK_ULONG ulSlotCount = 0; |
|
3476 int n_cipher = 0, n_digest = 0; |
|
3477 CK_FUNCTION_LIST_PTR pflist = NULL; |
|
3478 CK_SLOT_ID_PTR pSlotList = NULL_PTR; |
|
3479 int *tmp_hw_cnids = NULL, *tmp_hw_dnids = NULL; |
|
3480 int hw_ctable_size, hw_dtable_size; |
|
3481 |
|
3482 DEBUG_SLOT_SEL("%s: SOLARIS_HW_SLOT_SELECTION code running\n", |
|
3483 PK11_DBG); |
|
3484 /* |
|
3485 * Use RTLD_GROUP to limit the pkcs11_kernel provider to its own |
|
3486 * symbols, which prevents it from mistakenly accessing C_* functions |
|
3487 * from the top-level PKCS#11 library. |
|
3488 */ |
|
3489 if ((handle = dlopen(pkcs11_kernel, RTLD_LAZY | RTLD_GROUP)) == NULL) |
|
3490 { |
|
3491 PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE); |
|
3492 goto err; |
|
3493 } |
|
3494 |
|
3495 if ((p = (CK_C_GetFunctionList)dlsym(handle, |
|
3496 PK11_GET_FUNCTION_LIST)) == NULL) |
|
3497 { |
|
3498 PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE); |
|
3499 goto err; |
|
3500 } |
|
3501 |
|
3502 /* get the full function list from the loaded library */ |
|
3503 if (p(&pflist) != CKR_OK) |
|
3504 { |
|
3505 PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE); |
|
3506 goto err; |
|
3507 } |
|
3508 |
|
3509 rv = pflist->C_Initialize(NULL_PTR); |
|
3510 if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) |
|
3511 { |
|
3512 PK11err_add_data(PK11_F_CHECK_HW_MECHANISMS, |
|
3513 PK11_R_INITIALIZE, rv); |
|
3514 goto err; |
|
3515 } |
|
3516 |
|
3517 if (pflist->C_GetSlotList(0, NULL_PTR, &ulSlotCount) != CKR_OK) |
|
3518 { |
|
3519 PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST); |
|
3520 goto err; |
|
3521 } |
|
3522 |
|
3523 /* no slots, set the hw mechanism tables as empty */ |
|
3524 if (ulSlotCount == 0) |
|
3525 { |
|
3526 DEBUG_SLOT_SEL("%s: no hardware mechanisms found\n", PK11_DBG); |
|
3527 hw_cnids = OPENSSL_malloc(sizeof (int)); |
|
3528 hw_dnids = OPENSSL_malloc(sizeof (int)); |
|
3529 if (hw_cnids == NULL || hw_dnids == NULL) |
|
3530 { |
|
3531 PK11err(PK11_F_CHECK_HW_MECHANISMS, |
|
3532 PK11_R_MALLOC_FAILURE); |
|
3533 return (0); |
|
3534 } |
|
3535 /* this means empty tables */ |
|
3536 hw_cnids[0] = NID_undef; |
|
3537 hw_dnids[0] = NID_undef; |
|
3538 return (1); |
|
3539 } |
|
3540 |
|
3541 pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID)); |
|
3542 if (pSlotList == NULL) |
|
3543 { |
|
3544 PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE); |
|
3545 goto err; |
|
3546 } |
|
3547 |
|
3548 /* Get the slot list for processing */ |
|
3549 if (pflist->C_GetSlotList(0, pSlotList, &ulSlotCount) != CKR_OK) |
|
3550 { |
|
3551 PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST); |
|
3552 goto err; |
|
3553 } |
|
3554 |
|
3555 /* |
|
3556 * We don't care about duplicate mechanisms in multiple slots and also |
|
3557 * reserve one slot for the terminal NID_undef which we use to stop the |
|
3558 * search. |
|
3559 */ |
|
3560 hw_ctable_size = ulSlotCount * PK11_CIPHER_MAX + 1; |
|
3561 hw_dtable_size = ulSlotCount * PK11_DIGEST_MAX + 1; |
|
3562 tmp_hw_cnids = OPENSSL_malloc(hw_ctable_size * sizeof (int)); |
|
3563 tmp_hw_dnids = OPENSSL_malloc(hw_dtable_size * sizeof (int)); |
|
3564 if (tmp_hw_cnids == NULL || tmp_hw_dnids == NULL) |
|
3565 { |
|
3566 PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE); |
|
3567 goto err; |
|
3568 } |
|
3569 |
|
3570 /* |
|
3571 * Do not use memset since we should not rely on the fact that NID_undef |
|
3572 * is zero now. |
|
3573 */ |
|
3574 for (i = 0; i < hw_ctable_size; ++i) |
|
3575 tmp_hw_cnids[i] = NID_undef; |
|
3576 for (i = 0; i < hw_dtable_size; ++i) |
|
3577 tmp_hw_dnids[i] = NID_undef; |
|
3578 |
|
3579 DEBUG_SLOT_SEL("%s: provider: %s\n", PK11_DBG, pkcs11_kernel); |
|
3580 DEBUG_SLOT_SEL("%s: found %d hardware slots\n", PK11_DBG, ulSlotCount); |
|
3581 DEBUG_SLOT_SEL("%s: now looking for mechs supported in hw\n", |
|
3582 PK11_DBG); |
|
3583 |
|
3584 for (i = 0; i < ulSlotCount; i++) |
|
3585 { |
|
3586 if (pflist->C_GetTokenInfo(pSlotList[i], &token_info) != CKR_OK) |
|
3587 continue; |
|
3588 |
|
3589 DEBUG_SLOT_SEL("%s: token label: %.32s\n", PK11_DBG, |
|
3590 token_info.label); |
|
3591 |
|
3592 /* |
|
3593 * We are filling the hw mech tables here. Global tables are |
|
3594 * still NULL so all mechanisms are put into tmp tables. |
|
3595 */ |
|
3596 pk11_find_symmetric_ciphers(pflist, pSlotList[i], |
|
3597 &n_cipher, tmp_hw_cnids); |
|
3598 pk11_find_digests(pflist, pSlotList[i], |
|
3599 &n_digest, tmp_hw_dnids); |
|
3600 } |
|
3601 |
|
3602 /* |
|
3603 * Since we are part of a library (libcrypto.so), calling this function |
|
3604 * may have side-effects. Also, C_Finalize() is triggered by |
|
3605 * dlclose(3C). |
|
3606 */ |
|
3607 #if 0 |
|
3608 pflist->C_Finalize(NULL); |
|
3609 #endif |
|
3610 OPENSSL_free(pSlotList); |
|
3611 (void) dlclose(handle); |
|
3612 hw_cnids = tmp_hw_cnids; |
|
3613 hw_dnids = tmp_hw_dnids; |
|
3614 |
|
3615 DEBUG_SLOT_SEL("%s: hw mechs check complete\n", PK11_DBG); |
|
3616 return (1); |
|
3617 |
|
3618 err: |
|
3619 if (pSlotList != NULL) |
|
3620 OPENSSL_free(pSlotList); |
|
3621 if (tmp_hw_cnids != NULL) |
|
3622 OPENSSL_free(tmp_hw_cnids); |
|
3623 if (tmp_hw_dnids != NULL) |
|
3624 OPENSSL_free(tmp_hw_dnids); |
|
3625 |
|
3626 return (0); |
|
3627 } |
|
3628 |
|
3629 /* |
|
3630 * Check presence of a NID in the table of NIDs unless the mechanism is |
|
3631 * supported directly in a CPU instruction set. The table may be NULL (i.e., |
|
3632 */ |
|
3633 static int nid_in_table(int nid, int *nid_table) |
|
3634 { |
|
3635 int i = 0; |
|
3636 |
|
3637 /* |
|
3638 * Special case first. NULL means that we are initializing a new table. |
|
3639 */ |
|
3640 if (nid_table == NULL) |
|
3641 return (1); |
|
3642 |
|
3643 /* |
|
3644 * the table is never full, there is always at least one |
|
3645 * NID_undef. |
|
3646 */ |
|
3647 while (nid_table[i] != NID_undef) |
|
3648 { |
|
3649 if (nid_table[i++] == nid) |
|
3650 { |
|
3651 DEBUG_SLOT_SEL(" (NID %d in hw table, idx %d)", nid, i); |
|
3652 return (1); |
|
3653 } |
|
3654 } |
|
3655 |
|
3656 return (0); |
|
3657 } |
|
3658 #endif /* SOLARIS_HW_SLOT_SELECTION */ |
|
3659 |
|
3660 #endif /* OPENSSL_NO_HW_PK11 */ |
|
3661 #endif /* OPENSSL_NO_HW */ |
|