set PIN function when reading a certificate
[gnutls.git] / src / tpmtool.c
blobedd219a056c0f5525acdccbc00d77d576193b2bc
1 /*
2 * Copyright (C) 2010-2012 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/tpm.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 "certtool-common.h"
50 #include "tpmtool-args.h"
52 static void cmd_parser (int argc, char **argv);
53 static void tpm_generate(FILE* outfile, unsigned int key_type, unsigned int bits, unsigned int flags);
54 static void tpm_pubkey(const char* url, FILE* outfile);
55 static void tpm_delete(const char* url, FILE* outfile);
56 static void tpm_list(FILE* outfile);
58 static FILE *outfile;
59 static FILE *infile;
60 int batch = 0;
62 static void
63 tls_log_func (int level, const char *str)
65 fprintf (stderr, "|<%d>| %s", level, str);
69 int
70 main (int argc, char **argv)
72 set_program_name (argv[0]);
73 cmd_parser (argc, argv);
75 return 0;
78 static void
79 cmd_parser (int argc, char **argv)
81 int ret, debug = 0;
82 unsigned int optct;
83 unsigned int key_type = GNUTLS_PK_UNKNOWN;
84 unsigned int bits = 0;
85 unsigned int genflags = 0;
86 /* Note that the default sec-param is legacy because several TPMs
87 * cannot handle larger keys.
89 const char* sec_param = "legacy";
91 optct = optionProcess( &tpmtoolOptions, argc, argv);
92 argc += optct;
93 argv += optct;
95 if (HAVE_OPT(DEBUG))
96 debug = OPT_VALUE_DEBUG;
98 if (HAVE_OPT(REGISTER))
99 genflags |= GNUTLS_TPM_REGISTER_KEY;
100 if (!HAVE_OPT(LEGACY))
101 genflags |= GNUTLS_TPM_KEY_SIGNING;
102 if (HAVE_OPT(USER))
103 genflags |= GNUTLS_TPM_KEY_USER;
105 gnutls_global_set_log_function (tls_log_func);
106 gnutls_global_set_log_level (debug);
107 if (debug > 1)
108 printf ("Setting log level to %d\n", debug);
110 if ((ret = gnutls_global_init ()) < 0)
111 error (EXIT_FAILURE, 0, "global_init: %s", gnutls_strerror (ret));
113 if (HAVE_OPT(OUTFILE))
115 outfile = safe_open_rw (OPT_ARG(OUTFILE), 0);
116 if (outfile == NULL)
117 error (EXIT_FAILURE, errno, "%s", OPT_ARG(OUTFILE));
119 else
120 outfile = stdout;
122 if (HAVE_OPT(INFILE))
124 infile = fopen (OPT_ARG(INFILE), "rb");
125 if (infile == NULL)
126 error (EXIT_FAILURE, errno, "%s", OPT_ARG(INFILE));
128 else
129 infile = stdin;
131 if (HAVE_OPT(SEC_PARAM))
132 sec_param = OPT_ARG(SEC_PARAM);
133 if (HAVE_OPT(BITS))
134 bits = OPT_VALUE_BITS;
137 if (HAVE_OPT(GENERATE_RSA))
139 key_type = GNUTLS_PK_RSA;
140 bits = get_bits (key_type, bits, sec_param);
141 tpm_generate (outfile, key_type, bits, genflags);
143 else if (HAVE_OPT(PUBKEY))
145 tpm_pubkey (OPT_ARG(PUBKEY), outfile);
147 else if (HAVE_OPT(DELETE))
149 tpm_delete (OPT_ARG(DELETE), outfile);
151 else if (HAVE_OPT(LIST))
153 tpm_list (outfile);
155 else
157 USAGE(1);
160 fclose (outfile);
162 gnutls_global_deinit ();
165 static void tpm_generate(FILE* outfile, unsigned int key_type, unsigned int bits, unsigned int flags)
167 int ret;
168 char* srk_pass, *key_pass;
169 gnutls_datum_t privkey, pubkey;
171 srk_pass = getpass ("Enter SRK password: ");
172 if (srk_pass != NULL)
173 srk_pass = strdup(srk_pass);
175 key_pass = getpass ("Enter key password: ");
176 if (key_pass != NULL)
177 key_pass = strdup(srk_pass);
179 ret = gnutls_tpm_privkey_generate(key_type, bits, srk_pass, key_pass,
180 GNUTLS_X509_FMT_PEM, &privkey, &pubkey,
181 flags);
183 free(key_pass);
184 free(srk_pass);
186 if (ret < 0)
187 error (EXIT_FAILURE, 0, "gnutls_tpm_privkey_generate: %s", gnutls_strerror (ret));
189 /* fwrite (pubkey.data, 1, pubkey.size, outfile);
190 fputs ("\n", outfile);*/
191 fwrite (privkey.data, 1, privkey.size, outfile);
192 fputs ("\n", outfile);
194 gnutls_free(privkey.data);
195 gnutls_free(pubkey.data);
198 static void tpm_delete(const char* url, FILE* outfile)
200 int ret;
201 char* srk_pass;
203 srk_pass = getpass ("Enter SRK password: ");
205 ret = gnutls_tpm_privkey_delete(url, srk_pass);
206 if (ret < 0)
207 error (EXIT_FAILURE, 0, "gnutls_tpm_privkey_delete: %s", gnutls_strerror (ret));
209 fprintf (outfile, "Key %s deleted\n", url);
212 static void tpm_list(FILE* outfile)
214 int ret;
215 gnutls_tpm_key_list_t list;
216 unsigned int i;
217 char* url;
219 ret = gnutls_tpm_get_registered (&list);
220 if (ret < 0)
221 error (EXIT_FAILURE, 0, "gnutls_tpm_get_registered: %s", gnutls_strerror (ret));
223 fprintf(outfile, "Available keys:\n");
224 for (i=0;;i++)
226 ret = gnutls_tpm_key_list_get_url(list, i, &url, 0);
227 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
228 break;
229 else if (ret < 0)
230 error (EXIT_FAILURE, 0, "gnutls_tpm_key_list_get_url: %s", gnutls_strerror (ret));
232 fprintf(outfile, "\t%u: %s\n", i, url);
233 gnutls_free(url);
236 fputs ("\n", outfile);
239 static void tpm_pubkey(const char* url, FILE* outfile)
241 int ret;
242 char* srk_pass;
243 gnutls_pubkey_t pubkey;
245 srk_pass = getpass ("Enter SRK password: ");
246 if (srk_pass != NULL)
247 srk_pass = strdup(srk_pass);
249 gnutls_pubkey_init(&pubkey);
251 ret = gnutls_pubkey_import_tpm_url(pubkey, url, srk_pass, 0);
253 free(srk_pass);
255 if (ret < 0)
256 error (EXIT_FAILURE, 0, "gnutls_pubkey_import_tpm_url: %s", gnutls_strerror (ret));
258 _pubkey_info(outfile, pubkey);
260 gnutls_pubkey_deinit(pubkey);