summaryrefslogtreecommitdiffstats
path: root/src/tpmtool-options.c
blob: 158b4912be4d0df1154367e3275320bd5fdba093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "tpmtool-options.h"
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif /* !_WIN32 */
#include <limits.h>

struct tpmtool_options tpmtool_options;

static long
parse_number (const char *arg)
{
  char *endptr = NULL;
  errno = 0;
  long result;

  if (strncmp (arg, "0x", 2) == 0)
    result = strtol (arg + 2, &endptr, 16);
  else if (strncmp (arg, "0", 1) == 0
           && strspn (arg, "012345678") == strlen (optarg))
    result = strtol (arg + 1, &endptr, 8);
  else
    result = strtol (arg, &endptr, 10);

  if (errno != 0 || (endptr && *endptr != '\0'))
    error (EXIT_FAILURE, errno, "'%s' is not a recognizable number.",
           arg);

  return result;
}

/* Long options.  */
static const struct option long_options[] =
{
  { "debug", required_argument, 0, 'd' },
  { "infile", required_argument, 0, CHAR_MAX + 1 },
  { "outfile", required_argument, 0, CHAR_MAX + 2 },
  { "generate-rsa", no_argument, 0, CHAR_MAX + 3 },
  { "register", no_argument, 0, CHAR_MAX + 4 },
  { "signing", no_argument, 0, CHAR_MAX + 5 },
  { "legacy", no_argument, 0, CHAR_MAX + 6 },
  { "user", no_argument, 0, CHAR_MAX + 7 },
  { "system", no_argument, 0, CHAR_MAX + 8 },
  { "pubkey", required_argument, 0, CHAR_MAX + 9 },
  { "list", no_argument, 0, CHAR_MAX + 10 },
  { "delete", required_argument, 0, CHAR_MAX + 11 },
  { "test-sign", required_argument, 0, CHAR_MAX + 12 },
  { "sec-param", required_argument, 0, CHAR_MAX + 13 },
  { "bits", required_argument, 0, CHAR_MAX + 14 },
  { "inder", no_argument, 0, CHAR_MAX + 15 },
  { "no-inder", no_argument, 0, CHAR_MAX + 16 },
  { "outder", no_argument, 0, CHAR_MAX + 17 },
  { "no-outder", no_argument, 0, CHAR_MAX + 18 },
  { "srk-well-known", no_argument, 0, CHAR_MAX + 19 },
  { "version", optional_argument, 0, 'v' },
  { "help", no_argument, 0, 'h' },
  { "more-help", no_argument, 0, '!' },
  { 0, 0, 0, 0 }

};

int
process_options (int argc, char **argv)
{
  struct tpmtool_options *opts = &tpmtool_options;
  int opt;


  while ((opt = getopt_long (argc, argv, "!d:hv:",
                             long_options, NULL)) != EOF)
    switch (opt)
      {
      case '\0': /* Long option.  */
        break;
      case 'd':
        opts->present.debug = true;
        opts->arg.debug = optarg;
        opts->value.debug = parse_number(optarg);
        opts->enabled.debug = true;
        break;
      case CHAR_MAX + 1: /* --infile */
        opts->present.infile = true;
        opts->arg.infile = optarg;
        opts->enabled.infile = true;
        break;
      case CHAR_MAX + 2: /* --outfile */
        opts->present.outfile = true;
        opts->arg.outfile = optarg;
        opts->enabled.outfile = true;
        break;
      case CHAR_MAX + 3: /* --generate-rsa */
        opts->present.generate_rsa = true;
        opts->enabled.generate_rsa = true;
        break;
      case CHAR_MAX + 4: /* --register */
        opts->present.register_ = true;
        opts->enabled.register_ = true;
        break;
      case CHAR_MAX + 5: /* --signing */
        opts->present.signing = true;
        opts->enabled.signing = true;
        break;
      case CHAR_MAX + 6: /* --legacy */
        opts->present.legacy = true;
        opts->enabled.legacy = true;
        break;
      case CHAR_MAX + 7: /* --user */
        opts->present.user = true;
        opts->enabled.user = true;
        break;
      case CHAR_MAX + 8: /* --system */
        opts->present.system = true;
        opts->enabled.system = true;
        break;
      case CHAR_MAX + 9: /* --pubkey */
        opts->present.pubkey = true;
        opts->arg.pubkey = optarg;
        opts->enabled.pubkey = true;
        break;
      case CHAR_MAX + 10: /* --list */
        opts->present.list = true;
        opts->enabled.list = true;
        break;
      case CHAR_MAX + 11: /* --delete */
        opts->present.delete = true;
        opts->arg.delete = optarg;
        opts->enabled.delete = true;
        break;
      case CHAR_MAX + 12: /* --test-sign */
        opts->present.test_sign = true;
        opts->arg.test_sign = optarg;
        opts->enabled.test_sign = true;
        break;
      case CHAR_MAX + 13: /* --sec-param */
        opts->present.sec_param = true;
        opts->arg.sec_param = optarg;
        opts->enabled.sec_param = true;
        break;
      case CHAR_MAX + 14: /* --bits */
        opts->present.bits = true;
        opts->arg.bits = optarg;
        opts->value.bits = parse_number(optarg);
        opts->enabled.bits = true;
        break;
      case CHAR_MAX + 15: /* --inder */
        opts->present.inder = true;
        opts->enabled.inder = true;
        break;
      case CHAR_MAX + 16: /* --no-inder */
        opts->present.inder = true;
        opts->enabled.inder = false;
        break;
      case CHAR_MAX + 17: /* --outder */
        opts->present.outder = true;
        opts->enabled.outder = true;
        break;
      case CHAR_MAX + 18: /* --no-outder */
        opts->present.outder = true;
        opts->enabled.outder = false;
        break;
      case CHAR_MAX + 19: /* --srk-well-known */
        opts->present.srk_well_known = true;
        opts->enabled.srk_well_known = true;
        break;
      case 'v':
        opts->present.version = true;
        opts->arg.version = optarg;
        opts->enabled.version = true;
        break;
      case 'h':
        opts->present.help = true;
        opts->enabled.help = true;
        break;
      case '!':
        opts->present.more_help = true;
        opts->enabled.more_help = true;
        break;
      default:
        usage (stderr, EXIT_FAILURE);
        break;
      }

  if (HAVE_OPT(DEBUG) && OPT_VALUE_DEBUG < 0)
    {
      error (EXIT_FAILURE, 0, "%s option value %d is out of range.",
             "debug", opts->value.debug);
    }
  if (HAVE_OPT(DEBUG) && OPT_VALUE_DEBUG > 9999)
    {
      error (EXIT_FAILURE, 0, "%s option value %d is out of range",
             "debug", opts->value.debug);
    }
  if (HAVE_OPT(REGISTER) && !HAVE_OPT(GENERATE_RSA))
    {
      error (EXIT_FAILURE, 0, "%s option requires the %s options",
             "register", "generate_rsa");
    }
  if (HAVE_OPT(SIGNING) && HAVE_OPT(LEGACY))
    {
      error (EXIT_FAILURE, 0, "the '%s' and '%s' options conflict",
             "signing", "legacy");
    }
  if (HAVE_OPT(SIGNING) && !HAVE_OPT(GENERATE_RSA))
    {
      error (EXIT_FAILURE, 0, "%s option requires the %s options",
             "signing", "generate_rsa");
    }
  if (HAVE_OPT(LEGACY) && HAVE_OPT(SIGNING))
    {
      error (EXIT_FAILURE, 0, "the '%s' and '%s' options conflict",
             "legacy", "signing");
    }
  if (HAVE_OPT(LEGACY) && !HAVE_OPT(GENERATE_RSA))
    {
      error (EXIT_FAILURE, 0, "%s option requires the %s options",
             "legacy", "generate_rsa");
    }
  if (HAVE_OPT(USER) && HAVE_OPT(SYSTEM))
    {
      error (EXIT_FAILURE, 0, "the '%s' and '%s' options conflict",
             "user", "system");
    }
  if (HAVE_OPT(USER) && !HAVE_OPT(REGISTER))
    {
      error (EXIT_FAILURE, 0, "%s option requires the %s options",
             "user", "register");
    }
  if (HAVE_OPT(SYSTEM) && HAVE_OPT(USER))
    {
      error (EXIT_FAILURE, 0, "the '%s' and '%s' options conflict",
             "system", "user");
    }
  if (HAVE_OPT(SYSTEM) && !HAVE_OPT(REGISTER))
    {
      error (EXIT_FAILURE, 0, "%s option requires the %s options",
             "system", "register");
    }
  if (optind < argc)
    {
      error (EXIT_FAILURE, 0, "Command line arguments are not allowed.");
    }


  if (HAVE_OPT(HELP))
    {
      USAGE(0);
    }

  if (HAVE_OPT(MORE_HELP))
#ifdef _WIN32
    {
      USAGE(0);
    }
#else /* _WIN32 */
    {
      pid_t pid;
      int pfds[2];

      if (pipe (pfds) < 0)
        error (EXIT_FAILURE, errno, "pipe");

      pid = fork ();
      if (pid < 0)
        error (EXIT_FAILURE, errno, "fork");

      if (pid == 0)
        {
          close (pfds[0]);
          dup2 (pfds[1], STDOUT_FILENO);
          close (pfds[1]);

          usage (stdout, 0);
        }
      else
        {
          const char *args[2];
          const char *envvar;

          close (pfds[1]);
          dup2 (pfds[0], STDIN_FILENO);
          close (pfds[0]);

          envvar = secure_getenv ("PAGER");
          if (!envvar || *envvar == '\0')
            args[0] = "more";
          else
            args[0] = envvar;

          args[1] = NULL;

          execvp (args[0], (char * const *)args);

          exit (EXIT_FAILURE);
        }
    }
#endif /* !_WIN32 */

  if (HAVE_OPT(VERSION))
    {
      if (!OPT_ARG_VERSION || !strcmp (OPT_ARG_VERSION, "c"))
        {
          const char str[] =
            "tpmtool 3.7.9\n"
            "Copyright (C) 2000-2021 Free Software Foundation, and others\n"
            "This is free software. It is licensed for use, modification and\n"
            "redistribution under the terms of the GNU General Public License,\n"
            "version 3 or later <http://gnu.org/licenses/gpl.html>\n"
            "\n"
            "Please send bug reports to:  <bugs@gnutls.org>            \n";
          fprintf (stdout, "%s", str);
          exit(0);
        }
      else if (!strcmp (OPT_ARG_VERSION, "v"))
        {
          const char str[] =
            "tpmtool 3.7.9\n";
          fprintf (stdout, "%s", str);
          exit(0);
        }
      else if (!strcmp (OPT_ARG_VERSION, "n"))
        {
          const char str[] =
            "tpmtool 3.7.9\n"
            "Copyright (C) 2000-2021 Free Software Foundation, and others\n"
            "This is free software. It is licensed for use, modification and\n"
            "redistribution under the terms of the GNU General Public License,\n"
            "version 3 or later <http://gnu.org/licenses/gpl.html>\n"
            "\n"
            "gnutls is free software: you can redistribute it and/or\n"
            "modify it under the terms of the GNU General Public License\n"
            "as published by the Free Software Foundation,\n"
            "either version 3 of the License, or (at your option) any later version.\n"
            "\n"
            "gnutls is distributed in the hope that it will be useful,\n"
            "but WITHOUT ANY WARRANTY; without even the implied warranty\n"
            "of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
            "See the GNU General Public License for more details.\n"
            "\n"
            "You should have received a copy of the GNU General Public License\n"
            "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n"
            "\n"
            "Please send bug reports to:  <bugs@gnutls.org>            \n";
          fprintf (stdout, "%s", str);
          exit(0);
        }
      else
        {
          error (EXIT_FAILURE, 0,
                 "version option argument 'a' invalid.  Use:\n"
                 "	'v' - version only\n"
                 "	'c' - version and copyright\n"
                 "	'n' - version and full copyright notice");
        }
    }

  return optind;
}

void
usage (FILE *out, int status)
{
  const char str[] =
    "tpmtool - GnuTLS TPM tool\n"
    "Usage:  tpmtool [ -<flag> [<val>] | --<name>[{=| }<val>] ]... \n"
    "\n"
    "None:\n"
    "\n"
    "   -d, --debug=num            Enable debugging\n"
    "				- it must be in the range:\n"
    "				  0 to 9999\n"
    "       --infile=file          Input file\n"
    "				- file must pre-exist\n"
    "       --outfile=str          Output file\n"
    "       --generate-rsa         Generate an RSA private-public key pair\n"
    "       --register             Any generated key will be registered in the TPM\n"
    "				- requires the option 'generate-rsa'\n"
    "       --signing              Any generated key will be a signing key\n"
    "				- prohibits the option 'legacy'\n"
    "				- requires the option 'generate-rsa'\n"
    "       --legacy               Any generated key will be a legacy key\n"
    "				- prohibits the option 'signing'\n"
    "				- requires the option 'generate-rsa'\n"
    "       --user                 Any registered key will be a user key\n"
    "				- prohibits the option 'system'\n"
    "				- requires the option 'register'\n"
    "       --system               Any registered key will be a system key\n"
    "				- prohibits the option 'user'\n"
    "				- requires the option 'register'\n"
    "       --pubkey=str           Prints the public key of the provided key\n"
    "       --list                 Lists all stored keys in the TPM\n"
    "       --delete=str           Delete the key identified by the given URL (UUID)\n"
    "       --test-sign=str        Tests the signature operation of the provided object\n"
    "       --sec-param=str        Specify the security level [low, legacy, medium, high, ultra]\n"
    "       --bits=num             Specify the number of bits for key generate\n"
    "       --inder                Use the DER format for keys\n"
    "       --outder               Use DER format for output keys\n"
    "       --srk-well-known       SRK has well known password (20 bytes of zeros)\n"
    "\n"
    "Version, usage and configuration options:\n"
    "\n"
    "   -v, --version[=arg]        output version information and exit\n"
    "   -h, --help                 display extended usage information and exit\n"
    "   -!, --more-help            extended usage information passed thru pager\n"
    "\n"
    "Options are specified by doubled hyphens and their name or by a single\n"
    "hyphen and the flag character.\n"
    "\n"
    "Program that allows handling cryptographic data from the TPM chip.\n"
    "\n"
    "Please send bug reports to:  <bugs@gnutls.org>\n"
    "\n";
  fprintf (out, "%s", str);
  exit (status);
}