1 /* $OpenBSD: dsa.c,v 1.15 2019/07/14 03:30:45 guenther 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 (single_execution) { 240 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) { 241 perror("pledge"); 242 exit(1); 243 } 244 } 245 246 memset(&dsa_config, 0, sizeof(dsa_config)); 247 248 dsa_config.pvk_encr = 2; 249 dsa_config.informat = FORMAT_PEM; 250 dsa_config.outformat = FORMAT_PEM; 251 252 if (options_parse(argc, argv, dsa_options, NULL, NULL) != 0) { 253 dsa_usage(); 254 goto end; 255 } 256 257 if (!app_passwd(bio_err, dsa_config.passargin, dsa_config.passargout, 258 &passin, &passout)) { 259 BIO_printf(bio_err, "Error getting passwords\n"); 260 goto end; 261 } 262 263 in = BIO_new(BIO_s_file()); 264 out = BIO_new(BIO_s_file()); 265 if (in == NULL || out == NULL) { 266 ERR_print_errors(bio_err); 267 goto end; 268 } 269 if (dsa_config.infile == NULL) 270 BIO_set_fp(in, stdin, BIO_NOCLOSE); 271 else { 272 if (BIO_read_filename(in, dsa_config.infile) <= 0) { 273 perror(dsa_config.infile); 274 goto end; 275 } 276 } 277 278 BIO_printf(bio_err, "read DSA key\n"); 279 280 { 281 EVP_PKEY *pkey; 282 283 if (dsa_config.pubin) 284 pkey = load_pubkey(bio_err, dsa_config.infile, 285 dsa_config.informat, 1, passin, "Public Key"); 286 else 287 pkey = load_key(bio_err, dsa_config.infile, 288 dsa_config.informat, 1, passin, "Private Key"); 289 290 if (pkey) { 291 dsa = EVP_PKEY_get1_DSA(pkey); 292 EVP_PKEY_free(pkey); 293 } 294 } 295 if (dsa == NULL) { 296 BIO_printf(bio_err, "unable to load Key\n"); 297 ERR_print_errors(bio_err); 298 goto end; 299 } 300 if (dsa_config.outfile == NULL) { 301 BIO_set_fp(out, stdout, BIO_NOCLOSE); 302 } else { 303 if (BIO_write_filename(out, dsa_config.outfile) <= 0) { 304 perror(dsa_config.outfile); 305 goto end; 306 } 307 } 308 309 if (dsa_config.text) { 310 if (!DSA_print(out, dsa, 0)) { 311 perror(dsa_config.outfile); 312 ERR_print_errors(bio_err); 313 goto end; 314 } 315 } 316 if (dsa_config.modulus) { 317 fprintf(stdout, "Public Key="); 318 BN_print(out, dsa->pub_key); 319 fprintf(stdout, "\n"); 320 } 321 if (dsa_config.noout) 322 goto end; 323 BIO_printf(bio_err, "writing DSA key\n"); 324 if (dsa_config.outformat == FORMAT_ASN1) { 325 if (dsa_config.pubin || dsa_config.pubout) 326 i = i2d_DSA_PUBKEY_bio(out, dsa); 327 else 328 i = i2d_DSAPrivateKey_bio(out, dsa); 329 } else if (dsa_config.outformat == FORMAT_PEM) { 330 if (dsa_config.pubin || dsa_config.pubout) 331 i = PEM_write_bio_DSA_PUBKEY(out, dsa); 332 else 333 i = PEM_write_bio_DSAPrivateKey(out, dsa, dsa_config.enc, 334 NULL, 0, NULL, passout); 335 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4) 336 } else if (dsa_config.outformat == FORMAT_MSBLOB || 337 dsa_config.outformat == FORMAT_PVK) { 338 EVP_PKEY *pk; 339 pk = EVP_PKEY_new(); 340 EVP_PKEY_set1_DSA(pk, dsa); 341 if (dsa_config.outformat == FORMAT_PVK) 342 i = i2b_PVK_bio(out, pk, dsa_config.pvk_encr, 0, 343 passout); 344 else if (dsa_config.pubin || dsa_config.pubout) 345 i = i2b_PublicKey_bio(out, pk); 346 else 347 i = i2b_PrivateKey_bio(out, pk); 348 EVP_PKEY_free(pk); 349 #endif 350 } else { 351 BIO_printf(bio_err, "bad output format specified for outfile\n"); 352 goto end; 353 } 354 if (i <= 0) { 355 BIO_printf(bio_err, "unable to write private key\n"); 356 ERR_print_errors(bio_err); 357 } else 358 ret = 0; 359 end: 360 BIO_free(in); 361 BIO_free_all(out); 362 DSA_free(dsa); 363 free(passin); 364 free(passout); 365 366 return (ret); 367 } 368