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