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/>.
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>
39 #include <sys/types.h>
44 /* Gnulib portability files. */
45 #include <read-file.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
);
63 tls_log_func (int level
, const char *str
)
65 fprintf (stderr
, "|<%d>| %s", level
, str
);
70 main (int argc
, char **argv
)
72 set_program_name (argv
[0]);
73 cmd_parser (argc
, argv
);
79 cmd_parser (int argc
, char **argv
)
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
);
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
;
103 genflags
|= GNUTLS_TPM_KEY_USER
;
105 gnutls_global_set_log_function (tls_log_func
);
106 gnutls_global_set_log_level (debug
);
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);
117 error (EXIT_FAILURE
, errno
, "%s", OPT_ARG(OUTFILE
));
122 if (HAVE_OPT(INFILE
))
124 infile
= fopen (OPT_ARG(INFILE
), "rb");
126 error (EXIT_FAILURE
, errno
, "%s", OPT_ARG(INFILE
));
131 if (HAVE_OPT(SEC_PARAM
))
132 sec_param
= OPT_ARG(SEC_PARAM
);
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
))
162 gnutls_global_deinit ();
165 static void tpm_generate(FILE* outfile
, unsigned int key_type
, unsigned int bits
, unsigned int flags
)
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
,
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
)
203 srk_pass
= getpass ("Enter SRK password: ");
205 ret
= gnutls_tpm_privkey_delete(url
, srk_pass
);
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
)
215 gnutls_tpm_key_list_t list
;
219 ret
= gnutls_tpm_get_registered (&list
);
221 error (EXIT_FAILURE
, 0, "gnutls_tpm_get_registered: %s", gnutls_strerror (ret
));
223 fprintf(outfile
, "Available keys:\n");
226 ret
= gnutls_tpm_key_list_get_url(list
, i
, &url
, 0);
227 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
230 error (EXIT_FAILURE
, 0, "gnutls_tpm_key_list_get_url: %s", gnutls_strerror (ret
));
232 fprintf(outfile
, "\t%u: %s\n", i
, url
);
236 fputs ("\n", outfile
);
239 static void tpm_pubkey(const char* url
, FILE* outfile
)
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);
256 error (EXIT_FAILURE
, 0, "gnutls_pubkey_import_tpm_url: %s", gnutls_strerror (ret
));
258 _pubkey_info(outfile
, pubkey
);
260 gnutls_pubkey_deinit(pubkey
);