freebsd-nq/crypto/openssl/apps/gendsa.c

147 lines
4.0 KiB
C
Raw Normal View History

2018-09-13 19:18:07 +00:00
/*
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
*
2018-09-13 19:18:07 +00:00
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
2018-09-13 19:18:07 +00:00
#include <openssl/opensslconf.h>
#ifdef OPENSSL_NO_DSA
NON_EMPTY_TRANSLATION_UNIT
#else
2015-03-20 15:28:40 +00:00
# include <stdio.h>
# include <string.h>
# include <sys/types.h>
# include <sys/stat.h>
# include "apps.h"
2018-09-13 19:18:07 +00:00
# include "progs.h"
2015-03-20 15:28:40 +00:00
# include <openssl/bio.h>
# include <openssl/err.h>
# include <openssl/bn.h>
# include <openssl/dsa.h>
# include <openssl/x509.h>
# include <openssl/pem.h>
2018-09-13 19:18:07 +00:00
typedef enum OPTION_choice {
OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER,
OPT_R_ENUM
} OPTION_CHOICE;
const OPTIONS gendsa_options[] = {
{OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
{OPT_HELP_STR, 1, '-', "Valid options are:\n"},
{"help", OPT_HELP, '-', "Display this summary"},
{"out", OPT_OUT, '>', "Output the key to the specified file"},
{"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
OPT_R_OPTIONS,
{"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
# ifndef OPENSSL_NO_ENGINE
{"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
# endif
{NULL}
};
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
int gendsa_main(int argc, char **argv)
2015-03-20 15:28:40 +00:00
{
2018-09-13 19:18:07 +00:00
ENGINE *e = NULL;
2015-03-20 15:28:40 +00:00
BIO *out = NULL, *in = NULL;
2018-09-13 19:18:07 +00:00
DSA *dsa = NULL;
2015-03-20 15:28:40 +00:00
const EVP_CIPHER *enc = NULL;
2018-09-13 19:18:07 +00:00
char *dsaparams = NULL;
char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
OPTION_CHOICE o;
int ret = 1, private = 0;
const BIGNUM *p = NULL;
prog = opt_init(argc, argv, gendsa_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
goto end;
case OPT_HELP:
ret = 0;
opt_help(gendsa_options);
goto end;
case OPT_OUT:
outfile = opt_arg();
break;
case OPT_PASSOUT:
passoutarg = opt_arg();
break;
case OPT_ENGINE:
e = setup_engine(opt_arg(), 0);
break;
case OPT_R_CASES:
if (!opt_rand(o))
goto end;
break;
case OPT_CIPHER:
if (!opt_cipher(opt_unknown(), &enc))
goto end;
2015-03-20 15:28:40 +00:00
break;
}
}
2018-09-13 19:18:07 +00:00
argc = opt_num_rest();
argv = opt_rest();
private = 1;
2018-09-13 19:18:07 +00:00
if (argc != 1)
goto opthelp;
dsaparams = *argv;
2018-09-13 19:18:07 +00:00
if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
2015-03-20 15:28:40 +00:00
BIO_printf(bio_err, "Error getting password\n");
goto end;
}
2000-04-13 06:33:22 +00:00
2018-09-13 19:18:07 +00:00
in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
if (in == NULL)
goto end2;
2000-04-13 06:33:22 +00:00
2015-03-20 15:28:40 +00:00
if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
BIO_printf(bio_err, "unable to load DSA parameter file\n");
goto end;
}
BIO_free(in);
in = NULL;
2018-09-13 19:18:07 +00:00
out = bio_open_owner(outfile, FORMAT_PEM, private);
2015-03-20 15:28:40 +00:00
if (out == NULL)
2018-09-13 19:18:07 +00:00
goto end2;
2018-09-13 19:18:07 +00:00
DSA_get0_pqg(dsa, &p, NULL, NULL);
2018-09-13 19:18:07 +00:00
if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
2015-03-20 15:28:40 +00:00
BIO_printf(bio_err,
2018-09-13 19:18:07 +00:00
"Warning: It is not recommended to use more than %d bit for DSA keys.\n"
" Your key size is %d! Larger key size may behave not as expected.\n",
OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
2018-09-13 19:18:07 +00:00
BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
2015-03-20 15:28:40 +00:00
if (!DSA_generate_key(dsa))
goto end;
2018-09-13 19:18:07 +00:00
assert(private);
2015-03-20 15:28:40 +00:00
if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
goto end;
ret = 0;
end:
if (ret != 0)
ERR_print_errors(bio_err);
2018-09-13 19:18:07 +00:00
end2:
BIO_free(in);
BIO_free_all(out);
DSA_free(dsa);
2017-01-26 18:32:12 +00:00
release_engine(e);
2018-09-13 19:18:07 +00:00
OPENSSL_free(passout);
return ret;
2015-03-20 15:28:40 +00:00
}
#endif