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