]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
6738bf14 | 2 | * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. |
79dfa975 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (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 | |
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 |
17 | typedef 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 | 22 | const OPTIONS nseq_options[] = { |
7e1b7485 RS |
23 | {"help", OPT_HELP, '-', "Display this summary"}, |
24 | {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"}, | |
25 | {"in", OPT_IN, '<', "Input file"}, | |
26 | {"out", OPT_OUT, '>', "Output file"}, | |
27 | {NULL} | |
28 | }; | |
79dfa975 | 29 | |
7e1b7485 | 30 | int nseq_main(int argc, char **argv) |
79dfa975 | 31 | { |
0f113f3e | 32 | BIO *in = NULL, *out = NULL; |
0f113f3e MC |
33 | X509 *x509 = NULL; |
34 | NETSCAPE_CERT_SEQUENCE *seq = NULL; | |
7e1b7485 RS |
35 | OPTION_CHOICE o; |
36 | int toseq = 0, ret = 1, i; | |
37 | char *infile = NULL, *outfile = NULL, *prog; | |
79dfa975 | 38 | |
7e1b7485 RS |
39 | prog = opt_init(argc, argv, nseq_options); |
40 | while ((o = opt_next()) != OPT_EOF) { | |
41 | switch (o) { | |
42 | case OPT_EOF: | |
43 | case OPT_ERR: | |
03358517 | 44 | opthelp: |
7e1b7485 | 45 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
0f113f3e | 46 | goto end; |
7e1b7485 RS |
47 | case OPT_HELP: |
48 | ret = 0; | |
49 | opt_help(nseq_options); | |
0f113f3e | 50 | goto end; |
7e1b7485 RS |
51 | case OPT_TOSEQ: |
52 | toseq = 1; | |
53 | break; | |
54 | case OPT_IN: | |
55 | infile = opt_arg(); | |
56 | break; | |
57 | case OPT_OUT: | |
58 | outfile = opt_arg(); | |
59 | break; | |
0f113f3e | 60 | } |
0f113f3e | 61 | } |
7e1b7485 | 62 | argc = opt_num_rest(); |
03358517 KR |
63 | if (argc != 0) |
64 | goto opthelp; | |
7e1b7485 | 65 | |
bdd58d98 | 66 | in = bio_open_default(infile, 'r', FORMAT_PEM); |
7e1b7485 RS |
67 | if (in == NULL) |
68 | goto end; | |
bdd58d98 | 69 | out = bio_open_default(outfile, 'w', FORMAT_PEM); |
7e1b7485 RS |
70 | if (out == NULL) |
71 | goto end; | |
72 | ||
0f113f3e MC |
73 | if (toseq) { |
74 | seq = NETSCAPE_CERT_SEQUENCE_new(); | |
96487cdd MC |
75 | if (seq == NULL) |
76 | goto end; | |
0f113f3e | 77 | seq->certs = sk_X509_new_null(); |
96487cdd | 78 | if (seq->certs == NULL) |
7e1b7485 | 79 | goto end; |
0f113f3e MC |
80 | while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) |
81 | sk_X509_push(seq->certs, x509); | |
79dfa975 | 82 | |
0f113f3e | 83 | if (!sk_X509_num(seq->certs)) { |
7e1b7485 RS |
84 | BIO_printf(bio_err, "%s: Error reading certs file %s\n", |
85 | prog, infile); | |
0f113f3e MC |
86 | ERR_print_errors(bio_err); |
87 | goto end; | |
88 | } | |
89 | PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq); | |
90 | ret = 0; | |
91 | goto end; | |
92 | } | |
79dfa975 | 93 | |
7e1b7485 RS |
94 | seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL); |
95 | if (seq == NULL) { | |
96 | BIO_printf(bio_err, "%s: Error reading sequence file %s\n", | |
97 | prog, infile); | |
0f113f3e MC |
98 | ERR_print_errors(bio_err); |
99 | goto end; | |
100 | } | |
79dfa975 | 101 | |
0f113f3e MC |
102 | for (i = 0; i < sk_X509_num(seq->certs); i++) { |
103 | x509 = sk_X509_value(seq->certs, i); | |
104 | dump_cert_text(out, x509); | |
105 | PEM_write_bio_X509(out, x509); | |
106 | } | |
107 | ret = 0; | |
108 | end: | |
109 | BIO_free(in); | |
110 | BIO_free_all(out); | |
111 | NETSCAPE_CERT_SEQUENCE_free(seq); | |
79dfa975 | 112 | |
26a7d938 | 113 | return ret; |
79dfa975 | 114 | } |