]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 1999-2020 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 | 17 | typedef enum OPTION_choice { |
b0f96018 | 18 | OPT_COMMON, |
6bd4e3f2 P |
19 | OPT_TOSEQ, OPT_IN, OPT_OUT, |
20 | OPT_PROV_ENUM | |
7e1b7485 | 21 | } OPTION_CHOICE; |
79dfa975 | 22 | |
44c83ebd | 23 | const OPTIONS nseq_options[] = { |
5388f986 | 24 | OPT_SECTION("General"), |
7e1b7485 | 25 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
26 | |
27 | OPT_SECTION("Input"), | |
7e1b7485 | 28 | {"in", OPT_IN, '<', "Input file"}, |
5388f986 RS |
29 | |
30 | OPT_SECTION("Output"), | |
31 | {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"}, | |
7e1b7485 | 32 | {"out", OPT_OUT, '>', "Output file"}, |
6bd4e3f2 P |
33 | |
34 | OPT_PROV_OPTIONS, | |
7e1b7485 RS |
35 | {NULL} |
36 | }; | |
79dfa975 | 37 | |
7e1b7485 | 38 | int nseq_main(int argc, char **argv) |
79dfa975 | 39 | { |
0f113f3e | 40 | BIO *in = NULL, *out = NULL; |
0f113f3e MC |
41 | X509 *x509 = NULL; |
42 | NETSCAPE_CERT_SEQUENCE *seq = NULL; | |
7e1b7485 RS |
43 | OPTION_CHOICE o; |
44 | int toseq = 0, ret = 1, i; | |
45 | char *infile = NULL, *outfile = NULL, *prog; | |
79dfa975 | 46 | |
7e1b7485 RS |
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: | |
03358517 | 52 | opthelp: |
7e1b7485 | 53 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
0f113f3e | 54 | goto end; |
7e1b7485 RS |
55 | case OPT_HELP: |
56 | ret = 0; | |
57 | opt_help(nseq_options); | |
0f113f3e | 58 | goto end; |
7e1b7485 RS |
59 | case OPT_TOSEQ: |
60 | toseq = 1; | |
61 | break; | |
62 | case OPT_IN: | |
63 | infile = opt_arg(); | |
64 | break; | |
65 | case OPT_OUT: | |
66 | outfile = opt_arg(); | |
67 | break; | |
6bd4e3f2 P |
68 | case OPT_PROV_CASES: |
69 | if (!opt_provider(o)) | |
70 | goto end; | |
71 | break; | |
0f113f3e | 72 | } |
0f113f3e | 73 | } |
021410ea RS |
74 | |
75 | /* No extra arguments. */ | |
7e1b7485 | 76 | argc = opt_num_rest(); |
03358517 KR |
77 | if (argc != 0) |
78 | goto opthelp; | |
7e1b7485 | 79 | |
bdd58d98 | 80 | in = bio_open_default(infile, 'r', FORMAT_PEM); |
7e1b7485 RS |
81 | if (in == NULL) |
82 | goto end; | |
bdd58d98 | 83 | out = bio_open_default(outfile, 'w', FORMAT_PEM); |
7e1b7485 RS |
84 | if (out == NULL) |
85 | goto end; | |
86 | ||
0f113f3e MC |
87 | if (toseq) { |
88 | seq = NETSCAPE_CERT_SEQUENCE_new(); | |
96487cdd MC |
89 | if (seq == NULL) |
90 | goto end; | |
0f113f3e | 91 | seq->certs = sk_X509_new_null(); |
96487cdd | 92 | if (seq->certs == NULL) |
7e1b7485 | 93 | goto end; |
d5e66eab SL |
94 | while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
95 | if (!sk_X509_push(seq->certs, x509)) | |
96 | goto end; | |
97 | } | |
79dfa975 | 98 | |
0f113f3e | 99 | if (!sk_X509_num(seq->certs)) { |
7e1b7485 RS |
100 | BIO_printf(bio_err, "%s: Error reading certs file %s\n", |
101 | prog, infile); | |
0f113f3e MC |
102 | ERR_print_errors(bio_err); |
103 | goto end; | |
104 | } | |
105 | PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq); | |
106 | ret = 0; | |
107 | goto end; | |
108 | } | |
79dfa975 | 109 | |
7e1b7485 RS |
110 | seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL); |
111 | if (seq == NULL) { | |
112 | BIO_printf(bio_err, "%s: Error reading sequence file %s\n", | |
113 | prog, infile); | |
0f113f3e MC |
114 | ERR_print_errors(bio_err); |
115 | goto end; | |
116 | } | |
79dfa975 | 117 | |
0f113f3e MC |
118 | for (i = 0; i < sk_X509_num(seq->certs); i++) { |
119 | x509 = sk_X509_value(seq->certs, i); | |
120 | dump_cert_text(out, x509); | |
121 | PEM_write_bio_X509(out, x509); | |
122 | } | |
123 | ret = 0; | |
124 | end: | |
125 | BIO_free(in); | |
126 | BIO_free_all(out); | |
127 | NETSCAPE_CERT_SEQUENCE_free(seq); | |
79dfa975 | 128 | |
26a7d938 | 129 | return ret; |
79dfa975 | 130 | } |