1 /* $OpenBSD: dsa.c,v 1.17 2022/11/11 17:07:38 joshua 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 #include <stdio.h> 62 #include <stdlib.h> 63 #include <time.h> 64 #include <string.h> 65 66 #include "apps.h" 67 68 #include <openssl/bio.h> 69 #include <openssl/bn.h> 70 #include <openssl/dsa.h> 71 #include <openssl/err.h> 72 #include <openssl/evp.h> 73 #include <openssl/pem.h> 74 #include <openssl/x509.h> 75 76 static struct { 77 const EVP_CIPHER *enc; 78 char *infile; 79 int informat; 80 int modulus; 81 int noout; 82 char *outfile; 83 int outformat; 84 char *passargin; 85 char *passargout; 86 int pubin; 87 int pubout; 88 int pvk_encr; 89 int text; 90 } dsa_config; 91 92 static int 93 dsa_opt_enc(int argc, char **argv, int *argsused) 94 { 95 char *name = argv[0]; 96 97 if (*name++ != '-') 98 return (1); 99 100 if ((dsa_config.enc = EVP_get_cipherbyname(name)) != NULL) { 101 *argsused = 1; 102 return (0); 103 } 104 105 return (1); 106 } 107 108 static const struct option dsa_options[] = { 109 { 110 .name = "in", 111 .argname = "file", 112 .desc = "Input file (default stdin)", 113 .type = OPTION_ARG, 114 .opt.arg = &dsa_config.infile, 115 }, 116 { 117 .name = "inform", 118 .argname = "format", 119 .desc = "Input format (PEM (default) or any other supported" 120 " format)", 121 .type = OPTION_ARG_FORMAT, 122 .opt.value = &dsa_config.informat, 123 }, 124 { 125 .name = "modulus", 126 .desc = "Print the DSA public value", 127 .type = OPTION_FLAG, 128 .opt.flag = &dsa_config.modulus, 129 }, 130 { 131 .name = "noout", 132 .desc = "No output", 133 .type = OPTION_FLAG, 134 .opt.flag = &dsa_config.noout, 135 }, 136 { 137 .name = "out", 138 .argname = "file", 139 .desc = "Output file (default stdout)", 140 .type = OPTION_ARG, 141 .opt.arg = &dsa_config.outfile, 142 }, 143 { 144 .name = "outform", 145 .argname = "format", 146 .desc = "Output format (DER, MSBLOB, PEM (default) or PVK)", 147 .type = OPTION_ARG_FORMAT, 148 .opt.value = &dsa_config.outformat, 149 }, 150 { 151 .name = "passin", 152 .argname = "source", 153 .desc = "Input file passphrase source", 154 .type = OPTION_ARG, 155 .opt.arg = &dsa_config.passargin, 156 }, 157 { 158 .name = "passout", 159 .argname = "source", 160 .desc = "Output file passphrase source", 161 .type = OPTION_ARG, 162 .opt.arg = &dsa_config.passargout, 163 }, 164 { 165 .name = "pubin", 166 .desc = "Read a public key from the input file instead of" 167 " private key", 168 .type = OPTION_FLAG, 169 .opt.flag = &dsa_config.pubin, 170 }, 171 { 172 .name = "pubout", 173 .desc = "Output a public key instead of private key", 174 .type = OPTION_FLAG, 175 .opt.flag = &dsa_config.pubout, 176 }, 177 { 178 .name = "pvk-none", 179 .desc = "PVK encryption level", 180 .type = OPTION_VALUE, 181 .value = 0, 182 .opt.value = &dsa_config.pvk_encr, 183 }, 184 { 185 .name = "pvk-strong", 186 .desc = "PVK encryption level (default)", 187 .type = OPTION_VALUE, 188 .value = 2, 189 .opt.value = &dsa_config.pvk_encr, 190 }, 191 { 192 .name = "pvk-weak", 193 .desc = "PVK encryption level", 194 .type = OPTION_VALUE, 195 .value = 1, 196 .opt.value = &dsa_config.pvk_encr, 197 }, 198 { 199 .name = "text", 200 .desc = "Print the key in text form", 201 .type = OPTION_FLAG, 202 .opt.flag = &dsa_config.text, 203 }, 204 { 205 .name = NULL, 206 .type = OPTION_ARGV_FUNC, 207 .opt.argvfunc = dsa_opt_enc, 208 }, 209 { NULL }, 210 }; 211 212 static void 213 dsa_usage(void) 214 { 215 int n = 0; 216 217 fprintf(stderr, 218 "usage: dsa [-in file] [-inform format] [-modulus] [-noout]\n" 219 " [-out file] [-outform format] [-passin src] [-passout src]\n" 220 " [-pubin] [-pubout] [-pvk-none | -pvk-strong | -pvk-weak]\n" 221 " [-text] [-ciphername]\n\n"); 222 options_usage(dsa_options); 223 fprintf(stderr, "\n"); 224 225 fprintf(stderr, "Valid ciphername values:\n\n"); 226 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_cipher, &n); 227 fprintf(stderr, "\n"); 228 } 229 230 int 231 dsa_main(int argc, char **argv) 232 { 233 int ret = 1; 234 DSA *dsa = NULL; 235 int i; 236 BIO *in = NULL, *out = NULL; 237 char *passin = NULL, *passout = NULL; 238 239 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) { 240 perror("pledge"); 241 exit(1); 242 } 243 244 memset(&dsa_config, 0, sizeof(dsa_config)); 245 246 dsa_config.pvk_encr = 2; 247 dsa_config.informat = FORMAT_PEM; 248 dsa_config.outformat = FORMAT_PEM; 249 250 if (options_parse(argc, argv, dsa_options, NULL, NULL) != 0) { 251 dsa_usage(); 252 goto end; 253 } 254 255 if (!app_passwd(bio_err, dsa_config.passargin, dsa_config.passargout, 256 &passin, &passout)) { 257 BIO_printf(bio_err, "Error getting passwords\n"); 258 goto end; 259 } 260 261 in = BIO_new(BIO_s_file()); 262 out = BIO_new(BIO_s_file()); 263 if (in == NULL || out == NULL) { 264 ERR_print_errors(bio_err); 265 goto end; 266 } 267 if (dsa_config.infile == NULL) 268 BIO_set_fp(in, stdin, BIO_NOCLOSE); 269 else { 270 if (BIO_read_filename(in, dsa_config.infile) <= 0) { 271 perror(dsa_config.infile); 272 goto end; 273 } 274 } 275 276 BIO_printf(bio_err, "read DSA key\n"); 277 278 { 279 EVP_PKEY *pkey; 280 281 if (dsa_config.pubin) 282 pkey = load_pubkey(bio_err, dsa_config.infile, 283 dsa_config.informat, 1, passin, "Public Key"); 284 else 285 pkey = load_key(bio_err, dsa_config.infile, 286 dsa_config.informat, 1, passin, "Private Key"); 287 288 if (pkey) { 289 dsa = EVP_PKEY_get1_DSA(pkey); 290 EVP_PKEY_free(pkey); 291 } 292 } 293 if (dsa == NULL) { 294 BIO_printf(bio_err, "unable to load Key\n"); 295 ERR_print_errors(bio_err); 296 goto end; 297 } 298 if (dsa_config.outfile == NULL) { 299 BIO_set_fp(out, stdout, BIO_NOCLOSE); 300 } else { 301 if (BIO_write_filename(out, dsa_config.outfile) <= 0) { 302 perror(dsa_config.outfile); 303 goto end; 304 } 305 } 306 307 if (dsa_config.text) { 308 if (!DSA_print(out, dsa, 0)) { 309 perror(dsa_config.outfile); 310 ERR_print_errors(bio_err); 311 goto end; 312 } 313 } 314 if (dsa_config.modulus) { 315 fprintf(stdout, "Public Key="); 316 BN_print(out, DSA_get0_pub_key(dsa)); 317 fprintf(stdout, "\n"); 318 } 319 if (dsa_config.noout) 320 goto end; 321 BIO_printf(bio_err, "writing DSA key\n"); 322 if (dsa_config.outformat == FORMAT_ASN1) { 323 if (dsa_config.pubin || dsa_config.pubout) 324 i = i2d_DSA_PUBKEY_bio(out, dsa); 325 else 326 i = i2d_DSAPrivateKey_bio(out, dsa); 327 } else if (dsa_config.outformat == FORMAT_PEM) { 328 if (dsa_config.pubin || dsa_config.pubout) 329 i = PEM_write_bio_DSA_PUBKEY(out, dsa); 330 else 331 i = PEM_write_bio_DSAPrivateKey(out, dsa, dsa_config.enc, 332 NULL, 0, NULL, passout); 333 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4) 334 } else if (dsa_config.outformat == FORMAT_MSBLOB || 335 dsa_config.outformat == FORMAT_PVK) { 336 EVP_PKEY *pk; 337 pk = EVP_PKEY_new(); 338 EVP_PKEY_set1_DSA(pk, dsa); 339 if (dsa_config.outformat == FORMAT_PVK) 340 i = i2b_PVK_bio(out, pk, dsa_config.pvk_encr, 0, 341 passout); 342 else if (dsa_config.pubin || dsa_config.pubout) 343 i = i2b_PublicKey_bio(out, pk); 344 else 345 i = i2b_PrivateKey_bio(out, pk); 346 EVP_PKEY_free(pk); 347 #endif 348 } else { 349 BIO_printf(bio_err, "bad output format specified for outfile\n"); 350 goto end; 351 } 352 if (i <= 0) { 353 BIO_printf(bio_err, "unable to write private key\n"); 354 ERR_print_errors(bio_err); 355 } else 356 ret = 0; 357 end: 358 BIO_free(in); 359 BIO_free_all(out); 360 DSA_free(dsa); 361 free(passin); 362 free(passout); 363 364 return (ret); 365 } 366