| File: | out/../deps/openssl/openssl/apps/dsaparam.c |
| Warning: | line 223, column 13 Value stored to 'i' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright 1995-2022 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 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include "apps.h" |
| 15 | #include <time.h> |
| 16 | #include <string.h> |
| 17 | #include "apps.h" |
| 18 | #include "progs.h" |
| 19 | #include <openssl/bio.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/bn.h> |
| 22 | #include <openssl/dsa.h> |
| 23 | #include <openssl/x509.h> |
| 24 | #include <openssl/pem.h> |
| 25 | |
| 26 | static int verbose = 0; |
| 27 | |
| 28 | static int gendsa_cb(EVP_PKEY_CTX *ctx); |
| 29 | |
| 30 | typedef enum OPTION_choice { |
| 31 | OPT_COMMONOPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
| 32 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, |
| 33 | OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE, |
| 34 | OPT_R_ENUMOPT_R__FIRST=1500, OPT_R_RAND, OPT_R_WRITERAND, OPT_R__LAST, OPT_PROV_ENUMOPT_PROV__FIRST=1600, OPT_PROV_PROVIDER, OPT_PROV_PROVIDER_PATH , OPT_PROV_PROPQUERY, OPT_PROV__LAST |
| 35 | } OPTION_CHOICE; |
| 36 | |
| 37 | const OPTIONS dsaparam_options[] = { |
| 38 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"}, |
| 39 | |
| 40 | OPT_SECTION("General"){ OPT_SECTION_STR, 1, '-', "General" " options:\n" }, |
| 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"){ OPT_SECTION_STR, 1, '-', "Input" " options:\n" }, |
| 47 | {"in", OPT_IN, '<', "Input file"}, |
| 48 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, |
| 49 | |
| 50 | OPT_SECTION("Output"){ OPT_SECTION_STR, 1, '-', "Output" " options:\n" }, |
| 51 | {"out", OPT_OUT, '>', "Output file"}, |
| 52 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, |
| 53 | {"text", OPT_TEXT, '-', "Print as text"}, |
| 54 | {"noout", OPT_NOOUT, '-', "No output"}, |
| 55 | {"verbose", OPT_VERBOSE, '-', "Verbose output"}, |
| 56 | {"genkey", OPT_GENKEY, '-', "Generate a DSA key"}, |
| 57 | |
| 58 | OPT_R_OPTIONS{ OPT_SECTION_STR, 1, '-', "Random state" " options:\n" }, {"rand" , OPT_R_RAND, 's', "Load the given file(s) into the random number generator" }, {"writerand", OPT_R_WRITERAND, '>', "Write random data to the specified file" }, |
| 59 | OPT_PROV_OPTIONS{ OPT_SECTION_STR, 1, '-', "Provider" " options:\n" }, { "provider-path" , OPT_PROV_PROVIDER_PATH, 's', "Provider load path (must be before 'provider' argument if required)" }, { "provider", OPT_PROV_PROVIDER, 's', "Provider to load (can be specified multiple times)" }, { "propquery", OPT_PROV_PROPQUERY, 's', "Property query used when fetching algorithms" }, |
| 60 | |
| 61 | OPT_PARAMETERS(){ OPT_PARAM_STR, 1, '-', "Parameters:\n" }, |
| 62 | {"numbits", 0, 0, "Number of bits if generating parameters (optional)"}, |
| 63 | {NULL((void*)0)} |
| 64 | }; |
| 65 | |
| 66 | int dsaparam_main(int argc, char **argv) |
| 67 | { |
| 68 | ENGINE *e = NULL((void*)0); |
| 69 | BIO *out = NULL((void*)0); |
| 70 | EVP_PKEY *params = NULL((void*)0), *pkey = NULL((void*)0); |
| 71 | EVP_PKEY_CTX *ctx = NULL((void*)0); |
| 72 | int numbits = -1, num = 0, genkey = 0; |
| 73 | int informat = FORMAT_UNDEF0, outformat = FORMAT_PEM(5 | 0x8000), noout = 0; |
| 74 | int ret = 1, i, text = 0, private = 0; |
| 75 | char *infile = NULL((void*)0), *outfile = NULL((void*)0), *prog; |
| 76 | OPTION_CHOICE o; |
| 77 | |
| 78 | prog = opt_init(argc, argv, dsaparam_options); |
| 79 | while ((o = opt_next()) != OPT_EOF) { |
| 80 | switch (o) { |
| 81 | case OPT_EOF: |
| 82 | case OPT_ERR: |
| 83 | opthelp: |
| 84 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 85 | goto end; |
| 86 | case OPT_HELP: |
| 87 | opt_help(dsaparam_options); |
| 88 | ret = 0; |
| 89 | goto end; |
| 90 | case OPT_INFORM: |
| 91 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER(1L << 1), &informat)) |
| 92 | goto opthelp; |
| 93 | break; |
| 94 | case OPT_IN: |
| 95 | infile = opt_arg(); |
| 96 | break; |
| 97 | case OPT_OUTFORM: |
| 98 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER(1L << 1), &outformat)) |
| 99 | goto opthelp; |
| 100 | break; |
| 101 | case OPT_OUT: |
| 102 | outfile = opt_arg(); |
| 103 | break; |
| 104 | case OPT_ENGINE: |
| 105 | e = setup_engine(opt_arg(), 0)setup_engine_methods(opt_arg(), (unsigned int)-1, 0); |
| 106 | break; |
| 107 | case OPT_TEXT: |
| 108 | text = 1; |
| 109 | break; |
| 110 | case OPT_GENKEY: |
| 111 | genkey = 1; |
| 112 | break; |
| 113 | case OPT_R_CASESOPT_R__FIRST: case OPT_R__LAST: break; case OPT_R_RAND: case OPT_R_WRITERAND: |
| 114 | if (!opt_rand(o)) |
| 115 | goto end; |
| 116 | break; |
| 117 | case OPT_PROV_CASESOPT_PROV__FIRST: case OPT_PROV__LAST: break; case OPT_PROV_PROVIDER : case OPT_PROV_PROVIDER_PATH: case OPT_PROV_PROPQUERY: |
| 118 | if (!opt_provider(o)) |
| 119 | goto end; |
| 120 | break; |
| 121 | case OPT_NOOUT: |
| 122 | noout = 1; |
| 123 | break; |
| 124 | case OPT_VERBOSE: |
| 125 | verbose = 1; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* Optional arg is bitsize. */ |
| 131 | argc = opt_num_rest(); |
| 132 | argv = opt_rest(); |
| 133 | if (argc == 1) { |
| 134 | if (!opt_int(argv[0], &num) || num < 0) |
| 135 | goto opthelp; |
| 136 | } else if (argc != 0) { |
| 137 | goto opthelp; |
| 138 | } |
| 139 | if (!app_RAND_load()) |
| 140 | goto end; |
| 141 | |
| 142 | /* generate a key */ |
| 143 | numbits = num; |
| 144 | private = genkey ? 1 : 0; |
| 145 | |
| 146 | out = bio_open_owner(outfile, outformat, private); |
| 147 | if (out == NULL((void*)0)) |
| 148 | goto end; |
| 149 | |
| 150 | ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq()); |
| 151 | if (ctx == NULL((void*)0)) { |
| 152 | BIO_printf(bio_err, |
| 153 | "Error, DSA parameter generation context allocation failed\n"); |
| 154 | goto end; |
| 155 | } |
| 156 | if (numbits > 0) { |
| 157 | if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS10000) |
| 158 | BIO_printf(bio_err, |
| 159 | "Warning: It is not recommended to use more than %d bit for DSA keys.\n" |
| 160 | " Your key size is %d! Larger key size may behave not as expected.\n", |
| 161 | OPENSSL_DSA_MAX_MODULUS_BITS10000, numbits); |
| 162 | |
| 163 | EVP_PKEY_CTX_set_cb(ctx, gendsa_cb); |
| 164 | EVP_PKEY_CTX_set_app_data(ctx, bio_err); |
| 165 | if (verbose) { |
| 166 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", |
| 167 | num); |
| 168 | BIO_printf(bio_err, "This could take some time\n"); |
| 169 | } |
| 170 | if (EVP_PKEY_paramgen_init(ctx) <= 0) { |
| 171 | BIO_printf(bio_err, |
| 172 | "Error, DSA key generation paramgen init failed\n"); |
| 173 | goto end; |
| 174 | } |
| 175 | if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) { |
| 176 | BIO_printf(bio_err, |
| 177 | "Error, DSA key generation setting bit length failed\n"); |
| 178 | goto end; |
| 179 | } |
| 180 | params = app_paramgen(ctx, "DSA"); |
| 181 | } else { |
| 182 | params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters"); |
| 183 | } |
| 184 | if (params == NULL((void*)0)) { |
| 185 | /* Error message should already have been displayed */ |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | if (text) { |
| 190 | EVP_PKEY_print_params(out, params, 0, NULL((void*)0)); |
| 191 | } |
| 192 | |
| 193 | if (outformat == FORMAT_ASN14 && genkey) |
| 194 | noout = 1; |
| 195 | |
| 196 | if (!noout) { |
| 197 | if (outformat == FORMAT_ASN14) |
| 198 | i = i2d_KeyParams_bio(out, params); |
| 199 | else |
| 200 | i = PEM_write_bio_Parameters(out, params); |
| 201 | if (!i) { |
| 202 | BIO_printf(bio_err, "Error, unable to write DSA parameters\n"); |
| 203 | goto end; |
| 204 | } |
| 205 | } |
| 206 | if (genkey) { |
| 207 | EVP_PKEY_CTX_free(ctx); |
| 208 | ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params, |
| 209 | app_get0_propq()); |
| 210 | if (ctx == NULL((void*)0)) { |
| 211 | BIO_printf(bio_err, |
| 212 | "Error, DSA key generation context allocation failed\n"); |
| 213 | goto end; |
| 214 | } |
| 215 | if (EVP_PKEY_keygen_init(ctx) <= 0) { |
| 216 | BIO_printf(bio_err, |
| 217 | "Error, unable to initialise for key generation\n"); |
| 218 | goto end; |
| 219 | } |
| 220 | pkey = app_keygen(ctx, "DSA", numbits, verbose); |
| 221 | assert(private)((void) (0)); |
| 222 | if (outformat == FORMAT_ASN14) |
| 223 | i = i2d_PrivateKey_bio(out, pkey); |
Value stored to 'i' is never read | |
| 224 | else |
| 225 | i = PEM_write_bio_PrivateKey(out, pkey, NULL((void*)0), NULL((void*)0), 0, NULL((void*)0), NULL((void*)0)); |
| 226 | } |
| 227 | ret = 0; |
| 228 | end: |
| 229 | if (ret != 0) |
| 230 | ERR_print_errors(bio_err); |
| 231 | BIO_free_all(out); |
| 232 | EVP_PKEY_CTX_free(ctx); |
| 233 | EVP_PKEY_free(pkey); |
| 234 | EVP_PKEY_free(params); |
| 235 | release_engine(e); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | static int gendsa_cb(EVP_PKEY_CTX *ctx) |
| 240 | { |
| 241 | static const char symbols[] = ".+*\n"; |
| 242 | int p; |
| 243 | char c; |
| 244 | BIO *b; |
| 245 | |
| 246 | if (!verbose) |
| 247 | return 1; |
| 248 | |
| 249 | b = EVP_PKEY_CTX_get_app_data(ctx); |
| 250 | p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); |
| 251 | c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?'; |
| 252 | |
| 253 | BIO_write(b, &c, 1); |
| 254 | (void)BIO_flush(b)(int)BIO_ctrl(b,11,0,((void*)0)); |
| 255 | return 1; |
| 256 | } |