1 /* 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <openssl/opensslconf.h> 11 #ifdef OPENSSL_NO_DSA 12 NON_EMPTY_TRANSLATION_UNIT 13 #else 14 15 # include <stdio.h> 16 # include <stdlib.h> 17 # include <string.h> 18 # include <time.h> 19 # include "apps.h" 20 # include "progs.h" 21 # include <openssl/bio.h> 22 # include <openssl/err.h> 23 # include <openssl/dsa.h> 24 # include <openssl/evp.h> 25 # include <openssl/x509.h> 26 # include <openssl/pem.h> 27 # include <openssl/bn.h> 28 29 typedef enum OPTION_choice { 30 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, 31 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENGINE, 32 /* Do not change the order here; see case statements below */ 33 OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG, 34 OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_PUBIN, 35 OPT_PUBOUT, OPT_CIPHER, OPT_PASSIN, OPT_PASSOUT 36 } OPTION_CHOICE; 37 38 const OPTIONS dsa_options[] = { 39 {"help", OPT_HELP, '-', "Display this summary"}, 40 {"inform", OPT_INFORM, 'f', "Input format, DER PEM PVK"}, 41 {"outform", OPT_OUTFORM, 'f', "Output format, DER PEM PVK"}, 42 {"in", OPT_IN, 's', "Input key"}, 43 {"out", OPT_OUT, '>', "Output file"}, 44 {"noout", OPT_NOOUT, '-', "Don't print key out"}, 45 {"text", OPT_TEXT, '-', "Print the key in text"}, 46 {"modulus", OPT_MODULUS, '-', "Print the DSA public value"}, 47 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"}, 48 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"}, 49 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, 50 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, 51 {"", OPT_CIPHER, '-', "Any supported cipher"}, 52 # ifndef OPENSSL_NO_RC4 53 {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"}, 54 {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"}, 55 {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"}, 56 # endif 57 # ifndef OPENSSL_NO_ENGINE 58 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, 59 # endif 60 {NULL} 61 }; 62 63 int dsa_main(int argc, char **argv) 64 { 65 BIO *out = NULL; 66 DSA *dsa = NULL; 67 ENGINE *e = NULL; 68 const EVP_CIPHER *enc = NULL; 69 char *infile = NULL, *outfile = NULL, *prog; 70 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL; 71 OPTION_CHOICE o; 72 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0; 73 int i, modulus = 0, pubin = 0, pubout = 0, ret = 1; 74 # ifndef OPENSSL_NO_RC4 75 int pvk_encr = 2; 76 # endif 77 int private = 0; 78 79 prog = opt_init(argc, argv, dsa_options); 80 while ((o = opt_next()) != OPT_EOF) { 81 switch (o) { 82 case OPT_EOF: 83 case OPT_ERR: 84 opthelp: 85 ret = 0; 86 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 87 goto end; 88 case OPT_HELP: 89 opt_help(dsa_options); 90 ret = 0; 91 goto end; 92 case OPT_INFORM: 93 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) 94 goto opthelp; 95 break; 96 case OPT_IN: 97 infile = opt_arg(); 98 break; 99 case OPT_OUTFORM: 100 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat)) 101 goto opthelp; 102 break; 103 case OPT_OUT: 104 outfile = opt_arg(); 105 break; 106 case OPT_ENGINE: 107 e = setup_engine(opt_arg(), 0); 108 break; 109 case OPT_PASSIN: 110 passinarg = opt_arg(); 111 break; 112 case OPT_PASSOUT: 113 passoutarg = opt_arg(); 114 break; 115 case OPT_PVK_STRONG: /* pvk_encr:= 2 */ 116 case OPT_PVK_WEAK: /* pvk_encr:= 1 */ 117 case OPT_PVK_NONE: /* pvk_encr:= 0 */ 118 #ifndef OPENSSL_NO_RC4 119 pvk_encr = (o - OPT_PVK_NONE); 120 #endif 121 break; 122 case OPT_NOOUT: 123 noout = 1; 124 break; 125 case OPT_TEXT: 126 text = 1; 127 break; 128 case OPT_MODULUS: 129 modulus = 1; 130 break; 131 case OPT_PUBIN: 132 pubin = 1; 133 break; 134 case OPT_PUBOUT: 135 pubout = 1; 136 break; 137 case OPT_CIPHER: 138 if (!opt_cipher(opt_unknown(), &enc)) 139 goto end; 140 break; 141 } 142 } 143 argc = opt_num_rest(); 144 if (argc != 0) 145 goto opthelp; 146 147 private = pubin || pubout ? 0 : 1; 148 if (text && !pubin) 149 private = 1; 150 151 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { 152 BIO_printf(bio_err, "Error getting passwords\n"); 153 goto end; 154 } 155 156 BIO_printf(bio_err, "read DSA key\n"); 157 { 158 EVP_PKEY *pkey; 159 160 if (pubin) 161 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key"); 162 else 163 pkey = load_key(infile, informat, 1, passin, e, "Private Key"); 164 165 if (pkey != NULL) { 166 dsa = EVP_PKEY_get1_DSA(pkey); 167 EVP_PKEY_free(pkey); 168 } 169 } 170 if (dsa == NULL) { 171 BIO_printf(bio_err, "unable to load Key\n"); 172 ERR_print_errors(bio_err); 173 goto end; 174 } 175 176 out = bio_open_owner(outfile, outformat, private); 177 if (out == NULL) 178 goto end; 179 180 if (text) { 181 assert(pubin || private); 182 if (!DSA_print(out, dsa, 0)) { 183 perror(outfile); 184 ERR_print_errors(bio_err); 185 goto end; 186 } 187 } 188 189 if (modulus) { 190 const BIGNUM *pub_key = NULL; 191 DSA_get0_key(dsa, &pub_key, NULL); 192 BIO_printf(out, "Public Key="); 193 BN_print(out, pub_key); 194 BIO_printf(out, "\n"); 195 } 196 197 if (noout) { 198 ret = 0; 199 goto end; 200 } 201 BIO_printf(bio_err, "writing DSA key\n"); 202 if (outformat == FORMAT_ASN1) { 203 if (pubin || pubout) { 204 i = i2d_DSA_PUBKEY_bio(out, dsa); 205 } else { 206 assert(private); 207 i = i2d_DSAPrivateKey_bio(out, dsa); 208 } 209 } else if (outformat == FORMAT_PEM) { 210 if (pubin || pubout) { 211 i = PEM_write_bio_DSA_PUBKEY(out, dsa); 212 } else { 213 assert(private); 214 i = PEM_write_bio_DSAPrivateKey(out, dsa, enc, 215 NULL, 0, NULL, passout); 216 } 217 # ifndef OPENSSL_NO_RSA 218 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) { 219 EVP_PKEY *pk; 220 pk = EVP_PKEY_new(); 221 if (pk == NULL) 222 goto end; 223 224 EVP_PKEY_set1_DSA(pk, dsa); 225 if (outformat == FORMAT_PVK) { 226 if (pubin) { 227 BIO_printf(bio_err, "PVK form impossible with public key input\n"); 228 EVP_PKEY_free(pk); 229 goto end; 230 } 231 assert(private); 232 # ifdef OPENSSL_NO_RC4 233 BIO_printf(bio_err, "PVK format not supported\n"); 234 EVP_PKEY_free(pk); 235 goto end; 236 # else 237 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout); 238 # endif 239 } else if (pubin || pubout) { 240 i = i2b_PublicKey_bio(out, pk); 241 } else { 242 assert(private); 243 i = i2b_PrivateKey_bio(out, pk); 244 } 245 EVP_PKEY_free(pk); 246 # endif 247 } else { 248 BIO_printf(bio_err, "bad output format specified for outfile\n"); 249 goto end; 250 } 251 if (i <= 0) { 252 BIO_printf(bio_err, "unable to write private key\n"); 253 ERR_print_errors(bio_err); 254 goto end; 255 } 256 ret = 0; 257 end: 258 BIO_free_all(out); 259 DSA_free(dsa); 260 release_engine(e); 261 OPENSSL_free(passin); 262 OPENSSL_free(passout); 263 return ret; 264 } 265 #endif 266