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