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