1 /* $OpenBSD: dsa.c,v 1.13 2019/02/09 15:49:21 inoguchi 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 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 = "noout", 126 .desc = "No output", 127 .type = OPTION_FLAG, 128 .opt.flag = &dsa_config.noout, 129 }, 130 { 131 .name = "out", 132 .argname = "file", 133 .desc = "Output file (default stdout)", 134 .type = OPTION_ARG, 135 .opt.arg = &dsa_config.outfile, 136 }, 137 { 138 .name = "outform", 139 .argname = "format", 140 .desc = "Output format (DER, MSBLOB, PEM (default) or PVK)", 141 .type = OPTION_ARG_FORMAT, 142 .opt.value = &dsa_config.outformat, 143 }, 144 { 145 .name = "passin", 146 .argname = "source", 147 .desc = "Input file passphrase source", 148 .type = OPTION_ARG, 149 .opt.arg = &dsa_config.passargin, 150 }, 151 { 152 .name = "passout", 153 .argname = "source", 154 .desc = "Output file passphrase source", 155 .type = OPTION_ARG, 156 .opt.arg = &dsa_config.passargout, 157 }, 158 { 159 .name = "pubin", 160 .desc = "Read a public key from the input file instead of" 161 " private key", 162 .type = OPTION_FLAG, 163 .opt.flag = &dsa_config.pubin, 164 }, 165 { 166 .name = "pubout", 167 .desc = "Output a public key instead of private key", 168 .type = OPTION_FLAG, 169 .opt.flag = &dsa_config.pubout, 170 }, 171 { 172 .name = "pvk-none", 173 .desc = "PVK encryption level", 174 .type = OPTION_VALUE, 175 .value = 0, 176 .opt.value = &dsa_config.pvk_encr, 177 }, 178 { 179 .name = "pvk-strong", 180 .desc = "PVK encryption level (default)", 181 .type = OPTION_VALUE, 182 .value = 2, 183 .opt.value = &dsa_config.pvk_encr, 184 }, 185 { 186 .name = "pvk-weak", 187 .desc = "PVK encryption level", 188 .type = OPTION_VALUE, 189 .value = 1, 190 .opt.value = &dsa_config.pvk_encr, 191 }, 192 { 193 .name = "text", 194 .desc = "Print the key in text form", 195 .type = OPTION_FLAG, 196 .opt.flag = &dsa_config.text, 197 }, 198 { 199 .name = NULL, 200 .type = OPTION_ARGV_FUNC, 201 .opt.argvfunc = dsa_opt_enc, 202 }, 203 { NULL }, 204 }; 205 206 static void 207 dsa_usage(void) 208 { 209 int n = 0; 210 211 fprintf(stderr, 212 "usage: dsa [-in file] [-inform format] [-noout]\n" 213 " [-out file] [-outform format] [-passin src] [-passout src]\n" 214 " [-pubin] [-pubout] [-pvk-none | -pvk-strong | -pvk-weak]\n" 215 " [-text] [-ciphername]\n\n"); 216 options_usage(dsa_options); 217 fprintf(stderr, "\n"); 218 219 fprintf(stderr, "Valid ciphername values:\n\n"); 220 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_cipher, &n); 221 fprintf(stderr, "\n"); 222 } 223 224 int 225 dsa_main(int argc, char **argv) 226 { 227 int ret = 1; 228 DSA *dsa = NULL; 229 int i; 230 BIO *in = NULL, *out = NULL; 231 char *passin = NULL, *passout = NULL; 232 233 if (single_execution) { 234 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) { 235 perror("pledge"); 236 exit(1); 237 } 238 } 239 240 memset(&dsa_config, 0, sizeof(dsa_config)); 241 242 dsa_config.pvk_encr = 2; 243 dsa_config.informat = FORMAT_PEM; 244 dsa_config.outformat = FORMAT_PEM; 245 246 if (options_parse(argc, argv, dsa_options, NULL, NULL) != 0) { 247 dsa_usage(); 248 goto end; 249 } 250 251 if (!app_passwd(bio_err, dsa_config.passargin, dsa_config.passargout, 252 &passin, &passout)) { 253 BIO_printf(bio_err, "Error getting passwords\n"); 254 goto end; 255 } 256 257 in = BIO_new(BIO_s_file()); 258 out = BIO_new(BIO_s_file()); 259 if (in == NULL || out == NULL) { 260 ERR_print_errors(bio_err); 261 goto end; 262 } 263 if (dsa_config.infile == NULL) 264 BIO_set_fp(in, stdin, BIO_NOCLOSE); 265 else { 266 if (BIO_read_filename(in, dsa_config.infile) <= 0) { 267 perror(dsa_config.infile); 268 goto end; 269 } 270 } 271 272 BIO_printf(bio_err, "read DSA key\n"); 273 274 { 275 EVP_PKEY *pkey; 276 277 if (dsa_config.pubin) 278 pkey = load_pubkey(bio_err, dsa_config.infile, 279 dsa_config.informat, 1, passin, "Public Key"); 280 else 281 pkey = load_key(bio_err, dsa_config.infile, 282 dsa_config.informat, 1, passin, "Private Key"); 283 284 if (pkey) { 285 dsa = EVP_PKEY_get1_DSA(pkey); 286 EVP_PKEY_free(pkey); 287 } 288 } 289 if (dsa == NULL) { 290 BIO_printf(bio_err, "unable to load Key\n"); 291 ERR_print_errors(bio_err); 292 goto end; 293 } 294 if (dsa_config.outfile == NULL) { 295 BIO_set_fp(out, stdout, BIO_NOCLOSE); 296 } else { 297 if (BIO_write_filename(out, dsa_config.outfile) <= 0) { 298 perror(dsa_config.outfile); 299 goto end; 300 } 301 } 302 303 if (dsa_config.text) { 304 if (!DSA_print(out, dsa, 0)) { 305 perror(dsa_config.outfile); 306 ERR_print_errors(bio_err); 307 goto end; 308 } 309 } 310 if (dsa_config.modulus) { 311 fprintf(stdout, "Public Key="); 312 BN_print(out, dsa->pub_key); 313 fprintf(stdout, "\n"); 314 } 315 if (dsa_config.noout) 316 goto end; 317 BIO_printf(bio_err, "writing DSA key\n"); 318 if (dsa_config.outformat == FORMAT_ASN1) { 319 if (dsa_config.pubin || dsa_config.pubout) 320 i = i2d_DSA_PUBKEY_bio(out, dsa); 321 else 322 i = i2d_DSAPrivateKey_bio(out, dsa); 323 } else if (dsa_config.outformat == FORMAT_PEM) { 324 if (dsa_config.pubin || dsa_config.pubout) 325 i = PEM_write_bio_DSA_PUBKEY(out, dsa); 326 else 327 i = PEM_write_bio_DSAPrivateKey(out, dsa, dsa_config.enc, 328 NULL, 0, NULL, passout); 329 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4) 330 } else if (dsa_config.outformat == FORMAT_MSBLOB || 331 dsa_config.outformat == FORMAT_PVK) { 332 EVP_PKEY *pk; 333 pk = EVP_PKEY_new(); 334 EVP_PKEY_set1_DSA(pk, dsa); 335 if (dsa_config.outformat == FORMAT_PVK) 336 i = i2b_PVK_bio(out, pk, dsa_config.pvk_encr, 0, 337 passout); 338 else if (dsa_config.pubin || dsa_config.pubout) 339 i = i2b_PublicKey_bio(out, pk); 340 else 341 i = i2b_PrivateKey_bio(out, pk); 342 EVP_PKEY_free(pk); 343 #endif 344 } else { 345 BIO_printf(bio_err, "bad output format specified for outfile\n"); 346 goto end; 347 } 348 if (i <= 0) { 349 BIO_printf(bio_err, "unable to write private key\n"); 350 ERR_print_errors(bio_err); 351 } else 352 ret = 0; 353 end: 354 BIO_free(in); 355 BIO_free_all(out); 356 DSA_free(dsa); 357 free(passin); 358 free(passout); 359 360 return (ret); 361 } 362