1 /* 2 * Copyright 1995-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_RSA 12 NON_EMPTY_TRANSLATION_UNIT 13 #else 14 15 # include <stdio.h> 16 # include <string.h> 17 # include <sys/types.h> 18 # include <sys/stat.h> 19 # include "apps.h" 20 # include <openssl/bio.h> 21 # include <openssl/err.h> 22 # include <openssl/bn.h> 23 # include <openssl/rsa.h> 24 # include <openssl/evp.h> 25 # include <openssl/x509.h> 26 # include <openssl/pem.h> 27 # include <openssl/rand.h> 28 29 # define DEFBITS 2048 30 31 static int genrsa_cb(int p, int n, BN_GENCB *cb); 32 33 typedef enum OPTION_choice { 34 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, 35 OPT_3, OPT_F4, OPT_ENGINE, 36 OPT_OUT, OPT_RAND, OPT_PASSOUT, OPT_CIPHER 37 } OPTION_CHOICE; 38 39 OPTIONS genrsa_options[] = { 40 {"help", OPT_HELP, '-', "Display this summary"}, 41 {"3", OPT_3, '-', "Use 3 for the E value"}, 42 {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"}, 43 {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"}, 44 {"out", OPT_OUT, 's', "Output the key to specified file"}, 45 {"rand", OPT_RAND, 's', 46 "Load the file(s) into the random number generator"}, 47 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, 48 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"}, 49 # ifndef OPENSSL_NO_ENGINE 50 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, 51 # endif 52 {NULL} 53 }; 54 55 int genrsa_main(int argc, char **argv) 56 { 57 BN_GENCB *cb = BN_GENCB_new(); 58 PW_CB_DATA cb_data; 59 ENGINE *eng = NULL; 60 BIGNUM *bn = BN_new(); 61 BIO *out = NULL; 62 const BIGNUM *e; 63 RSA *rsa = NULL; 64 const EVP_CIPHER *enc = NULL; 65 int ret = 1, num = DEFBITS, private = 0; 66 unsigned long f4 = RSA_F4; 67 char *outfile = NULL, *passoutarg = NULL, *passout = NULL; 68 char *inrand = NULL, *prog, *hexe, *dece; 69 OPTION_CHOICE o; 70 71 if (bn == NULL || cb == NULL) 72 goto end; 73 74 BN_GENCB_set(cb, genrsa_cb, bio_err); 75 76 prog = opt_init(argc, argv, genrsa_options); 77 while ((o = opt_next()) != OPT_EOF) { 78 switch (o) { 79 case OPT_EOF: 80 case OPT_ERR: 81 opthelp: 82 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 83 goto end; 84 case OPT_HELP: 85 ret = 0; 86 opt_help(genrsa_options); 87 goto end; 88 case OPT_3: 89 f4 = 3; 90 break; 91 case OPT_F4: 92 f4 = RSA_F4; 93 break; 94 case OPT_OUT: 95 outfile = opt_arg(); 96 break; 97 case OPT_ENGINE: 98 eng = setup_engine(opt_arg(), 0); 99 break; 100 case OPT_RAND: 101 inrand = opt_arg(); 102 break; 103 case OPT_PASSOUT: 104 passoutarg = opt_arg(); 105 break; 106 case OPT_CIPHER: 107 if (!opt_cipher(opt_unknown(), &enc)) 108 goto end; 109 break; 110 } 111 } 112 argc = opt_num_rest(); 113 argv = opt_rest(); 114 115 if (argc == 1) { 116 if (!opt_int(argv[0], &num) || num <= 0) 117 goto end; 118 } else if (argc > 0) { 119 BIO_printf(bio_err, "Extra arguments given.\n"); 120 goto opthelp; 121 } 122 123 private = 1; 124 if (!app_passwd(NULL, passoutarg, NULL, &passout)) { 125 BIO_printf(bio_err, "Error getting password\n"); 126 goto end; 127 } 128 129 out = bio_open_owner(outfile, FORMAT_PEM, private); 130 if (out == NULL) 131 goto end; 132 133 if (!app_RAND_load_file(NULL, 1) && inrand == NULL 134 && !RAND_status()) { 135 BIO_printf(bio_err, 136 "warning, not much extra random data, consider using the -rand option\n"); 137 } 138 if (inrand != NULL) 139 BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 140 app_RAND_load_files(inrand)); 141 142 BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus\n", 143 num); 144 rsa = eng ? RSA_new_method(eng) : RSA_new(); 145 if (rsa == NULL) 146 goto end; 147 148 if (!BN_set_word(bn, f4) || !RSA_generate_key_ex(rsa, num, bn, cb)) 149 goto end; 150 151 app_RAND_write_file(NULL); 152 153 RSA_get0_key(rsa, NULL, &e, NULL); 154 hexe = BN_bn2hex(e); 155 dece = BN_bn2dec(e); 156 if (hexe && dece) { 157 BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe); 158 } 159 OPENSSL_free(hexe); 160 OPENSSL_free(dece); 161 cb_data.password = passout; 162 cb_data.prompt_info = outfile; 163 assert(private); 164 if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0, 165 (pem_password_cb *)password_callback, 166 &cb_data)) 167 goto end; 168 169 ret = 0; 170 end: 171 BN_free(bn); 172 BN_GENCB_free(cb); 173 RSA_free(rsa); 174 BIO_free_all(out); 175 release_engine(eng); 176 OPENSSL_free(passout); 177 if (ret != 0) 178 ERR_print_errors(bio_err); 179 return (ret); 180 } 181 182 static int genrsa_cb(int p, int n, BN_GENCB *cb) 183 { 184 char c = '*'; 185 186 if (p == 0) 187 c = '.'; 188 if (p == 1) 189 c = '+'; 190 if (p == 2) 191 c = '*'; 192 if (p == 3) 193 c = '\n'; 194 BIO_write(BN_GENCB_get_arg(cb), &c, 1); 195 (void)BIO_flush(BN_GENCB_get_arg(cb)); 196 return 1; 197 } 198 #endif 199