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