]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
fecb3aae | 2 | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
7e1b7485 | 8 | */ |
d02b48c6 RE |
9 | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <time.h> | |
14 | #include "apps.h" | |
dab2cd68 | 15 | #include "progs.h" |
ec577822 BM |
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> | |
d02b48c6 | 22 | |
7e1b7485 | 23 | typedef enum OPTION_choice { |
b0f96018 | 24 | OPT_COMMON, |
7e1b7485 | 25 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT, |
632e8be2 RF |
26 | OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_QUIET, |
27 | OPT_ENGINE, OPT_PROV_ENUM | |
7e1b7485 | 28 | } OPTION_CHOICE; |
d02b48c6 | 29 | |
44c83ebd | 30 | const OPTIONS pkcs7_options[] = { |
5388f986 | 31 | OPT_SECTION("General"), |
7e1b7485 | 32 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
33 | #ifndef OPENSSL_NO_ENGINE |
34 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
35 | #endif | |
36 | ||
37 | OPT_SECTION("Input"), | |
7e1b7485 | 38 | {"in", OPT_IN, '<', "Input file"}, |
5388f986 RS |
39 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, |
40 | ||
41 | OPT_SECTION("Output"), | |
7e1b7485 RS |
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"}, | |
12d56b29 | 46 | {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"}, |
7e1b7485 RS |
47 | {"print_certs", OPT_PRINT_CERTS, '-', |
48 | "Print_certs print any certs or crl in the input"}, | |
632e8be2 RF |
49 | {"quiet", OPT_QUIET, '-', |
50 | "When used with -print_certs, it produces a cleaner output"}, | |
6bd4e3f2 P |
51 | |
52 | OPT_PROV_OPTIONS, | |
7e1b7485 RS |
53 | {NULL} |
54 | }; | |
667ac4ec | 55 | |
7e1b7485 | 56 | int pkcs7_main(int argc, char **argv) |
0f113f3e | 57 | { |
dd1abd44 | 58 | ENGINE *e = NULL; |
90a1f2d7 | 59 | PKCS7 *p7 = NULL, *p7i; |
0f113f3e | 60 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 61 | int informat = FORMAT_PEM, outformat = FORMAT_PEM; |
333b070e | 62 | char *infile = NULL, *outfile = NULL, *prog; |
632e8be2 | 63 | int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, quiet = 0, ret = 1; |
7e1b7485 | 64 | OPTION_CHOICE o; |
b4250010 | 65 | OSSL_LIB_CTX *libctx = app_get0_libctx(); |
3f39976d | 66 | |
7e1b7485 RS |
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: | |
0f113f3e | 94 | noout = 1; |
7e1b7485 RS |
95 | break; |
96 | case OPT_TEXT: | |
0f113f3e | 97 | text = 1; |
7e1b7485 RS |
98 | break; |
99 | case OPT_PRINT: | |
0f113f3e | 100 | p7_print = 1; |
7e1b7485 RS |
101 | break; |
102 | case OPT_PRINT_CERTS: | |
0f113f3e | 103 | print_certs = 1; |
7e1b7485 | 104 | break; |
632e8be2 RF |
105 | case OPT_QUIET: |
106 | quiet = 1; | |
107 | break; | |
7e1b7485 | 108 | case OPT_ENGINE: |
dd1abd44 | 109 | e = setup_engine(opt_arg(), 0); |
0f113f3e | 110 | break; |
6bd4e3f2 P |
111 | case OPT_PROV_CASES: |
112 | if (!opt_provider(o)) | |
113 | goto end; | |
114 | break; | |
0f113f3e | 115 | } |
0f113f3e | 116 | } |
021410ea RS |
117 | |
118 | /* No extra arguments. */ | |
d9f07357 | 119 | if (!opt_check_rest_arg(NULL)) |
03358517 | 120 | goto opthelp; |
d02b48c6 | 121 | |
bdd58d98 | 122 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 | 123 | if (in == NULL) |
0f113f3e | 124 | goto end; |
d02b48c6 | 125 | |
7dc67708 | 126 | p7 = PKCS7_new_ex(libctx, app_get0_propq()); |
90a1f2d7 SL |
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 | ||
0f113f3e | 133 | if (informat == FORMAT_ASN1) |
90a1f2d7 | 134 | p7i = d2i_PKCS7_bio(in, &p7); |
7e1b7485 | 135 | else |
90a1f2d7 SL |
136 | p7i = PEM_read_bio_PKCS7(in, &p7, NULL, NULL); |
137 | if (p7i == NULL) { | |
0f113f3e MC |
138 | BIO_printf(bio_err, "unable to load PKCS7 object\n"); |
139 | ERR_print_errors(bio_err); | |
140 | goto end; | |
141 | } | |
d02b48c6 | 142 | |
bdd58d98 | 143 | out = bio_open_default(outfile, 'w', outformat); |
7e1b7485 RS |
144 | if (out == NULL) |
145 | goto end; | |
d02b48c6 | 146 | |
0f113f3e MC |
147 | if (p7_print) |
148 | PKCS7_print_ctx(out, p7, 0, NULL); | |
9194296d | 149 | |
0f113f3e MC |
150 | if (print_certs) { |
151 | STACK_OF(X509) *certs = NULL; | |
152 | STACK_OF(X509_CRL) *crls = NULL; | |
d02b48c6 | 153 | |
0f113f3e MC |
154 | i = OBJ_obj2nid(p7->type); |
155 | switch (i) { | |
156 | case NID_pkcs7_signed: | |
79356a83 RS |
157 | if (p7->d.sign != NULL) { |
158 | certs = p7->d.sign->cert; | |
159 | crls = p7->d.sign->crl; | |
160 | } | |
0f113f3e MC |
161 | break; |
162 | case NID_pkcs7_signedAndEnveloped: | |
79356a83 RS |
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 | } | |
0f113f3e MC |
167 | break; |
168 | default: | |
169 | break; | |
170 | } | |
d02b48c6 | 171 | |
0f113f3e MC |
172 | if (certs != NULL) { |
173 | X509 *x; | |
d02b48c6 | 174 | |
0f113f3e MC |
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); | |
632e8be2 | 179 | else if (!quiet) |
0f113f3e | 180 | dump_cert_text(out, x); |
d02b48c6 | 181 | |
0f113f3e MC |
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; | |
d02b48c6 | 189 | |
0f113f3e MC |
190 | for (i = 0; i < sk_X509_CRL_num(crls); i++) { |
191 | crl = sk_X509_CRL_value(crls, i); | |
d02b48c6 | 192 | |
b5c4209b | 193 | X509_CRL_print_ex(out, crl, get_nameopt()); |
d02b48c6 | 194 | |
0f113f3e MC |
195 | if (!noout) |
196 | PEM_write_bio_X509_CRL(out, crl); | |
197 | BIO_puts(out, "\n"); | |
198 | } | |
199 | } | |
d02b48c6 | 200 | |
0f113f3e MC |
201 | ret = 0; |
202 | goto end; | |
203 | } | |
d02b48c6 | 204 | |
0f113f3e MC |
205 | if (!noout) { |
206 | if (outformat == FORMAT_ASN1) | |
207 | i = i2d_PKCS7_bio(out, p7); | |
7e1b7485 | 208 | else |
0f113f3e | 209 | i = PEM_write_bio_PKCS7(out, p7); |
d02b48c6 | 210 | |
0f113f3e MC |
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: | |
e0e920b1 | 219 | PKCS7_free(p7); |
dd1abd44 | 220 | release_engine(e); |
ca3a82c3 RS |
221 | BIO_free(in); |
222 | BIO_free_all(out); | |
26a7d938 | 223 | return ret; |
0f113f3e | 224 | } |