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
);
58 static gnutls_x509_crt_fmt_t incert_format
, outcert_format
;
59 static gnutls_tpmkey_fmt_t inkey_format
, outkey_format
;
66 tls_log_func (int level
, const char *str
)
68 fprintf (stderr
, "|<%d>| %s", level
, str
);
73 main (int argc
, char **argv
)
75 set_program_name (argv
[0]);
76 cmd_parser (argc
, argv
);
82 cmd_parser (int argc
, char **argv
)
86 unsigned int key_type
= GNUTLS_PK_UNKNOWN
;
87 unsigned int bits
= 0;
88 unsigned int genflags
= 0;
89 /* Note that the default sec-param is legacy because several TPMs
90 * cannot handle larger keys.
92 const char* sec_param
= "legacy";
94 optct
= optionProcess( &tpmtoolOptions
, argc
, argv
);
99 debug
= OPT_VALUE_DEBUG
;
103 incert_format
= GNUTLS_X509_FMT_DER
;
104 inkey_format
= GNUTLS_TPMKEY_FMT_DER
;
108 incert_format
= GNUTLS_X509_FMT_PEM
;
109 inkey_format
= GNUTLS_TPMKEY_FMT_CTK_PEM
;
112 if (HAVE_OPT(OUTDER
))
114 outcert_format
= GNUTLS_X509_FMT_DER
;
115 outkey_format
= GNUTLS_TPMKEY_FMT_DER
;
119 outcert_format
= GNUTLS_X509_FMT_PEM
;
120 outkey_format
= GNUTLS_TPMKEY_FMT_CTK_PEM
;
123 if (HAVE_OPT(REGISTER
))
124 genflags
|= GNUTLS_TPM_REGISTER_KEY
;
125 if (!HAVE_OPT(LEGACY
))
126 genflags
|= GNUTLS_TPM_KEY_SIGNING
;
128 genflags
|= GNUTLS_TPM_KEY_USER
;
130 gnutls_global_set_log_function (tls_log_func
);
131 gnutls_global_set_log_level (debug
);
133 printf ("Setting log level to %d\n", debug
);
135 if ((ret
= gnutls_global_init ()) < 0)
136 error (EXIT_FAILURE
, 0, "global_init: %s", gnutls_strerror (ret
));
138 if (HAVE_OPT(OUTFILE
))
140 outfile
= safe_open_rw (OPT_ARG(OUTFILE
), 0);
142 error (EXIT_FAILURE
, errno
, "%s", OPT_ARG(OUTFILE
));
147 if (HAVE_OPT(INFILE
))
149 infile
= fopen (OPT_ARG(INFILE
), "rb");
151 error (EXIT_FAILURE
, errno
, "%s", OPT_ARG(INFILE
));
156 if (HAVE_OPT(SEC_PARAM
))
157 sec_param
= OPT_ARG(SEC_PARAM
);
159 bits
= OPT_VALUE_BITS
;
162 if (HAVE_OPT(GENERATE_RSA
))
164 key_type
= GNUTLS_PK_RSA
;
165 bits
= get_bits (key_type
, bits
, sec_param
);
166 tpm_generate (outfile
, key_type
, bits
, genflags
);
168 else if (HAVE_OPT(PUBKEY
))
170 tpm_pubkey (OPT_ARG(PUBKEY
), outfile
);
172 else if (HAVE_OPT(DELETE
))
174 tpm_delete (OPT_ARG(DELETE
), outfile
);
176 else if (HAVE_OPT(LIST
))
187 gnutls_global_deinit ();
190 static void tpm_generate(FILE* outfile
, unsigned int key_type
, unsigned int bits
, unsigned int flags
)
193 char* srk_pass
, *key_pass
= NULL
;
194 gnutls_datum_t privkey
, pubkey
;
196 srk_pass
= getpass ("Enter SRK password: ");
197 if (srk_pass
!= NULL
)
198 srk_pass
= strdup(srk_pass
);
200 if (!(flags
& GNUTLS_TPM_REGISTER_KEY
))
202 key_pass
= getpass ("Enter key password: ");
203 if (key_pass
!= NULL
)
204 key_pass
= strdup(srk_pass
);
207 ret
= gnutls_tpm_privkey_generate(key_type
, bits
, srk_pass
, key_pass
,
208 outkey_format
, outcert_format
,
216 error (EXIT_FAILURE
, 0, "gnutls_tpm_privkey_generate: %s", gnutls_strerror (ret
));
218 /* fwrite (pubkey.data, 1, pubkey.size, outfile);
219 fputs ("\n", outfile);*/
220 fwrite (privkey
.data
, 1, privkey
.size
, outfile
);
221 fputs ("\n", outfile
);
223 gnutls_free(privkey
.data
);
224 gnutls_free(pubkey
.data
);
227 static void tpm_delete(const char* url
, FILE* outfile
)
232 srk_pass
= getpass ("Enter SRK password: ");
234 ret
= gnutls_tpm_privkey_delete(url
, srk_pass
);
236 error (EXIT_FAILURE
, 0, "gnutls_tpm_privkey_delete: %s", gnutls_strerror (ret
));
238 fprintf (outfile
, "Key %s deleted\n", url
);
241 static void tpm_list(FILE* outfile
)
244 gnutls_tpm_key_list_t list
;
248 ret
= gnutls_tpm_get_registered (&list
);
250 error (EXIT_FAILURE
, 0, "gnutls_tpm_get_registered: %s", gnutls_strerror (ret
));
252 fprintf(outfile
, "Available keys:\n");
255 ret
= gnutls_tpm_key_list_get_url(list
, i
, &url
, 0);
256 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
259 error (EXIT_FAILURE
, 0, "gnutls_tpm_key_list_get_url: %s", gnutls_strerror (ret
));
261 fprintf(outfile
, "\t%u: %s\n", i
, url
);
265 fputs ("\n", outfile
);
268 static void tpm_pubkey(const char* url
, FILE* outfile
)
272 gnutls_pubkey_t pubkey
;
274 srk_pass
= getpass ("Enter SRK password: ");
275 if (srk_pass
!= NULL
)
276 srk_pass
= strdup(srk_pass
);
278 gnutls_pubkey_init(&pubkey
);
280 ret
= gnutls_pubkey_import_tpm_url(pubkey
, url
, srk_pass
, 0);
285 error (EXIT_FAILURE
, 0, "gnutls_pubkey_import_tpm_url: %s", gnutls_strerror (ret
));
287 _pubkey_info(outfile
, pubkey
);
289 gnutls_pubkey_deinit(pubkey
);