Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1 | /* |
Matt Caswell | aff636a | 2021-05-06 13:03:23 +0100 | [diff] [blame] | 2 | * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved. |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 3 | * |
Richard Levitte | dffa752 | 2018-12-06 13:00:26 +0100 | [diff] [blame] | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Ulf Möller | 95dc05b | 1999-04-20 22:50:42 +0000 | [diff] [blame] | 11 | #include <string.h> |
Lutz Jänicke | 7b63c0f | 2002-07-10 07:01:54 +0000 | [diff] [blame] | 12 | #include "apps.h" |
Richard Levitte | dab2cd6 | 2018-01-31 11:13:10 +0100 | [diff] [blame] | 13 | #include "progs.h" |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 14 | #include <openssl/pem.h> |
| 15 | #include <openssl/err.h> |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 16 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 17 | typedef enum OPTION_choice { |
Dr. David von Oheimb | b0f9601 | 2021-05-01 15:29:00 +0200 | [diff] [blame] | 18 | OPT_COMMON, |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 19 | OPT_TOSEQ, OPT_IN, OPT_OUT, |
| 20 | OPT_PROV_ENUM |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 21 | } OPTION_CHOICE; |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 22 | |
FdaSilvaYY | 44c83eb | 2016-03-13 14:07:50 +0100 | [diff] [blame] | 23 | const OPTIONS nseq_options[] = { |
Rich Salz | 5388f98 | 2019-11-08 06:08:30 +1000 | [diff] [blame] | 24 | OPT_SECTION("General"), |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 25 | {"help", OPT_HELP, '-', "Display this summary"}, |
Rich Salz | 5388f98 | 2019-11-08 06:08:30 +1000 | [diff] [blame] | 26 | |
| 27 | OPT_SECTION("Input"), |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 28 | {"in", OPT_IN, '<', "Input file"}, |
Rich Salz | 5388f98 | 2019-11-08 06:08:30 +1000 | [diff] [blame] | 29 | |
| 30 | OPT_SECTION("Output"), |
| 31 | {"toseq", OPT_TOSEQ, '-', "Output NS Sequence file"}, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 32 | {"out", OPT_OUT, '>', "Output file"}, |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 33 | |
| 34 | OPT_PROV_OPTIONS, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 35 | {NULL} |
| 36 | }; |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 37 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 38 | int nseq_main(int argc, char **argv) |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 39 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 40 | BIO *in = NULL, *out = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 41 | X509 *x509 = NULL; |
| 42 | NETSCAPE_CERT_SEQUENCE *seq = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 43 | OPTION_CHOICE o; |
| 44 | int toseq = 0, ret = 1, i; |
| 45 | char *infile = NULL, *outfile = NULL, *prog; |
| 46 | |
| 47 | prog = opt_init(argc, argv, nseq_options); |
| 48 | while ((o = opt_next()) != OPT_EOF) { |
| 49 | switch (o) { |
| 50 | case OPT_EOF: |
| 51 | case OPT_ERR: |
Kurt Roeckx | 0335851 | 2016-02-14 20:45:02 +0100 | [diff] [blame] | 52 | opthelp: |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 53 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 54 | goto end; |
| 55 | case OPT_HELP: |
| 56 | ret = 0; |
| 57 | opt_help(nseq_options); |
| 58 | goto end; |
| 59 | case OPT_TOSEQ: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 60 | toseq = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 61 | break; |
| 62 | case OPT_IN: |
| 63 | infile = opt_arg(); |
| 64 | break; |
| 65 | case OPT_OUT: |
| 66 | outfile = opt_arg(); |
| 67 | break; |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 68 | case OPT_PROV_CASES: |
| 69 | if (!opt_provider(o)) |
| 70 | goto end; |
| 71 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 72 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 73 | } |
Rich Salz | 021410e | 2020-11-28 16:12:58 -0500 | [diff] [blame] | 74 | |
| 75 | /* No extra arguments. */ |
Dr. David von Oheimb | d9f0735 | 2021-08-27 15:33:18 +0200 | [diff] [blame] | 76 | if (!opt_check_rest_arg(NULL)) |
Kurt Roeckx | 0335851 | 2016-02-14 20:45:02 +0100 | [diff] [blame] | 77 | goto opthelp; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 78 | |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 79 | in = bio_open_default(infile, 'r', FORMAT_PEM); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 80 | if (in == NULL) |
| 81 | goto end; |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 82 | out = bio_open_default(outfile, 'w', FORMAT_PEM); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 83 | if (out == NULL) |
| 84 | goto end; |
| 85 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 86 | if (toseq) { |
| 87 | seq = NETSCAPE_CERT_SEQUENCE_new(); |
Matt Caswell | 96487cd | 2015-10-30 11:18:04 +0000 | [diff] [blame] | 88 | if (seq == NULL) |
| 89 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 90 | seq->certs = sk_X509_new_null(); |
Matt Caswell | 96487cd | 2015-10-30 11:18:04 +0000 | [diff] [blame] | 91 | if (seq->certs == NULL) |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 92 | goto end; |
Shane Lontis | d5e66ea | 2020-02-04 13:50:51 +1000 | [diff] [blame] | 93 | while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 94 | if (!sk_X509_push(seq->certs, x509)) |
| 95 | goto end; |
| 96 | } |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 97 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 98 | if (!sk_X509_num(seq->certs)) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 99 | BIO_printf(bio_err, "%s: Error reading certs file %s\n", |
| 100 | prog, infile); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 101 | ERR_print_errors(bio_err); |
| 102 | goto end; |
| 103 | } |
| 104 | PEM_write_bio_NETSCAPE_CERT_SEQUENCE(out, seq); |
| 105 | ret = 0; |
| 106 | goto end; |
| 107 | } |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 108 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 109 | seq = PEM_read_bio_NETSCAPE_CERT_SEQUENCE(in, NULL, NULL, NULL); |
| 110 | if (seq == NULL) { |
| 111 | BIO_printf(bio_err, "%s: Error reading sequence file %s\n", |
| 112 | prog, infile); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 113 | ERR_print_errors(bio_err); |
| 114 | goto end; |
| 115 | } |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 116 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 117 | for (i = 0; i < sk_X509_num(seq->certs); i++) { |
| 118 | x509 = sk_X509_value(seq->certs, i); |
| 119 | dump_cert_text(out, x509); |
| 120 | PEM_write_bio_X509(out, x509); |
| 121 | } |
| 122 | ret = 0; |
| 123 | end: |
| 124 | BIO_free(in); |
| 125 | BIO_free_all(out); |
| 126 | NETSCAPE_CERT_SEQUENCE_free(seq); |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 127 | |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 128 | return ret; |
Dr. Stephen Henson | 79dfa97 | 1999-01-29 23:34:19 +0000 | [diff] [blame] | 129 | } |