1 /* 2 * Copyright 2006-2016 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 <stdio.h> 11 #include <string.h> 12 #include "apps.h" 13 #include <openssl/pem.h> 14 #include <openssl/err.h> 15 #include <openssl/evp.h> 16 #ifndef OPENSSL_NO_ENGINE 17 # include <openssl/engine.h> 18 #endif 19 20 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e); 21 static int genpkey_cb(EVP_PKEY_CTX *ctx); 22 23 typedef enum OPTION_choice { 24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, 25 OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE, 26 OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER 27 } OPTION_CHOICE; 28 29 OPTIONS genpkey_options[] = { 30 {"help", OPT_HELP, '-', "Display this summary"}, 31 {"out", OPT_OUT, '>', "Output file"}, 32 {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"}, 33 {"pass", OPT_PASS, 's', "Output file pass phrase source"}, 34 {"paramfile", OPT_PARAMFILE, '<', "Parameters file"}, 35 {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"}, 36 {"pkeyopt", OPT_PKEYOPT, 's', 37 "Set the public key algorithm option as opt:value"}, 38 {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"}, 39 {"text", OPT_TEXT, '-', "Print the in text"}, 40 {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"}, 41 #ifndef OPENSSL_NO_ENGINE 42 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, 43 #endif 44 /* This is deliberately last. */ 45 {OPT_HELP_STR, 1, 1, 46 "Order of options may be important! See the documentation.\n"}, 47 {NULL} 48 }; 49 50 int genpkey_main(int argc, char **argv) 51 { 52 BIO *in = NULL, *out = NULL; 53 ENGINE *e = NULL; 54 EVP_PKEY *pkey = NULL; 55 EVP_PKEY_CTX *ctx = NULL; 56 char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog; 57 const EVP_CIPHER *cipher = NULL; 58 OPTION_CHOICE o; 59 int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0; 60 int private = 0; 61 62 prog = opt_init(argc, argv, genpkey_options); 63 while ((o = opt_next()) != OPT_EOF) { 64 switch (o) { 65 case OPT_EOF: 66 case OPT_ERR: 67 opthelp: 68 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 69 goto end; 70 case OPT_HELP: 71 ret = 0; 72 opt_help(genpkey_options); 73 goto end; 74 case OPT_OUTFORM: 75 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) 76 goto opthelp; 77 break; 78 case OPT_OUT: 79 outfile = opt_arg(); 80 break; 81 case OPT_PASS: 82 passarg = opt_arg(); 83 break; 84 case OPT_ENGINE: 85 e = setup_engine(opt_arg(), 0); 86 break; 87 case OPT_PARAMFILE: 88 if (do_param == 1) 89 goto opthelp; 90 if (!init_keygen_file(&ctx, opt_arg(), e)) 91 goto end; 92 break; 93 case OPT_ALGORITHM: 94 if (!init_gen_str(&ctx, opt_arg(), e, do_param)) 95 goto end; 96 break; 97 case OPT_PKEYOPT: 98 if (ctx == NULL) { 99 BIO_printf(bio_err, "%s: No keytype specified.\n", prog); 100 goto opthelp; 101 } 102 if (pkey_ctrl_string(ctx, opt_arg()) <= 0) { 103 BIO_printf(bio_err, 104 "%s: Error setting %s parameter:\n", 105 prog, opt_arg()); 106 ERR_print_errors(bio_err); 107 goto end; 108 } 109 break; 110 case OPT_GENPARAM: 111 if (ctx != NULL) 112 goto opthelp; 113 do_param = 1; 114 break; 115 case OPT_TEXT: 116 text = 1; 117 break; 118 case OPT_CIPHER: 119 if (!opt_cipher(opt_unknown(), &cipher) 120 || do_param == 1) 121 goto opthelp; 122 } 123 } 124 argc = opt_num_rest(); 125 if (argc != 0) 126 goto opthelp; 127 128 private = do_param ? 0 : 1; 129 130 if (ctx == NULL) 131 goto opthelp; 132 133 if (!app_passwd(passarg, NULL, &pass, NULL)) { 134 BIO_puts(bio_err, "Error getting password\n"); 135 goto end; 136 } 137 138 out = bio_open_owner(outfile, outformat, private); 139 if (out == NULL) 140 goto end; 141 142 EVP_PKEY_CTX_set_cb(ctx, genpkey_cb); 143 EVP_PKEY_CTX_set_app_data(ctx, bio_err); 144 145 if (do_param) { 146 if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) { 147 BIO_puts(bio_err, "Error generating parameters\n"); 148 ERR_print_errors(bio_err); 149 goto end; 150 } 151 } else { 152 if (EVP_PKEY_keygen(ctx, &pkey) <= 0) { 153 BIO_puts(bio_err, "Error generating key\n"); 154 ERR_print_errors(bio_err); 155 goto end; 156 } 157 } 158 159 if (do_param) 160 rv = PEM_write_bio_Parameters(out, pkey); 161 else if (outformat == FORMAT_PEM) { 162 assert(private); 163 rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass); 164 } else if (outformat == FORMAT_ASN1) { 165 assert(private); 166 rv = i2d_PrivateKey_bio(out, pkey); 167 } else { 168 BIO_printf(bio_err, "Bad format specified for key\n"); 169 goto end; 170 } 171 172 if (rv <= 0) { 173 BIO_puts(bio_err, "Error writing key\n"); 174 ERR_print_errors(bio_err); 175 } 176 177 if (text) { 178 if (do_param) 179 rv = EVP_PKEY_print_params(out, pkey, 0, NULL); 180 else 181 rv = EVP_PKEY_print_private(out, pkey, 0, NULL); 182 183 if (rv <= 0) { 184 BIO_puts(bio_err, "Error printing key\n"); 185 ERR_print_errors(bio_err); 186 } 187 } 188 189 ret = 0; 190 191 end: 192 EVP_PKEY_free(pkey); 193 EVP_PKEY_CTX_free(ctx); 194 BIO_free_all(out); 195 BIO_free(in); 196 release_engine(e); 197 OPENSSL_free(pass); 198 return ret; 199 } 200 201 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e) 202 { 203 BIO *pbio; 204 EVP_PKEY *pkey = NULL; 205 EVP_PKEY_CTX *ctx = NULL; 206 if (*pctx) { 207 BIO_puts(bio_err, "Parameters already set!\n"); 208 return 0; 209 } 210 211 pbio = BIO_new_file(file, "r"); 212 if (!pbio) { 213 BIO_printf(bio_err, "Can't open parameter file %s\n", file); 214 return 0; 215 } 216 217 pkey = PEM_read_bio_Parameters(pbio, NULL); 218 BIO_free(pbio); 219 220 if (!pkey) { 221 BIO_printf(bio_err, "Error reading parameter file %s\n", file); 222 return 0; 223 } 224 225 ctx = EVP_PKEY_CTX_new(pkey, e); 226 if (ctx == NULL) 227 goto err; 228 if (EVP_PKEY_keygen_init(ctx) <= 0) 229 goto err; 230 EVP_PKEY_free(pkey); 231 *pctx = ctx; 232 return 1; 233 234 err: 235 BIO_puts(bio_err, "Error initializing context\n"); 236 ERR_print_errors(bio_err); 237 EVP_PKEY_CTX_free(ctx); 238 EVP_PKEY_free(pkey); 239 return 0; 240 241 } 242 243 int init_gen_str(EVP_PKEY_CTX **pctx, 244 const char *algname, ENGINE *e, int do_param) 245 { 246 EVP_PKEY_CTX *ctx = NULL; 247 const EVP_PKEY_ASN1_METHOD *ameth; 248 ENGINE *tmpeng = NULL; 249 int pkey_id; 250 251 if (*pctx) { 252 BIO_puts(bio_err, "Algorithm already set!\n"); 253 return 0; 254 } 255 256 ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1); 257 258 #ifndef OPENSSL_NO_ENGINE 259 if (!ameth && e) 260 ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1); 261 #endif 262 263 if (!ameth) { 264 BIO_printf(bio_err, "Algorithm %s not found\n", algname); 265 return 0; 266 } 267 268 ERR_clear_error(); 269 270 EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); 271 #ifndef OPENSSL_NO_ENGINE 272 ENGINE_finish(tmpeng); 273 #endif 274 ctx = EVP_PKEY_CTX_new_id(pkey_id, e); 275 276 if (!ctx) 277 goto err; 278 if (do_param) { 279 if (EVP_PKEY_paramgen_init(ctx) <= 0) 280 goto err; 281 } else { 282 if (EVP_PKEY_keygen_init(ctx) <= 0) 283 goto err; 284 } 285 286 *pctx = ctx; 287 return 1; 288 289 err: 290 BIO_printf(bio_err, "Error initializing %s context\n", algname); 291 ERR_print_errors(bio_err); 292 EVP_PKEY_CTX_free(ctx); 293 return 0; 294 295 } 296 297 static int genpkey_cb(EVP_PKEY_CTX *ctx) 298 { 299 char c = '*'; 300 BIO *b = EVP_PKEY_CTX_get_app_data(ctx); 301 int p; 302 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); 303 if (p == 0) 304 c = '.'; 305 if (p == 1) 306 c = '+'; 307 if (p == 2) 308 c = '*'; 309 if (p == 3) 310 c = '\n'; 311 BIO_write(b, &c, 1); 312 (void)BIO_flush(b); 313 return 1; 314 } 315