xref: /titanic_52/usr/src/lib/libcryptoutil/common/cryptoutil.h (revision 4246c8e92ef9ad6ada2b992b7af02832ff071bf7)
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 _CRYPTOUTIL_H
27 #define	_CRYPTOUTIL_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/types.h>
34 #include <syslog.h>
35 #include <security/cryptoki.h>
36 #include <sys/param.h>
37 
38 #define	LOG_STDERR	-1
39 #define	SUCCESS		0
40 #define	FAILURE		1
41 #define	MECH_ID_HEX_LEN	11	/* length of mechanism id in hex form */
42 
43 #define	_PATH_PKCS11_CONF	"/etc/crypto/pkcs11.conf"
44 #define	_PATH_KCFD_LOCK		"/var/run/kcfd.lock"
45 
46 /* $ISA substitution for parsing pkcs11.conf data */
47 #define	PKCS11_ISA	"/$ISA/"
48 #if defined(_LP64)
49 #define	PKCS11_ISA_DIR	"/64/"
50 #else	/* !_LP64 */
51 #define	PKCS11_ISA_DIR	"/"
52 #endif
53 
54 /* keywords and delimiters for parsing configuration files */
55 #define	SEP_COLON	":"
56 #define	SEP_SEMICOLON	";"
57 #define	SEP_EQUAL	"="
58 #define	SEP_COMMA	","
59 #define	METASLOT_KEYWORD	"metaslot"
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	METASLOT_AUTO_KEY_MIGRATE	"metaslot_auto_key_migrate="
67 #define	METASLOT_ENABLED	"enabled"
68 #define	METASLOT_DISABLED	"disabled"
69 #define	SLOT_DESCRIPTION_SIZE	64
70 #define	TOKEN_LABEL_SIZE	32
71 #define	TOKEN_MANUFACTURER_SIZE	32
72 #define	TOKEN_SERIAL_SIZE	16
73 
74 /*
75  * Define the following softtoken values that are used by softtoken
76  * library, cryptoadm and pktool command.
77  */
78 #define	SOFT_SLOT_DESCRIPTION	\
79 			"Sun Crypto Softtoken            " \
80 			"                                "
81 #define	SOFT_TOKEN_LABEL	"Sun Software PKCS#11 softtoken  "
82 #define	SOFT_TOKEN_SERIAL	"                "
83 #define	SOFT_MANUFACTURER_ID	"Sun Microsystems, Inc.          "
84 #define	SOFT_DEFAULT_PIN	"changeme"
85 
86 typedef char libname_t[MAXPATHLEN];
87 typedef char midstr_t[MECH_ID_HEX_LEN];
88 
89 typedef struct umechlist {
90 	midstr_t		name;	/* mechanism name in hex form */
91 	struct umechlist	*next;
92 } umechlist_t;
93 
94 typedef struct uentry {
95 	libname_t	name;
96 	boolean_t	flag_norandom; /* TRUE if random is disabled */
97 	boolean_t	flag_enabledlist; /* TRUE if an enabledlist */
98 	umechlist_t	*policylist; /* disabledlist or enabledlist */
99 	boolean_t	flag_metaslot_enabled; /* TRUE if metaslot's enabled */
100 	boolean_t	flag_metaslot_auto_key_migrate;
101 	CK_UTF8CHAR	metaslot_ks_slot[SLOT_DESCRIPTION_SIZE + 1];
102 	CK_UTF8CHAR	metaslot_ks_token[TOKEN_LABEL_SIZE + 1];
103 	int 		count;
104 } uentry_t;
105 
106 typedef struct uentrylist {
107 	uentry_t	*puent;
108 	struct uentrylist	*next;
109 } uentrylist_t;
110 
111 extern void cryptodebug(const char *fmt, ...);
112 extern void cryptoerror(int priority, const char *fmt, ...);
113 extern void cryptodebug_init(const char *prefix);
114 extern void cryptoerror_off();
115 extern void cryptoerror_on();
116 
117 extern const char *pkcs11_mech2str(CK_MECHANISM_TYPE mech);
118 extern CK_RV pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech);
119 
120 extern int get_pkcs11conf_info(uentrylist_t **);
121 extern umechlist_t *create_umech(char *);
122 extern void free_umechlist(umechlist_t *);
123 extern void free_uentrylist(uentrylist_t *);
124 extern void free_uentry(uentry_t *);
125 extern uentry_t *getent_uef(char *);
126 
127 extern void tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen);
128 extern int hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes,
129     size_t *blen);
130 extern CK_RV pkcs11_mech2keytype(CK_MECHANISM_TYPE mech_type,
131     CK_KEY_TYPE *ktype);
132 extern CK_RV pkcs11_mech2keygen(CK_MECHANISM_TYPE mech_type,
133     CK_MECHANISM_TYPE *gen_mech);
134 extern char *pkcs11_strerror(CK_RV rv);
135 
136 extern int
137 get_metaslot_info(boolean_t  *status_enabled, boolean_t *migrate_enabled,
138     char **objectstore_slot_info, char **objectstore_token_info);
139 
140 extern char *get_fullpath(char *dir, char *filepath);
141 extern int str2lifetime(char *ltimestr, uint32_t *ltime);
142 
143 extern char *pkcs11_default_token(void);
144 extern int pkcs11_get_pass(char *token_name, char **pdata, size_t *psize,
145     size_t min_psize, boolean_t with_confirmation);
146 
147 extern int pkcs11_seed_urandom(void *sbuf, size_t slen);
148 extern int pkcs11_get_random(void *dbuf, size_t dlen);
149 extern int pkcs11_get_urandom(void *dbuf, size_t dlen);
150 extern int pkcs11_get_nzero_urandom(void *dbuf, size_t dlen);
151 extern void pkcs11_close_random(void);
152 extern void pkcs11_close_urandom(void);
153 extern void pkcs11_close_urandom_seed(void);
154 extern int pkcs11_read_data(char *filename, void **dbuf, size_t *dlen);
155 
156 extern int open_nointr(const char *path, int oflag, ...);
157 extern ssize_t readn_nointr(int fd, void *dbuf, size_t dlen);
158 extern ssize_t writen_nointr(int fd, void *dbuf, size_t dlen);
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif /* _CRYPTOUTIL_H */
165