]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/rsa.c
Refactor into clear_ciphers; RT3588
[thirdparty/openssl.git] / apps / rsa.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
d02b48c6
RE
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
d02b48c6
RE
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
7e1b7485
RS
57/* ====================================================================
58 * Copyright (c) 1999-2015 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * licensing@OpenSSL.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 */
d02b48c6 105
3eeaab4b 106#include <openssl/opensslconf.h>
cf1b7d96 107#ifndef OPENSSL_NO_RSA
0f113f3e
MC
108# include <stdio.h>
109# include <stdlib.h>
110# include <string.h>
111# include <time.h>
112# include "apps.h"
113# include <openssl/bio.h>
114# include <openssl/err.h>
115# include <openssl/rsa.h>
116# include <openssl/evp.h>
117# include <openssl/x509.h>
118# include <openssl/pem.h>
119# include <openssl/bn.h>
120
7e1b7485
RS
121typedef enum OPTION_choice {
122 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
123 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
124 OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
125 OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT, OPT_PVK_STRONG, OPT_PVK_WEAK,
126 OPT_PVK_NONE, OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
127} OPTION_CHOICE;
128
129OPTIONS rsa_options[] = {
130 {"help", OPT_HELP, '-', "Display this summary"},
131 {"inform", OPT_INFORM, 'f', "Input format, one of DER NET PEM"},
132 {"outform", OPT_OUTFORM, 'f', "Output format, one of DER NET PEM PVK"},
133 {"in", OPT_IN, '<', "Input file"},
134 {"out", OPT_OUT, '>', "Output file"},
135 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
136 {"pubout", OPT_PUBOUT, '-', "Output a public key"},
137 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
138 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
bc2f5803
RS
139 {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
140 {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
7e1b7485
RS
141 {"noout", OPT_NOOUT, '-', "Don't print key out"},
142 {"text", OPT_TEXT, '-', "Print the key in text"},
143 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
144 {"check", OPT_CHECK, '-', "Verify key consistency"},
145 {"", OPT_CIPHER, '-', "Any supported cipher"},
9c3bcfa0
RS
146# ifdef OPENSSL_NO_RC4
147 {"pvk-strong", OPT_PVK_STRONG, '-'},
148 {"pvk-weak", OPT_PVK_WEAK, '-'},
149 {"pvk-none", OPT_PVK_NONE, '-'},
150# endif
7e1b7485
RS
151# ifndef OPENSSL_NO_ENGINE
152 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
153# endif
154 {NULL}
155};
667ac4ec 156
7e1b7485 157int rsa_main(int argc, char **argv)
0f113f3e
MC
158{
159 ENGINE *e = NULL;
7e1b7485 160 BIO *out = NULL;
0f113f3e 161 RSA *rsa = NULL;
0f113f3e 162 const EVP_CIPHER *enc = NULL;
333b070e 163 char *infile = NULL, *outfile = NULL, *prog;
7e1b7485
RS
164 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
165 int i;
166 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
167 int noout = 0, modulus = 0, pubin = 0, pubout = 0, pvk_encr = 2, ret = 1;
168 OPTION_CHOICE o;
169
170 prog = opt_init(argc, argv, rsa_options);
171 while ((o = opt_next()) != OPT_EOF) {
172 switch (o) {
173 case OPT_EOF:
174 case OPT_ERR:
7e1b7485
RS
175 opthelp:
176 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
177 goto end;
178 case OPT_HELP:
179 opt_help(rsa_options);
180 ret = 0;
181 goto end;
182 case OPT_INFORM:
183 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
184 goto opthelp;
185 break;
186 case OPT_IN:
187 infile = opt_arg();
188 break;
189 case OPT_OUTFORM:
190 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
191 goto opthelp;
192 break;
193 case OPT_OUT:
194 outfile = opt_arg();
195 break;
196 case OPT_PASSIN:
197 passinarg = opt_arg();
198 break;
199 case OPT_PASSOUT:
200 passoutarg = opt_arg();
201 break;
202 case OPT_ENGINE:
333b070e 203 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
204 break;
205 case OPT_PUBIN:
0f113f3e 206 pubin = 1;
7e1b7485
RS
207 break;
208 case OPT_PUBOUT:
0f113f3e 209 pubout = 1;
7e1b7485
RS
210 break;
211 case OPT_RSAPUBKEY_IN:
0f113f3e 212 pubin = 2;
7e1b7485
RS
213 break;
214 case OPT_RSAPUBKEY_OUT:
0f113f3e 215 pubout = 2;
7e1b7485 216 break;
35313768 217#ifndef OPENSSL_NO_RC4
7e1b7485 218 case OPT_PVK_STRONG:
0f113f3e 219 pvk_encr = 2;
7e1b7485
RS
220 break;
221 case OPT_PVK_WEAK:
0f113f3e 222 pvk_encr = 1;
7e1b7485
RS
223 break;
224 case OPT_PVK_NONE:
0f113f3e 225 pvk_encr = 0;
7e1b7485 226 break;
9c3bcfa0
RS
227#else
228 case OPT_PVK_STRONG:
229 case OPT_PVK_WEAK:
230 case OPT_PVK_NONE:
231 break;
35313768 232#endif
7e1b7485 233 case OPT_NOOUT:
0f113f3e 234 noout = 1;
7e1b7485
RS
235 break;
236 case OPT_TEXT:
0f113f3e 237 text = 1;
7e1b7485
RS
238 break;
239 case OPT_MODULUS:
0f113f3e 240 modulus = 1;
7e1b7485
RS
241 break;
242 case OPT_CHECK:
0f113f3e 243 check = 1;
7e1b7485
RS
244 break;
245 case OPT_CIPHER:
246 if (!opt_cipher(opt_unknown(), &enc))
247 goto opthelp;
0f113f3e
MC
248 break;
249 }
0f113f3e 250 }
7e1b7485
RS
251 argc = opt_num_rest();
252 argv = opt_rest();
0f113f3e 253
7e1b7485 254 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
255 BIO_printf(bio_err, "Error getting passwords\n");
256 goto end;
257 }
258
296f54ee
RL
259 if (!app_load_modules(NULL))
260 goto end;
261
0f113f3e
MC
262 if (check && pubin) {
263 BIO_printf(bio_err, "Only private keys can be checked\n");
264 goto end;
265 }
266
0f113f3e
MC
267 {
268 EVP_PKEY *pkey;
269
270 if (pubin) {
271 int tmpformat = -1;
272 if (pubin == 2) {
273 if (informat == FORMAT_PEM)
274 tmpformat = FORMAT_PEMRSA;
275 else if (informat == FORMAT_ASN1)
276 tmpformat = FORMAT_ASN1RSA;
7e1b7485 277 } else
0f113f3e
MC
278 tmpformat = informat;
279
7e1b7485 280 pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
0f113f3e 281 } else
7e1b7485 282 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
0f113f3e
MC
283
284 if (pkey != NULL)
285 rsa = EVP_PKEY_get1_RSA(pkey);
286 EVP_PKEY_free(pkey);
287 }
288
289 if (rsa == NULL) {
290 ERR_print_errors(bio_err);
291 goto end;
292 }
293
7e1b7485
RS
294 out = bio_open_default(outfile, "w");
295 if (out == NULL)
296 goto end;
0f113f3e
MC
297
298 if (text)
299 if (!RSA_print(out, rsa, 0)) {
300 perror(outfile);
301 ERR_print_errors(bio_err);
302 goto end;
303 }
304
305 if (modulus) {
306 BIO_printf(out, "Modulus=");
307 BN_print(out, rsa->n);
308 BIO_printf(out, "\n");
309 }
310
311 if (check) {
312 int r = RSA_check_key(rsa);
313
314 if (r == 1)
315 BIO_printf(out, "RSA key ok\n");
316 else if (r == 0) {
317 unsigned long err;
318
319 while ((err = ERR_peek_error()) != 0 &&
320 ERR_GET_LIB(err) == ERR_LIB_RSA &&
321 ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
322 ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
323 BIO_printf(out, "RSA key error: %s\n",
324 ERR_reason_error_string(err));
325 ERR_get_error(); /* remove e from error stack */
326 }
327 }
328
bc2f5803
RS
329 /* should happen only if r == -1 */
330 if (r == -1 || ERR_peek_error() != 0) {
0f113f3e
MC
331 ERR_print_errors(bio_err);
332 goto end;
333 }
334 }
335
336 if (noout) {
337 ret = 0;
338 goto end;
339 }
340 BIO_printf(bio_err, "writing RSA key\n");
341 if (outformat == FORMAT_ASN1) {
342 if (pubout || pubin) {
343 if (pubout == 2)
344 i = i2d_RSAPublicKey_bio(out, rsa);
345 else
346 i = i2d_RSA_PUBKEY_bio(out, rsa);
347 } else
348 i = i2d_RSAPrivateKey_bio(out, rsa);
349 }
350# ifndef OPENSSL_NO_RC4
351 else if (outformat == FORMAT_NETSCAPE) {
68dc6824
RS
352 unsigned char *p, *save;
353 int size = i2d_RSA_NET(rsa, NULL, NULL, 0);
0f113f3e 354
68dc6824 355 save = p = app_malloc(size, "RSA i2d buffer");
7e1b7485 356 i2d_RSA_NET(rsa, &p, NULL, 0);
68dc6824
RS
357 BIO_write(out, (char *)save, size);
358 OPENSSL_free(save);
359 i = 1;
0f113f3e
MC
360 }
361# endif
362 else if (outformat == FORMAT_PEM) {
363 if (pubout || pubin) {
364 if (pubout == 2)
365 i = PEM_write_bio_RSAPublicKey(out, rsa);
366 else
367 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
368 } else
369 i = PEM_write_bio_RSAPrivateKey(out, rsa,
370 enc, NULL, 0, NULL, passout);
371# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
372 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
373 EVP_PKEY *pk;
374 pk = EVP_PKEY_new();
375 EVP_PKEY_set1_RSA(pk, rsa);
376 if (outformat == FORMAT_PVK)
377 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
378 else if (pubin || pubout)
379 i = i2b_PublicKey_bio(out, pk);
380 else
381 i = i2b_PrivateKey_bio(out, pk);
382 EVP_PKEY_free(pk);
383# endif
384 } else {
385 BIO_printf(bio_err, "bad output format specified for outfile\n");
386 goto end;
387 }
388 if (i <= 0) {
389 BIO_printf(bio_err, "unable to write key\n");
390 ERR_print_errors(bio_err);
391 } else
392 ret = 0;
393 end:
ca3a82c3 394 BIO_free_all(out);
d6407083 395 RSA_free(rsa);
b548a1f1
RS
396 OPENSSL_free(passin);
397 OPENSSL_free(passout);
7e1b7485 398 return (ret);
0f113f3e
MC
399}
400#else /* !OPENSSL_NO_RSA */
752d706a
BL
401
402# if PEDANTIC
0f113f3e 403static void *dummy = &dummy;
752d706a
BL
404# endif
405
f5d7a031 406#endif