1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
---|
5 | * |
---|
6 | * Licensed under the OpenSSL license (the "License"). You may not use |
---|
7 | * this file except in compliance with the License. You can obtain a copy |
---|
8 | * in the file LICENSE in the source distribution or at |
---|
9 | * https://www.openssl.org/source/license.html |
---|
10 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> |
---|
15 | #include <time.h> |
---|
16 | #include "apps.h" |
---|
17 | #include "progs.h" |
---|
18 | #include <openssl/err.h> |
---|
19 | #include <openssl/objects.h> |
---|
20 | #include <openssl/evp.h> |
---|
21 | #include <openssl/x509.h> |
---|
22 | #include <openssl/pkcs7.h> |
---|
23 | #include <openssl/pem.h> |
---|
24 | |
---|
25 | typedef enum OPTION_choice { |
---|
26 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
---|
27 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT, |
---|
28 | OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE |
---|
29 | } OPTION_CHOICE; |
---|
30 | |
---|
31 | const OPTIONS pkcs7_options[] = { |
---|
32 | {"help", OPT_HELP, '-', "Display this summary"}, |
---|
33 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, |
---|
34 | {"in", OPT_IN, '<', "Input file"}, |
---|
35 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, |
---|
36 | {"out", OPT_OUT, '>', "Output file"}, |
---|
37 | {"noout", OPT_NOOUT, '-', "Don't output encoded data"}, |
---|
38 | {"text", OPT_TEXT, '-', "Print full details of certificates"}, |
---|
39 | {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"}, |
---|
40 | {"print_certs", OPT_PRINT_CERTS, '-', |
---|
41 | "Print_certs print any certs or crl in the input"}, |
---|
42 | #ifndef OPENSSL_NO_ENGINE |
---|
43 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
---|
44 | #endif |
---|
45 | {NULL} |
---|
46 | }; |
---|
47 | |
---|
48 | int pkcs7_main(int argc, char **argv) |
---|
49 | { |
---|
50 | ENGINE *e = NULL; |
---|
51 | PKCS7 *p7 = NULL; |
---|
52 | BIO *in = NULL, *out = NULL; |
---|
53 | int informat = FORMAT_PEM, outformat = FORMAT_PEM; |
---|
54 | char *infile = NULL, *outfile = NULL, *prog; |
---|
55 | int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1; |
---|
56 | OPTION_CHOICE o; |
---|
57 | |
---|
58 | prog = opt_init(argc, argv, pkcs7_options); |
---|
59 | while ((o = opt_next()) != OPT_EOF) { |
---|
60 | switch (o) { |
---|
61 | case OPT_EOF: |
---|
62 | case OPT_ERR: |
---|
63 | opthelp: |
---|
64 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
---|
65 | goto end; |
---|
66 | case OPT_HELP: |
---|
67 | opt_help(pkcs7_options); |
---|
68 | ret = 0; |
---|
69 | goto end; |
---|
70 | case OPT_INFORM: |
---|
71 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) |
---|
72 | goto opthelp; |
---|
73 | break; |
---|
74 | case OPT_OUTFORM: |
---|
75 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) |
---|
76 | goto opthelp; |
---|
77 | break; |
---|
78 | case OPT_IN: |
---|
79 | infile = opt_arg(); |
---|
80 | break; |
---|
81 | case OPT_OUT: |
---|
82 | outfile = opt_arg(); |
---|
83 | break; |
---|
84 | case OPT_NOOUT: |
---|
85 | noout = 1; |
---|
86 | break; |
---|
87 | case OPT_TEXT: |
---|
88 | text = 1; |
---|
89 | break; |
---|
90 | case OPT_PRINT: |
---|
91 | p7_print = 1; |
---|
92 | break; |
---|
93 | case OPT_PRINT_CERTS: |
---|
94 | print_certs = 1; |
---|
95 | break; |
---|
96 | case OPT_ENGINE: |
---|
97 | e = setup_engine(opt_arg(), 0); |
---|
98 | break; |
---|
99 | } |
---|
100 | } |
---|
101 | argc = opt_num_rest(); |
---|
102 | if (argc != 0) |
---|
103 | goto opthelp; |
---|
104 | |
---|
105 | in = bio_open_default(infile, 'r', informat); |
---|
106 | if (in == NULL) |
---|
107 | goto end; |
---|
108 | |
---|
109 | if (informat == FORMAT_ASN1) |
---|
110 | p7 = d2i_PKCS7_bio(in, NULL); |
---|
111 | else |
---|
112 | p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); |
---|
113 | if (p7 == NULL) { |
---|
114 | BIO_printf(bio_err, "unable to load PKCS7 object\n"); |
---|
115 | ERR_print_errors(bio_err); |
---|
116 | goto end; |
---|
117 | } |
---|
118 | |
---|
119 | out = bio_open_default(outfile, 'w', outformat); |
---|
120 | if (out == NULL) |
---|
121 | goto end; |
---|
122 | |
---|
123 | if (p7_print) |
---|
124 | PKCS7_print_ctx(out, p7, 0, NULL); |
---|
125 | |
---|
126 | if (print_certs) { |
---|
127 | STACK_OF(X509) *certs = NULL; |
---|
128 | STACK_OF(X509_CRL) *crls = NULL; |
---|
129 | |
---|
130 | i = OBJ_obj2nid(p7->type); |
---|
131 | switch (i) { |
---|
132 | case NID_pkcs7_signed: |
---|
133 | if (p7->d.sign != NULL) { |
---|
134 | certs = p7->d.sign->cert; |
---|
135 | crls = p7->d.sign->crl; |
---|
136 | } |
---|
137 | break; |
---|
138 | case NID_pkcs7_signedAndEnveloped: |
---|
139 | if (p7->d.signed_and_enveloped != NULL) { |
---|
140 | certs = p7->d.signed_and_enveloped->cert; |
---|
141 | crls = p7->d.signed_and_enveloped->crl; |
---|
142 | } |
---|
143 | break; |
---|
144 | default: |
---|
145 | break; |
---|
146 | } |
---|
147 | |
---|
148 | if (certs != NULL) { |
---|
149 | X509 *x; |
---|
150 | |
---|
151 | for (i = 0; i < sk_X509_num(certs); i++) { |
---|
152 | x = sk_X509_value(certs, i); |
---|
153 | if (text) |
---|
154 | X509_print(out, x); |
---|
155 | else |
---|
156 | dump_cert_text(out, x); |
---|
157 | |
---|
158 | if (!noout) |
---|
159 | PEM_write_bio_X509(out, x); |
---|
160 | BIO_puts(out, "\n"); |
---|
161 | } |
---|
162 | } |
---|
163 | if (crls != NULL) { |
---|
164 | X509_CRL *crl; |
---|
165 | |
---|
166 | for (i = 0; i < sk_X509_CRL_num(crls); i++) { |
---|
167 | crl = sk_X509_CRL_value(crls, i); |
---|
168 | |
---|
169 | X509_CRL_print_ex(out, crl, get_nameopt()); |
---|
170 | |
---|
171 | if (!noout) |
---|
172 | PEM_write_bio_X509_CRL(out, crl); |
---|
173 | BIO_puts(out, "\n"); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | ret = 0; |
---|
178 | goto end; |
---|
179 | } |
---|
180 | |
---|
181 | if (!noout) { |
---|
182 | if (outformat == FORMAT_ASN1) |
---|
183 | i = i2d_PKCS7_bio(out, p7); |
---|
184 | else |
---|
185 | i = PEM_write_bio_PKCS7(out, p7); |
---|
186 | |
---|
187 | if (!i) { |
---|
188 | BIO_printf(bio_err, "unable to write pkcs7 object\n"); |
---|
189 | ERR_print_errors(bio_err); |
---|
190 | goto end; |
---|
191 | } |
---|
192 | } |
---|
193 | ret = 0; |
---|
194 | end: |
---|
195 | PKCS7_free(p7); |
---|
196 | release_engine(e); |
---|
197 | BIO_free(in); |
---|
198 | BIO_free_all(out); |
---|
199 | return ret; |
---|
200 | } |
---|