92ae7bd34dac2b49a56c639ef54130f7a8600ad1
[openssl.git] / apps / nseq.c
1 /*
2  * Copyright 1999-2020 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     OPT_PROV_ENUM
21 } OPTION_CHOICE;
22
23 const OPTIONS nseq_options[] = {
24     OPT_SECTION("General"),
25     {"help", OPT_HELP, '-', "Display this summary"},
26
27     OPT_SECTION("Input"),
28     {"in", OPT_IN, '<', "Input file"},
29
30     OPT_SECTION("Output"),
31     {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
32     {"out", OPT_OUT, '>', "Output file"},
33
34     OPT_PROV_OPTIONS,
35     {NULL}
36 };
37
38 int nseq_main(int argc, char **argv)
39 {
40     BIO *in = NULL, *out = NULL;
41     X509 *x509 = NULL;
42     NETSCAPE_CERT_SEQUENCE *seq = NULL;
43     OPTION_CHOICE o;
44     int toseq = 0, ret = 1, i;
45     char *infile = NULL, *outfile = NULL, *prog;
46
47     prog = opt_init(argc, argv, nseq_options);
48     while ((o = opt_next()) != OPT_EOF) {
49         switch (o) {
50         case OPT_EOF:
51         case OPT_ERR:
52  opthelp:
53             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
54             goto end;
55         case OPT_HELP:
56             ret = 0;
57             opt_help(nseq_options);
58             goto end;
59         case OPT_TOSEQ:
60             toseq = 1;
61             break;
62         case OPT_IN:
63             infile = opt_arg();
64             break;
65         case OPT_OUT:
66             outfile = opt_arg();
67             break;
68         case OPT_PROV_CASES:
69             if (!opt_provider(o))
70                 goto end;
71             break;
72         }
73     }
74     argc = opt_num_rest();
75     if (argc != 0)
76         goto opthelp;
77
78     in = bio_open_default(infile, 'r', FORMAT_PEM);
79     if (in == NULL)
80         goto end;
81     out = bio_open_default(outfile, 'w', FORMAT_PEM);
82     if (out == NULL)
83         goto end;
84
85     if (toseq) {
86         seq = NETSCAPE_CERT_SEQUENCE_new();
87         if (seq == NULL)
88             goto end;
89         seq->certs = sk_X509_new_null();
90         if (seq->certs == NULL)
91             goto end;
92         while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
93             if (!sk_X509_push(seq->certs, x509))
94                 goto end;
95         }
96
97         if (!sk_X509_num(seq->certs)) {
98             BIO_printf(bio_err, "%s: Error reading certs file %s\n",
99                        prog, infile);
100             ERR_print_errors(bio_err);
101             goto end;
102         }
103         PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
104         ret = 0;
105         goto end;
106     }
107
108     seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
109     if (seq == NULL) {
110         BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
111                    prog, infile);
112         ERR_print_errors(bio_err);
113         goto end;
114     }
115
116     for (i = 0; i < sk_X509_num(seq->certs); i++) {
117         x509 = sk_X509_value(seq->certs, i);
118         dump_cert_text(out, x509);
119         PEM_write_bio_X509(out, x509);
120     }
121     ret = 0;
122  end:
123     BIO_free(in);
124     BIO_free_all(out);
125     NETSCAPE_CERT_SEQUENCE_free(seq);
126
127     return ret;
128 }