155714Skris/* apps/gendsa.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. 8280297Sjkim * 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). 15280297Sjkim * 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. 22280297Sjkim * 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 :-). 37280297Sjkim * 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)" 40280297Sjkim * 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. 52280297Sjkim * 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 59280297Sjkim#include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ 60109998Smarkm#ifndef OPENSSL_NO_DSA 61280297Sjkim# include <stdio.h> 62280297Sjkim# include <string.h> 63280297Sjkim# include <sys/types.h> 64280297Sjkim# include <sys/stat.h> 65280297Sjkim# include "apps.h" 66280297Sjkim# include <openssl/bio.h> 67280297Sjkim# include <openssl/err.h> 68280297Sjkim# include <openssl/bn.h> 69280297Sjkim# include <openssl/dsa.h> 70280297Sjkim# include <openssl/x509.h> 71280297Sjkim# include <openssl/pem.h> 7255714Skris 73280297Sjkim# define DEFBITS 512 74280297Sjkim# undef PROG 75280297Sjkim# define PROG gendsa_main 7655714Skris 7759191Skrisint MAIN(int, char **); 7859191Skris 7955714Skrisint MAIN(int argc, char **argv) 80280297Sjkim{ 81280297Sjkim DSA *dsa = NULL; 82280297Sjkim int ret = 1; 83280297Sjkim char *outfile = NULL; 84280297Sjkim char *inrand = NULL, *dsaparams = NULL; 85280297Sjkim char *passargout = NULL, *passout = NULL; 86280297Sjkim BIO *out = NULL, *in = NULL; 87280297Sjkim const EVP_CIPHER *enc = NULL; 88280297Sjkim char *engine = NULL; 89312826Sjkim ENGINE *e = NULL; 9055714Skris 91280297Sjkim apps_startup(); 9255714Skris 93280297Sjkim if (bio_err == NULL) 94280297Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 95280297Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 9655714Skris 97280297Sjkim if (!load_config(bio_err, NULL)) 98280297Sjkim goto end; 99109998Smarkm 100280297Sjkim argv++; 101280297Sjkim argc--; 102280297Sjkim for (;;) { 103280297Sjkim if (argc <= 0) 104280297Sjkim break; 105280297Sjkim if (strcmp(*argv, "-out") == 0) { 106280297Sjkim if (--argc < 1) 107280297Sjkim goto bad; 108280297Sjkim outfile = *(++argv); 109280297Sjkim } else if (strcmp(*argv, "-passout") == 0) { 110280297Sjkim if (--argc < 1) 111280297Sjkim goto bad; 112280297Sjkim passargout = *(++argv); 113280297Sjkim } 114280297Sjkim# ifndef OPENSSL_NO_ENGINE 115280297Sjkim else if (strcmp(*argv, "-engine") == 0) { 116280297Sjkim if (--argc < 1) 117280297Sjkim goto bad; 118280297Sjkim engine = *(++argv); 119280297Sjkim } 120280297Sjkim# endif 121280297Sjkim else if (strcmp(*argv, "-rand") == 0) { 122280297Sjkim if (--argc < 1) 123280297Sjkim goto bad; 124280297Sjkim inrand = *(++argv); 125280297Sjkim } else if (strcmp(*argv, "-") == 0) 126280297Sjkim goto bad; 127280297Sjkim# ifndef OPENSSL_NO_DES 128280297Sjkim else if (strcmp(*argv, "-des") == 0) 129280297Sjkim enc = EVP_des_cbc(); 130280297Sjkim else if (strcmp(*argv, "-des3") == 0) 131280297Sjkim enc = EVP_des_ede3_cbc(); 132280297Sjkim# endif 133280297Sjkim# ifndef OPENSSL_NO_IDEA 134280297Sjkim else if (strcmp(*argv, "-idea") == 0) 135280297Sjkim enc = EVP_idea_cbc(); 136280297Sjkim# endif 137280297Sjkim# ifndef OPENSSL_NO_SEED 138280297Sjkim else if (strcmp(*argv, "-seed") == 0) 139280297Sjkim enc = EVP_seed_cbc(); 140280297Sjkim# endif 141280297Sjkim# ifndef OPENSSL_NO_AES 142280297Sjkim else if (strcmp(*argv, "-aes128") == 0) 143280297Sjkim enc = EVP_aes_128_cbc(); 144280297Sjkim else if (strcmp(*argv, "-aes192") == 0) 145280297Sjkim enc = EVP_aes_192_cbc(); 146280297Sjkim else if (strcmp(*argv, "-aes256") == 0) 147280297Sjkim enc = EVP_aes_256_cbc(); 148280297Sjkim# endif 149280297Sjkim# ifndef OPENSSL_NO_CAMELLIA 150280297Sjkim else if (strcmp(*argv, "-camellia128") == 0) 151280297Sjkim enc = EVP_camellia_128_cbc(); 152280297Sjkim else if (strcmp(*argv, "-camellia192") == 0) 153280297Sjkim enc = EVP_camellia_192_cbc(); 154280297Sjkim else if (strcmp(*argv, "-camellia256") == 0) 155280297Sjkim enc = EVP_camellia_256_cbc(); 156280297Sjkim# endif 157280297Sjkim else if (**argv != '-' && dsaparams == NULL) { 158280297Sjkim dsaparams = *argv; 159280297Sjkim } else 160280297Sjkim goto bad; 161280297Sjkim argv++; 162280297Sjkim argc--; 163280297Sjkim } 16455714Skris 165280297Sjkim if (dsaparams == NULL) { 166280297Sjkim bad: 167280297Sjkim BIO_printf(bio_err, "usage: gendsa [args] dsaparam-file\n"); 168280297Sjkim BIO_printf(bio_err, " -out file - output the key to 'file'\n"); 169280297Sjkim# ifndef OPENSSL_NO_DES 170280297Sjkim BIO_printf(bio_err, 171280297Sjkim " -des - encrypt the generated key with DES in cbc mode\n"); 172280297Sjkim BIO_printf(bio_err, 173280297Sjkim " -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); 174280297Sjkim# endif 175280297Sjkim# ifndef OPENSSL_NO_IDEA 176280297Sjkim BIO_printf(bio_err, 177280297Sjkim " -idea - encrypt the generated key with IDEA in cbc mode\n"); 178280297Sjkim# endif 179280297Sjkim# ifndef OPENSSL_NO_SEED 180280297Sjkim BIO_printf(bio_err, " -seed\n"); 181280297Sjkim BIO_printf(bio_err, 182280297Sjkim " encrypt PEM output with cbc seed\n"); 183280297Sjkim# endif 184280297Sjkim# ifndef OPENSSL_NO_AES 185280297Sjkim BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); 186280297Sjkim BIO_printf(bio_err, 187280297Sjkim " encrypt PEM output with cbc aes\n"); 188280297Sjkim# endif 189280297Sjkim# ifndef OPENSSL_NO_CAMELLIA 190280297Sjkim BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n"); 191280297Sjkim BIO_printf(bio_err, 192280297Sjkim " encrypt PEM output with cbc camellia\n"); 193280297Sjkim# endif 194280297Sjkim# ifndef OPENSSL_NO_ENGINE 195280297Sjkim BIO_printf(bio_err, 196280297Sjkim " -engine e - use engine e, possibly a hardware device.\n"); 197280297Sjkim# endif 198280297Sjkim BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 199280297Sjkim LIST_SEPARATOR_CHAR); 200280297Sjkim BIO_printf(bio_err, 201280297Sjkim " - load the file (or the files in the directory) into\n"); 202280297Sjkim BIO_printf(bio_err, " the random number generator\n"); 203280297Sjkim BIO_printf(bio_err, " dsaparam-file\n"); 204280297Sjkim BIO_printf(bio_err, 205280297Sjkim " - a DSA parameter file as generated by the dsaparam command\n"); 206280297Sjkim goto end; 207280297Sjkim } 208312826Sjkim e = setup_engine(bio_err, engine, 0); 20955714Skris 210280297Sjkim if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { 211280297Sjkim BIO_printf(bio_err, "Error getting password\n"); 212280297Sjkim goto end; 213280297Sjkim } 214109998Smarkm 215280297Sjkim in = BIO_new(BIO_s_file()); 216280297Sjkim if (!(BIO_read_filename(in, dsaparams))) { 217280297Sjkim perror(dsaparams); 218280297Sjkim goto end; 219280297Sjkim } 22059191Skris 221280297Sjkim if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { 222280297Sjkim BIO_printf(bio_err, "unable to load DSA parameter file\n"); 223280297Sjkim goto end; 224280297Sjkim } 225280297Sjkim BIO_free(in); 226280297Sjkim in = NULL; 22759191Skris 228280297Sjkim out = BIO_new(BIO_s_file()); 229280297Sjkim if (out == NULL) 230280297Sjkim goto end; 23155714Skris 232280297Sjkim if (outfile == NULL) { 233280297Sjkim BIO_set_fp(out, stdout, BIO_NOCLOSE); 234280297Sjkim# ifdef OPENSSL_SYS_VMS 235280297Sjkim { 236280297Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 237280297Sjkim out = BIO_push(tmpbio, out); 238280297Sjkim } 239280297Sjkim# endif 240280297Sjkim } else { 241280297Sjkim if (BIO_write_filename(out, outfile) <= 0) { 242280297Sjkim perror(outfile); 243280297Sjkim goto end; 244280297Sjkim } 245280297Sjkim } 24655714Skris 247280297Sjkim if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) { 248280297Sjkim BIO_printf(bio_err, 249280297Sjkim "warning, not much extra random data, consider using the -rand option\n"); 250280297Sjkim } 251280297Sjkim if (inrand != NULL) 252280297Sjkim BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 253280297Sjkim app_RAND_load_files(inrand)); 25455714Skris 255280297Sjkim BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(dsa->p)); 256280297Sjkim if (!DSA_generate_key(dsa)) 257280297Sjkim goto end; 25855714Skris 259280297Sjkim app_RAND_write_file(NULL, bio_err); 26055714Skris 261280297Sjkim if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) 262280297Sjkim goto end; 263280297Sjkim ret = 0; 264280297Sjkim end: 265280297Sjkim if (ret != 0) 266280297Sjkim ERR_print_errors(bio_err); 267280297Sjkim if (in != NULL) 268280297Sjkim BIO_free(in); 269280297Sjkim if (out != NULL) 270280297Sjkim BIO_free_all(out); 271280297Sjkim if (dsa != NULL) 272280297Sjkim DSA_free(dsa); 273312826Sjkim release_engine(e); 274280297Sjkim if (passout) 275280297Sjkim OPENSSL_free(passout); 276280297Sjkim apps_shutdown(); 277280297Sjkim OPENSSL_EXIT(ret); 278280297Sjkim} 279280297Sjkim#else /* !OPENSSL_NO_DSA */ 28055714Skris 281205128Ssimon# if PEDANTIC 282280297Sjkimstatic void *dummy = &dummy; 283205128Ssimon# endif 284205128Ssimon 28555714Skris#endif 286