Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / src / tpmtool.c
1 /*
2  * Copyright (C) 2010-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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/>.
21  */
22
23 #include <config.h>
24
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>
31
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
43 /* Gnulib portability files. */
44 #include <read-file.h>
45
46 #include "certtool-common.h"
47 #include "tpmtool-args.h"
48 #include "common.h"
49
50 static void cmd_parser(int argc, char **argv);
51 static void tpm_generate(FILE * outfile, unsigned int key_type,
52                          unsigned int bits, unsigned int flags);
53 static void tpm_pubkey(const char *url, FILE * outfile);
54 static void tpm_delete(const char *url, FILE * outfile);
55 static void tpm_test_sign(const char *url, FILE * outfile);
56 static void tpm_list(FILE * outfile);
57
58 static gnutls_x509_crt_fmt_t incert_format, outcert_format;
59 static gnutls_tpmkey_fmt_t inkey_format, outkey_format;
60
61 static FILE *outfile;
62 static FILE *infile;
63 int batch = 0;
64 int ask_pass = 0;
65
66 static void tls_log_func(int level, const char *str)
67 {
68         fprintf(stderr, "|<%d>| %s", level, str);
69 }
70
71
72 int main(int argc, char **argv)
73 {
74         cmd_parser(argc, argv);
75
76         return 0;
77 }
78
79 static void cmd_parser(int argc, char **argv)
80 {
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.
88          */
89         const char *sec_param = "legacy";
90
91         optct = optionProcess(&tpmtoolOptions, argc, argv);
92         argc += optct;
93         argv += optct;
94
95         if (HAVE_OPT(DEBUG))
96                 debug = OPT_VALUE_DEBUG;
97
98         if (HAVE_OPT(INDER)) {
99                 incert_format = GNUTLS_X509_FMT_DER;
100                 inkey_format = GNUTLS_TPMKEY_FMT_DER;
101         } else {
102                 incert_format = GNUTLS_X509_FMT_PEM;
103                 inkey_format = GNUTLS_TPMKEY_FMT_CTK_PEM;
104         }
105
106         if (HAVE_OPT(OUTDER)) {
107                 outcert_format = GNUTLS_X509_FMT_DER;
108                 outkey_format = GNUTLS_TPMKEY_FMT_DER;
109         } else {
110                 outcert_format = GNUTLS_X509_FMT_PEM;
111                 outkey_format = GNUTLS_TPMKEY_FMT_CTK_PEM;
112         }
113
114         if (HAVE_OPT(REGISTER))
115                 genflags |= GNUTLS_TPM_REGISTER_KEY;
116         if (!HAVE_OPT(LEGACY))
117                 genflags |= GNUTLS_TPM_KEY_SIGNING;
118         if (HAVE_OPT(USER))
119                 genflags |= GNUTLS_TPM_KEY_USER;
120
121         gnutls_global_set_log_function(tls_log_func);
122         gnutls_global_set_log_level(debug);
123         if (debug > 1)
124                 printf("Setting log level to %d\n", debug);
125
126         if ((ret = gnutls_global_init()) < 0) {
127                 fprintf(stderr, "global_init: %s\n", gnutls_strerror(ret));
128                 exit(1);
129         }
130
131         if (HAVE_OPT(OUTFILE)) {
132                 outfile = safe_open_rw(OPT_ARG(OUTFILE), 0);
133                 if (outfile == NULL) {
134                         fprintf(stderr, "%s\n", OPT_ARG(OUTFILE));
135                         exit(1);
136                 }
137         } else
138                 outfile = stdout;
139
140         if (HAVE_OPT(INFILE)) {
141                 infile = fopen(OPT_ARG(INFILE), "rb");
142                 if (infile == NULL) {
143                         fprintf(stderr, "%s\n", OPT_ARG(INFILE));
144                         exit(1);
145                 }
146         } else
147                 infile = stdin;
148
149         if (HAVE_OPT(SEC_PARAM))
150                 sec_param = OPT_ARG(SEC_PARAM);
151         if (HAVE_OPT(BITS))
152                 bits = OPT_VALUE_BITS;
153
154
155         if (HAVE_OPT(GENERATE_RSA)) {
156                 key_type = GNUTLS_PK_RSA;
157                 bits = get_bits(key_type, bits, sec_param, 0);
158                 tpm_generate(outfile, key_type, bits, genflags);
159         } else if (HAVE_OPT(PUBKEY)) {
160                 tpm_pubkey(OPT_ARG(PUBKEY), outfile);
161         } else if (HAVE_OPT(DELETE)) {
162                 tpm_delete(OPT_ARG(DELETE), outfile);
163         } else if (HAVE_OPT(LIST)) {
164                 tpm_list(outfile);
165         } else if (HAVE_OPT(TEST_SIGN)) {
166                 tpm_test_sign(OPT_ARG(TEST_SIGN), outfile);
167         } else {
168                 USAGE(1);
169         }
170
171         fclose(outfile);
172
173         gnutls_global_deinit();
174 }
175
176 #define TEST_DATA "Test data to sign"
177
178 static void
179 tpm_test_sign(const char *url, FILE * out)
180 {
181         gnutls_privkey_t privkey;
182         gnutls_pubkey_t pubkey;
183         int ret;
184         gnutls_datum_t data, sig = {NULL, 0};
185         int pk;
186
187         pkcs11_common(NULL);
188
189         data.data = (void*)TEST_DATA;
190         data.size = sizeof(TEST_DATA)-1;
191
192         ret = gnutls_privkey_init(&privkey);
193         if (ret < 0) {
194                 fprintf(stderr, "Error in %s:%d: %s\n", __func__,
195                         __LINE__, gnutls_strerror(ret));
196                 exit(1);
197         }
198
199         ret = gnutls_pubkey_init(&pubkey);
200         if (ret < 0) {
201                 fprintf(stderr, "Error in %s:%d: %s\n", __func__,
202                         __LINE__, gnutls_strerror(ret));
203                 exit(1);
204         }
205
206         ret = gnutls_privkey_import_url(privkey, url, 0);
207         if (ret < 0) {
208                 fprintf(stderr, "Cannot import private key: %s\n",
209                         gnutls_strerror(ret));
210                 exit(1);
211         }
212
213         ret = gnutls_pubkey_import_tpm_url(pubkey, url, NULL, 0);
214         if (ret < 0) {
215                 fprintf(stderr, "Cannot import public key: %s\n",
216                         gnutls_strerror(ret));
217                 exit(1);
218         }
219
220         ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA1, 0, &data, &sig);
221         if (ret < 0) {
222                 fprintf(stderr, "Cannot sign data: %s\n",
223                         gnutls_strerror(ret));
224                 exit(1);
225         }
226
227         pk = gnutls_pubkey_get_pk_algorithm(pubkey, NULL);
228
229         fprintf(stderr, "Verifying against private key parameters... ");
230         ret = gnutls_pubkey_verify_data2(pubkey, gnutls_pk_to_sign(pk, GNUTLS_DIG_SHA1),
231                 0, &data, &sig);
232         if (ret < 0) {
233                 fprintf(stderr, "Cannot verify signed data: %s\n",
234                         gnutls_strerror(ret));
235                 exit(1);
236         }
237
238         fprintf(stderr, "ok\n");
239
240         gnutls_free(sig.data);
241         gnutls_pubkey_deinit(pubkey);
242         gnutls_privkey_deinit(privkey);
243 }
244
245 static void tpm_generate(FILE * out, unsigned int key_type,
246                          unsigned int bits, unsigned int flags)
247 {
248         int ret;
249         char *srk_pass, *key_pass = NULL;
250         gnutls_datum_t privkey, pubkey;
251
252         srk_pass = getpass("Enter SRK password: ");
253         if (srk_pass != NULL)
254                 srk_pass = strdup(srk_pass);
255
256         if (!(flags & GNUTLS_TPM_REGISTER_KEY)) {
257                 key_pass = getpass("Enter key password: ");
258                 if (key_pass != NULL)
259                         key_pass = strdup(key_pass);
260         }
261
262         ret =
263             gnutls_tpm_privkey_generate(key_type, bits, srk_pass, key_pass,
264                                         outkey_format, outcert_format,
265                                         &privkey, &pubkey, flags);
266
267         free(key_pass);
268         free(srk_pass);
269
270         if (ret < 0) {
271                 fprintf(stderr, "gnutls_tpm_privkey_generate: %s\n",
272                         gnutls_strerror(ret));
273                 exit(1);
274         }
275
276
277         fwrite(privkey.data, 1, privkey.size, out);
278         fputs("\n", out);
279
280         gnutls_free(privkey.data);
281         gnutls_free(pubkey.data);
282 }
283
284 static void tpm_delete(const char *url, FILE * out)
285 {
286         int ret;
287         char *srk_pass;
288
289         srk_pass = getpass("Enter SRK password: ");
290
291         ret = gnutls_tpm_privkey_delete(url, srk_pass);
292         if (ret < 0) {
293                 fprintf(stderr, "gnutls_tpm_privkey_delete: %s\n",
294                         gnutls_strerror(ret));
295                 exit(1);
296         }
297
298         fprintf(out, "Key %s deleted\n", url);
299 }
300
301 static void tpm_list(FILE * out)
302 {
303         int ret;
304         gnutls_tpm_key_list_t list;
305         unsigned int i;
306         char *url;
307
308         ret = gnutls_tpm_get_registered(&list);
309         if (ret < 0) {
310                 fprintf(stderr, "gnutls_tpm_get_registered: %s\n",
311                         gnutls_strerror(ret));
312                 exit(1);
313         }
314
315         fprintf(out, "Available keys:\n");
316         for (i = 0;; i++) {
317                 ret = gnutls_tpm_key_list_get_url(list, i, &url, 0);
318                 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
319                         break;
320                 else if (ret < 0) {
321                         fprintf(stderr, "gnutls_tpm_key_list_get_url: %s\n",
322                                 gnutls_strerror(ret));
323                         exit(1);
324                 }
325
326                 fprintf(out, "\t%u: %s\n", i, url);
327                 gnutls_free(url);
328         }
329
330         fputs("\n", out);
331 }
332
333 static void tpm_pubkey(const char *url, FILE * out)
334 {
335         int ret;
336         char *srk_pass;
337         gnutls_pubkey_t pubkey;
338
339         srk_pass = getpass("Enter SRK password: ");
340         if (srk_pass != NULL)
341                 srk_pass = strdup(srk_pass);
342
343         gnutls_pubkey_init(&pubkey);
344
345         ret = gnutls_pubkey_import_tpm_url(pubkey, url, srk_pass, 0);
346
347         free(srk_pass);
348
349         if (ret < 0) {
350                 fprintf(stderr, "gnutls_pubkey_import_tpm_url: %s\n",
351                         gnutls_strerror(ret));
352                 exit(1);
353         }
354
355         _pubkey_info(out, GNUTLS_CRT_PRINT_FULL, pubkey);
356
357         gnutls_pubkey_deinit(pubkey);
358 }