]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 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 RS |
23 | typedef enum OPTION_choice { |
24 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
25 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT, | |
6bd4e3f2 P |
26 | OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE, |
27 | 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"}, | |
6bd4e3f2 P |
49 | |
50 | OPT_PROV_OPTIONS, | |
7e1b7485 RS |
51 | {NULL} |
52 | }; | |
667ac4ec | 53 | |
7e1b7485 | 54 | int pkcs7_main(int argc, char **argv) |
0f113f3e | 55 | { |
dd1abd44 | 56 | ENGINE *e = NULL; |
90a1f2d7 | 57 | PKCS7 *p7 = NULL, *p7i; |
0f113f3e | 58 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 59 | int informat = FORMAT_PEM, outformat = FORMAT_PEM; |
333b070e | 60 | char *infile = NULL, *outfile = NULL, *prog; |
7e1b7485 RS |
61 | int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1; |
62 | OPTION_CHOICE o; | |
b4250010 | 63 | OSSL_LIB_CTX *libctx = app_get0_libctx(); |
90a1f2d7 | 64 | const char *propq = app_get0_propq(); |
3f39976d | 65 | |
7e1b7485 RS |
66 | prog = opt_init(argc, argv, pkcs7_options); |
67 | while ((o = opt_next()) != OPT_EOF) { | |
68 | switch (o) { | |
69 | case OPT_EOF: | |
70 | case OPT_ERR: | |
71 | opthelp: | |
72 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
73 | goto end; | |
74 | case OPT_HELP: | |
75 | opt_help(pkcs7_options); | |
76 | ret = 0; | |
77 | goto end; | |
78 | case OPT_INFORM: | |
79 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
80 | goto opthelp; | |
81 | break; | |
82 | case OPT_OUTFORM: | |
83 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
84 | goto opthelp; | |
85 | break; | |
86 | case OPT_IN: | |
87 | infile = opt_arg(); | |
88 | break; | |
89 | case OPT_OUT: | |
90 | outfile = opt_arg(); | |
91 | break; | |
92 | case OPT_NOOUT: | |
0f113f3e | 93 | noout = 1; |
7e1b7485 RS |
94 | break; |
95 | case OPT_TEXT: | |
0f113f3e | 96 | text = 1; |
7e1b7485 RS |
97 | break; |
98 | case OPT_PRINT: | |
0f113f3e | 99 | p7_print = 1; |
7e1b7485 RS |
100 | break; |
101 | case OPT_PRINT_CERTS: | |
0f113f3e | 102 | print_certs = 1; |
7e1b7485 RS |
103 | break; |
104 | case OPT_ENGINE: | |
dd1abd44 | 105 | e = setup_engine(opt_arg(), 0); |
0f113f3e | 106 | break; |
6bd4e3f2 P |
107 | case OPT_PROV_CASES: |
108 | if (!opt_provider(o)) | |
109 | goto end; | |
110 | break; | |
0f113f3e | 111 | } |
0f113f3e | 112 | } |
021410ea RS |
113 | |
114 | /* No extra arguments. */ | |
7e1b7485 | 115 | argc = opt_num_rest(); |
03358517 KR |
116 | if (argc != 0) |
117 | goto opthelp; | |
d02b48c6 | 118 | |
bdd58d98 | 119 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 | 120 | if (in == NULL) |
0f113f3e | 121 | goto end; |
d02b48c6 | 122 | |
d8652be0 | 123 | p7 = PKCS7_new_ex(libctx, propq); |
90a1f2d7 SL |
124 | if (p7 == NULL) { |
125 | BIO_printf(bio_err, "unable to allocate PKCS7 object\n"); | |
126 | ERR_print_errors(bio_err); | |
127 | goto end; | |
128 | } | |
129 | ||
0f113f3e | 130 | if (informat == FORMAT_ASN1) |
90a1f2d7 | 131 | p7i = d2i_PKCS7_bio(in, &p7); |
7e1b7485 | 132 | else |
90a1f2d7 SL |
133 | p7i = PEM_read_bio_PKCS7(in, &p7, NULL, NULL); |
134 | if (p7i == NULL) { | |
0f113f3e MC |
135 | BIO_printf(bio_err, "unable to load PKCS7 object\n"); |
136 | ERR_print_errors(bio_err); | |
137 | goto end; | |
138 | } | |
d02b48c6 | 139 | |
bdd58d98 | 140 | out = bio_open_default(outfile, 'w', outformat); |
7e1b7485 RS |
141 | if (out == NULL) |
142 | goto end; | |
d02b48c6 | 143 | |
0f113f3e MC |
144 | if (p7_print) |
145 | PKCS7_print_ctx(out, p7, 0, NULL); | |
9194296d | 146 | |
0f113f3e MC |
147 | if (print_certs) { |
148 | STACK_OF(X509) *certs = NULL; | |
149 | STACK_OF(X509_CRL) *crls = NULL; | |
d02b48c6 | 150 | |
0f113f3e MC |
151 | i = OBJ_obj2nid(p7->type); |
152 | switch (i) { | |
153 | case NID_pkcs7_signed: | |
79356a83 RS |
154 | if (p7->d.sign != NULL) { |
155 | certs = p7->d.sign->cert; | |
156 | crls = p7->d.sign->crl; | |
157 | } | |
0f113f3e MC |
158 | break; |
159 | case NID_pkcs7_signedAndEnveloped: | |
79356a83 RS |
160 | if (p7->d.signed_and_enveloped != NULL) { |
161 | certs = p7->d.signed_and_enveloped->cert; | |
162 | crls = p7->d.signed_and_enveloped->crl; | |
163 | } | |
0f113f3e MC |
164 | break; |
165 | default: | |
166 | break; | |
167 | } | |
d02b48c6 | 168 | |
0f113f3e MC |
169 | if (certs != NULL) { |
170 | X509 *x; | |
d02b48c6 | 171 | |
0f113f3e MC |
172 | for (i = 0; i < sk_X509_num(certs); i++) { |
173 | x = sk_X509_value(certs, i); | |
174 | if (text) | |
175 | X509_print(out, x); | |
176 | else | |
177 | dump_cert_text(out, x); | |
d02b48c6 | 178 | |
0f113f3e MC |
179 | if (!noout) |
180 | PEM_write_bio_X509(out, x); | |
181 | BIO_puts(out, "\n"); | |
182 | } | |
183 | } | |
184 | if (crls != NULL) { | |
185 | X509_CRL *crl; | |
d02b48c6 | 186 | |
0f113f3e MC |
187 | for (i = 0; i < sk_X509_CRL_num(crls); i++) { |
188 | crl = sk_X509_CRL_value(crls, i); | |
d02b48c6 | 189 | |
b5c4209b | 190 | X509_CRL_print_ex(out, crl, get_nameopt()); |
d02b48c6 | 191 | |
0f113f3e MC |
192 | if (!noout) |
193 | PEM_write_bio_X509_CRL(out, crl); | |
194 | BIO_puts(out, "\n"); | |
195 | } | |
196 | } | |
d02b48c6 | 197 | |
0f113f3e MC |
198 | ret = 0; |
199 | goto end; | |
200 | } | |
d02b48c6 | 201 | |
0f113f3e MC |
202 | if (!noout) { |
203 | if (outformat == FORMAT_ASN1) | |
204 | i = i2d_PKCS7_bio(out, p7); | |
7e1b7485 | 205 | else |
0f113f3e | 206 | i = PEM_write_bio_PKCS7(out, p7); |
d02b48c6 | 207 | |
0f113f3e MC |
208 | if (!i) { |
209 | BIO_printf(bio_err, "unable to write pkcs7 object\n"); | |
210 | ERR_print_errors(bio_err); | |
211 | goto end; | |
212 | } | |
213 | } | |
214 | ret = 0; | |
215 | end: | |
e0e920b1 | 216 | PKCS7_free(p7); |
dd1abd44 | 217 | release_engine(e); |
ca3a82c3 RS |
218 | BIO_free(in); |
219 | BIO_free_all(out); | |
26a7d938 | 220 | return ret; |
0f113f3e | 221 | } |