]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | ||
0f113f3e MC |
58 | /* |
59 | * This was written by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> and | |
60 | * donated 'to the cause' along with lots and lots of other fixes to the | |
61 | * library. | |
62 | */ | |
d02b48c6 RE |
63 | |
64 | #include <stdio.h> | |
65 | #include <string.h> | |
66 | #include <sys/types.h> | |
d02b48c6 | 67 | #include "apps.h" |
ec577822 BM |
68 | #include <openssl/err.h> |
69 | #include <openssl/evp.h> | |
70 | #include <openssl/x509.h> | |
71 | #include <openssl/pkcs7.h> | |
72 | #include <openssl/pem.h> | |
73 | #include <openssl/objects.h> | |
d02b48c6 | 74 | |
f73e07cf | 75 | static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile); |
d02b48c6 | 76 | |
7e1b7485 RS |
77 | typedef enum OPTION_choice { |
78 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
79 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOCRL, OPT_CERTFILE | |
80 | } OPTION_CHOICE; | |
d02b48c6 | 81 | |
7e1b7485 RS |
82 | OPTIONS crl2pkcs7_options[] = { |
83 | {"help", OPT_HELP, '-', "Display this summary"}, | |
84 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, | |
85 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, | |
86 | {"in", OPT_IN, '<', "Input file"}, | |
87 | {"out", OPT_OUT, '>', "Output file"}, | |
88 | {"nocrl", OPT_NOCRL, '-', "No crl to load, just certs from '-certfile'"}, | |
89 | {"certfile", OPT_CERTFILE, '<', | |
90 | "File of chain of certs to a trusted CA; can be repeated"}, | |
91 | {NULL} | |
92 | }; | |
667ac4ec | 93 | |
7e1b7485 | 94 | int crl2pkcs7_main(int argc, char **argv) |
0f113f3e | 95 | { |
0f113f3e | 96 | BIO *in = NULL, *out = NULL; |
0f113f3e MC |
97 | PKCS7 *p7 = NULL; |
98 | PKCS7_SIGNED *p7s = NULL; | |
0f113f3e | 99 | STACK_OF(OPENSSL_STRING) *certflst = NULL; |
0f113f3e | 100 | STACK_OF(X509) *cert_stack = NULL; |
7e1b7485 RS |
101 | STACK_OF(X509_CRL) *crl_stack = NULL; |
102 | X509_CRL *crl = NULL; | |
103 | char *infile = NULL, *outfile = NULL, *prog, *certfile; | |
104 | int i = 0, informat = FORMAT_PEM, outformat = FORMAT_PEM, ret = 1, nocrl = | |
105 | 0; | |
106 | OPTION_CHOICE o; | |
d02b48c6 | 107 | |
7e1b7485 RS |
108 | prog = opt_init(argc, argv, crl2pkcs7_options); |
109 | while ((o = opt_next()) != OPT_EOF) { | |
110 | switch (o) { | |
111 | case OPT_EOF: | |
112 | case OPT_ERR: | |
113 | opthelp: | |
114 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
115 | goto end; | |
116 | case OPT_HELP: | |
117 | opt_help(crl2pkcs7_options); | |
118 | ret = 0; | |
119 | goto end; | |
120 | case OPT_INFORM: | |
121 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
122 | goto opthelp; | |
123 | break; | |
124 | case OPT_OUTFORM: | |
125 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
126 | goto opthelp; | |
127 | break; | |
128 | case OPT_IN: | |
129 | infile = opt_arg(); | |
130 | break; | |
131 | case OPT_OUT: | |
132 | outfile = opt_arg(); | |
133 | break; | |
134 | case OPT_NOCRL: | |
0f113f3e | 135 | nocrl = 1; |
7e1b7485 RS |
136 | break; |
137 | case OPT_CERTFILE: | |
138 | if (!certflst && !(certflst = sk_OPENSSL_STRING_new_null())) | |
0f113f3e MC |
139 | goto end; |
140 | if (!sk_OPENSSL_STRING_push(certflst, *(++argv))) { | |
141 | sk_OPENSSL_STRING_free(certflst); | |
142 | goto end; | |
143 | } | |
0f113f3e MC |
144 | break; |
145 | } | |
0f113f3e | 146 | } |
7e1b7485 RS |
147 | argc = opt_num_rest(); |
148 | argv = opt_rest(); | |
d02b48c6 | 149 | |
0f113f3e | 150 | if (!nocrl) { |
7e1b7485 RS |
151 | in = bio_open_default(infile, RB(informat)); |
152 | if (in == NULL) | |
153 | goto end; | |
d02b48c6 | 154 | |
0f113f3e MC |
155 | if (informat == FORMAT_ASN1) |
156 | crl = d2i_X509_CRL_bio(in, NULL); | |
157 | else if (informat == FORMAT_PEM) | |
158 | crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); | |
0f113f3e MC |
159 | if (crl == NULL) { |
160 | BIO_printf(bio_err, "unable to load CRL\n"); | |
161 | ERR_print_errors(bio_err); | |
162 | goto end; | |
163 | } | |
164 | } | |
d02b48c6 | 165 | |
0f113f3e MC |
166 | if ((p7 = PKCS7_new()) == NULL) |
167 | goto end; | |
168 | if ((p7s = PKCS7_SIGNED_new()) == NULL) | |
169 | goto end; | |
170 | p7->type = OBJ_nid2obj(NID_pkcs7_signed); | |
171 | p7->d.sign = p7s; | |
172 | p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data); | |
d02b48c6 | 173 | |
0f113f3e MC |
174 | if (!ASN1_INTEGER_set(p7s->version, 1)) |
175 | goto end; | |
176 | if ((crl_stack = sk_X509_CRL_new_null()) == NULL) | |
177 | goto end; | |
178 | p7s->crl = crl_stack; | |
179 | if (crl != NULL) { | |
180 | sk_X509_CRL_push(crl_stack, crl); | |
181 | crl = NULL; /* now part of p7 for OPENSSL_freeing */ | |
182 | } | |
d02b48c6 | 183 | |
0f113f3e MC |
184 | if ((cert_stack = sk_X509_new_null()) == NULL) |
185 | goto end; | |
186 | p7s->cert = cert_stack; | |
9fe84296 | 187 | |
0f113f3e MC |
188 | if (certflst) |
189 | for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) { | |
190 | certfile = sk_OPENSSL_STRING_value(certflst, i); | |
191 | if (add_certs_from_file(cert_stack, certfile) < 0) { | |
192 | BIO_printf(bio_err, "error loading certificates\n"); | |
193 | ERR_print_errors(bio_err); | |
194 | goto end; | |
195 | } | |
196 | } | |
d02b48c6 | 197 | |
0f113f3e MC |
198 | sk_OPENSSL_STRING_free(certflst); |
199 | ||
7e1b7485 RS |
200 | out = bio_open_default(outfile, WB(outformat)); |
201 | if (out == NULL) | |
202 | goto end; | |
d02b48c6 | 203 | |
0f113f3e MC |
204 | if (outformat == FORMAT_ASN1) |
205 | i = i2d_PKCS7_bio(out, p7); | |
206 | else if (outformat == FORMAT_PEM) | |
207 | i = PEM_write_bio_PKCS7(out, p7); | |
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 | ret = 0; | |
214 | end: | |
ca3a82c3 RS |
215 | BIO_free(in); |
216 | BIO_free_all(out); | |
e0e920b1 | 217 | PKCS7_free(p7); |
222561fe | 218 | X509_CRL_free(crl); |
d02b48c6 | 219 | |
7e1b7485 | 220 | return (ret); |
0f113f3e | 221 | } |
d02b48c6 | 222 | |
c80fd6b2 | 223 | /*- |
d02b48c6 RE |
224 | *---------------------------------------------------------------------- |
225 | * int add_certs_from_file | |
226 | * | |
0f113f3e | 227 | * Read a list of certificates to be checked from a file. |
d02b48c6 RE |
228 | * |
229 | * Results: | |
0f113f3e | 230 | * number of certs added if successful, -1 if not. |
d02b48c6 RE |
231 | *---------------------------------------------------------------------- |
232 | */ | |
6b691a5c | 233 | static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile) |
0f113f3e MC |
234 | { |
235 | BIO *in = NULL; | |
236 | int count = 0; | |
237 | int ret = -1; | |
238 | STACK_OF(X509_INFO) *sk = NULL; | |
239 | X509_INFO *xi; | |
d02b48c6 | 240 | |
7e1b7485 RS |
241 | in = BIO_new_file(certfile, "r"); |
242 | if (in == NULL) { | |
0f113f3e MC |
243 | BIO_printf(bio_err, "error opening the file, %s\n", certfile); |
244 | goto end; | |
245 | } | |
d02b48c6 | 246 | |
0f113f3e MC |
247 | /* This loads from a file, a stack of x509/crl/pkey sets */ |
248 | sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL); | |
249 | if (sk == NULL) { | |
250 | BIO_printf(bio_err, "error reading the file, %s\n", certfile); | |
251 | goto end; | |
252 | } | |
d02b48c6 | 253 | |
0f113f3e MC |
254 | /* scan over it and pull out the CRL's */ |
255 | while (sk_X509_INFO_num(sk)) { | |
256 | xi = sk_X509_INFO_shift(sk); | |
257 | if (xi->x509 != NULL) { | |
258 | sk_X509_push(stack, xi->x509); | |
259 | xi->x509 = NULL; | |
260 | count++; | |
261 | } | |
262 | X509_INFO_free(xi); | |
263 | } | |
d02b48c6 | 264 | |
0f113f3e MC |
265 | ret = count; |
266 | end: | |
267 | /* never need to OPENSSL_free x */ | |
ca3a82c3 | 268 | BIO_free(in); |
222561fe | 269 | sk_X509_INFO_free(sk); |
0f113f3e MC |
270 | return (ret); |
271 | } |