00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
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