]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/nseq.c
Fix coverity issues CID 1457745...1457752, 1457853, 1457854
[thirdparty/openssl.git] / apps / nseq.c
1 /*
2 * Copyright 1999-2018 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 } OPTION_CHOICE;
21
22 const OPTIONS nseq_options[] = {
23 OPT_SECTION("General"),
24 {"help", OPT_HELP, '-', "Display this summary"},
25
26 OPT_SECTION("Input"),
27 {"in", OPT_IN, '<', "Input file"},
28
29 OPT_SECTION("Output"),
30 {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
31 {"out", OPT_OUT, '>', "Output file"},
32 {NULL}
33 };
34
35 int nseq_main(int argc, char **argv)
36 {
37 BIO *in = NULL, *out = NULL;
38 X509 *x509 = NULL;
39 NETSCAPE_CERT_SEQUENCE *seq = NULL;
40 OPTION_CHOICE o;
41 int toseq = 0, ret = 1, i;
42 char *infile = NULL, *outfile = NULL, *prog;
43
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:
49 opthelp:
50 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
51 goto end;
52 case OPT_HELP:
53 ret = 0;
54 opt_help(nseq_options);
55 goto end;
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;
65 }
66 }
67 argc = opt_num_rest();
68 if (argc != 0)
69 goto opthelp;
70
71 in = bio_open_default(infile, 'r', FORMAT_PEM);
72 if (in == NULL)
73 goto end;
74 out = bio_open_default(outfile, 'w', FORMAT_PEM);
75 if (out == NULL)
76 goto end;
77
78 if (toseq) {
79 seq = NETSCAPE_CERT_SEQUENCE_new();
80 if (seq == NULL)
81 goto end;
82 seq->certs = sk_X509_new_null();
83 if (seq->certs == NULL)
84 goto end;
85 while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
86 if (!sk_X509_push(seq->certs, x509))
87 goto end;
88 }
89
90 if (!sk_X509_num(seq->certs)) {
91 BIO_printf(bio_err, "%s: Error reading certs file %s\n",
92 prog, infile);
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 }
100
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);
105 ERR_print_errors(bio_err);
106 goto end;
107 }
108
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);
119
120 return ret;
121 }