16f9291ceSJung-uk Kim /*
2*b077aed3SPierre Pronchery * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
874664626SKris Kennaway */
974664626SKris Kennaway
1074664626SKris Kennaway #include <stdio.h>
1174664626SKris Kennaway #include <string.h>
125c87c606SMark Murray #include "apps.h"
13e71b7053SJung-uk Kim #include "progs.h"
1474664626SKris Kennaway #include <openssl/pem.h>
1574664626SKris Kennaway #include <openssl/err.h>
1674664626SKris Kennaway
17e71b7053SJung-uk Kim typedef enum OPTION_choice {
18*b077aed3SPierre Pronchery OPT_COMMON,
19*b077aed3SPierre Pronchery OPT_TOSEQ, OPT_IN, OPT_OUT,
20*b077aed3SPierre Pronchery OPT_PROV_ENUM
21e71b7053SJung-uk Kim } OPTION_CHOICE;
2274664626SKris Kennaway
23e71b7053SJung-uk Kim const OPTIONS nseq_options[] = {
24*b077aed3SPierre Pronchery OPT_SECTION("General"),
25e71b7053SJung-uk Kim {"help", OPT_HELP, '-', "Display this summary"},
26*b077aed3SPierre Pronchery
27*b077aed3SPierre Pronchery OPT_SECTION("Input"),
28e71b7053SJung-uk Kim {"in", OPT_IN, '<', "Input file"},
29*b077aed3SPierre Pronchery
30*b077aed3SPierre Pronchery OPT_SECTION("Output"),
31*b077aed3SPierre Pronchery {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
32e71b7053SJung-uk Kim {"out", OPT_OUT, '>', "Output file"},
33*b077aed3SPierre Pronchery
34*b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
35e71b7053SJung-uk Kim {NULL}
36e71b7053SJung-uk Kim };
3774664626SKris Kennaway
nseq_main(int argc,char ** argv)38e71b7053SJung-uk Kim int nseq_main(int argc, char **argv)
3974664626SKris Kennaway {
4074664626SKris Kennaway BIO *in = NULL, *out = NULL;
4174664626SKris Kennaway X509 *x509 = NULL;
4274664626SKris Kennaway NETSCAPE_CERT_SEQUENCE *seq = NULL;
43e71b7053SJung-uk Kim OPTION_CHOICE o;
44e71b7053SJung-uk Kim int toseq = 0, ret = 1, i;
45e71b7053SJung-uk Kim char *infile = NULL, *outfile = NULL, *prog;
46e71b7053SJung-uk Kim
47e71b7053SJung-uk Kim prog = opt_init(argc, argv, nseq_options);
48e71b7053SJung-uk Kim while ((o = opt_next()) != OPT_EOF) {
49e71b7053SJung-uk Kim switch (o) {
50e71b7053SJung-uk Kim case OPT_EOF:
51e71b7053SJung-uk Kim case OPT_ERR:
52e71b7053SJung-uk Kim opthelp:
53e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
54e71b7053SJung-uk Kim goto end;
55e71b7053SJung-uk Kim case OPT_HELP:
56e71b7053SJung-uk Kim ret = 0;
57e71b7053SJung-uk Kim opt_help(nseq_options);
58e71b7053SJung-uk Kim goto end;
59e71b7053SJung-uk Kim case OPT_TOSEQ:
606f9291ceSJung-uk Kim toseq = 1;
61e71b7053SJung-uk Kim break;
62e71b7053SJung-uk Kim case OPT_IN:
63e71b7053SJung-uk Kim infile = opt_arg();
64e71b7053SJung-uk Kim break;
65e71b7053SJung-uk Kim case OPT_OUT:
66e71b7053SJung-uk Kim outfile = opt_arg();
67e71b7053SJung-uk Kim break;
68*b077aed3SPierre Pronchery case OPT_PROV_CASES:
69*b077aed3SPierre Pronchery if (!opt_provider(o))
70*b077aed3SPierre Pronchery goto end;
71*b077aed3SPierre Pronchery break;
7274664626SKris Kennaway }
7374664626SKris Kennaway }
74*b077aed3SPierre Pronchery
75*b077aed3SPierre Pronchery /* No extra arguments. */
76e71b7053SJung-uk Kim argc = opt_num_rest();
77e71b7053SJung-uk Kim if (argc != 0)
78e71b7053SJung-uk Kim goto opthelp;
7974664626SKris Kennaway
80e71b7053SJung-uk Kim in = bio_open_default(infile, 'r', FORMAT_PEM);
81e71b7053SJung-uk Kim if (in == NULL)
8274664626SKris Kennaway goto end;
83e71b7053SJung-uk Kim out = bio_open_default(outfile, 'w', FORMAT_PEM);
84e71b7053SJung-uk Kim if (out == NULL)
8574664626SKris Kennaway goto end;
86e71b7053SJung-uk Kim
8774664626SKris Kennaway if (toseq) {
8874664626SKris Kennaway seq = NETSCAPE_CERT_SEQUENCE_new();
89e71b7053SJung-uk Kim if (seq == NULL)
90e71b7053SJung-uk Kim goto end;
91ddd58736SKris Kennaway seq->certs = sk_X509_new_null();
92e71b7053SJung-uk Kim if (seq->certs == NULL)
93e71b7053SJung-uk Kim goto end;
94*b077aed3SPierre Pronchery while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
95*b077aed3SPierre Pronchery if (!sk_X509_push(seq->certs, x509))
96*b077aed3SPierre Pronchery goto end;
97*b077aed3SPierre Pronchery }
9874664626SKris Kennaway
996f9291ceSJung-uk Kim if (!sk_X509_num(seq->certs)) {
100e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Error reading certs file %s\n",
101e71b7053SJung-uk Kim prog, infile);
10274664626SKris Kennaway ERR_print_errors(bio_err);
10374664626SKris Kennaway goto end;
10474664626SKris Kennaway }
10574664626SKris Kennaway PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
10674664626SKris Kennaway ret = 0;
10774664626SKris Kennaway goto end;
10874664626SKris Kennaway }
10974664626SKris Kennaway
110e71b7053SJung-uk Kim seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
111e71b7053SJung-uk Kim if (seq == NULL) {
112e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
113e71b7053SJung-uk Kim prog, infile);
11474664626SKris Kennaway ERR_print_errors(bio_err);
11574664626SKris Kennaway goto end;
11674664626SKris Kennaway }
11774664626SKris Kennaway
11874664626SKris Kennaway for (i = 0; i < sk_X509_num(seq->certs); i++) {
11974664626SKris Kennaway x509 = sk_X509_value(seq->certs, i);
12074664626SKris Kennaway dump_cert_text(out, x509);
12174664626SKris Kennaway PEM_write_bio_X509(out, x509);
12274664626SKris Kennaway }
12374664626SKris Kennaway ret = 0;
12474664626SKris Kennaway end:
12574664626SKris Kennaway BIO_free(in);
126ddd58736SKris Kennaway BIO_free_all(out);
12774664626SKris Kennaway NETSCAPE_CERT_SEQUENCE_free(seq);
12874664626SKris Kennaway
129e71b7053SJung-uk Kim return ret;
13074664626SKris Kennaway }
131