10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * 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. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 20*12575SJan.Pechanec@Sun.COM * 21*12575SJan.Pechanec@Sun.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 220Sstevel@tonic-gate */ 230Sstevel@tonic-gate 240Sstevel@tonic-gate #ifndef _CRYPTOUTIL_H 250Sstevel@tonic-gate #define _CRYPTOUTIL_H 260Sstevel@tonic-gate 270Sstevel@tonic-gate #ifdef __cplusplus 280Sstevel@tonic-gate extern "C" { 290Sstevel@tonic-gate #endif 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <syslog.h> 330Sstevel@tonic-gate #include <security/cryptoki.h> 340Sstevel@tonic-gate #include <sys/param.h> 350Sstevel@tonic-gate 360Sstevel@tonic-gate #define LOG_STDERR -1 370Sstevel@tonic-gate #define SUCCESS 0 380Sstevel@tonic-gate #define FAILURE 1 390Sstevel@tonic-gate #define MECH_ID_HEX_LEN 11 /* length of mechanism id in hex form */ 400Sstevel@tonic-gate 410Sstevel@tonic-gate #define _PATH_PKCS11_CONF "/etc/crypto/pkcs11.conf" 4210500SHai-May.Chao@Sun.COM #define _PATH_KCF_CONF "/etc/crypto/kcf.conf" 430Sstevel@tonic-gate #define _PATH_KCFD_LOCK "/var/run/kcfd.lock" 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* $ISA substitution for parsing pkcs11.conf data */ 460Sstevel@tonic-gate #define PKCS11_ISA "/$ISA/" 470Sstevel@tonic-gate #if defined(_LP64) 480Sstevel@tonic-gate #define PKCS11_ISA_DIR "/64/" 490Sstevel@tonic-gate #else /* !_LP64 */ 500Sstevel@tonic-gate #define PKCS11_ISA_DIR "/" 510Sstevel@tonic-gate #endif 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* keywords and delimiters for parsing configuration files */ 540Sstevel@tonic-gate #define SEP_COLON ":" 550Sstevel@tonic-gate #define SEP_SEMICOLON ";" 560Sstevel@tonic-gate #define SEP_EQUAL "=" 570Sstevel@tonic-gate #define SEP_COMMA "," 580Sstevel@tonic-gate #define METASLOT_KEYWORD "metaslot" 5910500SHai-May.Chao@Sun.COM #define FIPS_KEYWORD "fips-140" 600Sstevel@tonic-gate #define EF_DISABLED "disabledlist=" 610Sstevel@tonic-gate #define EF_ENABLED "enabledlist=" 620Sstevel@tonic-gate #define EF_NORANDOM "NO_RANDOM" 630Sstevel@tonic-gate #define METASLOT_TOKEN "metaslot_token=" 640Sstevel@tonic-gate #define METASLOT_SLOT "metaslot_slot=" 650Sstevel@tonic-gate #define METASLOT_STATUS "metaslot_status=" 6610500SHai-May.Chao@Sun.COM #define EF_FIPS_STATUS "fips_status=" 670Sstevel@tonic-gate #define METASLOT_AUTO_KEY_MIGRATE "metaslot_auto_key_migrate=" 6810500SHai-May.Chao@Sun.COM #define ENABLED_KEYWORD "enabled" 6910500SHai-May.Chao@Sun.COM #define DISABLED_KEYWORD "disabled" 700Sstevel@tonic-gate #define SLOT_DESCRIPTION_SIZE 64 710Sstevel@tonic-gate #define TOKEN_LABEL_SIZE 32 725252Sdinak #define TOKEN_MANUFACTURER_SIZE 32 735252Sdinak #define TOKEN_SERIAL_SIZE 16 7410500SHai-May.Chao@Sun.COM #define CRYPTO_FIPS_MODE_DISABLED 0 7510500SHai-May.Chao@Sun.COM #define CRYPTO_FIPS_MODE_ENABLED 1 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * Define the following softtoken values that are used by softtoken 790Sstevel@tonic-gate * library, cryptoadm and pktool command. 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate #define SOFT_SLOT_DESCRIPTION \ 820Sstevel@tonic-gate "Sun Crypto Softtoken " \ 830Sstevel@tonic-gate " " 840Sstevel@tonic-gate #define SOFT_TOKEN_LABEL "Sun Software PKCS#11 softtoken " 850Sstevel@tonic-gate #define SOFT_TOKEN_SERIAL " " 860Sstevel@tonic-gate #define SOFT_MANUFACTURER_ID "Sun Microsystems, Inc. " 870Sstevel@tonic-gate #define SOFT_DEFAULT_PIN "changeme" 880Sstevel@tonic-gate 890Sstevel@tonic-gate typedef char libname_t[MAXPATHLEN]; 900Sstevel@tonic-gate typedef char midstr_t[MECH_ID_HEX_LEN]; 910Sstevel@tonic-gate 920Sstevel@tonic-gate typedef struct umechlist { 930Sstevel@tonic-gate midstr_t name; /* mechanism name in hex form */ 940Sstevel@tonic-gate struct umechlist *next; 950Sstevel@tonic-gate } umechlist_t; 960Sstevel@tonic-gate 970Sstevel@tonic-gate typedef struct uentry { 980Sstevel@tonic-gate libname_t name; 990Sstevel@tonic-gate boolean_t flag_norandom; /* TRUE if random is disabled */ 1000Sstevel@tonic-gate boolean_t flag_enabledlist; /* TRUE if an enabledlist */ 1010Sstevel@tonic-gate umechlist_t *policylist; /* disabledlist or enabledlist */ 1020Sstevel@tonic-gate boolean_t flag_metaslot_enabled; /* TRUE if metaslot's enabled */ 1030Sstevel@tonic-gate boolean_t flag_metaslot_auto_key_migrate; 1040Sstevel@tonic-gate CK_UTF8CHAR metaslot_ks_slot[SLOT_DESCRIPTION_SIZE + 1]; 1050Sstevel@tonic-gate CK_UTF8CHAR metaslot_ks_token[TOKEN_LABEL_SIZE + 1]; 1060Sstevel@tonic-gate int count; 10710979SHai-May.Chao@Sun.COM boolean_t flag_fips_enabled; 1080Sstevel@tonic-gate } uentry_t; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate typedef struct uentrylist { 1110Sstevel@tonic-gate uentry_t *puent; 1120Sstevel@tonic-gate struct uentrylist *next; 1130Sstevel@tonic-gate } uentrylist_t; 1140Sstevel@tonic-gate 115*12575SJan.Pechanec@Sun.COM /* Return codes for pkcs11_parse_uri() */ 116*12575SJan.Pechanec@Sun.COM #define PK11_URI_OK 0 117*12575SJan.Pechanec@Sun.COM #define PK11_URI_INVALID 1 118*12575SJan.Pechanec@Sun.COM #define PK11_MALLOC_ERROR 2 119*12575SJan.Pechanec@Sun.COM #define PK11_URI_VALUE_OVERFLOW 3 120*12575SJan.Pechanec@Sun.COM #define PK11_NOT_PKCS11_URI 4 121*12575SJan.Pechanec@Sun.COM 122*12575SJan.Pechanec@Sun.COM /* 123*12575SJan.Pechanec@Sun.COM * There is no limit for the attribute length in the spec. 256 bytes should be 124*12575SJan.Pechanec@Sun.COM * enough for the object name. 125*12575SJan.Pechanec@Sun.COM */ 126*12575SJan.Pechanec@Sun.COM #define PK11_MAX_OBJECT_LEN 256 127*12575SJan.Pechanec@Sun.COM /* 128*12575SJan.Pechanec@Sun.COM * CKA_ID is of type "byte array" which can be of arbitrary length. 256 bytes 129*12575SJan.Pechanec@Sun.COM * should be sufficient though. 130*12575SJan.Pechanec@Sun.COM */ 131*12575SJan.Pechanec@Sun.COM #define PK11_MAX_ID_LEN 256 132*12575SJan.Pechanec@Sun.COM 133*12575SJan.Pechanec@Sun.COM /* Structure for the PKCS#11 URI. */ 134*12575SJan.Pechanec@Sun.COM typedef struct pkcs11_uri_t { 135*12575SJan.Pechanec@Sun.COM /* CKA_LABEL attribute to the C_FindObjectsInit function. */ 136*12575SJan.Pechanec@Sun.COM CK_UTF8CHAR_PTR object; 137*12575SJan.Pechanec@Sun.COM /* 138*12575SJan.Pechanec@Sun.COM * CKA_CLASS attribute to the C_FindObjectsInit function. The 139*12575SJan.Pechanec@Sun.COM * "objecttype" URI attribute can have a value one of "private", 140*12575SJan.Pechanec@Sun.COM * "public", "cert", "secretkey", and "data". The "objecttype" field can 141*12575SJan.Pechanec@Sun.COM * have a value of CKO_PUBLIC_KEY, CKO_PRIVATE_KEY, CKO_CERTIFICATE, 142*12575SJan.Pechanec@Sun.COM * CKO_SECRET_KEY, and CKO_DATA. This attribute cannot be empty in the 143*12575SJan.Pechanec@Sun.COM * URI. 144*12575SJan.Pechanec@Sun.COM */ 145*12575SJan.Pechanec@Sun.COM CK_ULONG objecttype; 146*12575SJan.Pechanec@Sun.COM /* CKO_DATA is 0 so we need this flag. Not part of the URI itself. */ 147*12575SJan.Pechanec@Sun.COM boolean_t objecttype_present; 148*12575SJan.Pechanec@Sun.COM /* 149*12575SJan.Pechanec@Sun.COM * Token, manufufacturer, serial and model are of fixed size length in 150*12575SJan.Pechanec@Sun.COM * the specification. We allocate memory on the fly to distinguish 151*12575SJan.Pechanec@Sun.COM * between an attribute not present and an empty value. We check for 152*12575SJan.Pechanec@Sun.COM * overflows. We always terminate the string with '\0' even when that is 153*12575SJan.Pechanec@Sun.COM * not used in the PKCS#11's CK_TOKEN_INFO structure (fields are padded 154*12575SJan.Pechanec@Sun.COM * with spaces). 155*12575SJan.Pechanec@Sun.COM */ 156*12575SJan.Pechanec@Sun.COM /* Token label from CK_TOKEN_INFO. */ 157*12575SJan.Pechanec@Sun.COM CK_UTF8CHAR_PTR token; 158*12575SJan.Pechanec@Sun.COM /* ManufacturerID from CK_TOKEN_INFO. */ 159*12575SJan.Pechanec@Sun.COM CK_UTF8CHAR_PTR manuf; 160*12575SJan.Pechanec@Sun.COM /* SerialNumber from CK_TOKEN_INFO. */ 161*12575SJan.Pechanec@Sun.COM CK_CHAR_PTR serial; 162*12575SJan.Pechanec@Sun.COM /* Model from CK_TOKEN_INFO. */ 163*12575SJan.Pechanec@Sun.COM CK_UTF8CHAR_PTR model; 164*12575SJan.Pechanec@Sun.COM /* This is a byte array, we need a length parameter as well. */ 165*12575SJan.Pechanec@Sun.COM CK_BYTE_PTR id; 166*12575SJan.Pechanec@Sun.COM int id_len; 167*12575SJan.Pechanec@Sun.COM /* 168*12575SJan.Pechanec@Sun.COM * Location of the file with a token PIN. Application can overload this, 169*12575SJan.Pechanec@Sun.COM * eg. "/bin/askpass|" may mean to read the PIN from a command. However, 170*12575SJan.Pechanec@Sun.COM * the pkcs11_parse_uri() function does not interpret this field in any 171*12575SJan.Pechanec@Sun.COM * way. 172*12575SJan.Pechanec@Sun.COM */ 173*12575SJan.Pechanec@Sun.COM char *pinfile; 174*12575SJan.Pechanec@Sun.COM } pkcs11_uri_t; 175*12575SJan.Pechanec@Sun.COM 1760Sstevel@tonic-gate extern void cryptodebug(const char *fmt, ...); 1770Sstevel@tonic-gate extern void cryptoerror(int priority, const char *fmt, ...); 1780Sstevel@tonic-gate extern void cryptodebug_init(const char *prefix); 1798309SAnthony.Scarpino@Sun.COM extern void cryptoerror_off(); 1808309SAnthony.Scarpino@Sun.COM extern void cryptoerror_on(); 1810Sstevel@tonic-gate 1827011Sda73024 extern const char *pkcs11_mech2str(CK_MECHANISM_TYPE mech); 1830Sstevel@tonic-gate extern CK_RV pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech); 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate extern int get_pkcs11conf_info(uentrylist_t **); 1860Sstevel@tonic-gate extern umechlist_t *create_umech(char *); 1870Sstevel@tonic-gate extern void free_umechlist(umechlist_t *); 1880Sstevel@tonic-gate extern void free_uentrylist(uentrylist_t *); 1890Sstevel@tonic-gate extern void free_uentry(uentry_t *); 1903089Swyllys extern uentry_t *getent_uef(char *); 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate extern void tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen); 1938309SAnthony.Scarpino@Sun.COM extern int hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes, 1948309SAnthony.Scarpino@Sun.COM size_t *blen); 1950Sstevel@tonic-gate extern CK_RV pkcs11_mech2keytype(CK_MECHANISM_TYPE mech_type, 1960Sstevel@tonic-gate CK_KEY_TYPE *ktype); 1976104Sdinak extern CK_RV pkcs11_mech2keygen(CK_MECHANISM_TYPE mech_type, 1986104Sdinak CK_MECHANISM_TYPE *gen_mech); 1990Sstevel@tonic-gate extern char *pkcs11_strerror(CK_RV rv); 2000Sstevel@tonic-gate 2013089Swyllys extern int 2023089Swyllys get_metaslot_info(boolean_t *status_enabled, boolean_t *migrate_enabled, 2033089Swyllys char **objectstore_slot_info, char **objectstore_token_info); 2043089Swyllys 2053089Swyllys extern char *get_fullpath(char *dir, char *filepath); 2063089Swyllys extern int str2lifetime(char *ltimestr, uint32_t *ltime); 2073089Swyllys 2085252Sdinak extern char *pkcs11_default_token(void); 2095252Sdinak extern int pkcs11_get_pass(char *token_name, char **pdata, size_t *psize, 2105252Sdinak size_t min_psize, boolean_t with_confirmation); 2115252Sdinak 2129127SDina.Nimeh@Sun.COM extern int pkcs11_seed_urandom(void *sbuf, size_t slen); 2139127SDina.Nimeh@Sun.COM extern int pkcs11_get_random(void *dbuf, size_t dlen); 2149127SDina.Nimeh@Sun.COM extern int pkcs11_get_urandom(void *dbuf, size_t dlen); 2159127SDina.Nimeh@Sun.COM extern int pkcs11_get_nzero_urandom(void *dbuf, size_t dlen); 2169127SDina.Nimeh@Sun.COM extern void pkcs11_close_random(void); 2179127SDina.Nimeh@Sun.COM extern void pkcs11_close_urandom(void); 2189127SDina.Nimeh@Sun.COM extern void pkcs11_close_urandom_seed(void); 2195252Sdinak extern int pkcs11_read_data(char *filename, void **dbuf, size_t *dlen); 2205252Sdinak 2218932SDina.Nimeh@Sun.COM extern int open_nointr(const char *path, int oflag, ...); 2228932SDina.Nimeh@Sun.COM extern ssize_t readn_nointr(int fd, void *dbuf, size_t dlen); 2238932SDina.Nimeh@Sun.COM extern ssize_t writen_nointr(int fd, void *dbuf, size_t dlen); 22410500SHai-May.Chao@Sun.COM extern int update_conf(char *conf_file, char *entry); 22510500SHai-May.Chao@Sun.COM 22610500SHai-May.Chao@Sun.COM extern CK_RV get_fips_mode(int *); 2278932SDina.Nimeh@Sun.COM 228*12575SJan.Pechanec@Sun.COM extern int pkcs11_parse_uri(const char *str, pkcs11_uri_t *uri); 229*12575SJan.Pechanec@Sun.COM extern void pkcs11_free_uri(pkcs11_uri_t *uri); 230*12575SJan.Pechanec@Sun.COM 2310Sstevel@tonic-gate #ifdef __cplusplus 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate #endif 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate #endif /* _CRYPTOUTIL_H */ 236