]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/dsa.c
Update copyright year
[thirdparty/openssl.git] / apps / dsa.c
CommitLineData
846e33c7 1/*
6738bf14 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
846e33c7
RS
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
d02b48c6
RE
8 */
9
effaf4de
RS
10#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_DSA
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
0f113f3e
MC
15# include <stdio.h>
16# include <stdlib.h>
17# include <string.h>
18# include <time.h>
19# include "apps.h"
dab2cd68 20# include "progs.h"
0f113f3e
MC
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>
d02b48c6 28
7e1b7485
RS
29typedef enum OPTION_choice {
30 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
e7917e38
F
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,
7e1b7485
RS
35 OPT_PUBOUT, OPT_CIPHER, OPT_PASSIN, OPT_PASSOUT
36} OPTION_CHOICE;
d02b48c6 37
44c83ebd 38const OPTIONS dsa_options[] = {
7e1b7485 39 {"help", OPT_HELP, '-', "Display this summary"},
d18ba3cc 40 {"inform", OPT_INFORM, 'f', "Input format, DER PEM PVK"},
b3795987 41 {"outform", OPT_OUTFORM, 'f', "Output format, DER PEM PVK"},
dd958974 42 {"in", OPT_IN, 's', "Input key"},
7e1b7485 43 {"out", OPT_OUT, '>', "Output file"},
7e1b7485
RS
44 {"noout", OPT_NOOUT, '-', "Don't print key out"},
45 {"text", OPT_TEXT, '-', "Print the key in text"},
46 {"modulus", OPT_MODULUS, '-', "Print the DSA public value"},
16e1b281
F
47 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
48 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
7e1b7485
RS
49 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
50 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
51 {"", OPT_CIPHER, '-', "Any supported cipher"},
9c3bcfa0 52# ifndef OPENSSL_NO_RC4
e7917e38
F
53 {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
54 {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
55 {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
9c3bcfa0
RS
56# endif
57# ifndef OPENSSL_NO_ENGINE
58 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
59# endif
7e1b7485
RS
60 {NULL}
61};
667ac4ec 62
7e1b7485 63int dsa_main(int argc, char **argv)
0f113f3e 64{
7e1b7485 65 BIO *out = NULL;
0f113f3e 66 DSA *dsa = NULL;
7e1b7485 67 ENGINE *e = NULL;
0f113f3e 68 const EVP_CIPHER *enc = NULL;
333b070e
RS
69 char *infile = NULL, *outfile = NULL, *prog;
70 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
7e1b7485
RS
71 OPTION_CHOICE o;
72 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
9730043f
BL
73 int i, modulus = 0, pubin = 0, pubout = 0, ret = 1;
74# ifndef OPENSSL_NO_RC4
75 int pvk_encr = 2;
76# endif
3b061a00 77 int private = 0;
d02b48c6 78
7e1b7485
RS
79 prog = opt_init(argc, argv, dsa_options);
80 while ((o = opt_next()) != OPT_EOF) {
81 switch (o) {
82 case OPT_EOF:
83 case OPT_ERR:
7e1b7485
RS
84 opthelp:
85 ret = 0;
86 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
87 goto end;
88 case OPT_HELP:
89 opt_help(dsa_options);
90 ret = 0;
91 goto end;
92 case OPT_INFORM:
dd958974 93 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
7e1b7485
RS
94 goto opthelp;
95 break;
96 case OPT_IN:
97 infile = opt_arg();
98 break;
99 case OPT_OUTFORM:
b3795987 100 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
7e1b7485
RS
101 goto opthelp;
102 break;
103 case OPT_OUT:
104 outfile = opt_arg();
105 break;
106 case OPT_ENGINE:
333b070e 107 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
108 break;
109 case OPT_PASSIN:
110 passinarg = opt_arg();
111 break;
112 case OPT_PASSOUT:
113 passoutarg = opt_arg();
114 break;
e7917e38
F
115 case OPT_PVK_STRONG: /* pvk_encr:= 2 */
116 case OPT_PVK_WEAK: /* pvk_encr:= 1 */
117 case OPT_PVK_NONE: /* pvk_encr:= 0 */
35313768 118#ifndef OPENSSL_NO_RC4
e7917e38 119 pvk_encr = (o - OPT_PVK_NONE);
35313768 120#endif
e7917e38 121 break;
7e1b7485 122 case OPT_NOOUT:
0f113f3e 123 noout = 1;
7e1b7485
RS
124 break;
125 case OPT_TEXT:
0f113f3e 126 text = 1;
7e1b7485
RS
127 break;
128 case OPT_MODULUS:
0f113f3e 129 modulus = 1;
7e1b7485
RS
130 break;
131 case OPT_PUBIN:
0f113f3e 132 pubin = 1;
7e1b7485
RS
133 break;
134 case OPT_PUBOUT:
0f113f3e 135 pubout = 1;
7e1b7485
RS
136 break;
137 case OPT_CIPHER:
138 if (!opt_cipher(opt_unknown(), &enc))
139 goto end;
0f113f3e
MC
140 break;
141 }
0f113f3e 142 }
7e1b7485 143 argc = opt_num_rest();
03358517
KR
144 if (argc != 0)
145 goto opthelp;
146
3b061a00 147 private = pubin || pubout ? 0 : 1;
7eff6aa0 148 if (text && !pubin)
3b061a00 149 private = 1;
d02b48c6 150
7e1b7485 151 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
152 BIO_printf(bio_err, "Error getting passwords\n");
153 goto end;
154 }
a3fe382e 155
0f113f3e 156 BIO_printf(bio_err, "read DSA key\n");
0f113f3e
MC
157 {
158 EVP_PKEY *pkey;
a0156a92 159
0f113f3e 160 if (pubin)
7e1b7485 161 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
0f113f3e 162 else
7e1b7485 163 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
a0156a92 164
2234212c 165 if (pkey != NULL) {
0f113f3e
MC
166 dsa = EVP_PKEY_get1_DSA(pkey);
167 EVP_PKEY_free(pkey);
168 }
169 }
170 if (dsa == NULL) {
171 BIO_printf(bio_err, "unable to load Key\n");
172 ERR_print_errors(bio_err);
173 goto end;
174 }
d02b48c6 175
bdd58d98 176 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
177 if (out == NULL)
178 goto end;
d02b48c6 179
3b061a00 180 if (text) {
7eff6aa0 181 assert(pubin || private);
0f113f3e
MC
182 if (!DSA_print(out, dsa, 0)) {
183 perror(outfile);
184 ERR_print_errors(bio_err);
185 goto end;
186 }
3b061a00 187 }
d02b48c6 188
0f113f3e 189 if (modulus) {
2ac6115d 190 const BIGNUM *pub_key = NULL;
6e9fa57c 191 DSA_get0_key(dsa, &pub_key, NULL);
7e1b7485 192 BIO_printf(out, "Public Key=");
6e9fa57c 193 BN_print(out, pub_key);
7e1b7485 194 BIO_printf(out, "\n");
0f113f3e 195 }
d02b48c6 196
7e1b7485
RS
197 if (noout) {
198 ret = 0;
0f113f3e 199 goto end;
7e1b7485 200 }
0f113f3e
MC
201 BIO_printf(bio_err, "writing DSA key\n");
202 if (outformat == FORMAT_ASN1) {
2234212c 203 if (pubin || pubout) {
0f113f3e 204 i = i2d_DSA_PUBKEY_bio(out, dsa);
2234212c 205 } else {
3b061a00 206 assert(private);
0f113f3e 207 i = i2d_DSAPrivateKey_bio(out, dsa);
3b061a00 208 }
0f113f3e 209 } else if (outformat == FORMAT_PEM) {
2234212c 210 if (pubin || pubout) {
0f113f3e 211 i = PEM_write_bio_DSA_PUBKEY(out, dsa);
2234212c 212 } else {
3b061a00 213 assert(private);
0f113f3e
MC
214 i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
215 NULL, 0, NULL, passout);
3b061a00 216 }
b6c68982 217# ifndef OPENSSL_NO_RSA
0f113f3e
MC
218 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
219 EVP_PKEY *pk;
220 pk = EVP_PKEY_new();
a6f622bc
PK
221 if (pk == NULL)
222 goto end;
223
0f113f3e 224 EVP_PKEY_set1_DSA(pk, dsa);
3b061a00 225 if (outformat == FORMAT_PVK) {
7eff6aa0
VD
226 if (pubin) {
227 BIO_printf(bio_err, "PVK form impossible with public key input\n");
228 EVP_PKEY_free(pk);
229 goto end;
230 }
3b061a00 231 assert(private);
b6c68982
DSH
232# ifdef OPENSSL_NO_RC4
233 BIO_printf(bio_err, "PVK format not supported\n");
234 EVP_PKEY_free(pk);
235 goto end;
236# else
0f113f3e 237 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
b6c68982 238# endif
2234212c 239 } else if (pubin || pubout) {
0f113f3e 240 i = i2b_PublicKey_bio(out, pk);
2234212c 241 } else {
3b061a00 242 assert(private);
0f113f3e 243 i = i2b_PrivateKey_bio(out, pk);
3b061a00 244 }
0f113f3e
MC
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);
7e1b7485
RS
254 goto end;
255 }
256 ret = 0;
0f113f3e 257 end:
ca3a82c3 258 BIO_free_all(out);
d6407083 259 DSA_free(dsa);
dd1abd44 260 release_engine(e);
b548a1f1
RS
261 OPENSSL_free(passin);
262 OPENSSL_free(passout);
26a7d938 263 return ret;
0f113f3e 264}
f5d7a031 265#endif