]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/gendsa.c
Convert num_alloc to a size_t in stack.c and tweak style
[thirdparty/openssl.git] / apps / gendsa.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
846e33c7
RS
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
d02b48c6
RE
8 */
9
effaf4de
RS
10#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_DSA
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
0f113f3e
MC
15# include <stdio.h>
16# include <string.h>
17# include <sys/types.h>
18# include <sys/stat.h>
19# include "apps.h"
20# include <openssl/bio.h>
21# include <openssl/err.h>
22# include <openssl/bn.h>
23# include <openssl/dsa.h>
24# include <openssl/x509.h>
25# include <openssl/pem.h>
d02b48c6 26
7e1b7485
RS
27typedef enum OPTION_choice {
28 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
29 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_RAND, OPT_CIPHER
30} OPTION_CHOICE;
31
32OPTIONS gendsa_options[] = {
33 {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
34 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
35 {"help", OPT_HELP, '-', "Display this summary"},
36 {"out", OPT_OUT, '>', "Output the key to the specified file"},
37 {"passout", OPT_PASSOUT, 's'},
38 {"rand", OPT_RAND, 's',
39 "Load the file(s) into the random number generator"},
9c3bcfa0 40 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
7e1b7485
RS
41# ifndef OPENSSL_NO_ENGINE
42 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
43# endif
7e1b7485
RS
44 {NULL}
45};
667ac4ec 46
7e1b7485 47int gendsa_main(int argc, char **argv)
0f113f3e 48{
0f113f3e 49 BIO *out = NULL, *in = NULL;
7e1b7485 50 DSA *dsa = NULL;
0f113f3e 51 const EVP_CIPHER *enc = NULL;
333b070e 52 char *inrand = NULL, *dsaparams = NULL;
7e1b7485
RS
53 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
54 OPTION_CHOICE o;
3b061a00 55 int ret = 1, private = 0;
2ac6115d 56 const BIGNUM *p = NULL;
d02b48c6 57
7e1b7485
RS
58 prog = opt_init(argc, argv, gendsa_options);
59 while ((o = opt_next()) != OPT_EOF) {
60 switch (o) {
61 case OPT_EOF:
62 case OPT_ERR:
63 opthelp:
64 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
65 goto end;
66 case OPT_HELP:
67 ret = 0;
68 opt_help(gendsa_options);
69 goto end;
70 case OPT_OUT:
71 outfile = opt_arg();
72 break;
73 case OPT_PASSOUT:
74 passoutarg = opt_arg();
75 break;
76 case OPT_ENGINE:
333b070e 77 (void)setup_engine(opt_arg(), 0);
7e1b7485
RS
78 break;
79 case OPT_RAND:
80 inrand = opt_arg();
81 break;
82 case OPT_CIPHER:
83 if (!opt_cipher(opt_unknown(), &enc))
84 goto end;
0f113f3e 85 break;
0f113f3e 86 }
0f113f3e 87 }
7e1b7485
RS
88 argc = opt_num_rest();
89 argv = opt_rest();
3b061a00 90 private = 1;
7e1b7485
RS
91
92 if (argc != 1)
93 goto opthelp;
94 dsaparams = *argv;
d02b48c6 95
7e1b7485 96 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
0f113f3e
MC
97 BIO_printf(bio_err, "Error getting password\n");
98 goto end;
99 }
a3fe382e 100
bdd58d98 101 in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
7e1b7485
RS
102 if (in == NULL)
103 goto end2;
a3fe382e 104
0f113f3e
MC
105 if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
106 BIO_printf(bio_err, "unable to load DSA parameter file\n");
107 goto end;
108 }
109 BIO_free(in);
110 in = NULL;
d02b48c6 111
bdd58d98 112 out = bio_open_owner(outfile, FORMAT_PEM, private);
0f113f3e 113 if (out == NULL)
7e1b7485 114 goto end2;
d02b48c6 115
7e1b7485 116 if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {
0f113f3e
MC
117 BIO_printf(bio_err,
118 "warning, not much extra random data, consider using the -rand option\n");
119 }
120 if (inrand != NULL)
121 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
122 app_RAND_load_files(inrand));
d02b48c6 123
6e9fa57c
MC
124 DSA_get0_pqg(dsa, &p, NULL, NULL);
125 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
0f113f3e
MC
126 if (!DSA_generate_key(dsa))
127 goto end;
d02b48c6 128
7e1b7485 129 app_RAND_write_file(NULL);
d02b48c6 130
3b061a00 131 assert(private);
0f113f3e
MC
132 if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
133 goto end;
134 ret = 0;
135 end:
136 if (ret != 0)
137 ERR_print_errors(bio_err);
7e1b7485 138 end2:
ca3a82c3
RS
139 BIO_free(in);
140 BIO_free_all(out);
d6407083 141 DSA_free(dsa);
b548a1f1 142 OPENSSL_free(passout);
7e1b7485 143 return (ret);
0f113f3e 144}
f5d7a031 145#endif