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