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