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