]>
Commit | Line | Data |
---|---|---|
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 | const 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', "Output file pass phrase source"}, | |
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 | ENGINE *e = NULL; | |
50 | BIO *out = NULL, *in = NULL; | |
51 | DSA *dsa = NULL; | |
52 | const EVP_CIPHER *enc = NULL; | |
53 | char *inrand = NULL, *dsaparams = NULL; | |
54 | char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog; | |
55 | OPTION_CHOICE o; | |
56 | int ret = 1, private = 0; | |
57 | const BIGNUM *p = NULL; | |
58 | ||
59 | prog = opt_init(argc, argv, gendsa_options); | |
60 | while ((o = opt_next()) != OPT_EOF) { | |
61 | switch (o) { | |
62 | case OPT_EOF: | |
63 | case OPT_ERR: | |
64 | opthelp: | |
65 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
66 | goto end; | |
67 | case OPT_HELP: | |
68 | ret = 0; | |
69 | opt_help(gendsa_options); | |
70 | goto end; | |
71 | case OPT_OUT: | |
72 | outfile = opt_arg(); | |
73 | break; | |
74 | case OPT_PASSOUT: | |
75 | passoutarg = opt_arg(); | |
76 | break; | |
77 | case OPT_ENGINE: | |
78 | e = setup_engine(opt_arg(), 0); | |
79 | break; | |
80 | case OPT_RAND: | |
81 | inrand = opt_arg(); | |
82 | break; | |
83 | case OPT_CIPHER: | |
84 | if (!opt_cipher(opt_unknown(), &enc)) | |
85 | goto end; | |
86 | break; | |
87 | } | |
88 | } | |
89 | argc = opt_num_rest(); | |
90 | argv = opt_rest(); | |
91 | private = 1; | |
92 | ||
93 | if (argc != 1) | |
94 | goto opthelp; | |
95 | dsaparams = *argv; | |
96 | ||
97 | if (!app_passwd(NULL, passoutarg, NULL, &passout)) { | |
98 | BIO_printf(bio_err, "Error getting password\n"); | |
99 | goto end; | |
100 | } | |
101 | ||
102 | in = bio_open_default(dsaparams, 'r', FORMAT_PEM); | |
103 | if (in == NULL) | |
104 | goto end2; | |
105 | ||
106 | if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { | |
107 | BIO_printf(bio_err, "unable to load DSA parameter file\n"); | |
108 | goto end; | |
109 | } | |
110 | BIO_free(in); | |
111 | in = NULL; | |
112 | ||
113 | out = bio_open_owner(outfile, FORMAT_PEM, private); | |
114 | if (out == NULL) | |
115 | goto end2; | |
116 | ||
117 | if (!app_RAND_load_file(NULL, 1) && inrand == NULL) { | |
118 | BIO_printf(bio_err, | |
119 | "warning, not much extra random data, consider using the -rand option\n"); | |
120 | } | |
121 | if (inrand != NULL) | |
122 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", | |
123 | app_RAND_load_files(inrand)); | |
124 | ||
125 | DSA_get0_pqg(dsa, &p, NULL, NULL); | |
126 | BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p)); | |
127 | if (!DSA_generate_key(dsa)) | |
128 | goto end; | |
129 | ||
130 | app_RAND_write_file(NULL); | |
131 | ||
132 | assert(private); | |
133 | if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) | |
134 | goto end; | |
135 | ret = 0; | |
136 | end: | |
137 | if (ret != 0) | |
138 | ERR_print_errors(bio_err); | |
139 | end2: | |
140 | BIO_free(in); | |
141 | BIO_free_all(out); | |
142 | DSA_free(dsa); | |
143 | release_engine(e); | |
144 | OPENSSL_free(passout); | |
145 | return (ret); | |
146 | } | |
147 | #endif |