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