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