xref: /titanic_51/usr/src/lib/libcryptoutil/common/cryptoutil.h (revision a7e661a2db6ff10cd43a089e7f4dcbd0cfd609f1)
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
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2226ff1ce9Sdinak  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef _CRYPTOUTIL_H
277c478bd9Sstevel@tonic-gate #define	_CRYPTOUTIL_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #ifdef __cplusplus
307c478bd9Sstevel@tonic-gate extern "C" {
317c478bd9Sstevel@tonic-gate #endif
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	LOG_STDERR	-1
397c478bd9Sstevel@tonic-gate #define	SUCCESS		0
407c478bd9Sstevel@tonic-gate #define	FAILURE		1
417c478bd9Sstevel@tonic-gate #define	MECH_ID_HEX_LEN	11	/* length of mechanism id in hex form */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	_PATH_PKCS11_CONF	"/etc/crypto/pkcs11.conf"
447c478bd9Sstevel@tonic-gate #define	_PATH_KCFD_LOCK		"/var/run/kcfd.lock"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* $ISA substitution for parsing pkcs11.conf data */
477c478bd9Sstevel@tonic-gate #define	PKCS11_ISA	"/$ISA/"
487c478bd9Sstevel@tonic-gate #if defined(_LP64)
497c478bd9Sstevel@tonic-gate #define	PKCS11_ISA_DIR	"/64/"
507c478bd9Sstevel@tonic-gate #else	/* !_LP64 */
517c478bd9Sstevel@tonic-gate #define	PKCS11_ISA_DIR	"/"
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* keywords and delimiters for parsing configuration files */
557c478bd9Sstevel@tonic-gate #define	SEP_COLON	":"
567c478bd9Sstevel@tonic-gate #define	SEP_SEMICOLON	";"
577c478bd9Sstevel@tonic-gate #define	SEP_EQUAL	"="
587c478bd9Sstevel@tonic-gate #define	SEP_COMMA	","
597c478bd9Sstevel@tonic-gate #define	METASLOT_KEYWORD	"metaslot"
607c478bd9Sstevel@tonic-gate #define	EF_DISABLED	"disabledlist="
617c478bd9Sstevel@tonic-gate #define	EF_ENABLED	"enabledlist="
627c478bd9Sstevel@tonic-gate #define	EF_NORANDOM	"NO_RANDOM"
637c478bd9Sstevel@tonic-gate #define	METASLOT_TOKEN	"metaslot_token="
647c478bd9Sstevel@tonic-gate #define	METASLOT_SLOT	"metaslot_slot="
657c478bd9Sstevel@tonic-gate #define	METASLOT_STATUS	"metaslot_status="
667c478bd9Sstevel@tonic-gate #define	METASLOT_AUTO_KEY_MIGRATE	"metaslot_auto_key_migrate="
677c478bd9Sstevel@tonic-gate #define	METASLOT_ENABLED	"enabled"
687c478bd9Sstevel@tonic-gate #define	METASLOT_DISABLED	"disabled"
697c478bd9Sstevel@tonic-gate #define	SLOT_DESCRIPTION_SIZE	64
707c478bd9Sstevel@tonic-gate #define	TOKEN_LABEL_SIZE	32
711c9bd843Sdinak #define	TOKEN_MANUFACTURER_SIZE	32
721c9bd843Sdinak #define	TOKEN_SERIAL_SIZE	16
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Define the following softtoken values that are used by softtoken
767c478bd9Sstevel@tonic-gate  * library, cryptoadm and pktool command.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate #define	SOFT_SLOT_DESCRIPTION	\
797c478bd9Sstevel@tonic-gate 			"Sun Crypto Softtoken            " \
807c478bd9Sstevel@tonic-gate 			"                                "
817c478bd9Sstevel@tonic-gate #define	SOFT_TOKEN_LABEL	"Sun Software PKCS#11 softtoken  "
827c478bd9Sstevel@tonic-gate #define	SOFT_TOKEN_SERIAL	"                "
837c478bd9Sstevel@tonic-gate #define	SOFT_MANUFACTURER_ID	"Sun Microsystems, Inc.          "
847c478bd9Sstevel@tonic-gate #define	SOFT_DEFAULT_PIN	"changeme"
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate typedef char libname_t[MAXPATHLEN];
877c478bd9Sstevel@tonic-gate typedef char midstr_t[MECH_ID_HEX_LEN];
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate typedef struct umechlist {
907c478bd9Sstevel@tonic-gate 	midstr_t		name;	/* mechanism name in hex form */
917c478bd9Sstevel@tonic-gate 	struct umechlist	*next;
927c478bd9Sstevel@tonic-gate } umechlist_t;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate typedef struct uentry {
957c478bd9Sstevel@tonic-gate 	libname_t	name;
967c478bd9Sstevel@tonic-gate 	boolean_t	flag_norandom; /* TRUE if random is disabled */
977c478bd9Sstevel@tonic-gate 	boolean_t	flag_enabledlist; /* TRUE if an enabledlist */
987c478bd9Sstevel@tonic-gate 	umechlist_t	*policylist; /* disabledlist or enabledlist */
997c478bd9Sstevel@tonic-gate 	boolean_t	flag_metaslot_enabled; /* TRUE if metaslot's enabled */
1007c478bd9Sstevel@tonic-gate 	boolean_t	flag_metaslot_auto_key_migrate;
1017c478bd9Sstevel@tonic-gate 	CK_UTF8CHAR	metaslot_ks_slot[SLOT_DESCRIPTION_SIZE + 1];
1027c478bd9Sstevel@tonic-gate 	CK_UTF8CHAR	metaslot_ks_token[TOKEN_LABEL_SIZE + 1];
1037c478bd9Sstevel@tonic-gate 	int 		count;
1047c478bd9Sstevel@tonic-gate } uentry_t;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate typedef struct uentrylist {
1077c478bd9Sstevel@tonic-gate 	uentry_t	*puent;
1087c478bd9Sstevel@tonic-gate 	struct uentrylist	*next;
1097c478bd9Sstevel@tonic-gate } uentrylist_t;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate extern void cryptodebug(const char *fmt, ...);
1127c478bd9Sstevel@tonic-gate extern void cryptoerror(int priority, const char *fmt, ...);
1137c478bd9Sstevel@tonic-gate extern void cryptodebug_init(const char *prefix);
114*a7e661a2SAnthony Scarpino extern void cryptoerror_off();
115*a7e661a2SAnthony Scarpino extern void cryptoerror_on();
1167c478bd9Sstevel@tonic-gate 
1172321aa36Sda73024 extern const char *pkcs11_mech2str(CK_MECHANISM_TYPE mech);
1187c478bd9Sstevel@tonic-gate extern CK_RV pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate extern int get_pkcs11conf_info(uentrylist_t **);
1217c478bd9Sstevel@tonic-gate extern umechlist_t *create_umech(char *);
1227c478bd9Sstevel@tonic-gate extern void free_umechlist(umechlist_t *);
1237c478bd9Sstevel@tonic-gate extern void free_uentrylist(uentrylist_t *);
1247c478bd9Sstevel@tonic-gate extern void free_uentry(uentry_t *);
12599ebb4caSwyllys extern uentry_t *getent_uef(char *);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate extern void tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen);
128*a7e661a2SAnthony Scarpino extern int hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes,
129*a7e661a2SAnthony Scarpino     size_t *blen);
1307c478bd9Sstevel@tonic-gate extern CK_RV pkcs11_mech2keytype(CK_MECHANISM_TYPE mech_type,
1317c478bd9Sstevel@tonic-gate     CK_KEY_TYPE *ktype);
13226ff1ce9Sdinak extern CK_RV pkcs11_mech2keygen(CK_MECHANISM_TYPE mech_type,
13326ff1ce9Sdinak     CK_MECHANISM_TYPE *gen_mech);
1347c478bd9Sstevel@tonic-gate extern char *pkcs11_strerror(CK_RV rv);
1357c478bd9Sstevel@tonic-gate 
13699ebb4caSwyllys extern int
13799ebb4caSwyllys get_metaslot_info(boolean_t  *status_enabled, boolean_t *migrate_enabled,
13899ebb4caSwyllys     char **objectstore_slot_info, char **objectstore_token_info);
13999ebb4caSwyllys 
14099ebb4caSwyllys extern char *get_fullpath(char *dir, char *filepath);
14199ebb4caSwyllys extern int str2lifetime(char *ltimestr, uint32_t *ltime);
14299ebb4caSwyllys 
1431c9bd843Sdinak extern char *pkcs11_default_token(void);
1441c9bd843Sdinak extern int pkcs11_get_pass(char *token_name, char **pdata, size_t *psize,
1451c9bd843Sdinak     size_t min_psize, boolean_t with_confirmation);
1461c9bd843Sdinak 
1471c9bd843Sdinak extern int pkcs11_random_data(void *dbuf, size_t dlen);
14826ff1ce9Sdinak extern int pkcs11_nzero_random_data(void *dbuf, size_t dlen);
1491c9bd843Sdinak extern int pkcs11_read_data(char *filename, void **dbuf, size_t *dlen);
1501c9bd843Sdinak 
1517c478bd9Sstevel@tonic-gate #ifdef __cplusplus
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #endif /* _CRYPTOUTIL_H */
156