]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/gendsa.c
Constify the parameter getters for RSA, DSA and DH
[thirdparty/openssl.git] / apps / gendsa.c
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_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 <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>
26
27 typedef 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
32 OPTIONS 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"},
40 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
41 # ifndef OPENSSL_NO_ENGINE
42 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
43 # endif
44 {NULL}
45 };
46
47 int gendsa_main(int argc, char **argv)
48 {
49 BIO *out = NULL, *in = NULL;
50 DSA *dsa = NULL;
51 const EVP_CIPHER *enc = NULL;
52 char *inrand = NULL, *dsaparams = NULL;
53 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
54 OPTION_CHOICE o;
55 int ret = 1, private = 0;
56 BIGNUM *p = NULL;
57
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:
77 (void)setup_engine(opt_arg(), 0);
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;
85 break;
86 }
87 }
88 argc = opt_num_rest();
89 argv = opt_rest();
90 private = 1;
91
92 if (argc != 1)
93 goto opthelp;
94 dsaparams = *argv;
95
96 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
97 BIO_printf(bio_err, "Error getting password\n");
98 goto end;
99 }
100
101 in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
102 if (in == NULL)
103 goto end2;
104
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;
111
112 out = bio_open_owner(outfile, FORMAT_PEM, private);
113 if (out == NULL)
114 goto end2;
115
116 if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {
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));
123
124 DSA_get0_pqg(dsa, &p, NULL, NULL);
125 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
126 if (!DSA_generate_key(dsa))
127 goto end;
128
129 app_RAND_write_file(NULL);
130
131 assert(private);
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);
138 end2:
139 BIO_free(in);
140 BIO_free_all(out);
141 DSA_free(dsa);
142 OPENSSL_free(passout);
143 return (ret);
144 }
145 #endif