diff options
Diffstat (limited to 'src/usr.bin/openssl/ecparam.c')
-rw-r--r-- | src/usr.bin/openssl/ecparam.c | 448 |
1 files changed, 0 insertions, 448 deletions
diff --git a/src/usr.bin/openssl/ecparam.c b/src/usr.bin/openssl/ecparam.c deleted file mode 100644 index 285f5d563e..0000000000 --- a/src/usr.bin/openssl/ecparam.c +++ /dev/null | |||
@@ -1,448 +0,0 @@ | |||
1 | /* $OpenBSD: ecparam.c,v 1.25 2025/01/19 10:24:17 tb Exp $ */ | ||
2 | /* | ||
3 | * Written by Nils Larsch for the OpenSSL project. | ||
4 | */ | ||
5 | /* ==================================================================== | ||
6 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in | ||
17 | * the documentation and/or other materials provided with the | ||
18 | * distribution. | ||
19 | * | ||
20 | * 3. All advertising materials mentioning features or use of this | ||
21 | * software must display the following acknowledgment: | ||
22 | * "This product includes software developed by the OpenSSL Project | ||
23 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
24 | * | ||
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
26 | * endorse or promote products derived from this software without | ||
27 | * prior written permission. For written permission, please contact | ||
28 | * openssl-core@openssl.org. | ||
29 | * | ||
30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
31 | * nor may "OpenSSL" appear in their names without prior written | ||
32 | * permission of the OpenSSL Project. | ||
33 | * | ||
34 | * 6. Redistributions of any form whatsoever must retain the following | ||
35 | * acknowledgment: | ||
36 | * "This product includes software developed by the OpenSSL Project | ||
37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
38 | * | ||
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
51 | * ==================================================================== | ||
52 | * | ||
53 | * This product includes cryptographic software written by Eric Young | ||
54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
55 | * Hudson (tjh@cryptsoft.com). | ||
56 | * | ||
57 | */ | ||
58 | /* ==================================================================== | ||
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
60 | * | ||
61 | * Portions of the attached software ("Contribution") are developed by | ||
62 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. | ||
63 | * | ||
64 | * The Contribution is licensed pursuant to the OpenSSL open source | ||
65 | * license provided above. | ||
66 | * | ||
67 | * The elliptic curve binary polynomial software is originally written by | ||
68 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. | ||
69 | * | ||
70 | */ | ||
71 | |||
72 | #include <openssl/opensslconf.h> | ||
73 | |||
74 | #ifndef OPENSSL_NO_EC | ||
75 | |||
76 | #include <stdio.h> | ||
77 | #include <stdlib.h> | ||
78 | #include <string.h> | ||
79 | #include <time.h> | ||
80 | |||
81 | #include "apps.h" | ||
82 | |||
83 | #include <openssl/bio.h> | ||
84 | #include <openssl/bn.h> | ||
85 | #include <openssl/ec.h> | ||
86 | #include <openssl/err.h> | ||
87 | #include <openssl/pem.h> | ||
88 | #include <openssl/x509.h> | ||
89 | |||
90 | static struct { | ||
91 | int asn1_flag; | ||
92 | int check; | ||
93 | char *curve_name; | ||
94 | point_conversion_form_t form; | ||
95 | int genkey; | ||
96 | char *infile; | ||
97 | int informat; | ||
98 | int list_curves; | ||
99 | int new_asn1_flag; | ||
100 | int new_form; | ||
101 | int no_seed; | ||
102 | int noout; | ||
103 | char *outfile; | ||
104 | int outformat; | ||
105 | int text; | ||
106 | } cfg; | ||
107 | |||
108 | static int | ||
109 | ecparam_opt_form(char *arg) | ||
110 | { | ||
111 | if (strcmp(arg, "compressed") == 0) | ||
112 | cfg.form = POINT_CONVERSION_COMPRESSED; | ||
113 | else if (strcmp(arg, "uncompressed") == 0) | ||
114 | cfg.form = POINT_CONVERSION_UNCOMPRESSED; | ||
115 | else if (strcmp(arg, "hybrid") == 0) | ||
116 | cfg.form = POINT_CONVERSION_HYBRID; | ||
117 | else | ||
118 | return (1); | ||
119 | |||
120 | cfg.new_form = 1; | ||
121 | return (0); | ||
122 | } | ||
123 | |||
124 | static int | ||
125 | ecparam_opt_enctype(char *arg) | ||
126 | { | ||
127 | if (strcmp(arg, "explicit") == 0) | ||
128 | cfg.asn1_flag = 0; | ||
129 | else if (strcmp(arg, "named_curve") == 0) | ||
130 | cfg.asn1_flag = OPENSSL_EC_NAMED_CURVE; | ||
131 | else | ||
132 | return (1); | ||
133 | |||
134 | cfg.new_asn1_flag = 1; | ||
135 | return (0); | ||
136 | } | ||
137 | |||
138 | static const struct option ecparam_options[] = { | ||
139 | { | ||
140 | .name = "check", | ||
141 | .desc = "Validate the elliptic curve parameters", | ||
142 | .type = OPTION_FLAG, | ||
143 | .opt.flag = &cfg.check, | ||
144 | }, | ||
145 | { | ||
146 | .name = "conv_form", | ||
147 | .argname = "form", | ||
148 | .desc = "Specify point conversion form:\n" | ||
149 | " compressed, uncompressed (default), hybrid", | ||
150 | .type = OPTION_ARG_FUNC, | ||
151 | .opt.argfunc = ecparam_opt_form, | ||
152 | }, | ||
153 | { | ||
154 | .name = "genkey", | ||
155 | .desc = "Generate an EC private key using the specified " | ||
156 | "parameters", | ||
157 | .type = OPTION_FLAG, | ||
158 | .opt.flag = &cfg.genkey, | ||
159 | }, | ||
160 | { | ||
161 | .name = "in", | ||
162 | .argname = "file", | ||
163 | .desc = "Input file to read parameters from (default stdin)", | ||
164 | .type = OPTION_ARG, | ||
165 | .opt.arg = &cfg.infile, | ||
166 | }, | ||
167 | { | ||
168 | .name = "inform", | ||
169 | .argname = "format", | ||
170 | .desc = "Input format (DER or PEM)", | ||
171 | .type = OPTION_ARG_FORMAT, | ||
172 | .opt.value = &cfg.informat, | ||
173 | }, | ||
174 | { | ||
175 | .name = "list_curves", | ||
176 | .desc = "Print list of all currently implemented EC " | ||
177 | "parameter names", | ||
178 | .type = OPTION_FLAG, | ||
179 | .opt.flag = &cfg.list_curves, | ||
180 | }, | ||
181 | { | ||
182 | .name = "name", | ||
183 | .argname = "curve", | ||
184 | .desc = "Use the EC parameters with the specified name", | ||
185 | .type = OPTION_ARG, | ||
186 | .opt.arg = &cfg.curve_name, | ||
187 | }, | ||
188 | { | ||
189 | .name = "no_seed", | ||
190 | .desc = "Do not output seed with explicit parameter encoding", | ||
191 | .type = OPTION_FLAG, | ||
192 | .opt.flag = &cfg.no_seed, | ||
193 | }, | ||
194 | { | ||
195 | .name = "noout", | ||
196 | .desc = "Do not output encoded version of EC parameters", | ||
197 | .type = OPTION_FLAG, | ||
198 | .opt.flag = &cfg.noout, | ||
199 | }, | ||
200 | { | ||
201 | .name = "out", | ||
202 | .argname = "file", | ||
203 | .desc = "Output file to write parameters to (default stdout)", | ||
204 | .type = OPTION_ARG, | ||
205 | .opt.arg = &cfg.outfile, | ||
206 | }, | ||
207 | { | ||
208 | .name = "outform", | ||
209 | .argname = "format", | ||
210 | .desc = "Output format (DER or PEM)", | ||
211 | .type = OPTION_ARG_FORMAT, | ||
212 | .opt.value = &cfg.outformat, | ||
213 | }, | ||
214 | { | ||
215 | .name = "param_enc", | ||
216 | .argname = "type", | ||
217 | .desc = "Specify EC parameter ASN.1 encoding type:\n" | ||
218 | " explicit, named_curve (default)", | ||
219 | .type = OPTION_ARG_FUNC, | ||
220 | .opt.argfunc = ecparam_opt_enctype, | ||
221 | }, | ||
222 | { | ||
223 | .name = "text", | ||
224 | .desc = "Print out the EC parameters in human readable form", | ||
225 | .type = OPTION_FLAG, | ||
226 | .opt.flag = &cfg.text, | ||
227 | }, | ||
228 | {NULL}, | ||
229 | }; | ||
230 | |||
231 | static void | ||
232 | ecparam_usage(void) | ||
233 | { | ||
234 | fprintf(stderr, "usage: ecparam [-check] [-conv_form arg] " | ||
235 | " [-genkey]\n" | ||
236 | " [-in file] [-inform DER | PEM] [-list_curves] [-name arg]\n" | ||
237 | " [-no_seed] [-noout] [-out file] [-outform DER | PEM]\n" | ||
238 | " [-param_enc arg] [-text]\n\n"); | ||
239 | options_usage(ecparam_options); | ||
240 | } | ||
241 | |||
242 | int | ||
243 | ecparam_main(int argc, char **argv) | ||
244 | { | ||
245 | EC_GROUP *group = NULL; | ||
246 | BIO *in = NULL, *out = NULL; | ||
247 | int i, ret = 1; | ||
248 | |||
249 | if (pledge("stdio cpath wpath rpath", NULL) == -1) { | ||
250 | perror("pledge"); | ||
251 | exit(1); | ||
252 | } | ||
253 | |||
254 | memset(&cfg, 0, sizeof(cfg)); | ||
255 | cfg.asn1_flag = OPENSSL_EC_NAMED_CURVE; | ||
256 | cfg.form = POINT_CONVERSION_UNCOMPRESSED; | ||
257 | cfg.informat = FORMAT_PEM; | ||
258 | cfg.outformat = FORMAT_PEM; | ||
259 | |||
260 | if (options_parse(argc, argv, ecparam_options, NULL, NULL) != 0) { | ||
261 | ecparam_usage(); | ||
262 | goto end; | ||
263 | } | ||
264 | |||
265 | in = BIO_new(BIO_s_file()); | ||
266 | out = BIO_new(BIO_s_file()); | ||
267 | if ((in == NULL) || (out == NULL)) { | ||
268 | ERR_print_errors(bio_err); | ||
269 | goto end; | ||
270 | } | ||
271 | if (cfg.infile == NULL) | ||
272 | BIO_set_fp(in, stdin, BIO_NOCLOSE); | ||
273 | else { | ||
274 | if (BIO_read_filename(in, cfg.infile) <= 0) { | ||
275 | perror(cfg.infile); | ||
276 | goto end; | ||
277 | } | ||
278 | } | ||
279 | if (cfg.outfile == NULL) { | ||
280 | BIO_set_fp(out, stdout, BIO_NOCLOSE); | ||
281 | } else { | ||
282 | if (BIO_write_filename(out, cfg.outfile) <= 0) { | ||
283 | perror(cfg.outfile); | ||
284 | goto end; | ||
285 | } | ||
286 | } | ||
287 | |||
288 | if (cfg.list_curves) { | ||
289 | EC_builtin_curve *curves = NULL; | ||
290 | size_t crv_len = 0; | ||
291 | size_t n = 0; | ||
292 | |||
293 | crv_len = EC_get_builtin_curves(NULL, 0); | ||
294 | |||
295 | curves = reallocarray(NULL, crv_len, sizeof(EC_builtin_curve)); | ||
296 | if (curves == NULL) | ||
297 | goto end; | ||
298 | |||
299 | if (!EC_get_builtin_curves(curves, crv_len)) { | ||
300 | free(curves); | ||
301 | goto end; | ||
302 | } | ||
303 | for (n = 0; n < crv_len; n++) { | ||
304 | const char *comment; | ||
305 | const char *sname; | ||
306 | comment = curves[n].comment; | ||
307 | sname = OBJ_nid2sn(curves[n].nid); | ||
308 | if (comment == NULL) | ||
309 | comment = "CURVE DESCRIPTION NOT AVAILABLE"; | ||
310 | if (sname == NULL) | ||
311 | sname = ""; | ||
312 | |||
313 | BIO_printf(out, " %-10s: ", sname); | ||
314 | BIO_printf(out, "%s\n", comment); | ||
315 | } | ||
316 | |||
317 | free(curves); | ||
318 | ret = 0; | ||
319 | goto end; | ||
320 | } | ||
321 | if (cfg.curve_name != NULL) { | ||
322 | int nid; | ||
323 | |||
324 | /* | ||
325 | * workaround for the SECG curve names secp192r1 and | ||
326 | * secp256r1 (which are the same as the curves prime192v1 and | ||
327 | * prime256v1 defined in X9.62) | ||
328 | */ | ||
329 | if (!strcmp(cfg.curve_name, "secp192r1")) { | ||
330 | BIO_printf(bio_err, "using curve name prime192v1 " | ||
331 | "instead of secp192r1\n"); | ||
332 | nid = NID_X9_62_prime192v1; | ||
333 | } else if (!strcmp(cfg.curve_name, "secp256r1")) { | ||
334 | BIO_printf(bio_err, "using curve name prime256v1 " | ||
335 | "instead of secp256r1\n"); | ||
336 | nid = NID_X9_62_prime256v1; | ||
337 | } else | ||
338 | nid = OBJ_sn2nid(cfg.curve_name); | ||
339 | |||
340 | if (nid == 0) | ||
341 | nid = EC_curve_nist2nid(cfg.curve_name); | ||
342 | |||
343 | if (nid == 0) { | ||
344 | BIO_printf(bio_err, "unknown curve name (%s)\n", | ||
345 | cfg.curve_name); | ||
346 | goto end; | ||
347 | } | ||
348 | group = EC_GROUP_new_by_curve_name(nid); | ||
349 | if (group == NULL) { | ||
350 | BIO_printf(bio_err, "unable to create curve (%s)\n", | ||
351 | cfg.curve_name); | ||
352 | goto end; | ||
353 | } | ||
354 | EC_GROUP_set_asn1_flag(group, cfg.asn1_flag); | ||
355 | EC_GROUP_set_point_conversion_form(group, cfg.form); | ||
356 | } else if (cfg.informat == FORMAT_ASN1) { | ||
357 | group = d2i_ECPKParameters_bio(in, NULL); | ||
358 | } else if (cfg.informat == FORMAT_PEM) { | ||
359 | group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL); | ||
360 | } else { | ||
361 | BIO_printf(bio_err, "bad input format specified\n"); | ||
362 | goto end; | ||
363 | } | ||
364 | |||
365 | if (group == NULL) { | ||
366 | BIO_printf(bio_err, | ||
367 | "unable to load elliptic curve parameters\n"); | ||
368 | ERR_print_errors(bio_err); | ||
369 | goto end; | ||
370 | } | ||
371 | if (cfg.new_form) | ||
372 | EC_GROUP_set_point_conversion_form(group, cfg.form); | ||
373 | |||
374 | if (cfg.new_asn1_flag) | ||
375 | EC_GROUP_set_asn1_flag(group, cfg.asn1_flag); | ||
376 | |||
377 | if (cfg.no_seed) | ||
378 | EC_GROUP_set_seed(group, NULL, 0); | ||
379 | |||
380 | if (cfg.text) { | ||
381 | if (!ECPKParameters_print(out, group, 0)) | ||
382 | goto end; | ||
383 | } | ||
384 | if (cfg.check) { | ||
385 | BIO_printf(bio_err, "checking elliptic curve parameters: "); | ||
386 | if (!EC_GROUP_check(group, NULL)) { | ||
387 | BIO_printf(bio_err, "failed\n"); | ||
388 | ERR_print_errors(bio_err); | ||
389 | } else | ||
390 | BIO_printf(bio_err, "ok\n"); | ||
391 | |||
392 | } | ||
393 | if (!cfg.noout) { | ||
394 | if (cfg.outformat == FORMAT_ASN1) | ||
395 | i = i2d_ECPKParameters_bio(out, group); | ||
396 | else if (cfg.outformat == FORMAT_PEM) | ||
397 | i = PEM_write_bio_ECPKParameters(out, group); | ||
398 | else { | ||
399 | BIO_printf(bio_err, "bad output format specified for" | ||
400 | " outfile\n"); | ||
401 | goto end; | ||
402 | } | ||
403 | if (!i) { | ||
404 | BIO_printf(bio_err, "unable to write elliptic " | ||
405 | "curve parameters\n"); | ||
406 | ERR_print_errors(bio_err); | ||
407 | goto end; | ||
408 | } | ||
409 | } | ||
410 | if (cfg.genkey) { | ||
411 | EC_KEY *eckey = EC_KEY_new(); | ||
412 | |||
413 | if (eckey == NULL) | ||
414 | goto end; | ||
415 | |||
416 | if (EC_KEY_set_group(eckey, group) == 0) { | ||
417 | EC_KEY_free(eckey); | ||
418 | goto end; | ||
419 | } | ||
420 | |||
421 | if (!EC_KEY_generate_key(eckey)) { | ||
422 | EC_KEY_free(eckey); | ||
423 | goto end; | ||
424 | } | ||
425 | if (cfg.outformat == FORMAT_ASN1) | ||
426 | i = i2d_ECPrivateKey_bio(out, eckey); | ||
427 | else if (cfg.outformat == FORMAT_PEM) | ||
428 | i = PEM_write_bio_ECPrivateKey(out, eckey, NULL, | ||
429 | NULL, 0, NULL, NULL); | ||
430 | else { | ||
431 | BIO_printf(bio_err, "bad output format specified " | ||
432 | "for outfile\n"); | ||
433 | EC_KEY_free(eckey); | ||
434 | goto end; | ||
435 | } | ||
436 | EC_KEY_free(eckey); | ||
437 | } | ||
438 | ret = 0; | ||
439 | |||
440 | end: | ||
441 | BIO_free(in); | ||
442 | BIO_free_all(out); | ||
443 | EC_GROUP_free(group); | ||
444 | |||
445 | return (ret); | ||
446 | } | ||
447 | |||
448 | #endif | ||