diff options
author | doug <> | 2015-07-12 16:32:21 +0000 |
---|---|---|
committer | doug <> | 2015-07-12 16:32:21 +0000 |
commit | 7b5c206c69f4bf71acfc7583571a9e43b3ce43e8 (patch) | |
tree | 912e32d824d8fa2e8d8512a5f3cd5baf3097db40 | |
parent | c72c50e028bb7f0bdfac41e425b6cee5393458d9 (diff) | |
download | openbsd-7b5c206c69f4bf71acfc7583571a9e43b3ce43e8.tar.gz openbsd-7b5c206c69f4bf71acfc7583571a9e43b3ce43e8.tar.bz2 openbsd-7b5c206c69f4bf71acfc7583571a9e43b3ce43e8.zip |
Convert openssl(1) crl2pkcs7 to the new option handling.
input + ok jsing@
Diffstat (limited to '')
-rw-r--r-- | src/usr.bin/openssl/crl2p7.c | 205 |
1 files changed, 110 insertions, 95 deletions
diff --git a/src/usr.bin/openssl/crl2p7.c b/src/usr.bin/openssl/crl2p7.c index b2c114aac8..208bce7866 100644 --- a/src/usr.bin/openssl/crl2p7.c +++ b/src/usr.bin/openssl/crl2p7.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: crl2p7.c,v 1.2 2014/08/28 14:23:52 jsing Exp $ */ | 1 | /* $OpenBSD: crl2p7.c,v 1.3 2015/07/12 16:32:21 doug Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -76,105 +76,130 @@ | |||
76 | 76 | ||
77 | static int add_certs_from_file(STACK_OF(X509) * stack, char *certfile); | 77 | static int add_certs_from_file(STACK_OF(X509) * stack, char *certfile); |
78 | 78 | ||
79 | /* -inform arg - input format - default PEM (DER or PEM) | 79 | static struct { |
80 | * -outform arg - output format - default PEM | 80 | STACK_OF(OPENSSL_STRING) *certflst; |
81 | * -in arg - input file - default stdin | 81 | char *infile; |
82 | * -out arg - output file - default stdout | 82 | int informat; |
83 | */ | 83 | int nocrl; |
84 | char *outfile; | ||
85 | int outformat; | ||
86 | } crl2p7_config; | ||
87 | |||
88 | static int | ||
89 | crl2p7_opt_certfile(char *arg) | ||
90 | { | ||
91 | if (crl2p7_config.certflst == NULL) | ||
92 | crl2p7_config.certflst = sk_OPENSSL_STRING_new_null(); | ||
93 | if (crl2p7_config.certflst == NULL) { | ||
94 | fprintf(stderr, "out of memory\n"); | ||
95 | return (1); | ||
96 | } | ||
97 | if (!sk_OPENSSL_STRING_push(crl2p7_config.certflst, arg)) { | ||
98 | fprintf(stderr, "out of memory\n"); | ||
99 | return (1); | ||
100 | } | ||
101 | |||
102 | return (0); | ||
103 | } | ||
104 | |||
105 | static struct option crl2p7_options[] = { | ||
106 | { | ||
107 | .name = "certfile", | ||
108 | .argname = "file", | ||
109 | .desc = "Chain of PEM certificates to a trusted CA", | ||
110 | .type = OPTION_ARG_FUNC, | ||
111 | .opt.argfunc = crl2p7_opt_certfile, | ||
112 | }, | ||
113 | { | ||
114 | .name = "in", | ||
115 | .argname = "file", | ||
116 | .desc = "Input file (default stdin)", | ||
117 | .type = OPTION_ARG, | ||
118 | .opt.arg = &crl2p7_config.infile, | ||
119 | }, | ||
120 | { | ||
121 | .name = "inform", | ||
122 | .argname = "format", | ||
123 | .desc = "Input format (DER or PEM (default))", | ||
124 | .type = OPTION_ARG_FORMAT, | ||
125 | .opt.value = &crl2p7_config.informat, | ||
126 | }, | ||
127 | { | ||
128 | .name = "nocrl", | ||
129 | .desc = "Do not read CRL from input or include CRL in output", | ||
130 | .type = OPTION_FLAG, | ||
131 | .opt.flag = &crl2p7_config.nocrl, | ||
132 | }, | ||
133 | { | ||
134 | .name = "out", | ||
135 | .argname = "file", | ||
136 | .desc = "Output file (default stdout)", | ||
137 | .type = OPTION_ARG, | ||
138 | .opt.arg = &crl2p7_config.outfile, | ||
139 | }, | ||
140 | { | ||
141 | .name = "outform", | ||
142 | .argname = "format", | ||
143 | .desc = "Output format (DER or PEM (default))", | ||
144 | .type = OPTION_ARG_FORMAT, | ||
145 | .opt.value = &crl2p7_config.outformat, | ||
146 | }, | ||
147 | { NULL }, | ||
148 | }; | ||
149 | |||
150 | static void | ||
151 | crl2p7_usage(void) | ||
152 | { | ||
153 | fprintf(stderr, | ||
154 | "usage: crl2p7 [-certfile file] [-in file] [-inform DER | PEM]\n" | ||
155 | " [-nocrl] [-out file] [-outform DER | PEM]\n\n"); | ||
156 | options_usage(crl2p7_options); | ||
157 | } | ||
84 | 158 | ||
85 | int crl2pkcs7_main(int, char **); | 159 | int crl2pkcs7_main(int, char **); |
86 | 160 | ||
87 | int | 161 | int |
88 | crl2pkcs7_main(int argc, char **argv) | 162 | crl2pkcs7_main(int argc, char **argv) |
89 | { | 163 | { |
90 | int i, badops = 0; | 164 | int i; |
91 | BIO *in = NULL, *out = NULL; | 165 | BIO *in = NULL, *out = NULL; |
92 | int informat, outformat; | 166 | char *certfile; |
93 | char *infile, *outfile, *prog, *certfile; | ||
94 | PKCS7 *p7 = NULL; | 167 | PKCS7 *p7 = NULL; |
95 | PKCS7_SIGNED *p7s = NULL; | 168 | PKCS7_SIGNED *p7s = NULL; |
96 | X509_CRL *crl = NULL; | 169 | X509_CRL *crl = NULL; |
97 | STACK_OF(OPENSSL_STRING) * certflst = NULL; | 170 | STACK_OF(X509_CRL) *crl_stack = NULL; |
98 | STACK_OF(X509_CRL) * crl_stack = NULL; | 171 | STACK_OF(X509) *cert_stack = NULL; |
99 | STACK_OF(X509) * cert_stack = NULL; | 172 | int ret = 1; |
100 | int ret = 1, nocrl = 0; | ||
101 | 173 | ||
102 | infile = NULL; | 174 | memset(&crl2p7_config, 0, sizeof(crl2p7_config)); |
103 | outfile = NULL; | ||
104 | informat = FORMAT_PEM; | ||
105 | outformat = FORMAT_PEM; | ||
106 | 175 | ||
107 | prog = argv[0]; | 176 | crl2p7_config.informat = FORMAT_PEM; |
108 | argc--; | 177 | crl2p7_config.outformat = FORMAT_PEM; |
109 | argv++; | ||
110 | while (argc >= 1) { | ||
111 | if (strcmp(*argv, "-inform") == 0) { | ||
112 | if (--argc < 1) | ||
113 | goto bad; | ||
114 | informat = str2fmt(*(++argv)); | ||
115 | } else if (strcmp(*argv, "-outform") == 0) { | ||
116 | if (--argc < 1) | ||
117 | goto bad; | ||
118 | outformat = str2fmt(*(++argv)); | ||
119 | } else if (strcmp(*argv, "-in") == 0) { | ||
120 | if (--argc < 1) | ||
121 | goto bad; | ||
122 | infile = *(++argv); | ||
123 | } else if (strcmp(*argv, "-nocrl") == 0) { | ||
124 | nocrl = 1; | ||
125 | } else if (strcmp(*argv, "-out") == 0) { | ||
126 | if (--argc < 1) | ||
127 | goto bad; | ||
128 | outfile = *(++argv); | ||
129 | } else if (strcmp(*argv, "-certfile") == 0) { | ||
130 | if (--argc < 1) | ||
131 | goto bad; | ||
132 | if (!certflst) | ||
133 | certflst = sk_OPENSSL_STRING_new_null(); | ||
134 | sk_OPENSSL_STRING_push(certflst, *(++argv)); | ||
135 | } else { | ||
136 | BIO_printf(bio_err, "unknown option %s\n", *argv); | ||
137 | badops = 1; | ||
138 | break; | ||
139 | } | ||
140 | argc--; | ||
141 | argv++; | ||
142 | } | ||
143 | 178 | ||
144 | if (badops) { | 179 | if (options_parse(argc, argv, crl2p7_options, NULL, NULL) != 0) { |
145 | bad: | 180 | crl2p7_usage(); |
146 | BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog); | ||
147 | BIO_printf(bio_err, "where options are\n"); | ||
148 | BIO_printf(bio_err, " -inform arg input format - DER or PEM\n"); | ||
149 | BIO_printf(bio_err, " -outform arg output format - DER or PEM\n"); | ||
150 | BIO_printf(bio_err, " -in arg input file\n"); | ||
151 | BIO_printf(bio_err, " -out arg output file\n"); | ||
152 | BIO_printf(bio_err, " -certfile arg certificates file of chain to a trusted CA\n"); | ||
153 | BIO_printf(bio_err, " (can be used more than once)\n"); | ||
154 | BIO_printf(bio_err, " -nocrl no crl to load, just certs from '-certfile'\n"); | ||
155 | ret = 1; | ||
156 | goto end; | 181 | goto end; |
157 | } | 182 | } |
158 | 183 | ||
159 | in = BIO_new(BIO_s_file()); | 184 | in = BIO_new(BIO_s_file()); |
160 | out = BIO_new(BIO_s_file()); | 185 | out = BIO_new(BIO_s_file()); |
161 | if ((in == NULL) || (out == NULL)) { | 186 | if (in == NULL || out == NULL) { |
162 | ERR_print_errors(bio_err); | 187 | ERR_print_errors(bio_err); |
163 | goto end; | 188 | goto end; |
164 | } | 189 | } |
165 | if (!nocrl) { | 190 | if (!crl2p7_config.nocrl) { |
166 | if (infile == NULL) | 191 | if (crl2p7_config.infile == NULL) |
167 | BIO_set_fp(in, stdin, BIO_NOCLOSE); | 192 | BIO_set_fp(in, stdin, BIO_NOCLOSE); |
168 | else { | 193 | else { |
169 | if (BIO_read_filename(in, infile) <= 0) { | 194 | if (BIO_read_filename(in, crl2p7_config.infile) <= 0) { |
170 | perror(infile); | 195 | perror(crl2p7_config.infile); |
171 | goto end; | 196 | goto end; |
172 | } | 197 | } |
173 | } | 198 | } |
174 | 199 | ||
175 | if (informat == FORMAT_ASN1) | 200 | if (crl2p7_config.informat == FORMAT_ASN1) |
176 | crl = d2i_X509_CRL_bio(in, NULL); | 201 | crl = d2i_X509_CRL_bio(in, NULL); |
177 | else if (informat == FORMAT_PEM) | 202 | else if (crl2p7_config.informat == FORMAT_PEM) |
178 | crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); | 203 | crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); |
179 | else { | 204 | else { |
180 | BIO_printf(bio_err, | 205 | BIO_printf(bio_err, |
@@ -208,9 +233,9 @@ bad: | |||
208 | goto end; | 233 | goto end; |
209 | p7s->cert = cert_stack; | 234 | p7s->cert = cert_stack; |
210 | 235 | ||
211 | if (certflst) | 236 | if (crl2p7_config.certflst) { |
212 | for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) { | 237 | for (i = 0; i < sk_OPENSSL_STRING_num(crl2p7_config.certflst); i++) { |
213 | certfile = sk_OPENSSL_STRING_value(certflst, i); | 238 | certfile = sk_OPENSSL_STRING_value(crl2p7_config.certflst, i); |
214 | if (add_certs_from_file(cert_stack, certfile) < 0) { | 239 | if (add_certs_from_file(cert_stack, certfile) < 0) { |
215 | BIO_printf(bio_err, | 240 | BIO_printf(bio_err, |
216 | "error loading certificates\n"); | 241 | "error loading certificates\n"); |
@@ -218,21 +243,22 @@ bad: | |||
218 | goto end; | 243 | goto end; |
219 | } | 244 | } |
220 | } | 245 | } |
246 | } | ||
221 | 247 | ||
222 | sk_OPENSSL_STRING_free(certflst); | 248 | sk_OPENSSL_STRING_free(crl2p7_config.certflst); |
223 | 249 | ||
224 | if (outfile == NULL) { | 250 | if (crl2p7_config.outfile == NULL) { |
225 | BIO_set_fp(out, stdout, BIO_NOCLOSE); | 251 | BIO_set_fp(out, stdout, BIO_NOCLOSE); |
226 | } else { | 252 | } else { |
227 | if (BIO_write_filename(out, outfile) <= 0) { | 253 | if (BIO_write_filename(out, crl2p7_config.outfile) <= 0) { |
228 | perror(outfile); | 254 | perror(crl2p7_config.outfile); |
229 | goto end; | 255 | goto end; |
230 | } | 256 | } |
231 | } | 257 | } |
232 | 258 | ||
233 | if (outformat == FORMAT_ASN1) | 259 | if (crl2p7_config.outformat == FORMAT_ASN1) |
234 | i = i2d_PKCS7_bio(out, p7); | 260 | i = i2d_PKCS7_bio(out, p7); |
235 | else if (outformat == FORMAT_PEM) | 261 | else if (crl2p7_config.outformat == FORMAT_PEM) |
236 | i = PEM_write_bio_PKCS7(out, p7); | 262 | i = PEM_write_bio_PKCS7(out, p7); |
237 | else { | 263 | else { |
238 | BIO_printf(bio_err, | 264 | BIO_printf(bio_err, |
@@ -256,31 +282,20 @@ end: | |||
256 | if (crl != NULL) | 282 | if (crl != NULL) |
257 | X509_CRL_free(crl); | 283 | X509_CRL_free(crl); |
258 | 284 | ||
259 | |||
260 | return (ret); | 285 | return (ret); |
261 | } | 286 | } |
262 | 287 | ||
263 | /* | ||
264 | *---------------------------------------------------------------------- | ||
265 | * int add_certs_from_file | ||
266 | * | ||
267 | * Read a list of certificates to be checked from a file. | ||
268 | * | ||
269 | * Results: | ||
270 | * number of certs added if successful, -1 if not. | ||
271 | *---------------------------------------------------------------------- | ||
272 | */ | ||
273 | static int | 288 | static int |
274 | add_certs_from_file(STACK_OF(X509) * stack, char *certfile) | 289 | add_certs_from_file(STACK_OF(X509) *stack, char *certfile) |
275 | { | 290 | { |
276 | BIO *in = NULL; | 291 | BIO *in = NULL; |
277 | int count = 0; | 292 | int count = 0; |
278 | int ret = -1; | 293 | int ret = -1; |
279 | STACK_OF(X509_INFO) * sk = NULL; | 294 | STACK_OF(X509_INFO) *sk = NULL; |
280 | X509_INFO *xi; | 295 | X509_INFO *xi; |
281 | 296 | ||
282 | in = BIO_new(BIO_s_file()); | 297 | in = BIO_new(BIO_s_file()); |
283 | if ((in == NULL) || (BIO_read_filename(in, certfile) <= 0)) { | 298 | if (in == NULL || BIO_read_filename(in, certfile) <= 0) { |
284 | BIO_printf(bio_err, "error opening the file, %s\n", certfile); | 299 | BIO_printf(bio_err, "error opening the file, %s\n", certfile); |
285 | goto end; | 300 | goto end; |
286 | } | 301 | } |