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