155714Skris/* apps/genrsa.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 59160814Ssimon#include <openssl/opensslconf.h> 60280304Sjkim/* 61280304Sjkim * Until the key-gen callbacks are modified to use newer prototypes, we allow 62280304Sjkim * deprecated functions for openssl-internal code 63280304Sjkim */ 64160814Ssimon#ifdef OPENSSL_NO_DEPRECATED 65280304Sjkim# undef OPENSSL_NO_DEPRECATED 66160814Ssimon#endif 67160814Ssimon 68109998Smarkm#ifndef OPENSSL_NO_RSA 69280304Sjkim# include <stdio.h> 70280304Sjkim# include <string.h> 71280304Sjkim# include <sys/types.h> 72280304Sjkim# include <sys/stat.h> 73280304Sjkim# include "apps.h" 74280304Sjkim# include <openssl/bio.h> 75280304Sjkim# include <openssl/err.h> 76280304Sjkim# include <openssl/bn.h> 77280304Sjkim# include <openssl/rsa.h> 78280304Sjkim# include <openssl/evp.h> 79280304Sjkim# include <openssl/x509.h> 80280304Sjkim# include <openssl/pem.h> 81280304Sjkim# include <openssl/rand.h> 8255714Skris 83280304Sjkim# define DEFBITS 1024 84280304Sjkim# undef PROG 85280304Sjkim# define PROG genrsa_main 8655714Skris 87160814Ssimonstatic int MS_CALLBACK genrsa_cb(int p, int n, BN_GENCB *cb); 8859191Skris 8959191Skrisint MAIN(int, char **); 9059191Skris 9155714Skrisint MAIN(int argc, char **argv) 92280304Sjkim{ 93280304Sjkim BN_GENCB cb; 94280304Sjkim# ifndef OPENSSL_NO_ENGINE 95280304Sjkim ENGINE *e = NULL; 96280304Sjkim# endif 97280304Sjkim int ret = 1; 98280304Sjkim int i, num = DEFBITS; 99280304Sjkim long l; 100280304Sjkim const EVP_CIPHER *enc = NULL; 101280304Sjkim unsigned long f4 = RSA_F4; 102280304Sjkim char *outfile = NULL; 103280304Sjkim char *passargout = NULL, *passout = NULL; 104280304Sjkim# ifndef OPENSSL_NO_ENGINE 105280304Sjkim char *engine = NULL; 106280304Sjkim# endif 107280304Sjkim char *inrand = NULL; 108280304Sjkim BIO *out = NULL; 109280304Sjkim BIGNUM *bn = BN_new(); 110280304Sjkim RSA *rsa = NULL; 11155714Skris 112280304Sjkim if (!bn) 113280304Sjkim goto err; 114160814Ssimon 115280304Sjkim apps_startup(); 116280304Sjkim BN_GENCB_set(&cb, genrsa_cb, bio_err); 11755714Skris 118280304Sjkim if (bio_err == NULL) 119280304Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 120280304Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 121109998Smarkm 122280304Sjkim if (!load_config(bio_err, NULL)) 123280304Sjkim goto err; 124280304Sjkim if ((out = BIO_new(BIO_s_file())) == NULL) { 125280304Sjkim BIO_printf(bio_err, "unable to create BIO for output\n"); 126280304Sjkim goto err; 127280304Sjkim } 12855714Skris 129280304Sjkim argv++; 130280304Sjkim argc--; 131280304Sjkim for (;;) { 132280304Sjkim if (argc <= 0) 133280304Sjkim break; 134280304Sjkim if (strcmp(*argv, "-out") == 0) { 135280304Sjkim if (--argc < 1) 136280304Sjkim goto bad; 137280304Sjkim outfile = *(++argv); 138280304Sjkim } else if (strcmp(*argv, "-3") == 0) 139280304Sjkim f4 = 3; 140280304Sjkim else if (strcmp(*argv, "-F4") == 0 || strcmp(*argv, "-f4") == 0) 141280304Sjkim f4 = RSA_F4; 142280304Sjkim# ifndef OPENSSL_NO_ENGINE 143280304Sjkim else if (strcmp(*argv, "-engine") == 0) { 144280304Sjkim if (--argc < 1) 145280304Sjkim goto bad; 146280304Sjkim engine = *(++argv); 147280304Sjkim } 148280304Sjkim# endif 149280304Sjkim else if (strcmp(*argv, "-rand") == 0) { 150280304Sjkim if (--argc < 1) 151280304Sjkim goto bad; 152280304Sjkim inrand = *(++argv); 153280304Sjkim } 154280304Sjkim# ifndef OPENSSL_NO_DES 155280304Sjkim else if (strcmp(*argv, "-des") == 0) 156280304Sjkim enc = EVP_des_cbc(); 157280304Sjkim else if (strcmp(*argv, "-des3") == 0) 158280304Sjkim enc = EVP_des_ede3_cbc(); 159280304Sjkim# endif 160280304Sjkim# ifndef OPENSSL_NO_IDEA 161280304Sjkim else if (strcmp(*argv, "-idea") == 0) 162280304Sjkim enc = EVP_idea_cbc(); 163280304Sjkim# endif 164280304Sjkim# ifndef OPENSSL_NO_SEED 165280304Sjkim else if (strcmp(*argv, "-seed") == 0) 166280304Sjkim enc = EVP_seed_cbc(); 167280304Sjkim# endif 168280304Sjkim# ifndef OPENSSL_NO_AES 169280304Sjkim else if (strcmp(*argv, "-aes128") == 0) 170280304Sjkim enc = EVP_aes_128_cbc(); 171280304Sjkim else if (strcmp(*argv, "-aes192") == 0) 172280304Sjkim enc = EVP_aes_192_cbc(); 173280304Sjkim else if (strcmp(*argv, "-aes256") == 0) 174280304Sjkim enc = EVP_aes_256_cbc(); 175280304Sjkim# endif 176280304Sjkim# ifndef OPENSSL_NO_CAMELLIA 177280304Sjkim else if (strcmp(*argv, "-camellia128") == 0) 178280304Sjkim enc = EVP_camellia_128_cbc(); 179280304Sjkim else if (strcmp(*argv, "-camellia192") == 0) 180280304Sjkim enc = EVP_camellia_192_cbc(); 181280304Sjkim else if (strcmp(*argv, "-camellia256") == 0) 182280304Sjkim enc = EVP_camellia_256_cbc(); 183280304Sjkim# endif 184280304Sjkim else if (strcmp(*argv, "-passout") == 0) { 185280304Sjkim if (--argc < 1) 186280304Sjkim goto bad; 187280304Sjkim passargout = *(++argv); 188280304Sjkim } else 189280304Sjkim break; 190280304Sjkim argv++; 191280304Sjkim argc--; 192280304Sjkim } 193280304Sjkim if ((argc >= 1) && ((sscanf(*argv, "%d", &num) == 0) || (num < 0))) { 194280304Sjkim bad: 195280304Sjkim BIO_printf(bio_err, "usage: genrsa [args] [numbits]\n"); 196280304Sjkim BIO_printf(bio_err, 197280304Sjkim " -des encrypt the generated key with DES in cbc mode\n"); 198280304Sjkim BIO_printf(bio_err, 199280304Sjkim " -des3 encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); 200280304Sjkim# ifndef OPENSSL_NO_IDEA 201280304Sjkim BIO_printf(bio_err, 202280304Sjkim " -idea encrypt the generated key with IDEA in cbc mode\n"); 203280304Sjkim# endif 204280304Sjkim# ifndef OPENSSL_NO_SEED 205280304Sjkim BIO_printf(bio_err, " -seed\n"); 206280304Sjkim BIO_printf(bio_err, 207280304Sjkim " encrypt PEM output with cbc seed\n"); 208280304Sjkim# endif 209280304Sjkim# ifndef OPENSSL_NO_AES 210280304Sjkim BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); 211280304Sjkim BIO_printf(bio_err, 212280304Sjkim " encrypt PEM output with cbc aes\n"); 213280304Sjkim# endif 214280304Sjkim# ifndef OPENSSL_NO_CAMELLIA 215280304Sjkim BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n"); 216280304Sjkim BIO_printf(bio_err, 217280304Sjkim " encrypt PEM output with cbc camellia\n"); 218280304Sjkim# endif 219280304Sjkim BIO_printf(bio_err, " -out file output the key to 'file\n"); 220280304Sjkim BIO_printf(bio_err, 221280304Sjkim " -passout arg output file pass phrase source\n"); 222280304Sjkim BIO_printf(bio_err, 223280304Sjkim " -f4 use F4 (0x10001) for the E value\n"); 224280304Sjkim BIO_printf(bio_err, " -3 use 3 for the E value\n"); 225280304Sjkim# ifndef OPENSSL_NO_ENGINE 226280304Sjkim BIO_printf(bio_err, 227280304Sjkim " -engine e use engine e, possibly a hardware device.\n"); 228280304Sjkim# endif 229280304Sjkim BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 230280304Sjkim LIST_SEPARATOR_CHAR); 231280304Sjkim BIO_printf(bio_err, 232280304Sjkim " load the file (or the files in the directory) into\n"); 233280304Sjkim BIO_printf(bio_err, " the random number generator\n"); 234280304Sjkim goto err; 235280304Sjkim } 23659191Skris 237280304Sjkim ERR_load_crypto_strings(); 23859191Skris 239280304Sjkim if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { 240280304Sjkim BIO_printf(bio_err, "Error getting password\n"); 241280304Sjkim goto err; 242280304Sjkim } 243280304Sjkim# ifndef OPENSSL_NO_ENGINE 244280304Sjkim e = setup_engine(bio_err, engine, 0); 245280304Sjkim# endif 246109998Smarkm 247280304Sjkim if (outfile == NULL) { 248280304Sjkim BIO_set_fp(out, stdout, BIO_NOCLOSE); 249280304Sjkim# ifdef OPENSSL_SYS_VMS 250280304Sjkim { 251280304Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 252280304Sjkim out = BIO_push(tmpbio, out); 253280304Sjkim } 254280304Sjkim# endif 255280304Sjkim } else { 256280304Sjkim if (BIO_write_filename(out, outfile) <= 0) { 257280304Sjkim perror(outfile); 258280304Sjkim goto err; 259280304Sjkim } 260280304Sjkim } 26155714Skris 262280304Sjkim if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL 263280304Sjkim && !RAND_status()) { 264280304Sjkim BIO_printf(bio_err, 265280304Sjkim "warning, not much extra random data, consider using the -rand option\n"); 266280304Sjkim } 267280304Sjkim if (inrand != NULL) 268280304Sjkim BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 269280304Sjkim app_RAND_load_files(inrand)); 27055714Skris 271280304Sjkim BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus\n", 272280304Sjkim num); 273280304Sjkim# ifdef OPENSSL_NO_ENGINE 274280304Sjkim rsa = RSA_new(); 275280304Sjkim# else 276280304Sjkim rsa = RSA_new_method(e); 277280304Sjkim# endif 278280304Sjkim if (!rsa) 279280304Sjkim goto err; 280205128Ssimon 281280304Sjkim if (!BN_set_word(bn, f4) || !RSA_generate_key_ex(rsa, num, bn, &cb)) 282280304Sjkim goto err; 28355714Skris 284280304Sjkim app_RAND_write_file(NULL, bio_err); 28555714Skris 286280304Sjkim /* 287280304Sjkim * We need to do the following for when the base number size is < long, 288280304Sjkim * esp windows 3.1 :-(. 289280304Sjkim */ 290280304Sjkim l = 0L; 291280304Sjkim for (i = 0; i < rsa->e->top; i++) { 292280304Sjkim# ifndef SIXTY_FOUR_BIT 293280304Sjkim l <<= BN_BITS4; 294280304Sjkim l <<= BN_BITS4; 295280304Sjkim# endif 296280304Sjkim l += rsa->e->d[i]; 297280304Sjkim } 298280304Sjkim BIO_printf(bio_err, "e is %ld (0x%lX)\n", l, l); 299280304Sjkim { 300280304Sjkim PW_CB_DATA cb_data; 301280304Sjkim cb_data.password = passout; 302280304Sjkim cb_data.prompt_info = outfile; 303280304Sjkim if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0, 304280304Sjkim (pem_password_cb *)password_callback, 305280304Sjkim &cb_data)) 306280304Sjkim goto err; 307280304Sjkim } 30855714Skris 309280304Sjkim ret = 0; 310280304Sjkim err: 311280304Sjkim if (bn) 312280304Sjkim BN_free(bn); 313280304Sjkim if (rsa) 314280304Sjkim RSA_free(rsa); 315280304Sjkim if (out) 316280304Sjkim BIO_free_all(out); 317280304Sjkim if (passout) 318280304Sjkim OPENSSL_free(passout); 319280304Sjkim if (ret != 0) 320280304Sjkim ERR_print_errors(bio_err); 321280304Sjkim apps_shutdown(); 322280304Sjkim OPENSSL_EXIT(ret); 323280304Sjkim} 324280304Sjkim 325160814Ssimonstatic int MS_CALLBACK genrsa_cb(int p, int n, BN_GENCB *cb) 326280304Sjkim{ 327280304Sjkim char c = '*'; 32855714Skris 329280304Sjkim if (p == 0) 330280304Sjkim c = '.'; 331280304Sjkim if (p == 1) 332280304Sjkim c = '+'; 333280304Sjkim if (p == 2) 334280304Sjkim c = '*'; 335280304Sjkim if (p == 3) 336280304Sjkim c = '\n'; 337280304Sjkim BIO_write(cb->arg, &c, 1); 338280304Sjkim (void)BIO_flush(cb->arg); 339280304Sjkim# ifdef LINT 340280304Sjkim p = n; 341280304Sjkim# endif 342280304Sjkim return 1; 343280304Sjkim} 344280304Sjkim#else /* !OPENSSL_NO_RSA */ 34555714Skris 34659191Skris# if PEDANTIC 347280304Sjkimstatic void *dummy = &dummy; 34859191Skris# endif 34955714Skris 35055714Skris#endif 351