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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_KERNELOBJECT_H
28 #define	_KERNELOBJECT_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <security/pkcs11t.h>
37 #include "kernelSession.h"
38 #include "kernelSlot.h"
39 
40 #define	KERNELTOKEN_OBJECT_MAGIC	0xECF0B003
41 
42 #define	RSA_PRI_ATTR_COUNT		7
43 #define	RSA_PUB_ATTR_COUNT		3
44 #define	DSA_ATTR_COUNT			4
45 
46 /*
47  * Secret key Struct
48  */
49 typedef struct secret_key_obj {
50 	CK_BYTE *sk_value;
51 	CK_ULONG sk_value_len;
52 } secret_key_obj_t;
53 
54 
55 /*
56  * This structure is used to hold the attributes in the
57  * Extra Attribute List.
58  */
59 typedef struct attribute_info {
60 	CK_ATTRIBUTE	attr;
61 	struct attribute_info *next;
62 } attribute_info_t;
63 
64 typedef attribute_info_t *CK_ATTRIBUTE_INFO_PTR;
65 
66 
67 /*
68  * biginteger Struct
69  */
70 typedef struct biginteger {
71 	CK_BYTE *big_value;
72 	CK_ULONG big_value_len;
73 } biginteger_t;
74 
75 
76 /*
77  * PKCS11: RSA Public Key Object Attributes
78  */
79 typedef struct rsa_pub_key {
80 	biginteger_t modulus;
81 	CK_ULONG modulus_bits;
82 	biginteger_t pub_exponent;
83 } rsa_pub_key_t;
84 
85 
86 /*
87  * PKCS11: DSA Public Key Object Attributes
88  */
89 typedef struct dsa_pub_key {
90 	biginteger_t prime;
91 	biginteger_t subprime;
92 	biginteger_t base;
93 	biginteger_t value;
94 } dsa_pub_key_t;
95 
96 
97 /*
98  * Public Key Main Struct
99  */
100 typedef struct public_key_obj {
101 	union {
102 		rsa_pub_key_t rsa_pub_key; /* RSA public key */
103 		dsa_pub_key_t dsa_pub_key; /* DSA public key */
104 	} key_type_u;
105 } public_key_obj_t;
106 
107 
108 /*
109  * PKCS11: RSA Private Key Object Attributes
110  */
111 typedef struct rsa_pri_key {
112 	biginteger_t modulus;
113 	biginteger_t pub_exponent;
114 	biginteger_t pri_exponent;
115 	biginteger_t prime_1;
116 	biginteger_t prime_2;
117 	biginteger_t exponent_1;
118 	biginteger_t exponent_2;
119 	biginteger_t coefficient;
120 } rsa_pri_key_t;
121 
122 
123 /*
124  * PKCS11: DSA Private Key Object Attributes
125  */
126 typedef struct dsa_pri_key {
127 	biginteger_t prime;
128 	biginteger_t subprime;
129 	biginteger_t base;
130 	biginteger_t value;
131 } dsa_pri_key_t;
132 
133 
134 /*
135  * Private Key Main Struct
136  */
137 typedef struct private_key_obj {
138 	union {
139 		rsa_pri_key_t rsa_pri_key; /* RSA private key */
140 		dsa_pri_key_t dsa_pri_key; /* DSA private key */
141 	} key_type_u;
142 } private_key_obj_t;
143 
144 
145 /*
146  * This is the main structure of the Objects.
147  */
148 typedef struct object {
149 	boolean_t	is_lib_obj; /* default is TRUE */
150 	crypto_object_id_t	k_handle;
151 
152 	/* Generic common fields. Always present */
153 	CK_OBJECT_CLASS class;
154 	CK_KEY_TYPE key_type;
155 	CK_ULONG magic_marker;
156 	uint64_t bool_attr_mask;
157 	CK_MECHANISM_TYPE mechanism;
158 
159 	/* Fields for access and arbitration */
160 	pthread_mutex_t object_mutex;
161 	struct object *next;
162 	struct object *prev;
163 
164 	/* Extra non-boolean attribute list */
165 	CK_ATTRIBUTE_INFO_PTR extra_attrlistp;
166 	CK_ULONG extra_attrcount;
167 
168 	/* For each object, only one object class is presented */
169 	union {
170 		secret_key_obj_t  *secret_key;
171 		public_key_obj_t  *public_key;
172 		private_key_obj_t *private_key;
173 	} object_class_u;
174 
175 	/* Session handle that the object belongs to */
176 	CK_SESSION_HANDLE	session_handle;
177 
178 } kernel_object_t;
179 
180 
181 typedef struct find_context {
182 	kernel_object_t **objs_found;
183 	CK_ULONG num_results;
184 	CK_ULONG next_result_index; /* next result object to return */
185 } find_context_t;
186 
187 /*
188  * The following definitions are the shortcuts
189  */
190 
191 /*
192  * Secret Key Object Attributes
193  */
194 #define	OBJ_SEC(o) \
195 	(o->object_class_u.secret_key)
196 #define	OBJ_SEC_VALUE(o) \
197 	(o->object_class_u.secret_key->sk_value)
198 #define	OBJ_SEC_VALUE_LEN(o) \
199 	(o->object_class_u.secret_key->sk_value_len)
200 
201 /*
202  * RSA Public Key Object Attributes
203  */
204 #define	OBJ_PUB(o) \
205 	((o)->object_class_u.public_key)
206 #define	KEY_PUB_RSA(k) \
207 	&((k)->key_type_u.rsa_pub_key)
208 #define	OBJ_PUB_RSA_MOD(o) \
209 	&((o)->object_class_u.public_key->key_type_u.rsa_pub_key.modulus)
210 #define	KEY_PUB_RSA_MOD(k) \
211 	&((k)->key_type_u.rsa_pub_key.modulus)
212 #define	OBJ_PUB_RSA_PUBEXPO(o) \
213 	&((o)->object_class_u.public_key->key_type_u.rsa_pub_key.pub_exponent)
214 #define	KEY_PUB_RSA_PUBEXPO(k) \
215 	&((k)->key_type_u.rsa_pub_key.pub_exponent)
216 #define	OBJ_PUB_RSA_MOD_BITS(o) \
217 	((o)->object_class_u.public_key->key_type_u.rsa_pub_key.modulus_bits)
218 #define	KEY_PUB_RSA_MOD_BITS(k) \
219 	((k)->key_type_u.rsa_pub_key.modulus_bits)
220 
221 
222 /*
223  * DSA Public Key Object Attributes
224  */
225 #define	KEY_PUB_DSA(k) \
226 	&((k)->key_type_u.dsa_pub_key)
227 #define	OBJ_PUB_DSA_PRIME(o) \
228 	&((o)->object_class_u.public_key->key_type_u.dsa_pub_key.prime)
229 #define	KEY_PUB_DSA_PRIME(k) \
230 	&((k)->key_type_u.dsa_pub_key.prime)
231 #define	OBJ_PUB_DSA_SUBPRIME(o) \
232 	&((o)->object_class_u.public_key->key_type_u.dsa_pub_key.subprime)
233 #define	KEY_PUB_DSA_SUBPRIME(k) \
234 	&((k)->key_type_u.dsa_pub_key.subprime)
235 #define	OBJ_PUB_DSA_BASE(o) \
236 	&((o)->object_class_u.public_key->key_type_u.dsa_pub_key.base)
237 #define	KEY_PUB_DSA_BASE(k) \
238 	&((k)->key_type_u.dsa_pub_key.base)
239 #define	OBJ_PUB_DSA_VALUE(o) \
240 	&((o)->object_class_u.public_key->key_type_u.dsa_pub_key.value)
241 #define	KEY_PUB_DSA_VALUE(k) \
242 	&((k)->key_type_u.dsa_pub_key.value)
243 
244 
245 /*
246  * RSA Private Key Object Attributes
247  */
248 #define	OBJ_PRI(o) \
249 	((o)->object_class_u.private_key)
250 #define	KEY_PRI_RSA(k) \
251 	&((k)->key_type_u.rsa_pri_key)
252 #define	OBJ_PRI_RSA_MOD(o) \
253 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.modulus)
254 #define	KEY_PRI_RSA_MOD(k) \
255 	&((k)->key_type_u.rsa_pri_key.modulus)
256 #define	OBJ_PRI_RSA_PUBEXPO(o) \
257 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.pub_exponent)
258 #define	KEY_PRI_RSA_PUBEXPO(k) \
259 	&((k)->key_type_u.rsa_pri_key.pub_exponent)
260 #define	OBJ_PRI_RSA_PRIEXPO(o) \
261 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.pri_exponent)
262 #define	KEY_PRI_RSA_PRIEXPO(k) \
263 	&((k)->key_type_u.rsa_pri_key.pri_exponent)
264 #define	OBJ_PRI_RSA_PRIME1(o) \
265 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.prime_1)
266 #define	KEY_PRI_RSA_PRIME1(k) \
267 	&((k)->key_type_u.rsa_pri_key.prime_1)
268 #define	OBJ_PRI_RSA_PRIME2(o) \
269 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.prime_2)
270 #define	KEY_PRI_RSA_PRIME2(k) \
271 	&((k)->key_type_u.rsa_pri_key.prime_2)
272 #define	OBJ_PRI_RSA_EXPO1(o) \
273 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.exponent_1)
274 #define	KEY_PRI_RSA_EXPO1(k) \
275 	&((k)->key_type_u.rsa_pri_key.exponent_1)
276 #define	OBJ_PRI_RSA_EXPO2(o) \
277 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.exponent_2)
278 #define	KEY_PRI_RSA_EXPO2(k) \
279 	&((k)->key_type_u.rsa_pri_key.exponent_2)
280 #define	OBJ_PRI_RSA_COEF(o) \
281 	&((o)->object_class_u.private_key->key_type_u.rsa_pri_key.coefficient)
282 #define	KEY_PRI_RSA_COEF(k) \
283 	&((k)->key_type_u.rsa_pri_key.coefficient)
284 
285 /*
286  * DSA Private Key Object Attributes
287  */
288 #define	KEY_PRI_DSA(k) \
289 	&((k)->key_type_u.dsa_pri_key)
290 #define	OBJ_PRI_DSA_PRIME(o) \
291 	&((o)->object_class_u.private_key->key_type_u.dsa_pri_key.prime)
292 #define	KEY_PRI_DSA_PRIME(k) \
293 	&((k)->key_type_u.dsa_pri_key.prime)
294 #define	OBJ_PRI_DSA_SUBPRIME(o) \
295 	&((o)->object_class_u.private_key->key_type_u.dsa_pri_key.subprime)
296 #define	KEY_PRI_DSA_SUBPRIME(k) \
297 	&((k)->key_type_u.dsa_pri_key.subprime)
298 #define	OBJ_PRI_DSA_BASE(o) \
299 	&((o)->object_class_u.private_key->key_type_u.dsa_pri_key.base)
300 #define	KEY_PRI_DSA_BASE(k) \
301 	&((k)->key_type_u.dsa_pri_key.base)
302 #define	OBJ_PRI_DSA_VALUE(o) \
303 	&((o)->object_class_u.private_key->key_type_u.dsa_pri_key.value)
304 #define	KEY_PRI_DSA_VALUE(k) \
305 	&((k)->key_type_u.dsa_pri_key.value)
306 
307 /*
308  * key related attributes with CK_BBOOL data type
309  */
310 #define	DERIVE_BOOL_ON			0x00000001
311 #define	LOCAL_BOOL_ON			0x00000002
312 #define	SENSITIVE_BOOL_ON		0x00000004
313 #define	SECONDARY_AUTH_BOOL_ON		0x00000008
314 #define	ENCRYPT_BOOL_ON			0x00000010
315 #define	DECRYPT_BOOL_ON			0x00000020
316 #define	SIGN_BOOL_ON			0x00000040
317 #define	SIGN_RECOVER_BOOL_ON		0x00000080
318 #define	VERIFY_BOOL_ON			0x00000100
319 #define	VERIFY_RECOVER_BOOL_ON		0x00000200
320 #define	WRAP_BOOL_ON			0x00000400
321 #define	UNWRAP_BOOL_ON			0x00000800
322 #define	TRUSTED_BOOL_ON			0x00001000
323 #define	EXTRACTABLE_BOOL_ON		0x00002000
324 #define	ALWAYS_SENSITIVE_BOOL_ON	0x00004000
325 #define	NEVER_EXTRACTABLE_BOOL_ON	0x00008000
326 #define	PRIVATE_BOOL_ON			0x00010000
327 #define	TOKEN_BOOL_ON			0x00020000
328 #define	MODIFIABLE_BOOL_ON		0x00040000
329 
330 #define	SECRET_KEY_DEFAULT	(ENCRYPT_BOOL_ON|\
331 				DECRYPT_BOOL_ON|\
332 				SIGN_BOOL_ON|\
333 				VERIFY_BOOL_ON|\
334 				EXTRACTABLE_BOOL_ON|\
335 				MODIFIABLE_BOOL_ON)
336 
337 #define	PUBLIC_KEY_DEFAULT	(ENCRYPT_BOOL_ON|\
338 				VERIFY_BOOL_ON|\
339 				VERIFY_RECOVER_BOOL_ON|\
340 				MODIFIABLE_BOOL_ON)
341 
342 #define	PRIVATE_KEY_DEFAULT	(DECRYPT_BOOL_ON|\
343 				SIGN_BOOL_ON|\
344 				SIGN_RECOVER_BOOL_ON|\
345 				EXTRACTABLE_BOOL_ON|\
346 				MODIFIABLE_BOOL_ON)
347 
348 /*
349  * This macro is used to type cast an object handle to a pointer to
350  * the object struct. Also, it checks to see if the object struct
351  * is tagged with an object magic number. This is to detect when an
352  * application passes a bogus object pointer.
353  */
354 #define	HANDLE2OBJECT(hObject, object_p, rv) \
355 	if (hObject == NULL) { \
356 		rv = CKR_OBJECT_HANDLE_INVALID; \
357 	} else { \
358 		object_p = (kernel_object_t *)(hObject); \
359 		rv = ((object_p->magic_marker == KERNELTOKEN_OBJECT_MAGIC) \
360 			? CKR_OK : CKR_OBJECT_HANDLE_INVALID); \
361 	}
362 
363 /*
364  * Function Prototypes.
365  */
366 void kernel_cleanup_object(kernel_object_t *objp);
367 
368 CK_RV kernel_add_object(CK_ATTRIBUTE_PTR pTemplate,  CK_ULONG ulCount,
369     CK_ULONG *objecthandle_p, kernel_session_t *sp);
370 
371 CK_RV kernel_delete_session_object(kernel_session_t *sp, kernel_object_t *objp,
372     boolean_t lock_held, boolean_t wrapper_only);
373 
374 void kernel_cleanup_extra_attr(kernel_object_t *object_p);
375 
376 CK_RV kernel_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp,
377     kernel_object_t *object_p);
378 
379 void kernel_cleanup_object_bigint_attrs(kernel_object_t *object_p);
380 
381 CK_RV kernel_build_object(CK_ATTRIBUTE_PTR template,
382     CK_ULONG ulAttrNum, kernel_object_t *new_object, kernel_session_t *sp);
383 
384 CK_RV kernel_copy_object(kernel_object_t *old_object,
385     kernel_object_t **new_object, boolean_t copy_everything,
386     kernel_session_t *sp);
387 
388 void kernel_merge_object(kernel_object_t *old_object,
389     kernel_object_t *new_object);
390 
391 CK_RV kernel_get_attribute(kernel_object_t *object_p,
392     CK_ATTRIBUTE_PTR template);
393 
394 CK_RV kernel_set_attribute(kernel_object_t *object_p,
395     CK_ATTRIBUTE_PTR template, boolean_t copy, kernel_session_t *sp);
396 
397 void copy_bigint_attr(biginteger_t *src, biginteger_t *dst);
398 
399 void kernel_add_object_to_session(kernel_object_t *objp, kernel_session_t *sp);
400 
401 CK_RV kernel_copy_public_key_attr(public_key_obj_t *old_pub_key_obj_p,
402     public_key_obj_t **new_pub_key_obj_p, CK_KEY_TYPE key_type);
403 
404 CK_RV kernel_copy_private_key_attr(private_key_obj_t *old_pri_key_obj_p,
405     private_key_obj_t **new_pri_key_obj_p, CK_KEY_TYPE key_type);
406 
407 CK_RV kernel_copy_secret_key_attr(secret_key_obj_t *old_secret_key_obj_p,
408     secret_key_obj_t **new_secret_key_obj_p);
409 
410 CK_RV kernel_validate_attr(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
411     CK_OBJECT_CLASS *class);
412 
413 CK_RV kernel_find_objects_init(kernel_session_t *sp,
414     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount);
415 
416 void kernel_find_objects_final(kernel_session_t *sp);
417 
418 void kernel_find_objects(kernel_session_t *sp,
419     CK_OBJECT_HANDLE *obj_found, CK_ULONG max_obj_requested,
420     CK_ULONG *found_obj_count);
421 
422 void kernel_process_find_attr(CK_OBJECT_CLASS *pclasses,
423     CK_ULONG *num_result_pclasses, CK_ATTRIBUTE_PTR pTemplate,
424     CK_ULONG ulCount);
425 
426 boolean_t kernel_find_match_attrs(kernel_object_t *obj,
427     CK_OBJECT_CLASS *pclasses, CK_ULONG num_pclasses,
428     CK_ATTRIBUTE *tmpl_attr, CK_ULONG num_attr);
429 
430 CK_ATTRIBUTE_PTR get_extra_attr(CK_ATTRIBUTE_TYPE type, kernel_object_t *obj);
431 
432 CK_RV get_string_from_template(CK_ATTRIBUTE_PTR dest, CK_ATTRIBUTE_PTR src);
433 
434 void string_attr_cleanup(CK_ATTRIBUTE_PTR template);
435 
436 void kernel_add_token_object_to_slot(kernel_object_t *objp,
437     kernel_slot_t *pslot);
438 
439 void kernel_remove_token_object_from_slot(kernel_slot_t *pslot,
440     kernel_object_t *objp);
441 
442 CK_RV kernel_delete_token_object(kernel_slot_t *pslot, kernel_session_t *sp,
443     kernel_object_t *obj, boolean_t lock_held, boolean_t wrapper_only);
444 
445 void kernel_cleanup_pri_objects_in_slot(kernel_slot_t *pslot,
446     kernel_session_t *sp);
447 
448 CK_RV kernel_get_object_size(kernel_object_t *objp, CK_ULONG_PTR pulSize);
449 
450 #ifdef	__cplusplus
451 }
452 #endif
453 
454 #endif /* _KERNELOBJECT_H */
455