]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/gendsa.c
Make asn1 fuzzer more reproducible
[thirdparty/openssl.git] / apps / gendsa.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
846e33c7
RS
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
d02b48c6
RE
8 */
9
effaf4de
RS
10#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_DSA
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
0f113f3e
MC
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>
d02b48c6 26
7e1b7485
RS
27typedef enum OPTION_choice {
28 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
29 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_RAND, OPT_CIPHER
30} OPTION_CHOICE;
31
44c83ebd 32const OPTIONS gendsa_options[] = {
7e1b7485
RS
33 {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
34 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
35 {"help", OPT_HELP, '-', "Display this summary"},
36 {"out", OPT_OUT, '>', "Output the key to the specified file"},
12d56b29 37 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
7e1b7485
RS
38 {"rand", OPT_RAND, 's',
39 "Load the file(s) into the random number generator"},
9c3bcfa0 40 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
7e1b7485
RS
41# ifndef OPENSSL_NO_ENGINE
42 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
43# endif
7e1b7485
RS
44 {NULL}
45};
667ac4ec 46
7e1b7485 47int gendsa_main(int argc, char **argv)
0f113f3e 48{
dd1abd44 49 ENGINE *e = NULL;
0f113f3e 50 BIO *out = NULL, *in = NULL;
7e1b7485 51 DSA *dsa = NULL;
0f113f3e 52 const EVP_CIPHER *enc = NULL;
333b070e 53 char *inrand = NULL, *dsaparams = NULL;
7e1b7485
RS
54 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
55 OPTION_CHOICE o;
3b061a00 56 int ret = 1, private = 0;
2ac6115d 57 const BIGNUM *p = NULL;
d02b48c6 58
7e1b7485
RS
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:
dd1abd44 78 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
79 break;
80 case OPT_RAND:
81 inrand = opt_arg();
82 break;
83 case OPT_CIPHER:
84 if (!opt_cipher(opt_unknown(), &enc))
85 goto end;
0f113f3e 86 break;
0f113f3e 87 }
0f113f3e 88 }
7e1b7485
RS
89 argc = opt_num_rest();
90 argv = opt_rest();
3b061a00 91 private = 1;
7e1b7485
RS
92
93 if (argc != 1)
94 goto opthelp;
95 dsaparams = *argv;
d02b48c6 96
7e1b7485 97 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
0f113f3e
MC
98 BIO_printf(bio_err, "Error getting password\n");
99 goto end;
100 }
a3fe382e 101
bdd58d98 102 in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
7e1b7485
RS
103 if (in == NULL)
104 goto end2;
a3fe382e 105
0f113f3e
MC
106 if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
107 BIO_printf(bio_err, "unable to load DSA parameter file\n");
108 goto end;
109 }
110 BIO_free(in);
111 in = NULL;
d02b48c6 112
bdd58d98 113 out = bio_open_owner(outfile, FORMAT_PEM, private);
0f113f3e 114 if (out == NULL)
7e1b7485 115 goto end2;
d02b48c6 116
7e1b7485 117 if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {
0f113f3e
MC
118 BIO_printf(bio_err,
119 "warning, not much extra random data, consider using the -rand option\n");
120 }
121 if (inrand != NULL)
122 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
123 app_RAND_load_files(inrand));
d02b48c6 124
6e9fa57c
MC
125 DSA_get0_pqg(dsa, &p, NULL, NULL);
126 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
0f113f3e
MC
127 if (!DSA_generate_key(dsa))
128 goto end;
d02b48c6 129
7e1b7485 130 app_RAND_write_file(NULL);
d02b48c6 131
3b061a00 132 assert(private);
0f113f3e
MC
133 if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
134 goto end;
135 ret = 0;
136 end:
137 if (ret != 0)
138 ERR_print_errors(bio_err);
7e1b7485 139 end2:
ca3a82c3
RS
140 BIO_free(in);
141 BIO_free_all(out);
d6407083 142 DSA_free(dsa);
dd1abd44 143 release_engine(e);
b548a1f1 144 OPENSSL_free(passout);
7e1b7485 145 return (ret);
0f113f3e 146}
f5d7a031 147#endif