1 /* $OpenBSD: gendsa.c,v 1.11 2019/06/07 02:32:22 inoguchi Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 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 59 #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ 60 61 62 #include <sys/types.h> 63 #include <sys/stat.h> 64 65 #include <stdio.h> 66 #include <string.h> 67 68 #include "apps.h" 69 70 #include <openssl/bio.h> 71 #include <openssl/bn.h> 72 #include <openssl/dsa.h> 73 #include <openssl/err.h> 74 #include <openssl/pem.h> 75 #include <openssl/x509.h> 76 77 static int set_enc(int argc, char **argv, int *argsused); 78 static const EVP_CIPHER *get_cipher_by_name(char *name); 79 80 static struct { 81 const EVP_CIPHER *enc; 82 char *outfile; 83 char *passargout; 84 } gendsa_config; 85 86 static struct option gendsa_options[] = { 87 #ifndef OPENSSL_NO_AES 88 { 89 .name = "aes128", 90 .desc = "Encrypt PEM output with cbc aes", 91 .type = OPTION_ARGV_FUNC, 92 .opt.argvfunc = set_enc, 93 }, 94 { 95 .name = "aes192", 96 .desc = "Encrypt PEM output with cbc aes", 97 .type = OPTION_ARGV_FUNC, 98 .opt.argvfunc = set_enc, 99 }, 100 { 101 .name = "aes256", 102 .desc = "Encrypt PEM output with cbc aes", 103 .type = OPTION_ARGV_FUNC, 104 .opt.argvfunc = set_enc, 105 }, 106 #endif 107 #ifndef OPENSSL_NO_CAMELLIA 108 { 109 .name = "camellia128", 110 .desc = "Encrypt PEM output with cbc camellia", 111 .type = OPTION_ARGV_FUNC, 112 .opt.argvfunc = set_enc, 113 }, 114 { 115 .name = "camellia192", 116 .desc = "Encrypt PEM output with cbc camellia", 117 .type = OPTION_ARGV_FUNC, 118 .opt.argvfunc = set_enc, 119 }, 120 { 121 .name = "camellia256", 122 .desc = "Encrypt PEM output with cbc camellia", 123 .type = OPTION_ARGV_FUNC, 124 .opt.argvfunc = set_enc, 125 }, 126 #endif 127 #ifndef OPENSSL_NO_DES 128 { 129 .name = "des", 130 .desc = "Encrypt the generated key with DES in cbc mode", 131 .type = OPTION_ARGV_FUNC, 132 .opt.argvfunc = set_enc, 133 }, 134 { 135 .name = "des3", 136 .desc = "Encrypt the generated key with DES in ede cbc mode (168 bit key)", 137 .type = OPTION_ARGV_FUNC, 138 .opt.argvfunc = set_enc, 139 }, 140 #endif 141 #ifndef OPENSSL_NO_IDEA 142 { 143 .name = "idea", 144 .desc = "Encrypt the generated key with IDEA in cbc mode", 145 .type = OPTION_ARGV_FUNC, 146 .opt.argvfunc = set_enc, 147 }, 148 #endif 149 { 150 .name = "out", 151 .argname = "file", 152 .desc = "Output the key to 'file'", 153 .type = OPTION_ARG, 154 .opt.arg = &gendsa_config.outfile, 155 }, 156 { 157 .name = "passout", 158 .argname = "src", 159 .desc = "Output file passphrase source", 160 .type = OPTION_ARG, 161 .opt.arg = &gendsa_config.passargout, 162 }, 163 { NULL }, 164 }; 165 166 static void 167 gendsa_usage(void) 168 { 169 fprintf(stderr, "usage: gendsa [-aes128 | -aes192 | -aes256 |\n"); 170 fprintf(stderr, " -camellia128 | -camellia192 | -camellia256 |\n"); 171 fprintf(stderr, " -des | -des3 | -idea] [-out file] [-passout src]"); 172 fprintf(stderr, " paramfile\n\n"); 173 options_usage(gendsa_options); 174 fprintf(stderr, "\n"); 175 } 176 177 int 178 gendsa_main(int argc, char **argv) 179 { 180 DSA *dsa = NULL; 181 int ret = 1; 182 char *dsaparams = NULL; 183 char *passout = NULL; 184 BIO *out = NULL, *in = NULL; 185 186 if (single_execution) { 187 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) { 188 perror("pledge"); 189 exit(1); 190 } 191 } 192 193 memset(&gendsa_config, 0, sizeof(gendsa_config)); 194 195 if (options_parse(argc, argv, gendsa_options, &dsaparams, NULL) != 0) { 196 gendsa_usage(); 197 goto end; 198 } 199 200 if (dsaparams == NULL) { 201 gendsa_usage(); 202 goto end; 203 } 204 if (!app_passwd(bio_err, NULL, gendsa_config.passargout, NULL, 205 &passout)) { 206 BIO_printf(bio_err, "Error getting password\n"); 207 goto end; 208 } 209 in = BIO_new(BIO_s_file()); 210 if (!(BIO_read_filename(in, dsaparams))) { 211 perror(dsaparams); 212 goto end; 213 } 214 if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { 215 BIO_printf(bio_err, "unable to load DSA parameter file\n"); 216 goto end; 217 } 218 BIO_free(in); 219 in = NULL; 220 221 out = BIO_new(BIO_s_file()); 222 if (out == NULL) 223 goto end; 224 225 if (gendsa_config.outfile == NULL) { 226 BIO_set_fp(out, stdout, BIO_NOCLOSE); 227 } else { 228 if (BIO_write_filename(out, gendsa_config.outfile) <= 0) { 229 perror(gendsa_config.outfile); 230 goto end; 231 } 232 } 233 234 BIO_printf(bio_err, "Generating DSA key, %d bits\n", 235 BN_num_bits(dsa->p)); 236 if (!DSA_generate_key(dsa)) 237 goto end; 238 239 if (!PEM_write_bio_DSAPrivateKey(out, dsa, gendsa_config.enc, NULL, 0, 240 NULL, passout)) 241 goto end; 242 ret = 0; 243 end: 244 if (ret != 0) 245 ERR_print_errors(bio_err); 246 BIO_free(in); 247 BIO_free_all(out); 248 DSA_free(dsa); 249 free(passout); 250 251 return (ret); 252 } 253 254 static int 255 set_enc(int argc, char **argv, int *argsused) 256 { 257 char *name = argv[0]; 258 259 if (*name++ != '-') 260 return (1); 261 262 if ((gendsa_config.enc = get_cipher_by_name(name)) == NULL) 263 return (1); 264 265 *argsused = 1; 266 return (0); 267 } 268 269 static const EVP_CIPHER *get_cipher_by_name(char *name) 270 { 271 if (name == NULL || strcmp(name, "") == 0) 272 return (NULL); 273 #ifndef OPENSSL_NO_AES 274 else if (strcmp(name, "aes128") == 0) 275 return EVP_aes_128_cbc(); 276 else if (strcmp(name, "aes192") == 0) 277 return EVP_aes_192_cbc(); 278 else if (strcmp(name, "aes256") == 0) 279 return EVP_aes_256_cbc(); 280 #endif 281 #ifndef OPENSSL_NO_CAMELLIA 282 else if (strcmp(name, "camellia128") == 0) 283 return EVP_camellia_128_cbc(); 284 else if (strcmp(name, "camellia192") == 0) 285 return EVP_camellia_192_cbc(); 286 else if (strcmp(name, "camellia256") == 0) 287 return EVP_camellia_256_cbc(); 288 #endif 289 #ifndef OPENSSL_NO_DES 290 else if (strcmp(name, "des") == 0) 291 return EVP_des_cbc(); 292 else if (strcmp(name, "des3") == 0) 293 return EVP_des_ede3_cbc(); 294 #endif 295 #ifndef OPENSSL_NO_IDEA 296 else if (strcmp(name, "idea") == 0) 297 return EVP_idea_cbc(); 298 #endif 299 else 300 return (NULL); 301 } 302