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