1ebfedea0SLionel Sambuc /* apps/genpkey.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc * 2006
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <string.h>
61ebfedea0SLionel Sambuc #include "apps.h"
62ebfedea0SLionel Sambuc #include <openssl/pem.h>
63ebfedea0SLionel Sambuc #include <openssl/err.h>
64ebfedea0SLionel Sambuc #include <openssl/evp.h>
65ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
66ebfedea0SLionel Sambuc # include <openssl/engine.h>
67ebfedea0SLionel Sambuc #endif
68ebfedea0SLionel Sambuc
69ebfedea0SLionel Sambuc static int init_keygen_file(BIO *err, EVP_PKEY_CTX **pctx,
70ebfedea0SLionel Sambuc const char *file, ENGINE *e);
71ebfedea0SLionel Sambuc static int genpkey_cb(EVP_PKEY_CTX *ctx);
72ebfedea0SLionel Sambuc
73ebfedea0SLionel Sambuc #define PROG genpkey_main
74ebfedea0SLionel Sambuc
75ebfedea0SLionel Sambuc int MAIN(int, char **);
76ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)77ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
78ebfedea0SLionel Sambuc {
79ebfedea0SLionel Sambuc ENGINE *e = NULL;
80ebfedea0SLionel Sambuc char **args, *outfile = NULL;
81ebfedea0SLionel Sambuc char *passarg = NULL;
82ebfedea0SLionel Sambuc BIO *in = NULL, *out = NULL;
83ebfedea0SLionel Sambuc const EVP_CIPHER *cipher = NULL;
84ebfedea0SLionel Sambuc int outformat;
85ebfedea0SLionel Sambuc int text = 0;
86ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
87ebfedea0SLionel Sambuc EVP_PKEY_CTX *ctx = NULL;
88ebfedea0SLionel Sambuc char *pass = NULL;
89ebfedea0SLionel Sambuc int badarg = 0;
90ebfedea0SLionel Sambuc int ret = 1, rv;
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc int do_param = 0;
93ebfedea0SLionel Sambuc
94ebfedea0SLionel Sambuc if (bio_err == NULL)
95ebfedea0SLionel Sambuc bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc if (!load_config(bio_err, NULL))
98ebfedea0SLionel Sambuc goto end;
99ebfedea0SLionel Sambuc
100ebfedea0SLionel Sambuc outformat = FORMAT_PEM;
101ebfedea0SLionel Sambuc
102ebfedea0SLionel Sambuc ERR_load_crypto_strings();
103ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms();
104ebfedea0SLionel Sambuc args = argv + 1;
105*0a6a1f1dSLionel Sambuc while (!badarg && *args && *args[0] == '-') {
106*0a6a1f1dSLionel Sambuc if (!strcmp(*args, "-outform")) {
107*0a6a1f1dSLionel Sambuc if (args[1]) {
108ebfedea0SLionel Sambuc args++;
109ebfedea0SLionel Sambuc outformat = str2fmt(*args);
110*0a6a1f1dSLionel Sambuc } else
111*0a6a1f1dSLionel Sambuc badarg = 1;
112*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-pass")) {
113*0a6a1f1dSLionel Sambuc if (!args[1])
114*0a6a1f1dSLionel Sambuc goto bad;
115ebfedea0SLionel Sambuc passarg = *(++args);
116ebfedea0SLionel Sambuc }
117ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
118*0a6a1f1dSLionel Sambuc else if (strcmp(*args, "-engine") == 0) {
119ebfedea0SLionel Sambuc if (!args[1])
120ebfedea0SLionel Sambuc goto bad;
121ebfedea0SLionel Sambuc e = setup_engine(bio_err, *(++args), 0);
122ebfedea0SLionel Sambuc }
123ebfedea0SLionel Sambuc #endif
124*0a6a1f1dSLionel Sambuc else if (!strcmp(*args, "-paramfile")) {
125ebfedea0SLionel Sambuc if (!args[1])
126ebfedea0SLionel Sambuc goto bad;
127ebfedea0SLionel Sambuc args++;
128ebfedea0SLionel Sambuc if (do_param == 1)
129ebfedea0SLionel Sambuc goto bad;
130ebfedea0SLionel Sambuc if (!init_keygen_file(bio_err, &ctx, *args, e))
131ebfedea0SLionel Sambuc goto end;
132*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-out")) {
133*0a6a1f1dSLionel Sambuc if (args[1]) {
134ebfedea0SLionel Sambuc args++;
135ebfedea0SLionel Sambuc outfile = *args;
136*0a6a1f1dSLionel Sambuc } else
137*0a6a1f1dSLionel Sambuc badarg = 1;
138*0a6a1f1dSLionel Sambuc } else if (strcmp(*args, "-algorithm") == 0) {
139ebfedea0SLionel Sambuc if (!args[1])
140ebfedea0SLionel Sambuc goto bad;
141ebfedea0SLionel Sambuc if (!init_gen_str(bio_err, &ctx, *(++args), e, do_param))
142ebfedea0SLionel Sambuc goto end;
143*0a6a1f1dSLionel Sambuc } else if (strcmp(*args, "-pkeyopt") == 0) {
144ebfedea0SLionel Sambuc if (!args[1])
145ebfedea0SLionel Sambuc goto bad;
146*0a6a1f1dSLionel Sambuc if (!ctx) {
147ebfedea0SLionel Sambuc BIO_puts(bio_err, "No keytype specified\n");
148ebfedea0SLionel Sambuc goto bad;
149*0a6a1f1dSLionel Sambuc } else if (pkey_ctrl_string(ctx, *(++args)) <= 0) {
150ebfedea0SLionel Sambuc BIO_puts(bio_err, "parameter setting error\n");
151ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
152ebfedea0SLionel Sambuc goto end;
153ebfedea0SLionel Sambuc }
154*0a6a1f1dSLionel Sambuc } else if (strcmp(*args, "-genparam") == 0) {
155ebfedea0SLionel Sambuc if (ctx)
156ebfedea0SLionel Sambuc goto bad;
157ebfedea0SLionel Sambuc do_param = 1;
158*0a6a1f1dSLionel Sambuc } else if (strcmp(*args, "-text") == 0)
159ebfedea0SLionel Sambuc text = 1;
160*0a6a1f1dSLionel Sambuc else {
161ebfedea0SLionel Sambuc cipher = EVP_get_cipherbyname(*args + 1);
162*0a6a1f1dSLionel Sambuc if (!cipher) {
163*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Unknown cipher %s\n", *args + 1);
164ebfedea0SLionel Sambuc badarg = 1;
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc if (do_param == 1)
167ebfedea0SLionel Sambuc badarg = 1;
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc args++;
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc
172ebfedea0SLionel Sambuc if (!ctx)
173ebfedea0SLionel Sambuc badarg = 1;
174ebfedea0SLionel Sambuc
175*0a6a1f1dSLionel Sambuc if (badarg) {
176ebfedea0SLionel Sambuc bad:
177ebfedea0SLionel Sambuc BIO_printf(bio_err, "Usage: genpkey [options]\n");
178ebfedea0SLionel Sambuc BIO_printf(bio_err, "where options may be\n");
179ebfedea0SLionel Sambuc BIO_printf(bio_err, "-out file output file\n");
180*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
181*0a6a1f1dSLionel Sambuc "-outform X output format (DER or PEM)\n");
182*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
183*0a6a1f1dSLionel Sambuc "-pass arg output file pass phrase source\n");
184*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
185*0a6a1f1dSLionel Sambuc "-<cipher> use cipher <cipher> to encrypt the key\n");
186ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
187*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
188*0a6a1f1dSLionel Sambuc "-engine e use engine e, possibly a hardware device.\n");
189ebfedea0SLionel Sambuc #endif
190ebfedea0SLionel Sambuc BIO_printf(bio_err, "-paramfile file parameters file\n");
191ebfedea0SLionel Sambuc BIO_printf(bio_err, "-algorithm alg the public key algorithm\n");
192*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
193*0a6a1f1dSLionel Sambuc "-pkeyopt opt:value set the public key algorithm option <opt>\n"
194ebfedea0SLionel Sambuc " to value <value>\n");
195*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
196*0a6a1f1dSLionel Sambuc "-genparam generate parameters, not key\n");
197ebfedea0SLionel Sambuc BIO_printf(bio_err, "-text print the in text\n");
198*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
199*0a6a1f1dSLionel Sambuc "NB: options order may be important! See the manual page.\n");
200ebfedea0SLionel Sambuc goto end;
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc
203*0a6a1f1dSLionel Sambuc if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
204ebfedea0SLionel Sambuc BIO_puts(bio_err, "Error getting password\n");
205ebfedea0SLionel Sambuc goto end;
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc
208*0a6a1f1dSLionel Sambuc if (outfile) {
209*0a6a1f1dSLionel Sambuc if (!(out = BIO_new_file(outfile, "wb"))) {
210*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Can't open output file %s\n", outfile);
211ebfedea0SLionel Sambuc goto end;
212ebfedea0SLionel Sambuc }
213*0a6a1f1dSLionel Sambuc } else {
214ebfedea0SLionel Sambuc out = BIO_new_fp(stdout, BIO_NOCLOSE);
215ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
216ebfedea0SLionel Sambuc {
217ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
218ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
219ebfedea0SLionel Sambuc }
220ebfedea0SLionel Sambuc #endif
221ebfedea0SLionel Sambuc }
222ebfedea0SLionel Sambuc
223ebfedea0SLionel Sambuc EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
224ebfedea0SLionel Sambuc EVP_PKEY_CTX_set_app_data(ctx, bio_err);
225ebfedea0SLionel Sambuc
226*0a6a1f1dSLionel Sambuc if (do_param) {
227*0a6a1f1dSLionel Sambuc if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
228ebfedea0SLionel Sambuc BIO_puts(bio_err, "Error generating parameters\n");
229ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
230ebfedea0SLionel Sambuc goto end;
231ebfedea0SLionel Sambuc }
232*0a6a1f1dSLionel Sambuc } else {
233*0a6a1f1dSLionel Sambuc if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
234ebfedea0SLionel Sambuc BIO_puts(bio_err, "Error generating key\n");
235ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
236ebfedea0SLionel Sambuc goto end;
237ebfedea0SLionel Sambuc }
238ebfedea0SLionel Sambuc }
239ebfedea0SLionel Sambuc
240ebfedea0SLionel Sambuc if (do_param)
241ebfedea0SLionel Sambuc rv = PEM_write_bio_Parameters(out, pkey);
242ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM)
243*0a6a1f1dSLionel Sambuc rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
244ebfedea0SLionel Sambuc else if (outformat == FORMAT_ASN1)
245ebfedea0SLionel Sambuc rv = i2d_PrivateKey_bio(out, pkey);
246*0a6a1f1dSLionel Sambuc else {
247ebfedea0SLionel Sambuc BIO_printf(bio_err, "Bad format specified for key\n");
248ebfedea0SLionel Sambuc goto end;
249ebfedea0SLionel Sambuc }
250ebfedea0SLionel Sambuc
251*0a6a1f1dSLionel Sambuc if (rv <= 0) {
252ebfedea0SLionel Sambuc BIO_puts(bio_err, "Error writing key\n");
253ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
254ebfedea0SLionel Sambuc }
255ebfedea0SLionel Sambuc
256*0a6a1f1dSLionel Sambuc if (text) {
257ebfedea0SLionel Sambuc if (do_param)
258ebfedea0SLionel Sambuc rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
259ebfedea0SLionel Sambuc else
260ebfedea0SLionel Sambuc rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
261ebfedea0SLionel Sambuc
262*0a6a1f1dSLionel Sambuc if (rv <= 0) {
263ebfedea0SLionel Sambuc BIO_puts(bio_err, "Error printing key\n");
264ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
265ebfedea0SLionel Sambuc }
266ebfedea0SLionel Sambuc }
267ebfedea0SLionel Sambuc
268ebfedea0SLionel Sambuc ret = 0;
269ebfedea0SLionel Sambuc
270ebfedea0SLionel Sambuc end:
271ebfedea0SLionel Sambuc if (pkey)
272ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
273ebfedea0SLionel Sambuc if (ctx)
274ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(ctx);
275ebfedea0SLionel Sambuc if (out)
276ebfedea0SLionel Sambuc BIO_free_all(out);
277ebfedea0SLionel Sambuc BIO_free(in);
278ebfedea0SLionel Sambuc if (pass)
279ebfedea0SLionel Sambuc OPENSSL_free(pass);
280ebfedea0SLionel Sambuc
281ebfedea0SLionel Sambuc return ret;
282ebfedea0SLionel Sambuc }
283ebfedea0SLionel Sambuc
init_keygen_file(BIO * err,EVP_PKEY_CTX ** pctx,const char * file,ENGINE * e)284ebfedea0SLionel Sambuc static int init_keygen_file(BIO *err, EVP_PKEY_CTX **pctx,
285ebfedea0SLionel Sambuc const char *file, ENGINE *e)
286ebfedea0SLionel Sambuc {
287ebfedea0SLionel Sambuc BIO *pbio;
288ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
289ebfedea0SLionel Sambuc EVP_PKEY_CTX *ctx = NULL;
290*0a6a1f1dSLionel Sambuc if (*pctx) {
291ebfedea0SLionel Sambuc BIO_puts(err, "Parameters already set!\n");
292ebfedea0SLionel Sambuc return 0;
293ebfedea0SLionel Sambuc }
294ebfedea0SLionel Sambuc
295ebfedea0SLionel Sambuc pbio = BIO_new_file(file, "r");
296*0a6a1f1dSLionel Sambuc if (!pbio) {
297ebfedea0SLionel Sambuc BIO_printf(err, "Can't open parameter file %s\n", file);
298ebfedea0SLionel Sambuc return 0;
299ebfedea0SLionel Sambuc }
300ebfedea0SLionel Sambuc
301ebfedea0SLionel Sambuc pkey = PEM_read_bio_Parameters(pbio, NULL);
302ebfedea0SLionel Sambuc BIO_free(pbio);
303ebfedea0SLionel Sambuc
304*0a6a1f1dSLionel Sambuc if (!pkey) {
305ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error reading parameter file %s\n", file);
306ebfedea0SLionel Sambuc return 0;
307ebfedea0SLionel Sambuc }
308ebfedea0SLionel Sambuc
309ebfedea0SLionel Sambuc ctx = EVP_PKEY_CTX_new(pkey, e);
310ebfedea0SLionel Sambuc if (!ctx)
311ebfedea0SLionel Sambuc goto err;
312ebfedea0SLionel Sambuc if (EVP_PKEY_keygen_init(ctx) <= 0)
313ebfedea0SLionel Sambuc goto err;
314ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
315ebfedea0SLionel Sambuc *pctx = ctx;
316ebfedea0SLionel Sambuc return 1;
317ebfedea0SLionel Sambuc
318ebfedea0SLionel Sambuc err:
319ebfedea0SLionel Sambuc BIO_puts(err, "Error initializing context\n");
320ebfedea0SLionel Sambuc ERR_print_errors(err);
321ebfedea0SLionel Sambuc if (ctx)
322ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(ctx);
323ebfedea0SLionel Sambuc if (pkey)
324ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
325ebfedea0SLionel Sambuc return 0;
326ebfedea0SLionel Sambuc
327ebfedea0SLionel Sambuc }
328ebfedea0SLionel Sambuc
init_gen_str(BIO * err,EVP_PKEY_CTX ** pctx,const char * algname,ENGINE * e,int do_param)329ebfedea0SLionel Sambuc int init_gen_str(BIO *err, EVP_PKEY_CTX **pctx,
330ebfedea0SLionel Sambuc const char *algname, ENGINE *e, int do_param)
331ebfedea0SLionel Sambuc {
332ebfedea0SLionel Sambuc EVP_PKEY_CTX *ctx = NULL;
333ebfedea0SLionel Sambuc const EVP_PKEY_ASN1_METHOD *ameth;
334ebfedea0SLionel Sambuc ENGINE *tmpeng = NULL;
335ebfedea0SLionel Sambuc int pkey_id;
336ebfedea0SLionel Sambuc
337*0a6a1f1dSLionel Sambuc if (*pctx) {
338ebfedea0SLionel Sambuc BIO_puts(err, "Algorithm already set!\n");
339ebfedea0SLionel Sambuc return 0;
340ebfedea0SLionel Sambuc }
341ebfedea0SLionel Sambuc
342ebfedea0SLionel Sambuc ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
343ebfedea0SLionel Sambuc
344ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
345ebfedea0SLionel Sambuc if (!ameth && e)
346ebfedea0SLionel Sambuc ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
347ebfedea0SLionel Sambuc #endif
348ebfedea0SLionel Sambuc
349*0a6a1f1dSLionel Sambuc if (!ameth) {
350ebfedea0SLionel Sambuc BIO_printf(bio_err, "Algorithm %s not found\n", algname);
351ebfedea0SLionel Sambuc return 0;
352ebfedea0SLionel Sambuc }
353ebfedea0SLionel Sambuc
354ebfedea0SLionel Sambuc ERR_clear_error();
355ebfedea0SLionel Sambuc
356ebfedea0SLionel Sambuc EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
357ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
358ebfedea0SLionel Sambuc if (tmpeng)
359ebfedea0SLionel Sambuc ENGINE_finish(tmpeng);
360ebfedea0SLionel Sambuc #endif
361ebfedea0SLionel Sambuc ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
362ebfedea0SLionel Sambuc
363ebfedea0SLionel Sambuc if (!ctx)
364ebfedea0SLionel Sambuc goto err;
365*0a6a1f1dSLionel Sambuc if (do_param) {
366ebfedea0SLionel Sambuc if (EVP_PKEY_paramgen_init(ctx) <= 0)
367ebfedea0SLionel Sambuc goto err;
368*0a6a1f1dSLionel Sambuc } else {
369ebfedea0SLionel Sambuc if (EVP_PKEY_keygen_init(ctx) <= 0)
370ebfedea0SLionel Sambuc goto err;
371ebfedea0SLionel Sambuc }
372ebfedea0SLionel Sambuc
373ebfedea0SLionel Sambuc *pctx = ctx;
374ebfedea0SLionel Sambuc return 1;
375ebfedea0SLionel Sambuc
376ebfedea0SLionel Sambuc err:
377ebfedea0SLionel Sambuc BIO_printf(err, "Error initializing %s context\n", algname);
378ebfedea0SLionel Sambuc ERR_print_errors(err);
379ebfedea0SLionel Sambuc if (ctx)
380ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(ctx);
381ebfedea0SLionel Sambuc return 0;
382ebfedea0SLionel Sambuc
383ebfedea0SLionel Sambuc }
384ebfedea0SLionel Sambuc
genpkey_cb(EVP_PKEY_CTX * ctx)385ebfedea0SLionel Sambuc static int genpkey_cb(EVP_PKEY_CTX *ctx)
386ebfedea0SLionel Sambuc {
387ebfedea0SLionel Sambuc char c = '*';
388ebfedea0SLionel Sambuc BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
389ebfedea0SLionel Sambuc int p;
390ebfedea0SLionel Sambuc p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
391*0a6a1f1dSLionel Sambuc if (p == 0)
392*0a6a1f1dSLionel Sambuc c = '.';
393*0a6a1f1dSLionel Sambuc if (p == 1)
394*0a6a1f1dSLionel Sambuc c = '+';
395*0a6a1f1dSLionel Sambuc if (p == 2)
396*0a6a1f1dSLionel Sambuc c = '*';
397*0a6a1f1dSLionel Sambuc if (p == 3)
398*0a6a1f1dSLionel Sambuc c = '\n';
399ebfedea0SLionel Sambuc BIO_write(b, &c, 1);
400ebfedea0SLionel Sambuc (void)BIO_flush(b);
401ebfedea0SLionel Sambuc #ifdef LINT
402ebfedea0SLionel Sambuc p = n;
403ebfedea0SLionel Sambuc #endif
404ebfedea0SLionel Sambuc return 1;
405ebfedea0SLionel Sambuc }
406