1ebfedea0SLionel Sambuc /* nseq.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc * 1999.
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc
60ebfedea0SLionel Sambuc #include <stdio.h>
61ebfedea0SLionel Sambuc #include <string.h>
62ebfedea0SLionel Sambuc #include "apps.h"
63ebfedea0SLionel Sambuc #include <openssl/pem.h>
64ebfedea0SLionel Sambuc #include <openssl/err.h>
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc #undef PROG
67ebfedea0SLionel Sambuc #define PROG nseq_main
68ebfedea0SLionel Sambuc
69ebfedea0SLionel Sambuc int MAIN(int, char **);
70ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)71ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
72ebfedea0SLionel Sambuc {
73ebfedea0SLionel Sambuc char **args, *infile = NULL, *outfile = NULL;
74ebfedea0SLionel Sambuc BIO *in = NULL, *out = NULL;
75ebfedea0SLionel Sambuc int toseq = 0;
76ebfedea0SLionel Sambuc X509 *x509 = NULL;
77ebfedea0SLionel Sambuc NETSCAPE_CERT_SEQUENCE *seq = NULL;
78ebfedea0SLionel Sambuc int i, ret = 1;
79ebfedea0SLionel Sambuc int badarg = 0;
80*0a6a1f1dSLionel Sambuc if (bio_err == NULL)
81*0a6a1f1dSLionel Sambuc bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
82ebfedea0SLionel Sambuc ERR_load_crypto_strings();
83ebfedea0SLionel Sambuc args = argv + 1;
84ebfedea0SLionel Sambuc while (!badarg && *args && *args[0] == '-') {
85*0a6a1f1dSLionel Sambuc if (!strcmp(*args, "-toseq"))
86*0a6a1f1dSLionel Sambuc toseq = 1;
87ebfedea0SLionel Sambuc else if (!strcmp(*args, "-in")) {
88ebfedea0SLionel Sambuc if (args[1]) {
89ebfedea0SLionel Sambuc args++;
90ebfedea0SLionel Sambuc infile = *args;
91*0a6a1f1dSLionel Sambuc } else
92*0a6a1f1dSLionel Sambuc badarg = 1;
93ebfedea0SLionel Sambuc } else if (!strcmp(*args, "-out")) {
94ebfedea0SLionel Sambuc if (args[1]) {
95ebfedea0SLionel Sambuc args++;
96ebfedea0SLionel Sambuc outfile = *args;
97*0a6a1f1dSLionel Sambuc } else
98*0a6a1f1dSLionel Sambuc badarg = 1;
99*0a6a1f1dSLionel Sambuc } else
100*0a6a1f1dSLionel Sambuc badarg = 1;
101ebfedea0SLionel Sambuc args++;
102ebfedea0SLionel Sambuc }
103ebfedea0SLionel Sambuc
104ebfedea0SLionel Sambuc if (badarg) {
105ebfedea0SLionel Sambuc BIO_printf(bio_err, "Netscape certificate sequence utility\n");
106ebfedea0SLionel Sambuc BIO_printf(bio_err, "Usage nseq [options]\n");
107ebfedea0SLionel Sambuc BIO_printf(bio_err, "where options are\n");
108ebfedea0SLionel Sambuc BIO_printf(bio_err, "-in file input file\n");
109ebfedea0SLionel Sambuc BIO_printf(bio_err, "-out file output file\n");
110ebfedea0SLionel Sambuc BIO_printf(bio_err, "-toseq output NS Sequence file\n");
111ebfedea0SLionel Sambuc OPENSSL_EXIT(1);
112ebfedea0SLionel Sambuc }
113ebfedea0SLionel Sambuc
114ebfedea0SLionel Sambuc if (infile) {
115ebfedea0SLionel Sambuc if (!(in = BIO_new_file(infile, "r"))) {
116*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Can't open input file %s\n", infile);
117ebfedea0SLionel Sambuc goto end;
118ebfedea0SLionel Sambuc }
119*0a6a1f1dSLionel Sambuc } else
120*0a6a1f1dSLionel Sambuc in = BIO_new_fp(stdin, BIO_NOCLOSE);
121ebfedea0SLionel Sambuc
122ebfedea0SLionel Sambuc if (outfile) {
123ebfedea0SLionel Sambuc if (!(out = BIO_new_file(outfile, "w"))) {
124*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Can't open output file %s\n", outfile);
125ebfedea0SLionel Sambuc goto end;
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc } else {
128ebfedea0SLionel Sambuc out = BIO_new_fp(stdout, BIO_NOCLOSE);
129ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
130ebfedea0SLionel Sambuc {
131ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
132ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
133ebfedea0SLionel Sambuc }
134ebfedea0SLionel Sambuc #endif
135ebfedea0SLionel Sambuc }
136ebfedea0SLionel Sambuc if (toseq) {
137ebfedea0SLionel Sambuc seq = NETSCAPE_CERT_SEQUENCE_new();
138ebfedea0SLionel Sambuc seq->certs = sk_X509_new_null();
139ebfedea0SLionel Sambuc while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
140ebfedea0SLionel Sambuc sk_X509_push(seq->certs, x509);
141ebfedea0SLionel Sambuc
142*0a6a1f1dSLionel Sambuc if (!sk_X509_num(seq->certs)) {
143ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error reading certs file %s\n", infile);
144ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
145ebfedea0SLionel Sambuc goto end;
146ebfedea0SLionel Sambuc }
147ebfedea0SLionel Sambuc PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
148ebfedea0SLionel Sambuc ret = 0;
149ebfedea0SLionel Sambuc goto end;
150ebfedea0SLionel Sambuc }
151ebfedea0SLionel Sambuc
152ebfedea0SLionel Sambuc if (!(seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL))) {
153ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error reading sequence file %s\n", infile);
154ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
155ebfedea0SLionel Sambuc goto end;
156ebfedea0SLionel Sambuc }
157ebfedea0SLionel Sambuc
158ebfedea0SLionel Sambuc for (i = 0; i < sk_X509_num(seq->certs); i++) {
159ebfedea0SLionel Sambuc x509 = sk_X509_value(seq->certs, i);
160ebfedea0SLionel Sambuc dump_cert_text(out, x509);
161ebfedea0SLionel Sambuc PEM_write_bio_X509(out, x509);
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc ret = 0;
164ebfedea0SLionel Sambuc end:
165ebfedea0SLionel Sambuc BIO_free(in);
166ebfedea0SLionel Sambuc BIO_free_all(out);
167ebfedea0SLionel Sambuc NETSCAPE_CERT_SEQUENCE_free(seq);
168ebfedea0SLionel Sambuc
169ebfedea0SLionel Sambuc OPENSSL_EXIT(ret);
170ebfedea0SLionel Sambuc }
171