]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/gendsa.c
Reduce optimization in hppa builds
[thirdparty/openssl.git] / apps / gendsa.c
CommitLineData
846e33c7 1/*
556009c5 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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 10#include <openssl/opensslconf.h>
1ae56f2f
RS
11
12#include <stdio.h>
13#include <string.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include "apps.h"
17#include "progs.h"
18#include <openssl/bio.h>
19#include <openssl/err.h>
20#include <openssl/bn.h>
21#include <openssl/dsa.h>
22#include <openssl/x509.h>
23#include <openssl/pem.h>
d02b48c6 24
7e1b7485 25typedef enum OPTION_choice {
b0f96018 26 OPT_COMMON,
a414fd67 27 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE, OPT_QUIET,
6bd4e3f2 28 OPT_R_ENUM, OPT_PROV_ENUM
7e1b7485
RS
29} OPTION_CHOICE;
30
44c83ebd 31const OPTIONS gendsa_options[] = {
92de469f 32 {OPT_HELP_STR, 1, '-', "Usage: %s [options] dsaparam-file\n"},
5388f986
RS
33
34 OPT_SECTION("General"),
7e1b7485 35 {"help", OPT_HELP, '-', "Display this summary"},
1ae56f2f 36#ifndef OPENSSL_NO_ENGINE
5388f986 37 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
1ae56f2f 38#endif
5388f986
RS
39
40 OPT_SECTION("Output"),
7e1b7485 41 {"out", OPT_OUT, '>', "Output the key to the specified file"},
12d56b29 42 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
3ee1eac2 43 OPT_R_OPTIONS,
6bd4e3f2 44 OPT_PROV_OPTIONS,
9c3bcfa0 45 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
b6a07f67 46 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
a414fd67 47 {"quiet", OPT_QUIET, '-', "Terse output"},
92de469f
RS
48
49 OPT_PARAMETERS(),
50 {"dsaparam-file", 0, 0, "File containing DSA parameters"},
7e1b7485
RS
51 {NULL}
52};
667ac4ec 53
7e1b7485 54int gendsa_main(int argc, char **argv)
0f113f3e 55{
dd1abd44 56 ENGINE *e = NULL;
0f113f3e 57 BIO *out = NULL, *in = NULL;
dddbbc6f
P
58 EVP_PKEY *pkey = NULL;
59 EVP_PKEY_CTX *ctx = NULL;
606a417f 60 EVP_CIPHER *enc = NULL;
03bbd346 61 char *dsaparams = NULL, *ciphername = NULL;
7e1b7485
RS
62 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
63 OPTION_CHOICE o;
26d52442 64 int ret = 1, private = 0, verbose = 0, nbits;
d02b48c6 65
2c272447 66 opt_set_unknown_name("cipher");
7e1b7485
RS
67 prog = opt_init(argc, argv, gendsa_options);
68 while ((o = opt_next()) != OPT_EOF) {
69 switch (o) {
70 case OPT_EOF:
71 case OPT_ERR:
72 opthelp:
73 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74 goto end;
75 case OPT_HELP:
76 ret = 0;
77 opt_help(gendsa_options);
78 goto end;
79 case OPT_OUT:
80 outfile = opt_arg();
81 break;
82 case OPT_PASSOUT:
83 passoutarg = opt_arg();
84 break;
85 case OPT_ENGINE:
dd1abd44 86 e = setup_engine(opt_arg(), 0);
7e1b7485 87 break;
3ee1eac2
RS
88 case OPT_R_CASES:
89 if (!opt_rand(o))
90 goto end;
7e1b7485 91 break;
6bd4e3f2
P
92 case OPT_PROV_CASES:
93 if (!opt_provider(o))
94 goto end;
95 break;
7e1b7485 96 case OPT_CIPHER:
03bbd346 97 ciphername = opt_unknown();
0f113f3e 98 break;
b6a07f67
PP
99 case OPT_VERBOSE:
100 verbose = 1;
101 break;
a414fd67
PP
102 case OPT_QUIET:
103 verbose = 0;
104 break;
0f113f3e 105 }
0f113f3e 106 }
021410ea
RS
107
108 /* One argument, the params file. */
d9f07357 109 if (!opt_check_rest_arg("params file"))
7e1b7485 110 goto opthelp;
d9f07357 111 argv = opt_rest();
03bbd346 112 dsaparams = argv[0];
021410ea 113
3ad60309
DDO
114 if (!app_RAND_load())
115 goto end;
116
d9f07357
DDO
117 if (!opt_cipher(ciphername, &enc))
118 goto end;
021410ea 119 private = 1;
d02b48c6 120
7e1b7485 121 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
0f113f3e
MC
122 BIO_printf(bio_err, "Error getting password\n");
123 goto end;
124 }
a3fe382e 125
d382e796 126 pkey = load_keyparams(dsaparams, FORMAT_UNDEF, 1, "DSA", "DSA parameters");
d02b48c6 127
bdd58d98 128 out = bio_open_owner(outfile, FORMAT_PEM, private);
0f113f3e 129 if (out == NULL)
7e1b7485 130 goto end2;
d02b48c6 131
ed576acd 132 nbits = EVP_PKEY_get_bits(pkey);
26d52442 133 if (nbits > OPENSSL_DSA_MAX_MODULUS_BITS)
0336df2f
GS
134 BIO_printf(bio_err,
135 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
136 " Your key size is %d! Larger key size may behave not as expected.\n",
ed576acd 137 OPENSSL_DSA_MAX_MODULUS_BITS, EVP_PKEY_get_bits(pkey));
0336df2f 138
30b2c359 139 ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), pkey, app_get0_propq());
dddbbc6f
P
140 if (ctx == NULL) {
141 BIO_printf(bio_err, "unable to create PKEY context\n");
142 goto end;
143 }
144 EVP_PKEY_free(pkey);
145 pkey = NULL;
146 if (EVP_PKEY_keygen_init(ctx) <= 0) {
147 BIO_printf(bio_err, "unable to set up for key generation\n");
148 goto end;
149 }
a7e4ca5b 150 pkey = app_keygen(ctx, "DSA", nbits, verbose);
8c040c08
BE
151 if (pkey == NULL)
152 goto end;
d02b48c6 153
3b061a00 154 assert(private);
dddbbc6f
P
155 if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
156 BIO_printf(bio_err, "unable to output generated key\n");
0f113f3e 157 goto end;
dddbbc6f 158 }
0f113f3e
MC
159 ret = 0;
160 end:
161 if (ret != 0)
162 ERR_print_errors(bio_err);
7e1b7485 163 end2:
ca3a82c3
RS
164 BIO_free(in);
165 BIO_free_all(out);
dddbbc6f
P
166 EVP_PKEY_free(pkey);
167 EVP_PKEY_CTX_free(ctx);
606a417f 168 EVP_CIPHER_free(enc);
dd1abd44 169 release_engine(e);
b548a1f1 170 OPENSSL_free(passout);
26a7d938 171 return ret;
0f113f3e 172}