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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
30 #include <cryptoutil.h>
32 #define MAX_PASS_TRIES 5 /* maximum tries to get passphrase */
34 #define DEFAULT_TOKEN_PROMPT gettext("Enter PIN for %s: ")
35 #define DEFAULT_TOKEN_REPROMPT gettext("Re-enter PIN for %s: ")
36 #define DEFAULT_TOKEN_MINSIZE gettext("PIN must be at least %d characters.\n")
38 #define DEFAULT_USER_PROMPT gettext("Enter passphrase: ")
39 #define DEFAULT_USER_REPROMPT gettext("Re-enter passphrase: ")
40 #define DEFAULT_USER_MINSIZE \
41 gettext("Passphrase must be at least %d characters.\n")
43 #define DEFAULT_PK11TOKEN SOFT_TOKEN_LABEL
49 pkcs11_default_token(void)
51 return (DEFAULT_PK11TOKEN
);
55 * Prompt user for a passphrase or the PIN for a token.
57 * An optional minimum length can be enforced. Caller can optionally also
58 * reprompt for the passphrase/PIN to confirm it was entered correctly.
59 * The caller must free the buffer containing the passphrase/PIN with free().
60 * 0 returned for success, -1 for failure with the first passphrase/PIN,
61 * -2 for failure with the optional second passphrase/PIN used to confirm.
64 pkcs11_get_pass(char *token_name
, char **pdata
, size_t *psize
, size_t min_psize
,
65 boolean_t with_confirmation
)
72 if (token_name
!= NULL
)
73 (void) snprintf(prompt
, sizeof (prompt
), DEFAULT_TOKEN_PROMPT
,
76 (void) snprintf(prompt
, sizeof (prompt
), DEFAULT_USER_PROMPT
);
78 for (tries
= MAX_PASS_TRIES
; tries
> 0; tries
--) {
79 tmpbuf
= getpassphrase(prompt
);
83 if (strnlen(tmpbuf
, min_psize
) >= min_psize
)
86 if (token_name
!= NULL
)
87 (void) printf(DEFAULT_TOKEN_MINSIZE
, min_psize
);
89 (void) printf(DEFAULT_USER_MINSIZE
, min_psize
);
92 (void) printf(gettext("Exceeded number of attempts.\n"));
96 databuf
= strdup(tmpbuf
);
97 (void) memset(tmpbuf
, 0, strlen(tmpbuf
)); /* clean up */
101 if (with_confirmation
) {
102 if (token_name
!= NULL
)
103 (void) snprintf(prompt
, sizeof (prompt
),
104 DEFAULT_TOKEN_REPROMPT
, token_name
);
106 (void) snprintf(prompt
, sizeof (prompt
),
107 DEFAULT_USER_REPROMPT
);
108 tmpbuf
= getpassphrase(prompt
);
109 if (tmpbuf
== NULL
) {
111 (void) memset(databuf
, 0, strlen(databuf
));
116 if (strcmp(databuf
, tmpbuf
) != 0) {
118 (void) memset(tmpbuf
, 0, strlen(tmpbuf
));
119 (void) memset(databuf
, 0, strlen(databuf
));
126 *psize
= strlen(databuf
);