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