Debian Codegraph @

usr/lib/ep11_stdll/ep11_specific.h

raw
1 /*
2 * COPYRIGHT (c) International Business Machines Corp. 2015-2023
3 *
4 * This program is provided under the terms of the Common Public License,
5 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7 * found in the file LICENSE file or at
8 * https://opensource.org/licenses/cpl1.0.php
9 */
10
11 /*
12 * OpenCryptoki EP11 token - EP11 token functions
13 *
14 */
15
16 #ifndef EP11_SPECIFIC_H
17 #define EP11_SPECIFIC_H
18
19 #include "ep11_func.h"
20 #include "configuration.h"
21
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
24 #include <ica_api.h>
25 #pragma GCC diagnostic pop
26
27 #include <pthread.h>
28
29 #define EP11SHAREDLIB_NAME "OCK_EP11_LIBRARY"
30 #define EP11SHAREDLIB_V4 "libep11.so.4"
31 #define EP11SHAREDLIB_V3 "libep11.so.3"
32 #define EP11SHAREDLIB_V2 "libep11.so.2"
33 #define EP11SHAREDLIB_V1 "libep11.so.1"
34 #define EP11SHAREDLIB "libep11.so"
35
36 #define EP11_DEFAULT_CFG_FILE "ep11tok.conf"
37 #define EP11_DEFAULT_CPFILTER_FILE "ep11cpfilter.conf"
38
39 /* largest blobsize ever seen is about 5k (for 4096 mod bits RSA keys) */
40 /* Attribute bound keys can be larger */
41 #define MAX_BLOBSIZE (8192 * 2)
42 #define MAX_CSUMSIZE 64
43 #define EP11_CSUMSIZE 3
44 #define MAX_DIGEST_STATE_BYTES 1024
45 #define MAX_CRYPT_STATE_BYTES 12288
46 #define MAX_SIGN_STATE_BYTES 12288
47 #define MAX_APQN 256
48 #define EP11_BLOB_WKID_OFFSET 32
49 #define EP11_BLOB_BOOL_ATTR_OFFSET 52 /* second 32 bits of bool attr field */
50 #define EP11_BLOB_VERSION_OFFSET 64
51 #define EP11_BLOB_VERSION 0x1234
52
53 typedef struct {
54 uint32_t version : 4,
55 reserved : 4,
56 num_int_attrs : 8,
57 num_varlen_attrs : 4,
58 wordcount_varlen_attrs : 12;
59 } __attribute__ ((packed)) blob_attr_header_t;
60
61 #define EP11_BLOB_ATTR_HDR_VERSION 1
62
63 typedef struct cp_mech_config {
64 CK_MECHANISM_TYPE mech;
65 struct cp_mech_config *next;
66 } cp_mech_config_t;
67
68 typedef struct cp_config {
69 unsigned long int cp;
70 cp_mech_config_t *mech;
71 struct cp_config *next;
72 } cp_config_t;
73
74 #define MAX_RETRY_COUNT 100
75
76 typedef struct ep11_card_version {
77 struct ep11_card_version *next;
78 CK_ULONG card_type;
79 CK_VERSION firmware_version;
80 CK_ULONG firmware_API_version;
81 } ep11_card_version_t;
82
83 typedef struct {
84 const CK_VERSION *min_lib_version;
85 const CK_VERSION *min_firmware_version;
86 const CK_ULONG *min_firmware_API_version;
87 CK_ULONG card_type;
88 CK_ULONG apply_to_firmware_version_only;
89 } version_req_t;
90
91 typedef CK_CHAR ep11_serialno_t[16];
92
93 typedef struct {
94 SESSION *session;
95 CK_BYTE session_id[SHA256_HASH_SIZE];
96 CK_BBOOL pin_blob_valid;
97 CK_BYTE session_pin_blob[XCP_PINBLOB_BYTES];
98 CK_OBJECT_HANDLE session_object;
99 ep11_serialno_t *serial_numbers;
100 CK_ULONG num_serial_numbers;
101 } ep11_session_t;
102
103
104 typedef struct {
105 STDLL_TokData_t *tokdata;
106 ep11_session_t *ep11_session;
107 CK_BBOOL relogin;
108 } login_logout_data_t;
109
110 typedef struct {
111 CK_SLOT_ID_32 slot_id; /* Slot number as BE32 */
112 CK_BYTE purpose[12];
113 } __attribute__ ((packed)) session_nonce_t;
114
115 #define DEFAULT_EP11_PIN " "
116
117 #define VHSM_NONCE_PURPOSE "VHSM-session"
118 #define FIPS_NONCE_PURPOSE "FIPS-session"
119
120 #define CKH_IBM_EP11_SESSION CKH_VENDOR_DEFINED + 1
121 #define CKH_IBM_EP11_VHSMPIN CKH_VENDOR_DEFINED + 2
122 #define CKH_IBM_EP11_FIPSPIN CKH_VENDOR_DEFINED + 3
123
124 #define EP11_PINBLOB_MARKER_OFS 4
125
126 #define EP11_PINBLOB_V0_MARKER 0xaa
127 #define EP11_PINBLOB_V1_MARKER 0xab
128
129 #define PUBLIC_SESSION_ID_LENGTH 16
130
131 #define CKF_EP11_HELPER_SESSION 0x80000000
132
133 /* Definitions for loading libica dynamically */
134 #define ICASHAREDLIB_V4 "libica.so.4"
135 #define ICASHAREDLIB_V3 "libica.so.3"
136
137 typedef unsigned int (*ica_sha1_t)(unsigned int message_part,
138 unsigned int input_length,
139 unsigned char *input_data,
140 sha_context_t *sha_context,
141 unsigned char *output_data);
142
143 typedef unsigned int (*ica_sha224_t)(unsigned int message_part,
144 unsigned int input_length,
145 unsigned char *input_data,
146 sha256_context_t *sha_context,
147 unsigned char *output_data);
148
149 typedef unsigned int (*ica_sha256_t)(unsigned int message_part,
150 unsigned int input_length,
151 unsigned char *input_data,
152 sha256_context_t *sha_context,
153 unsigned char *output_data);
154
155 typedef unsigned int (*ica_sha384_t)(unsigned int message_part,
156 unsigned int input_length,
157 unsigned char *input_data,
158 sha512_context_t *sha_context,
159 unsigned char *output_data);
160
161 typedef unsigned int (*ica_sha512_t)(unsigned int message_part,
162 unsigned int input_length,
163 unsigned char *input_data,
164 sha512_context_t *sha_context,
165 unsigned char *output_data);
166
167 typedef unsigned int (*ica_sha512_224_t)(unsigned int message_part,
168 unsigned int input_length,
169 unsigned char *input_data,
170 sha512_context_t *sha_context,
171 unsigned char *output_data);
172
173 typedef unsigned int (*ica_sha512_256_t)(unsigned int message_part,
174 unsigned int input_length,
175 unsigned char *input_data,
176 sha512_context_t *sha_context,
177 unsigned char *output_data);
178
179 #ifdef SHA3_224
180 typedef unsigned int (*ica_sha3_224_t)(unsigned int message_part,
181 unsigned int input_length,
182 unsigned char *input_data,
183 sha3_224_context_t *sha3_224_context,
184 unsigned char *output_data);
185
186 typedef unsigned int (*ica_sha3_256_t)(unsigned int message_part,
187 unsigned int input_length,
188 unsigned char *input_data,
189 sha3_256_context_t *sha3_256_context,
190 unsigned char *output_data);
191
192 typedef unsigned int (*ica_sha3_384_t)(unsigned int message_part,
193 uint64_t input_length,
194 unsigned char *input_data,
195 sha3_384_context_t *sha3_384_context,
196 unsigned char *output_data);
197
198 typedef unsigned int (*ica_sha3_512_t)(unsigned int message_part,
199 uint64_t input_length,
200 unsigned char *input_data,
201 sha3_512_context_t *sha3_512_context,
202 unsigned char *output_data);
203 #endif
204 typedef void (*ica_cleanup_t) (void);
205 typedef int (*ica_fips_status_t) (void);
206
207 typedef struct {
208 CK_BYTE buffer[MAX_SHA_BLOCK_SIZE];
209 CK_ULONG block_size;
210 CK_ULONG offset;
211 CK_BBOOL first;
212 union {
213 sha_context_t sha1;
214 sha256_context_t sha256;
215 sha512_context_t sha512;
216 #ifdef SHA3_224
217 sha3_224_context_t sha3_224;
218 sha3_256_context_t sha3_256;
219 sha3_384_context_t sha3_384;
220 sha3_512_context_t sha3_512;
221 #endif
222 } ctx;
223 } libica_sha_context_t;
224
225 typedef struct {
226 void *library;
227 ica_sha1_t ica_sha1;
228 ica_sha224_t ica_sha224;
229 ica_sha256_t ica_sha256;
230 ica_sha384_t ica_sha384;
231 ica_sha512_t ica_sha512;
232 ica_sha512_224_t ica_sha512_224;
233 ica_sha512_256_t ica_sha512_256;
234 #ifdef SHA3_224
235 ica_sha3_224_t ica_sha3_224;
236 ica_sha3_256_t ica_sha3_256;
237 ica_sha3_384_t ica_sha3_384;
238 ica_sha3_512_t ica_sha3_512;
239 #endif
240 ica_cleanup_t ica_cleanup;
241 ica_fips_status_t ica_fips_status;
242 } libica_t;
243
244 /*
245 * target list of adapters/domains, specified in a config file by user,
246 * tells the device driver which adapter/domain pairs should be used,
247 * they must have the same master key
248 */
249 typedef struct {
250 short format;
251 short length;
252 short apqns[2 * MAX_APQN];
253 } __attribute__ ((packed)) ep11_target_t;
254
255 /* EP11 token private data */
256 #define PKEY_MK_VP_LENGTH 32
257
258 #define PKEY_MODE_DISABLED 0
259 #define PKEY_MODE_DEFAULT 1
260 #define PKEY_MODE_ENABLE4NONEXTR 2
261 #define PKEY_MODE_ENABLE4EXTR 3
262 #define PKEY_MODE_ENABLE4ALL 4
263
264 #define PQC_BYTE_NO(idx) (((idx) - 1) / 8)
265 #define PQC_BIT_IN_BYTE(idx) (((idx - 1)) % 8)
266 #define PQC_BIT_MASK(idx) (0x80 >> PQC_BIT_IN_BYTE(idx))
267 #define PQC_BYTES ((((XCP_PQC_MAX / 32) * 32) + 32) / 8)
268
269 typedef struct {
270 volatile unsigned long ref_count;
271 target_t target;
272 ep11_card_version_t *card_versions;
273 CK_ULONG used_firmware_API_version;
274 unsigned char control_points[XCP_CP_BYTES];
275 size_t control_points_len;
276 size_t max_control_point_index;
277 CK_CHAR serialNumber[16];
278 CK_BYTE pqc_strength[PQC_BYTES];
279 int single_apqn;
280 uint_32 adapter; /* set if single_apqn = 1 */
281 uint_32 domain; /* set if single_apqn = 1 */
282 volatile int single_apqn_has_new_wk;
283 } ep11_target_info_t;
284
285 typedef struct {
286 char token_config_filename[PATH_MAX];
287 ep11_target_t target_list;
288 CK_BYTE raw2key_wrap_blob[MAX_BLOBSIZE];
289 CK_BYTE raw2key_wrap_blob_reenc[MAX_BLOBSIZE];
290 size_t raw2key_wrap_blob_l;
291 pthread_mutex_t raw2key_wrap_blob_mutex;
292 int cka_sensitive_default_true;
293 char cp_filter_config_filename[PATH_MAX];
294 cp_config_t *cp_config;
295 int strict_mode;
296 int vhsm_mode;
297 int fips_session_mode;
298 int optimize_single_ops;
299 int pkey_mode;
300 volatile int pkey_combined_extract_supported;
301 volatile int pkey_wrap_supported;
302 int pkey_wrap_support_checked;
303 char pkey_mk_vp[PKEY_MK_VP_LENGTH];
304 pthread_mutex_t pkey_mutex;
305 int msa_level;
306 int digest_libica;
307 char digest_libica_path[PATH_MAX];
308 unsigned char expected_wkvp[XCP_WKID_BYTES];
309 int expected_wkvp_set;
310 volatile int mk_change_active;
311 char mk_change_op[8]; /* set if mk_change_active = 1 */
312 unsigned char new_wkvp[XCP_WKID_BYTES]; /* set if mk_change_active = 1 */
313 struct apqn *mk_change_apqns; /* set if mk_change_active = 1 */
314 unsigned int num_mk_change_apqns; /* set if mk_change_active = 1 */
315 int inconsistent;
316 libica_t libica;
317 void *lib_ep11;
318 CK_VERSION ep11_lib_version;
319 volatile ep11_target_info_t *target_info;
320 pthread_rwlock_t target_rwlock;
321 CK_BYTE vhsm_pin[XCP_MAX_PINBYTES];
322 CK_BBOOL vhsm_pin_valid;
323 CK_BYTE vhsm_pin_blob[XCP_PINBLOB_BYTES];
324 CK_BBOOL vhsm_pin_blob_valid;
325 CK_BYTE fips_pin[XCP_MAX_PINBYTES];
326 CK_BBOOL fips_pin_valid;
327 CK_BYTE fips_pin_blob[XCP_PINBLOB_V1_BYTES];
328 CK_BBOOL fips_pin_blob_valid;
329 uint32 session_refcount;
330 uint32_t (*get_session_refcount)(STDLL_TokData_t *tokdata);
331 void (*incr_session_refcount)(STDLL_TokData_t *tokdata);
332 void (*decr_session_refcount)(STDLL_TokData_t *tokdata);
333 pthread_mutex_t session_mutex;
334 } ep11_private_data_t;
335
336 #define UNKNOWN_CP 0xFFFFFFFF
337
338 #define CP_BYTE_NO(cp) ((cp) / 8)
339 #define CP_BIT_IN_BYTE(cp) ((cp) % 8)
340 #define CP_BIT_MASK(cp) (0x80 >> CP_BIT_IN_BYTE(cp))
341
342 #define SYSFS_DEVICES_AP "/sys/devices/ap/"
343 #define REGEX_CARD_PATTERN "card[0-9a-fA-F]+"
344 #define REGEX_SUB_CARD_PATTERN "[0-9a-fA-F]+\\.[0-9a-fA-F]+"
345 #define MASK_EP11 0x04000000
346
347 #define SYSFS_SEL_GUEST "/sys/firmware/uv/prot_virt_guest"
348
349 typedef struct {
350 STDLL_TokData_t *tokdata;
351 ep11_session_t *ep11_session;
352 CK_BBOOL wrap_was_successful;
353 CK_RV wrap_error;
354 CK_VOID_PTR secure_key;
355 CK_ULONG secure_key_len;
356 CK_VOID_PTR secure_key_reenc;
357 CK_ULONG secure_key_reenc_len;
358 CK_BYTE *pkey_buf;
359 size_t *pkey_buflen_p;
360 /* for AES XTS processing */
361 CK_VOID_PTR secure_key2;
362 CK_ULONG secure_key_len2;
363 CK_VOID_PTR secure_key_reenc2;
364 CK_ULONG secure_key_reenc_len2;
365 CK_BYTE *pkey_buf2;
366 size_t *pkey_buflen_p2;
367 CK_BBOOL aes_xts;
368 } pkey_wrap_handler_data_t;
369
370 /* A wrapped key can have max 132 bytes for an EC-p521 key */
371 #define EP11_MAX_WRAPPED_KEY_SIZE (2 * (521 / 8 + 1))
372 #define EP11_WRAPPED_KEY_VERSION_1 0x0001
373 #define EP11_WRAPPED_KEY_TYPE_AES 0x1
374 #define EP11_WRAPPED_KEY_TYPE_DES 0x2
375 #define EP11_WRAPPED_KEY_TYPE_EC 0x3
376 #define EP11_WRAPPED_KEY_TYPE_ED 0x4
377
378 typedef struct {
379 uint16_t version;
380 uint8_t res0[16];
381 uint32_t wrapped_key_type;
382 uint32_t bit_length;
383 uint64_t token_size;
384 uint8_t res1[8];
385 uint8_t wrapped_key[EP11_MAX_WRAPPED_KEY_SIZE];
386 uint8_t res2[50];
387 } __attribute__((packed)) wrapped_key_t;
388
389 extern m_DigestSingle_t dll_m_DigestSingle;
390 extern m_Login_t dll_m_Login;
391 extern m_Logout_t dll_m_Logout;
392 extern m_LoginExtended_t dll_m_LoginExtended;
393 extern m_LogoutExtended_t dll_m_LogoutExtended;
394 extern m_get_xcp_info_t dll_m_get_xcp_info;
395 extern m_admin_t dll_m_admin;
396 extern xcpa_cmdblock_t dll_xcpa_cmdblock;
397 extern xcpa_internal_rv_t dll_xcpa_internal_rv;
398
399 typedef CK_RV(*adapter_handler_t) (uint_32 adapter, uint_32 domain,
400 void *handler_data);
401
402 CK_RV handle_all_ep11_cards(ep11_target_t * ep11_targets,
403 adapter_handler_t handler, void *handler_data);
404 CK_BBOOL is_apqn_online(uint_32 card, uint_32 domain);
405
406 CK_BOOL ep11_is_session_object(CK_ATTRIBUTE_PTR attrs, CK_ULONG attrs_len);
407 CK_BOOL ep11_is_private_object(CK_ATTRIBUTE_PTR attrs, CK_ULONG attrs_len);
408 CK_RV ep11tok_relogin_session(STDLL_TokData_t *tokdata, SESSION *session);
409 void ep11_get_pin_blob(STDLL_TokData_t *tokdata, ep11_session_t *ep11_session,
410 CK_BOOL is_session_obj, CK_BOOL is_private_obj,
411 CK_BYTE **pin_blob, CK_ULONG *pin_blob_len);
412 CK_RV ep11_login_handler(uint_32 adapter, uint_32 domain, void *handler_data);
413
414 ep11_target_info_t *get_target_info(STDLL_TokData_t *tokdata);
415 void put_target_info(STDLL_TokData_t *tokdata, ep11_target_info_t *target_info);
416 CK_RV refresh_target_info(STDLL_TokData_t *tokdata, CK_BBOOL wait_for_new_wk);
417
418 CK_RV get_ep11_target_for_apqn(uint_32 adapter, uint_32 domain,
419 target_t *target, uint64_t flags);
420 void free_ep11_target_for_apqn(target_t target);
421
422 CK_RV ep11_error_to_pkcs11_error(CK_RV rc, SESSION *session);
423
424 CK_BBOOL ep11tok_libica_digest_available(STDLL_TokData_t *tokdata,
425 ep11_private_data_t *ep11_data,
426 CK_MECHANISM_TYPE mech);
427 CK_RV ep11tok_libica_digest(STDLL_TokData_t *tokdata,
428 ep11_private_data_t *ep11_data,
429 CK_MECHANISM_TYPE mech, libica_sha_context_t *ctx,
430 CK_BYTE *in_data, CK_ULONG in_data_len,
431 CK_BYTE *out_data, CK_ULONG *out_data_len,
432 unsigned int message_part);
433
434 CK_RV ep11tok_handle_mk_change_event(STDLL_TokData_t *tokdata,
435 unsigned int event_type,
436 unsigned int event_flags,
437 const char *payload,
438 unsigned int payload_len);
439 CK_RV ep11tok_mk_change_check_pending_ops(STDLL_TokData_t *tokdata);
440 CK_BBOOL ep11tok_is_blob_new_wkid(STDLL_TokData_t *tokdata,
441 CK_BYTE *blob, CK_ULONG blob_len);
442 CK_RV ep11tok_reencipher_blob(STDLL_TokData_t *tokdata, SESSION *session,
443 ep11_target_info_t **target_info,
444 CK_BYTE *blob, CK_ULONG blob_len,
445 CK_BYTE *new_blob);
446
447 void ep11_config_parse_error(int line, int col, const char *msg);
448 void ep11_config_error_token(const char *fname, const char *key,
449 int line, const char *expected);
450 CK_RV ep11_config_next(struct ConfigBaseNode **c, unsigned typemask,
451 const char *fname, const char *expected);
452
453 CK_RV ep11tok_get_mechanism_list(STDLL_TokData_t * tokdata,
454 CK_MECHANISM_TYPE_PTR pMechanismList,
455 CK_ULONG_PTR pulCount);
456
457 CK_RV ep11tok_get_mechanism_info(STDLL_TokData_t * tokdata,
458 CK_MECHANISM_TYPE type,
459 CK_MECHANISM_INFO_PTR pInfo);
460
461 CK_RV ep11tok_is_mechanism_supported(STDLL_TokData_t *tokdata,
462 CK_MECHANISM_TYPE type);
463
464 CK_RV ep11tok_is_mechanism_supported_ex(STDLL_TokData_t *tokdata,
465 CK_MECHANISM_PTR mech);
466
467 CK_RV ep11tok_init(STDLL_TokData_t * tokdata, CK_SLOT_ID SlotNumber,
468 char *conf_name);
469
470 CK_RV ep11tok_final(STDLL_TokData_t * tokdata, CK_BBOOL in_fork_initializer);
471
472 CK_RV ep11tok_generate_key(STDLL_TokData_t * tokdata, SESSION * session,
473 CK_MECHANISM_PTR mech, CK_ATTRIBUTE_PTR attrs,
474 CK_ULONG attrs_len, CK_OBJECT_HANDLE_PTR handle,
475 CK_BBOOL count_statistics, CK_ULONG operation);
476
477 CK_RV is_ec_aggregate_mechanism(CK_MECHANISM_PTR pMechanism);
478
479 CK_RV ep11tok_derive_key(STDLL_TokData_t * tokdata, SESSION * session,
480 CK_MECHANISM_PTR mech, CK_OBJECT_HANDLE hBaseKey,
481 CK_OBJECT_HANDLE_PTR handle, CK_ATTRIBUTE_PTR attrs,
482 CK_ULONG attrs_len, CK_BBOOL count_statistics,
483 CK_ULONG operation);
484
485 CK_RV ep11tok_generate_key_pair(STDLL_TokData_t * tokdata, SESSION * sess,
486 CK_MECHANISM_PTR pMechanism,
487 CK_ATTRIBUTE_PTR pPublicKeyTemplate,
488 CK_ULONG ulPublicKeyAttributeCount,
489 CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
490 CK_ULONG ulPrivateKeyAttributeCount,
491 CK_OBJECT_HANDLE_PTR phPublicKey,
492 CK_OBJECT_HANDLE_PTR phPrivateKey,
493 CK_BBOOL count_statistics,
494 CK_ULONG operation);
495
496 CK_RV ep11tok_check_single_mech_key(STDLL_TokData_t *tokdata, SESSION * session,
497 CK_MECHANISM *mech, CK_OBJECT_HANDLE key,
498 CK_ULONG operation,
499 CK_BBOOL *auth_required);
500
501 CK_BOOL ep11tok_mech_single_only(CK_MECHANISM *mech);
502
503 CK_BBOOL ep11tok_is_common_code_derive_mech(CK_MECHANISM *mech);
504 CK_BBOOL ep11tok_is_common_code_sign_verify_mech(CK_MECHANISM *mech);
505
506 CK_RV ep11tok_sign_init(STDLL_TokData_t * tokdata, SESSION * session,
507 CK_MECHANISM * mech, CK_BBOOL recover_mode,
508 CK_OBJECT_HANDLE key, CK_BBOOL checkauth);
509
510 CK_RV ep11tok_sign(STDLL_TokData_t * tokdata, SESSION * session,
511 CK_BBOOL length_only, CK_BYTE * in_data,
512 CK_ULONG in_data_len, CK_BYTE * signature,
513 CK_ULONG * sig_len);
514
515 CK_RV ep11tok_sign_update(STDLL_TokData_t * tokdata, SESSION * session,
516 CK_BYTE * in_data, CK_ULONG in_data_len);
517
518 CK_RV ep11tok_sign_final(STDLL_TokData_t * tokdata, SESSION * session,
519 CK_BBOOL length_only, CK_BYTE * signature,
520 CK_ULONG * sig_len);
521
522 CK_RV ep11tok_sign_single(STDLL_TokData_t *tokdata, SESSION *session,
523 CK_MECHANISM *mech, CK_BBOOL length_only,
524 CK_OBJECT_HANDLE key, CK_BYTE_PTR in_data,
525 CK_ULONG in_data_len, CK_BYTE_PTR signature,
526 CK_ULONG_PTR sig_len);
527
528 CK_RV ep11tok_verify_init(STDLL_TokData_t * tokdata, SESSION * session,
529 CK_MECHANISM * mech, CK_BBOOL recover_mode,
530 CK_OBJECT_HANDLE key);
531
532 CK_RV ep11tok_verify(STDLL_TokData_t * tokdata, SESSION * session,
533 CK_BYTE * in_data, CK_ULONG in_data_len,
534 CK_BYTE * signature, CK_ULONG sig_len);
535
536 CK_RV ep11tok_verify_update(STDLL_TokData_t * tokdata, SESSION * session,
537 CK_BYTE * in_data, CK_ULONG in_data_len);
538
539 CK_RV ep11tok_verify_final(STDLL_TokData_t * tokdata, SESSION * session,
540 CK_BYTE * signature, CK_ULONG sig_len);
541
542 CK_RV ep11tok_verify_single(STDLL_TokData_t *tokdata, SESSION *session,
543 CK_MECHANISM *mech, CK_OBJECT_HANDLE key,
544 CK_BYTE_PTR in_data, CK_ULONG in_data_len,
545 CK_BYTE_PTR signature, CK_ULONG sig_len);
546
547 CK_RV ep11tok_decrypt_final(STDLL_TokData_t * tokdata, SESSION * session,
548 CK_BYTE_PTR output_part,
549 CK_ULONG_PTR p_output_part_len);
550
551 CK_RV ep11tok_decrypt(STDLL_TokData_t * tokdata, SESSION * session,
552 CK_BYTE_PTR input_data, CK_ULONG input_data_len,
553 CK_BYTE_PTR output_data, CK_ULONG_PTR p_output_data_len);
554
555 CK_RV ep11tok_decrypt_update(STDLL_TokData_t * tokdata, SESSION * session,
556 CK_BYTE_PTR input_part, CK_ULONG input_part_len,
557 CK_BYTE_PTR output_part,
558 CK_ULONG_PTR p_output_part_len);
559
560 CK_RV ep11tok_encrypt_final(STDLL_TokData_t * tokdata, SESSION * session,
561 CK_BYTE_PTR output_part,
562 CK_ULONG_PTR p_output_part_len);
563
564 CK_RV ep11tok_encrypt(STDLL_TokData_t * tokdata, SESSION * session,
565 CK_BYTE_PTR input_data, CK_ULONG input_data_len,
566 CK_BYTE_PTR output_data, CK_ULONG_PTR p_output_data_len);
567
568 CK_RV ep11tok_encrypt_update(STDLL_TokData_t * tokdata, SESSION * session,
569 CK_BYTE_PTR input_part,
570 CK_ULONG input_part_len, CK_BYTE_PTR output_part,
571 CK_ULONG_PTR p_output_part_len);
572
573 CK_RV ep11tok_encrypt_init(STDLL_TokData_t * tokdata, SESSION * session,
574 CK_MECHANISM_PTR mech, CK_OBJECT_HANDLE key);
575
576 CK_RV ep11tok_encrypt_single(STDLL_TokData_t *tokdata, SESSION *session,
577 CK_MECHANISM *mech, CK_BBOOL length_only,
578 CK_OBJECT_HANDLE key, CK_BYTE *input_data,
579 CK_ULONG input_data_len, CK_BYTE *output_data,
580 CK_ULONG_PTR p_output_data_len);
581
582 CK_RV ep11tok_decrypt_init(STDLL_TokData_t * tokdata, SESSION * session,
583 CK_MECHANISM_PTR mech, CK_OBJECT_HANDLE key,
584 CK_BBOOL checkauth);
585
586 CK_RV ep11tok_decrypt_single(STDLL_TokData_t *tokdata, SESSION *session,
587 CK_MECHANISM *mech, CK_BBOOL length_only,
588 CK_OBJECT_HANDLE key, CK_BYTE_PTR input_data,
589 CK_ULONG input_data_len, CK_BYTE_PTR output_data,
590 CK_ULONG_PTR p_output_data_len);
591
592 CK_RV ep11tok_wrap_key(STDLL_TokData_t * tokdata, SESSION * session,
593 CK_MECHANISM_PTR mech, CK_OBJECT_HANDLE wrapping_key,
594 CK_OBJECT_HANDLE key, CK_BYTE_PTR wrapped_key,
595 CK_ULONG_PTR p_wrapped_key_len,
596 CK_BBOOL count_statistics);
597
598 CK_RV ep11tok_unwrap_key(STDLL_TokData_t * tokdata, SESSION * session,
599 CK_MECHANISM_PTR mech, CK_ATTRIBUTE_PTR attrs,
600 CK_ULONG attrs_len, CK_BYTE_PTR wrapped_key,
601 CK_ULONG wrapped_key_len,
602 CK_OBJECT_HANDLE wrapping_key,
603 CK_OBJECT_HANDLE_PTR p_key, CK_ULONG operation);
604
605 CK_RV ep11tok_login_session(STDLL_TokData_t * tokdata, SESSION * session);
606
607 CK_RV ep11tok_logout_session(STDLL_TokData_t * tokdata, SESSION * session,
608 CK_BBOOL in_fork_initializer);
609
610 CK_BBOOL ep11tok_optimize_single_ops(STDLL_TokData_t *tokdata);
611
612 CK_BBOOL ep11tok_libica_mech_available(STDLL_TokData_t *tokdata,
613 CK_MECHANISM_TYPE mech,
614 CK_OBJECT_HANDLE hKey);
615
616 CK_RV ep11tok_copy_firmware_info(STDLL_TokData_t *tokdata,
617 CK_TOKEN_INFO_PTR pInfo);
618
619 CK_RV ep11tok_pkey_usage_ok(STDLL_TokData_t *tokdata, SESSION *session,
620 CK_OBJECT_HANDLE hkey, CK_MECHANISM *mech);
621
622 CK_RV ep11tok_set_operation_state(STDLL_TokData_t *tokdata, SESSION *session);
623
624 CK_RV do_LoginExtended(XCP_LoginAlgorithm_t alg,
625 XCP_LoginImporter_t imp_keytype,
626 const CK_BYTE *pin, CK_ULONG pin_len,
627 const CK_BYTE *nonce, CK_ULONG nonce_len,
628 CK_BYTE *pin_blob, CK_ULONG *pin_blob_len,
629 const CK_BYTE *parent_session_id, target_t target);
630
631 CK_RV do_LogoutExtended(XCP_LoginAlgorithm_t alg,
632 XCP_LoginImporter_t imp_keytype,
633 const CK_BYTE *pin, CK_ULONG pin_len,
634 const CK_BYTE *nonce, CK_ULONG nonce_len,
635 target_t target);
636 CK_RV ep11tok_extract_blob_info(CK_BYTE *blob, CK_ULONG blob_len, CK_BBOOL *is_spki,
637 CK_BYTE **wkvp, CK_BYTE** session_id,
638 XCP_Attr_t **bool_attrs);
639
640 /*
641 * Macros to enclose EP11 library calls involving session bound blobs.
642 * If the EP11 token is in an inconsistent state, fail with CKR_DEVICE_ERROR.
643 * Obtain a target_info to be used with the EP11 library call.
644 * If in single-APQN mode, and that APQN went offline, select another APQN and
645 * retry the library call.
646 * In case of EP11 library function failed with CKR_SESSION_CLOSED or
647 * CKR_PIN_INCORRECT, relogin all APQNs and retry the library call.
648 */
649 #define RETRY_SESSION_SINGLE_APQN_START(rc, tokdata) \
650 do { \
651 ep11_target_info_t* target_info; \
652 int retry_count; \
653 CK_RV rc2; \
654 if (((ep11_private_data_t *) \
655 (tokdata)->private_data)->inconsistent) { \
656 (rc) = CKR_DEVICE_ERROR; \
657 TRACE_ERROR("%s\n", ock_err(ERR_DEVICE_ERROR)); \
658 break; \
659 } \
660 target_info = get_target_info((tokdata)); \
661 if (target_info == NULL) { \
662 (rc) = CKR_FUNCTION_FAILED; \
663 break; \
664 } \
665 for (retry_count = 0; \
666 target_info != NULL && \
667 retry_count < MAX_RETRY_COUNT; \
668 retry_count ++) {
669
670 #define RETRY_SESSION_SINGLE_APQN_END(rc, tokdata, session) \
671 if ((target_info) == NULL) \
672 break; \
673 if (target_info->single_apqn && \
674 ((rc) == CKR_IBM_TARGET_INVALID || \
675 ((rc) == CKR_FUNCTION_FAILED && \
676 !is_apqn_online(target_info->adapter, \
677 target_info->domain)))) { \
678 /* Single APQN went offline, select other */\
679 TRACE_DEVEL("%s single APQN went offline\n",\
680 __func__); \
681 put_target_info((tokdata), target_info); \
682 target_info = NULL; \
683 (rc) = refresh_target_info((tokdata), \
684 FALSE); \
685 if ((rc) != CKR_OK) \
686 break; \
687 target_info = get_target_info((tokdata)); \
688 if (target_info == NULL) { \
689 (rc) = CKR_FUNCTION_FAILED; \
690 break; \
691 } \
692 continue; \
693 } \
694 if ((rc) != CKR_SESSION_CLOSED && \
695 (rc) != CKR_PIN_INCORRECT) \
696 break; \
697 rc2 = ep11tok_relogin_session((tokdata), \
698 (session)); \
699 if (rc2 != CKR_OK) { \
700 (rc) = rc2; \
701 break; \
702 } \
703 } \
704 put_target_info((tokdata), target_info); \
705 } while (0);
706
707 /*
708 * Macros to enclose EP11 library calls not involving session bound blobs, and
709 * with given target_info.
710 * If the EP11 token is in an inconsistent state, fail with CKR_DEVICE_ERROR.
711 * If in single-APQN mode, and that APQN went offline, select another APQN and
712 * retry the library call.
713 */
714 #define RETRY_SINGLE_APQN_START(tokdata, rc) \
715 do { \
716 int retry_count; \
717 if (((ep11_private_data_t *) \
718 (tokdata)->private_data)->inconsistent) { \
719 (rc) = CKR_DEVICE_ERROR; \
720 TRACE_ERROR("%s\n", ock_err(ERR_DEVICE_ERROR)); \
721 break; \
722 } \
723 for (retry_count = 0; \
724 retry_count < MAX_RETRY_COUNT; \
725 retry_count ++) {
726
727 #define RETRY_SINGLE_APQN_END(rc, tokdata, target_info) \
728 if ((target_info) == NULL) \
729 break; \
730 if ((target_info)->single_apqn && \
731 ((rc) == CKR_IBM_TARGET_INVALID || \
732 ((rc) == CKR_FUNCTION_FAILED && \
733 !is_apqn_online((target_info)->adapter, \
734 (target_info)->domain)))) { \
735 /* Single APQN went offline, select other */\
736 TRACE_DEVEL("%s single APQN went offline\n",\
737 __func__); \
738 put_target_info((tokdata), (target_info)); \
739 target_info = NULL; \
740 (rc) = refresh_target_info((tokdata), \
741 FALSE); \
742 if ((rc) != CKR_OK) \
743 break; \
744 (target_info) = get_target_info((tokdata)); \
745 if ((target_info) == NULL) { \
746 (rc) = CKR_FUNCTION_FAILED; \
747 break; \
748 } \
749 continue; \
750 } \
751 break; \
752 } \
753 } while (0);
754
755 /*
756 * Macros to enclose EP11 library calls with 1, 2 or 3 key blobs as argument.
757 * If a master key change is active, and the single APQN has the new WK,
758 * obtain and use the re-enciphered blob(s) from CKA_IBM_OPAQUE_REENC from
759 * the key object(s).
760 * If a master key change is active, and the EP11 library call fails with
761 * CKR_IBM_WKID_MISMATCH and the used blob(s) are enciphered with the new WK,
762 * then select a new single APQN that has the new WK. If no available APQN has
763 * the new WK, wait until on at least one the new WK gets activated.
764 * In case the blob(s) are enciphered with the old WK, then obtain the
765 * re-enciphered blob(s) from CKA_IBM_OPAQUE_REENC from the key object(s)
766 * and retry the library call.
767 * If the retry was successful, indicate that the single APQN has the new WK.
768 */
769 #define RETRY_REENC_BLOB_START(tokdata, target_info, obj, blob, blobsize,\
770 useblob, useblobsize, rc) \
771 RETRY_REENC_BLOB3_START(tokdata, target_info, obj, blob, \
772 blobsize, useblob, useblobsize, \
773 NULL, blob, blobsize, blob, \
774 blobsize, NULL, blob, blobsize, \
775 blob, blobsize, rc)
776
777 #define RETRY_REENC_BLOB_END(tokdata, target_info, useblob, useblobsize, \
778 rc) \
779 RETRY_REENC_BLOB3_END(tokdata, target_info, useblob, \
780 useblobsize, NULL, 0, NULL, 0, rc)
781
782 #define RETRY_REENC_BLOB2_START(tokdata, target_info, obj1, blob1, \
783 blobsize1, useblob1, useblobsize1, \
784 obj2, blob2, blobsize2, useblob2, \
785 useblobsize2, rc) \
786 RETRY_REENC_BLOB3_START(tokdata, target_info, obj1, \
787 blob1, blobsize1, useblob1, \
788 useblobsize1, obj2, blob2, \
789 blobsize2, useblob2, \
790 useblobsize2, NULL, blob1, \
791 blobsize1, blob1, blobsize1, rc)
792
793 #define RETRY_REENC_BLOB2_END(tokdata, target_info, useblob1, \
794 useblobsize1, useblob2, useblobsize2, rc) \
795 RETRY_REENC_BLOB3_END(tokdata, target_info, useblob1, \
796 useblobsize1, useblob2, \
797 useblobsize2, NULL, 0, rc)
798
799 #define RETRY_REENC_BLOB3_START(tokdata, target_info, obj1, blob1, \
800 blobsize1, useblob1, useblobsize1, \
801 obj2, blob2, blobsize2, useblob2, \
802 useblobsize2, obj3, blob3, blobsize3, \
803 useblob3, useblobsize3, rc) \
804 do { \
805 int retry = 0; \
806 do { \
807 if (((ep11_private_data_t *)(tokdata)-> \
808 private_data)->mk_change_active && \
809 ((target_info)->single_apqn_has_new_wk || \
810 retry == 1)) { \
811 /* New WK is set on single APQN */ \
812 TRACE_DEVEL("%s single APQN has new WK\n", \
813 __func__); \
814 if (obj1 != NULL) { \
815 (rc) = obj_opaque_2_reenc_blob((tokdata),\
816 (obj1), &(useblob1),\
817 &(useblobsize1)); \
818 if ((rc) == CKR_TEMPLATE_INCOMPLETE) { \
819 (useblob1) = (blob1); \
820 (useblobsize1) = (blobsize1); \
821 } else if ((rc) != CKR_OK) { \
822 TRACE_ERROR( \
823 "%s reenc blob1 invalid\n", \
824 __func__); \
825 break; \
826 } \
827 } \
828 if (obj2 != NULL) { \
829 (rc) = obj_opaque_2_reenc_blob((tokdata),\
830 (obj2), &(useblob2),\
831 &(useblobsize2)); \
832 if ((rc) == CKR_TEMPLATE_INCOMPLETE) { \
833 (useblob2) = (blob2); \
834 (useblobsize2) = (blobsize2); \
835 } else if ((rc) != CKR_OK) { \
836 TRACE_ERROR( \
837 "%s reenc blob2 invalid\n", \
838 __func__); \
839 break; \
840 } \
841 } \
842 if (obj3 != NULL) { \
843 (rc) = obj_opaque_2_reenc_blob((tokdata),\
844 (obj3), &(useblob3),\
845 &(useblobsize3)); \
846 if ((rc) == CKR_TEMPLATE_INCOMPLETE) { \
847 (useblob3) = (blob3); \
848 (useblobsize3) = (blobsize3); \
849 } else if ((rc) != CKR_OK) { \
850 TRACE_ERROR( \
851 "%s reenc blob3 invalid\n", \
852 __func__); \
853 break; \
854 } \
855 } \
856 retry = 1; \
857 } else { \
858 if (obj1 != NULL) { \
859 (useblob1) = (blob1); \
860 (useblobsize1) = (blobsize1); \
861 } \
862 if (obj2 != NULL) { \
863 (useblob2) = (blob2); \
864 (useblobsize2) = (blobsize2); \
865 } \
866 if (obj3 != NULL) { \
867 (useblob3) = (blob3); \
868 (useblobsize3) = (blobsize3); \
869 } \
870 }
871
872 #define RETRY_REENC_BLOB3_END(tokdata, target_info, useblob1, \
873 useblobsize1, useblob2, useblobsize2, \
874 useblob3, useblobsize3, rc) \
875 if (((ep11_private_data_t *)(tokdata)-> \
876 private_data)->mk_change_active && \
877 retry == 0 && \
878 (rc) == CKR_IBM_WKID_MISMATCH) { \
879 if ((useblob1 != NULL && \
880 ep11tok_is_blob_new_wkid((tokdata), \
881 (useblob1), (useblobsize1))) || \
882 (useblob2 != NULL && \
883 ep11tok_is_blob_new_wkid((tokdata), \
884 (useblob2), (useblobsize2))) || \
885 (useblob3 != NULL && \
886 ep11tok_is_blob_new_wkid((tokdata), \
887 (useblob3), (useblobsize3)))) { \
888 /* blob has new WK, but single APQN not.
889 Wait until other single APQN with
890 new WK set is available */ \
891 TRACE_DEVEL("%s WKID mismatch, blob has "\
892 "new WK, retry with new " \
893 "single APQN with new WK, " \
894 "wait if required\n", \
895 __func__); \
896 put_target_info((tokdata), \
897 (target_info)); \
898 (target_info) = NULL; \
899 (rc) = refresh_target_info((tokdata), \
900 TRUE); \
901 if ((rc) != CKR_OK) \
902 break; \
903 (target_info) = \
904 get_target_info((tokdata)); \
905 if ((target_info) == NULL) { \
906 (rc) = CKR_FUNCTION_FAILED; \
907 break; \
908 } \
909 continue; \
910 } \
911 /* Single APQN seems to now have new WK */ \
912 TRACE_DEVEL("%s WKID mismatch, retry with " \
913 "reenc-blob(s)\n", __func__); \
914 retry = 1; \
915 continue; \
916 } \
917 if (((ep11_private_data_t *)(tokdata)-> \
918 private_data)->mk_change_active && \
919 retry == 1 && \
920 (rc) != CKR_IBM_WKID_MISMATCH && \
921 (rc) != CKR_IBM_TARGET_INVALID && \
922 (rc) != CKR_FUNCTION_FAILED) { \
923 /* retry with re-enciphered blob worked */ \
924 TRACE_DEVEL("%s single APQN has new WK\n", \
925 __func__); \
926 __sync_or_and_fetch( \
927 &(target_info)->single_apqn_has_new_wk, \
928 1); \
929 } \
930 break; \
931 } while (1); \
932 } while (0);
933
934 /*
935 * Macros to enclose EP11 library calls with the wrap-blob as argument.
936 * If a master key change is active, and the single APQN has the new WK,
937 * obtain and use the re-enciphered wrap-blob.
938 * If a master key change is active, and the EP11 library call fails with
939 * CKR_IBM_WKID_MISMATCH and the used wrap blob is enciphered with the new WK,
940 * then select a new single APQN that has the new WK. If no available APQN has
941 * the new WK, wait until on at least one the new WK gets activated.
942 * In case the used wrap blob is enciphered with the old WK, then obtain the
943 * re-enciphered wrap-blob and retry the library call.
944 * If the retry was successful, indicate that the single APQN has the new WK.
945 */
946 #define RETRY_REENC_WRAPBLOB_START(tokdata, target_info, useblob) \
947 do { \
948 int retry = 0; \
949 do { \
950 if (((ep11_private_data_t *)(tokdata)-> \
951 private_data)->mk_change_active && \
952 (target_info)->single_apqn_has_new_wk && \
953 retry == 0) { \
954 /* New WK is already set on single APQN */ \
955 TRACE_DEVEL("%s single APQN has new WK\n", \
956 __func__); \
957 (useblob) = ((ep11_private_data_t *) \
958 (tokdata)->private_data)-> \
959 raw2key_wrap_blob_reenc; \
960 retry = 1; \
961 } else { \
962 (useblob) = ((ep11_private_data_t *) \
963 (tokdata)->private_data)-> \
964 raw2key_wrap_blob; \
965 }
966
967 #define RETRY_REENC_WRAPBLOB_END(tokdata, target_info, useblob, rc) \
968 if (((ep11_private_data_t *)(tokdata)-> \
969 private_data)->mk_change_active && \
970 retry == 0 && \
971 (rc) == CKR_IBM_WKID_MISMATCH) { \
972 if (ep11tok_is_blob_new_wkid((tokdata), \
973 (useblob), \
974 ((ep11_private_data_t *) \
975 (tokdata)->private_data)-> \
976 raw2key_wrap_blob_l)) { \
977 /* blob has new WK, but single APQN not.
978 Wait until other single APQN with
979 new WK set is available */ \
980 TRACE_DEVEL("%s WKID mismatch, blob has "\
981 "new WK, retry with new " \
982 "single APQN with new WK, " \
983 "wait if required\n", \
984 __func__); \
985 put_target_info((tokdata), \
986 (target_info)); \
987 (target_info) = NULL; \
988 (rc) = refresh_target_info((tokdata), \
989 TRUE); \
990 if ((rc) != CKR_OK) \
991 break; \
992 (target_info) = \
993 get_target_info((tokdata)); \
994 if ((target_info) == NULL) { \
995 (rc) = CKR_FUNCTION_FAILED; \
996 break; \
997 } \
998 continue; \
999 } \
1000 /* Single APQN seems to now have new WK */ \
1001 TRACE_DEVEL("%s WKID mismatch, retry with " \
1002 "reenc-wrap-blob\n", __func__); \
1003 (useblob) = ((ep11_private_data_t *) \
1004 (tokdata)->private_data)-> \
1005 raw2key_wrap_blob_reenc; \
1006 retry = 1; \
1007 continue; \
1008 } \
1009 if (((ep11_private_data_t *)(tokdata)-> \
1010 private_data)->mk_change_active && \
1011 retry == 1 && \
1012 (rc) != CKR_IBM_WKID_MISMATCH && \
1013 (rc) != CKR_IBM_TARGET_INVALID && \
1014 (rc) != CKR_FUNCTION_FAILED) { \
1015 /* retry with re-enciphered blob worked */ \
1016 TRACE_DEVEL("%s single APQN has new WK\n", \
1017 __func__); \
1018 __sync_or_and_fetch( \
1019 &(target_info)->single_apqn_has_new_wk, \
1020 1); \
1021 } \
1022 break; \
1023 } while (1); \
1024 } while (0);
1025
1026 /*
1027 * Macros to enclose EP11 library calls with a blob as argument that gets
1028 * updated by the EP11 library call.
1029 * If a master key change is active, and the single APQN has the new WK,
1030 * use the re-enciphered blob for the EP11 library call.
1031 * If a master key change is active, and the EP11 library call fails with
1032 * CKR_IBM_WKID_MISMATCH use the re-enciphered blob and retry the library call.
1033 * Also indicate that the single APQN has the new WK.
1034 * If a master key change is active, and the EP11 library call was successful
1035 * when using the old blob, re-encipher the (potentially updated) blob.
1036 * When re-enciphering fails with CKR_IBM_WK_NOT_INITIALIZED, because the
1037 * new WK was just activated on the single APQN, indicate that the single APQN
1038 * has the new WK, and retry the EP11 library call with the (still unchanged)
1039 * re-enciphered blob (which then will be potentially updated by the EP11
1040 * library call).
1041 */
1042 #define RETRY_UPDATE_BLOB_START(tokdata, target_info, blob, blobsize, \
1043 reencblob, reencblobsize, \
1044 useblob, useblobsize) \
1045 do { \
1046 CK_RV rc2; \
1047 int retry = 0; \
1048 do { \
1049 if (((ep11_private_data_t *)(tokdata)-> \
1050 private_data)->mk_change_active && \
1051 ((target_info)->single_apqn_has_new_wk || \
1052 retry == 1)) { \
1053 /* New WK is set on single APQN */ \
1054 TRACE_DEVEL("%s single APQN has new WK\n", \
1055 __func__); \
1056 (useblob) = (reencblob); \
1057 (useblobsize) = (reencblobsize); \
1058 retry = 1; \
1059 } else { \
1060 (useblob) = (blob); \
1061 (useblobsize) = (blobsize); \
1062 }
1063
1064 #define RETRY_UPDATE_BLOB_END(tokdata, session, target_info, blob, \
1065 blobsize, reencblob, reencblobsize, \
1066 useblob, useblobsize, rc) \
1067 if (((ep11_private_data_t *)(tokdata)-> \
1068 private_data)->mk_change_active && \
1069 retry == 0 && \
1070 (rc) == CKR_IBM_WKID_MISMATCH) { \
1071 if (ep11tok_is_blob_new_wkid((tokdata), \
1072 (useblob), (useblobsize))) { \
1073 /* blob has new WK, but single APQN not.
1074 Wait until other single APQN with
1075 new WK set is available */ \
1076 TRACE_DEVEL("%s WKID mismatch, blob has "\
1077 "new WK, retry with new " \
1078 "single APQN with new WK, " \
1079 "wait if required\n", \
1080 __func__); \
1081 put_target_info((tokdata), \
1082 (target_info)); \
1083 (target_info) = NULL; \
1084 (rc) = refresh_target_info((tokdata), \
1085 TRUE); \
1086 if ((rc) != CKR_OK) \
1087 break; \
1088 (target_info) = \
1089 get_target_info((tokdata)); \
1090 if ((target_info) == NULL) { \
1091 (rc) = CKR_FUNCTION_FAILED; \
1092 break; \
1093 } \
1094 continue; \
1095 } \
1096 /* Single APQN seems to now have new WK */ \
1097 TRACE_DEVEL("%s single APQN has new WK\n", \
1098 __func__); \
1099 retry = 1; \
1100 continue; \
1101 } \
1102 if (((ep11_private_data_t *)(tokdata)-> \
1103 private_data)->mk_change_active && \
1104 retry == 0 && \
1105 (rc) != CKR_IBM_WKID_MISMATCH && \
1106 (rc) != CKR_IBM_TARGET_INVALID && \
1107 (rc) != CKR_FUNCTION_FAILED) { \
1108 /* Op worked, re-encipher updated blob */ \
1109 TRACE_DEVEL("%s reencipher updated blob\n", \
1110 __func__); \
1111 if ((blobsize) != (reencblobsize)) { \
1112 TRACE_ERROR("%s reencblobsize wrong \n", \
1113 __func__); \
1114 (rc) = CKR_ARGUMENTS_BAD; \
1115 break; \
1116 } \
1117 rc2 = ep11tok_reencipher_blob((tokdata), \
1118 (session), \
1119 &(target_info),\
1120 (blob), \
1121 (blobsize), \
1122 (reencblob)); \
1123 if (rc2 == CKR_IBM_WK_NOT_INITIALIZED) { \
1124 /* Single APQN now has new WK */ \
1125 TRACE_DEVEL("%s WKID mismatch on " \
1126 "reencipher, retry\n", __func__); \
1127 retry = 1; \
1128 continue; \
1129 } \
1130 if (rc2 != CKR_OK) { \
1131 (rc) = CKR_DEVICE_ERROR; \
1132 TRACE_ERROR("%s\n", \
1133 ock_err(ERR_DEVICE_ERROR)); \
1134 break; \
1135 } \
1136 } \
1137 if (((ep11_private_data_t *)(tokdata)-> \
1138 private_data)->mk_change_active && \
1139 retry == 1 && \
1140 (rc) != CKR_IBM_WKID_MISMATCH && \
1141 (rc) != CKR_IBM_TARGET_INVALID && \
1142 (rc) != CKR_FUNCTION_FAILED) { \
1143 /* retry with re-enciphered blob worked */ \
1144 TRACE_DEVEL("%s single APQN has new WK\n", \
1145 __func__); \
1146 __sync_or_and_fetch( \
1147 &(target_info)->single_apqn_has_new_wk, \
1148 1); \
1149 /* old blob is no longer valid, replace with
1150 * re-enciphered blob */ \
1151 if ((blobsize) != (useblobsize)) { \
1152 TRACE_ERROR("%s useblobsize wrong \n", \
1153 __func__); \
1154 (rc) = CKR_ARGUMENTS_BAD; \
1155 break; \
1156 } \
1157 memcpy((blob), (useblob), (blobsize)); \
1158 } \
1159 break; \
1160 } while (1); \
1161 } while (0);
1162
1163 /*
1164 * Macros to enclose EP11 library calls with a key blob and a state blob as
1165 * arguments where the state blob gets updated by the EP11 library call.
1166 * If a master key change is active, and the single APQN has the new WK,
1167 * obtain and use the re-enciphered key blob CKA_IBM_OPAQUE_REENC from
1168 * the key object, and also use the re-enciphered state blob(s) for the EP11
1169 * library call.
1170 * If a master key change is active, and the EP11 library call fails with
1171 * CKR_IBM_WKID_MISMATCH use the re-enciphered blobs and retry the library
1172 * call.
1173 * If the retry was successful, indicate that the single APQN has the new WK.
1174 * If a master key change is active, and the EP11 library call was successful
1175 * when using the old blobs, re-encipher the (potentially updated) state blob.
1176 * When re-enciphering fails with CKR_IBM_WK_NOT_INITIALIZED, because the
1177 * new WK was just activated on the single APQN, indicate that the single APQN
1178 * has the new WK, and retry the EP11 library call with the (still unchanged)
1179 * re-enciphered state blob (which then will be potentially updated by the EP11
1180 * library call).
1181 */
1182 #define RETRY_REENC_BLOB_STATE_START(tokdata, target_info, obj, blob, \
1183 blobsize, useblob, useblobsize, \
1184 stateblob, reencstate, \
1185 stateblobsize, usestate, \
1186 usestatesize, rc) \
1187 do { \
1188 int retry = 0; \
1189 do { \
1190 if (((ep11_private_data_t *)(tokdata)-> \
1191 private_data)->mk_change_active && \
1192 ((target_info)->single_apqn_has_new_wk || \
1193 retry == 1)) { \
1194 /* New WK is set on single APQN */ \
1195 TRACE_DEVEL("%s single APQN has new WK\n", \
1196 __func__); \
1197 (rc) = obj_opaque_2_reenc_blob((tokdata), \
1198 (obj), &(useblob), &(useblobsize));\
1199 if ((rc) == CKR_TEMPLATE_INCOMPLETE) { \
1200 (useblob) = (blob); \
1201 (useblobsize) = (blobsize); \
1202 } else if ((rc) != CKR_OK) { \
1203 TRACE_ERROR("%s reenc blob invalid\n", \
1204 __func__); \
1205 break; \
1206 } \
1207 (usestate) = (reencstate); \
1208 retry = 1; \
1209 } else { \
1210 (useblob) = (blob); \
1211 (useblobsize) = (blobsize); \
1212 (usestate) = (stateblob); \
1213 } \
1214 (usestatesize) = (stateblobsize);
1215
1216 #define RETRY_REENC_BLOB_STATE_END(tokdata, session, target_info, \
1217 blob, blobsize, \
1218 useblob, useblobsize, stateblob, \
1219 reencstate, stateblobsize, usestate, \
1220 usestatesize, newstate, rc) \
1221 if (((ep11_private_data_t *)(tokdata)-> \
1222 private_data)->mk_change_active && \
1223 retry == 0 && \
1224 (rc) == CKR_IBM_WKID_MISMATCH) { \
1225 if (ep11tok_is_blob_new_wkid((tokdata), \
1226 (useblob), (useblobsize)) || \
1227 ep11tok_is_blob_new_wkid((tokdata), \
1228 (usestate), (usestatesize))) { \
1229 /* blob has new WK, but single APQN not.
1230 Wait until other single APQN with
1231 new WK set is available */ \
1232 TRACE_DEVEL("%s WKID mismatch, blob has "\
1233 "new WK, retry with new " \
1234 "single APQN with new WK, " \
1235 "wait if required\n", \
1236 __func__); \
1237 put_target_info((tokdata), \
1238 (target_info)); \
1239 (target_info) = NULL; \
1240 (rc) = refresh_target_info((tokdata), \
1241 TRUE); \
1242 if ((rc) != CKR_OK) \
1243 break; \
1244 (target_info) = \
1245 get_target_info((tokdata)); \
1246 if ((target_info) == NULL) { \
1247 (rc) = CKR_FUNCTION_FAILED; \
1248 break; \
1249 } \
1250 continue; \
1251 } \
1252 /* Single APQN seems to now have new WK */ \
1253 TRACE_DEVEL("%s WKID mismatch, retry with " \
1254 "reenc-blob\n", __func__); \
1255 retry = 1; \
1256 continue; \
1257 } \
1258 if (((ep11_private_data_t *)(tokdata)-> \
1259 private_data)->mk_change_active && \
1260 retry == 1 && \
1261 (((newstate) && (rc) == CKR_OK) || \
1262 (!(newstate) && \
1263 (rc) != CKR_IBM_WKID_MISMATCH && \
1264 (rc) != CKR_IBM_TARGET_INVALID && \
1265 (rc) != CKR_FUNCTION_FAILED))) { \
1266 /* retry with re-enciphered blob worked */ \
1267 TRACE_DEVEL("%s single APQN has new WK\n", \
1268 __func__); \
1269 __sync_or_and_fetch( \
1270 &(target_info)->single_apqn_has_new_wk, \
1271 1); \
1272 /* old blobs are no longer valid, replace with
1273 * re-enciphered blobs */ \
1274 if ((blobsize) != (useblobsize)) { \
1275 TRACE_ERROR("%s useblobsize wrong \n", \
1276 __func__); \
1277 (rc) = CKR_ARGUMENTS_BAD; \
1278 break; \
1279 } \
1280 memcpy((blob), (useblob), (blobsize)); \
1281 if ((stateblobsize) != (usestatesize)) { \
1282 TRACE_ERROR("%s usestatesize wrong \n", \
1283 __func__); \
1284 (rc) = CKR_ARGUMENTS_BAD; \
1285 break; \
1286 } \
1287 memcpy((stateblob), (usestate), \
1288 (stateblobsize)); \
1289 } \
1290 if (((ep11_private_data_t *)(tokdata)-> \
1291 private_data)->mk_change_active && \
1292 retry == 0 && \
1293 (((newstate) && (rc) == CKR_OK) || \
1294 (!(newstate) && \
1295 (rc) != CKR_IBM_WKID_MISMATCH && \
1296 (rc) != CKR_IBM_TARGET_INVALID && \
1297 (rc) != CKR_FUNCTION_FAILED))) { \
1298 /* worked, re-encipher updated state blob */ \
1299 TRACE_DEVEL("%s reencipher updated state " \
1300 "blob\n", __func__); \
1301 rc2 = ep11tok_reencipher_blob((tokdata), \
1302 (session), \
1303 &(target_info), \
1304 (stateblob), \
1305 (stateblobsize), \
1306 (reencstate)); \
1307 if (rc2 == CKR_IBM_WK_NOT_INITIALIZED) { \
1308 /* Single APQN now has new WK */ \
1309 TRACE_DEVEL("%s WKID mismatch on " \
1310 "reencipher, retry\n", \
1311 __func__); \
1312 __sync_or_and_fetch(&(target_info)-> \
1313 single_apqn_has_new_wk,\
1314 1); \
1315 continue; \
1316 } \
1317 if (rc2 != CKR_OK) { \
1318 (rc) = CKR_DEVICE_ERROR; \
1319 TRACE_ERROR("%s\n", \
1320 ock_err(ERR_DEVICE_ERROR)); \
1321 break; \
1322 } \
1323 } \
1324 break; \
1325 } while (1); \
1326 } while (0);
1327
1328 /*
1329 * Macros to enclose EP11 library calls that create 1 or 2 key blobs as output.
1330 * If a master key change is active, and the key blob(s) were created with the
1331 * new WK, copy them to the re-enciphered blob(s) and indicate that the
1332 * single APQN has the new WK.
1333 * If a master key change is active, and the key blob(s) were created with the
1334 * old WK, re-encipher the key blob(s).
1335 * If re-enciphering fails with CKR_IBM_WK_NOT_INITIALIZED, because the new WK
1336 * was just activated on the single APQN, indicate that the single APQN has
1337 * the new WK, and retry the EP11 library call. This creates new key blob(s)
1338 * that are then enciphered with the new WK.
1339 */
1340 #define RETRY_REENC_CREATE_KEY_START() \
1341 RETRY_REENC_CREATE_KEY2_START()
1342
1343 #define RETRY_REENC_CREATE_KEY_END(tokdata, session, target_info, blob, \
1344 reencblob, blobsize, rc) \
1345 RETRY_REENC_CREATE_KEY2_END(tokdata, session, \
1346 target_info, blob, \
1347 reencblob, blobsize, blob, \
1348 reencblob, 0, rc)
1349
1350 #define RETRY_REENC_CREATE_KEY2_START() \
1351 do { \
1352 do { \
1353
1354 #define RETRY_REENC_CREATE_KEY2_END(tokdata, session, target_info, blob1,\
1355 reencblob1, blobsize1, blob2, \
1356 reencblob2, blobsize2, rc) \
1357 if (((ep11_private_data_t *)(tokdata)-> \
1358 private_data)->mk_change_active && \
1359 (rc) == CKR_OK) { \
1360 /* Key creation successful */ \
1361 if (ep11tok_is_blob_new_wkid((tokdata), \
1362 (blob1), (blobsize1)) || \
1363 ((blobsize2) > 0 && \
1364 ep11tok_is_blob_new_wkid((tokdata), \
1365 (blob2), (blobsize2)))) { \
1366 /* Key created with new WK already:
1367 supply it in reencblob as well */ \
1368 TRACE_DEVEL("%s new key has new WK\n", \
1369 __func__); \
1370 memcpy((reencblob1), (blob1), \
1371 (blobsize1)); \
1372 if ((blobsize2) > 0) \
1373 memcpy((reencblob2), (blob2), \
1374 (blobsize2)); \
1375 __sync_or_and_fetch(&(target_info)-> \
1376 single_apqn_has_new_wk, \
1377 1); \
1378 } else { \
1379 /* created with old WK, re-encipher it */\
1380 TRACE_DEVEL("%s new key has old WK, " \
1381 "reencipher it\n", __func__);\
1382 (rc) = ep11tok_reencipher_blob((tokdata),\
1383 (session), \
1384 &(target_info), (blob1), \
1385 (blobsize1), (reencblob1)); \
1386 if ((rc) == CKR_IBM_WK_NOT_INITIALIZED) {\
1387 /* Single APQN now has new WK,
1388 repeat key creation. */ \
1389 TRACE_DEVEL("%s WKID mismatch on " \
1390 "reencipher, retry\n", \
1391 __func__); \
1392 continue; \
1393 } \
1394 if ((rc) != CKR_OK) { \
1395 (rc) = CKR_DEVICE_ERROR; \
1396 TRACE_ERROR("%s\n", \
1397 ock_err(ERR_DEVICE_ERROR)); \
1398 break; \
1399 } \
1400 if ((blobsize2) > 0) { \
1401 (rc) = ep11tok_reencipher_blob( \
1402 (tokdata), (session), \
1403 &(target_info), \
1404 (blob2), (blobsize2), \
1405 (reencblob2)); \
1406 if ((rc) == \
1407 CKR_IBM_WK_NOT_INITIALIZED) {\
1408 /* Single APQN now has new WK,
1409 repeat key creation. */ \
1410 TRACE_DEVEL("%s WKID mismatch " \
1411 "on reencipher, retry\n",\
1412 __func__); \
1413 continue; \
1414 } \
1415 if ((rc) != CKR_OK) { \
1416 (rc) = CKR_DEVICE_ERROR; \
1417 TRACE_ERROR("%s\n", \
1418 ock_err(ERR_DEVICE_ERROR)); \
1419 break; \
1420 } \
1421 } \
1422 } \
1423 } \
1424 break; \
1425 } while (1); \
1426 } while (0);
1427
1428 #endif