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. 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 59296465Sdelphij#include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ 60109998Smarkm#ifndef OPENSSL_NO_DSA 61296465Sdelphij# include <stdio.h> 62296465Sdelphij# include <string.h> 63296465Sdelphij# include <sys/types.h> 64296465Sdelphij# include <sys/stat.h> 65296465Sdelphij# include "apps.h" 66296465Sdelphij# include <openssl/bio.h> 67296465Sdelphij# include <openssl/err.h> 68296465Sdelphij# include <openssl/bn.h> 69296465Sdelphij# include <openssl/dsa.h> 70296465Sdelphij# include <openssl/x509.h> 71296465Sdelphij# include <openssl/pem.h> 7255714Skris 73296465Sdelphij# define DEFBITS 512 74296465Sdelphij# undef PROG 75296465Sdelphij# define PROG gendsa_main 7655714Skris 7759191Skrisint MAIN(int, char **); 7859191Skris 7955714Skrisint MAIN(int argc, char **argv) 80296465Sdelphij{ 81296465Sdelphij DSA *dsa = NULL; 82296465Sdelphij int ret = 1; 83296465Sdelphij char *outfile = NULL; 84296465Sdelphij char *inrand = NULL, *dsaparams = NULL; 85296465Sdelphij char *passargout = NULL, *passout = NULL; 86296465Sdelphij BIO *out = NULL, *in = NULL; 87296465Sdelphij const EVP_CIPHER *enc = NULL; 88296465Sdelphij# ifndef OPENSSL_NO_ENGINE 89296465Sdelphij char *engine = NULL; 90296465Sdelphij# endif 9155714Skris 92296465Sdelphij apps_startup(); 9355714Skris 94296465Sdelphij if (bio_err == NULL) 95296465Sdelphij if ((bio_err = BIO_new(BIO_s_file())) != NULL) 96296465Sdelphij BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 9755714Skris 98296465Sdelphij if (!load_config(bio_err, NULL)) 99296465Sdelphij goto end; 100109998Smarkm 101296465Sdelphij argv++; 102296465Sdelphij argc--; 103296465Sdelphij for (;;) { 104296465Sdelphij if (argc <= 0) 105296465Sdelphij break; 106296465Sdelphij if (strcmp(*argv, "-out") == 0) { 107296465Sdelphij if (--argc < 1) 108296465Sdelphij goto bad; 109296465Sdelphij outfile = *(++argv); 110296465Sdelphij } else if (strcmp(*argv, "-passout") == 0) { 111296465Sdelphij if (--argc < 1) 112296465Sdelphij goto bad; 113296465Sdelphij passargout = *(++argv); 114296465Sdelphij } 115296465Sdelphij# ifndef OPENSSL_NO_ENGINE 116296465Sdelphij else if (strcmp(*argv, "-engine") == 0) { 117296465Sdelphij if (--argc < 1) 118296465Sdelphij goto bad; 119296465Sdelphij engine = *(++argv); 120296465Sdelphij } 121296465Sdelphij# endif 122296465Sdelphij else if (strcmp(*argv, "-rand") == 0) { 123296465Sdelphij if (--argc < 1) 124296465Sdelphij goto bad; 125296465Sdelphij inrand = *(++argv); 126296465Sdelphij } else if (strcmp(*argv, "-") == 0) 127296465Sdelphij goto bad; 128296465Sdelphij# ifndef OPENSSL_NO_DES 129296465Sdelphij else if (strcmp(*argv, "-des") == 0) 130296465Sdelphij enc = EVP_des_cbc(); 131296465Sdelphij else if (strcmp(*argv, "-des3") == 0) 132296465Sdelphij enc = EVP_des_ede3_cbc(); 133296465Sdelphij# endif 134296465Sdelphij# ifndef OPENSSL_NO_IDEA 135296465Sdelphij else if (strcmp(*argv, "-idea") == 0) 136296465Sdelphij enc = EVP_idea_cbc(); 137296465Sdelphij# endif 138296465Sdelphij# ifndef OPENSSL_NO_SEED 139296465Sdelphij else if (strcmp(*argv, "-seed") == 0) 140296465Sdelphij enc = EVP_seed_cbc(); 141296465Sdelphij# endif 142296465Sdelphij# ifndef OPENSSL_NO_AES 143296465Sdelphij else if (strcmp(*argv, "-aes128") == 0) 144296465Sdelphij enc = EVP_aes_128_cbc(); 145296465Sdelphij else if (strcmp(*argv, "-aes192") == 0) 146296465Sdelphij enc = EVP_aes_192_cbc(); 147296465Sdelphij else if (strcmp(*argv, "-aes256") == 0) 148296465Sdelphij enc = EVP_aes_256_cbc(); 149296465Sdelphij# endif 150296465Sdelphij# ifndef OPENSSL_NO_CAMELLIA 151296465Sdelphij else if (strcmp(*argv, "-camellia128") == 0) 152296465Sdelphij enc = EVP_camellia_128_cbc(); 153296465Sdelphij else if (strcmp(*argv, "-camellia192") == 0) 154296465Sdelphij enc = EVP_camellia_192_cbc(); 155296465Sdelphij else if (strcmp(*argv, "-camellia256") == 0) 156296465Sdelphij enc = EVP_camellia_256_cbc(); 157296465Sdelphij# endif 158296465Sdelphij else if (**argv != '-' && dsaparams == NULL) { 159296465Sdelphij dsaparams = *argv; 160296465Sdelphij } else 161296465Sdelphij goto bad; 162296465Sdelphij argv++; 163296465Sdelphij argc--; 164296465Sdelphij } 16555714Skris 166296465Sdelphij if (dsaparams == NULL) { 167296465Sdelphij bad: 168296465Sdelphij BIO_printf(bio_err, "usage: gendsa [args] dsaparam-file\n"); 169296465Sdelphij BIO_printf(bio_err, " -out file - output the key to 'file'\n"); 170296465Sdelphij# ifndef OPENSSL_NO_DES 171296465Sdelphij BIO_printf(bio_err, 172296465Sdelphij " -des - encrypt the generated key with DES in cbc mode\n"); 173296465Sdelphij BIO_printf(bio_err, 174296465Sdelphij " -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); 175296465Sdelphij# endif 176296465Sdelphij# ifndef OPENSSL_NO_IDEA 177296465Sdelphij BIO_printf(bio_err, 178296465Sdelphij " -idea - encrypt the generated key with IDEA in cbc mode\n"); 179296465Sdelphij# endif 180296465Sdelphij# ifndef OPENSSL_NO_SEED 181296465Sdelphij BIO_printf(bio_err, " -seed\n"); 182296465Sdelphij BIO_printf(bio_err, 183296465Sdelphij " encrypt PEM output with cbc seed\n"); 184296465Sdelphij# endif 185296465Sdelphij# ifndef OPENSSL_NO_AES 186296465Sdelphij BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); 187296465Sdelphij BIO_printf(bio_err, 188296465Sdelphij " encrypt PEM output with cbc aes\n"); 189296465Sdelphij# endif 190296465Sdelphij# ifndef OPENSSL_NO_CAMELLIA 191296465Sdelphij BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n"); 192296465Sdelphij BIO_printf(bio_err, 193296465Sdelphij " encrypt PEM output with cbc camellia\n"); 194296465Sdelphij# endif 195296465Sdelphij# ifndef OPENSSL_NO_ENGINE 196296465Sdelphij BIO_printf(bio_err, 197296465Sdelphij " -engine e - use engine e, possibly a hardware device.\n"); 198296465Sdelphij# endif 199296465Sdelphij BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, 200296465Sdelphij LIST_SEPARATOR_CHAR); 201296465Sdelphij BIO_printf(bio_err, 202296465Sdelphij " - load the file (or the files in the directory) into\n"); 203296465Sdelphij BIO_printf(bio_err, " the random number generator\n"); 204296465Sdelphij BIO_printf(bio_err, " dsaparam-file\n"); 205296465Sdelphij BIO_printf(bio_err, 206296465Sdelphij " - a DSA parameter file as generated by the dsaparam command\n"); 207296465Sdelphij goto end; 208296465Sdelphij } 209296465Sdelphij# ifndef OPENSSL_NO_ENGINE 210296465Sdelphij setup_engine(bio_err, engine, 0); 211296465Sdelphij# endif 21255714Skris 213296465Sdelphij if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { 214296465Sdelphij BIO_printf(bio_err, "Error getting password\n"); 215296465Sdelphij goto end; 216296465Sdelphij } 217109998Smarkm 218296465Sdelphij in = BIO_new(BIO_s_file()); 219296465Sdelphij if (!(BIO_read_filename(in, dsaparams))) { 220296465Sdelphij perror(dsaparams); 221296465Sdelphij goto end; 222296465Sdelphij } 22359191Skris 224296465Sdelphij if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { 225296465Sdelphij BIO_printf(bio_err, "unable to load DSA parameter file\n"); 226296465Sdelphij goto end; 227296465Sdelphij } 228296465Sdelphij BIO_free(in); 229296465Sdelphij in = NULL; 23059191Skris 231296465Sdelphij out = BIO_new(BIO_s_file()); 232296465Sdelphij if (out == NULL) 233296465Sdelphij goto end; 23455714Skris 235296465Sdelphij if (outfile == NULL) { 236296465Sdelphij BIO_set_fp(out, stdout, BIO_NOCLOSE); 237296465Sdelphij# ifdef OPENSSL_SYS_VMS 238296465Sdelphij { 239296465Sdelphij BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 240296465Sdelphij out = BIO_push(tmpbio, out); 241296465Sdelphij } 242296465Sdelphij# endif 243296465Sdelphij } else { 244296465Sdelphij if (BIO_write_filename(out, outfile) <= 0) { 245296465Sdelphij perror(outfile); 246296465Sdelphij goto end; 247296465Sdelphij } 248296465Sdelphij } 24955714Skris 250296465Sdelphij if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) { 251296465Sdelphij BIO_printf(bio_err, 252296465Sdelphij "warning, not much extra random data, consider using the -rand option\n"); 253296465Sdelphij } 254296465Sdelphij if (inrand != NULL) 255296465Sdelphij BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 256296465Sdelphij app_RAND_load_files(inrand)); 25755714Skris 258296465Sdelphij BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(dsa->p)); 259296465Sdelphij if (!DSA_generate_key(dsa)) 260296465Sdelphij goto end; 26155714Skris 262296465Sdelphij app_RAND_write_file(NULL, bio_err); 26355714Skris 264296465Sdelphij if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) 265296465Sdelphij goto end; 266296465Sdelphij ret = 0; 267296465Sdelphij end: 268296465Sdelphij if (ret != 0) 269296465Sdelphij ERR_print_errors(bio_err); 270296465Sdelphij if (in != NULL) 271296465Sdelphij BIO_free(in); 272296465Sdelphij if (out != NULL) 273296465Sdelphij BIO_free_all(out); 274296465Sdelphij if (dsa != NULL) 275296465Sdelphij DSA_free(dsa); 276296465Sdelphij if (passout) 277296465Sdelphij OPENSSL_free(passout); 278296465Sdelphij apps_shutdown(); 279296465Sdelphij OPENSSL_EXIT(ret); 280296465Sdelphij} 281296465Sdelphij#else /* !OPENSSL_NO_DSA */ 28255714Skris 283205128Ssimon# if PEDANTIC 284296465Sdelphijstatic void *dummy = &dummy; 285205128Ssimon# endif 286205128Ssimon 28755714Skris#endif 288