xref: /titanic_51/usr/src/lib/libcryptoutil/common/cryptoutil.h (revision 05d57413471eaaa425913a06edc2ab33ad9b05bc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
599ebb4caSwyllys  * Common Development and Distribution License (the "License").
699ebb4caSwyllys  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
20ccd81fddSJan Pechanec  *
21ccd81fddSJan Pechanec  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
227c478bd9Sstevel@tonic-gate  */
236ea3c060SGarrett D'Amore /*
246ea3c060SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
25*05d57413SDan McDonald  * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
266ea3c060SGarrett D'Amore  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef _CRYPTOUTIL_H
297c478bd9Sstevel@tonic-gate #define	_CRYPTOUTIL_H
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifdef __cplusplus
327c478bd9Sstevel@tonic-gate extern "C" {
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <syslog.h>
377c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	LOG_STDERR	-1
417c478bd9Sstevel@tonic-gate #define	SUCCESS		0
427c478bd9Sstevel@tonic-gate #define	FAILURE		1
437c478bd9Sstevel@tonic-gate #define	MECH_ID_HEX_LEN	11	/* length of mechanism id in hex form */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	_PATH_PKCS11_CONF	"/etc/crypto/pkcs11.conf"
46b5a2d845SHai-May Chao #define	_PATH_KCF_CONF		"/etc/crypto/kcf.conf"
477c478bd9Sstevel@tonic-gate #define	_PATH_KCFD_LOCK		"/var/run/kcfd.lock"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* $ISA substitution for parsing pkcs11.conf data */
507c478bd9Sstevel@tonic-gate #define	PKCS11_ISA	"/$ISA/"
517c478bd9Sstevel@tonic-gate #if defined(_LP64)
527c478bd9Sstevel@tonic-gate #define	PKCS11_ISA_DIR	"/64/"
537c478bd9Sstevel@tonic-gate #else	/* !_LP64 */
547c478bd9Sstevel@tonic-gate #define	PKCS11_ISA_DIR	"/"
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* keywords and delimiters for parsing configuration files */
587c478bd9Sstevel@tonic-gate #define	SEP_COLON	":"
597c478bd9Sstevel@tonic-gate #define	SEP_SEMICOLON	";"
607c478bd9Sstevel@tonic-gate #define	SEP_EQUAL	"="
617c478bd9Sstevel@tonic-gate #define	SEP_COMMA	","
627c478bd9Sstevel@tonic-gate #define	METASLOT_KEYWORD	"metaslot"
63b5a2d845SHai-May Chao #define	FIPS_KEYWORD	"fips-140"
647c478bd9Sstevel@tonic-gate #define	EF_DISABLED	"disabledlist="
657c478bd9Sstevel@tonic-gate #define	EF_ENABLED	"enabledlist="
667c478bd9Sstevel@tonic-gate #define	EF_NORANDOM	"NO_RANDOM"
677c478bd9Sstevel@tonic-gate #define	METASLOT_TOKEN	"metaslot_token="
687c478bd9Sstevel@tonic-gate #define	METASLOT_SLOT	"metaslot_slot="
697c478bd9Sstevel@tonic-gate #define	METASLOT_STATUS	"metaslot_status="
70b5a2d845SHai-May Chao #define	EF_FIPS_STATUS	"fips_status="
717c478bd9Sstevel@tonic-gate #define	METASLOT_AUTO_KEY_MIGRATE	"metaslot_auto_key_migrate="
72b5a2d845SHai-May Chao #define	ENABLED_KEYWORD		"enabled"
73b5a2d845SHai-May Chao #define	DISABLED_KEYWORD	"disabled"
747c478bd9Sstevel@tonic-gate #define	SLOT_DESCRIPTION_SIZE	64
757c478bd9Sstevel@tonic-gate #define	TOKEN_LABEL_SIZE	32
761c9bd843Sdinak #define	TOKEN_MANUFACTURER_SIZE	32
771c9bd843Sdinak #define	TOKEN_SERIAL_SIZE	16
78b5a2d845SHai-May Chao #define	CRYPTO_FIPS_MODE_DISABLED	0
79b5a2d845SHai-May Chao #define	CRYPTO_FIPS_MODE_ENABLED	1
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Define the following softtoken values that are used by softtoken
837c478bd9Sstevel@tonic-gate  * library, cryptoadm and pktool command.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate #define	SOFT_SLOT_DESCRIPTION	\
867c478bd9Sstevel@tonic-gate 			"Sun Crypto Softtoken            " \
877c478bd9Sstevel@tonic-gate 			"                                "
887c478bd9Sstevel@tonic-gate #define	SOFT_TOKEN_LABEL	"Sun Software PKCS#11 softtoken  "
897c478bd9Sstevel@tonic-gate #define	SOFT_TOKEN_SERIAL	"                "
907c478bd9Sstevel@tonic-gate #define	SOFT_MANUFACTURER_ID	"Sun Microsystems, Inc.          "
917c478bd9Sstevel@tonic-gate #define	SOFT_DEFAULT_PIN	"changeme"
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate typedef char libname_t[MAXPATHLEN];
947c478bd9Sstevel@tonic-gate typedef char midstr_t[MECH_ID_HEX_LEN];
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate typedef struct umechlist {
977c478bd9Sstevel@tonic-gate 	midstr_t		name;	/* mechanism name in hex form */
987c478bd9Sstevel@tonic-gate 	struct umechlist	*next;
997c478bd9Sstevel@tonic-gate } umechlist_t;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate typedef struct uentry {
1027c478bd9Sstevel@tonic-gate 	libname_t	name;
1037c478bd9Sstevel@tonic-gate 	boolean_t	flag_norandom; /* TRUE if random is disabled */
1047c478bd9Sstevel@tonic-gate 	boolean_t	flag_enabledlist; /* TRUE if an enabledlist */
1057c478bd9Sstevel@tonic-gate 	umechlist_t	*policylist; /* disabledlist or enabledlist */
1067c478bd9Sstevel@tonic-gate 	boolean_t	flag_metaslot_enabled; /* TRUE if metaslot's enabled */
1077c478bd9Sstevel@tonic-gate 	boolean_t	flag_metaslot_auto_key_migrate;
1087c478bd9Sstevel@tonic-gate 	CK_UTF8CHAR	metaslot_ks_slot[SLOT_DESCRIPTION_SIZE + 1];
1097c478bd9Sstevel@tonic-gate 	CK_UTF8CHAR	metaslot_ks_token[TOKEN_LABEL_SIZE + 1];
1107c478bd9Sstevel@tonic-gate 	int 		count;
111d616ad8eSHai-May Chao 	boolean_t	flag_fips_enabled;
1127c478bd9Sstevel@tonic-gate } uentry_t;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate typedef struct uentrylist {
1157c478bd9Sstevel@tonic-gate 	uentry_t	*puent;
1167c478bd9Sstevel@tonic-gate 	struct uentrylist	*next;
1177c478bd9Sstevel@tonic-gate } uentrylist_t;
1187c478bd9Sstevel@tonic-gate 
119ccd81fddSJan Pechanec /* Return codes for pkcs11_parse_uri() */
120ccd81fddSJan Pechanec #define	PK11_URI_OK		0
121ccd81fddSJan Pechanec #define	PK11_URI_INVALID	1
122ccd81fddSJan Pechanec #define	PK11_MALLOC_ERROR	2
123ccd81fddSJan Pechanec #define	PK11_URI_VALUE_OVERFLOW	3
124ccd81fddSJan Pechanec #define	PK11_NOT_PKCS11_URI	4
125ccd81fddSJan Pechanec 
126ccd81fddSJan Pechanec /*
127ccd81fddSJan Pechanec  * There is no limit for the attribute length in the spec. 256 bytes should be
128ccd81fddSJan Pechanec  * enough for the object name.
129ccd81fddSJan Pechanec  */
130ccd81fddSJan Pechanec #define	PK11_MAX_OBJECT_LEN		256
131ccd81fddSJan Pechanec /*
132ccd81fddSJan Pechanec  * CKA_ID is of type "byte array" which can be of arbitrary length. 256 bytes
133ccd81fddSJan Pechanec  * should be sufficient though.
134ccd81fddSJan Pechanec  */
135ccd81fddSJan Pechanec #define	PK11_MAX_ID_LEN			256
136ccd81fddSJan Pechanec 
137ccd81fddSJan Pechanec /* Structure for the PKCS#11 URI. */
138ccd81fddSJan Pechanec typedef struct pkcs11_uri_t {
139ccd81fddSJan Pechanec 	/* CKA_LABEL attribute to the C_FindObjectsInit function. */
140ccd81fddSJan Pechanec 	CK_UTF8CHAR_PTR	object;
141ccd81fddSJan Pechanec 	/*
142ccd81fddSJan Pechanec 	 * CKA_CLASS attribute to the C_FindObjectsInit function. The
143ccd81fddSJan Pechanec 	 * "objecttype" URI attribute can have a value one of "private",
144ccd81fddSJan Pechanec 	 * "public", "cert", "secretkey", and "data". The "objecttype" field can
145ccd81fddSJan Pechanec 	 * have a value of CKO_PUBLIC_KEY, CKO_PRIVATE_KEY, CKO_CERTIFICATE,
146ccd81fddSJan Pechanec 	 * CKO_SECRET_KEY, and CKO_DATA. This attribute cannot be empty in the
147ccd81fddSJan Pechanec 	 * URI.
148ccd81fddSJan Pechanec 	 */
149ccd81fddSJan Pechanec 	CK_ULONG	objecttype;
150ccd81fddSJan Pechanec 	/* CKO_DATA is 0 so we need this flag. Not part of the URI itself. */
151ccd81fddSJan Pechanec 	boolean_t	objecttype_present;
152ccd81fddSJan Pechanec 	/*
153ccd81fddSJan Pechanec 	 * Token, manufufacturer, serial and model are of fixed size length in
154ccd81fddSJan Pechanec 	 * the specification. We allocate memory on the fly to distinguish
155ccd81fddSJan Pechanec 	 * between an attribute not present and an empty value. We check for
156ccd81fddSJan Pechanec 	 * overflows. We always terminate the string with '\0' even when that is
157ccd81fddSJan Pechanec 	 * not used in the PKCS#11's CK_TOKEN_INFO structure (fields are padded
158ccd81fddSJan Pechanec 	 * with spaces).
159ccd81fddSJan Pechanec 	 */
160ccd81fddSJan Pechanec 	/* Token label from CK_TOKEN_INFO. */
161ccd81fddSJan Pechanec 	CK_UTF8CHAR_PTR	token;
162ccd81fddSJan Pechanec 	/* ManufacturerID from CK_TOKEN_INFO. */
163ccd81fddSJan Pechanec 	CK_UTF8CHAR_PTR	manuf;
164ccd81fddSJan Pechanec 	/* SerialNumber from CK_TOKEN_INFO. */
165ccd81fddSJan Pechanec 	CK_CHAR_PTR	serial;
166ccd81fddSJan Pechanec 	/* Model from CK_TOKEN_INFO. */
167ccd81fddSJan Pechanec 	CK_UTF8CHAR_PTR	model;
168ccd81fddSJan Pechanec 	/* This is a byte array, we need a length parameter as well. */
169ccd81fddSJan Pechanec 	CK_BYTE_PTR	id;
170ccd81fddSJan Pechanec 	int		id_len;
171ccd81fddSJan Pechanec 	/*
172ccd81fddSJan Pechanec 	 * Location of the file with a token PIN. Application can overload this,
173ccd81fddSJan Pechanec 	 * eg. "/bin/askpass|" may mean to read the PIN from a command. However,
174ccd81fddSJan Pechanec 	 * the pkcs11_parse_uri() function does not interpret this field in any
175ccd81fddSJan Pechanec 	 * way.
176ccd81fddSJan Pechanec 	 */
177ccd81fddSJan Pechanec 	char		*pinfile;
178ccd81fddSJan Pechanec } pkcs11_uri_t;
179ccd81fddSJan Pechanec 
1807c478bd9Sstevel@tonic-gate extern void cryptodebug(const char *fmt, ...);
1817c478bd9Sstevel@tonic-gate extern void cryptoerror(int priority, const char *fmt, ...);
1827c478bd9Sstevel@tonic-gate extern void cryptodebug_init(const char *prefix);
183a7e661a2SAnthony Scarpino extern void cryptoerror_off();
184a7e661a2SAnthony Scarpino extern void cryptoerror_on();
1857c478bd9Sstevel@tonic-gate 
1862321aa36Sda73024 extern const char *pkcs11_mech2str(CK_MECHANISM_TYPE mech);
1877c478bd9Sstevel@tonic-gate extern CK_RV pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate extern int get_pkcs11conf_info(uentrylist_t **);
1907c478bd9Sstevel@tonic-gate extern umechlist_t *create_umech(char *);
1917c478bd9Sstevel@tonic-gate extern void free_umechlist(umechlist_t *);
1927c478bd9Sstevel@tonic-gate extern void free_uentrylist(uentrylist_t *);
1937c478bd9Sstevel@tonic-gate extern void free_uentry(uentry_t *);
19499ebb4caSwyllys extern uentry_t *getent_uef(char *);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate extern void tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen);
197a7e661a2SAnthony Scarpino extern int hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes,
198a7e661a2SAnthony Scarpino     size_t *blen);
1997c478bd9Sstevel@tonic-gate extern CK_RV pkcs11_mech2keytype(CK_MECHANISM_TYPE mech_type,
2007c478bd9Sstevel@tonic-gate     CK_KEY_TYPE *ktype);
20126ff1ce9Sdinak extern CK_RV pkcs11_mech2keygen(CK_MECHANISM_TYPE mech_type,
20226ff1ce9Sdinak     CK_MECHANISM_TYPE *gen_mech);
2037c478bd9Sstevel@tonic-gate extern char *pkcs11_strerror(CK_RV rv);
2047c478bd9Sstevel@tonic-gate 
20599ebb4caSwyllys extern int
20699ebb4caSwyllys get_metaslot_info(boolean_t  *status_enabled, boolean_t *migrate_enabled,
20799ebb4caSwyllys     char **objectstore_slot_info, char **objectstore_token_info);
20899ebb4caSwyllys 
20999ebb4caSwyllys extern char *get_fullpath(char *dir, char *filepath);
21099ebb4caSwyllys extern int str2lifetime(char *ltimestr, uint32_t *ltime);
21199ebb4caSwyllys 
2121c9bd843Sdinak extern char *pkcs11_default_token(void);
2131c9bd843Sdinak extern int pkcs11_get_pass(char *token_name, char **pdata, size_t *psize,
2141c9bd843Sdinak     size_t min_psize, boolean_t with_confirmation);
2151c9bd843Sdinak 
2167b79d846SDina K Nimeh extern int pkcs11_seed_urandom(void *sbuf, size_t slen);
2177b79d846SDina K Nimeh extern int pkcs11_get_random(void *dbuf, size_t dlen);
2187b79d846SDina K Nimeh extern int pkcs11_get_urandom(void *dbuf, size_t dlen);
2197b79d846SDina K Nimeh extern int pkcs11_get_nzero_urandom(void *dbuf, size_t dlen);
2201c9bd843Sdinak extern int pkcs11_read_data(char *filename, void **dbuf, size_t *dlen);
2211c9bd843Sdinak 
22219193bb6SDina K Nimeh extern int open_nointr(const char *path, int oflag, ...);
22319193bb6SDina K Nimeh extern ssize_t readn_nointr(int fd, void *dbuf, size_t dlen);
22419193bb6SDina K Nimeh extern ssize_t writen_nointr(int fd, void *dbuf, size_t dlen);
225b5a2d845SHai-May Chao extern int update_conf(char *conf_file, char *entry);
226b5a2d845SHai-May Chao 
227ccd81fddSJan Pechanec extern int pkcs11_parse_uri(const char *str, pkcs11_uri_t *uri);
228ccd81fddSJan Pechanec extern void pkcs11_free_uri(pkcs11_uri_t *uri);
229ccd81fddSJan Pechanec 
2307c478bd9Sstevel@tonic-gate #ifdef __cplusplus
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate #endif
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #endif /* _CRYPTOUTIL_H */
235