blob: e66b58d95705b267fa74be79369133973a1b3daf [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Matt Caswellaff636a2021-05-06 13:03:23 +01002 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +00003 *
Richard Levittedffa7522018-12-06 13:00:26 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salz846e33c2016-05-17 14:18:30 -04005 * 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
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +00008 */
9
10#include <stdio.h>
Ulf Möller95dc05b1999-04-20 22:50:42 +000011#include <string.h>
Lutz Jänicke7b63c0f2002-07-10 07:01:54 +000012#include "apps.h"
Richard Levittedab2cd62018-01-31 11:13:10 +010013#include "progs.h"
Bodo Möllerec577821999-04-23 22:13:45 +000014#include <openssl/pem.h>
15#include <openssl/err.h>
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +000016
Rich Salz7e1b7482015-04-24 15:26:15 -040017typedef enum OPTION_choice {
Dr. David von Oheimbb0f96012021-05-01 15:29:00 +020018 OPT_COMMON,
Pauli6bd4e3f2020-02-25 14:29:30 +100019 OPT_TOSEQ, OPT_IN, OPT_OUT,
20 OPT_PROV_ENUM
Rich Salz7e1b7482015-04-24 15:26:15 -040021} OPTION_CHOICE;
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +000022
FdaSilvaYY44c83eb2016-03-13 14:07:50 +010023const OPTIONS nseq_options[] = {
Rich Salz5388f982019-11-08 06:08:30 +100024 OPT_SECTION("General"),
Rich Salz7e1b7482015-04-24 15:26:15 -040025 {"help", OPT_HELP, '-', "Display this summary"},
Rich Salz5388f982019-11-08 06:08:30 +100026
27 OPT_SECTION("Input"),
Rich Salz7e1b7482015-04-24 15:26:15 -040028 {"in", OPT_IN, '<', "Input file"},
Rich Salz5388f982019-11-08 06:08:30 +100029
30 OPT_SECTION("Output"),
31 {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
Rich Salz7e1b7482015-04-24 15:26:15 -040032 {"out", OPT_OUT, '>', "Output file"},
Pauli6bd4e3f2020-02-25 14:29:30 +100033
34 OPT_PROV_OPTIONS,
Rich Salz7e1b7482015-04-24 15:26:15 -040035 {NULL}
36};
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +000037
Rich Salz7e1b7482015-04-24 15:26:15 -040038int nseq_main(int argc, char **argv)
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +000039{
Matt Caswell0f113f32015-01-22 03:40:55 +000040 BIO *in = NULL, *out = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000041 X509 *x509 = NULL;
42 NETSCAPE_CERT_SEQUENCE *seq = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -040043 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:
Kurt Roeckx03358512016-02-14 20:45:02 +010052 opthelp:
Rich Salz7e1b7482015-04-24 15:26:15 -040053 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:
Matt Caswell0f113f32015-01-22 03:40:55 +000060 toseq = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -040061 break;
62 case OPT_IN:
63 infile = opt_arg();
64 break;
65 case OPT_OUT:
66 outfile = opt_arg();
67 break;
Pauli6bd4e3f2020-02-25 14:29:30 +100068 case OPT_PROV_CASES:
69 if (!opt_provider(o))
70 goto end;
71 break;
Matt Caswell0f113f32015-01-22 03:40:55 +000072 }
Matt Caswell0f113f32015-01-22 03:40:55 +000073 }
Rich Salz021410e2020-11-28 16:12:58 -050074
75 /* No extra arguments. */
Dr. David von Oheimbd9f07352021-08-27 15:33:18 +020076 if (!opt_check_rest_arg(NULL))
Kurt Roeckx03358512016-02-14 20:45:02 +010077 goto opthelp;
Rich Salz7e1b7482015-04-24 15:26:15 -040078
Richard Levittebdd58d92015-09-04 12:49:06 +020079 in = bio_open_default(infile, 'r', FORMAT_PEM);
Rich Salz7e1b7482015-04-24 15:26:15 -040080 if (in == NULL)
81 goto end;
Richard Levittebdd58d92015-09-04 12:49:06 +020082 out = bio_open_default(outfile, 'w', FORMAT_PEM);
Rich Salz7e1b7482015-04-24 15:26:15 -040083 if (out == NULL)
84 goto end;
85
Matt Caswell0f113f32015-01-22 03:40:55 +000086 if (toseq) {
87 seq = NETSCAPE_CERT_SEQUENCE_new();
Matt Caswell96487cd2015-10-30 11:18:04 +000088 if (seq == NULL)
89 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +000090 seq->certs = sk_X509_new_null();
Matt Caswell96487cd2015-10-30 11:18:04 +000091 if (seq->certs == NULL)
Rich Salz7e1b7482015-04-24 15:26:15 -040092 goto end;
Shane Lontisd5e66ea2020-02-04 13:50:51 +100093 while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
94 if (!sk_X509_push(seq->certs, x509))
95 goto end;
96 }
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +000097
Matt Caswell0f113f32015-01-22 03:40:55 +000098 if (!sk_X509_num(seq->certs)) {
Rich Salz7e1b7482015-04-24 15:26:15 -040099 BIO_printf(bio_err, "%s: Error reading certs file %s\n",
100 prog, infile);
Matt Caswell0f113f32015-01-22 03:40:55 +0000101 ERR_print_errors(bio_err);
102 goto end;
103 }
104 PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
105 ret = 0;
106 goto end;
107 }
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +0000108
Rich Salz7e1b7482015-04-24 15:26:15 -0400109 seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
110 if (seq == NULL) {
111 BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
112 prog, infile);
Matt Caswell0f113f32015-01-22 03:40:55 +0000113 ERR_print_errors(bio_err);
114 goto end;
115 }
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +0000116
Matt Caswell0f113f32015-01-22 03:40:55 +0000117 for (i = 0; i < sk_X509_num(seq->certs); i++) {
118 x509 = sk_X509_value(seq->certs, i);
119 dump_cert_text(out, x509);
120 PEM_write_bio_X509(out, x509);
121 }
122 ret = 0;
123 end:
124 BIO_free(in);
125 BIO_free_all(out);
126 NETSCAPE_CERT_SEQUENCE_free(seq);
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +0000127
KaoruToda26a7d932017-10-17 23:04:09 +0900128 return ret;
Dr. Stephen Henson79dfa971999-01-29 23:34:19 +0000129}