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