Home | History | Annotate | Line # | Download | only in apps
      1      1.1  christos /*
      2  1.1.1.2  christos  * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4  1.1.1.2  christos  * Licensed under the OpenSSL license (the "License").  You may not use
      5  1.1.1.2  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1.1.2  christos  * in the file LICENSE in the source distribution or at
      7  1.1.1.2  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include <stdio.h>
     11      1.1  christos #include <string.h>
     12      1.1  christos #include "apps.h"
     13  1.1.1.2  christos #include "progs.h"
     14      1.1  christos #include <openssl/pem.h>
     15      1.1  christos #include <openssl/err.h>
     16      1.1  christos 
     17  1.1.1.2  christos typedef enum OPTION_choice {
     18  1.1.1.2  christos     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
     19  1.1.1.2  christos     OPT_TOSEQ, OPT_IN, OPT_OUT
     20  1.1.1.2  christos } OPTION_CHOICE;
     21  1.1.1.2  christos 
     22  1.1.1.2  christos const OPTIONS nseq_options[] = {
     23  1.1.1.2  christos     {"help", OPT_HELP, '-', "Display this summary"},
     24  1.1.1.2  christos     {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"},
     25  1.1.1.2  christos     {"in", OPT_IN, '<', "Input file"},
     26  1.1.1.2  christos     {"out", OPT_OUT, '>', "Output file"},
     27  1.1.1.2  christos     {NULL}
     28  1.1.1.2  christos };
     29      1.1  christos 
     30  1.1.1.2  christos int nseq_main(int argc, char **argv)
     31      1.1  christos {
     32      1.1  christos     BIO *in = NULL, *out = NULL;
     33      1.1  christos     X509 *x509 = NULL;
     34      1.1  christos     NETSCAPE_CERT_SEQUENCE *seq = NULL;
     35  1.1.1.2  christos     OPTION_CHOICE o;
     36  1.1.1.2  christos     int toseq = 0, ret = 1, i;
     37  1.1.1.2  christos     char *infile = NULL, *outfile = NULL, *prog;
     38  1.1.1.2  christos 
     39  1.1.1.2  christos     prog = opt_init(argc, argv, nseq_options);
     40  1.1.1.2  christos     while ((o = opt_next()) != OPT_EOF) {
     41  1.1.1.2  christos         switch (o) {
     42  1.1.1.2  christos         case OPT_EOF:
     43  1.1.1.2  christos         case OPT_ERR:
     44  1.1.1.2  christos  opthelp:
     45  1.1.1.2  christos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
     46      1.1  christos             goto end;
     47  1.1.1.2  christos         case OPT_HELP:
     48  1.1.1.2  christos             ret = 0;
     49  1.1.1.2  christos             opt_help(nseq_options);
     50      1.1  christos             goto end;
     51  1.1.1.2  christos         case OPT_TOSEQ:
     52  1.1.1.2  christos             toseq = 1;
     53  1.1.1.2  christos             break;
     54  1.1.1.2  christos         case OPT_IN:
     55  1.1.1.2  christos             infile = opt_arg();
     56  1.1.1.2  christos             break;
     57  1.1.1.2  christos         case OPT_OUT:
     58  1.1.1.2  christos             outfile = opt_arg();
     59  1.1.1.2  christos             break;
     60      1.1  christos         }
     61      1.1  christos     }
     62  1.1.1.2  christos     argc = opt_num_rest();
     63  1.1.1.2  christos     if (argc != 0)
     64  1.1.1.2  christos         goto opthelp;
     65  1.1.1.2  christos 
     66  1.1.1.2  christos     in = bio_open_default(infile, 'r', FORMAT_PEM);
     67  1.1.1.2  christos     if (in == NULL)
     68  1.1.1.2  christos         goto end;
     69  1.1.1.2  christos     out = bio_open_default(outfile, 'w', FORMAT_PEM);
     70  1.1.1.2  christos     if (out == NULL)
     71  1.1.1.2  christos         goto end;
     72  1.1.1.2  christos 
     73      1.1  christos     if (toseq) {
     74      1.1  christos         seq = NETSCAPE_CERT_SEQUENCE_new();
     75  1.1.1.2  christos         if (seq == NULL)
     76  1.1.1.2  christos             goto end;
     77      1.1  christos         seq->certs = sk_X509_new_null();
     78  1.1.1.2  christos         if (seq->certs == NULL)
     79  1.1.1.2  christos             goto end;
     80      1.1  christos         while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
     81      1.1  christos             sk_X509_push(seq->certs, x509);
     82      1.1  christos 
     83      1.1  christos         if (!sk_X509_num(seq->certs)) {
     84  1.1.1.2  christos             BIO_printf(bio_err, "%s: Error reading certs file %s\n",
     85  1.1.1.2  christos                        prog, infile);
     86      1.1  christos             ERR_print_errors(bio_err);
     87      1.1  christos             goto end;
     88      1.1  christos         }
     89      1.1  christos         PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
     90      1.1  christos         ret = 0;
     91      1.1  christos         goto end;
     92      1.1  christos     }
     93      1.1  christos 
     94  1.1.1.2  christos     seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL);
     95  1.1.1.2  christos     if (seq == NULL) {
     96  1.1.1.2  christos         BIO_printf(bio_err, "%s: Error reading sequence file %s\n",
     97  1.1.1.2  christos                    prog, infile);
     98      1.1  christos         ERR_print_errors(bio_err);
     99      1.1  christos         goto end;
    100      1.1  christos     }
    101      1.1  christos 
    102      1.1  christos     for (i = 0; i < sk_X509_num(seq->certs); i++) {
    103      1.1  christos         x509 = sk_X509_value(seq->certs, i);
    104      1.1  christos         dump_cert_text(out, x509);
    105      1.1  christos         PEM_write_bio_X509(out, x509);
    106      1.1  christos     }
    107      1.1  christos     ret = 0;
    108      1.1  christos  end:
    109      1.1  christos     BIO_free(in);
    110      1.1  christos     BIO_free_all(out);
    111      1.1  christos     NETSCAPE_CERT_SEQUENCE_free(seq);
    112      1.1  christos 
    113  1.1.1.2  christos     return ret;
    114      1.1  christos }
    115