Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1 | /* apps/gendsa.c */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. |
| 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: |
| 26 | * 1. Redistributions of source code must retain the copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * 2. Redistributions in binary form must reproduce the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer in the |
| 30 | * documentation and/or other materials provided with the distribution. |
| 31 | * 3. All advertising materials mentioning features or use of this software |
| 32 | * must display the following acknowledgement: |
| 33 | * "This product includes cryptographic software written by |
| 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. |
| 52 | * |
| 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] |
| 57 | */ |
| 58 | |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 59 | #ifndef OPENSSL_NO_DSA |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 60 | #include <stdio.h> |
| 61 | #include <string.h> |
| 62 | #include <sys/types.h> |
| 63 | #include <sys/stat.h> |
| 64 | #include "apps.h" |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 65 | #include <openssl/bio.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 66 | #include <openssl/err.h> |
| 67 | #include <openssl/bn.h> |
| 68 | #include <openssl/dsa.h> |
| 69 | #include <openssl/x509.h> |
| 70 | #include <openssl/pem.h> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 71 | |
| 72 | #define DEFBITS 512 |
| 73 | #undef PROG |
| 74 | #define PROG gendsa_main |
| 75 | |
Ralf S. Engelschall | 667ac4e | 2000-02-11 09:47:18 +0000 | [diff] [blame] | 76 | int MAIN(int, char **); |
| 77 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 78 | int MAIN(int argc, char **argv) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 79 | { |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 80 | ENGINE *e = NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 81 | DSA *dsa=NULL; |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 82 | int ret=1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 83 | char *outfile=NULL; |
Richard Levitte | f365611 | 2000-06-28 16:47:45 +0000 | [diff] [blame] | 84 | char *inrand=NULL,*dsaparams=NULL; |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 85 | char *passargout = NULL, *passout = NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 86 | BIO *out=NULL,*in=NULL; |
Dr. Stephen Henson | 1358835 | 2001-03-09 02:51:02 +0000 | [diff] [blame] | 87 | const EVP_CIPHER *enc=NULL; |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 88 | char *engine=NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 89 | |
| 90 | apps_startup(); |
| 91 | |
| 92 | if (bio_err == NULL) |
| 93 | if ((bio_err=BIO_new(BIO_s_file())) != NULL) |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 94 | BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 95 | |
| 96 | argv++; |
| 97 | argc--; |
| 98 | for (;;) |
| 99 | { |
| 100 | if (argc <= 0) break; |
| 101 | if (strcmp(*argv,"-out") == 0) |
| 102 | { |
| 103 | if (--argc < 1) goto bad; |
| 104 | outfile= *(++argv); |
| 105 | } |
Dr. Stephen Henson | f07fb9b | 2000-02-08 01:34:59 +0000 | [diff] [blame] | 106 | else if (strcmp(*argv,"-passout") == 0) |
| 107 | { |
| 108 | if (--argc < 1) goto bad; |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 109 | passargout= *(++argv); |
Dr. Stephen Henson | f07fb9b | 2000-02-08 01:34:59 +0000 | [diff] [blame] | 110 | } |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 111 | else if (strcmp(*argv,"-engine") == 0) |
| 112 | { |
| 113 | if (--argc < 1) goto bad; |
| 114 | engine= *(++argv); |
| 115 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 116 | else if (strcmp(*argv,"-rand") == 0) |
| 117 | { |
| 118 | if (--argc < 1) goto bad; |
| 119 | inrand= *(++argv); |
| 120 | } |
| 121 | else if (strcmp(*argv,"-") == 0) |
| 122 | goto bad; |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 123 | #ifndef OPENSSL_NO_DES |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 124 | else if (strcmp(*argv,"-des") == 0) |
| 125 | enc=EVP_des_cbc(); |
| 126 | else if (strcmp(*argv,"-des3") == 0) |
| 127 | enc=EVP_des_ede3_cbc(); |
| 128 | #endif |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 129 | #ifndef OPENSSL_NO_IDEA |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 130 | else if (strcmp(*argv,"-idea") == 0) |
| 131 | enc=EVP_idea_cbc(); |
| 132 | #endif |
Richard Levitte | b3dfaaa | 2002-02-20 18:03:07 +0000 | [diff] [blame^] | 133 | #ifndef OPENSSL_NO_AES |
| 134 | else if (strcmp(*argv,"-aes128") == 0) |
| 135 | enc=EVP_aes_128_cbc(); |
| 136 | else if (strcmp(*argv,"-aes192") == 0) |
| 137 | enc=EVP_aes_192_cbc(); |
| 138 | else if (strcmp(*argv,"-aes256") == 0) |
| 139 | enc=EVP_aes_256_cbc(); |
| 140 | #endif |
Bodo Möller | e42979f | 1999-08-06 11:18:44 +0000 | [diff] [blame] | 141 | else if (**argv != '-' && dsaparams == NULL) |
Bodo Möller | c69e361 | 1999-07-14 18:37:51 +0000 | [diff] [blame] | 142 | { |
| 143 | dsaparams = *argv; |
| 144 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 145 | else |
| 146 | goto bad; |
| 147 | argv++; |
| 148 | argc--; |
| 149 | } |
| 150 | |
| 151 | if (dsaparams == NULL) |
| 152 | { |
| 153 | bad: |
Ralf S. Engelschall | f2f351c | 1999-02-23 08:52:20 +0000 | [diff] [blame] | 154 | BIO_printf(bio_err,"usage: gendsa [args] dsaparam-file\n"); |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 155 | BIO_printf(bio_err," -out file - output the key to 'file'\n"); |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 156 | #ifndef OPENSSL_NO_DES |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 157 | BIO_printf(bio_err," -des - encrypt the generated key with DES in cbc mode\n"); |
| 158 | BIO_printf(bio_err," -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n"); |
| 159 | #endif |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 160 | #ifndef OPENSSL_NO_IDEA |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 161 | BIO_printf(bio_err," -idea - encrypt the generated key with IDEA in cbc mode\n"); |
| 162 | #endif |
Richard Levitte | b3dfaaa | 2002-02-20 18:03:07 +0000 | [diff] [blame^] | 163 | #ifndef OPENSSL_NO_AES |
| 164 | BIO_printf(bio_err," -aes128, -aes192, -aes256\n"); |
| 165 | BIO_printf(bio_err," encrypt PEM output with cbc aes\n"); |
| 166 | #endif |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 167 | BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n"); |
Bodo Möller | afbd074 | 2000-03-01 11:45:53 +0000 | [diff] [blame] | 168 | BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 169 | BIO_printf(bio_err," - load the file (or the files in the directory) into\n"); |
| 170 | BIO_printf(bio_err," the random number generator\n"); |
Ralf S. Engelschall | f2f351c | 1999-02-23 08:52:20 +0000 | [diff] [blame] | 171 | BIO_printf(bio_err," dsaparam-file\n"); |
| 172 | BIO_printf(bio_err," - a DSA parameter file as generated by the dsaparam command\n"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 173 | goto end; |
| 174 | } |
| 175 | |
Dr. Stephen Henson | a3376fe | 2001-06-19 00:23:47 +0000 | [diff] [blame] | 176 | e = setup_engine(bio_err, engine, 0); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 177 | |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 178 | if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { |
| 179 | BIO_printf(bio_err, "Error getting password\n"); |
| 180 | goto end; |
| 181 | } |
| 182 | |
| 183 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 184 | in=BIO_new(BIO_s_file()); |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 185 | if (!(BIO_read_filename(in,dsaparams))) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 186 | { |
| 187 | perror(dsaparams); |
| 188 | goto end; |
| 189 | } |
| 190 | |
Bodo Möller | 74678cc | 1999-07-21 20:57:16 +0000 | [diff] [blame] | 191 | if ((dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 192 | { |
| 193 | BIO_printf(bio_err,"unable to load DSA parameter file\n"); |
| 194 | goto end; |
| 195 | } |
| 196 | BIO_free(in); |
Bodo Möller | 8652d1c | 2000-02-06 02:48:53 +0000 | [diff] [blame] | 197 | in = NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 198 | |
| 199 | out=BIO_new(BIO_s_file()); |
| 200 | if (out == NULL) goto end; |
| 201 | |
| 202 | if (outfile == NULL) |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 203 | { |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 204 | BIO_set_fp(out,stdout,BIO_NOCLOSE); |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 205 | #ifdef OPENSSL_SYS_VMS |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 206 | { |
| 207 | BIO *tmpbio = BIO_new(BIO_f_linebuffer()); |
| 208 | out = BIO_push(tmpbio, out); |
| 209 | } |
| 210 | #endif |
| 211 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 212 | else |
| 213 | { |
| 214 | if (BIO_write_filename(out,outfile) <= 0) |
| 215 | { |
| 216 | perror(outfile); |
| 217 | goto end; |
| 218 | } |
| 219 | } |
| 220 | |
Richard Levitte | f365611 | 2000-06-28 16:47:45 +0000 | [diff] [blame] | 221 | if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 222 | { |
Bodo Möller | a31011e | 1999-10-26 01:56:29 +0000 | [diff] [blame] | 223 | BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 224 | } |
Bodo Möller | a31011e | 1999-10-26 01:56:29 +0000 | [diff] [blame] | 225 | if (inrand != NULL) |
| 226 | BIO_printf(bio_err,"%ld semi-random bytes loaded\n", |
| 227 | app_RAND_load_files(inrand)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 228 | |
Dr. Stephen Henson | 7f9b7b0 | 1999-01-09 17:29:34 +0000 | [diff] [blame] | 229 | BIO_printf(bio_err,"Generating DSA key, %d bits\n", |
| 230 | BN_num_bits(dsa->p)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 231 | if (!DSA_generate_key(dsa)) goto end; |
| 232 | |
Bodo Möller | a31011e | 1999-10-26 01:56:29 +0000 | [diff] [blame] | 233 | app_RAND_write_file(NULL, bio_err); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 234 | |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 235 | if (!PEM_write_bio_DSAPrivateKey(out,dsa,enc,NULL,0,NULL, passout)) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 236 | goto end; |
| 237 | ret=0; |
| 238 | end: |
| 239 | if (ret != 0) |
| 240 | ERR_print_errors(bio_err); |
Bodo Möller | 8652d1c | 2000-02-06 02:48:53 +0000 | [diff] [blame] | 241 | if (in != NULL) BIO_free(in); |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 242 | if (out != NULL) BIO_free_all(out); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 243 | if (dsa != NULL) DSA_free(dsa); |
Richard Levitte | 26a3a48 | 2000-06-01 22:19:21 +0000 | [diff] [blame] | 244 | if(passout) OPENSSL_free(passout); |
Richard Levitte | c04f8cf | 2001-06-23 16:37:32 +0000 | [diff] [blame] | 245 | apps_shutdown(); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 246 | EXIT(ret); |
| 247 | } |
Ulf Möller | f5d7a03 | 1999-04-27 01:14:46 +0000 | [diff] [blame] | 248 | #endif |