xref: /onnv-gate/usr/src/cmd/cmd-crypto/pktool/common.h (revision 6884:ca518f772fa0)
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
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226051Swyllys  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef _PKTOOL_COMMON_H
270Sstevel@tonic-gate #define	_PKTOOL_COMMON_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * This file contains data and functions shared between all the
330Sstevel@tonic-gate  * modules that comprise this tool.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #ifdef __cplusplus
370Sstevel@tonic-gate extern "C" {
380Sstevel@tonic-gate #endif
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <cryptoutil.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /* I18N helpers. */
430Sstevel@tonic-gate #include <libintl.h>
440Sstevel@tonic-gate #include <locale.h>
453089Swyllys #include <errno.h>
463089Swyllys #include <kmfapi.h>
470Sstevel@tonic-gate 
4817Sdinak /* Defines used throughout */
4917Sdinak 
500Sstevel@tonic-gate /* Error codes */
510Sstevel@tonic-gate #define	PK_ERR_NONE		0
520Sstevel@tonic-gate #define	PK_ERR_USAGE		1
530Sstevel@tonic-gate #define	PK_ERR_QUIT		2
5417Sdinak #define	PK_ERR_PK11		3
5517Sdinak #define	PK_ERR_SYSTEM		4
5617Sdinak #define	PK_ERR_OPENSSL		5
573089Swyllys #define	PK_ERR_NSS		6
5817Sdinak 
5917Sdinak /* Types of objects for searches. */
6017Sdinak #define	PK_PRIVATE_OBJ		0x0001
6117Sdinak #define	PK_PUBLIC_OBJ		0x0002
6217Sdinak #define	PK_CERT_OBJ		0x0010
6317Sdinak #define	PK_PRIKEY_OBJ		0x0020
6417Sdinak #define	PK_PUBKEY_OBJ		0x0040
653089Swyllys #define	PK_SYMKEY_OBJ		0x0080
663089Swyllys #define	PK_CRL_OBJ		0x0100
6717Sdinak 
683089Swyllys #define	PK_KEY_OBJ		(PK_PRIKEY_OBJ | PK_PUBKEY_OBJ | PK_SYMKEY_OBJ)
693089Swyllys #define	PK_ALL_OBJ		(PK_PRIVATE_OBJ | PK_PUBLIC_OBJ |\
703089Swyllys 				PK_CERT_OBJ| PK_CRL_OBJ | PK_KEY_OBJ)
713089Swyllys 
723089Swyllys #define	PK_DEFAULT_KEYTYPE	"rsa"
733089Swyllys #define	PK_DEFAULT_KEYLENGTH	1024
743089Swyllys #define	PK_DEFAULT_DIRECTORY	"."
753089Swyllys #define	PK_DEFAULT_SERIALNUM	1
763089Swyllys #define	PK_DEFAULT_PK11TOKEN	SOFT_TOKEN_LABEL
7717Sdinak 
7817Sdinak /* Constants for attribute templates. */
7917Sdinak extern CK_BBOOL	pk_false;
8017Sdinak extern CK_BBOOL	pk_true;
8117Sdinak 
826051Swyllys typedef struct {
836051Swyllys 	int	eku_count;
846051Swyllys 	int	*critlist;
856051Swyllys 	KMF_OID	*ekulist;
866051Swyllys } EKU_LIST;
8717Sdinak 
8817Sdinak /* Common functions. */
8917Sdinak extern void	final_pk11(CK_SESSION_HANDLE sess);
9017Sdinak 
9117Sdinak extern CK_RV	login_token(CK_SLOT_ID slot_id, CK_UTF8CHAR_PTR pin,
9217Sdinak 		    CK_ULONG pinlen, CK_SESSION_HANDLE_PTR sess);
930Sstevel@tonic-gate 
9417Sdinak extern CK_RV	quick_start(CK_SLOT_ID slot_id, CK_FLAGS sess_flags,
9517Sdinak 		    CK_UTF8CHAR_PTR pin, CK_ULONG pinlen,
9617Sdinak 		    CK_SESSION_HANDLE_PTR sess);
9717Sdinak 
9817Sdinak extern CK_RV	get_pin(char *prompt1, char *prompt2, CK_UTF8CHAR_PTR *pin,
9917Sdinak 		    CK_ULONG *pinlen);
10017Sdinak extern boolean_t	yesno(char *prompt, char *invalid, boolean_t dflt);
10117Sdinak 
10217Sdinak extern CK_RV	get_token_slots(CK_SLOT_ID_PTR *slot_list,
10317Sdinak 		    CK_ULONG *slot_count);
10417Sdinak 
1053089Swyllys extern int get_subname(char **);
1065221Swyllys extern int get_serial(char **);
1075221Swyllys extern int get_certlabel(char **);
1085221Swyllys extern int get_filename(char *, char **);
1090Sstevel@tonic-gate 
110864Sdinak extern int	getopt_av(int argc, char * const argv[], const char *optstring);
111864Sdinak extern char	*optarg_av;
112864Sdinak extern int	optind_av;
113864Sdinak 
1143089Swyllys int OT2Int(char *);
1153089Swyllys int PK2Int(char *);
1163089Swyllys KMF_KEYSTORE_TYPE KS2Int(char *);
1173089Swyllys int Str2KeyType(char *, KMF_KEY_ALG *, KMF_ALGORITHM_INDEX *);
1183089Swyllys int Str2SymKeyType(char *, KMF_KEY_ALG *);
1193089Swyllys int Str2Lifetime(char *, uint32_t *);
1203089Swyllys KMF_RETURN select_token(void *, char *, int);
1213089Swyllys KMF_RETURN configure_nss(void *, char *, char *);
1223089Swyllys 
1233089Swyllys KMF_ENCODE_FORMAT Str2Format(char *);
1243089Swyllys KMF_RETURN get_pk12_password(KMF_CREDENTIAL *);
1253089Swyllys KMF_RETURN hexstring2bytes(uchar_t *, uchar_t **, size_t *);
1263089Swyllys KMF_RETURN verify_altname(char *arg, KMF_GENERALNAMECHOICES *, int *);
1273089Swyllys KMF_RETURN verify_keyusage(char *arg, uint16_t *, int *);
1283089Swyllys KMF_RETURN verify_file(char *);
1296051Swyllys KMF_RETURN verify_ekunames(char *, EKU_LIST **);
1306354Swyllys KMF_RETURN token_auth_needed(KMF_HANDLE_T, char *, int *);
1316051Swyllys 
1326051Swyllys void free_eku_list(EKU_LIST *);
1336051Swyllys 
1346051Swyllys int yn_to_int(char *);
1356051Swyllys 
1363089Swyllys int get_token_password(KMF_KEYSTORE_TYPE, char *, KMF_CREDENTIAL *);
1373089Swyllys void display_error(void *, KMF_RETURN, char *);
1386354Swyllys 
1393089Swyllys #define	DEFAULT_NSS_TOKEN	"internal"
1404006Shylee #define	DEFAULT_TOKEN_PROMPT	"Enter PIN for %s: "
1413089Swyllys 
1423089Swyllys #define	EMPTYSTRING(s) (s == NULL || !strlen((char *)s))
143*6884Swyllys /*
144*6884Swyllys  * The "dir" option is only valid with the NSS keystore.  This check
145*6884Swyllys  * forces PK_ERR_USAGE when it is used with non-NSS keystore.
146*6884Swyllys  */
147*6884Swyllys #define	DIR_OPTION_CHECK(k, d) \
148*6884Swyllys if (k != KMF_KEYSTORE_NSS && d != NULL) { \
149*6884Swyllys 	cryptoerror(LOG_STDERR, gettext("The 'dir' option is " \
150*6884Swyllys 	    "not supported with the indicated keystore\n")); \
151*6884Swyllys 	return (PK_ERR_USAGE); \
152*6884Swyllys }
153*6884Swyllys 
1543089Swyllys 
1550Sstevel@tonic-gate #ifdef __cplusplus
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate #endif
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #endif /* _PKTOOL_COMMON_H */
160