12e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor/* nseq.c */
22e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
32e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * project 1999.
42e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor */
52e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor/* ====================================================================
62e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
72e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *
82e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * Redistribution and use in source and binary forms, with or without
92e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * modification, are permitted provided that the following conditions
102e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * are met:
112e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *
122e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * 1. Redistributions of source code must retain the above copyright
132e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    notice, this list of conditions and the following disclaimer.
142e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *
152e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * 2. Redistributions in binary form must reproduce the above copyright
162e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    notice, this list of conditions and the following disclaimer in
172e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    the documentation and/or other materials provided with the
1850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor *    distribution.
19d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer *
202e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * 3. All advertising materials mentioning features or use of this
212e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    software must display the following acknowledgment:
222e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    "This product includes software developed by the OpenSSL Project
232e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
242e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *
252e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26011bb4edf731d529da1cbf71c7c2696aaf5a054fChris Lattner *    endorse or promote products derived from this software without
27011bb4edf731d529da1cbf71c7c2696aaf5a054fChris Lattner *    prior written permission. For written permission, please contact
283e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt *    licensing@OpenSSL.org.
29011bb4edf731d529da1cbf71c7c2696aaf5a054fChris Lattner *
302e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * 5. Products derived from this software may not be called "OpenSSL"
31011bb4edf731d529da1cbf71c7c2696aaf5a054fChris Lattner *    nor may "OpenSSL" appear in their names without prior written
322a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor *    permission of the OpenSSL Project.
332577743c5650c646fb705df01403707e94f2df04Abramo Bagnara *
342e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * 6. Redistributions of any form whatsoever must retain the following
352e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    acknowledgment:
362e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    "This product includes software developed by the OpenSSL Project
372e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
382e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *
392e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
402e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
412e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
422e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
432e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
442e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
452e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
462e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
472e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
482e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
492e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
502e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * OF THE POSSIBILITY OF SUCH DAMAGE.
51e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor * ====================================================================
522a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor *
533e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt * This product includes cryptographic software written by Eric Young
542a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor * (eay@cryptsoft.com).  This product includes software written by Tim
552e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor * Hudson (tjh@cryptsoft.com).
562e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor *
572e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor */
582e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
592e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor#include <stdio.h>
602e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor#include <string.h>
612e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor#include "apps.h"
622e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor#include <openssl/pem.h>
632e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor#include <openssl/err.h>
64e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor
652e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor#undef PROG
662e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor#define PROG nseq_main
672e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
682e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregorint MAIN(int, char **);
692e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
702e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregorint MAIN(int argc, char **argv)
712e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor{
722e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	char **args, *infile = NULL, *outfile = NULL;
73e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	BIO *in = NULL, *out = NULL;
74e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	int toseq = 0;
752e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	X509 *x509 = NULL;
76e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	NETSCAPE_CERT_SEQUENCE *seq = NULL;
77e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	int i, ret = 1;
78e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	int badarg = 0;
792e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);
80e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	ERR_load_crypto_strings();
81e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	args = argv + 1;
82e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	while (!badarg && *args && *args[0] == '-') {
832e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		if (!strcmp (*args, "-toseq")) toseq = 1;
84e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor		else if (!strcmp (*args, "-in")) {
85e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor			if (args[1]) {
86e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor				args++;
87e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor				infile = *args;
882e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor			} else badarg = 1;
892e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		} else if (!strcmp (*args, "-out")) {
902e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor			if (args[1]) {
912e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor				args++;
922e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor				outfile = *args;
932e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor			} else badarg = 1;
942e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		} else badarg = 1;
952e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		args++;
962e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	}
972e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
982e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	if (badarg) {
99e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor		BIO_printf (bio_err, "Netscape certificate sequence utility\n");
1002e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		BIO_printf (bio_err, "Usage nseq [options]\n");
1012e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		BIO_printf (bio_err, "where options are\n");
1022e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		BIO_printf (bio_err, "-in file  input file\n");
1032e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		BIO_printf (bio_err, "-out file output file\n");
1042e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		BIO_printf (bio_err, "-toseq    output NS Sequence file\n");
1052e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		OPENSSL_EXIT(1);
1062e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	}
1072e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump	if (infile) {
1092e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		if (!(in = BIO_new_file (infile, "r"))) {
1102e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor			BIO_printf (bio_err,
111ac8d75fe94f2aefde5179d53e230b99a1fe1201aChris Lattner				 "Can't open input file %s\n", infile);
1122e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor			goto end;
1132e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		}
114e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	} else in = BIO_new_fp(stdin, BIO_NOCLOSE);
115e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor
116e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor	if (outfile) {
117e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor		if (!(out = BIO_new_file (outfile, "w"))) {
118ac8d75fe94f2aefde5179d53e230b99a1fe1201aChris Lattner			BIO_printf (bio_err,
119e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor				 "Can't open output file %s\n", outfile);
120e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor			goto end;
1213e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt		}
1223e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt	} else {
1233e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt		out = BIO_new_fp(stdout, BIO_NOCLOSE);
1243e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt#ifdef OPENSSL_SYS_VMS
1253e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt		{
1263e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1272e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		out = BIO_push(tmpbio, out);
1282e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		}
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#endif
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump	}
1312e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	if (toseq) {
132e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor		seq = NETSCAPE_CERT_SEQUENCE_new();
133e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor		seq->certs = sk_X509_new_null();
134e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor		while((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
135e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor		    sk_X509_push(seq->certs,x509);
136e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor
1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump		if(!sk_X509_num(seq->certs))
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump		{
139e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor			BIO_printf (bio_err, "Error reading certs file %s\n", infile);
140e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor			ERR_print_errors(bio_err);
1412e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor			goto end;
1422e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		}
1433e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt		PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
1443e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt		ret = 0;
1453e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt		goto end;
1463e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt	}
1473e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt
1483e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt	if (!(seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL))) {
1492e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		BIO_printf (bio_err, "Error reading sequence file %s\n", infile);
1502e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		ERR_print_errors(bio_err);
1512e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor		goto end;
1522e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	}
1532a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
1542e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	for(i = 0; i < sk_X509_num(seq->certs); i++) {
1552def48394f6d48bde0dec2b514193c2b533265b5Douglas Gregor		x509 = sk_X509_value(seq->certs, i);
1562def48394f6d48bde0dec2b514193c2b533265b5Douglas Gregor		dump_cert_text(out, x509);
1572def48394f6d48bde0dec2b514193c2b533265b5Douglas Gregor		PEM_write_bio_X509(out, x509);
1582def48394f6d48bde0dec2b514193c2b533265b5Douglas Gregor	}
1592e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	ret = 0;
1602e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregorend:
1612e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	BIO_free(in);
1622e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	BIO_free_all(out);
1632e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor	NETSCAPE_CERT_SEQUENCE_free(seq);
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump	OPENSSL_EXIT(ret);
1662e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor}
1672e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
1682e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor