blob: dddc5b816c4cd030fe5296b9dac79c3c1a61a677 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * 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
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00008 */
9
Rich Salzeffaf4d2016-01-31 13:08:23 -050010#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_DSA
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
Matt Caswell0f113f32015-01-22 03:40:55 +000015# include <stdio.h>
16# include <stdlib.h>
17# include <string.h>
18# include <time.h>
19# include "apps.h"
20# include <openssl/bio.h>
21# include <openssl/err.h>
22# include <openssl/dsa.h>
23# include <openssl/evp.h>
24# include <openssl/x509.h>
25# include <openssl/pem.h>
26# include <openssl/bn.h>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000027
Rich Salz7e1b7482015-04-24 15:26:15 -040028typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
FdaSilvaYYe7917e32016-08-05 00:19:36 +020030 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENGINE,
31 /* Do not change the order here; see case statements below */
32 OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
33 OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_PUBIN,
Rich Salz7e1b7482015-04-24 15:26:15 -040034 OPT_PUBOUT, OPT_CIPHER, OPT_PASSIN, OPT_PASSOUT
35} OPTION_CHOICE;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000036
FdaSilvaYY44c83eb2016-03-13 14:07:50 +010037const OPTIONS dsa_options[] = {
Rich Salz7e1b7482015-04-24 15:26:15 -040038 {"help", OPT_HELP, '-', "Display this summary"},
Dr. Stephen Hensond18ba3c2016-05-10 15:19:56 +010039 {"inform", OPT_INFORM, 'f', "Input format, DER PEM PVK"},
Dr. Stephen Hensonb3795982016-11-16 23:14:30 +000040 {"outform", OPT_OUTFORM, 'f', "Output format, DER PEM PVK"},
Dr. Stephen Hensondd958972016-02-08 15:06:48 +000041 {"in", OPT_IN, 's', "Input key"},
Rich Salz7e1b7482015-04-24 15:26:15 -040042 {"out", OPT_OUT, '>', "Output file"},
Rich Salz7e1b7482015-04-24 15:26:15 -040043 {"noout", OPT_NOOUT, '-', "Don't print key out"},
44 {"text", OPT_TEXT, '-', "Print the key in text"},
45 {"modulus", OPT_MODULUS, '-', "Print the DSA public value"},
FdaSilvaYY16e1b282016-03-20 21:14:10 +010046 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
47 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
Rich Salz7e1b7482015-04-24 15:26:15 -040048 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
49 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
50 {"", OPT_CIPHER, '-', "Any supported cipher"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -040051# ifndef OPENSSL_NO_RC4
FdaSilvaYYe7917e32016-08-05 00:19:36 +020052 {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
53 {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
54 {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -040055# endif
56# ifndef OPENSSL_NO_ENGINE
57 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
58# endif
Rich Salz7e1b7482015-04-24 15:26:15 -040059 {NULL}
60};
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000061
Rich Salz7e1b7482015-04-24 15:26:15 -040062int dsa_main(int argc, char **argv)
Matt Caswell0f113f32015-01-22 03:40:55 +000063{
Rich Salz7e1b7482015-04-24 15:26:15 -040064 BIO *out = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000065 DSA *dsa = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -040066 ENGINE *e = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000067 const EVP_CIPHER *enc = NULL;
Rich Salz333b0702015-04-25 15:41:29 -040068 char *infile = NULL, *outfile = NULL, *prog;
69 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -040070 OPTION_CHOICE o;
71 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
Ben Laurie97300432016-03-22 02:39:36 +000072 int i, modulus = 0, pubin = 0, pubout = 0, ret = 1;
73# ifndef OPENSSL_NO_RC4
74 int pvk_encr = 2;
75# endif
Rich Salz3b061a02015-05-02 10:01:33 -040076 int private = 0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000077
Rich Salz7e1b7482015-04-24 15:26:15 -040078 prog = opt_init(argc, argv, dsa_options);
79 while ((o = opt_next()) != OPT_EOF) {
80 switch (o) {
81 case OPT_EOF:
82 case OPT_ERR:
Rich Salz7e1b7482015-04-24 15:26:15 -040083 opthelp:
84 ret = 0;
85 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
86 goto end;
87 case OPT_HELP:
88 opt_help(dsa_options);
89 ret = 0;
90 goto end;
91 case OPT_INFORM:
Dr. Stephen Hensondd958972016-02-08 15:06:48 +000092 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
Rich Salz7e1b7482015-04-24 15:26:15 -040093 goto opthelp;
94 break;
95 case OPT_IN:
96 infile = opt_arg();
97 break;
98 case OPT_OUTFORM:
Dr. Stephen Hensonb3795982016-11-16 23:14:30 +000099 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
Rich Salz7e1b7482015-04-24 15:26:15 -0400100 goto opthelp;
101 break;
102 case OPT_OUT:
103 outfile = opt_arg();
104 break;
105 case OPT_ENGINE:
Rich Salz333b0702015-04-25 15:41:29 -0400106 e = setup_engine(opt_arg(), 0);
Rich Salz7e1b7482015-04-24 15:26:15 -0400107 break;
108 case OPT_PASSIN:
109 passinarg = opt_arg();
110 break;
111 case OPT_PASSOUT:
112 passoutarg = opt_arg();
113 break;
FdaSilvaYYe7917e32016-08-05 00:19:36 +0200114 case OPT_PVK_STRONG: /* pvk_encr:= 2 */
115 case OPT_PVK_WEAK: /* pvk_encr:= 1 */
116 case OPT_PVK_NONE: /* pvk_encr:= 0 */
Dr. Stephen Henson35313762015-02-26 19:23:38 +0000117#ifndef OPENSSL_NO_RC4
FdaSilvaYYe7917e32016-08-05 00:19:36 +0200118 pvk_encr = (o - OPT_PVK_NONE);
Dr. Stephen Henson35313762015-02-26 19:23:38 +0000119#endif
FdaSilvaYYe7917e32016-08-05 00:19:36 +0200120 break;
Rich Salz7e1b7482015-04-24 15:26:15 -0400121 case OPT_NOOUT:
Matt Caswell0f113f32015-01-22 03:40:55 +0000122 noout = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400123 break;
124 case OPT_TEXT:
Matt Caswell0f113f32015-01-22 03:40:55 +0000125 text = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400126 break;
127 case OPT_MODULUS:
Matt Caswell0f113f32015-01-22 03:40:55 +0000128 modulus = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400129 break;
130 case OPT_PUBIN:
Matt Caswell0f113f32015-01-22 03:40:55 +0000131 pubin = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400132 break;
133 case OPT_PUBOUT:
Matt Caswell0f113f32015-01-22 03:40:55 +0000134 pubout = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400135 break;
136 case OPT_CIPHER:
137 if (!opt_cipher(opt_unknown(), &enc))
138 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000139 break;
140 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000141 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400142 argc = opt_num_rest();
Kurt Roeckx03358512016-02-14 20:45:02 +0100143 if (argc != 0)
144 goto opthelp;
145
Rich Salz3b061a02015-05-02 10:01:33 -0400146 private = pubin || pubout ? 0 : 1;
Viktor Dukhovni7eff6aa2015-12-13 02:51:44 -0500147 if (text && !pubin)
Rich Salz3b061a02015-05-02 10:01:33 -0400148 private = 1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000149
Rich Salz7e1b7482015-04-24 15:26:15 -0400150 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000151 BIO_printf(bio_err, "Error getting passwords\n");
152 goto end;
153 }
Dr. Stephen Hensona3fe3822000-02-16 23:16:01 +0000154
Matt Caswell0f113f32015-01-22 03:40:55 +0000155 BIO_printf(bio_err, "read DSA key\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000156 {
157 EVP_PKEY *pkey;
Dr. Stephen Hensona0156a92005-08-31 16:37:54 +0000158
Matt Caswell0f113f32015-01-22 03:40:55 +0000159 if (pubin)
Rich Salz7e1b7482015-04-24 15:26:15 -0400160 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
Matt Caswell0f113f32015-01-22 03:40:55 +0000161 else
Rich Salz7e1b7482015-04-24 15:26:15 -0400162 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
Dr. Stephen Hensona0156a92005-08-31 16:37:54 +0000163
Matt Caswell0f113f32015-01-22 03:40:55 +0000164 if (pkey) {
165 dsa = EVP_PKEY_get1_DSA(pkey);
166 EVP_PKEY_free(pkey);
167 }
168 }
169 if (dsa == NULL) {
170 BIO_printf(bio_err, "unable to load Key\n");
171 ERR_print_errors(bio_err);
172 goto end;
173 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000174
Richard Levittebdd58d92015-09-04 12:49:06 +0200175 out = bio_open_owner(outfile, outformat, private);
Rich Salz7e1b7482015-04-24 15:26:15 -0400176 if (out == NULL)
177 goto end;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000178
Rich Salz3b061a02015-05-02 10:01:33 -0400179 if (text) {
Viktor Dukhovni7eff6aa2015-12-13 02:51:44 -0500180 assert(pubin || private);
Matt Caswell0f113f32015-01-22 03:40:55 +0000181 if (!DSA_print(out, dsa, 0)) {
182 perror(outfile);
183 ERR_print_errors(bio_err);
184 goto end;
185 }
Rich Salz3b061a02015-05-02 10:01:33 -0400186 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000187
Matt Caswell0f113f32015-01-22 03:40:55 +0000188 if (modulus) {
Richard Levitte2ac61152016-06-14 15:49:05 +0200189 const BIGNUM *pub_key = NULL;
Matt Caswell6e9fa572016-03-30 17:18:55 +0100190 DSA_get0_key(dsa, &pub_key, NULL);
Rich Salz7e1b7482015-04-24 15:26:15 -0400191 BIO_printf(out, "Public Key=");
Matt Caswell6e9fa572016-03-30 17:18:55 +0100192 BN_print(out, pub_key);
Rich Salz7e1b7482015-04-24 15:26:15 -0400193 BIO_printf(out, "\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000194 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000195
Rich Salz7e1b7482015-04-24 15:26:15 -0400196 if (noout) {
197 ret = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000198 goto end;
Rich Salz7e1b7482015-04-24 15:26:15 -0400199 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000200 BIO_printf(bio_err, "writing DSA key\n");
201 if (outformat == FORMAT_ASN1) {
202 if (pubin || pubout)
203 i = i2d_DSA_PUBKEY_bio(out, dsa);
Rich Salz3b061a02015-05-02 10:01:33 -0400204 else {
205 assert(private);
Matt Caswell0f113f32015-01-22 03:40:55 +0000206 i = i2d_DSAPrivateKey_bio(out, dsa);
Rich Salz3b061a02015-05-02 10:01:33 -0400207 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000208 } else if (outformat == FORMAT_PEM) {
209 if (pubin || pubout)
210 i = PEM_write_bio_DSA_PUBKEY(out, dsa);
Rich Salz3b061a02015-05-02 10:01:33 -0400211 else {
212 assert(private);
Matt Caswell0f113f32015-01-22 03:40:55 +0000213 i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
214 NULL, 0, NULL, passout);
Rich Salz3b061a02015-05-02 10:01:33 -0400215 }
Dr. Stephen Hensonb6c68982016-11-17 13:17:28 +0000216# ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +0000217 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
218 EVP_PKEY *pk;
219 pk = EVP_PKEY_new();
220 EVP_PKEY_set1_DSA(pk, dsa);
Rich Salz3b061a02015-05-02 10:01:33 -0400221 if (outformat == FORMAT_PVK) {
Viktor Dukhovni7eff6aa2015-12-13 02:51:44 -0500222 if (pubin) {
223 BIO_printf(bio_err, "PVK form impossible with public key input\n");
224 EVP_PKEY_free(pk);
225 goto end;
226 }
Rich Salz3b061a02015-05-02 10:01:33 -0400227 assert(private);
Dr. Stephen Hensonb6c68982016-11-17 13:17:28 +0000228# ifdef OPENSSL_NO_RC4
229 BIO_printf(bio_err, "PVK format not supported\n");
230 EVP_PKEY_free(pk);
231 goto end;
232# else
Matt Caswell0f113f32015-01-22 03:40:55 +0000233 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
Dr. Stephen Hensonb6c68982016-11-17 13:17:28 +0000234# endif
Rich Salz3b061a02015-05-02 10:01:33 -0400235 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000236 else if (pubin || pubout)
237 i = i2b_PublicKey_bio(out, pk);
Rich Salz3b061a02015-05-02 10:01:33 -0400238 else {
239 assert(private);
Matt Caswell0f113f32015-01-22 03:40:55 +0000240 i = i2b_PrivateKey_bio(out, pk);
Rich Salz3b061a02015-05-02 10:01:33 -0400241 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000242 EVP_PKEY_free(pk);
243# endif
244 } else {
245 BIO_printf(bio_err, "bad output format specified for outfile\n");
246 goto end;
247 }
248 if (i <= 0) {
249 BIO_printf(bio_err, "unable to write private key\n");
250 ERR_print_errors(bio_err);
Rich Salz7e1b7482015-04-24 15:26:15 -0400251 goto end;
252 }
253 ret = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000254 end:
Rich Salzca3a82c2015-03-25 11:31:18 -0400255 BIO_free_all(out);
Rich Salzd6407082015-03-24 10:17:37 -0400256 DSA_free(dsa);
Richard Levittedd1abd42016-09-28 23:39:18 +0200257 release_engine(e);
Rich Salzb548a1f2015-05-01 10:02:07 -0400258 OPENSSL_free(passin);
259 OPENSSL_free(passout);
Rich Salz7e1b7482015-04-24 15:26:15 -0400260 return (ret);
Matt Caswell0f113f32015-01-22 03:40:55 +0000261}
Ulf Möllerf5d7a031999-04-27 01:14:46 +0000262#endif