Remove parentheses of return.
[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_CIPHER,
30     OPT_R_ENUM
31 } OPTION_CHOICE;
32
33 const OPTIONS gendsa_options[] = {
34     {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
35     {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
36     {"help", OPT_HELP, '-', "Display this summary"},
37     {"out", OPT_OUT, '>', "Output the key to the specified file"},
38     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
39     OPT_R_OPTIONS,
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 *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_R_CASES:
81             if (!opt_rand(o))
82                 goto end;
83             break;
84         case OPT_CIPHER:
85             if (!opt_cipher(opt_unknown(), &enc))
86                 goto end;
87             break;
88         }
89     }
90     argc = opt_num_rest();
91     argv = opt_rest();
92     private = 1;
93
94     if (argc != 1)
95         goto opthelp;
96     dsaparams = *argv;
97
98     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
99         BIO_printf(bio_err, "Error getting password\n");
100         goto end;
101     }
102
103     in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
104     if (in == NULL)
105         goto end2;
106
107     if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
108         BIO_printf(bio_err, "unable to load DSA parameter file\n");
109         goto end;
110     }
111     BIO_free(in);
112     in = NULL;
113
114     out = bio_open_owner(outfile, FORMAT_PEM, private);
115     if (out == NULL)
116         goto end2;
117
118     DSA_get0_pqg(dsa, &p, NULL, NULL);
119     BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
120     if (!DSA_generate_key(dsa))
121         goto end;
122
123     assert(private);
124     if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
125         goto end;
126     ret = 0;
127  end:
128     if (ret != 0)
129         ERR_print_errors(bio_err);
130  end2:
131     BIO_free(in);
132     BIO_free_all(out);
133     DSA_free(dsa);
134     release_engine(e);
135     OPENSSL_free(passout);
136     return ret;
137 }
138 #endif