xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softKeystore.h (revision b885580b43755ee4ea1e280b85428893d2ba9291)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SOFTKEYSTORE_H
27 #define	_SOFTKEYSTORE_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/types.h>
34 #include <security/pkcs11t.h>
35 
36 #define	PBKD2_SALT_SIZE	16
37 #define	PBKD2_ITERATIONS (1000)
38 #define	PWD_BUFFER_SIZE	1024
39 
40 /*
41  * The following structure is the object header
42  * in the keystore.
43  */
44 typedef struct ks_obj_hdr {
45 	uint64_t class;
46 	uint64_t key_type;
47 	uint64_t cert_type;
48 	uint64_t bool_attr_mask;
49 	uint64_t mechanism;
50 	uchar_t object_type;
51 
52 	/* Extra non-boolean attribute list */
53 	int	num_attrs;
54 } ks_obj_hdr_t;
55 
56 /*
57  * This structure contains the individual attribute
58  * (from extra_attrlistp) in the keystore.
59  */
60 typedef struct ks_attr_hdr {
61 	uint64_t type;
62 	uint64_t ulValueLen;
63 } ks_attr_hdr_t;
64 
65 #define	ROUNDUP(x, y)	roundup(x, y)	/* defined in sys/sysmacros.h */
66 
67 #ifdef _LITTLE_ENDIAN
68 #define	SWAP16(value)  \
69 	((((value) & 0xff) << 8) | ((value) >> 8))
70 
71 #define	SWAP32(value)	\
72 	(((uint32_t)SWAP16((uint16_t)((value) & 0xffff)) << 16) | \
73 	(uint32_t)SWAP16((uint16_t)((value) >> 16)))
74 
75 #define	SWAP64(value)	\
76 	(((uint64_t)SWAP32((uint32_t)((value) & 0xffffffff)) \
77 	    << 32) | \
78 	(uint64_t)SWAP32((uint32_t)((value) >> 32)))
79 #else /* !_LITTLE_ENDIAN */
80 #define	SWAP16(value)	(value)
81 #define	SWAP32(value)	(value)
82 #define	SWAP64(value)	(value)
83 #endif
84 
85 /*
86  * Function Prototypes
87  */
88 int soft_gen_hashed_pin(CK_UTF8CHAR_PTR pPin, char **result, char **salt);
89 
90 CK_RV soft_verify_pin(CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen);
91 
92 CK_RV soft_gen_crypt_key(uchar_t *pPIN, soft_object_t **key,
93 	CK_BYTE **saltdata);
94 
95 CK_RV soft_gen_hmac_key(uchar_t *pPIN, soft_object_t **key, CK_BYTE **saltdata);
96 
97 CK_RV soft_keystore_pack_obj(struct object *obj, uchar_t **ks_buf, size_t *len);
98 
99 CK_RV soft_keystore_unpack_obj(struct object *obj, ks_obj_t *ks_obj);
100 
101 CK_RV soft_unpack_obj_attribute(uchar_t *buf, biginteger_t *key_dest,
102 	cert_attr_t **cert_dest, ulong_t *offset, boolean_t cert);
103 
104 ulong_t soft_pack_object_size(struct object *objp);
105 
106 CK_RV soft_pack_object(struct object *objp, uchar_t *buf);
107 
108 CK_RV soft_unpack_object(struct object *objp, uchar_t *buf);
109 
110 CK_RV soft_setpin(CK_UTF8CHAR_PTR pOldPin, CK_ULONG ulOldPinLen,
111 	CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewPinLen);
112 
113 CK_RV soft_put_object_to_keystore(struct object *objp);
114 
115 CK_RV soft_modify_object_to_keystore(struct object *objp);
116 
117 CK_RV soft_get_token_objects_from_keystore(ks_search_type_t type);
118 
119 CK_RV soft_init_token_session(void);
120 
121 void soft_destroy_token_session(void);
122 
123 CK_RV soft_keystore_crypt(soft_object_t *key_p, uchar_t *ivec,
124 	boolean_t encrypt, CK_BYTE_PTR in, CK_ULONG in_len, CK_BYTE_PTR out,
125 	CK_ULONG_PTR out_len);
126 
127 CK_RV soft_keystore_hmac(soft_object_t *key_p, boolean_t sign,
128 	CK_BYTE_PTR in, CK_ULONG in_len, CK_BYTE_PTR out, CK_ULONG_PTR out_len);
129 
130 
131 #ifdef	__cplusplus
132 }
133 #endif
134 
135 #endif /* _SOFTKEYSTORE_H */
136