Annotation of src/usr.bin/openssl/genpkey.c, Revision 1.4
1.4 ! jsing 1: /* $OpenBSD: genpkey.c,v 1.3 2014/08/28 14:25:48 jsing Exp $ */
1.1 jsing 2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3: * project 2006
4: */
5: /* ====================================================================
6: * Copyright (c) 2006 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: * licensing@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: #include <stdio.h>
60: #include <string.h>
61:
62: #include "apps.h"
63:
64: #include <openssl/err.h>
65: #include <openssl/evp.h>
66: #include <openssl/pem.h>
67:
68: #ifndef OPENSSL_NO_ENGINE
69: #include <openssl/engine.h>
70: #endif
71:
72: static int
73: init_keygen_file(BIO * err, EVP_PKEY_CTX ** pctx, const char *file,
74: ENGINE * e);
75: static int genpkey_cb(EVP_PKEY_CTX * ctx);
76:
77: int
78: genpkey_main(int argc, char **argv)
79: {
80: ENGINE *e = NULL;
81: char **args, *outfile = NULL;
82: char *passarg = NULL;
83: BIO *in = NULL, *out = NULL;
84: const EVP_CIPHER *cipher = NULL;
85: int outformat;
86: int text = 0;
87: EVP_PKEY *pkey = NULL;
88: EVP_PKEY_CTX *ctx = NULL;
89: char *pass = NULL;
90: int badarg = 0;
91: int ret = 1, rv;
92:
93: int do_param = 0;
94:
95: outformat = FORMAT_PEM;
96:
97: args = argv + 1;
98: while (!badarg && *args && *args[0] == '-') {
99: if (!strcmp(*args, "-outform")) {
100: if (args[1]) {
101: args++;
102: outformat = str2fmt(*args);
103: } else
104: badarg = 1;
105: } else if (!strcmp(*args, "-pass")) {
106: if (!args[1])
107: goto bad;
108: passarg = *(++args);
109: }
110: #ifndef OPENSSL_NO_ENGINE
111: else if (strcmp(*args, "-engine") == 0) {
112: if (!args[1])
113: goto bad;
114: e = setup_engine(bio_err, *(++args), 0);
115: }
116: #endif
117: else if (!strcmp(*args, "-paramfile")) {
118: if (!args[1])
119: goto bad;
120: args++;
121: if (do_param == 1)
122: goto bad;
123: if (!init_keygen_file(bio_err, &ctx, *args, e))
124: goto end;
125: } else if (!strcmp(*args, "-out")) {
126: if (args[1]) {
127: args++;
128: outfile = *args;
129: } else
130: badarg = 1;
131: } else if (strcmp(*args, "-algorithm") == 0) {
132: if (!args[1])
133: goto bad;
134: if (!init_gen_str(bio_err, &ctx, *(++args), e, do_param))
135: goto end;
136: } else if (strcmp(*args, "-pkeyopt") == 0) {
137: if (!args[1])
138: goto bad;
139: if (!ctx) {
140: BIO_puts(bio_err, "No keytype specified\n");
141: goto bad;
142: } else if (pkey_ctrl_string(ctx, *(++args)) <= 0) {
143: BIO_puts(bio_err, "parameter setting error\n");
144: ERR_print_errors(bio_err);
145: goto end;
146: }
147: } else if (strcmp(*args, "-genparam") == 0) {
148: if (ctx)
149: goto bad;
150: do_param = 1;
151: } else if (strcmp(*args, "-text") == 0)
152: text = 1;
153: else {
154: cipher = EVP_get_cipherbyname(*args + 1);
155: if (!cipher) {
156: BIO_printf(bio_err, "Unknown cipher %s\n",
157: *args + 1);
158: badarg = 1;
159: }
160: if (do_param == 1)
161: badarg = 1;
162: }
163: args++;
164: }
165:
166: if (!ctx)
167: badarg = 1;
168:
169: if (badarg) {
170: bad:
171: BIO_printf(bio_err, "Usage: genpkey [options]\n");
172: BIO_printf(bio_err, "where options may be\n");
173: BIO_printf(bio_err, "-out file output file\n");
174: BIO_printf(bio_err, "-outform X output format (DER or PEM)\n");
175: BIO_printf(bio_err, "-pass arg output file pass phrase source\n");
176: BIO_printf(bio_err, "-<cipher> use cipher <cipher> to encrypt the key\n");
177: #ifndef OPENSSL_NO_ENGINE
178: BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
179: #endif
180: BIO_printf(bio_err, "-paramfile file parameters file\n");
181: BIO_printf(bio_err, "-algorithm alg the public key algorithm\n");
182: BIO_printf(bio_err, "-pkeyopt opt:value set the public key algorithm option <opt>\n"
183: " to value <value>\n");
184: BIO_printf(bio_err, "-genparam generate parameters, not key\n");
185: BIO_printf(bio_err, "-text print the in text\n");
186: BIO_printf(bio_err, "NB: options order may be important! See the manual page.\n");
187: goto end;
188: }
189: if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
190: BIO_puts(bio_err, "Error getting password\n");
191: goto end;
192: }
193: if (outfile) {
194: if (!(out = BIO_new_file(outfile, "wb"))) {
195: BIO_printf(bio_err,
196: "Can't open output file %s\n", outfile);
197: goto end;
198: }
199: } else {
200: out = BIO_new_fp(stdout, BIO_NOCLOSE);
201: }
202:
203: EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
204: EVP_PKEY_CTX_set_app_data(ctx, bio_err);
205:
206: if (do_param) {
207: if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
208: BIO_puts(bio_err, "Error generating parameters\n");
209: ERR_print_errors(bio_err);
210: goto end;
211: }
212: } else {
213: if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
214: BIO_puts(bio_err, "Error generating key\n");
215: ERR_print_errors(bio_err);
216: goto end;
217: }
218: }
219:
220: if (do_param)
221: rv = PEM_write_bio_Parameters(out, pkey);
222: else if (outformat == FORMAT_PEM)
223: rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0,
224: NULL, pass);
225: else if (outformat == FORMAT_ASN1)
226: rv = i2d_PrivateKey_bio(out, pkey);
227: else {
228: BIO_printf(bio_err, "Bad format specified for key\n");
229: goto end;
230: }
231:
232: if (rv <= 0) {
233: BIO_puts(bio_err, "Error writing key\n");
234: ERR_print_errors(bio_err);
235: }
236: if (text) {
237: if (do_param)
238: rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
239: else
240: rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
241:
242: if (rv <= 0) {
243: BIO_puts(bio_err, "Error printing key\n");
244: ERR_print_errors(bio_err);
245: }
246: }
247: ret = 0;
248:
249: end:
250: if (pkey)
251: EVP_PKEY_free(pkey);
252: if (ctx)
253: EVP_PKEY_CTX_free(ctx);
254: if (out)
255: BIO_free_all(out);
256: BIO_free(in);
257: free(pass);
258:
259: return ret;
260: }
261:
262: static int
263: init_keygen_file(BIO * err, EVP_PKEY_CTX ** pctx,
264: const char *file, ENGINE * e)
265: {
266: BIO *pbio;
267: EVP_PKEY *pkey = NULL;
268: EVP_PKEY_CTX *ctx = NULL;
269: if (*pctx) {
270: BIO_puts(err, "Parameters already set!\n");
271: return 0;
272: }
273: pbio = BIO_new_file(file, "r");
274: if (!pbio) {
275: BIO_printf(err, "Can't open parameter file %s\n", file);
276: return 0;
277: }
278: pkey = PEM_read_bio_Parameters(pbio, NULL);
279: BIO_free(pbio);
280:
281: if (!pkey) {
282: BIO_printf(bio_err, "Error reading parameter file %s\n", file);
283: return 0;
284: }
285: ctx = EVP_PKEY_CTX_new(pkey, e);
286: if (!ctx)
287: goto err;
288: if (EVP_PKEY_keygen_init(ctx) <= 0)
289: goto err;
290: EVP_PKEY_free(pkey);
291: *pctx = ctx;
292: return 1;
293:
294: err:
295: BIO_puts(err, "Error initializing context\n");
296: ERR_print_errors(err);
297: if (ctx)
298: EVP_PKEY_CTX_free(ctx);
299: if (pkey)
300: EVP_PKEY_free(pkey);
301: return 0;
302:
303: }
304:
305: int
306: init_gen_str(BIO * err, EVP_PKEY_CTX ** pctx,
307: const char *algname, ENGINE * e, int do_param)
308: {
309: EVP_PKEY_CTX *ctx = NULL;
310: const EVP_PKEY_ASN1_METHOD *ameth;
311: ENGINE *tmpeng = NULL;
312: int pkey_id;
313:
314: if (*pctx) {
315: BIO_puts(err, "Algorithm already set!\n");
316: return 0;
317: }
318: ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
319:
320: #ifndef OPENSSL_NO_ENGINE
321: if (!ameth && e)
322: ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
323: #endif
324:
325: if (!ameth) {
326: BIO_printf(bio_err, "Algorithm %s not found\n", algname);
327: return 0;
328: }
329: ERR_clear_error();
330:
331: EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
332: #ifndef OPENSSL_NO_ENGINE
333: if (tmpeng)
334: ENGINE_finish(tmpeng);
335: #endif
336: ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
337:
338: if (!ctx)
339: goto err;
340: if (do_param) {
341: if (EVP_PKEY_paramgen_init(ctx) <= 0)
342: goto err;
343: } else {
344: if (EVP_PKEY_keygen_init(ctx) <= 0)
345: goto err;
346: }
347:
348: *pctx = ctx;
349: return 1;
350:
351: err:
352: BIO_printf(err, "Error initializing %s context\n", algname);
353: ERR_print_errors(err);
354: if (ctx)
355: EVP_PKEY_CTX_free(ctx);
356: return 0;
357:
358: }
359:
360: static int
361: genpkey_cb(EVP_PKEY_CTX * ctx)
362: {
363: char c = '*';
364: BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
365: int p;
366: p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
367: if (p == 0)
368: c = '.';
369: if (p == 1)
370: c = '+';
371: if (p == 2)
372: c = '*';
373: if (p == 3)
374: c = '\n';
375: BIO_write(b, &c, 1);
376: (void) BIO_flush(b);
377: return 1;
378: }