xref: /titanic_44/usr/src/lib/libcryptoutil/common/cryptoutil.h (revision f6da83d4178694e7113b71d1e452f15b296f73d8)
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  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
22  */
23 
24 #ifndef _CRYPTOUTIL_H
25 #define	_CRYPTOUTIL_H
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #include <sys/types.h>
32 #include <syslog.h>
33 #include <security/cryptoki.h>
34 #include <sys/param.h>
35 
36 #define	LOG_STDERR	-1
37 #define	SUCCESS		0
38 #define	FAILURE		1
39 #define	MECH_ID_HEX_LEN	11	/* length of mechanism id in hex form */
40 
41 #define	_PATH_PKCS11_CONF	"/etc/crypto/pkcs11.conf"
42 #define	_PATH_KCF_CONF		"/etc/crypto/kcf.conf"
43 #define	_PATH_KCFD_LOCK		"/var/run/kcfd.lock"
44 
45 /* $ISA substitution for parsing pkcs11.conf data */
46 #define	PKCS11_ISA	"/$ISA/"
47 #if defined(_LP64)
48 #define	PKCS11_ISA_DIR	"/64/"
49 #else	/* !_LP64 */
50 #define	PKCS11_ISA_DIR	"/"
51 #endif
52 
53 /* keywords and delimiters for parsing configuration files */
54 #define	SEP_COLON	":"
55 #define	SEP_SEMICOLON	";"
56 #define	SEP_EQUAL	"="
57 #define	SEP_COMMA	","
58 #define	METASLOT_KEYWORD	"metaslot"
59 #define	FIPS_KEYWORD	"fips-140"
60 #define	EF_DISABLED	"disabledlist="
61 #define	EF_ENABLED	"enabledlist="
62 #define	EF_NORANDOM	"NO_RANDOM"
63 #define	METASLOT_TOKEN	"metaslot_token="
64 #define	METASLOT_SLOT	"metaslot_slot="
65 #define	METASLOT_STATUS	"metaslot_status="
66 #define	EF_FIPS_STATUS	"fips_status="
67 #define	METASLOT_AUTO_KEY_MIGRATE	"metaslot_auto_key_migrate="
68 #define	ENABLED_KEYWORD		"enabled"
69 #define	DISABLED_KEYWORD	"disabled"
70 #define	SLOT_DESCRIPTION_SIZE	64
71 #define	TOKEN_LABEL_SIZE	32
72 #define	TOKEN_MANUFACTURER_SIZE	32
73 #define	TOKEN_SERIAL_SIZE	16
74 #define	CRYPTO_FIPS_MODE_DISABLED	0
75 #define	CRYPTO_FIPS_MODE_ENABLED	1
76 
77 /*
78  * Define the following softtoken values that are used by softtoken
79  * library, cryptoadm and pktool command.
80  */
81 #define	SOFT_SLOT_DESCRIPTION	\
82 			"Sun Crypto Softtoken            " \
83 			"                                "
84 #define	SOFT_TOKEN_LABEL	"Sun Software PKCS#11 softtoken  "
85 #define	SOFT_TOKEN_SERIAL	"                "
86 #define	SOFT_MANUFACTURER_ID	"Sun Microsystems, Inc.          "
87 #define	SOFT_DEFAULT_PIN	"changeme"
88 
89 typedef char libname_t[MAXPATHLEN];
90 typedef char midstr_t[MECH_ID_HEX_LEN];
91 
92 typedef struct umechlist {
93 	midstr_t		name;	/* mechanism name in hex form */
94 	struct umechlist	*next;
95 } umechlist_t;
96 
97 typedef struct uentry {
98 	libname_t	name;
99 	boolean_t	flag_norandom; /* TRUE if random is disabled */
100 	boolean_t	flag_enabledlist; /* TRUE if an enabledlist */
101 	umechlist_t	*policylist; /* disabledlist or enabledlist */
102 	boolean_t	flag_metaslot_enabled; /* TRUE if metaslot's enabled */
103 	boolean_t	flag_metaslot_auto_key_migrate;
104 	CK_UTF8CHAR	metaslot_ks_slot[SLOT_DESCRIPTION_SIZE + 1];
105 	CK_UTF8CHAR	metaslot_ks_token[TOKEN_LABEL_SIZE + 1];
106 	int 		count;
107 	boolean_t	flag_fips_enabled;
108 } uentry_t;
109 
110 typedef struct uentrylist {
111 	uentry_t	*puent;
112 	struct uentrylist	*next;
113 } uentrylist_t;
114 
115 /* Return codes for pkcs11_parse_uri() */
116 #define	PK11_URI_OK		0
117 #define	PK11_URI_INVALID	1
118 #define	PK11_MALLOC_ERROR	2
119 #define	PK11_URI_VALUE_OVERFLOW	3
120 #define	PK11_NOT_PKCS11_URI	4
121 
122 /*
123  * There is no limit for the attribute length in the spec. 256 bytes should be
124  * enough for the object name.
125  */
126 #define	PK11_MAX_OBJECT_LEN		256
127 /*
128  * CKA_ID is of type "byte array" which can be of arbitrary length. 256 bytes
129  * should be sufficient though.
130  */
131 #define	PK11_MAX_ID_LEN			256
132 
133 /* Structure for the PKCS#11 URI. */
134 typedef struct pkcs11_uri_t {
135 	/* CKA_LABEL attribute to the C_FindObjectsInit function. */
136 	CK_UTF8CHAR_PTR	object;
137 	/*
138 	 * CKA_CLASS attribute to the C_FindObjectsInit function. The
139 	 * "objecttype" URI attribute can have a value one of "private",
140 	 * "public", "cert", "secretkey", and "data". The "objecttype" field can
141 	 * have a value of CKO_PUBLIC_KEY, CKO_PRIVATE_KEY, CKO_CERTIFICATE,
142 	 * CKO_SECRET_KEY, and CKO_DATA. This attribute cannot be empty in the
143 	 * URI.
144 	 */
145 	CK_ULONG	objecttype;
146 	/* CKO_DATA is 0 so we need this flag. Not part of the URI itself. */
147 	boolean_t	objecttype_present;
148 	/*
149 	 * Token, manufufacturer, serial and model are of fixed size length in
150 	 * the specification. We allocate memory on the fly to distinguish
151 	 * between an attribute not present and an empty value. We check for
152 	 * overflows. We always terminate the string with '\0' even when that is
153 	 * not used in the PKCS#11's CK_TOKEN_INFO structure (fields are padded
154 	 * with spaces).
155 	 */
156 	/* Token label from CK_TOKEN_INFO. */
157 	CK_UTF8CHAR_PTR	token;
158 	/* ManufacturerID from CK_TOKEN_INFO. */
159 	CK_UTF8CHAR_PTR	manuf;
160 	/* SerialNumber from CK_TOKEN_INFO. */
161 	CK_CHAR_PTR	serial;
162 	/* Model from CK_TOKEN_INFO. */
163 	CK_UTF8CHAR_PTR	model;
164 	/* This is a byte array, we need a length parameter as well. */
165 	CK_BYTE_PTR	id;
166 	int		id_len;
167 	/*
168 	 * Location of the file with a token PIN. Application can overload this,
169 	 * eg. "/bin/askpass|" may mean to read the PIN from a command. However,
170 	 * the pkcs11_parse_uri() function does not interpret this field in any
171 	 * way.
172 	 */
173 	char		*pinfile;
174 } pkcs11_uri_t;
175 
176 extern void cryptodebug(const char *fmt, ...);
177 extern void cryptoerror(int priority, const char *fmt, ...);
178 extern void cryptodebug_init(const char *prefix);
179 extern void cryptoerror_off();
180 extern void cryptoerror_on();
181 
182 extern const char *pkcs11_mech2str(CK_MECHANISM_TYPE mech);
183 extern CK_RV pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech);
184 
185 extern int get_pkcs11conf_info(uentrylist_t **);
186 extern umechlist_t *create_umech(char *);
187 extern void free_umechlist(umechlist_t *);
188 extern void free_uentrylist(uentrylist_t *);
189 extern void free_uentry(uentry_t *);
190 extern uentry_t *getent_uef(char *);
191 
192 extern void tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen);
193 extern int hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes,
194     size_t *blen);
195 extern CK_RV pkcs11_mech2keytype(CK_MECHANISM_TYPE mech_type,
196     CK_KEY_TYPE *ktype);
197 extern CK_RV pkcs11_mech2keygen(CK_MECHANISM_TYPE mech_type,
198     CK_MECHANISM_TYPE *gen_mech);
199 extern char *pkcs11_strerror(CK_RV rv);
200 
201 extern int
202 get_metaslot_info(boolean_t  *status_enabled, boolean_t *migrate_enabled,
203     char **objectstore_slot_info, char **objectstore_token_info);
204 
205 extern char *get_fullpath(char *dir, char *filepath);
206 extern int str2lifetime(char *ltimestr, uint32_t *ltime);
207 
208 extern char *pkcs11_default_token(void);
209 extern int pkcs11_get_pass(char *token_name, char **pdata, size_t *psize,
210     size_t min_psize, boolean_t with_confirmation);
211 
212 extern int pkcs11_seed_urandom(void *sbuf, size_t slen);
213 extern int pkcs11_get_random(void *dbuf, size_t dlen);
214 extern int pkcs11_get_urandom(void *dbuf, size_t dlen);
215 extern int pkcs11_get_nzero_urandom(void *dbuf, size_t dlen);
216 extern void pkcs11_close_random(void);
217 extern void pkcs11_close_urandom(void);
218 extern void pkcs11_close_urandom_seed(void);
219 extern int pkcs11_read_data(char *filename, void **dbuf, size_t *dlen);
220 
221 extern int open_nointr(const char *path, int oflag, ...);
222 extern ssize_t readn_nointr(int fd, void *dbuf, size_t dlen);
223 extern ssize_t writen_nointr(int fd, void *dbuf, size_t dlen);
224 extern int update_conf(char *conf_file, char *entry);
225 
226 extern CK_RV get_fips_mode(int *);
227 
228 extern int pkcs11_parse_uri(const char *str, pkcs11_uri_t *uri);
229 extern void pkcs11_free_uri(pkcs11_uri_t *uri);
230 
231 #ifdef __cplusplus
232 }
233 #endif
234 
235 #endif /* _CRYPTOUTIL_H */
236