1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. |
---|
5 | * |
---|
6 | * Licensed under the OpenSSL license (the "License"). You may not use |
---|
7 | * this file except in compliance with the License. You can obtain a copy |
---|
8 | * in the file LICENSE in the source distribution or at |
---|
9 | * https://www.openssl.org/source/license.html |
---|
10 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include "apps.h" |
---|
15 | #include "progs.h" |
---|
16 | #include <openssl/pem.h> |
---|
17 | #include <openssl/err.h> |
---|
18 | |
---|
19 | typedef enum OPTION_choice { |
---|
20 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
---|
21 | OPT_TOSEQ, OPT_IN, OPT_OUT |
---|
22 | } OPTION_CHOICE; |
---|
23 | |
---|
24 | const OPTIONS nseq_options[] = { |
---|
25 | {"help", OPT_HELP, '-', "Display this summary"}, |
---|
26 | {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"}, |
---|
27 | {"in", OPT_IN, '<', "Input file"}, |
---|
28 | {"out", OPT_OUT, '>', "Output file"}, |
---|
29 | {NULL} |
---|
30 | }; |
---|
31 | |
---|
32 | int nseq_main(int argc, char **argv) |
---|
33 | { |
---|
34 | BIO *in = NULL, *out = NULL; |
---|
35 | X509 *x509 = NULL; |
---|
36 | NETSCAPE_CERT_SEQUENCE *seq = NULL; |
---|
37 | OPTION_CHOICE o; |
---|
38 | int toseq = 0, ret = 1, i; |
---|
39 | char *infile = NULL, *outfile = NULL, *prog; |
---|
40 | |
---|
41 | prog = opt_init(argc, argv, nseq_options); |
---|
42 | while ((o = opt_next()) != OPT_EOF) { |
---|
43 | switch (o) { |
---|
44 | case OPT_EOF: |
---|
45 | case OPT_ERR: |
---|
46 | opthelp: |
---|
47 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
---|
48 | goto end; |
---|
49 | case OPT_HELP: |
---|
50 | ret = 0; |
---|
51 | opt_help(nseq_options); |
---|
52 | goto end; |
---|
53 | case OPT_TOSEQ: |
---|
54 | toseq = 1; |
---|
55 | break; |
---|
56 | case OPT_IN: |
---|
57 | infile = opt_arg(); |
---|
58 | break; |
---|
59 | case OPT_OUT: |
---|
60 | outfile = opt_arg(); |
---|
61 | break; |
---|
62 | } |
---|
63 | } |
---|
64 | argc = opt_num_rest(); |
---|
65 | if (argc != 0) |
---|
66 | goto opthelp; |
---|
67 | |
---|
68 | in = bio_open_default(infile, 'r', FORMAT_PEM); |
---|
69 | if (in == NULL) |
---|
70 | goto end; |
---|
71 | out = bio_open_default(outfile, 'w', FORMAT_PEM); |
---|
72 | if (out == NULL) |
---|
73 | goto end; |
---|
74 | |
---|
75 | if (toseq) { |
---|
76 | seq = NETSCAPE_CERT_SEQUENCE_new(); |
---|
77 | if (seq == NULL) |
---|
78 | goto end; |
---|
79 | seq->certs = sk_X509_new_null(); |
---|
80 | if (seq->certs == NULL) |
---|
81 | goto end; |
---|
82 | while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) |
---|
83 | sk_X509_push(seq->certs, x509); |
---|
84 | |
---|
85 | if (!sk_X509_num(seq->certs)) { |
---|
86 | BIO_printf(bio_err, "%s: Error reading certs file %s\n", |
---|
87 | prog, infile); |
---|
88 | ERR_print_errors(bio_err); |
---|
89 | goto end; |
---|
90 | } |
---|
91 | PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq); |
---|
92 | ret = 0; |
---|
93 | goto end; |
---|
94 | } |
---|
95 | |
---|
96 | seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL); |
---|
97 | if (seq == NULL) { |
---|
98 | BIO_printf(bio_err, "%s: Error reading sequence file %s\n", |
---|
99 | prog, infile); |
---|
100 | ERR_print_errors(bio_err); |
---|
101 | goto end; |
---|
102 | } |
---|
103 | |
---|
104 | for (i = 0; i < sk_X509_num(seq->certs); i++) { |
---|
105 | x509 = sk_X509_value(seq->certs, i); |
---|
106 | dump_cert_text(out, x509); |
---|
107 | PEM_write_bio_X509(out, x509); |
---|
108 | } |
---|
109 | ret = 0; |
---|
110 | end: |
---|
111 | BIO_free(in); |
---|
112 | BIO_free_all(out); |
---|
113 | NETSCAPE_CERT_SEQUENCE_free(seq); |
---|
114 | |
---|
115 | return ret; |
---|
116 | } |
---|