common.h revision 6051:7b29d160facb
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#ifndef _PKTOOL_COMMON_H
27#define	_PKTOOL_COMMON_H
28
29#pragma ident	"%Z%%M%	%I%	%E% SMI"
30
31/*
32 * This file contains data and functions shared between all the
33 * modules that comprise this tool.
34 */
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#include <cryptoutil.h>
41
42/* I18N helpers. */
43#include <libintl.h>
44#include <locale.h>
45#include <errno.h>
46#include <kmfapi.h>
47
48/* Defines used throughout */
49
50/* Error codes */
51#define	PK_ERR_NONE		0
52#define	PK_ERR_USAGE		1
53#define	PK_ERR_QUIT		2
54#define	PK_ERR_PK11		3
55#define	PK_ERR_SYSTEM		4
56#define	PK_ERR_OPENSSL		5
57#define	PK_ERR_NSS		6
58
59/* Types of objects for searches. */
60#define	PK_PRIVATE_OBJ		0x0001
61#define	PK_PUBLIC_OBJ		0x0002
62#define	PK_CERT_OBJ		0x0010
63#define	PK_PRIKEY_OBJ		0x0020
64#define	PK_PUBKEY_OBJ		0x0040
65#define	PK_SYMKEY_OBJ		0x0080
66#define	PK_CRL_OBJ		0x0100
67
68#define	PK_KEY_OBJ		(PK_PRIKEY_OBJ | PK_PUBKEY_OBJ | PK_SYMKEY_OBJ)
69#define	PK_ALL_OBJ		(PK_PRIVATE_OBJ | PK_PUBLIC_OBJ |\
70				PK_CERT_OBJ| PK_CRL_OBJ | PK_KEY_OBJ)
71
72#define	PK_DEFAULT_KEYTYPE	"rsa"
73#define	PK_DEFAULT_KEYLENGTH	1024
74#define	PK_DEFAULT_DIRECTORY	"."
75#define	PK_DEFAULT_SERIALNUM	1
76#define	PK_DEFAULT_PK11TOKEN	SOFT_TOKEN_LABEL
77
78/* Constants for attribute templates. */
79extern CK_BBOOL	pk_false;
80extern CK_BBOOL	pk_true;
81
82typedef struct {
83	int	eku_count;
84	int	*critlist;
85	KMF_OID	*ekulist;
86} EKU_LIST;
87
88/* Common functions. */
89extern CK_RV	init_pk11(void);
90extern void	final_pk11(CK_SESSION_HANDLE sess);
91
92extern CK_RV	login_token(CK_SLOT_ID slot_id, CK_UTF8CHAR_PTR pin,
93		    CK_ULONG pinlen, CK_SESSION_HANDLE_PTR sess);
94
95extern CK_RV	quick_start(CK_SLOT_ID slot_id, CK_FLAGS sess_flags,
96		    CK_UTF8CHAR_PTR pin, CK_ULONG pinlen,
97		    CK_SESSION_HANDLE_PTR sess);
98
99extern CK_RV	get_pin(char *prompt1, char *prompt2, CK_UTF8CHAR_PTR *pin,
100		    CK_ULONG *pinlen);
101extern boolean_t	yesno(char *prompt, char *invalid, boolean_t dflt);
102
103extern CK_RV	get_token_slots(CK_SLOT_ID_PTR *slot_list,
104		    CK_ULONG *slot_count);
105
106extern int get_subname(char **);
107extern int get_serial(char **);
108extern int get_certlabel(char **);
109extern int get_filename(char *, char **);
110
111extern int	getopt_av(int argc, char * const argv[], const char *optstring);
112extern char	*optarg_av;
113extern int	optind_av;
114
115int OT2Int(char *);
116int PK2Int(char *);
117KMF_KEYSTORE_TYPE KS2Int(char *);
118int Str2KeyType(char *, KMF_KEY_ALG *, KMF_ALGORITHM_INDEX *);
119int Str2SymKeyType(char *, KMF_KEY_ALG *);
120int Str2Lifetime(char *, uint32_t *);
121KMF_RETURN select_token(void *, char *, int);
122KMF_RETURN configure_nss(void *, char *, char *);
123
124KMF_ENCODE_FORMAT Str2Format(char *);
125KMF_RETURN get_pk12_password(KMF_CREDENTIAL *);
126KMF_RETURN hexstring2bytes(uchar_t *, uchar_t **, size_t *);
127KMF_RETURN verify_altname(char *arg, KMF_GENERALNAMECHOICES *, int *);
128KMF_RETURN verify_keyusage(char *arg, uint16_t *, int *);
129KMF_RETURN verify_file(char *);
130KMF_RETURN verify_ekunames(char *, EKU_LIST **);
131
132void free_eku_list(EKU_LIST *);
133
134int yn_to_int(char *);
135
136int get_token_password(KMF_KEYSTORE_TYPE, char *, KMF_CREDENTIAL *);
137void display_error(void *, KMF_RETURN, char *);
138#define	DEFAULT_NSS_TOKEN	"internal"
139#define	DEFAULT_TOKEN_PROMPT	"Enter PIN for %s: "
140
141#define	EMPTYSTRING(s) (s == NULL || !strlen((char *)s))
142
143#ifdef __cplusplus
144}
145#endif
146
147#endif /* _PKTOOL_COMMON_H */
148