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