summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2014-07-02 13:17:55 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2014-07-02 13:17:55 +0200
commitff3619a0129ec11fc605ef6ef8e85f62441babaa (patch)
treedee123cfc08e8dedfe6b3c83450b35f7fde5aaa0 /src
parent6f7eb1cae8cf517d038fe4328c488bfbc5d10ac8 (diff)
downloadgnutls-ff3619a0129ec11fc605ef6ef8e85f62441babaa.tar.gz
p11tool: If there is only a single token available, don't bother complaining about specifying the correct URL
Diffstat (limited to 'src')
-rw-r--r--src/p11tool.c2
-rw-r--r--src/p11tool.h2
-rw-r--r--src/pkcs11.c58
3 files changed, 48 insertions, 14 deletions
diff --git a/src/p11tool.c b/src/p11tool.c
index 898092dd05..72e8888df9 100644
--- a/src/p11tool.c
+++ b/src/p11tool.c
@@ -202,7 +202,7 @@ static void cmd_parser(int argc, char **argv)
/* handle actions
*/
if (HAVE_OPT(LIST_TOKENS))
- pkcs11_token_list(outfile, detailed_url, &cinfo);
+ pkcs11_token_list(outfile, detailed_url, &cinfo, 0);
else if (HAVE_OPT(LIST_MECHANISMS))
pkcs11_mechanism_list(outfile, url, login, &cinfo);
else if (HAVE_OPT(GENERATE_RANDOM))
diff --git a/src/p11tool.h b/src/p11tool.h
index 2fe8b1f28e..c9e86e7647 100644
--- a/src/p11tool.h
+++ b/src/p11tool.h
@@ -39,7 +39,7 @@ pkcs11_export_chain(FILE * outfile, const char *url, unsigned int login,
common_info_st * info);
void pkcs11_token_list(FILE * outfile, unsigned int detailed,
- common_info_st *);
+ common_info_st *, unsigned brief);
void pkcs11_write(FILE * outfile, const char *pkcs11_url,
const char *label, int trusted,
int ca, int private,
diff --git a/src/pkcs11.c b/src/pkcs11.c
index c7fa1d106a..790567f4d7 100644
--- a/src/pkcs11.c
+++ b/src/pkcs11.c
@@ -34,10 +34,17 @@
#include <stdint.h>
#include <common.h>
-#define FIX(url) \
+static
+char *get_single_token_url(common_info_st * info);
+
+#define FIX(url, out, det, info) \
if (url == NULL) { \
- fprintf(stderr, "warning: no token URL was provided for this operation;\nuse --list-tokens for the available ones.\n"); \
- exit(1); \
+ url = get_single_token_url(info); \
+ if (url == NULL) { \
+ fprintf(stderr, "warning: no token URL was provided for this operation; the available tokens are:\n"); \
+ pkcs11_token_list(out, det, info, 1); \
+ exit(1); \
+ } \
}
#define CHECK_LOGIN_FLAG(flag) \
@@ -95,7 +102,7 @@ pkcs11_list(FILE * outfile, const char *url, int type, unsigned int login_flags,
pkcs11_common();
- FIX(url);
+ FIX(url, outfile, detailed, info);
if (type == PKCS11_TYPE_TRUSTED) {
attrs = GNUTLS_PKCS11_OBJ_ATTR_CRT_TRUSTED;
@@ -185,7 +192,7 @@ pkcs11_export(FILE * outfile, const char *url, unsigned int login_flags,
pkcs11_common();
- FIX(url);
+ FIX(url, outfile, 0, info);
ret = gnutls_pkcs11_obj_init(&obj);
if (ret < 0) {
@@ -233,7 +240,7 @@ pkcs11_export_chain(FILE * outfile, const char *url, unsigned int login_flags,
pkcs11_common();
- FIX(url);
+ FIX(url, outfile, 0, info);
ret = gnutls_pkcs11_obj_init(&obj);
if (ret < 0) {
@@ -318,9 +325,33 @@ pkcs11_export_chain(FILE * outfile, const char *url, unsigned int login_flags,
return;
}
+/* If there is a single token only present, return its URL.
+ */
+static
+char *get_single_token_url(common_info_st * info)
+{
+ int ret;
+ char *url = NULL, *t = NULL;
+
+ pkcs11_common();
+
+ ret = gnutls_pkcs11_token_get_url(0, 0, &url);
+ if (ret < 0)
+ return NULL;
+
+ ret = gnutls_pkcs11_token_get_url(1, 0, &t);
+ if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
+ gnutls_free(url);
+ gnutls_free(t);
+ return NULL;
+ }
+
+ return url;
+}
+
void
pkcs11_token_list(FILE * outfile, unsigned int detailed,
- common_info_st * info)
+ common_info_st * info, unsigned brief)
{
int ret;
int i;
@@ -343,6 +374,9 @@ pkcs11_token_list(FILE * outfile, unsigned int detailed,
fprintf(outfile, "Token %d:\n\tURL: %s\n", i, url);
+ if (brief != 0)
+ goto cont;
+
size = sizeof(buf);
ret =
gnutls_pkcs11_token_get_info(url,
@@ -395,7 +429,7 @@ pkcs11_token_list(FILE * outfile, unsigned int detailed,
fprintf(outfile, "\tSerial: %s\n", buf);
fprintf(outfile, "\n\n");
-
+ cont:
gnutls_free(url);
}
@@ -419,7 +453,7 @@ pkcs11_write(FILE * outfile, const char *url, const char *label,
pkcs11_common();
- FIX(url);
+ FIX(url, outfile, 0, info);
CHECK_LOGIN_FLAG(login_flags);
secret_key = load_secret_key(0, info);
@@ -500,7 +534,7 @@ pkcs11_generate(FILE * outfile, const char *url, gnutls_pk_algorithm_t pk,
pkcs11_common();
- FIX(url);
+ FIX(url, outfile, detailed, info);
CHECK_LOGIN_FLAG(login_flags);
if (outfile == stderr || outfile == stdout) {
@@ -813,7 +847,7 @@ pkcs11_mechanism_list(FILE * outfile, const char *url, unsigned int login_flags,
pkcs11_common();
- FIX(url);
+ FIX(url, outfile, 0, info);
idx = 0;
do {
@@ -846,7 +880,7 @@ pkcs11_get_random(FILE * outfile, const char *url, unsigned bytes,
pkcs11_common();
- FIX(url);
+ FIX(url, outfile, 0, info);
output = malloc(bytes);
if (output == NULL) {