1 /* 2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <openssl/opensslconf.h> 11 #ifdef OPENSSL_NO_DH 12 NON_EMPTY_TRANSLATION_UNIT 13 #else 14 15 # include <stdio.h> 16 # include <stdlib.h> 17 # include <time.h> 18 # include <string.h> 19 # include "apps.h" 20 # include <openssl/bio.h> 21 # include <openssl/err.h> 22 # include <openssl/bn.h> 23 # include <openssl/dh.h> 24 # include <openssl/x509.h> 25 # include <openssl/pem.h> 26 27 # ifndef OPENSSL_NO_DSA 28 # include <openssl/dsa.h> 29 # endif 30 31 # define DEFBITS 2048 32 33 static int dh_cb(int p, int n, BN_GENCB *cb); 34 35 typedef enum OPTION_choice { 36 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, 37 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, 38 OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT, 39 OPT_RAND, OPT_DSAPARAM, OPT_C, OPT_2, OPT_5 40 } OPTION_CHOICE; 41 42 OPTIONS dhparam_options[] = { 43 {OPT_HELP_STR, 1, '-', "Usage: %s [flags] [numbits]\n"}, 44 {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, 45 {"help", OPT_HELP, '-', "Display this summary"}, 46 {"in", OPT_IN, '<', "Input file"}, 47 {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"}, 48 {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"}, 49 {"out", OPT_OUT, '>', "Output file"}, 50 {"check", OPT_CHECK, '-', "Check the DH parameters"}, 51 {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"}, 52 {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"}, 53 {"rand", OPT_RAND, 's', 54 "Load the file(s) into the random number generator"}, 55 {"C", OPT_C, '-', "Print C code"}, 56 {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"}, 57 {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"}, 58 # ifndef OPENSSL_NO_DSA 59 {"dsaparam", OPT_DSAPARAM, '-', 60 "Read or generate DSA parameters, convert to DH"}, 61 # endif 62 # ifndef OPENSSL_NO_ENGINE 63 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, 64 # endif 65 {NULL} 66 }; 67 68 int dhparam_main(int argc, char **argv) 69 { 70 BIO *in = NULL, *out = NULL; 71 DH *dh = NULL; 72 char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL; 73 ENGINE *e = NULL; 74 #ifndef OPENSSL_NO_DSA 75 int dsaparam = 0; 76 #endif 77 int i, text = 0, C = 0, ret = 1, num = 0, g = 0; 78 int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0; 79 OPTION_CHOICE o; 80 81 prog = opt_init(argc, argv, dhparam_options); 82 while ((o = opt_next()) != OPT_EOF) { 83 switch (o) { 84 case OPT_EOF: 85 case OPT_ERR: 86 opthelp: 87 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 88 goto end; 89 case OPT_HELP: 90 opt_help(dhparam_options); 91 ret = 0; 92 goto end; 93 case OPT_INFORM: 94 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) 95 goto opthelp; 96 break; 97 case OPT_OUTFORM: 98 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) 99 goto opthelp; 100 break; 101 case OPT_IN: 102 infile = opt_arg(); 103 break; 104 case OPT_OUT: 105 outfile = opt_arg(); 106 break; 107 case OPT_ENGINE: 108 e = setup_engine(opt_arg(), 0); 109 break; 110 case OPT_CHECK: 111 check = 1; 112 break; 113 case OPT_TEXT: 114 text = 1; 115 break; 116 case OPT_DSAPARAM: 117 #ifndef OPENSSL_NO_DSA 118 dsaparam = 1; 119 #endif 120 break; 121 case OPT_C: 122 C = 1; 123 break; 124 case OPT_2: 125 g = 2; 126 break; 127 case OPT_5: 128 g = 5; 129 break; 130 case OPT_NOOUT: 131 noout = 1; 132 break; 133 case OPT_RAND: 134 inrand = opt_arg(); 135 break; 136 } 137 } 138 argc = opt_num_rest(); 139 argv = opt_rest(); 140 141 if (argv[0] && (!opt_int(argv[0], &num) || num <= 0)) 142 goto end; 143 144 if (g && !num) 145 num = DEFBITS; 146 147 # ifndef OPENSSL_NO_DSA 148 if (dsaparam && g) { 149 BIO_printf(bio_err, 150 "generator may not be chosen for DSA parameters\n"); 151 goto end; 152 } 153 # endif 154 /* DH parameters */ 155 if (num && !g) 156 g = 2; 157 158 if (num) { 159 160 BN_GENCB *cb; 161 cb = BN_GENCB_new(); 162 if (cb == NULL) { 163 ERR_print_errors(bio_err); 164 goto end; 165 } 166 167 BN_GENCB_set(cb, dh_cb, bio_err); 168 if (!app_RAND_load_file(NULL, 1) && inrand == NULL) { 169 BIO_printf(bio_err, 170 "warning, not much extra random data, consider using the -rand option\n"); 171 } 172 if (inrand != NULL) 173 BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 174 app_RAND_load_files(inrand)); 175 176 # ifndef OPENSSL_NO_DSA 177 if (dsaparam) { 178 DSA *dsa = DSA_new(); 179 180 BIO_printf(bio_err, 181 "Generating DSA parameters, %d bit long prime\n", num); 182 if (dsa == NULL 183 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, 184 cb)) { 185 DSA_free(dsa); 186 BN_GENCB_free(cb); 187 ERR_print_errors(bio_err); 188 goto end; 189 } 190 191 dh = DSA_dup_DH(dsa); 192 DSA_free(dsa); 193 if (dh == NULL) { 194 BN_GENCB_free(cb); 195 ERR_print_errors(bio_err); 196 goto end; 197 } 198 } else 199 # endif 200 { 201 dh = DH_new(); 202 BIO_printf(bio_err, 203 "Generating DH parameters, %d bit long safe prime, generator %d\n", 204 num, g); 205 BIO_printf(bio_err, "This is going to take a long time\n"); 206 if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) { 207 BN_GENCB_free(cb); 208 ERR_print_errors(bio_err); 209 goto end; 210 } 211 } 212 213 BN_GENCB_free(cb); 214 app_RAND_write_file(NULL); 215 } else { 216 217 in = bio_open_default(infile, 'r', informat); 218 if (in == NULL) 219 goto end; 220 221 # ifndef OPENSSL_NO_DSA 222 if (dsaparam) { 223 DSA *dsa; 224 225 if (informat == FORMAT_ASN1) 226 dsa = d2i_DSAparams_bio(in, NULL); 227 else /* informat == FORMAT_PEM */ 228 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); 229 230 if (dsa == NULL) { 231 BIO_printf(bio_err, "unable to load DSA parameters\n"); 232 ERR_print_errors(bio_err); 233 goto end; 234 } 235 236 dh = DSA_dup_DH(dsa); 237 DSA_free(dsa); 238 if (dh == NULL) { 239 ERR_print_errors(bio_err); 240 goto end; 241 } 242 } else 243 # endif 244 { 245 if (informat == FORMAT_ASN1) { 246 /* 247 * We have no PEM header to determine what type of DH params it 248 * is. We'll just try both. 249 */ 250 dh = d2i_DHparams_bio(in, NULL); 251 /* BIO_reset() returns 0 for success for file BIOs only!!! */ 252 if (dh == NULL && BIO_reset(in) == 0) 253 dh = d2i_DHxparams_bio(in, NULL); 254 } else { 255 /* informat == FORMAT_PEM */ 256 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); 257 } 258 259 if (dh == NULL) { 260 BIO_printf(bio_err, "unable to load DH parameters\n"); 261 ERR_print_errors(bio_err); 262 goto end; 263 } 264 } 265 266 /* dh != NULL */ 267 } 268 269 out = bio_open_default(outfile, 'w', outformat); 270 if (out == NULL) 271 goto end; 272 273 if (text) { 274 DHparams_print(out, dh); 275 } 276 277 if (check) { 278 if (!DH_check(dh, &i)) { 279 ERR_print_errors(bio_err); 280 goto end; 281 } 282 if (i & DH_CHECK_P_NOT_PRIME) 283 BIO_printf(bio_err, "WARNING: p value is not prime\n"); 284 if (i & DH_CHECK_P_NOT_SAFE_PRIME) 285 BIO_printf(bio_err, "WARNING: p value is not a safe prime\n"); 286 if (i & DH_CHECK_Q_NOT_PRIME) 287 BIO_printf(bio_err, "WARNING: q value is not a prime\n"); 288 if (i & DH_CHECK_INVALID_Q_VALUE) 289 BIO_printf(bio_err, "WARNING: q value is invalid\n"); 290 if (i & DH_CHECK_INVALID_J_VALUE) 291 BIO_printf(bio_err, "WARNING: j value is invalid\n"); 292 if (i & DH_UNABLE_TO_CHECK_GENERATOR) 293 BIO_printf(bio_err, 294 "WARNING: unable to check the generator value\n"); 295 if (i & DH_NOT_SUITABLE_GENERATOR) 296 BIO_printf(bio_err, "WARNING: the g value is not a generator\n"); 297 if (i == 0) 298 BIO_printf(bio_err, "DH parameters appear to be ok.\n"); 299 if (num != 0 && i != 0) { 300 /* 301 * We have generated parameters but DH_check() indicates they are 302 * invalid! This should never happen! 303 */ 304 BIO_printf(bio_err, "ERROR: Invalid parameters generated\n"); 305 goto end; 306 } 307 } 308 if (C) { 309 unsigned char *data; 310 int len, bits; 311 const BIGNUM *pbn, *gbn; 312 313 len = DH_size(dh); 314 bits = DH_bits(dh); 315 DH_get0_pqg(dh, &pbn, NULL, &gbn); 316 data = app_malloc(len, "print a BN"); 317 BIO_printf(out, "#ifndef HEADER_DH_H\n" 318 "# include <openssl/dh.h>\n" 319 "#endif\n" 320 "\n"); 321 BIO_printf(out, "DH *get_dh%d()\n{\n", bits); 322 print_bignum_var(out, pbn, "dhp", bits, data); 323 print_bignum_var(out, gbn, "dhg", bits, data); 324 BIO_printf(out, " DH *dh = DH_new();\n" 325 " BIGNUM *dhp_bn, *dhg_bn;\n" 326 "\n" 327 " if (dh == NULL)\n" 328 " return NULL;\n"); 329 BIO_printf(out, " dhp_bn = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n", 330 bits, bits); 331 BIO_printf(out, " dhg_bn = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n", 332 bits, bits); 333 BIO_printf(out, " if (dhp_bn == NULL || dhg_bn == NULL\n" 334 " || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) {\n" 335 " DH_free(dh);\n" 336 " BN_free(dhp_bn);\n" 337 " BN_free(dhg_bn);\n" 338 " return NULL;\n" 339 " }\n"); 340 if (DH_get_length(dh) > 0) 341 BIO_printf(out, 342 " if (!DH_set_length(dh, %ld)) {\n" 343 " DH_free(dh);\n" 344 " }\n", DH_get_length(dh)); 345 BIO_printf(out, " return dh;\n}\n"); 346 OPENSSL_free(data); 347 } 348 349 if (!noout) { 350 const BIGNUM *q; 351 DH_get0_pqg(dh, NULL, &q, NULL); 352 if (outformat == FORMAT_ASN1) { 353 if (q != NULL) 354 i = i2d_DHxparams_bio(out, dh); 355 else 356 i = i2d_DHparams_bio(out, dh); 357 } else if (q != NULL) 358 i = PEM_write_bio_DHxparams(out, dh); 359 else 360 i = PEM_write_bio_DHparams(out, dh); 361 if (!i) { 362 BIO_printf(bio_err, "unable to write DH parameters\n"); 363 ERR_print_errors(bio_err); 364 goto end; 365 } 366 } 367 ret = 0; 368 end: 369 BIO_free(in); 370 BIO_free_all(out); 371 DH_free(dh); 372 release_engine(e); 373 return (ret); 374 } 375 376 static int dh_cb(int p, int n, BN_GENCB *cb) 377 { 378 char c = '*'; 379 380 if (p == 0) 381 c = '.'; 382 if (p == 1) 383 c = '+'; 384 if (p == 2) 385 c = '*'; 386 if (p == 3) 387 c = '\n'; 388 BIO_write(BN_GENCB_get_arg(cb), &c, 1); 389 (void)BIO_flush(BN_GENCB_get_arg(cb)); 390 return 1; 391 } 392 #endif 393