If a callback fails try the other.
[gnutls.git] / src / p11tool.c
blobce3bebb0a57edb5f2fa4afa3f1db8dc922019926
1 /*
2 * Copyright (C) 2010, 2011 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * GnuTLS is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
23 #include <config.h>
25 #include <gnutls/gnutls.h>
26 #include <gnutls/x509.h>
27 #include <gnutls/openpgp.h>
28 #include <gnutls/pkcs12.h>
29 #include <gnutls/pkcs11.h>
30 #include <gnutls/abstract.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <error.h>
44 /* Gnulib portability files. */
45 #include <read-file.h>
46 #include <progname.h>
47 #include <version-etc.h>
49 #include "p11tool-gaa.h"
50 #include "p11tool.h"
51 #include "certtool-common.h"
53 static void gaa_parser (int argc, char **argv);
55 static gaainfo info;
56 static FILE *outfile;
57 int batch = 0;
59 static void
60 tls_log_func (int level, const char *str)
62 fprintf (stderr, "|<%d>| %s", level, str);
66 int
67 main (int argc, char **argv)
69 set_program_name (argv[0]);
70 gaa_parser (argc, argv);
72 return 0;
75 static void
76 gaa_parser (int argc, char **argv)
78 int ret;
79 common_info_st cinfo;
81 if (gaa (argc, argv, &info) != -1)
83 fprintf (stderr, "Try `%s --help' for more information.\n",
84 program_name);
85 exit (1);
88 gnutls_global_set_log_function (tls_log_func);
89 gnutls_global_set_log_level (info.debug);
90 if (info.debug > 1)
91 printf ("Setting log level to %d\n", info.debug);
93 if ((ret = gnutls_global_init ()) < 0)
94 error (EXIT_FAILURE, 0, "global_init: %s", gnutls_strerror (ret));
96 if (info.pkcs11_provider != NULL)
98 ret = gnutls_pkcs11_init (GNUTLS_PKCS11_FLAG_MANUAL, NULL);
99 if (ret < 0)
100 fprintf (stderr, "pkcs11_init: %s", gnutls_strerror (ret));
101 else
103 ret = gnutls_pkcs11_add_provider (info.pkcs11_provider, NULL);
104 if (ret < 0)
105 error (EXIT_FAILURE, 0, "pkcs11_add_provider: %s",
106 gnutls_strerror (ret));
109 else
111 ret = gnutls_pkcs11_init (GNUTLS_PKCS11_FLAG_AUTO, NULL);
112 if (ret < 0)
113 fprintf (stderr, "pkcs11_init: %s", gnutls_strerror (ret));
116 if (info.outfile)
118 outfile = safe_open_rw (info.outfile, 0);
119 if (outfile == NULL)
120 error (EXIT_FAILURE, errno, "%s", info.outfile);
122 else
123 outfile = stdout;
125 memset (&cinfo, 0, sizeof (cinfo));
126 cinfo.secret_key = info.secret_key;
127 cinfo.privkey = info.privkey;
128 cinfo.pkcs8 = info.pkcs8;
129 cinfo.incert_format = info.incert_format;
130 cinfo.cert = info.cert;
132 switch (info.action)
134 case ACTION_PKCS11_LIST:
135 pkcs11_list (outfile, info.pkcs11_url, info.pkcs11_type,
136 info.pkcs11_login, info.pkcs11_detailed_url, &cinfo);
137 break;
138 case ACTION_PKCS11_TOKENS:
139 pkcs11_token_list (outfile, info.pkcs11_detailed_url, &cinfo);
140 break;
141 case ACTION_PKCS11_MECHANISMS:
142 pkcs11_mechanism_list (outfile, info.pkcs11_url, info.pkcs11_login,
143 &cinfo);
144 break;
145 case ACTION_PKCS11_EXPORT_URL:
146 pkcs11_export (outfile, info.pkcs11_url, info.pkcs11_login, &cinfo);
147 break;
148 case ACTION_PKCS11_WRITE_URL:
149 pkcs11_write (outfile, info.pkcs11_url, info.pkcs11_label,
150 info.pkcs11_trusted, info.pkcs11_login, &cinfo);
151 break;
152 case ACTION_PKCS11_TOKEN_INIT:
153 pkcs11_init (outfile, info.pkcs11_url, info.pkcs11_label, &cinfo);
154 break;
155 case ACTION_PKCS11_DELETE_URL:
156 pkcs11_delete (outfile, info.pkcs11_url, 0, info.pkcs11_login, &cinfo);
157 break;
158 default:
159 gaa_help ();
160 exit (0);
162 fclose (outfile);
164 #ifdef ENABLE_PKCS11
165 gnutls_pkcs11_deinit ();
166 #endif
167 gnutls_global_deinit ();