1 /* $OpenBSD: dsaparam.c,v 1.16 2025/01/19 10:24:17 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */ 60 61 /* Until the key-gen callbacks are modified to use newer prototypes, we allow 62 * deprecated functions for openssl-internal code */ 63 #ifdef OPENSSL_NO_DEPRECATED 64 #undef OPENSSL_NO_DEPRECATED 65 #endif 66 67 #include <limits.h> 68 #include <stdio.h> 69 #include <stdlib.h> 70 #include <string.h> 71 #include <time.h> 72 73 #include "apps.h" 74 75 #include <openssl/bio.h> 76 #include <openssl/bn.h> 77 #include <openssl/err.h> 78 #include <openssl/dsa.h> 79 #include <openssl/pem.h> 80 #include <openssl/x509.h> 81 82 static struct { 83 int genkey; 84 char *infile; 85 int informat; 86 int noout; 87 char *outfile; 88 int outformat; 89 int text; 90 } cfg; 91 92 static const struct option dsaparam_options[] = { 93 { 94 .name = "genkey", 95 .desc = "Generate a DSA key", 96 .type = OPTION_FLAG, 97 .opt.flag = &cfg.genkey, 98 }, 99 { 100 .name = "in", 101 .argname = "file", 102 .desc = "Input file (default stdin)", 103 .type = OPTION_ARG, 104 .opt.arg = &cfg.infile, 105 }, 106 { 107 .name = "inform", 108 .argname = "format", 109 .desc = "Input format (DER or PEM (default))", 110 .type = OPTION_ARG_FORMAT, 111 .opt.value = &cfg.informat, 112 }, 113 { 114 .name = "noout", 115 .desc = "No output", 116 .type = OPTION_FLAG, 117 .opt.flag = &cfg.noout, 118 }, 119 { 120 .name = "out", 121 .argname = "file", 122 .desc = "Output file (default stdout)", 123 .type = OPTION_ARG, 124 .opt.arg = &cfg.outfile, 125 }, 126 { 127 .name = "outform", 128 .argname = "format", 129 .desc = "Output format (DER or PEM (default))", 130 .type = OPTION_ARG_FORMAT, 131 .opt.value = &cfg.outformat, 132 }, 133 { 134 .name = "text", 135 .desc = "Print as text", 136 .type = OPTION_FLAG, 137 .opt.flag = &cfg.text, 138 }, 139 { NULL }, 140 }; 141 142 static void 143 dsaparam_usage(void) 144 { 145 fprintf(stderr, 146 "usage: dsaparam [-genkey] [-in file]\n" 147 " [-inform format] [-noout] [-out file] [-outform format]\n" 148 " [-text] [numbits]\n\n"); 149 options_usage(dsaparam_options); 150 } 151 152 static int dsa_cb(int p, int n, BN_GENCB *cb); 153 154 int 155 dsaparam_main(int argc, char **argv) 156 { 157 DSA *dsa = NULL; 158 int i; 159 BIO *in = NULL, *out = NULL; 160 BN_GENCB *cb = NULL; 161 int ret = 1; 162 int numbits = -1; 163 char *strbits = NULL; 164 165 if (pledge("stdio cpath wpath rpath", NULL) == -1) { 166 perror("pledge"); 167 exit(1); 168 } 169 170 memset(&cfg, 0, sizeof(cfg)); 171 172 cfg.informat = FORMAT_PEM; 173 cfg.outformat = FORMAT_PEM; 174 175 if (options_parse(argc, argv, dsaparam_options, &strbits, NULL) != 0) { 176 dsaparam_usage(); 177 goto end; 178 } 179 180 if (strbits != NULL) { 181 const char *errstr; 182 numbits = strtonum(strbits, 0, INT_MAX, &errstr); 183 if (errstr) { 184 fprintf(stderr, "Invalid number of bits: %s", errstr); 185 goto end; 186 } 187 } 188 189 in = BIO_new(BIO_s_file()); 190 out = BIO_new(BIO_s_file()); 191 if (in == NULL || out == NULL) { 192 ERR_print_errors(bio_err); 193 goto end; 194 } 195 if (cfg.infile == NULL) 196 BIO_set_fp(in, stdin, BIO_NOCLOSE); 197 else { 198 if (BIO_read_filename(in, cfg.infile) <= 0) { 199 perror(cfg.infile); 200 goto end; 201 } 202 } 203 if (cfg.outfile == NULL) { 204 BIO_set_fp(out, stdout, BIO_NOCLOSE); 205 } else { 206 if (BIO_write_filename(out, cfg.outfile) <= 0) { 207 perror(cfg.outfile); 208 goto end; 209 } 210 } 211 212 if (numbits > 0) { 213 if ((cb = BN_GENCB_new()) == NULL) { 214 BIO_printf(bio_err, 215 "Error allocating BN_GENCB object\n"); 216 goto end; 217 } 218 219 BN_GENCB_set(cb, dsa_cb, bio_err); 220 221 dsa = DSA_new(); 222 if (!dsa) { 223 BIO_printf(bio_err, "Error allocating DSA object\n"); 224 goto end; 225 } 226 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", numbits); 227 BIO_printf(bio_err, "This could take some time\n"); 228 if (!DSA_generate_parameters_ex(dsa, numbits, NULL, 0, NULL, NULL, cb)) { 229 ERR_print_errors(bio_err); 230 BIO_printf(bio_err, "Error, DSA key generation failed\n"); 231 goto end; 232 } 233 } else if (cfg.informat == FORMAT_ASN1) 234 dsa = d2i_DSAparams_bio(in, NULL); 235 else if (cfg.informat == FORMAT_PEM) 236 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); 237 else { 238 BIO_printf(bio_err, "bad input format specified\n"); 239 goto end; 240 } 241 if (dsa == NULL) { 242 BIO_printf(bio_err, "unable to load DSA parameters\n"); 243 ERR_print_errors(bio_err); 244 goto end; 245 } 246 if (cfg.text) { 247 DSAparams_print(out, dsa); 248 } 249 if (!cfg.noout) { 250 if (cfg.outformat == FORMAT_ASN1) 251 i = i2d_DSAparams_bio(out, dsa); 252 else if (cfg.outformat == FORMAT_PEM) 253 i = PEM_write_bio_DSAparams(out, dsa); 254 else { 255 BIO_printf(bio_err, "bad output format specified for outfile\n"); 256 goto end; 257 } 258 if (!i) { 259 BIO_printf(bio_err, "unable to write DSA parameters\n"); 260 ERR_print_errors(bio_err); 261 goto end; 262 } 263 } 264 if (cfg.genkey) { 265 DSA *dsakey; 266 267 if ((dsakey = DSAparams_dup(dsa)) == NULL) 268 goto end; 269 if (!DSA_generate_key(dsakey)) { 270 ERR_print_errors(bio_err); 271 DSA_free(dsakey); 272 goto end; 273 } 274 if (cfg.outformat == FORMAT_ASN1) 275 i = i2d_DSAPrivateKey_bio(out, dsakey); 276 else if (cfg.outformat == FORMAT_PEM) 277 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, NULL); 278 else { 279 BIO_printf(bio_err, "bad output format specified for outfile\n"); 280 DSA_free(dsakey); 281 goto end; 282 } 283 DSA_free(dsakey); 284 } 285 ret = 0; 286 287 end: 288 BIO_free(in); 289 BIO_free_all(out); 290 BN_GENCB_free(cb); 291 DSA_free(dsa); 292 293 return (ret); 294 } 295 296 static int 297 dsa_cb(int p, int n, BN_GENCB *cb) 298 { 299 char c = '*'; 300 301 if (p == 0) 302 c = '.'; 303 if (p == 1) 304 c = '+'; 305 if (p == 2) 306 c = '*'; 307 if (p == 3) 308 c = '\n'; 309 BIO_write(BN_GENCB_get_arg(cb), &c, 1); 310 (void) BIO_flush(BN_GENCB_get_arg(cb)); 311 #ifdef GENCB_TEST 312 if (stop_keygen_flag) 313 return 0; 314 #endif 315 return 1; 316 } 317