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