summaryrefslogtreecommitdiffstats
path: root/src/ocsptool-options.c
blob: 3290f5b6cce18045c4cb4723ecc4269c2034f0a8 (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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ocsptool-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 ocsptool_options ocsptool_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' },
  { "verbose", no_argument, 0, 'V' },
  { "infile", required_argument, 0, CHAR_MAX + 1 },
  { "outfile", required_argument, 0, CHAR_MAX + 2 },
  { "ask", optional_argument, 0, CHAR_MAX + 3 },
  { "verify-response", no_argument, 0, 'e' },
  { "request-info", no_argument, 0, 'i' },
  { "response-info", no_argument, 0, 'j' },
  { "generate-request", no_argument, 0, 'q' },
  { "nonce", no_argument, 0, CHAR_MAX + 4 },
  { "no-nonce", no_argument, 0, CHAR_MAX + 5 },
  { "load-chain", required_argument, 0, CHAR_MAX + 6 },
  { "load-issuer", required_argument, 0, CHAR_MAX + 7 },
  { "load-cert", required_argument, 0, CHAR_MAX + 8 },
  { "load-trust", required_argument, 0, CHAR_MAX + 9 },
  { "load-signer", required_argument, 0, CHAR_MAX + 10 },
  { "inder", no_argument, 0, CHAR_MAX + 11 },
  { "no-inder", no_argument, 0, CHAR_MAX + 12 },
  { "outder", no_argument, 0, CHAR_MAX + 13 },
  { "outpem", no_argument, 0, CHAR_MAX + 14 },
  { "load-request", required_argument, 0, 'Q' },
  { "load-response", required_argument, 0, 'S' },
  { "ignore-errors", no_argument, 0, CHAR_MAX + 15 },
  { "verify-allow-broken", no_argument, 0, CHAR_MAX + 16 },
  { "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 ocsptool_options *opts = &ocsptool_options;
  int opt;


  while ((opt = getopt_long (argc, argv, "!Q:S:Vd:ehijqv:",
                             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 'V':
        opts->present.verbose = true;
        opts->enabled.verbose = 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: /* --ask */
        opts->present.ask = true;
        opts->arg.ask = optarg;
        opts->enabled.ask = true;
        break;
      case 'e':
        opts->present.verify_response = true;
        opts->enabled.verify_response = true;
        break;
      case 'i':
        opts->present.request_info = true;
        opts->enabled.request_info = true;
        break;
      case 'j':
        opts->present.response_info = true;
        opts->enabled.response_info = true;
        break;
      case 'q':
        opts->present.generate_request = true;
        opts->enabled.generate_request = true;
        break;
      case CHAR_MAX + 4: /* --nonce */
        opts->present.nonce = true;
        opts->enabled.nonce = true;
        break;
      case CHAR_MAX + 5: /* --no-nonce */
        opts->present.nonce = true;
        opts->enabled.nonce = false;
        break;
      case CHAR_MAX + 6: /* --load-chain */
        opts->present.load_chain = true;
        opts->arg.load_chain = optarg;
        opts->enabled.load_chain = true;
        break;
      case CHAR_MAX + 7: /* --load-issuer */
        opts->present.load_issuer = true;
        opts->arg.load_issuer = optarg;
        opts->enabled.load_issuer = true;
        break;
      case CHAR_MAX + 8: /* --load-cert */
        opts->present.load_cert = true;
        opts->arg.load_cert = optarg;
        opts->enabled.load_cert = true;
        break;
      case CHAR_MAX + 9: /* --load-trust */
        opts->present.load_trust = true;
        opts->arg.load_trust = optarg;
        opts->enabled.load_trust = true;
        break;
      case CHAR_MAX + 10: /* --load-signer */
        opts->present.load_signer = true;
        opts->arg.load_signer = optarg;
        opts->enabled.load_signer = true;
        break;
      case CHAR_MAX + 11: /* --inder */
        opts->present.inder = true;
        opts->enabled.inder = true;
        break;
      case CHAR_MAX + 12: /* --no-inder */
        opts->present.inder = true;
        opts->enabled.inder = false;
        break;
      case CHAR_MAX + 13: /* --outder */
        opts->present.outder = true;
        opts->enabled.outder = true;
        break;
      case CHAR_MAX + 14: /* --outpem */
        opts->present.outpem = true;
        opts->enabled.outpem = true;
        break;
      case 'Q':
        opts->present.load_request = true;
        opts->arg.load_request = optarg;
        opts->enabled.load_request = true;
        break;
      case 'S':
        opts->present.load_response = true;
        opts->arg.load_response = optarg;
        opts->enabled.load_response = true;
        break;
      case CHAR_MAX + 15: /* --ignore-errors */
        opts->present.ignore_errors = true;
        opts->enabled.ignore_errors = true;
        break;
      case CHAR_MAX + 16: /* --verify-allow-broken */
        opts->present.verify_allow_broken = true;
        opts->enabled.verify_allow_broken = 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(LOAD_TRUST) && HAVE_OPT(LOAD_SIGNER))
    {
      error (EXIT_FAILURE, 0, "the '%s' and '%s' options conflict",
             "load-trust", "load_signer");
    }
  if (HAVE_OPT(LOAD_SIGNER) && HAVE_OPT(LOAD_TRUST))
    {
      error (EXIT_FAILURE, 0, "the '%s' and '%s' options conflict",
             "load-signer", "load_trust");
    }
  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[] =
            "ocsptool 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[] =
            "ocsptool 3.7.9\n";
          fprintf (stdout, "%s", str);
          exit(0);
        }
      else if (!strcmp (OPT_ARG_VERSION, "n"))
        {
          const char str[] =
            "ocsptool 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[] =
    "ocsptool - GnuTLS OCSP tool\n"
    "Usage:  ocsptool [ -<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"
    "   -V, --verbose              More verbose output\n"
    "       --infile=file          Input file\n"
    "				- file must pre-exist\n"
    "       --outfile=str          Output file\n"
    "       --ask[=str]            Ask an OCSP/HTTP server on a certificate validity\n"
    "   -e, --verify-response      Verify response\n"
    "   -i, --request-info         Print information on a OCSP request\n"
    "   -j, --response-info        Print information on a OCSP response\n"
    "   -q, --generate-request     Generates an OCSP request\n"
    "       --nonce                Use (or not) a nonce to OCSP request\n"
    "       --load-chain=file      Reads a set of certificates forming a chain from file\n"
    "				- file must pre-exist\n"
    "       --load-issuer=file     Reads issuer's certificate from file\n"
    "				- file must pre-exist\n"
    "       --load-cert=file       Reads the certificate to check from file\n"
    "				- file must pre-exist\n"
    "       --load-trust=file      Read OCSP trust anchors from file\n"
    "				- prohibits the option 'load-signer'\n"
    "				- file must pre-exist\n"
    "       --load-signer=file     Reads the OCSP response signer from file\n"
    "				- prohibits the option 'load-trust'\n"
    "				- file must pre-exist\n"
    "       --inder                Use DER format for input certificates and private keys\n"
    "       --outder               Use DER format for output of responses (this is the default)\n"
    "       --outpem               Use PEM format for output of responses\n"
    "   -Q, --load-request=file    Reads the DER encoded OCSP request from file\n"
    "				- file must pre-exist\n"
    "   -S, --load-response=file   Reads the DER encoded OCSP response from file\n"
    "				- file must pre-exist\n"
    "       --ignore-errors        Ignore any verification errors\n"
    "       --verify-allow-broken  Allow broken algorithms, such as MD5 for verification\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"
    "ocsptool is a program that can parse and print information about\n"
    "OCSP requests/responses, generate requests and verify responses. Unlike\n"
    "other GnuTLS applications it outputs DER encoded structures by default\n"
    "unless the '--outpem' option is specified.\n"
    "\n"
    "Please send bug reports to:  <bugs@gnutls.org>\n"
    "\n";
  fprintf (out, "%s", str);
  exit (status);
}