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. 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 59280304Sjkim#include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ 60109998Smarkm#ifndef OPENSSL_NO_DSA 61280304Sjkim# include <stdio.h> 62280304Sjkim# include <string.h> 63280304Sjkim# include <sys/types.h> 64280304Sjkim# include <sys/stat.h> 65280304Sjkim# include "apps.h" 66280304Sjkim# include <openssl/bio.h> 67280304Sjkim# include <openssl/err.h> 68280304Sjkim# include <openssl/bn.h> 69280304Sjkim# include <openssl/dsa.h> 70280304Sjkim# include <openssl/x509.h> 71280304Sjkim# include <openssl/pem.h> 7255714Skris 73280304Sjkim# define DEFBITS 512 74280304Sjkim# undef PROG 75280304Sjkim# define PROG gendsa_main 7655714Skris 7759191Skrisint MAIN(int, char **); 7859191Skris 7955714Skrisint MAIN(int argc, char **argv) 80280304Sjkim{ 81280304Sjkim DSA *dsa = NULL; 82280304Sjkim int ret = 1; 83280304Sjkim char *outfile = NULL; 84280304Sjkim char *inrand = NULL, *dsaparams = NULL; 85280304Sjkim char *passargout = NULL, *passout = NULL; 86280304Sjkim BIO *out = NULL, *in = NULL; 87280304Sjkim const EVP_CIPHER *enc = NULL; 88280304Sjkim# ifndef OPENSSL_NO_ENGINE 89280304Sjkim char *engine = NULL; 90280304Sjkim# endif 9155714Skris 92280304Sjkim apps_startup(); 9355714Skris 94280304Sjkim if (bio_err == NULL) 95280304Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 96280304Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 9755714Skris 98280304Sjkim if (!load_config(bio_err, NULL)) 99280304Sjkim goto end; 100109998Smarkm 101280304Sjkim argv++; 102280304Sjkim argc--; 103280304Sjkim for (;;) { 104280304Sjkim if (argc <= 0) 105280304Sjkim break; 106280304Sjkim if (strcmp(*argv, "-out") == 0) { 107280304Sjkim if (--argc < 1) 108280304Sjkim goto bad; 109280304Sjkim outfile = *(++argv); 110280304Sjkim } else if (strcmp(*argv, "-passout") == 0) { 111280304Sjkim if (--argc < 1) 112280304Sjkim goto bad; 113280304Sjkim passargout = *(++argv); 114280304Sjkim } 115280304Sjkim# ifndef OPENSSL_NO_ENGINE 116280304Sjkim else if (strcmp(*argv, "-engine") == 0) { 117280304Sjkim if (--argc < 1) 118280304Sjkim goto bad; 119280304Sjkim engine = *(++argv); 120280304Sjkim } 121280304Sjkim# endif 122280304Sjkim else if (strcmp(*argv, "-rand") == 0) { 123280304Sjkim if (--argc < 1) 124280304Sjkim goto bad; 125280304Sjkim inrand = *(++argv); 126280304Sjkim } else if (strcmp(*argv, "-") == 0) 127280304Sjkim goto bad; 128280304Sjkim# ifndef OPENSSL_NO_DES 129280304Sjkim else if (strcmp(*argv, "-des") == 0) 130280304Sjkim enc = EVP_des_cbc(); 131280304Sjkim else if (strcmp(*argv, "-des3") == 0) 132280304Sjkim enc = EVP_des_ede3_cbc(); 133280304Sjkim# endif 134280304Sjkim# ifndef OPENSSL_NO_IDEA 135280304Sjkim else if (strcmp(*argv, "-idea") == 0) 136280304Sjkim enc = EVP_idea_cbc(); 137280304Sjkim# endif 138280304Sjkim# ifndef OPENSSL_NO_SEED 139280304Sjkim else if (strcmp(*argv, "-seed") == 0) 140280304Sjkim enc = EVP_seed_cbc(); 141280304Sjkim# endif 142280304Sjkim# ifndef OPENSSL_NO_AES 143280304Sjkim else if (strcmp(*argv, "-aes128") == 0) 144280304Sjkim enc = EVP_aes_128_cbc(); 145280304Sjkim else if (strcmp(*argv, "-aes192") == 0) 146280304Sjkim enc = EVP_aes_192_cbc(); 147280304Sjkim else if (strcmp(*argv, "-aes256") == 0) 148280304Sjkim enc = EVP_aes_256_cbc(); 149280304Sjkim# endif 150280304Sjkim# ifndef OPENSSL_NO_CAMELLIA 151280304Sjkim else if (strcmp(*argv, "-camellia128") == 0) 152280304Sjkim enc = EVP_camellia_128_cbc(); 153280304Sjkim else if (strcmp(*argv, "-camellia192") == 0) 154280304Sjkim enc = EVP_camellia_192_cbc(); 155280304Sjkim else if (strcmp(*argv, "-camellia256") == 0) 156280304Sjkim enc = EVP_camellia_256_cbc(); 157280304Sjkim# endif 158280304Sjkim else if (**argv != '-' && dsaparams == NULL) { 159280304Sjkim dsaparams = *argv; 160280304Sjkim } else 161280304Sjkim goto bad; 162280304Sjkim argv++; 163280304Sjkim argc--; 164280304Sjkim } 16555714Skris 166280304Sjkim if (dsaparams == NULL) { 167280304Sjkim bad: 168280304Sjkim BIO_printf(bio_err, "usage: gendsa [args] dsaparam-file\n"); 169280304Sjkim BIO_printf(bio_err, " -out file - output the key to 'file'\n"); 170280304Sjkim# ifndef OPENSSL_NO_DES 171280304Sjkim BIO_printf(bio_err, 172280304Sjkim " -des - encrypt the generated key with DES in cbc mode\n"); 173280304Sjkim BIO_printf(bio_err, 174280304Sjkim " -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); 175280304Sjkim# endif 176280304Sjkim# ifndef OPENSSL_NO_IDEA 177280304Sjkim BIO_printf(bio_err, 178280304Sjkim " -idea - encrypt the generated key with IDEA in cbc mode\n"); 179280304Sjkim# endif 180280304Sjkim# ifndef OPENSSL_NO_SEED 181280304Sjkim BIO_printf(bio_err, " -seed\n"); 182280304Sjkim BIO_printf(bio_err, 183280304Sjkim " encrypt PEM output with cbc seed\n"); 184280304Sjkim# endif 185280304Sjkim# ifndef OPENSSL_NO_AES 186280304Sjkim BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); 187280304Sjkim BIO_printf(bio_err, 188280304Sjkim " encrypt PEM output with cbc aes\n"); 189280304Sjkim# endif 190280304Sjkim# ifndef OPENSSL_NO_CAMELLIA 191280304Sjkim BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n"); 192280304Sjkim BIO_printf(bio_err, 193280304Sjkim " encrypt PEM output with cbc camellia\n"); 194280304Sjkim# endif 195280304Sjkim# ifndef OPENSSL_NO_ENGINE 196280304Sjkim BIO_printf(bio_err, 197280304Sjkim " -engine e - use engine e, possibly a hardware device.\n"); 198280304Sjkim# endif 199280304Sjkim BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 200280304Sjkim LIST_SEPARATOR_CHAR); 201280304Sjkim BIO_printf(bio_err, 202280304Sjkim " - load the file (or the files in the directory) into\n"); 203280304Sjkim BIO_printf(bio_err, " the random number generator\n"); 204280304Sjkim BIO_printf(bio_err, " dsaparam-file\n"); 205280304Sjkim BIO_printf(bio_err, 206280304Sjkim " - a DSA parameter file as generated by the dsaparam command\n"); 207280304Sjkim goto end; 208280304Sjkim } 209280304Sjkim# ifndef OPENSSL_NO_ENGINE 210280304Sjkim setup_engine(bio_err, engine, 0); 211280304Sjkim# endif 21255714Skris 213280304Sjkim if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { 214280304Sjkim BIO_printf(bio_err, "Error getting password\n"); 215280304Sjkim goto end; 216280304Sjkim } 217109998Smarkm 218280304Sjkim in = BIO_new(BIO_s_file()); 219280304Sjkim if (!(BIO_read_filename(in, dsaparams))) { 220280304Sjkim perror(dsaparams); 221280304Sjkim goto end; 222280304Sjkim } 22359191Skris 224280304Sjkim if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { 225280304Sjkim BIO_printf(bio_err, "unable to load DSA parameter file\n"); 226280304Sjkim goto end; 227280304Sjkim } 228280304Sjkim BIO_free(in); 229280304Sjkim in = NULL; 23059191Skris 231280304Sjkim out = BIO_new(BIO_s_file()); 232280304Sjkim if (out == NULL) 233280304Sjkim goto end; 23455714Skris 235280304Sjkim if (outfile == NULL) { 236280304Sjkim BIO_set_fp(out, stdout, BIO_NOCLOSE); 237280304Sjkim# ifdef OPENSSL_SYS_VMS 238280304Sjkim { 239280304Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 240280304Sjkim out = BIO_push(tmpbio, out); 241280304Sjkim } 242280304Sjkim# endif 243280304Sjkim } else { 244280304Sjkim if (BIO_write_filename(out, outfile) <= 0) { 245280304Sjkim perror(outfile); 246280304Sjkim goto end; 247280304Sjkim } 248280304Sjkim } 24955714Skris 250280304Sjkim if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) { 251280304Sjkim BIO_printf(bio_err, 252280304Sjkim "warning, not much extra random data, consider using the -rand option\n"); 253280304Sjkim } 254280304Sjkim if (inrand != NULL) 255280304Sjkim BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 256280304Sjkim app_RAND_load_files(inrand)); 25755714Skris 258280304Sjkim BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(dsa->p)); 259280304Sjkim if (!DSA_generate_key(dsa)) 260280304Sjkim goto end; 26155714Skris 262280304Sjkim app_RAND_write_file(NULL, bio_err); 26355714Skris 264280304Sjkim if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) 265280304Sjkim goto end; 266280304Sjkim ret = 0; 267280304Sjkim end: 268280304Sjkim if (ret != 0) 269280304Sjkim ERR_print_errors(bio_err); 270280304Sjkim if (in != NULL) 271280304Sjkim BIO_free(in); 272280304Sjkim if (out != NULL) 273280304Sjkim BIO_free_all(out); 274280304Sjkim if (dsa != NULL) 275280304Sjkim DSA_free(dsa); 276280304Sjkim if (passout) 277280304Sjkim OPENSSL_free(passout); 278280304Sjkim apps_shutdown(); 279280304Sjkim OPENSSL_EXIT(ret); 280280304Sjkim} 281280304Sjkim#else /* !OPENSSL_NO_DSA */ 28255714Skris 283205128Ssimon# if PEDANTIC 284280304Sjkimstatic void *dummy = &dummy; 285205128Ssimon# endif 286205128Ssimon 28755714Skris#endif 288