]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/pkcs7.c
Reduce optimization in hppa builds
[thirdparty/openssl.git] / apps / pkcs7.c
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, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
26 OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_QUIET,
27 OPT_ENGINE, 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 {"quiet", OPT_QUIET, '-',
50 "When used with -print_certs, it produces a cleaner output"},
51
52 OPT_PROV_OPTIONS,
53 {NULL}
54 };
55
56 int pkcs7_main(int argc, char **argv)
57 {
58 ENGINE *e = NULL;
59 PKCS7 *p7 = NULL, *p7i;
60 BIO *in = NULL, *out = NULL;
61 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
62 char *infile = NULL, *outfile = NULL, *prog;
63 int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, quiet = 0, ret = 1;
64 OPTION_CHOICE o;
65 OSSL_LIB_CTX *libctx = app_get0_libctx();
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_QUIET:
106 quiet = 1;
107 break;
108 case OPT_ENGINE:
109 e = setup_engine(opt_arg(), 0);
110 break;
111 case OPT_PROV_CASES:
112 if (!opt_provider(o))
113 goto end;
114 break;
115 }
116 }
117
118 /* No extra arguments. */
119 if (!opt_check_rest_arg(NULL))
120 goto opthelp;
121
122 in = bio_open_default(infile, 'r', informat);
123 if (in == NULL)
124 goto end;
125
126 p7 = PKCS7_new_ex(libctx, app_get0_propq());
127 if (p7 == NULL) {
128 BIO_printf(bio_err, "unable to allocate PKCS7 object\n");
129 ERR_print_errors(bio_err);
130 goto end;
131 }
132
133 if (informat == FORMAT_ASN1)
134 p7i = d2i_PKCS7_bio(in, &p7);
135 else
136 p7i = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
137 if (p7i == NULL) {
138 BIO_printf(bio_err, "unable to load PKCS7 object\n");
139 ERR_print_errors(bio_err);
140 goto end;
141 }
142
143 out = bio_open_default(outfile, 'w', outformat);
144 if (out == NULL)
145 goto end;
146
147 if (p7_print)
148 PKCS7_print_ctx(out, p7, 0, NULL);
149
150 if (print_certs) {
151 STACK_OF(X509) *certs = NULL;
152 STACK_OF(X509_CRL) *crls = NULL;
153
154 i = OBJ_obj2nid(p7->type);
155 switch (i) {
156 case NID_pkcs7_signed:
157 if (p7->d.sign != NULL) {
158 certs = p7->d.sign->cert;
159 crls = p7->d.sign->crl;
160 }
161 break;
162 case NID_pkcs7_signedAndEnveloped:
163 if (p7->d.signed_and_enveloped != NULL) {
164 certs = p7->d.signed_and_enveloped->cert;
165 crls = p7->d.signed_and_enveloped->crl;
166 }
167 break;
168 default:
169 break;
170 }
171
172 if (certs != NULL) {
173 X509 *x;
174
175 for (i = 0; i < sk_X509_num(certs); i++) {
176 x = sk_X509_value(certs, i);
177 if (text)
178 X509_print(out, x);
179 else if (!quiet)
180 dump_cert_text(out, x);
181
182 if (!noout)
183 PEM_write_bio_X509(out, x);
184 BIO_puts(out, "\n");
185 }
186 }
187 if (crls != NULL) {
188 X509_CRL *crl;
189
190 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
191 crl = sk_X509_CRL_value(crls, i);
192
193 X509_CRL_print_ex(out, crl, get_nameopt());
194
195 if (!noout)
196 PEM_write_bio_X509_CRL(out, crl);
197 BIO_puts(out, "\n");
198 }
199 }
200
201 ret = 0;
202 goto end;
203 }
204
205 if (!noout) {
206 if (outformat == FORMAT_ASN1)
207 i = i2d_PKCS7_bio(out, p7);
208 else
209 i = PEM_write_bio_PKCS7(out, p7);
210
211 if (!i) {
212 BIO_printf(bio_err, "unable to write pkcs7 object\n");
213 ERR_print_errors(bio_err);
214 goto end;
215 }
216 }
217 ret = 0;
218 end:
219 PKCS7_free(p7);
220 release_engine(e);
221 BIO_free(in);
222 BIO_free_all(out);
223 return ret;
224 }