Main Page | Class List | Directories | File List | Class Members | File Members

nseq.c

Go to the documentation of this file.
00001 /* nseq.c */
00002 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
00003  * project 1999.
00004  */
00005 /* ====================================================================
00006  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer. 
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in
00017  *    the documentation and/or other materials provided with the
00018  *    distribution.
00019  *
00020  * 3. All advertising materials mentioning features or use of this
00021  *    software must display the following acknowledgment:
00022  *    "This product includes software developed by the OpenSSL Project
00023  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
00024  *
00025  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
00026  *    endorse or promote products derived from this software without
00027  *    prior written permission. For written permission, please contact
00028  *    licensing@OpenSSL.org.
00029  *
00030  * 5. Products derived from this software may not be called "OpenSSL"
00031  *    nor may "OpenSSL" appear in their names without prior written
00032  *    permission of the OpenSSL Project.
00033  *
00034  * 6. Redistributions of any form whatsoever must retain the following
00035  *    acknowledgment:
00036  *    "This product includes software developed by the OpenSSL Project
00037  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
00040  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00041  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00042  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
00043  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00044  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00045  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00046  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00048  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00049  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00050  * OF THE POSSIBILITY OF SUCH DAMAGE.
00051  * ====================================================================
00052  *
00053  * This product includes cryptographic software written by Eric Young
00054  * (eay@cryptsoft.com).  This product includes software written by Tim
00055  * Hudson (tjh@cryptsoft.com).
00056  *
00057  */
00058 
00059 #include <stdio.h>
00060 #include <string.h>
00061 #include "apps.h"
00062 #include <openssl/pem.h>
00063 #include <openssl/err.h>
00064 
00065 #undef PROG
00066 #define PROG nseq_main
00067 
00068 int MAIN(int, char **);
00069 
00070 int MAIN(int argc, char **argv)
00071 {
00072         char **args, *infile = NULL, *outfile = NULL;
00073         BIO *in = NULL, *out = NULL;
00074         int toseq = 0;
00075         X509 *x509 = NULL;
00076         NETSCAPE_CERT_SEQUENCE *seq = NULL;
00077         int i, ret = 1;
00078         int badarg = 0;
00079         if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);
00080         ERR_load_crypto_strings();
00081         args = argv + 1;
00082         while (!badarg && *args && *args[0] == '-') {
00083                 if (!strcmp (*args, "-toseq")) toseq = 1;
00084                 else if (!strcmp (*args, "-in")) {
00085                         if (args[1]) {
00086                                 args++;
00087                                 infile = *args;
00088                         } else badarg = 1;
00089                 } else if (!strcmp (*args, "-out")) {
00090                         if (args[1]) {
00091                                 args++;
00092                                 outfile = *args;
00093                         } else badarg = 1;
00094                 } else badarg = 1;
00095                 args++;
00096         }
00097 
00098         if (badarg) {
00099                 BIO_printf (bio_err, "Netscape certificate sequence utility\n");
00100                 BIO_printf (bio_err, "Usage nseq [options]\n");
00101                 BIO_printf (bio_err, "where options are\n");
00102                 BIO_printf (bio_err, "-in file  input file\n");
00103                 BIO_printf (bio_err, "-out file output file\n");
00104                 BIO_printf (bio_err, "-toseq    output NS Sequence file\n");
00105                 OPENSSL_EXIT(1);
00106         }
00107 
00108         if (infile) {
00109                 if (!(in = BIO_new_file (infile, "r"))) {
00110                         BIO_printf (bio_err,
00111                                  "Can't open input file %s\n", infile);
00112                         goto end;
00113                 }
00114         } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
00115 
00116         if (outfile) {
00117                 if (!(out = BIO_new_file (outfile, "w"))) {
00118                         BIO_printf (bio_err,
00119                                  "Can't open output file %s\n", outfile);
00120                         goto end;
00121                 }
00122         } else {
00123                 out = BIO_new_fp(stdout, BIO_NOCLOSE);
00124 #ifdef OPENSSL_SYS_VMS
00125                 {
00126                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00127                 out = BIO_push(tmpbio, out);
00128                 }
00129 #endif
00130         }
00131         if (toseq) {
00132                 seq = NETSCAPE_CERT_SEQUENCE_new();
00133                 seq->certs = sk_X509_new_null();
00134                 while((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) 
00135                     sk_X509_push(seq->certs,x509);
00136 
00137                 if(!sk_X509_num(seq->certs))
00138                 {
00139                         BIO_printf (bio_err, "Error reading certs file %s\n", infile);
00140                         ERR_print_errors(bio_err);
00141                         goto end;
00142                 }
00143                 PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
00144                 ret = 0;
00145                 goto end;
00146         }
00147 
00148         if (!(seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL))) {
00149                 BIO_printf (bio_err, "Error reading sequence file %s\n", infile);
00150                 ERR_print_errors(bio_err);
00151                 goto end;
00152         }
00153 
00154         for(i = 0; i < sk_X509_num(seq->certs); i++) {
00155                 x509 = sk_X509_value(seq->certs, i);
00156                 dump_cert_text(out, x509);
00157                 PEM_write_bio_X509(out, x509);
00158         }
00159         ret = 0;
00160 end:
00161         BIO_free(in);
00162         BIO_free_all(out);
00163         NETSCAPE_CERT_SEQUENCE_free(seq);
00164 
00165         OPENSSL_EXIT(ret);
00166 }
00167 

© sourcejam.com 2005-2008