Deal with the consequences of constifying getters
[openssl.git] / apps / dsa.c
1 /*
2  * Copyright 1995-2016 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 <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>
27
28 typedef enum OPTION_choice {
29     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
31     OPT_ENGINE, OPT_PVK_STRONG, OPT_PVK_WEAK,
32     OPT_PVK_NONE, OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_PUBIN,
33     OPT_PUBOUT, OPT_CIPHER, OPT_PASSIN, OPT_PASSOUT
34 } OPTION_CHOICE;
35
36 OPTIONS dsa_options[] = {
37     {"help", OPT_HELP, '-', "Display this summary"},
38     {"inform", OPT_INFORM, 'f', "Input format, DER PEM PVK"},
39     {"outform", OPT_OUTFORM, 'F', "Output format, DER PEM PVK"},
40     {"in", OPT_IN, 's', "Input key"},
41     {"out", OPT_OUT, '>', "Output file"},
42     {"noout", OPT_NOOUT, '-', "Don't print key out"},
43     {"text", OPT_TEXT, '-', "Print the key in text"},
44     {"modulus", OPT_MODULUS, '-', "Print the DSA public value"},
45     {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
46     {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
47     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
48     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
49     {"", OPT_CIPHER, '-', "Any supported cipher"},
50 # ifndef OPENSSL_NO_RC4
51     {"pvk-strong", OPT_PVK_STRONG, '-'},
52     {"pvk-weak", OPT_PVK_WEAK, '-'},
53     {"pvk-none", OPT_PVK_NONE, '-'},
54 # endif
55 # ifndef OPENSSL_NO_ENGINE
56     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
57 # endif
58     {NULL}
59 };
60
61 int dsa_main(int argc, char **argv)
62 {
63     BIO *out = NULL;
64     DSA *dsa = NULL;
65     ENGINE *e = NULL;
66     const EVP_CIPHER *enc = NULL;
67     char *infile = NULL, *outfile = NULL, *prog;
68     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
69     OPTION_CHOICE o;
70     int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
71     int i, modulus = 0, pubin = 0, pubout = 0, ret = 1;
72 # ifndef OPENSSL_NO_RC4
73     int pvk_encr = 2;
74 # endif
75     int private = 0;
76
77     prog = opt_init(argc, argv, dsa_options);
78     while ((o = opt_next()) != OPT_EOF) {
79         switch (o) {
80         case OPT_EOF:
81         case OPT_ERR:
82  opthelp:
83             ret = 0;
84             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85             goto end;
86         case OPT_HELP:
87             opt_help(dsa_options);
88             ret = 0;
89             goto end;
90         case OPT_INFORM:
91             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
92                 goto opthelp;
93             break;
94         case OPT_IN:
95             infile = opt_arg();
96             break;
97         case OPT_OUTFORM:
98             if (!opt_format
99                 (opt_arg(), OPT_FMT_PEMDER | OPT_FMT_PVK, &outformat))
100                 goto opthelp;
101             break;
102         case OPT_OUT:
103             outfile = opt_arg();
104             break;
105         case OPT_ENGINE:
106             e = setup_engine(opt_arg(), 0);
107             break;
108         case OPT_PASSIN:
109             passinarg = opt_arg();
110             break;
111         case OPT_PASSOUT:
112             passoutarg = opt_arg();
113             break;
114 #ifndef OPENSSL_NO_RC4
115         case OPT_PVK_STRONG:
116             pvk_encr = 2;
117             break;
118         case OPT_PVK_WEAK:
119             pvk_encr = 1;
120             break;
121         case OPT_PVK_NONE:
122             pvk_encr = 0;
123             break;
124 #else
125         case OPT_PVK_STRONG:
126         case OPT_PVK_WEAK:
127         case OPT_PVK_NONE:
128             break;
129 #endif
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         }
150     }
151     argc = opt_num_rest();
152     if (argc != 0)
153         goto opthelp;
154
155     private = pubin || pubout ? 0 : 1;
156     if (text && !pubin)
157         private = 1;
158
159     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
160         BIO_printf(bio_err, "Error getting passwords\n");
161         goto end;
162     }
163
164     BIO_printf(bio_err, "read DSA key\n");
165     {
166         EVP_PKEY *pkey;
167
168         if (pubin)
169             pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
170         else
171             pkey = load_key(infile, informat, 1, passin, e, "Private Key");
172
173         if (pkey) {
174             dsa = EVP_PKEY_get1_DSA(pkey);
175             EVP_PKEY_free(pkey);
176         }
177     }
178     if (dsa == NULL) {
179         BIO_printf(bio_err, "unable to load Key\n");
180         ERR_print_errors(bio_err);
181         goto end;
182     }
183
184     out = bio_open_owner(outfile, outformat, private);
185     if (out == NULL)
186         goto end;
187
188     if (text) {
189         assert(pubin || private);
190         if (!DSA_print(out, dsa, 0)) {
191             perror(outfile);
192             ERR_print_errors(bio_err);
193             goto end;
194         }
195     }
196
197     if (modulus) {
198         const BIGNUM *pub_key = NULL;
199         DSA_get0_key(dsa, &pub_key, NULL);
200         BIO_printf(out, "Public Key=");
201         BN_print(out, pub_key);
202         BIO_printf(out, "\n");
203     }
204
205     if (noout) {
206         ret = 0;
207         goto end;
208     }
209     BIO_printf(bio_err, "writing DSA key\n");
210     if (outformat == FORMAT_ASN1) {
211         if (pubin || pubout)
212             i = i2d_DSA_PUBKEY_bio(out, dsa);
213         else {
214             assert(private);
215             i = i2d_DSAPrivateKey_bio(out, dsa);
216         }
217     } else if (outformat == FORMAT_PEM) {
218         if (pubin || pubout)
219             i = PEM_write_bio_DSA_PUBKEY(out, dsa);
220         else {
221             assert(private);
222             i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
223                                             NULL, 0, NULL, passout);
224         }
225 # if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
226     } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
227         EVP_PKEY *pk;
228         pk = EVP_PKEY_new();
229         EVP_PKEY_set1_DSA(pk, dsa);
230         if (outformat == FORMAT_PVK) {
231             if (pubin) {
232                 BIO_printf(bio_err, "PVK form impossible with public key input\n");
233                 EVP_PKEY_free(pk);
234                 goto end;
235             }
236             assert(private);
237             i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
238         }
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     OPENSSL_free(passin);
261     OPENSSL_free(passout);
262     return (ret);
263 }
264 #endif