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