1*cca6fc52SDaniel Fojt /* $OpenBSD: nseq.c,v 1.9 2019/07/14 03:30:46 guenther Exp $ */
2f5b1c8a1SJohn Marino /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3f5b1c8a1SJohn Marino * project 1999.
4f5b1c8a1SJohn Marino */
5f5b1c8a1SJohn Marino /* ====================================================================
6f5b1c8a1SJohn Marino * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7f5b1c8a1SJohn Marino *
8f5b1c8a1SJohn Marino * Redistribution and use in source and binary forms, with or without
9f5b1c8a1SJohn Marino * modification, are permitted provided that the following conditions
10f5b1c8a1SJohn Marino * are met:
11f5b1c8a1SJohn Marino *
12f5b1c8a1SJohn Marino * 1. Redistributions of source code must retain the above copyright
13f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer.
14f5b1c8a1SJohn Marino *
15f5b1c8a1SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
16f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer in
17f5b1c8a1SJohn Marino * the documentation and/or other materials provided with the
18f5b1c8a1SJohn Marino * distribution.
19f5b1c8a1SJohn Marino *
20f5b1c8a1SJohn Marino * 3. All advertising materials mentioning features or use of this
21f5b1c8a1SJohn Marino * software must display the following acknowledgment:
22f5b1c8a1SJohn Marino * "This product includes software developed by the OpenSSL Project
23f5b1c8a1SJohn Marino * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24f5b1c8a1SJohn Marino *
25f5b1c8a1SJohn Marino * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26f5b1c8a1SJohn Marino * endorse or promote products derived from this software without
27f5b1c8a1SJohn Marino * prior written permission. For written permission, please contact
28f5b1c8a1SJohn Marino * licensing@OpenSSL.org.
29f5b1c8a1SJohn Marino *
30f5b1c8a1SJohn Marino * 5. Products derived from this software may not be called "OpenSSL"
31f5b1c8a1SJohn Marino * nor may "OpenSSL" appear in their names without prior written
32f5b1c8a1SJohn Marino * permission of the OpenSSL Project.
33f5b1c8a1SJohn Marino *
34f5b1c8a1SJohn Marino * 6. Redistributions of any form whatsoever must retain the following
35f5b1c8a1SJohn Marino * acknowledgment:
36f5b1c8a1SJohn Marino * "This product includes software developed by the OpenSSL Project
37f5b1c8a1SJohn Marino * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38f5b1c8a1SJohn Marino *
39f5b1c8a1SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40f5b1c8a1SJohn Marino * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41f5b1c8a1SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42f5b1c8a1SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43f5b1c8a1SJohn Marino * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44f5b1c8a1SJohn Marino * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45f5b1c8a1SJohn Marino * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46f5b1c8a1SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f5b1c8a1SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48f5b1c8a1SJohn Marino * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49f5b1c8a1SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50f5b1c8a1SJohn Marino * OF THE POSSIBILITY OF SUCH DAMAGE.
51f5b1c8a1SJohn Marino * ====================================================================
52f5b1c8a1SJohn Marino *
53f5b1c8a1SJohn Marino * This product includes cryptographic software written by Eric Young
54f5b1c8a1SJohn Marino * (eay@cryptsoft.com). This product includes software written by Tim
55f5b1c8a1SJohn Marino * Hudson (tjh@cryptsoft.com).
56f5b1c8a1SJohn Marino *
57f5b1c8a1SJohn Marino */
58f5b1c8a1SJohn Marino
59f5b1c8a1SJohn Marino #include <stdio.h>
60f5b1c8a1SJohn Marino #include <string.h>
61f5b1c8a1SJohn Marino
62f5b1c8a1SJohn Marino #include "apps.h"
63f5b1c8a1SJohn Marino
64f5b1c8a1SJohn Marino #include <openssl/err.h>
65f5b1c8a1SJohn Marino #include <openssl/pem.h>
66f5b1c8a1SJohn Marino
67f5b1c8a1SJohn Marino static struct {
68f5b1c8a1SJohn Marino char *infile;
69f5b1c8a1SJohn Marino char *outfile;
70f5b1c8a1SJohn Marino int toseq;
71f5b1c8a1SJohn Marino } nseq_config;
72f5b1c8a1SJohn Marino
73*cca6fc52SDaniel Fojt static const struct option nseq_options[] = {
74f5b1c8a1SJohn Marino {
75f5b1c8a1SJohn Marino .name = "in",
76f5b1c8a1SJohn Marino .argname = "file",
77f5b1c8a1SJohn Marino .desc = "Input file to read from (default stdin)",
78f5b1c8a1SJohn Marino .type = OPTION_ARG,
79f5b1c8a1SJohn Marino .opt.arg = &nseq_config.infile,
80f5b1c8a1SJohn Marino },
81f5b1c8a1SJohn Marino {
82f5b1c8a1SJohn Marino .name = "out",
83f5b1c8a1SJohn Marino .argname = "file",
84f5b1c8a1SJohn Marino .desc = "Output file to write to (default stdout)",
85f5b1c8a1SJohn Marino .type = OPTION_ARG,
86f5b1c8a1SJohn Marino .opt.arg = &nseq_config.outfile,
87f5b1c8a1SJohn Marino },
88f5b1c8a1SJohn Marino {
89f5b1c8a1SJohn Marino .name = "toseq",
90f5b1c8a1SJohn Marino .desc = "Convert certificates to Netscape certificate sequence",
91f5b1c8a1SJohn Marino .type = OPTION_FLAG,
92f5b1c8a1SJohn Marino .opt.flag = &nseq_config.toseq,
93f5b1c8a1SJohn Marino },
94f5b1c8a1SJohn Marino { NULL },
95f5b1c8a1SJohn Marino };
96f5b1c8a1SJohn Marino
97f5b1c8a1SJohn Marino static void
nseq_usage()98f5b1c8a1SJohn Marino nseq_usage()
99f5b1c8a1SJohn Marino {
100f5b1c8a1SJohn Marino fprintf(stderr, "usage: nseq [-in file] [-out file] [-toseq]\n");
101f5b1c8a1SJohn Marino options_usage(nseq_options);
102f5b1c8a1SJohn Marino }
103f5b1c8a1SJohn Marino
104f5b1c8a1SJohn Marino int
nseq_main(int argc,char ** argv)105f5b1c8a1SJohn Marino nseq_main(int argc, char **argv)
106f5b1c8a1SJohn Marino {
107f5b1c8a1SJohn Marino BIO *in = NULL, *out = NULL;
108f5b1c8a1SJohn Marino X509 *x509 = NULL;
109f5b1c8a1SJohn Marino NETSCAPE_CERT_SEQUENCE *seq = NULL;
110f5b1c8a1SJohn Marino int i, ret = 1;
111f5b1c8a1SJohn Marino
112f5b1c8a1SJohn Marino if (single_execution) {
11372c33676SMaxim Ag if (pledge("stdio cpath wpath rpath", NULL) == -1) {
114f5b1c8a1SJohn Marino perror("pledge");
115f5b1c8a1SJohn Marino exit(1);
116f5b1c8a1SJohn Marino }
117f5b1c8a1SJohn Marino }
118f5b1c8a1SJohn Marino
119f5b1c8a1SJohn Marino memset(&nseq_config, 0, sizeof(nseq_config));
120f5b1c8a1SJohn Marino
121f5b1c8a1SJohn Marino if (options_parse(argc, argv, nseq_options, NULL, NULL) != 0) {
122f5b1c8a1SJohn Marino nseq_usage();
123f5b1c8a1SJohn Marino return (1);
124f5b1c8a1SJohn Marino }
125f5b1c8a1SJohn Marino
126f5b1c8a1SJohn Marino if (nseq_config.infile) {
127f5b1c8a1SJohn Marino if (!(in = BIO_new_file(nseq_config.infile, "r"))) {
128f5b1c8a1SJohn Marino BIO_printf(bio_err,
129f5b1c8a1SJohn Marino "Can't open input file %s\n", nseq_config.infile);
130f5b1c8a1SJohn Marino goto end;
131f5b1c8a1SJohn Marino }
132f5b1c8a1SJohn Marino } else
133f5b1c8a1SJohn Marino in = BIO_new_fp(stdin, BIO_NOCLOSE);
134f5b1c8a1SJohn Marino
135f5b1c8a1SJohn Marino if (nseq_config.outfile) {
136f5b1c8a1SJohn Marino if (!(out = BIO_new_file(nseq_config.outfile, "w"))) {
137f5b1c8a1SJohn Marino BIO_printf(bio_err,
138f5b1c8a1SJohn Marino "Can't open output file %s\n", nseq_config.outfile);
139f5b1c8a1SJohn Marino goto end;
140f5b1c8a1SJohn Marino }
141f5b1c8a1SJohn Marino } else {
142f5b1c8a1SJohn Marino out = BIO_new_fp(stdout, BIO_NOCLOSE);
143f5b1c8a1SJohn Marino }
144f5b1c8a1SJohn Marino if (nseq_config.toseq) {
145f5b1c8a1SJohn Marino seq = NETSCAPE_CERT_SEQUENCE_new();
146f5b1c8a1SJohn Marino seq->certs = sk_X509_new_null();
147f5b1c8a1SJohn Marino while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
148f5b1c8a1SJohn Marino sk_X509_push(seq->certs, x509);
149f5b1c8a1SJohn Marino
150f5b1c8a1SJohn Marino if (!sk_X509_num(seq->certs)) {
151f5b1c8a1SJohn Marino BIO_printf(bio_err, "Error reading certs file %s\n", nseq_config.infile);
152f5b1c8a1SJohn Marino ERR_print_errors(bio_err);
153f5b1c8a1SJohn Marino goto end;
154f5b1c8a1SJohn Marino }
155f5b1c8a1SJohn Marino PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq);
156f5b1c8a1SJohn Marino ret = 0;
157f5b1c8a1SJohn Marino goto end;
158f5b1c8a1SJohn Marino }
159f5b1c8a1SJohn Marino if (!(seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL))) {
160f5b1c8a1SJohn Marino BIO_printf(bio_err, "Error reading sequence file %s\n", nseq_config.infile);
161f5b1c8a1SJohn Marino ERR_print_errors(bio_err);
162f5b1c8a1SJohn Marino goto end;
163f5b1c8a1SJohn Marino }
164f5b1c8a1SJohn Marino for (i = 0; i < sk_X509_num(seq->certs); i++) {
165f5b1c8a1SJohn Marino x509 = sk_X509_value(seq->certs, i);
166f5b1c8a1SJohn Marino dump_cert_text(out, x509);
167f5b1c8a1SJohn Marino PEM_write_bio_X509(out, x509);
168f5b1c8a1SJohn Marino }
169f5b1c8a1SJohn Marino ret = 0;
170f5b1c8a1SJohn Marino end:
171f5b1c8a1SJohn Marino BIO_free(in);
172f5b1c8a1SJohn Marino BIO_free_all(out);
173f5b1c8a1SJohn Marino NETSCAPE_CERT_SEQUENCE_free(seq);
174f5b1c8a1SJohn Marino
175f5b1c8a1SJohn Marino return (ret);
176f5b1c8a1SJohn Marino }
177