]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/gendsa.c
Document command parameters.
[thirdparty/openssl.git] / apps / gendsa.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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_DSA
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
14
15 # include <stdio.h>
16 # include <string.h>
17 # include <sys/types.h>
18 # include <sys/stat.h>
19 # include "apps.h"
20 # include "progs.h"
21 # include <openssl/bio.h>
22 # include <openssl/err.h>
23 # include <openssl/bn.h>
24 # include <openssl/dsa.h>
25 # include <openssl/x509.h>
26 # include <openssl/pem.h>
27
28 typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE,
31 OPT_R_ENUM
32 } OPTION_CHOICE;
33
34 const OPTIONS gendsa_options[] = {
35 {OPT_HELP_STR, 1, '-', "Usage: %s [options] dsaparam-file\n"},
36
37 OPT_SECTION("General"),
38 {"help", OPT_HELP, '-', "Display this summary"},
39 # ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
41 # endif
42
43 OPT_SECTION("Output"),
44 {"out", OPT_OUT, '>', "Output the key to the specified file"},
45 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
46 OPT_R_OPTIONS,
47 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
48 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
49
50 OPT_PARAMETERS(),
51 {"dsaparam-file", 0, 0, "File containing DSA parameters"},
52 {NULL}
53 };
54
55 int gendsa_main(int argc, char **argv)
56 {
57 ENGINE *e = NULL;
58 BIO *out = NULL, *in = NULL;
59 DSA *dsa = NULL;
60 const EVP_CIPHER *enc = NULL;
61 char *dsaparams = NULL;
62 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
63 OPTION_CHOICE o;
64 int ret = 1, private = 0, verbose = 0;
65 const BIGNUM *p = NULL;
66
67 prog = opt_init(argc, argv, gendsa_options);
68 while ((o = opt_next()) != OPT_EOF) {
69 switch (o) {
70 case OPT_EOF:
71 case OPT_ERR:
72 opthelp:
73 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74 goto end;
75 case OPT_HELP:
76 ret = 0;
77 opt_help(gendsa_options);
78 goto end;
79 case OPT_OUT:
80 outfile = opt_arg();
81 break;
82 case OPT_PASSOUT:
83 passoutarg = opt_arg();
84 break;
85 case OPT_ENGINE:
86 e = setup_engine(opt_arg(), 0);
87 break;
88 case OPT_R_CASES:
89 if (!opt_rand(o))
90 goto end;
91 break;
92 case OPT_CIPHER:
93 if (!opt_cipher(opt_unknown(), &enc))
94 goto end;
95 break;
96 case OPT_VERBOSE:
97 verbose = 1;
98 break;
99 }
100 }
101 argc = opt_num_rest();
102 argv = opt_rest();
103 private = 1;
104
105 if (argc != 1)
106 goto opthelp;
107 dsaparams = *argv;
108
109 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
110 BIO_printf(bio_err, "Error getting password\n");
111 goto end;
112 }
113
114 in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
115 if (in == NULL)
116 goto end2;
117
118 if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
119 BIO_printf(bio_err, "unable to load DSA parameter file\n");
120 goto end;
121 }
122 BIO_free(in);
123 in = NULL;
124
125 out = bio_open_owner(outfile, FORMAT_PEM, private);
126 if (out == NULL)
127 goto end2;
128
129 DSA_get0_pqg(dsa, &p, NULL, NULL);
130
131 if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
132 BIO_printf(bio_err,
133 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
134 " Your key size is %d! Larger key size may behave not as expected.\n",
135 OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
136
137 if (verbose)
138 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
139 if (!DSA_generate_key(dsa))
140 goto end;
141
142 assert(private);
143 if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
144 goto end;
145 ret = 0;
146 end:
147 if (ret != 0)
148 ERR_print_errors(bio_err);
149 end2:
150 BIO_free(in);
151 BIO_free_all(out);
152 DSA_free(dsa);
153 release_engine(e);
154 OPENSSL_free(passout);
155 return ret;
156 }
157 #endif