add OPENSSL_FUNC.pod documenting OPENSSL_MSTR, OPENSSL_FUNC, and friends
[openssl.git] / apps / dsaparam.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <stdlib.h>
17 # include <time.h>
18 # include <string.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 static int verbose = 0;
29
30 static int dsa_cb(int p, int n, BN_GENCB *cb);
31
32 typedef enum OPTION_choice {
33     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
34     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
35     OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
36     OPT_R_ENUM
37 } OPTION_CHOICE;
38
39 const OPTIONS dsaparam_options[] = {
40     OPT_SECTION("General"),
41     {"help", OPT_HELP, '-', "Display this summary"},
42 # ifndef OPENSSL_NO_ENGINE
43     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
44 # endif
45
46     OPT_SECTION("Input"),
47     {"in", OPT_IN, '<', "Input file"},
48     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
49
50     OPT_SECTION("Output"),
51     {"out", OPT_OUT, '>', "Output file"},
52     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
53     {"text", OPT_TEXT, '-', "Print as text"},
54     {"C", OPT_C, '-', "Output C code"},
55     {"noout", OPT_NOOUT, '-', "No output"},
56     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
57     {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
58
59     OPT_R_OPTIONS,
60     {NULL}
61 };
62
63 int dsaparam_main(int argc, char **argv)
64 {
65     ENGINE *e = NULL;
66     DSA *dsa = NULL;
67     BIO *in = NULL, *out = NULL;
68     BN_GENCB *cb = NULL;
69     int numbits = -1, num = 0, genkey = 0;
70     int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
71     int ret = 1, i, text = 0, private = 0;
72     char *infile = NULL, *outfile = NULL, *prog;
73     OPTION_CHOICE o;
74
75     prog = opt_init(argc, argv, dsaparam_options);
76     while ((o = opt_next()) != OPT_EOF) {
77         switch (o) {
78         case OPT_EOF:
79         case OPT_ERR:
80  opthelp:
81             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
82             goto end;
83         case OPT_HELP:
84             opt_help(dsaparam_options);
85             ret = 0;
86             goto end;
87         case OPT_INFORM:
88             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
89                 goto opthelp;
90             break;
91         case OPT_IN:
92             infile = opt_arg();
93             break;
94         case OPT_OUTFORM:
95             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
96                 goto opthelp;
97             break;
98         case OPT_OUT:
99             outfile = opt_arg();
100             break;
101         case OPT_ENGINE:
102             e = setup_engine(opt_arg(), 0);
103             break;
104         case OPT_TEXT:
105             text = 1;
106             break;
107         case OPT_C:
108             C = 1;
109             break;
110         case OPT_GENKEY:
111             genkey = 1;
112             break;
113         case OPT_R_CASES:
114             if (!opt_rand(o))
115                 goto end;
116             break;
117         case OPT_NOOUT:
118             noout = 1;
119             break;
120         case OPT_VERBOSE:
121             verbose = 1;
122             break;
123         }
124     }
125     argc = opt_num_rest();
126     argv = opt_rest();
127
128     if (argc == 1) {
129         if (!opt_int(argv[0], &num) || num < 0)
130             goto end;
131         /* generate a key */
132         numbits = num;
133     }
134     private = genkey ? 1 : 0;
135
136     in = bio_open_default(infile, 'r', informat);
137     if (in == NULL)
138         goto end;
139     out = bio_open_owner(outfile, outformat, private);
140     if (out == NULL)
141         goto end;
142
143     if (numbits > 0) {
144         if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
145             BIO_printf(bio_err,
146                        "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
147                        "         Your key size is %d! Larger key size may behave not as expected.\n",
148                        OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
149
150         cb = BN_GENCB_new();
151         if (cb == NULL) {
152             BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
153             goto end;
154         }
155         BN_GENCB_set(cb, dsa_cb, bio_err);
156         dsa = DSA_new();
157         if (dsa == NULL) {
158             BIO_printf(bio_err, "Error allocating DSA object\n");
159             goto end;
160         }
161         if (verbose) {
162             BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
163                        num);
164             BIO_printf(bio_err, "This could take some time\n");
165         }
166         if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
167             ERR_print_errors(bio_err);
168             BIO_printf(bio_err, "Error, DSA key generation failed\n");
169             goto end;
170         }
171     } else if (informat == FORMAT_ASN1) {
172         dsa = d2i_DSAparams_bio(in, NULL);
173     } else {
174         dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
175     }
176     if (dsa == NULL) {
177         BIO_printf(bio_err, "unable to load DSA parameters\n");
178         ERR_print_errors(bio_err);
179         goto end;
180     }
181
182     if (text) {
183         DSAparams_print(out, dsa);
184     }
185
186     if (C) {
187         const BIGNUM *p = NULL, *q = NULL, *g = NULL;
188         unsigned char *data;
189         int len, bits_p;
190
191         DSA_get0_pqg(dsa, &p, &q, &g);
192         len = BN_num_bytes(p);
193         bits_p = BN_num_bits(p);
194
195         data = app_malloc(len + 20, "BN space");
196
197         BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
198         print_bignum_var(bio_out, p, "dsap", bits_p, data);
199         print_bignum_var(bio_out, q, "dsaq", bits_p, data);
200         print_bignum_var(bio_out, g, "dsag", bits_p, data);
201         BIO_printf(bio_out, "    DSA *dsa = DSA_new();\n"
202                             "    BIGNUM *p, *q, *g;\n"
203                             "\n");
204         BIO_printf(bio_out, "    if (dsa == NULL)\n"
205                             "        return NULL;\n");
206         BIO_printf(bio_out, "    if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
207                    bits_p, bits_p);
208         BIO_printf(bio_out, "                           q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
209                    bits_p, bits_p);
210         BIO_printf(bio_out, "                           g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
211                    bits_p, bits_p);
212         BIO_printf(bio_out, "        DSA_free(dsa);\n"
213                             "        BN_free(p);\n"
214                             "        BN_free(q);\n"
215                             "        BN_free(g);\n"
216                             "        return NULL;\n"
217                             "    }\n"
218                             "    return dsa;\n}\n");
219         OPENSSL_free(data);
220     }
221
222     if (outformat == FORMAT_ASN1 && genkey)
223         noout = 1;
224
225     if (!noout) {
226         if (outformat == FORMAT_ASN1)
227             i = i2d_DSAparams_bio(out, dsa);
228         else
229             i = PEM_write_bio_DSAparams(out, dsa);
230         if (!i) {
231             BIO_printf(bio_err, "unable to write DSA parameters\n");
232             ERR_print_errors(bio_err);
233             goto end;
234         }
235     }
236     if (genkey) {
237         DSA *dsakey;
238
239         if ((dsakey = DSAparams_dup(dsa)) == NULL)
240             goto end;
241         if (!DSA_generate_key(dsakey)) {
242             ERR_print_errors(bio_err);
243             DSA_free(dsakey);
244             goto end;
245         }
246         assert(private);
247         if (outformat == FORMAT_ASN1)
248             i = i2d_DSAPrivateKey_bio(out, dsakey);
249         else
250             i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
251                                             NULL);
252         DSA_free(dsakey);
253     }
254     ret = 0;
255  end:
256     BN_GENCB_free(cb);
257     BIO_free(in);
258     BIO_free_all(out);
259     DSA_free(dsa);
260     release_engine(e);
261     return ret;
262 }
263
264 static int dsa_cb(int p, int n, BN_GENCB *cb)
265 {
266     static const char symbols[] = ".+*\n";
267     char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
268
269     if (!verbose)
270         return 1;
271
272     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
273     (void)BIO_flush(BN_GENCB_get_arg(cb));
274     return 1;
275 }
276 #endif