]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ec.c
Add "sections" to -help output
[thirdparty/openssl.git] / apps / ec.c
CommitLineData
d4a8f90c 1/*
6738bf14 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
4d94ae00 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
4d94ae00 8 */
4d94ae00 9
6e04afb8 10#include <openssl/opensslconf.h>
effaf4de
RS
11#ifdef OPENSSL_NO_EC
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
0f113f3e
MC
15# include <stdio.h>
16# include <stdlib.h>
17# include <string.h>
18# include "apps.h"
dab2cd68 19# include "progs.h"
0f113f3e
MC
20# include <openssl/bio.h>
21# include <openssl/err.h>
22# include <openssl/evp.h>
23# include <openssl/pem.h>
4d94ae00 24
7e1b7485
RS
25static OPT_PAIR conv_forms[] = {
26 {"compressed", POINT_CONVERSION_COMPRESSED},
27 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
28 {"hybrid", POINT_CONVERSION_HYBRID},
29 {NULL}
30};
4d94ae00 31
7e1b7485
RS
32static OPT_PAIR param_enc[] = {
33 {"named_curve", OPENSSL_EC_NAMED_CURVE},
34 {"explicit", 0},
35 {NULL}
36};
37
38typedef enum OPTION_choice {
39 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
40 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
41 OPT_NOOUT, OPT_TEXT, OPT_PARAM_OUT, OPT_PUBIN, OPT_PUBOUT,
16754806 42 OPT_PASSIN, OPT_PASSOUT, OPT_PARAM_ENC, OPT_CONV_FORM, OPT_CIPHER,
7565cbc4 43 OPT_NO_PUBLIC, OPT_CHECK
7e1b7485 44} OPTION_CHOICE;
4d94ae00 45
44c83ebd 46const OPTIONS ec_options[] = {
5388f986 47 OPT_SECTION("General"),
7e1b7485 48 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
49# ifndef OPENSSL_NO_ENGINE
50 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
51# endif
52
53 OPT_SECTION("Input"),
d18ba3cc
DSH
54 {"in", OPT_IN, 's', "Input file"},
55 {"inform", OPT_INFORM, 'f', "Input format - DER or PEM"},
5388f986
RS
56 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
57 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
58 {"check", OPT_CHECK, '-', "check key consistency"},
59 {"", OPT_CIPHER, '-', "Any supported cipher"},
60 {"param_enc", OPT_PARAM_ENC, 's',
61 "Specifies the way the ec parameters are encoded"},
62 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
63
64 OPT_SECTION("Output"),
7e1b7485
RS
65 {"out", OPT_OUT, '>', "Output file"},
66 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
7e1b7485
RS
67 {"noout", OPT_NOOUT, '-', "Don't print key out"},
68 {"text", OPT_TEXT, '-', "Print the key"},
69 {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
16e1b281 70 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
16754806 71 {"no_public", OPT_NO_PUBLIC, '-', "exclude public key from private key"},
7e1b7485 72 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
7e1b7485
RS
73 {NULL}
74};
4d94ae00 75
7e1b7485 76int ec_main(int argc, char **argv)
4d94ae00 77{
7e1b7485 78 BIO *in = NULL, *out = NULL;
396ba1ca 79 ENGINE *e = NULL;
0f113f3e
MC
80 EC_KEY *eckey = NULL;
81 const EC_GROUP *group;
0f113f3e 82 const EVP_CIPHER *enc = NULL;
0f113f3e 83 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
333b070e
RS
84 char *infile = NULL, *outfile = NULL, *prog;
85 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
7e1b7485
RS
86 OPTION_CHOICE o;
87 int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_form = 0, new_asn1_flag = 0;
88 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
3b061a00 89 int pubin = 0, pubout = 0, param_out = 0, i, ret = 1, private = 0;
7565cbc4 90 int no_public = 0, check = 0;
4d94ae00 91
7e1b7485
RS
92 prog = opt_init(argc, argv, ec_options);
93 while ((o = opt_next()) != OPT_EOF) {
94 switch (o) {
95 case OPT_EOF:
96 case OPT_ERR:
97 opthelp:
98 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
99 goto end;
100 case OPT_HELP:
101 opt_help(ec_options);
102 ret = 0;
103 goto end;
104 case OPT_INFORM:
d18ba3cc 105 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
7e1b7485
RS
106 goto opthelp;
107 break;
108 case OPT_IN:
109 infile = opt_arg();
110 break;
111 case OPT_OUTFORM:
112 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
113 goto opthelp;
114 break;
115 case OPT_OUT:
116 outfile = opt_arg();
117 break;
118 case OPT_NOOUT:
0f113f3e 119 noout = 1;
7e1b7485
RS
120 break;
121 case OPT_TEXT:
0f113f3e 122 text = 1;
7e1b7485
RS
123 break;
124 case OPT_PARAM_OUT:
0f113f3e 125 param_out = 1;
7e1b7485
RS
126 break;
127 case OPT_PUBIN:
0f113f3e 128 pubin = 1;
7e1b7485
RS
129 break;
130 case OPT_PUBOUT:
0f113f3e 131 pubout = 1;
7e1b7485
RS
132 break;
133 case OPT_PASSIN:
134 passinarg = opt_arg();
135 break;
136 case OPT_PASSOUT:
137 passoutarg = opt_arg();
138 break;
139 case OPT_ENGINE:
d18ba3cc 140 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
141 break;
142 case OPT_CIPHER:
143 if (!opt_cipher(opt_unknown(), &enc))
144 goto opthelp;
cc630cdb 145 break;
7e1b7485
RS
146 case OPT_CONV_FORM:
147 if (!opt_pair(opt_arg(), conv_forms, &i))
148 goto opthelp;
149 new_form = 1;
150 form = i;
151 break;
152 case OPT_PARAM_ENC:
153 if (!opt_pair(opt_arg(), param_enc, &i))
154 goto opthelp;
155 new_asn1_flag = 1;
156 asn1_flag = i;
0f113f3e 157 break;
16754806
DSH
158 case OPT_NO_PUBLIC:
159 no_public = 1;
160 break;
7565cbc4
DSH
161 case OPT_CHECK:
162 check = 1;
163 break;
0f113f3e 164 }
0f113f3e 165 }
7e1b7485 166 argc = opt_num_rest();
03358517
KR
167 if (argc != 0)
168 goto opthelp;
169
3b061a00 170 private = param_out || pubin || pubout ? 0 : 1;
7eff6aa0 171 if (text && !pubin)
3b061a00 172 private = 1;
4d94ae00 173
7e1b7485 174 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
175 BIO_printf(bio_err, "Error getting passwords\n");
176 goto end;
177 }
4d94ae00 178
d18ba3cc
DSH
179 if (informat != FORMAT_ENGINE) {
180 in = bio_open_default(infile, 'r', informat);
181 if (in == NULL)
182 goto end;
183 }
4d94ae00 184
0f113f3e
MC
185 BIO_printf(bio_err, "read EC key\n");
186 if (informat == FORMAT_ASN1) {
187 if (pubin)
188 eckey = d2i_EC_PUBKEY_bio(in, NULL);
189 else
190 eckey = d2i_ECPrivateKey_bio(in, NULL);
d18ba3cc
DSH
191 } else if (informat == FORMAT_ENGINE) {
192 EVP_PKEY *pkey;
193 if (pubin)
28b86f31 194 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
d18ba3cc
DSH
195 else
196 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
197 if (pkey != NULL) {
198 eckey = EVP_PKEY_get1_EC_KEY(pkey);
199 EVP_PKEY_free(pkey);
200 }
7e1b7485 201 } else {
0f113f3e
MC
202 if (pubin)
203 eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
204 else
205 eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
0f113f3e
MC
206 }
207 if (eckey == NULL) {
208 BIO_printf(bio_err, "unable to load Key\n");
209 ERR_print_errors(bio_err);
210 goto end;
211 }
4d94ae00 212
bdd58d98 213 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
214 if (out == NULL)
215 goto end;
4d94ae00 216
0f113f3e 217 group = EC_KEY_get0_group(eckey);
9dd84053 218
0f113f3e
MC
219 if (new_form)
220 EC_KEY_set_conv_form(eckey, form);
4d94ae00 221
0f113f3e
MC
222 if (new_asn1_flag)
223 EC_KEY_set_asn1_flag(eckey, asn1_flag);
eefa6e4e 224
16754806
DSH
225 if (no_public)
226 EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
227
3b061a00 228 if (text) {
7eff6aa0 229 assert(pubin || private);
0f113f3e
MC
230 if (!EC_KEY_print(out, eckey, 0)) {
231 perror(outfile);
232 ERR_print_errors(bio_err);
233 goto end;
234 }
3b061a00 235 }
4d94ae00 236
7565cbc4
DSH
237 if (check) {
238 if (EC_KEY_check_key(eckey) == 1) {
239 BIO_printf(bio_err, "EC Key valid.\n");
240 } else {
241 BIO_printf(bio_err, "EC Key Invalid!\n");
242 ERR_print_errors(bio_err);
243 }
244 }
245
0f113f3e
MC
246 if (noout) {
247 ret = 0;
248 goto end;
249 }
eefa6e4e 250
0f113f3e
MC
251 BIO_printf(bio_err, "writing EC key\n");
252 if (outformat == FORMAT_ASN1) {
2234212c 253 if (param_out) {
0f113f3e 254 i = i2d_ECPKParameters_bio(out, group);
2234212c 255 } else if (pubin || pubout) {
0f113f3e 256 i = i2d_EC_PUBKEY_bio(out, eckey);
2234212c 257 } else {
3b061a00 258 assert(private);
0f113f3e 259 i = i2d_ECPrivateKey_bio(out, eckey);
3b061a00 260 }
7e1b7485 261 } else {
2234212c 262 if (param_out) {
0f113f3e 263 i = PEM_write_bio_ECPKParameters(out, group);
2234212c 264 } else if (pubin || pubout) {
0f113f3e 265 i = PEM_write_bio_EC_PUBKEY(out, eckey);
2234212c 266 } else {
3b061a00 267 assert(private);
0f113f3e
MC
268 i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
269 NULL, 0, NULL, passout);
3b061a00 270 }
0f113f3e 271 }
eefa6e4e 272
0f113f3e
MC
273 if (!i) {
274 BIO_printf(bio_err, "unable to write private key\n");
275 ERR_print_errors(bio_err);
2234212c 276 } else {
0f113f3e 277 ret = 0;
2234212c 278 }
0f113f3e 279 end:
ca3a82c3
RS
280 BIO_free(in);
281 BIO_free_all(out);
8fdc3734 282 EC_KEY_free(eckey);
dd1abd44 283 release_engine(e);
b548a1f1
RS
284 OPENSSL_free(passin);
285 OPENSSL_free(passout);
26a7d938 286 return ret;
4d94ae00
BM
287}
288#endif