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