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/>.
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>
39 #include <sys/types.h>
44 /* Gnulib portability files. */
45 #include <read-file.h>
47 #include <version-etc.h>
49 #include "p11tool-gaa.h"
51 #include "certtool-common.h"
53 static void gaa_parser (int argc, char **argv);
60 tls_log_func (int level, const char *str)
62 fprintf (stderr, "|<%d>| %s", level, str);
67 main (int argc, char **argv)
69 set_program_name (argv[0]);
70 gaa_parser (argc, argv);
76 gaa_parser (int argc, char **argv)
81 if (gaa (argc, argv, &info) != -1)
83 fprintf (stderr, "Try `%s --help' for more information.\n",
88 gnutls_global_set_log_function (tls_log_func);
89 gnutls_global_set_log_level (info.debug);
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);
100 fprintf (stderr, "pkcs11_init: %s", gnutls_strerror (ret));
103 ret = gnutls_pkcs11_add_provider (info.pkcs11_provider, NULL);
105 error (EXIT_FAILURE, 0, "pkcs11_add_provider: %s",
106 gnutls_strerror (ret));
111 ret = gnutls_pkcs11_init (GNUTLS_PKCS11_FLAG_AUTO, NULL);
113 fprintf (stderr, "pkcs11_init: %s", gnutls_strerror (ret));
118 outfile = safe_open_rw (info.outfile, 0);
120 error (EXIT_FAILURE, errno, "%s", info.outfile);
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;
134 case ACTION_PKCS11_LIST:
135 pkcs11_list (outfile, info.pkcs11_url, info.pkcs11_type,
136 info.pkcs11_login, info.pkcs11_detailed_url, &cinfo);
138 case ACTION_PKCS11_TOKENS:
139 pkcs11_token_list (outfile, info.pkcs11_detailed_url, &cinfo);
141 case ACTION_PKCS11_MECHANISMS:
142 pkcs11_mechanism_list (outfile, info.pkcs11_url, info.pkcs11_login,
145 case ACTION_PKCS11_EXPORT_URL:
146 pkcs11_export (outfile, info.pkcs11_url, info.pkcs11_login, &cinfo);
148 case ACTION_PKCS11_WRITE_URL:
149 pkcs11_write (outfile, info.pkcs11_url, info.pkcs11_label,
150 info.pkcs11_trusted, info.pkcs11_login, &cinfo);
152 case ACTION_PKCS11_TOKEN_INIT:
153 pkcs11_init (outfile, info.pkcs11_url, info.pkcs11_label, &cinfo);
155 case ACTION_PKCS11_DELETE_URL:
156 pkcs11_delete (outfile, info.pkcs11_url, 0, info.pkcs11_login, &cinfo);
165 gnutls_pkcs11_deinit ();
167 gnutls_global_deinit ();