]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/pkcs7.c
In OpenSSL builds, declare STACK for datatypes ...
[thirdparty/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;
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
67 prog = opt_init(argc, argv, pkcs7_options);
68 while ((o = opt_next()) != OPT_EOF) {
69 switch (o) {
70 case OPT_EOF:
71 case OPT_ERR:
72 opthelp:
73 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74 goto end;
75 case OPT_HELP:
76 opt_help(pkcs7_options);
77 ret = 0;
78 goto end;
79 case OPT_INFORM:
80 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
81 goto opthelp;
82 break;
83 case OPT_OUTFORM:
84 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
85 goto opthelp;
86 break;
87 case OPT_IN:
88 infile = opt_arg();
89 break;
90 case OPT_OUT:
91 outfile = opt_arg();
92 break;
93 case OPT_NOOUT:
94 noout = 1;
95 break;
96 case OPT_TEXT:
97 text = 1;
98 break;
99 case OPT_PRINT:
100 p7_print = 1;
101 break;
102 case OPT_PRINT_CERTS:
103 print_certs = 1;
104 break;
105 case OPT_ENGINE:
106 e = setup_engine(opt_arg(), 0);
107 break;
108 case OPT_PROV_CASES:
109 if (!opt_provider(o))
110 goto end;
111 break;
112 }
113 }
114 argc = opt_num_rest();
115 if (argc != 0)
116 goto opthelp;
117
118 in = bio_open_default(infile, 'r', informat);
119 if (in == NULL)
120 goto end;
121
122 if (informat == FORMAT_ASN1)
123 p7 = d2i_PKCS7_bio(in, NULL);
124 else
125 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
126 if (p7 == NULL) {
127 BIO_printf(bio_err, "unable to load PKCS7 object\n");
128 ERR_print_errors(bio_err);
129 goto end;
130 }
131
132 out = bio_open_default(outfile, 'w', outformat);
133 if (out == NULL)
134 goto end;
135
136 if (p7_print)
137 PKCS7_print_ctx(out, p7, 0, NULL);
138
139 if (print_certs) {
140 STACK_OF(X509) *certs = NULL;
141 STACK_OF(X509_CRL) *crls = NULL;
142
143 i = OBJ_obj2nid(p7->type);
144 switch (i) {
145 case NID_pkcs7_signed:
146 if (p7->d.sign != NULL) {
147 certs = p7->d.sign->cert;
148 crls = p7->d.sign->crl;
149 }
150 break;
151 case NID_pkcs7_signedAndEnveloped:
152 if (p7->d.signed_and_enveloped != NULL) {
153 certs = p7->d.signed_and_enveloped->cert;
154 crls = p7->d.signed_and_enveloped->crl;
155 }
156 break;
157 default:
158 break;
159 }
160
161 if (certs != NULL) {
162 X509 *x;
163
164 for (i = 0; i < sk_X509_num(certs); i++) {
165 x = sk_X509_value(certs, i);
166 if (text)
167 X509_print(out, x);
168 else
169 dump_cert_text(out, x);
170
171 if (!noout)
172 PEM_write_bio_X509(out, x);
173 BIO_puts(out, "\n");
174 }
175 }
176 if (crls != NULL) {
177 X509_CRL *crl;
178
179 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
180 crl = sk_X509_CRL_value(crls, i);
181
182 X509_CRL_print_ex(out, crl, get_nameopt());
183
184 if (!noout)
185 PEM_write_bio_X509_CRL(out, crl);
186 BIO_puts(out, "\n");
187 }
188 }
189
190 ret = 0;
191 goto end;
192 }
193
194 if (!noout) {
195 if (outformat == FORMAT_ASN1)
196 i = i2d_PKCS7_bio(out, p7);
197 else
198 i = PEM_write_bio_PKCS7(out, p7);
199
200 if (!i) {
201 BIO_printf(bio_err, "unable to write pkcs7 object\n");
202 ERR_print_errors(bio_err);
203 goto end;
204 }
205 }
206 ret = 0;
207 end:
208 PKCS7_free(p7);
209 release_engine(e);
210 BIO_free(in);
211 BIO_free_all(out);
212 return ret;
213 }