]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/nseq.c
Fix coverity issues CID 1457745...1457752, 1457853, 1457854
[thirdparty/openssl.git] / apps / nseq.c
CommitLineData
0f113f3e 1/*
6738bf14 2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
79dfa975 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
79dfa975
DSH
8 */
9
10#include <stdio.h>
95dc05bc 11#include <string.h>
7b63c0fa 12#include "apps.h"
dab2cd68 13#include "progs.h"
ec577822
BM
14#include <openssl/pem.h>
15#include <openssl/err.h>
79dfa975 16
7e1b7485
RS
17typedef enum OPTION_choice {
18 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
19 OPT_TOSEQ, OPT_IN, OPT_OUT
20} OPTION_CHOICE;
79dfa975 21
44c83ebd 22const OPTIONS nseq_options[] = {
5388f986 23 OPT_SECTION("General"),
7e1b7485 24 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
25
26 OPT_SECTION("Input"),
7e1b7485 27 {"in", OPT_IN, '<', "Input file"},
5388f986
RS
28
29 OPT_SECTION("Output"),
30 {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
7e1b7485
RS
31 {"out", OPT_OUT, '>', "Output file"},
32 {NULL}
33};
79dfa975 34
7e1b7485 35int nseq_main(int argc, char **argv)
79dfa975 36{
0f113f3e 37 BIO *in = NULL, *out = NULL;
0f113f3e
MC
38 X509 *x509 = NULL;
39 NETSCAPE_CERT_SEQUENCE *seq = NULL;
7e1b7485
RS
40 OPTION_CHOICE o;
41 int toseq = 0, ret = 1, i;
42 char *infile = NULL, *outfile = NULL, *prog;
79dfa975 43
7e1b7485
RS
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:
03358517 49 opthelp:
7e1b7485 50 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
0f113f3e 51 goto end;
7e1b7485
RS
52 case OPT_HELP:
53 ret = 0;
54 opt_help(nseq_options);
0f113f3e 55 goto end;
7e1b7485
RS
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;
0f113f3e 65 }
0f113f3e 66 }
7e1b7485 67 argc = opt_num_rest();
03358517
KR
68 if (argc != 0)
69 goto opthelp;
7e1b7485 70
bdd58d98 71 in = bio_open_default(infile, 'r', FORMAT_PEM);
7e1b7485
RS
72 if (in == NULL)
73 goto end;
bdd58d98 74 out = bio_open_default(outfile, 'w', FORMAT_PEM);
7e1b7485
RS
75 if (out == NULL)
76 goto end;
77
0f113f3e
MC
78 if (toseq) {
79 seq = NETSCAPE_CERT_SEQUENCE_new();
96487cdd
MC
80 if (seq == NULL)
81 goto end;
0f113f3e 82 seq->certs = sk_X509_new_null();
96487cdd 83 if (seq->certs == NULL)
7e1b7485 84 goto end;
d5e66eab
SL
85 while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
86 if (!sk_X509_push(seq->certs, x509))
87 goto end;
88 }
79dfa975 89
0f113f3e 90 if (!sk_X509_num(seq->certs)) {
7e1b7485
RS
91 BIO_printf(bio_err, "%s: Error reading certs file %s\n",
92 prog, infile);
0f113f3e
MC
93 ERR_print_errors(bio_err);
94 goto end;
95 }
96 PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
97 ret = 0;
98 goto end;
99 }
79dfa975 100
7e1b7485
RS
101 seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
102 if (seq == NULL) {
103 BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
104 prog, infile);
0f113f3e
MC
105 ERR_print_errors(bio_err);
106 goto end;
107 }
79dfa975 108
0f113f3e
MC
109 for (i = 0; i < sk_X509_num(seq->certs); i++) {
110 x509 = sk_X509_value(seq->certs, i);
111 dump_cert_text(out, x509);
112 PEM_write_bio_X509(out, x509);
113 }
114 ret = 0;
115 end:
116 BIO_free(in);
117 BIO_free_all(out);
118 NETSCAPE_CERT_SEQUENCE_free(seq);
79dfa975 119
26a7d938 120 return ret;
79dfa975 121}