xref: /onnv-gate/usr/src/lib/libkmf/include/kmfapiP.h (revision 6669:bc95c5ad177e)
13089Swyllys /*
23089Swyllys  * CDDL HEADER START
33089Swyllys  *
43089Swyllys  * The contents of this file are subject to the terms of the
53089Swyllys  * Common Development and Distribution License (the "License").
63089Swyllys  * You may not use this file except in compliance with the License.
73089Swyllys  *
83089Swyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93089Swyllys  * or http://www.opensolaris.org/os/licensing.
103089Swyllys  * See the License for the specific language governing permissions
113089Swyllys  * and limitations under the License.
123089Swyllys  *
133089Swyllys  * When distributing Covered Code, include this CDDL HEADER in each
143089Swyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153089Swyllys  * If applicable, add the following below this CDDL HEADER, with the
163089Swyllys  * fields enclosed by brackets "[]" replaced with your own identifying
173089Swyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
183089Swyllys  *
193089Swyllys  * CDDL HEADER END
203089Swyllys  */
213089Swyllys /*
226051Swyllys  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
233089Swyllys  * Use is subject to license terms.
243089Swyllys  */
253089Swyllys #ifndef _KMFAPIP_H
263089Swyllys #define	_KMFAPIP_H
273089Swyllys 
283089Swyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
293089Swyllys 
303089Swyllys #include <kmfapi.h>
313089Swyllys #include <kmfpolicy.h>
323089Swyllys 
333089Swyllys #ifdef __cplusplus
343089Swyllys extern "C" {
353089Swyllys #endif
363089Swyllys 
373089Swyllys /* Plugin function table */
383089Swyllys typedef struct {
393089Swyllys 	ushort_t	version;
403089Swyllys 	KMF_RETURN	(*ConfigureKeystore) (
413089Swyllys 			KMF_HANDLE_T,
425051Swyllys 			int,
435051Swyllys 			KMF_ATTRIBUTE *);
443089Swyllys 
453089Swyllys 	KMF_RETURN	(*FindCert) (
463089Swyllys 			KMF_HANDLE_T,
475051Swyllys 			int,
485051Swyllys 			KMF_ATTRIBUTE *);
493089Swyllys 
503089Swyllys 	void		(*FreeKMFCert) (
513089Swyllys 			KMF_HANDLE_T,
523089Swyllys 			KMF_X509_DER_CERT *);
533089Swyllys 
543089Swyllys 	KMF_RETURN	(*StoreCert) (
553089Swyllys 			KMF_HANDLE_T,
565051Swyllys 			int, KMF_ATTRIBUTE *);
573089Swyllys 
583089Swyllys 	KMF_RETURN	(*ImportCert) (
593089Swyllys 			KMF_HANDLE_T,
605051Swyllys 			int, KMF_ATTRIBUTE *);
613089Swyllys 
623089Swyllys 	KMF_RETURN	(*ImportCRL) (
633089Swyllys 			KMF_HANDLE_T,
645051Swyllys 			int, KMF_ATTRIBUTE *);
653089Swyllys 
663089Swyllys 	KMF_RETURN	(*DeleteCert) (
673089Swyllys 			KMF_HANDLE_T,
685051Swyllys 			int, KMF_ATTRIBUTE *);
693089Swyllys 
703089Swyllys 	KMF_RETURN	(*DeleteCRL) (
713089Swyllys 			KMF_HANDLE_T,
725051Swyllys 			int, KMF_ATTRIBUTE *);
733089Swyllys 
743089Swyllys 	KMF_RETURN	(*CreateKeypair) (
753089Swyllys 			KMF_HANDLE_T,
765051Swyllys 			int,
775051Swyllys 			KMF_ATTRIBUTE *);
783089Swyllys 
793089Swyllys 	KMF_RETURN	(*FindKey) (
803089Swyllys 			KMF_HANDLE_T,
815051Swyllys 			int,
825051Swyllys 			KMF_ATTRIBUTE *);
833089Swyllys 
843089Swyllys 	KMF_RETURN	(*EncodePubkeyData) (
853089Swyllys 			KMF_HANDLE_T,
863089Swyllys 			KMF_KEY_HANDLE *,
873089Swyllys 			KMF_DATA *);
883089Swyllys 
893089Swyllys 	KMF_RETURN	(*SignData) (
903089Swyllys 			KMF_HANDLE_T,
913089Swyllys 			KMF_KEY_HANDLE *,
923089Swyllys 			KMF_OID *,
933089Swyllys 			KMF_DATA *,
943089Swyllys 			KMF_DATA *);
953089Swyllys 
963089Swyllys 	KMF_RETURN	(*DeleteKey) (
973089Swyllys 			KMF_HANDLE_T,
985051Swyllys 			int,
995051Swyllys 			KMF_ATTRIBUTE *);
1003089Swyllys 
1013089Swyllys 	KMF_RETURN	(*ListCRL) (
1023089Swyllys 			KMF_HANDLE_T,
1035051Swyllys 			int, KMF_ATTRIBUTE *);
1043089Swyllys 
1053089Swyllys 	KMF_RETURN	(*FindCRL) (
1063089Swyllys 			KMF_HANDLE_T,
1075051Swyllys 			int, KMF_ATTRIBUTE *);
1083089Swyllys 
1093089Swyllys 	KMF_RETURN	(*FindCertInCRL) (
1103089Swyllys 			KMF_HANDLE_T,
1115051Swyllys 			int, KMF_ATTRIBUTE *);
1123089Swyllys 
1133089Swyllys 	KMF_RETURN	(*GetErrorString) (
1143089Swyllys 			KMF_HANDLE_T,
1153089Swyllys 			char **);
1163089Swyllys 
1175051Swyllys 	KMF_RETURN	(*FindPrikeyByCert) (
1183089Swyllys 			KMF_HANDLE_T,
1195051Swyllys 			int,
1205051Swyllys 			KMF_ATTRIBUTE *);
1213089Swyllys 
1223089Swyllys 	KMF_RETURN	(*DecryptData) (
1233089Swyllys 			KMF_HANDLE_T,
1243089Swyllys 			KMF_KEY_HANDLE *,
1253089Swyllys 			KMF_OID *,
1263089Swyllys 			KMF_DATA *,
1273089Swyllys 			KMF_DATA *);
1283089Swyllys 
1295051Swyllys 	KMF_RETURN	(*ExportPK12)(
1303089Swyllys 			KMF_HANDLE_T,
1315051Swyllys 			int,
1325051Swyllys 			KMF_ATTRIBUTE *);
1333089Swyllys 
1343089Swyllys 	KMF_RETURN	(*CreateSymKey) (
1353089Swyllys 			KMF_HANDLE_T,
1365051Swyllys 			int,
1375051Swyllys 			KMF_ATTRIBUTE *);
1383089Swyllys 
1393089Swyllys 	KMF_RETURN	(*GetSymKeyValue) (
1403089Swyllys 			KMF_HANDLE_T,
1413089Swyllys 			KMF_KEY_HANDLE *,
1423089Swyllys 			KMF_RAW_SYM_KEY *);
1433089Swyllys 
1443089Swyllys 	KMF_RETURN	(*SetTokenPin) (
1453089Swyllys 			KMF_HANDLE_T,
1465051Swyllys 			int, KMF_ATTRIBUTE *);
1473089Swyllys 
1483754Swyllys 	KMF_RETURN	(*VerifyDataWithCert) (
1493754Swyllys 			KMF_HANDLE_T,
1503754Swyllys 			KMF_ALGORITHM_INDEX,
1513754Swyllys 			KMF_DATA *,
1523754Swyllys 			KMF_DATA *,
1533754Swyllys 			KMF_DATA *);
1543754Swyllys 
1555051Swyllys 	KMF_RETURN	(*StoreKey) (
1565051Swyllys 			KMF_HANDLE_T,
1575051Swyllys 			int,
1585051Swyllys 			KMF_ATTRIBUTE *);
1595051Swyllys 
1603089Swyllys 	void		(*Finalize) ();
1613089Swyllys 
1623089Swyllys } KMF_PLUGIN_FUNCLIST;
1633089Swyllys 
1643089Swyllys typedef struct {
1655051Swyllys 	KMF_ATTR_TYPE	type;
1665051Swyllys 	boolean_t	null_value_ok; /* Is the pValue required */
1675051Swyllys 	uint32_t	minlen;
1685051Swyllys 	uint32_t	maxlen;
1695051Swyllys } KMF_ATTRIBUTE_TESTER;
1705051Swyllys 
1715051Swyllys typedef struct {
1723089Swyllys 	KMF_KEYSTORE_TYPE	type;
1733089Swyllys 	char			*applications;
1743089Swyllys 	char 			*path;
1753089Swyllys 	void 			*dldesc;
1763089Swyllys 	KMF_PLUGIN_FUNCLIST	*funclist;
1773089Swyllys } KMF_PLUGIN;
1783089Swyllys 
1793089Swyllys typedef struct _KMF_PLUGIN_LIST {
1803089Swyllys 	KMF_PLUGIN		*plugin;
1813089Swyllys 	struct _KMF_PLUGIN_LIST *next;
1823089Swyllys } KMF_PLUGIN_LIST;
1833089Swyllys 
1843089Swyllys typedef struct _kmf_handle {
1853089Swyllys 	/*
1865051Swyllys 	 * session handle opened by kmf_select_token() to talk
1873089Swyllys 	 * to a specific slot in Crypto framework. It is used
1883089Swyllys 	 * by pkcs11 plugin module.
1893089Swyllys 	 */
1903089Swyllys 	CK_SESSION_HANDLE	pk11handle;
1913089Swyllys 	KMF_ERROR		lasterr;
1923089Swyllys 	KMF_POLICY_RECORD	*policy;
1933089Swyllys 	KMF_PLUGIN_LIST		*plugins;
1943089Swyllys } KMF_HANDLE;
1953089Swyllys 
1963089Swyllys #define	CLEAR_ERROR(h, rv) { \
1973089Swyllys 	if (h == NULL) { \
1983089Swyllys 		rv = KMF_ERR_BAD_PARAMETER; \
1993089Swyllys 	} else { \
2003089Swyllys 		h->lasterr.errcode = 0; \
2013089Swyllys 		h->lasterr.kstype = 0; \
2023089Swyllys 		rv = KMF_OK; \
2033089Swyllys 	} \
2043089Swyllys }
2053089Swyllys 
2063089Swyllys #define	KMF_PLUGIN_INIT_SYMBOL	"KMF_Plugin_Initialize"
2073089Swyllys 
2083089Swyllys #ifndef KMF_PLUGIN_PATH
2093089Swyllys #if defined(__sparcv9)
2103089Swyllys #define	KMF_PLUGIN_PATH "/usr/lib/security/sparcv9/"
2113089Swyllys #elif defined(__sparc)
2123089Swyllys #define	KMF_PLUGIN_PATH "/usr/lib/security/"
2133089Swyllys #elif defined(__i386)
2143089Swyllys #define	KMF_PLUGIN_PATH "/usr/lib/security/"
2153089Swyllys #elif defined(__amd64)
2163089Swyllys #define	KMF_PLUGIN_PATH "/usr/lib/security/amd64/"
2173089Swyllys #endif
2183089Swyllys #endif /* !KMF_PLUGIN_PATH */
2193089Swyllys 
2203089Swyllys KMF_PLUGIN_FUNCLIST *KMF_Plugin_Initialize();
2213089Swyllys 
2226051Swyllys extern KMF_RETURN
2236051Swyllys VerifyDataWithKey(KMF_HANDLE_T, KMF_DATA *, KMF_ALGORITHM_INDEX,
2246051Swyllys     KMF_DATA *, KMF_DATA *);
2253089Swyllys 
2266051Swyllys extern KMF_BOOL pkcs_algid_to_keytype(
2276051Swyllys     KMF_ALGORITHM_INDEX, CK_KEY_TYPE *);
2283089Swyllys 
2296051Swyllys extern KMF_RETURN PKCS_VerifyData(
2306051Swyllys     KMF_HANDLE *,
2316051Swyllys     KMF_ALGORITHM_INDEX,
2326051Swyllys     KMF_X509_SPKI *,
2336051Swyllys     KMF_DATA *, KMF_DATA *);
2343089Swyllys 
2356051Swyllys extern KMF_RETURN PKCS_EncryptData(
2366051Swyllys     KMF_HANDLE *,
2376051Swyllys     KMF_ALGORITHM_INDEX,
2386051Swyllys     KMF_X509_SPKI *,
2396051Swyllys     KMF_DATA *,
2406051Swyllys     KMF_DATA *);
2413089Swyllys 
2426051Swyllys extern KMF_PLUGIN *FindPlugin(KMF_HANDLE_T, KMF_KEYSTORE_TYPE);
2433089Swyllys 
2446051Swyllys extern KMF_BOOL IsEqualOid(KMF_OID *, KMF_OID *);
2453089Swyllys 
2466051Swyllys extern KMF_RETURN copy_algoid(KMF_X509_ALGORITHM_IDENTIFIER *destid,
2476051Swyllys     KMF_X509_ALGORITHM_IDENTIFIER *srcid);
2485051Swyllys 
2496051Swyllys extern KMF_OID *x509_algid_to_algoid(KMF_ALGORITHM_INDEX);
2506051Swyllys extern KMF_ALGORITHM_INDEX x509_algoid_to_algid(KMF_OID *);
2515051Swyllys 
2526051Swyllys extern KMF_RETURN PKCS_AcquirePublicKeyHandle(CK_SESSION_HANDLE ckSession,
2536051Swyllys     const KMF_X509_SPKI *, CK_KEY_TYPE, CK_OBJECT_HANDLE *,
2546051Swyllys     KMF_BOOL *);
2553089Swyllys 
2566051Swyllys extern KMF_RETURN GetIDFromSPKI(KMF_X509_SPKI *, KMF_DATA *);
2576051Swyllys extern KMF_RETURN kmf_select_token(KMF_HANDLE_T, char *, int);
2586051Swyllys extern KMF_RETURN kmf_set_altname(KMF_X509_EXTENSIONS *,
2596051Swyllys     KMF_OID *, int, KMF_GENERALNAMECHOICES, char *);
2606051Swyllys extern KMF_RETURN GetSequenceContents(char *, size_t, char **, size_t *);
2616051Swyllys extern KMF_X509_EXTENSION *FindExtn(KMF_X509_EXTENSIONS *, KMF_OID *);
2626051Swyllys extern KMF_RETURN add_an_extension(KMF_X509_EXTENSIONS *exts,
2636051Swyllys     KMF_X509_EXTENSION *newextn);
2646051Swyllys extern KMF_RETURN set_integer(KMF_DATA *, void *, int);
2656051Swyllys extern void free_keyidlist(KMF_OID *, int);
2666051Swyllys extern KMF_RETURN copy_data(KMF_DATA *, KMF_DATA *);
2676051Swyllys extern void Cleanup_PK11_Session(KMF_HANDLE_T handle);
2686051Swyllys extern void free_dp_name(KMF_CRL_DIST_POINT *);
2696051Swyllys extern void free_dp(KMF_CRL_DIST_POINT *);
2706051Swyllys extern KMF_RETURN set_key_usage_extension(KMF_X509_EXTENSIONS *,
2716051Swyllys     int, uint32_t);
2726051Swyllys extern KMF_RETURN init_pk11();
2736051Swyllys extern KMF_RETURN test_attributes(int, KMF_ATTRIBUTE_TESTER *,
2746051Swyllys     int, KMF_ATTRIBUTE_TESTER *, int, KMF_ATTRIBUTE *);
2753089Swyllys 
2763089Swyllys /* Indexes into the key parts array for RSA keys */
2773089Swyllys #define	KMF_RSA_MODULUS			(0)
2783089Swyllys #define	KMF_RSA_PUBLIC_EXPONENT		(1)
2793089Swyllys #define	KMF_RSA_PRIVATE_EXPONENT	(2)
2803089Swyllys #define	KMF_RSA_PRIME1			(3)
2813089Swyllys #define	KMF_RSA_PRIME2			(4)
2823089Swyllys #define	KMF_RSA_EXPONENT1		(5)
2833089Swyllys #define	KMF_RSA_EXPONENT2		(6)
2843089Swyllys #define	KMF_RSA_COEFFICIENT		(7)
2853089Swyllys 
2863089Swyllys /* Key part counts for RSA keys */
2873089Swyllys #define	KMF_NUMBER_RSA_PUBLIC_KEY_PARTS		(2)
2883089Swyllys #define	KMF_NUMBER_RSA_PRIVATE_KEY_PARTS	(8)
2893089Swyllys 
2903089Swyllys /* Key part counts for DSA keys */
2913089Swyllys #define	KMF_NUMBER_DSA_PUBLIC_KEY_PARTS		(4)
2923089Swyllys #define	KMF_NUMBER_DSA_PRIVATE_KEY_PARTS	(4)
2933089Swyllys 
2943089Swyllys /* Indexes into the key parts array for DSA keys */
2953089Swyllys #define	KMF_DSA_PRIME		(0)
2963089Swyllys #define	KMF_DSA_SUB_PRIME	(1)
2973089Swyllys #define	KMF_DSA_BASE		(2)
2983089Swyllys #define	KMF_DSA_PUBLIC_VALUE	(3)
2993089Swyllys 
3003089Swyllys #ifndef max
3013089Swyllys #define	max(a, b) ((a) < (b) ? (b) : (a))
3023089Swyllys #endif
3033089Swyllys 
3043089Swyllys /* Maximum key parts for all algorithms */
3053089Swyllys #define	KMF_MAX_PUBLIC_KEY_PARTS \
3063089Swyllys 	(max(KMF_NUMBER_RSA_PUBLIC_KEY_PARTS, \
3073089Swyllys 	KMF_NUMBER_DSA_PUBLIC_KEY_PARTS))
3083089Swyllys 
3093089Swyllys #define	KMF_MAX_PRIVATE_KEY_PARTS \
3103089Swyllys 	(max(KMF_NUMBER_RSA_PRIVATE_KEY_PARTS, \
3113089Swyllys 	KMF_NUMBER_DSA_PRIVATE_KEY_PARTS))
3123089Swyllys 
3133089Swyllys #define	KMF_MAX_KEY_PARTS \
3143089Swyllys 	(max(KMF_MAX_PUBLIC_KEY_PARTS, KMF_MAX_PRIVATE_KEY_PARTS))
3153089Swyllys 
3163089Swyllys typedef enum {
3173089Swyllys 	KMF_ALGMODE_NONE	= 0,
3183089Swyllys 	KMF_ALGMODE_CUSTOM,
3193089Swyllys 	KMF_ALGMODE_PUBLIC_KEY,
3203089Swyllys 	KMF_ALGMODE_PRIVATE_KEY,
3213089Swyllys 	KMF_ALGMODE_PKCS1_EMSA_V15
3223089Swyllys } KMF_SIGNATURE_MODE;
3233089Swyllys 
3243089Swyllys #define	KMF_CERT_PRINTABLE_LEN	1024
3253089Swyllys #define	SHA1_HASH_LENGTH 20
3263089Swyllys 
3273089Swyllys #define	OCSPREQ_TEMPNAME	"/tmp/ocsp.reqXXXXXX"
3283089Swyllys #define	OCSPRESP_TEMPNAME	"/tmp/ocsp.respXXXXXX"
3293089Swyllys 
3305626Shylee #define	_PATH_KMF_CONF	"/etc/crypto/kmf.conf"
3315626Shylee #define	CONF_MODULEPATH	"modulepath="
3325626Shylee #define	CONF_OPTION	"option="
3335626Shylee 
3345626Shylee typedef struct {
3355626Shylee 	char			*keystore;
3365626Shylee 	char			*modulepath;
3375626Shylee 	char 			*option;
3385626Shylee 	KMF_KEYSTORE_TYPE	kstype;
3395626Shylee } conf_entry_t;
3405626Shylee 
3415626Shylee typedef struct conf_entrylist {
3425626Shylee 	conf_entry_t		*entry;
3435626Shylee 	struct conf_entrylist 	*next;
3445626Shylee } conf_entrylist_t;
3455626Shylee 
3465626Shylee 
3475626Shylee extern KMF_RETURN get_entrylist(conf_entrylist_t **);
3485626Shylee extern void free_entrylist(conf_entrylist_t *);
3495626Shylee extern void free_entry(conf_entry_t *);
3505626Shylee extern conf_entry_t *dup_entry(conf_entry_t *);
3516051Swyllys extern boolean_t is_valid_keystore_type(KMF_KEYSTORE_TYPE);
3526051Swyllys extern KMF_BOOL is_eku_present(KMF_X509EXT_EKU *, KMF_OID *);
3536051Swyllys extern KMF_RETURN parse_eku_data(const KMF_DATA *, KMF_X509EXT_EKU *);
354*6669Swyllys extern KMF_RETURN
355*6669Swyllys copy_extension_data(KMF_X509_EXTENSION *, KMF_X509_EXTENSION *);
3565626Shylee 
3573089Swyllys #ifdef __cplusplus
3583089Swyllys }
3593089Swyllys #endif
3603089Swyllys #endif /* _KMFAPIP_H */
361