Add "sections" to -help output
[openssl.git] / apps / nseq.c
1 /*
2  * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include "apps.h"
13 #include "progs.h"
14 #include <openssl/pem.h>
15 #include <openssl/err.h>
16
17 typedef enum OPTION_choice {
18     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
19     OPT_TOSEQ, OPT_IN, OPT_OUT
20 } OPTION_CHOICE;
21
22 const OPTIONS nseq_options[] = {
23     OPT_SECTION("General"),
24     {"help", OPT_HELP, '-', "Display this summary"},
25
26     OPT_SECTION("Input"),
27     {"in", OPT_IN, '<', "Input file"},
28
29     OPT_SECTION("Output"),
30     {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
31     {"out", OPT_OUT, '>', "Output file"},
32     {NULL}
33 };
34
35 int nseq_main(int argc, char **argv)
36 {
37     BIO *in = NULL, *out = NULL;
38     X509 *x509 = NULL;
39     NETSCAPE_CERT_SEQUENCE *seq = NULL;
40     OPTION_CHOICE o;
41     int toseq = 0, ret = 1, i;
42     char *infile = NULL, *outfile = NULL, *prog;
43
44     prog = opt_init(argc, argv, nseq_options);
45     while ((o = opt_next()) != OPT_EOF) {
46         switch (o) {
47         case OPT_EOF:
48         case OPT_ERR:
49  opthelp:
50             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
51             goto end;
52         case OPT_HELP:
53             ret = 0;
54             opt_help(nseq_options);
55             goto end;
56         case OPT_TOSEQ:
57             toseq = 1;
58             break;
59         case OPT_IN:
60             infile = opt_arg();
61             break;
62         case OPT_OUT:
63             outfile = opt_arg();
64             break;
65         }
66     }
67     argc = opt_num_rest();
68     if (argc != 0)
69         goto opthelp;
70
71     in = bio_open_default(infile, 'r', FORMAT_PEM);
72     if (in == NULL)
73         goto end;
74     out = bio_open_default(outfile, 'w', FORMAT_PEM);
75     if (out == NULL)
76         goto end;
77
78     if (toseq) {
79         seq = NETSCAPE_CERT_SEQUENCE_new();
80         if (seq == NULL)
81             goto end;
82         seq->certs = sk_X509_new_null();
83         if (seq->certs == NULL)
84             goto end;
85         while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
86             sk_X509_push(seq->certs, x509);
87
88         if (!sk_X509_num(seq->certs)) {
89             BIO_printf(bio_err, "%s: Error reading certs file %s\n",
90                        prog, infile);
91             ERR_print_errors(bio_err);
92             goto end;
93         }
94         PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
95         ret = 0;
96         goto end;
97     }
98
99     seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
100     if (seq == NULL) {
101         BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
102                    prog, infile);
103         ERR_print_errors(bio_err);
104         goto end;
105     }
106
107     for (i = 0; i < sk_X509_num(seq->certs); i++) {
108         x509 = sk_X509_value(seq->certs, i);
109         dump_cert_text(out, x509);
110         PEM_write_bio_X509(out, x509);
111     }
112     ret = 0;
113  end:
114     BIO_free(in);
115     BIO_free_all(out);
116     NETSCAPE_CERT_SEQUENCE_free(seq);
117
118     return ret;
119 }