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