xref: /illumos-gate/usr/src/lib/pkcs11/libpkcs11/common/metaGlobal.h (revision 843e19887f64dde75055cf8842fc4db2171eff45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _METAGLOBAL_H
27 #define	_METAGLOBAL_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * This file contains all the data structures used for the meta slot
33  */
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #include <pthread.h>
40 #include <synch.h>
41 #include <unistd.h>
42 #include <security/cryptoki.h>
43 #include <stdio.h>
44 #include <cryptoutil.h>
45 #include <pkcs11Session.h>
46 #include <pkcs11Slot.h>
47 
48 /*
49  * In "generic_attr_t", attributes that are not CK_BBOOL and
50  * CK_ULONG, the data will be stored in generic_data.
51  * Currently, 16 bytes will be pre-allocated for this.
52  * This is just a _WILD_ guess.  If actual
53  * experience shows that 16 bytes is too small for most of the
54  * data that will be stored here, and cause this
55  * memory to be reallocated all the time, this should be increased.
56  */
57 #define	INITIAL_ATTR_LEN	16
58 
59 /* We provide one slot, with the following arbitrary identifier. */
60 #define	METASLOT_SLOTID	42
61 
62 /* Metaslot is always the first slot in the framdwork, with slotID=0 */
63 #define	METASLOT_FRAMEWORK_ID	0
64 
65 /*
66  * These are the 2 acceptable string values for ${METASLOT_ENABLE} and
67  * ${METASLOT_AUTO_KEY_MIGRATE} environment variable
68  */
69 #define	TRUE_STRING	"true"
70 #define	FALSE_STRING	"false"
71 
72 /* Magic values for different data structures */
73 #define	METASLOT_SESSION_MAGIC		0xECF00004
74 #define	METASLOT_SESSION_BADMAGIC	0xBAD00004
75 #define	METASLOT_OBJECT_MAGIC		0xECF0B004
76 #define	METASLOT_OBJECT_BADMAGIC	0xBAD0B004
77 #define	METASLOT_OPSTATE_MAGIC		0xECF09004
78 #define	METASLOT_OPSTATE_BADMAGIC	0xBAD09004
79 
80 #define	IS_READ_ONLY_SESSION(session_flag) \
81 	(!(session_flag & CKF_RW_SESSION))
82 
83 /*
84  * Operation modes passed to meta_do_operation()
85  * MODE_UPDATE_WITHKEY is only used for C_DigestKey.
86  */
87 #define	MODE_SINGLE		0x0100
88 #define	MODE_UPDATE		0x0200
89 #define	MODE_UPDATE_WITHKEY	0x0400
90 #define	MODE_FINAL		0x1000
91 
92 
93 /* CK_INFO: Information about cryptoki */
94 #define	METASLOT_CRYPTOKI_VERSION_MAJOR	2
95 #define	METASLOT_CRYPTOKI_VERSION_MINOR	11
96 #define	METASLOT_MANUFACTURER_ID	"Sun Microsystems, Inc.          "
97 #define	METASLOT_LIBRARY_DESCRIPTION	"Sun Metaslot                    "
98 #define	METASLOT_LIBRARY_VERSION_MAJOR	1
99 #define	METASLOT_LIBRARY_VERSION_MINOR	1
100 
101 /* CK_SLOT_INFO */
102 #define	METASLOT_SLOT_DESCRIPTION	"Sun Metaslot                    " \
103 				"                                "
104 #define	METASLOT_HARDWARE_VERSION_MAJOR	0
105 #define	METASLOT_HARDWARE_VERSION_MINOR	0
106 #define	METASLOT_FIRMWARE_VERSION_MAJOR	0
107 #define	METASLOT_FIRMWARE_VERSION_MINOR	0
108 
109 /* CK_TOKEN_INFO: More information about token */
110 #define	METASLOT_TOKEN_LABEL		"Sun Metaslot                    "
111 #define	METASLOT_TOKEN_MODEL		"1.0             "
112 #define	RANDOM_DEVICE	"/dev/urandom"
113 
114 /*
115  * Maximum number of objects and sessions to queue up before actually
116  * freeing them using the free() system.  This is necessary to workaround
117  * a problem in which applications re-uses handles that are no longer valid
118  */
119 #define	MAX_OBJ_TO_BE_FREED	300
120 #define	MAX_SESSION_TO_BE_FREED	300
121 
122 /*
123  * The following 2 functions deals with inserting and deleting
124  * from double linked lists.  It can work with any data structure
125  * that have "prev" and "next" defined.
126  */
127 
128 /* This always inserts into the head of the list */
129 #define	INSERT_INTO_LIST(list, item)			\
130 {							\
131 	if ((list) == NULL) {				\
132 		(item)->prev = NULL;			\
133 		(item)->next = NULL;			\
134 		(list) = (item);			\
135 	} else {					\
136 		(item)->next = (list);			\
137 		(item)->prev = NULL;			\
138 		(list)->prev = (item);			\
139 		(list) = (item);			\
140 	}						\
141 }
142 
143 
144 /*
145  * Remove item from list
146  */
147 #define	REMOVE_FROM_LIST(list, item) 				\
148 {								\
149 	if ((list) == item) {					\
150 		if ((item)->next == NULL) {			\
151 			(list) = NULL;				\
152 		} else {					\
153 			(item)->next->prev = NULL;		\
154 			(list) = (item)->next;			\
155 		}						\
156 	} else {						\
157 		if ((item)->next) {				\
158 			(item)->next->prev = item->prev;	\
159 			(item)->prev->next = (item)->next;	\
160 		} else {					\
161 			(item)->prev->next = NULL;		\
162 		}						\
163 	}							\
164 }
165 
166 /*
167  * OBJRELEASE
168  *
169  * Signal that a metaobject is no longer in use (but is still valid).
170  */
171 #define	OBJRELEASE(object)						\
172 	if (object != NULL) {						\
173 		(void) pthread_rwlock_unlock(&object->object_lock);	\
174 	}
175 
176 /*
177  * REFRELEASE
178  *
179  * Signal that a metasession is no longer in use (but is still valid).
180  *
181  */
182 #define	REFRELEASE(session)						\
183 	if (session != NULL) {						\
184 		(void) pthread_rwlock_unlock(&session->session_lock);	\
185 	}
186 
187 /* FreeObject/FreeToken Enumeration */
188 typedef enum {
189 	FREE_UNCHECKED = 0,	/* Has not been checked */
190 	FREE_DISABLED = 1,	/* No supported provider or key type */
191 	FREE_ALLOWED_KEY = 2,	/* Supported key type */
192 	FREE_ENABLED = 3	/* FreeObject/Token enabled */
193 } freeobject_state_t;
194 
195 
196 /* Generic attribute type, for storing and managing PKCS#11 attributes. */
197 typedef struct _attr {
198 	CK_ATTRIBUTE attribute;
199 
200 	boolean_t isMalloced;
201 
202 	/* attr is necessary for creating a clone of the object */
203 	boolean_t isCloneAttr;
204 
205 	/*
206 	 * depends on the PKCS#11 implementation, this attr might or might
207 	 * not have a value.  It's OK for it to not have a value
208 	 * (ie: the default value is empty)
209 	 */
210 	boolean_t canBeEmptyValue;
211 
212 	boolean_t hasValueForClone;
213 
214 	CK_BBOOL generic_bbool;
215 	CK_ULONG generic_ulong;
216 	CK_BYTE generic_data[INITIAL_ATTR_LEN];
217 } generic_attr_t;
218 
219 /*
220  * These need to be defined here before the actual structures are defined
221  * because they are used in some of the structure definitions.
222  */
223 typedef struct slotobject slot_object_t;
224 typedef struct metasession meta_session_t;
225 typedef struct metaobject meta_object_t;
226 typedef struct metaopstate meta_opstate_t;
227 
228 /*
229  * slot_session_t
230  *
231  * Wrapper for a session on a provider. This structure is only used internally
232  * in metaslot; it is never revealed to applications.
233  */
234 typedef struct slotsession {
235 	CK_ULONG slotnum;
236 	CK_SLOT_ID fw_st_id; /* used for accessing framework's slottable */
237 	CK_SESSION_HANDLE hSession;
238 
239 	boolean_t is_dualop_capable;
240 	CK_FLAGS session_flags;	/* what type of session */
241 
242 	struct slotsession *next;
243 	struct slotsession *prev;
244 
245 	pthread_rwlock_t object_list_lock;
246 	slot_object_t *object_list_head;
247 } slot_session_t;
248 
249 
250 /*
251  * slot_object_t
252  *
253  * Wrapper for an object on a provider. This structure is only used internally
254  * in metaslot; it is never revealed to applications.
255  */
256 struct slotobject {
257 	CK_OBJECT_HANDLE hObject;
258 
259 	struct slotobject *next;
260 	struct slotobject *prev;
261 
262 	slot_session_t *creator_session;
263 
264 	boolean_t isToken;
265 };
266 
267 
268 /*
269  * mechinfo_t
270  *
271  * A mechinfo_t is created for each mechanism on a slot.
272  *
273  * This information is used for selecting which slots support the given
274  * mechanism for a crypto operation.
275  *
276  */
277 typedef struct mechinfo {
278 	CK_ULONG slotnum;
279 
280 	boolean_t initialized;
281 	boolean_t supported;
282 	CK_MECHANISM_INFO mechanism_info;
283 } mechinfo_t;
284 
285 
286 /*
287  * operation_info_t
288  *
289  * Part of a meta_session_t, used to track active operations.
290  */
291 typedef struct opinfo {
292 	CK_FLAGS type;
293 	slot_session_t *session;
294 	mechinfo_t *stats;
295 } operation_info_t;
296 
297 typedef struct find_objs_info {
298 	boolean_t op_active;	/* Indicate whether FindObjects is active */
299 	meta_object_t **matched_objs;
300 	int num_matched_objs;
301 	int next_result_index;	/* index of next object to be returned */
302 } find_objs_info_t;
303 
304 typedef struct mech_support_info {
305 	CK_MECHANISM_TYPE mech;
306 	/* Array of mechinfo_t allocated based on number of slots */
307 	mechinfo_t **supporting_slots;
308 	unsigned long num_supporting_slots;
309 } mech_support_info_t;
310 
311 typedef struct	crypto_init {
312 	CK_FLAGS optype;		/* place holder for init parameters */
313 	struct metasession *session;	/* place holder for init parameters */
314 	CK_MECHANISM *pMech;		/* place holder for init parameters */
315 	struct metaobject *key;		/* place holder for init parameters */
316 	CK_ULONG slotnum;	/* slot where the init operation took place */
317 	boolean_t done;		/* set when the real init is done */
318 	boolean_t app;		/* set when C_xxxInit is called by app */
319 } crypto_init_t;
320 
321 /*
322  * meta_session_t
323  *
324  * The internal state for a meta-session is kept here. The session handles
325  * given to applications are always pointers to a structure of this type.
326  *
327  */
328 struct metasession {
329 	ulong_t magic_marker;
330 	pthread_rwlock_t session_lock;
331 
332 	pthread_mutex_t isClosingSession_lock;
333 	boolean_t isClosingSession;
334 
335 	struct metasession *next;
336 	struct metasession *prev;
337 
338 	CK_FLAGS session_flags;
339 
340 	/*
341 	 * Could have just declared this as "op", but declaring it as
342 	 * op1 so that "op2" can be easily added when dual-op support
343 	 * is implemented in the future
344 	 */
345 	operation_info_t op1;
346 
347 	/*
348 	 * This is for keeping track of which slots support a particular
349 	 * mechanism.  This information doesn't
350 	 * have to be kept on a per session bases, but having the
351 	 * memory pre-allocated per session would make things much simpiler,
352 	 * because memory doesn't need to be allocated/deallocated everytime
353 	 * we do an operation.
354 	 */
355 	mech_support_info_t mech_support_info;
356 
357 
358 	/* Session objects created by this session. */
359 	pthread_rwlock_t object_list_lock;
360 	meta_object_t *object_list_head;
361 
362 	/* C_FindObjects support. */
363 	find_objs_info_t find_objs_info;
364 
365 	/* deferred init to be used by digest, encrypt, decrypt */
366 	crypto_init_t	init;
367 };
368 
369 
370 /*
371  * meta_object_t
372  *
373  * The internal state for a meta-object is kept here. The object handles
374  * given to applications are always pointers to a structure of this type.
375  */
376 struct metaobject {
377 	ulong_t magic_marker;
378 	pthread_rwlock_t object_lock;
379 
380 	pthread_mutex_t isClosingObject_lock;
381 	boolean_t isClosingObject;
382 
383 	struct metaobject *next;
384 	struct metaobject *prev;
385 
386 	meta_session_t *creator_session; /* Only set for session objects */
387 
388 	boolean_t isToken;		/* alias for CKA_TOKEN */
389 	boolean_t isPrivate;		/* alias for CKA_PRIVATE */
390 	boolean_t isSensitive;		/* alias for CKA_SENSITIVE */
391 	boolean_t isExtractable;	/* alias for CKA_EXTRACTABLE */
392 
393 	freeobject_state_t isFreeToken;
394 	freeobject_state_t isFreeObject;
395 
396 	CK_ULONG master_clone_slotnum; /* set when object is created */
397 	slot_object_t **clones;
398 	/* indicate if tried to create clone object in a slot */
399 	boolean_t	*tried_create_clone;
400 
401 	pthread_rwlock_t attribute_lock;
402 	size_t num_attributes;
403 	generic_attr_t *attributes;
404 
405 	pthread_mutex_t clone_create_lock;
406 	size_t clone_template_size;	/* 0 if not yet known. */
407 	CK_ATTRIBUTE *clone_template; /* NULL if not yet known. */
408 };
409 
410 
411 /*
412  * struct metaopstate
413  *
414  * Used as the format for the operation state returned via
415  * C_GetOperationState.
416  */
417 typedef struct opstate_data {
418 	CK_FLAGS	op_type;
419 	CK_ULONG	op_slotnum;
420 	CK_ULONG	op_state_len;
421 	boolean_t	op_init_app;
422 	boolean_t	op_init_done;
423 } opstate_data_t;
424 
425 struct metaopstate {
426 	ulong_t magic_marker;
427 	/*
428 	 * Could have just declared this as "state", but declaring it like this
429 	 * so that when dual-op support is implemented in the future, the
430 	 * changes will be simplier.
431 	 */
432 	struct opstate_data state[1];
433 };
434 
435 
436 /*
437  * session_pool_t
438  *
439  * Used to cache open sessions in a slot.
440  */
441 typedef struct sessionpool {
442 	pthread_mutex_t list_lock;
443 
444 	/* list of sessions that's currently in use */
445 	slot_session_t *active_list_head;
446 
447 	/*
448 	 * list of sessions that are not in use, but can't be deleted because
449 	 * either session/token objects are created using these sessions
450 	 * or we need to have one session left with the provider to maintain
451 	 * the logged in state.  Any of these sessions could be re-used if
452 	 * a session is needed to be established with a provider.
453 	 */
454 	slot_session_t *persist_list_head;
455 
456 	/*
457 	 * List of sessions that are not in use at the moment.  We keep
458 	 * a list of sessions with a particular provider instead of
459 	 * creating a new session everytime for efficiency
460 	 */
461 	slot_session_t *idle_list_head;
462 	boolean_t keep_one_alive;
463 	int num_idle_sessions; /* number of sessions in "idle_list_head" */
464 } session_pool_t;
465 
466 
467 /*
468  * slot_data_t
469  *
470  * Each slot has a session pool, a collection of persistant sessions to
471  * allow for more efficient operation. Specifically, to allow reuse of
472  * previously session objects (which need the creating session to stick
473  * around), as well as being frugal with creating/closing sessions.
474  */
475 typedef struct slotdata {
476 	CK_SLOT_ID fw_st_id; /* framework slot table ID */
477 
478 	session_pool_t session_pool;
479 
480 	pthread_rwlock_t tokenobject_list_lock;
481 	slot_object_t *tokenobject_list_head;
482 } slot_data_t;
483 
484 
485 typedef enum {
486 	ALL_TOKEN = 0,
487 	PUBLIC_TOKEN = 1,
488 	PRIVATE_TOKEN = 2
489 } token_obj_type_t;
490 
491 /*
492  * metaslot_config_t
493  *
494  * This holds the configuration information for meta slot.
495  * It will first be filled with values that users defined
496  * in environment variables.  Any value not defined by the user
497  * will be filled with values from the system wide configuration file.
498  */
499 typedef struct _metaslot_config {
500 	/* token to be used as the keystore for metaslot */
501 	boolean_t keystore_token_specified;
502 	CK_UTF8CHAR keystore_token[TOKEN_LABEL_SIZE + 1];
503 
504 	/* slot to be used as the keystore for metaslot */
505 	boolean_t keystore_slot_specified;
506 	CK_UTF8CHAR keystore_slot[SLOT_DESCRIPTION_SIZE + 1];
507 
508 	/* should meta slot be enabled or not */
509 	boolean_t enabled_specified;
510 	boolean_t enabled;
511 
512 	/* should auto migration of sensitive token objects be enabled or not */
513 	boolean_t auto_key_migrate_specified;
514 	boolean_t auto_key_migrate;
515 } metaslot_config_t;
516 
517 /*
518  * The following 2 structures are used to link the to-be-freed
519  * meta sessions and meta objects into linked lists.
520  * The items on these linked list have not yet been freed via free(); instead
521  * they are added to this list. The actual free will take place when
522  * the number of objects queued reaches MAX_OBJ_TO_BE_FREED or
523  * MAX_SESSION_TO_BE_FREED, at which time the first object in the
524  * list will be freed.
525  */
526 typedef struct obj_to_be_freed_list {
527 	meta_object_t   *first; /* points to first obj in the list */
528 	meta_object_t   *last;  /* points to last obj in the list */
529 	uint32_t	count;  /* current total objs in the list */
530 	pthread_mutex_t	obj_to_be_free_mutex;
531 } object_to_be_freed_list_t;
532 
533 typedef struct ses_to_be_freed_list {
534 	meta_session_t *first; /* points to first session in the list */
535 	meta_session_t *last;  /* points to last session in the list */
536 	uint32_t	count;  /* current total session in the list */
537 	pthread_mutex_t ses_to_be_free_mutex;
538 } ses_to_be_freed_list_t;
539 
540 /* Global variables */
541 extern metaslot_config_t metaslot_config;
542 extern boolean_t metaslot_enabled;
543 extern CK_SLOT_ID metaslot_keystore_slotid;
544 extern boolean_t metaslot_auto_key_migrate;
545 extern struct CK_FUNCTION_LIST metaslot_functionList;
546 extern int meta_urandom_seed_fd;
547 extern pthread_mutex_t initmutex;
548 
549 extern ses_to_be_freed_list_t ses_delay_freed;
550 extern object_to_be_freed_list_t obj_delay_freed;
551 extern int (*Tmp_GetThreshold)(CK_MECHANISM_TYPE);
552 
553 extern CK_BBOOL falsevalue;
554 extern CK_BBOOL truevalue;
555 
556 /* --- Prototypes --- */
557 
558 CK_RV meta_slotManager_initialize();
559 void meta_slotManager_finalize();
560 void meta_slotManager_find_object_token();
561 CK_RV meta_get_slot_session(CK_ULONG slotnum, slot_session_t **session,
562     CK_FLAGS flags);
563 void meta_release_slot_session(slot_session_t *session);
564 
565 CK_RV meta_mechManager_initialize();
566 void meta_mechManager_finalize();
567 CK_RV meta_mechManager_get_mechs(CK_MECHANISM_TYPE *list, CK_ULONG *listsize);
568 CK_RV meta_mechManager_get_slots(mech_support_info_t  *mech_support_info,
569     boolean_t force_update, CK_MECHANISM_INFO *mech_info);
570 CK_RV meta_mechManager_slot_supports_mech(CK_MECHANISM_TYPE mechanism,
571     CK_ULONG slotnum, boolean_t *supports, mechinfo_t **slot_info,
572     boolean_t force_update, CK_MECHANISM_INFO *mech_info);
573 
574 CK_RV meta_operation_init(CK_FLAGS optype, meta_session_t *session,
575     CK_MECHANISM *pMechanism, meta_object_t *key);
576 CK_RV meta_operation_init_defer(CK_FLAGS optype, meta_session_t *session,
577     CK_MECHANISM *pMechanism, meta_object_t *key);
578 CK_RV meta_do_operation(CK_FLAGS optype, int mode,
579     meta_session_t *session, meta_object_t *object,
580     CK_BYTE *in, CK_ULONG inLen, CK_BYTE *out, CK_ULONG *outLen);
581 
582 void meta_operation_cleanup(meta_session_t *session, CK_FLAGS optype,
583     boolean_t finished_normally);
584 
585 CK_RV meta_generate_keys(meta_session_t *session, CK_MECHANISM *pMechanism,
586     CK_ATTRIBUTE *k1Template, CK_ULONG k1AttrCount, meta_object_t *key1,
587     CK_ATTRIBUTE *k2Template, CK_ULONG k2AttrCount, meta_object_t *key2);
588 
589 CK_RV meta_wrap_key(meta_session_t *session,
590     CK_MECHANISM *pMechanism, meta_object_t *wrappingkey,
591     meta_object_t *inputkey,
592     CK_BYTE *wrapped_key, CK_ULONG *wrapped_key_len);
593 
594 CK_RV meta_unwrap_key(meta_session_t *session,
595     CK_MECHANISM *pMechanism, meta_object_t *unwrapping_key,
596     CK_BYTE *wrapped_key, CK_ULONG wrapped_key_len,
597     CK_ATTRIBUTE *template, CK_ULONG template_size,
598     meta_object_t *unwrapped_key);
599 
600 CK_RV meta_derive_key(meta_session_t *session, CK_MECHANISM *pMech,
601     meta_object_t *basekey1, meta_object_t *basekey2,
602     CK_OBJECT_HANDLE *phBaseKey2,
603     CK_ATTRIBUTE *pTemplate, CK_ULONG ulAttributeCount,
604     meta_object_t *newKey1, meta_object_t *newKey2,
605     meta_object_t *newKey3, meta_object_t *newKey4);
606 
607 void get_user_metaslot_config();
608 
609 CK_RV meta_sessionManager_initialize();
610 void meta_sessionManager_finalize();
611 CK_RV meta_handle2session(CK_SESSION_HANDLE hSession,
612     meta_session_t **session_p);
613 CK_RV meta_session_alloc(meta_session_t **newSession);
614 CK_RV meta_session_activate(meta_session_t *session);
615 CK_RV meta_session_deactivate(meta_session_t *session,
616     boolean_t have_sessionlist_lock);
617 void meta_session_dealloc(meta_session_t *session);
618 void meta_session_delay_free(meta_session_t *sp);
619 
620 CK_RV meta_objectManager_initialize();
621 void meta_objectManager_finalize();
622 CK_RV meta_handle2object(CK_OBJECT_HANDLE hObject, meta_object_t **object);
623 CK_RV meta_object_alloc(meta_session_t *session, meta_object_t **object);
624 CK_RV meta_object_get_attr(slot_session_t *slot_session,
625     CK_OBJECT_HANDLE hObject, meta_object_t *object);
626 void meta_object_activate(meta_object_t *object);
627 CK_RV meta_object_deactivate(meta_object_t *object, boolean_t have_list_lock,
628     boolean_t have_object_lock);
629 CK_RV meta_object_dealloc(meta_object_t *object, boolean_t nukeSourceObj);
630 CK_RV meta_slot_object_alloc(slot_object_t **object);
631 void meta_slot_object_activate(slot_object_t *object, slot_session_t *session,
632 	boolean_t isToken);
633 void meta_slot_object_deactivate(slot_object_t *object);
634 void meta_slot_object_dealloc(slot_object_t *object);
635 CK_RV meta_object_copyin(meta_object_t *object);
636 CK_RV meta_object_get_clone(meta_object_t *object,
637 	CK_ULONG slot_num, slot_session_t *slot_session,
638 	slot_object_t **clone);
639 meta_object_t *meta_object_find_by_handle(CK_OBJECT_HANDLE hObject,
640 	CK_ULONG slotnum, boolean_t token_only);
641 CK_RV meta_token_object_deactivate(token_obj_type_t token_type);
642 void meta_object_delay_free(meta_object_t *objp);
643 boolean_t meta_freeobject_set(meta_object_t *object, CK_ATTRIBUTE *tmpl,
644     CK_ULONG tmpl_len, boolean_t create);
645 CK_RV meta_freetoken_set(CK_ULONG slot_num, CK_BBOOL *current_value,
646     CK_ATTRIBUTE *tmpl, CK_ULONG tmpl_len);
647 boolean_t meta_freeobject_check(meta_session_t *session, meta_object_t *obj,
648     CK_MECHANISM *pMech, CK_ATTRIBUTE *tmpl, CK_ULONG tmpl_len,
649     CK_KEY_TYPE keytype);
650 boolean_t meta_freeobject_clone(meta_session_t *session, meta_object_t *object);
651 
652 CK_RV get_master_attributes_by_object(slot_session_t *session,
653     slot_object_t *slot_object, generic_attr_t **attributes,
654     size_t *num_attributes);
655 CK_RV get_master_attributes_by_template(
656 	CK_ATTRIBUTE *template, CK_ULONG template_size,
657 	generic_attr_t **attributes, size_t *num_attributes);
658 CK_RV get_master_template_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
659 	generic_attr_t **attributes, size_t *num_attributes);
660 CK_RV get_master_attributes_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
661 	generic_attr_t **attributes, size_t *num_attributes);
662 CK_RV get_master_attributes_by_duplication(
663 	generic_attr_t *src_attrs, size_t num_src_attrs,
664 	generic_attr_t **dst_attrs, size_t *num_dst_attrs);
665 void dealloc_attributes(generic_attr_t *attributes, size_t num_attributes);
666 CK_RV attribute_set_value(CK_ATTRIBUTE *new_attr,
667 	generic_attr_t *attributes, size_t num_attributes);
668 boolean_t get_template_ulong(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
669 	CK_ULONG num_attributes, CK_ULONG *result);
670 boolean_t get_template_boolean(CK_ATTRIBUTE_TYPE type,
671     CK_ATTRIBUTE *attributes, CK_ULONG num_attributes, boolean_t *result);
672 int set_template_boolean(CK_ATTRIBUTE_TYPE type,
673     CK_ATTRIBUTE *attributes, CK_ULONG num_attributes, boolean_t local,
674     CK_BBOOL *value);
675 CK_ULONG get_keystore_slotnum(void);
676 CK_ULONG get_softtoken_slotnum(void);
677 CK_SLOT_ID meta_slotManager_get_framework_table_id(CK_ULONG slotnum);
678 CK_ULONG meta_slotManager_get_slotcount(void);
679 boolean_t meta_slotManager_token_write_protected(void);
680 boolean_t metaslot_logged_in();
681 void metaslot_set_logged_in_flag(boolean_t value);
682 
683 int looping_read(int, void *, int);
684 int looping_write(int, void *, int);
685 
686 /*
687  * Prototypes for the various meta_Foo implementations of C_Foo.
688  *
689  */
690 CK_RV meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList);
691 CK_RV meta_Initialize(CK_VOID_PTR pInitArgs);
692 CK_RV meta_Finalize(CK_VOID_PTR pReserved);
693 CK_RV meta_GetInfo(CK_INFO_PTR pInfo);
694 CK_RV meta_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList,
695     CK_ULONG_PTR pulCount);
696 CK_RV meta_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo);
697 CK_RV meta_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo);
698 CK_RV meta_GetMechanismList(CK_SLOT_ID slotID,
699     CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pulCount);
700 CK_RV meta_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
701     CK_MECHANISM_INFO_PTR pInfo);
702 CK_RV meta_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin,
703     CK_ULONG ulPinLen, CK_UTF8CHAR_PTR pLabel);
704 CK_RV meta_InitPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin,
705     CK_ULONG ulPinLen);
706 CK_RV meta_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
707     CK_ULONG ulOldPinLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewPinLen);
708 CK_RV meta_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags,
709     CK_VOID_PTR pApplication, CK_NOTIFY Notify,
710     CK_SESSION_HANDLE_PTR phSession);
711 CK_RV meta_CloseSession(CK_SESSION_HANDLE hSession);
712 CK_RV meta_CloseAllSessions(CK_SLOT_ID slotID);
713 CK_RV meta_GetSessionInfo(CK_SESSION_HANDLE hSession,
714     CK_SESSION_INFO_PTR pInfo);
715 CK_RV meta_GetOperationState(CK_SESSION_HANDLE hSession,
716     CK_BYTE_PTR pOperationState, CK_ULONG_PTR pulOperationStateLen);
717 CK_RV meta_SetOperationState(CK_SESSION_HANDLE hSession,
718     CK_BYTE_PTR pOperationState, CK_ULONG ulOperationStateLen,
719     CK_OBJECT_HANDLE hEncryptionKey, CK_OBJECT_HANDLE hAuthenticationKey);
720 CK_RV meta_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
721     CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen);
722 CK_RV meta_Logout(CK_SESSION_HANDLE hSession);
723 CK_RV meta_CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
724     CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject);
725 CK_RV meta_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
726     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
727     CK_OBJECT_HANDLE_PTR phNewObject);
728 CK_RV meta_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject);
729 CK_RV meta_GetObjectSize(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
730     CK_ULONG_PTR pulSize);
731 CK_RV meta_GetAttributeValue(CK_SESSION_HANDLE hSession,
732     CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount);
733 CK_RV meta_SetAttributeValue(CK_SESSION_HANDLE hSession,
734     CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount);
735 CK_RV meta_FindObjectsInit(CK_SESSION_HANDLE hSession,
736     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount);
737 CK_RV meta_FindObjects(CK_SESSION_HANDLE hSession,
738     CK_OBJECT_HANDLE_PTR phObject, CK_ULONG ulMaxObjectCount,
739     CK_ULONG_PTR pulObjectCount);
740 CK_RV meta_FindObjectsFinal(CK_SESSION_HANDLE hSession);
741 CK_RV meta_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
742     CK_OBJECT_HANDLE hKey);
743 CK_RV meta_Encrypt(CK_SESSION_HANDLE hSession,
744     CK_BYTE_PTR pData, CK_ULONG ulDataLen,
745     CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen);
746 CK_RV meta_EncryptUpdate(CK_SESSION_HANDLE hSession,
747     CK_BYTE_PTR pPart, CK_ULONG ulPartLen,
748     CK_BYTE_PTR pEncryptedPart, CK_ULONG_PTR pulEncryptedPartLen);
749 CK_RV meta_EncryptFinal(CK_SESSION_HANDLE hSession,
750     CK_BYTE_PTR pLastEncryptedPart, CK_ULONG_PTR pulLastEncryptedPartLen);
751 CK_RV meta_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
752     CK_OBJECT_HANDLE hKey);
753 CK_RV meta_Decrypt(CK_SESSION_HANDLE hSession,
754     CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen,
755     CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen);
756 CK_RV meta_DecryptUpdate(CK_SESSION_HANDLE hSession,
757     CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
758     CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen);
759 CK_RV meta_DecryptFinal(CK_SESSION_HANDLE hSession,
760     CK_BYTE_PTR pLastPart, CK_ULONG_PTR pulLastPartLen);
761 CK_RV meta_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism);
762 CK_RV meta_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
763     CK_ULONG ulDataLen, CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen);
764 CK_RV meta_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
765     CK_ULONG ulPartLen);
766 CK_RV meta_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey);
767 CK_RV meta_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
768     CK_ULONG_PTR pulDigestLen);
769 CK_RV meta_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
770     CK_OBJECT_HANDLE hKey);
771 CK_RV meta_Sign(CK_SESSION_HANDLE hSession,
772     CK_BYTE_PTR pData, CK_ULONG ulDataLen,
773     CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen);
774 CK_RV meta_SignUpdate(CK_SESSION_HANDLE hSession,
775     CK_BYTE_PTR pPart, CK_ULONG ulPartLen);
776 CK_RV meta_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
777     CK_ULONG_PTR pulSignatureLen);
778 CK_RV meta_SignRecoverInit(CK_SESSION_HANDLE hSession,
779     CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey);
780 CK_RV meta_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
781     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen);
782 CK_RV meta_VerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
783     CK_OBJECT_HANDLE hKey);
784 CK_RV meta_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
785     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen);
786 CK_RV meta_VerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
787     CK_ULONG ulPartLen);
788 CK_RV meta_VerifyFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
789     CK_ULONG ulSignatureLen);
790 CK_RV meta_VerifyRecoverInit(CK_SESSION_HANDLE hSession,
791     CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey);
792 CK_RV meta_VerifyRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
793     CK_ULONG ulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen);
794 CK_RV meta_DigestEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
795     CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
796     CK_ULONG_PTR pulEncryptedPartLen);
797 CK_RV meta_DecryptDigestUpdate(CK_SESSION_HANDLE hSession,
798     CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
799     CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen);
800 CK_RV meta_SignEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
801     CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
802     CK_ULONG_PTR pulEncryptedPartLen);
803 CK_RV meta_DecryptVerifyUpdate(CK_SESSION_HANDLE hSession,
804     CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
805     CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen);
806 CK_RV meta_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
807     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey);
808 CK_RV meta_GenerateKeyPair(CK_SESSION_HANDLE hSession,
809     CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pPublicKeyTemplate,
810     CK_ULONG ulPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
811     CK_ULONG ulPrivateKeyAttributeCount, CK_OBJECT_HANDLE_PTR phPublicKey,
812     CK_OBJECT_HANDLE_PTR phPrivateKey);
813 CK_RV meta_WrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
814     CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
815     CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen);
816 CK_RV meta_UnwrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
817     CK_OBJECT_HANDLE hUnwrappingKey, CK_BYTE_PTR pWrappedKey,
818     CK_ULONG ulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate,
819     CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey);
820 CK_RV meta_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
821     CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
822     CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey);
823 CK_RV meta_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed,
824     CK_ULONG ulSeedLen);
825 CK_RV meta_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
826     CK_ULONG ulRandomLen);
827 CK_RV meta_GetFunctionStatus(CK_SESSION_HANDLE hSession);
828 CK_RV meta_CancelFunction(CK_SESSION_HANDLE hSession);
829 CK_RV meta_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot,
830     CK_VOID_PTR pReserved);
831 
832 #ifdef	__cplusplus
833 }
834 #endif
835 
836 #endif /* _METAGLOBAL_H */
837