xref: /freebsd/crypto/openssl/apps/gendsa.c (revision f4f33ea0c752ff0f9bfad34991d5bbb54e71133d)
1 /*
2  * Copyright 1995-2018 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 "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,
31     OPT_R_ENUM
32 } OPTION_CHOICE;
33 
34 const OPTIONS gendsa_options[] = {
35     {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
36     {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
37     {"help", OPT_HELP, '-', "Display this summary"},
38     {"out", OPT_OUT, '>', "Output the key to the specified file"},
39     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
40     OPT_R_OPTIONS,
41     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
42 # ifndef OPENSSL_NO_ENGINE
43     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
44 # endif
45     {NULL}
46 };
47 
48 int gendsa_main(int argc, char **argv)
49 {
50     ENGINE *e = NULL;
51     BIO *out = NULL, *in = NULL;
52     DSA *dsa = NULL;
53     const EVP_CIPHER *enc = NULL;
54     char *dsaparams = NULL;
55     char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
56     OPTION_CHOICE o;
57     int ret = 1, private = 0;
58     const BIGNUM *p = NULL;
59 
60     prog = opt_init(argc, argv, gendsa_options);
61     while ((o = opt_next()) != OPT_EOF) {
62         switch (o) {
63         case OPT_EOF:
64         case OPT_ERR:
65  opthelp:
66             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
67             goto end;
68         case OPT_HELP:
69             ret = 0;
70             opt_help(gendsa_options);
71             goto end;
72         case OPT_OUT:
73             outfile = opt_arg();
74             break;
75         case OPT_PASSOUT:
76             passoutarg = opt_arg();
77             break;
78         case OPT_ENGINE:
79             e = setup_engine(opt_arg(), 0);
80             break;
81         case OPT_R_CASES:
82             if (!opt_rand(o))
83                 goto end;
84             break;
85         case OPT_CIPHER:
86             if (!opt_cipher(opt_unknown(), &enc))
87                 goto end;
88             break;
89         }
90     }
91     argc = opt_num_rest();
92     argv = opt_rest();
93     private = 1;
94 
95     if (argc != 1)
96         goto opthelp;
97     dsaparams = *argv;
98 
99     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
100         BIO_printf(bio_err, "Error getting password\n");
101         goto end;
102     }
103 
104     in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
105     if (in == NULL)
106         goto end2;
107 
108     if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
109         BIO_printf(bio_err, "unable to load DSA parameter file\n");
110         goto end;
111     }
112     BIO_free(in);
113     in = NULL;
114 
115     out = bio_open_owner(outfile, FORMAT_PEM, private);
116     if (out == NULL)
117         goto end2;
118 
119     DSA_get0_pqg(dsa, &p, NULL, NULL);
120 
121     if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
122         BIO_printf(bio_err,
123                    "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
124                    "         Your key size is %d! Larger key size may behave not as expected.\n",
125                    OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
126 
127     BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
128     if (!DSA_generate_key(dsa))
129         goto end;
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     release_engine(e);
143     OPENSSL_free(passout);
144     return ret;
145 }
146 #endif
147