Add "sections" to -help output
[openssl.git] / apps / pkcs7.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
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
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
14 #include "apps.h"
15 #include "progs.h"
16 #include <openssl/err.h>
17 #include <openssl/objects.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509.h>
20 #include <openssl/pkcs7.h>
21 #include <openssl/pem.h>
22
23 typedef enum OPTION_choice {
24     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
26     OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE
27 } OPTION_CHOICE;
28
29 const OPTIONS pkcs7_options[] = {
30     OPT_SECTION("General"),
31     {"help", OPT_HELP, '-', "Display this summary"},
32 #ifndef OPENSSL_NO_ENGINE
33     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
34 #endif
35
36     OPT_SECTION("Input"),
37     {"in", OPT_IN, '<', "Input file"},
38     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
39
40     OPT_SECTION("Output"),
41     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
42     {"out", OPT_OUT, '>', "Output file"},
43     {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
44     {"text", OPT_TEXT, '-', "Print full details of certificates"},
45     {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"},
46     {"print_certs", OPT_PRINT_CERTS, '-',
47      "Print_certs  print any certs or crl in the input"},
48     {NULL}
49 };
50
51 int pkcs7_main(int argc, char **argv)
52 {
53     ENGINE *e = NULL;
54     PKCS7 *p7 = NULL;
55     BIO *in = NULL, *out = NULL;
56     int informat = FORMAT_PEM, outformat = FORMAT_PEM;
57     char *infile = NULL, *outfile = NULL, *prog;
58     int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
59     OPTION_CHOICE o;
60
61     prog = opt_init(argc, argv, pkcs7_options);
62     while ((o = opt_next()) != OPT_EOF) {
63         switch (o) {
64         case OPT_EOF:
65         case OPT_ERR:
66  opthelp:
67             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
68             goto end;
69         case OPT_HELP:
70             opt_help(pkcs7_options);
71             ret = 0;
72             goto end;
73         case OPT_INFORM:
74             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
75                 goto opthelp;
76             break;
77         case OPT_OUTFORM:
78             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
79                 goto opthelp;
80             break;
81         case OPT_IN:
82             infile = opt_arg();
83             break;
84         case OPT_OUT:
85             outfile = opt_arg();
86             break;
87         case OPT_NOOUT:
88             noout = 1;
89             break;
90         case OPT_TEXT:
91             text = 1;
92             break;
93         case OPT_PRINT:
94             p7_print = 1;
95             break;
96         case OPT_PRINT_CERTS:
97             print_certs = 1;
98             break;
99         case OPT_ENGINE:
100             e = setup_engine(opt_arg(), 0);
101             break;
102         }
103     }
104     argc = opt_num_rest();
105     if (argc != 0)
106         goto opthelp;
107
108     in = bio_open_default(infile, 'r', informat);
109     if (in == NULL)
110         goto end;
111
112     if (informat == FORMAT_ASN1)
113         p7 = d2i_PKCS7_bio(in, NULL);
114     else
115         p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
116     if (p7 == NULL) {
117         BIO_printf(bio_err, "unable to load PKCS7 object\n");
118         ERR_print_errors(bio_err);
119         goto end;
120     }
121
122     out = bio_open_default(outfile, 'w', outformat);
123     if (out == NULL)
124         goto end;
125
126     if (p7_print)
127         PKCS7_print_ctx(out, p7, 0, NULL);
128
129     if (print_certs) {
130         STACK_OF(X509) *certs = NULL;
131         STACK_OF(X509_CRL) *crls = NULL;
132
133         i = OBJ_obj2nid(p7->type);
134         switch (i) {
135         case NID_pkcs7_signed:
136             if (p7->d.sign != NULL) {
137                 certs = p7->d.sign->cert;
138                 crls = p7->d.sign->crl;
139             }
140             break;
141         case NID_pkcs7_signedAndEnveloped:
142             if (p7->d.signed_and_enveloped != NULL) {
143                 certs = p7->d.signed_and_enveloped->cert;
144                 crls = p7->d.signed_and_enveloped->crl;
145             }
146             break;
147         default:
148             break;
149         }
150
151         if (certs != NULL) {
152             X509 *x;
153
154             for (i = 0; i < sk_X509_num(certs); i++) {
155                 x = sk_X509_value(certs, i);
156                 if (text)
157                     X509_print(out, x);
158                 else
159                     dump_cert_text(out, x);
160
161                 if (!noout)
162                     PEM_write_bio_X509(out, x);
163                 BIO_puts(out, "\n");
164             }
165         }
166         if (crls != NULL) {
167             X509_CRL *crl;
168
169             for (i = 0; i < sk_X509_CRL_num(crls); i++) {
170                 crl = sk_X509_CRL_value(crls, i);
171
172                 X509_CRL_print_ex(out, crl, get_nameopt());
173
174                 if (!noout)
175                     PEM_write_bio_X509_CRL(out, crl);
176                 BIO_puts(out, "\n");
177             }
178         }
179
180         ret = 0;
181         goto end;
182     }
183
184     if (!noout) {
185         if (outformat == FORMAT_ASN1)
186             i = i2d_PKCS7_bio(out, p7);
187         else
188             i = PEM_write_bio_PKCS7(out, p7);
189
190         if (!i) {
191             BIO_printf(bio_err, "unable to write pkcs7 object\n");
192             ERR_print_errors(bio_err);
193             goto end;
194         }
195     }
196     ret = 0;
197  end:
198     PKCS7_free(p7);
199     release_engine(e);
200     BIO_free(in);
201     BIO_free_all(out);
202     return ret;
203 }