gendsa.c revision 280297
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# ifndef OPENSSL_NO_ENGINE 89280297Sjkim char *engine = NULL; 90280297Sjkim# endif 9155714Skris 92280297Sjkim apps_startup(); 9355714Skris 94280297Sjkim if (bio_err == NULL) 95280297Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 96280297Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 9755714Skris 98280297Sjkim if (!load_config(bio_err, NULL)) 99280297Sjkim goto end; 100109998Smarkm 101280297Sjkim argv++; 102280297Sjkim argc--; 103280297Sjkim for (;;) { 104280297Sjkim if (argc <= 0) 105280297Sjkim break; 106280297Sjkim if (strcmp(*argv, "-out") == 0) { 107280297Sjkim if (--argc < 1) 108280297Sjkim goto bad; 109280297Sjkim outfile = *(++argv); 110280297Sjkim } else if (strcmp(*argv, "-passout") == 0) { 111280297Sjkim if (--argc < 1) 112280297Sjkim goto bad; 113280297Sjkim passargout = *(++argv); 114280297Sjkim } 115280297Sjkim# ifndef OPENSSL_NO_ENGINE 116280297Sjkim else if (strcmp(*argv, "-engine") == 0) { 117280297Sjkim if (--argc < 1) 118280297Sjkim goto bad; 119280297Sjkim engine = *(++argv); 120280297Sjkim } 121280297Sjkim# endif 122280297Sjkim else if (strcmp(*argv, "-rand") == 0) { 123280297Sjkim if (--argc < 1) 124280297Sjkim goto bad; 125280297Sjkim inrand = *(++argv); 126280297Sjkim } else if (strcmp(*argv, "-") == 0) 127280297Sjkim goto bad; 128280297Sjkim# ifndef OPENSSL_NO_DES 129280297Sjkim else if (strcmp(*argv, "-des") == 0) 130280297Sjkim enc = EVP_des_cbc(); 131280297Sjkim else if (strcmp(*argv, "-des3") == 0) 132280297Sjkim enc = EVP_des_ede3_cbc(); 133280297Sjkim# endif 134280297Sjkim# ifndef OPENSSL_NO_IDEA 135280297Sjkim else if (strcmp(*argv, "-idea") == 0) 136280297Sjkim enc = EVP_idea_cbc(); 137280297Sjkim# endif 138280297Sjkim# ifndef OPENSSL_NO_SEED 139280297Sjkim else if (strcmp(*argv, "-seed") == 0) 140280297Sjkim enc = EVP_seed_cbc(); 141280297Sjkim# endif 142280297Sjkim# ifndef OPENSSL_NO_AES 143280297Sjkim else if (strcmp(*argv, "-aes128") == 0) 144280297Sjkim enc = EVP_aes_128_cbc(); 145280297Sjkim else if (strcmp(*argv, "-aes192") == 0) 146280297Sjkim enc = EVP_aes_192_cbc(); 147280297Sjkim else if (strcmp(*argv, "-aes256") == 0) 148280297Sjkim enc = EVP_aes_256_cbc(); 149280297Sjkim# endif 150280297Sjkim# ifndef OPENSSL_NO_CAMELLIA 151280297Sjkim else if (strcmp(*argv, "-camellia128") == 0) 152280297Sjkim enc = EVP_camellia_128_cbc(); 153280297Sjkim else if (strcmp(*argv, "-camellia192") == 0) 154280297Sjkim enc = EVP_camellia_192_cbc(); 155280297Sjkim else if (strcmp(*argv, "-camellia256") == 0) 156280297Sjkim enc = EVP_camellia_256_cbc(); 157280297Sjkim# endif 158280297Sjkim else if (**argv != '-' && dsaparams == NULL) { 159280297Sjkim dsaparams = *argv; 160280297Sjkim } else 161280297Sjkim goto bad; 162280297Sjkim argv++; 163280297Sjkim argc--; 164280297Sjkim } 16555714Skris 166280297Sjkim if (dsaparams == NULL) { 167280297Sjkim bad: 168280297Sjkim BIO_printf(bio_err, "usage: gendsa [args] dsaparam-file\n"); 169280297Sjkim BIO_printf(bio_err, " -out file - output the key to 'file'\n"); 170280297Sjkim# ifndef OPENSSL_NO_DES 171280297Sjkim BIO_printf(bio_err, 172280297Sjkim " -des - encrypt the generated key with DES in cbc mode\n"); 173280297Sjkim BIO_printf(bio_err, 174280297Sjkim " -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); 175280297Sjkim# endif 176280297Sjkim# ifndef OPENSSL_NO_IDEA 177280297Sjkim BIO_printf(bio_err, 178280297Sjkim " -idea - encrypt the generated key with IDEA in cbc mode\n"); 179280297Sjkim# endif 180280297Sjkim# ifndef OPENSSL_NO_SEED 181280297Sjkim BIO_printf(bio_err, " -seed\n"); 182280297Sjkim BIO_printf(bio_err, 183280297Sjkim " encrypt PEM output with cbc seed\n"); 184280297Sjkim# endif 185280297Sjkim# ifndef OPENSSL_NO_AES 186280297Sjkim BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); 187280297Sjkim BIO_printf(bio_err, 188280297Sjkim " encrypt PEM output with cbc aes\n"); 189280297Sjkim# endif 190280297Sjkim# ifndef OPENSSL_NO_CAMELLIA 191280297Sjkim BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n"); 192280297Sjkim BIO_printf(bio_err, 193280297Sjkim " encrypt PEM output with cbc camellia\n"); 194280297Sjkim# endif 195280297Sjkim# ifndef OPENSSL_NO_ENGINE 196280297Sjkim BIO_printf(bio_err, 197280297Sjkim " -engine e - use engine e, possibly a hardware device.\n"); 198280297Sjkim# endif 199280297Sjkim BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 200280297Sjkim LIST_SEPARATOR_CHAR); 201280297Sjkim BIO_printf(bio_err, 202280297Sjkim " - load the file (or the files in the directory) into\n"); 203280297Sjkim BIO_printf(bio_err, " the random number generator\n"); 204280297Sjkim BIO_printf(bio_err, " dsaparam-file\n"); 205280297Sjkim BIO_printf(bio_err, 206280297Sjkim " - a DSA parameter file as generated by the dsaparam command\n"); 207280297Sjkim goto end; 208280297Sjkim } 209280297Sjkim# ifndef OPENSSL_NO_ENGINE 210280297Sjkim setup_engine(bio_err, engine, 0); 211280297Sjkim# endif 21255714Skris 213280297Sjkim if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { 214280297Sjkim BIO_printf(bio_err, "Error getting password\n"); 215280297Sjkim goto end; 216280297Sjkim } 217109998Smarkm 218280297Sjkim in = BIO_new(BIO_s_file()); 219280297Sjkim if (!(BIO_read_filename(in, dsaparams))) { 220280297Sjkim perror(dsaparams); 221280297Sjkim goto end; 222280297Sjkim } 22359191Skris 224280297Sjkim if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { 225280297Sjkim BIO_printf(bio_err, "unable to load DSA parameter file\n"); 226280297Sjkim goto end; 227280297Sjkim } 228280297Sjkim BIO_free(in); 229280297Sjkim in = NULL; 23059191Skris 231280297Sjkim out = BIO_new(BIO_s_file()); 232280297Sjkim if (out == NULL) 233280297Sjkim goto end; 23455714Skris 235280297Sjkim if (outfile == NULL) { 236280297Sjkim BIO_set_fp(out, stdout, BIO_NOCLOSE); 237280297Sjkim# ifdef OPENSSL_SYS_VMS 238280297Sjkim { 239280297Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 240280297Sjkim out = BIO_push(tmpbio, out); 241280297Sjkim } 242280297Sjkim# endif 243280297Sjkim } else { 244280297Sjkim if (BIO_write_filename(out, outfile) <= 0) { 245280297Sjkim perror(outfile); 246280297Sjkim goto end; 247280297Sjkim } 248280297Sjkim } 24955714Skris 250280297Sjkim if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) { 251280297Sjkim BIO_printf(bio_err, 252280297Sjkim "warning, not much extra random data, consider using the -rand option\n"); 253280297Sjkim } 254280297Sjkim if (inrand != NULL) 255280297Sjkim BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 256280297Sjkim app_RAND_load_files(inrand)); 25755714Skris 258280297Sjkim BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(dsa->p)); 259280297Sjkim if (!DSA_generate_key(dsa)) 260280297Sjkim goto end; 26155714Skris 262280297Sjkim app_RAND_write_file(NULL, bio_err); 26355714Skris 264280297Sjkim if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) 265280297Sjkim goto end; 266280297Sjkim ret = 0; 267280297Sjkim end: 268280297Sjkim if (ret != 0) 269280297Sjkim ERR_print_errors(bio_err); 270280297Sjkim if (in != NULL) 271280297Sjkim BIO_free(in); 272280297Sjkim if (out != NULL) 273280297Sjkim BIO_free_all(out); 274280297Sjkim if (dsa != NULL) 275280297Sjkim DSA_free(dsa); 276280297Sjkim if (passout) 277280297Sjkim OPENSSL_free(passout); 278280297Sjkim apps_shutdown(); 279280297Sjkim OPENSSL_EXIT(ret); 280280297Sjkim} 281280297Sjkim#else /* !OPENSSL_NO_DSA */ 28255714Skris 283205128Ssimon# if PEDANTIC 284280297Sjkimstatic void *dummy = &dummy; 285205128Ssimon# endif 286205128Ssimon 28755714Skris#endif 288