blob: 341480b8188974943c9eebce3cb2aee5845cf227 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
Matt Caswell6738bf12018-02-13 12:51:29 +00002 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * 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
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00008 */
9
Rich Salzeffaf4d2016-01-31 13:08:23 -050010#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_DSA
12NON_EMPTY_TRANSLATION_UNIT
13#else
Geoff Thorpe5daec7e2002-12-08 05:38:44 +000014
Matt Caswell0f113f32015-01-22 03:40:55 +000015# include <stdio.h>
16# include <stdlib.h>
17# include <time.h>
18# include <string.h>
19# include "apps.h"
Richard Levittedab2cd62018-01-31 11:13:10 +010020# include "progs.h"
Matt Caswell0f113f32015-01-22 03:40:55 +000021# 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>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000027
Rich Salz6d23cf92015-01-12 17:29:26 -050028static int dsa_cb(int p, int n, BN_GENCB *cb);
Ralf S. Engelschall667ac4e2000-02-11 09:47:18 +000029
Rich Salz7e1b7482015-04-24 15:26:15 -040030typedef enum OPTION_choice {
31 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
32 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
Rich Salz3ee1eac2017-07-05 10:58:48 -040033 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_R_ENUM
Rich Salz7e1b7482015-04-24 15:26:15 -040034} OPTION_CHOICE;
Ralf S. Engelschall667ac4e2000-02-11 09:47:18 +000035
FdaSilvaYY44c83eb2016-03-13 14:07:50 +010036const OPTIONS dsaparam_options[] = {
Rich Salz7e1b7482015-04-24 15:26:15 -040037 {"help", OPT_HELP, '-', "Display this summary"},
38 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
39 {"in", OPT_IN, '<', "Input file"},
40 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
41 {"out", OPT_OUT, '>', "Output file"},
42 {"text", OPT_TEXT, '-', "Print as text"},
43 {"C", OPT_C, '-', "Output C code"},
44 {"noout", OPT_NOOUT, '-', "No output"},
45 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
Rich Salz3ee1eac2017-07-05 10:58:48 -040046 OPT_R_OPTIONS,
Rich Salz9c3bcfa2015-05-15 13:50:38 -040047# ifndef OPENSSL_NO_ENGINE
48 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
49# endif
Rich Salz7e1b7482015-04-24 15:26:15 -040050 {NULL}
51};
52
53int dsaparam_main(int argc, char **argv)
Matt Caswell0f113f32015-01-22 03:40:55 +000054{
Richard Levittedd1abd42016-09-28 23:39:18 +020055 ENGINE *e = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000056 DSA *dsa = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000057 BIO *in = NULL, *out = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000058 BN_GENCB *cb = NULL;
Rich Salz3ee1eac2017-07-05 10:58:48 -040059 int numbits = -1, num = 0, genkey = 0;
Rich Salz3b061a02015-05-02 10:01:33 -040060 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
61 int ret = 1, i, text = 0, private = 0;
Rich Salz3ee1eac2017-07-05 10:58:48 -040062 char *infile = NULL, *outfile = NULL, *prog;
Rich Salz7e1b7482015-04-24 15:26:15 -040063 OPTION_CHOICE o;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000064
Rich Salz7e1b7482015-04-24 15:26:15 -040065 prog = opt_init(argc, argv, dsaparam_options);
66 while ((o = opt_next()) != OPT_EOF) {
67 switch (o) {
68 case OPT_EOF:
69 case OPT_ERR:
70 opthelp:
71 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
72 goto end;
73 case OPT_HELP:
74 opt_help(dsaparam_options);
75 ret = 0;
76 goto end;
77 case OPT_INFORM:
78 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
79 goto opthelp;
80 break;
81 case OPT_IN:
82 infile = opt_arg();
83 break;
84 case OPT_OUTFORM:
85 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
86 goto opthelp;
87 break;
88 case OPT_OUT:
89 outfile = opt_arg();
90 break;
91 case OPT_ENGINE:
Richard Levittedd1abd42016-09-28 23:39:18 +020092 e = setup_engine(opt_arg(), 0);
Rich Salz7e1b7482015-04-24 15:26:15 -040093 break;
Rich Salz7e1b7482015-04-24 15:26:15 -040094 case OPT_TEXT:
Matt Caswell0f113f32015-01-22 03:40:55 +000095 text = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -040096 break;
97 case OPT_C:
Matt Caswell0f113f32015-01-22 03:40:55 +000098 C = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -040099 break;
100 case OPT_GENKEY:
Rich Salz3ee1eac2017-07-05 10:58:48 -0400101 genkey = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400102 break;
Rich Salz3ee1eac2017-07-05 10:58:48 -0400103 case OPT_R_CASES:
104 if (!opt_rand(o))
105 goto end;
Rich Salz7e1b7482015-04-24 15:26:15 -0400106 break;
107 case OPT_NOOUT:
Matt Caswell0f113f32015-01-22 03:40:55 +0000108 noout = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400109 break;
Matt Caswell0f113f32015-01-22 03:40:55 +0000110 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000111 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400112 argc = opt_num_rest();
113 argv = opt_rest();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000114
Rich Salz7e1b7482015-04-24 15:26:15 -0400115 if (argc == 1) {
Rich Salzbd4850d2016-01-11 20:40:38 -0500116 if (!opt_int(argv[0], &num) || num < 0)
Matt Caswell0f113f32015-01-22 03:40:55 +0000117 goto end;
Rich Salz7e1b7482015-04-24 15:26:15 -0400118 /* generate a key */
119 numbits = num;
Matt Caswell0f113f32015-01-22 03:40:55 +0000120 }
Rich Salz3b061a02015-05-02 10:01:33 -0400121 private = genkey ? 1 : 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400122
Richard Levittebdd58d92015-09-04 12:49:06 +0200123 in = bio_open_default(infile, 'r', informat);
Rich Salz7e1b7482015-04-24 15:26:15 -0400124 if (in == NULL)
125 goto end;
Richard Levittebdd58d92015-09-04 12:49:06 +0200126 out = bio_open_owner(outfile, outformat, private);
Rich Salz7e1b7482015-04-24 15:26:15 -0400127 if (out == NULL)
128 goto end;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000129
Matt Caswell0f113f32015-01-22 03:40:55 +0000130 if (numbits > 0) {
131 cb = BN_GENCB_new();
Matt Caswell96487cd2015-10-30 11:18:04 +0000132 if (cb == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000133 BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
134 goto end;
135 }
136 BN_GENCB_set(cb, dsa_cb, bio_err);
Matt Caswell0f113f32015-01-22 03:40:55 +0000137 dsa = DSA_new();
Matt Caswell96487cd2015-10-30 11:18:04 +0000138 if (dsa == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000139 BIO_printf(bio_err, "Error allocating DSA object\n");
140 goto end;
141 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000142 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
143 num);
144 BIO_printf(bio_err, "This could take some time\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000145 if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000146 ERR_print_errors(bio_err);
147 BIO_printf(bio_err, "Error, DSA key generation failed\n");
148 goto end;
149 }
Paul Yang22342122017-06-13 01:24:02 +0800150 } else if (informat == FORMAT_ASN1) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000151 dsa = d2i_DSAparams_bio(in, NULL);
Paul Yang22342122017-06-13 01:24:02 +0800152 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +0000153 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
Paul Yang22342122017-06-13 01:24:02 +0800154 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000155 if (dsa == NULL) {
156 BIO_printf(bio_err, "unable to load DSA parameters\n");
157 ERR_print_errors(bio_err);
158 goto end;
159 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000160
Matt Caswell0f113f32015-01-22 03:40:55 +0000161 if (text) {
162 DSAparams_print(out, dsa);
163 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000164
Matt Caswell0f113f32015-01-22 03:40:55 +0000165 if (C) {
Richard Levitte2ac61152016-06-14 15:49:05 +0200166 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
Viktor Dukhovniae6c5532016-04-03 20:58:09 -0400167 unsigned char *data;
Matt Caswell6e9fa572016-03-30 17:18:55 +0100168 int len, bits_p;
169
170 DSA_get0_pqg(dsa, &p, &q, &g);
171 len = BN_num_bytes(p);
172 bits_p = BN_num_bits(p);
173
Viktor Dukhovniae6c5532016-04-03 20:58:09 -0400174 data = app_malloc(len + 20, "BN space");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000175
Rich Salz7e1b7482015-04-24 15:26:15 -0400176 BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
Matt Caswell6e9fa572016-03-30 17:18:55 +0100177 print_bignum_var(bio_out, p, "dsap", len, data);
178 print_bignum_var(bio_out, q, "dsaq", len, data);
179 print_bignum_var(bio_out, g, "dsag", len, data);
Rich Salz7e1b7482015-04-24 15:26:15 -0400180 BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
181 "\n");
182 BIO_printf(bio_out, " if (dsa == NULL)\n"
183 " return NULL;\n");
Rich Salzcbe29642017-12-07 13:39:34 -0500184 BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL);\n",
Matt Caswell0f113f32015-01-22 03:40:55 +0000185 bits_p, bits_p);
Rich Salzcbe29642017-12-07 13:39:34 -0500186 BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL);\n",
Matt Caswell0f113f32015-01-22 03:40:55 +0000187 bits_p, bits_p);
Rich Salzcbe29642017-12-07 13:39:34 -0500188 BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL);\n",
Matt Caswell0f113f32015-01-22 03:40:55 +0000189 bits_p, bits_p);
Rich Salz7e1b7482015-04-24 15:26:15 -0400190 BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n"
191 " DSA_free(dsa);\n"
192 " return NULL;\n"
193 " }\n"
194 " return(dsa);\n}\n");
Matt Caswella855d1a2016-04-27 14:54:58 +0100195 OPENSSL_free(data);
Matt Caswell0f113f32015-01-22 03:40:55 +0000196 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000197
Bernd Edlinger52814352018-03-25 12:50:17 +0200198 if (outformat == FORMAT_ASN1 && genkey)
199 noout = 1;
200
Matt Caswell0f113f32015-01-22 03:40:55 +0000201 if (!noout) {
202 if (outformat == FORMAT_ASN1)
203 i = i2d_DSAparams_bio(out, dsa);
Rich Salz7e1b7482015-04-24 15:26:15 -0400204 else
Matt Caswell0f113f32015-01-22 03:40:55 +0000205 i = PEM_write_bio_DSAparams(out, dsa);
Matt Caswell0f113f32015-01-22 03:40:55 +0000206 if (!i) {
207 BIO_printf(bio_err, "unable to write DSA parameters\n");
208 ERR_print_errors(bio_err);
209 goto end;
210 }
211 }
212 if (genkey) {
213 DSA *dsakey;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000214
Matt Caswell0f113f32015-01-22 03:40:55 +0000215 if ((dsakey = DSAparams_dup(dsa)) == NULL)
216 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000217 if (!DSA_generate_key(dsakey)) {
218 ERR_print_errors(bio_err);
219 DSA_free(dsakey);
220 goto end;
221 }
Rich Salz3b061a02015-05-02 10:01:33 -0400222 assert(private);
Matt Caswell0f113f32015-01-22 03:40:55 +0000223 if (outformat == FORMAT_ASN1)
224 i = i2d_DSAPrivateKey_bio(out, dsakey);
Rich Salz7e1b7482015-04-24 15:26:15 -0400225 else
Matt Caswell0f113f32015-01-22 03:40:55 +0000226 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
227 NULL);
Matt Caswell0f113f32015-01-22 03:40:55 +0000228 DSA_free(dsakey);
229 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000230 ret = 0;
231 end:
Rich Salz23a1d5e2015-04-30 21:37:06 -0400232 BN_GENCB_free(cb);
Rich Salzca3a82c2015-03-25 11:31:18 -0400233 BIO_free(in);
234 BIO_free_all(out);
Rich Salzd6407082015-03-24 10:17:37 -0400235 DSA_free(dsa);
Richard Levittedd1abd42016-09-28 23:39:18 +0200236 release_engine(e);
KaoruToda26a7d932017-10-17 23:04:09 +0900237 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000238}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000239
Rich Salz6d23cf92015-01-12 17:29:26 -0500240static int dsa_cb(int p, int n, BN_GENCB *cb)
Matt Caswell0f113f32015-01-22 03:40:55 +0000241{
242 char c = '*';
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000243
Matt Caswell0f113f32015-01-22 03:40:55 +0000244 if (p == 0)
245 c = '.';
246 if (p == 1)
247 c = '+';
248 if (p == 2)
249 c = '*';
250 if (p == 3)
251 c = '\n';
252 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
253 (void)BIO_flush(BN_GENCB_get_arg(cb));
Matt Caswell0f113f32015-01-22 03:40:55 +0000254 return 1;
255}
Ulf Möllerf5d7a031999-04-27 01:14:46 +0000256#endif