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