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