155714Skris/* nseq.c */
2296341Sdelphij/*
3296341Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296341Sdelphij * 1999.
555714Skris */
655714Skris/* ====================================================================
755714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
855714Skris *
955714Skris * Redistribution and use in source and binary forms, with or without
1055714Skris * modification, are permitted provided that the following conditions
1155714Skris * are met:
1255714Skris *
1355714Skris * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
1555714Skris *
1655714Skris * 2. Redistributions in binary form must reproduce the above copyright
1755714Skris *    notice, this list of conditions and the following disclaimer in
1855714Skris *    the documentation and/or other materials provided with the
1955714Skris *    distribution.
2055714Skris *
2155714Skris * 3. All advertising materials mentioning features or use of this
2255714Skris *    software must display the following acknowledgment:
2355714Skris *    "This product includes software developed by the OpenSSL Project
2455714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2555714Skris *
2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2755714Skris *    endorse or promote products derived from this software without
2855714Skris *    prior written permission. For written permission, please contact
2955714Skris *    licensing@OpenSSL.org.
3055714Skris *
3155714Skris * 5. Products derived from this software may not be called "OpenSSL"
3255714Skris *    nor may "OpenSSL" appear in their names without prior written
3355714Skris *    permission of the OpenSSL Project.
3455714Skris *
3555714Skris * 6. Redistributions of any form whatsoever must retain the following
3655714Skris *    acknowledgment:
3755714Skris *    "This product includes software developed by the OpenSSL Project
3855714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3955714Skris *
4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4355714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5255714Skris * ====================================================================
5355714Skris *
5455714Skris * This product includes cryptographic software written by Eric Young
5555714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5655714Skris * Hudson (tjh@cryptsoft.com).
5755714Skris *
5855714Skris */
5955714Skris
6055714Skris#include <stdio.h>
6155714Skris#include <string.h>
62109998Smarkm#include "apps.h"
6355714Skris#include <openssl/pem.h>
6455714Skris#include <openssl/err.h>
6555714Skris
6655714Skris#undef PROG
6755714Skris#define PROG nseq_main
6855714Skris
6959191Skrisint MAIN(int, char **);
7055714Skris
7155714Skrisint MAIN(int argc, char **argv)
7255714Skris{
73296341Sdelphij    char **args, *infile = NULL, *outfile = NULL;
74296341Sdelphij    BIO *in = NULL, *out = NULL;
75296341Sdelphij    int toseq = 0;
76296341Sdelphij    X509 *x509 = NULL;
77296341Sdelphij    NETSCAPE_CERT_SEQUENCE *seq = NULL;
78296341Sdelphij    int i, ret = 1;
79296341Sdelphij    int badarg = 0;
80296341Sdelphij    if (bio_err == NULL)
81296341Sdelphij        bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
82296341Sdelphij    ERR_load_crypto_strings();
83296341Sdelphij    args = argv + 1;
84296341Sdelphij    while (!badarg && *args && *args[0] == '-') {
85296341Sdelphij        if (!strcmp(*args, "-toseq"))
86296341Sdelphij            toseq = 1;
87296341Sdelphij        else if (!strcmp(*args, "-in")) {
88296341Sdelphij            if (args[1]) {
89296341Sdelphij                args++;
90296341Sdelphij                infile = *args;
91296341Sdelphij            } else
92296341Sdelphij                badarg = 1;
93296341Sdelphij        } else if (!strcmp(*args, "-out")) {
94296341Sdelphij            if (args[1]) {
95296341Sdelphij                args++;
96296341Sdelphij                outfile = *args;
97296341Sdelphij            } else
98296341Sdelphij                badarg = 1;
99296341Sdelphij        } else
100296341Sdelphij            badarg = 1;
101296341Sdelphij        args++;
102296341Sdelphij    }
10355714Skris
104296341Sdelphij    if (badarg) {
105296341Sdelphij        BIO_printf(bio_err, "Netscape certificate sequence utility\n");
106296341Sdelphij        BIO_printf(bio_err, "Usage nseq [options]\n");
107296341Sdelphij        BIO_printf(bio_err, "where options are\n");
108296341Sdelphij        BIO_printf(bio_err, "-in file  input file\n");
109296341Sdelphij        BIO_printf(bio_err, "-out file output file\n");
110296341Sdelphij        BIO_printf(bio_err, "-toseq    output NS Sequence file\n");
111296341Sdelphij        OPENSSL_EXIT(1);
112296341Sdelphij    }
11355714Skris
114296341Sdelphij    if (infile) {
115296341Sdelphij        if (!(in = BIO_new_file(infile, "r"))) {
116296341Sdelphij            BIO_printf(bio_err, "Can't open input file %s\n", infile);
117296341Sdelphij            goto end;
118296341Sdelphij        }
119296341Sdelphij    } else
120296341Sdelphij        in = BIO_new_fp(stdin, BIO_NOCLOSE);
12155714Skris
122296341Sdelphij    if (outfile) {
123296341Sdelphij        if (!(out = BIO_new_file(outfile, "w"))) {
124296341Sdelphij            BIO_printf(bio_err, "Can't open output file %s\n", outfile);
125296341Sdelphij            goto end;
126296341Sdelphij        }
127296341Sdelphij    } else {
128296341Sdelphij        out = BIO_new_fp(stdout, BIO_NOCLOSE);
129109998Smarkm#ifdef OPENSSL_SYS_VMS
130296341Sdelphij        {
131296341Sdelphij            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
132296341Sdelphij            out = BIO_push(tmpbio, out);
133296341Sdelphij        }
13468651Skris#endif
135296341Sdelphij    }
136296341Sdelphij    if (toseq) {
137296341Sdelphij        seq = NETSCAPE_CERT_SEQUENCE_new();
138296341Sdelphij        seq->certs = sk_X509_new_null();
139296341Sdelphij        while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
140296341Sdelphij            sk_X509_push(seq->certs, x509);
14155714Skris
142296341Sdelphij        if (!sk_X509_num(seq->certs)) {
143296341Sdelphij            BIO_printf(bio_err, "Error reading certs file %s\n", infile);
144296341Sdelphij            ERR_print_errors(bio_err);
145296341Sdelphij            goto end;
146296341Sdelphij        }
147296341Sdelphij        PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
148296341Sdelphij        ret = 0;
149296341Sdelphij        goto end;
150296341Sdelphij    }
15155714Skris
152296341Sdelphij    if (!(seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL))) {
153296341Sdelphij        BIO_printf(bio_err, "Error reading sequence file %s\n", infile);
154296341Sdelphij        ERR_print_errors(bio_err);
155296341Sdelphij        goto end;
156296341Sdelphij    }
15755714Skris
158296341Sdelphij    for (i = 0; i < sk_X509_num(seq->certs); i++) {
159296341Sdelphij        x509 = sk_X509_value(seq->certs, i);
160296341Sdelphij        dump_cert_text(out, x509);
161296341Sdelphij        PEM_write_bio_X509(out, x509);
162296341Sdelphij    }
163296341Sdelphij    ret = 0;
164296341Sdelphij end:
165296341Sdelphij    BIO_free(in);
166296341Sdelphij    BIO_free_all(out);
167296341Sdelphij    NETSCAPE_CERT_SEQUENCE_free(seq);
16855714Skris
169296341Sdelphij    OPENSSL_EXIT(ret);
17055714Skris}
171