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. 8296465Sdelphij * 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). 15296465Sdelphij * 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. 22296465Sdelphij * 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 :-). 37296465Sdelphij * 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)" 40296465Sdelphij * 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. 52296465Sdelphij * 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> 60296465Sdelphij/* 61296465Sdelphij * Until the key-gen callbacks are modified to use newer prototypes, we allow 62296465Sdelphij * deprecated functions for openssl-internal code 63296465Sdelphij */ 64160814Ssimon#ifdef OPENSSL_NO_DEPRECATED 65296465Sdelphij# undef OPENSSL_NO_DEPRECATED 66160814Ssimon#endif 67160814Ssimon 68109998Smarkm#ifndef OPENSSL_NO_RSA 69296465Sdelphij# include <stdio.h> 70296465Sdelphij# include <string.h> 71296465Sdelphij# include <sys/types.h> 72296465Sdelphij# include <sys/stat.h> 73296465Sdelphij# include "apps.h" 74296465Sdelphij# include <openssl/bio.h> 75296465Sdelphij# include <openssl/err.h> 76296465Sdelphij# include <openssl/bn.h> 77296465Sdelphij# include <openssl/rsa.h> 78296465Sdelphij# include <openssl/evp.h> 79296465Sdelphij# include <openssl/x509.h> 80296465Sdelphij# include <openssl/pem.h> 81296465Sdelphij# include <openssl/rand.h> 8255714Skris 83296465Sdelphij# define DEFBITS 512 84296465Sdelphij# undef PROG 85296465Sdelphij# 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) 92296465Sdelphij{ 93296465Sdelphij BN_GENCB cb; 94296465Sdelphij int ret = 1; 95296465Sdelphij int i, num = DEFBITS; 96296465Sdelphij long l; 97296465Sdelphij int use_x931 = 0; 98296465Sdelphij const EVP_CIPHER *enc = NULL; 99296465Sdelphij unsigned long f4 = RSA_F4; 100296465Sdelphij char *outfile = NULL; 101296465Sdelphij char *passargout = NULL, *passout = NULL; 102296465Sdelphij# ifndef OPENSSL_NO_ENGINE 103296465Sdelphij char *engine = NULL; 104296465Sdelphij# endif 105296465Sdelphij char *inrand = NULL; 106296465Sdelphij BIO *out = NULL; 107296465Sdelphij BIGNUM *bn = BN_new(); 108296465Sdelphij RSA *rsa = NULL; 10955714Skris 110296465Sdelphij if (!bn) 111296465Sdelphij goto err; 112160814Ssimon 113296465Sdelphij apps_startup(); 114296465Sdelphij BN_GENCB_set(&cb, genrsa_cb, bio_err); 11555714Skris 116296465Sdelphij if (bio_err == NULL) 117296465Sdelphij if ((bio_err = BIO_new(BIO_s_file())) != NULL) 118296465Sdelphij BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 119109998Smarkm 120296465Sdelphij if (!load_config(bio_err, NULL)) 121296465Sdelphij goto err; 122296465Sdelphij if ((out = BIO_new(BIO_s_file())) == NULL) { 123296465Sdelphij BIO_printf(bio_err, "unable to create BIO for output\n"); 124296465Sdelphij goto err; 125296465Sdelphij } 12655714Skris 127296465Sdelphij argv++; 128296465Sdelphij argc--; 129296465Sdelphij for (;;) { 130296465Sdelphij if (argc <= 0) 131296465Sdelphij break; 132296465Sdelphij if (strcmp(*argv, "-out") == 0) { 133296465Sdelphij if (--argc < 1) 134296465Sdelphij goto bad; 135296465Sdelphij outfile = *(++argv); 136296465Sdelphij } else if (strcmp(*argv, "-3") == 0) 137296465Sdelphij f4 = 3; 138296465Sdelphij else if (strcmp(*argv, "-F4") == 0 || strcmp(*argv, "-f4") == 0) 139296465Sdelphij f4 = RSA_F4; 140296465Sdelphij else if (strcmp(*argv, "-x931") == 0) 141296465Sdelphij use_x931 = 1; 142296465Sdelphij# ifndef OPENSSL_NO_ENGINE 143296465Sdelphij else if (strcmp(*argv, "-engine") == 0) { 144296465Sdelphij if (--argc < 1) 145296465Sdelphij goto bad; 146296465Sdelphij engine = *(++argv); 147296465Sdelphij } 148296465Sdelphij# endif 149296465Sdelphij else if (strcmp(*argv, "-rand") == 0) { 150296465Sdelphij if (--argc < 1) 151296465Sdelphij goto bad; 152296465Sdelphij inrand = *(++argv); 153296465Sdelphij } 154296465Sdelphij# ifndef OPENSSL_NO_DES 155296465Sdelphij else if (strcmp(*argv, "-des") == 0) 156296465Sdelphij enc = EVP_des_cbc(); 157296465Sdelphij else if (strcmp(*argv, "-des3") == 0) 158296465Sdelphij enc = EVP_des_ede3_cbc(); 159296465Sdelphij# endif 160296465Sdelphij# ifndef OPENSSL_NO_IDEA 161296465Sdelphij else if (strcmp(*argv, "-idea") == 0) 162296465Sdelphij enc = EVP_idea_cbc(); 163296465Sdelphij# endif 164296465Sdelphij# ifndef OPENSSL_NO_SEED 165296465Sdelphij else if (strcmp(*argv, "-seed") == 0) 166296465Sdelphij enc = EVP_seed_cbc(); 167296465Sdelphij# endif 168296465Sdelphij# ifndef OPENSSL_NO_AES 169296465Sdelphij else if (strcmp(*argv, "-aes128") == 0) 170296465Sdelphij enc = EVP_aes_128_cbc(); 171296465Sdelphij else if (strcmp(*argv, "-aes192") == 0) 172296465Sdelphij enc = EVP_aes_192_cbc(); 173296465Sdelphij else if (strcmp(*argv, "-aes256") == 0) 174296465Sdelphij enc = EVP_aes_256_cbc(); 175296465Sdelphij# endif 176296465Sdelphij# ifndef OPENSSL_NO_CAMELLIA 177296465Sdelphij else if (strcmp(*argv, "-camellia128") == 0) 178296465Sdelphij enc = EVP_camellia_128_cbc(); 179296465Sdelphij else if (strcmp(*argv, "-camellia192") == 0) 180296465Sdelphij enc = EVP_camellia_192_cbc(); 181296465Sdelphij else if (strcmp(*argv, "-camellia256") == 0) 182296465Sdelphij enc = EVP_camellia_256_cbc(); 183296465Sdelphij# endif 184296465Sdelphij else if (strcmp(*argv, "-passout") == 0) { 185296465Sdelphij if (--argc < 1) 186296465Sdelphij goto bad; 187296465Sdelphij passargout = *(++argv); 188296465Sdelphij } else 189296465Sdelphij break; 190296465Sdelphij argv++; 191296465Sdelphij argc--; 192296465Sdelphij } 193296465Sdelphij if ((argc >= 1) && ((sscanf(*argv, "%d", &num) == 0) || (num < 0))) { 194296465Sdelphij bad: 195296465Sdelphij BIO_printf(bio_err, "usage: genrsa [args] [numbits]\n"); 196296465Sdelphij BIO_printf(bio_err, 197296465Sdelphij " -des encrypt the generated key with DES in cbc mode\n"); 198296465Sdelphij BIO_printf(bio_err, 199296465Sdelphij " -des3 encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); 200296465Sdelphij# ifndef OPENSSL_NO_IDEA 201296465Sdelphij BIO_printf(bio_err, 202296465Sdelphij " -idea encrypt the generated key with IDEA in cbc mode\n"); 203296465Sdelphij# endif 204296465Sdelphij# ifndef OPENSSL_NO_SEED 205296465Sdelphij BIO_printf(bio_err, " -seed\n"); 206296465Sdelphij BIO_printf(bio_err, 207296465Sdelphij " encrypt PEM output with cbc seed\n"); 208296465Sdelphij# endif 209296465Sdelphij# ifndef OPENSSL_NO_AES 210296465Sdelphij BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); 211296465Sdelphij BIO_printf(bio_err, 212296465Sdelphij " encrypt PEM output with cbc aes\n"); 213296465Sdelphij# endif 214296465Sdelphij# ifndef OPENSSL_NO_CAMELLIA 215296465Sdelphij BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n"); 216296465Sdelphij BIO_printf(bio_err, 217296465Sdelphij " encrypt PEM output with cbc camellia\n"); 218296465Sdelphij# endif 219296465Sdelphij BIO_printf(bio_err, " -out file output the key to 'file\n"); 220296465Sdelphij BIO_printf(bio_err, 221296465Sdelphij " -passout arg output file pass phrase source\n"); 222296465Sdelphij BIO_printf(bio_err, 223296465Sdelphij " -f4 use F4 (0x10001) for the E value\n"); 224296465Sdelphij BIO_printf(bio_err, " -3 use 3 for the E value\n"); 225296465Sdelphij# ifndef OPENSSL_NO_ENGINE 226296465Sdelphij BIO_printf(bio_err, 227296465Sdelphij " -engine e use engine e, possibly a hardware device.\n"); 228296465Sdelphij# endif 229296465Sdelphij BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 230296465Sdelphij LIST_SEPARATOR_CHAR); 231296465Sdelphij BIO_printf(bio_err, 232296465Sdelphij " load the file (or the files in the directory) into\n"); 233296465Sdelphij BIO_printf(bio_err, " the random number generator\n"); 234296465Sdelphij goto err; 235296465Sdelphij } 23659191Skris 237296465Sdelphij ERR_load_crypto_strings(); 23859191Skris 239296465Sdelphij if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { 240296465Sdelphij BIO_printf(bio_err, "Error getting password\n"); 241296465Sdelphij goto err; 242296465Sdelphij } 243296465Sdelphij# ifndef OPENSSL_NO_ENGINE 244296465Sdelphij setup_engine(bio_err, engine, 0); 245296465Sdelphij# endif 246109998Smarkm 247296465Sdelphij if (outfile == NULL) { 248296465Sdelphij BIO_set_fp(out, stdout, BIO_NOCLOSE); 249296465Sdelphij# ifdef OPENSSL_SYS_VMS 250296465Sdelphij { 251296465Sdelphij BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 252296465Sdelphij out = BIO_push(tmpbio, out); 253296465Sdelphij } 254296465Sdelphij# endif 255296465Sdelphij } else { 256296465Sdelphij if (BIO_write_filename(out, outfile) <= 0) { 257296465Sdelphij perror(outfile); 258296465Sdelphij goto err; 259296465Sdelphij } 260296465Sdelphij } 26155714Skris 262296465Sdelphij if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL 263296465Sdelphij && !RAND_status()) { 264296465Sdelphij BIO_printf(bio_err, 265296465Sdelphij "warning, not much extra random data, consider using the -rand option\n"); 266296465Sdelphij } 267296465Sdelphij if (inrand != NULL) 268296465Sdelphij BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 269296465Sdelphij app_RAND_load_files(inrand)); 27055714Skris 271296465Sdelphij BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus\n", 272296465Sdelphij num); 273160814Ssimon 274296465Sdelphij rsa = RSA_new(); 275296465Sdelphij if (!rsa) 276296465Sdelphij goto err; 277205128Ssimon 278296465Sdelphij if (use_x931) { 279296465Sdelphij BIGNUM *pubexp; 280296465Sdelphij pubexp = BN_new(); 281296465Sdelphij if (!BN_set_word(pubexp, f4)) 282296465Sdelphij goto err; 283296465Sdelphij if (!RSA_X931_generate_key_ex(rsa, num, pubexp, &cb)) 284296465Sdelphij goto err; 285296465Sdelphij BN_free(pubexp); 286296465Sdelphij } else if (!BN_set_word(bn, f4) 287296465Sdelphij || !RSA_generate_key_ex(rsa, num, bn, &cb)) 288296465Sdelphij goto err; 28955714Skris 290296465Sdelphij app_RAND_write_file(NULL, bio_err); 29155714Skris 292296465Sdelphij /* 293296465Sdelphij * We need to do the following for when the base number size is < long, 294296465Sdelphij * esp windows 3.1 :-(. 295296465Sdelphij */ 296296465Sdelphij l = 0L; 297296465Sdelphij for (i = 0; i < rsa->e->top; i++) { 298296465Sdelphij# ifndef SIXTY_FOUR_BIT 299296465Sdelphij l <<= BN_BITS4; 300296465Sdelphij l <<= BN_BITS4; 301296465Sdelphij# endif 302296465Sdelphij l += rsa->e->d[i]; 303296465Sdelphij } 304296465Sdelphij BIO_printf(bio_err, "e is %ld (0x%lX)\n", l, l); 305296465Sdelphij { 306296465Sdelphij PW_CB_DATA cb_data; 307296465Sdelphij cb_data.password = passout; 308296465Sdelphij cb_data.prompt_info = outfile; 309296465Sdelphij if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0, 310296465Sdelphij (pem_password_cb *)password_callback, 311296465Sdelphij &cb_data)) 312296465Sdelphij goto err; 313296465Sdelphij } 31455714Skris 315296465Sdelphij ret = 0; 316296465Sdelphij err: 317296465Sdelphij if (bn) 318296465Sdelphij BN_free(bn); 319296465Sdelphij if (rsa) 320296465Sdelphij RSA_free(rsa); 321296465Sdelphij if (out) 322296465Sdelphij BIO_free_all(out); 323296465Sdelphij if (passout) 324296465Sdelphij OPENSSL_free(passout); 325296465Sdelphij if (ret != 0) 326296465Sdelphij ERR_print_errors(bio_err); 327296465Sdelphij apps_shutdown(); 328296465Sdelphij OPENSSL_EXIT(ret); 329296465Sdelphij} 330296465Sdelphij 331160814Ssimonstatic int MS_CALLBACK genrsa_cb(int p, int n, BN_GENCB *cb) 332296465Sdelphij{ 333296465Sdelphij char c = '*'; 33455714Skris 335296465Sdelphij if (p == 0) 336296465Sdelphij c = '.'; 337296465Sdelphij if (p == 1) 338296465Sdelphij c = '+'; 339296465Sdelphij if (p == 2) 340296465Sdelphij c = '*'; 341296465Sdelphij if (p == 3) 342296465Sdelphij c = '\n'; 343296465Sdelphij BIO_write(cb->arg, &c, 1); 344296465Sdelphij (void)BIO_flush(cb->arg); 345296465Sdelphij# ifdef LINT 346296465Sdelphij p = n; 347296465Sdelphij# endif 348296465Sdelphij return 1; 349296465Sdelphij} 350296465Sdelphij#else /* !OPENSSL_NO_RSA */ 35155714Skris 35259191Skris# if PEDANTIC 353296465Sdelphijstatic void *dummy = &dummy; 35459191Skris# endif 35555714Skris 35655714Skris#endif 357