]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/rsa.c
Fix no-ocsp
[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>
effaf4de
RS
107#ifdef OPENSSL_NO_RSA
108NON_EMPTY_TRANSLATION_UNIT
109#else
110
0f113f3e
MC
111# include <stdio.h>
112# include <stdlib.h>
113# include <string.h>
114# include <time.h>
115# include "apps.h"
116# include <openssl/bio.h>
117# include <openssl/err.h>
118# include <openssl/rsa.h>
119# include <openssl/evp.h>
120# include <openssl/x509.h>
121# include <openssl/pem.h>
122# include <openssl/bn.h>
123
7e1b7485
RS
124typedef enum OPTION_choice {
125 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
126 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
127 OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
128 OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT, OPT_PVK_STRONG, OPT_PVK_WEAK,
129 OPT_PVK_NONE, OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
130} OPTION_CHOICE;
131
132OPTIONS rsa_options[] = {
133 {"help", OPT_HELP, '-', "Display this summary"},
134 {"inform", OPT_INFORM, 'f', "Input format, one of DER NET PEM"},
135 {"outform", OPT_OUTFORM, 'f', "Output format, one of DER NET PEM PVK"},
dd958974 136 {"in", OPT_IN, 's', "Input file"},
7e1b7485
RS
137 {"out", OPT_OUT, '>', "Output file"},
138 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
139 {"pubout", OPT_PUBOUT, '-', "Output a public key"},
140 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
141 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
bc2f5803
RS
142 {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
143 {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
7e1b7485
RS
144 {"noout", OPT_NOOUT, '-', "Don't print key out"},
145 {"text", OPT_TEXT, '-', "Print the key in text"},
146 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
147 {"check", OPT_CHECK, '-', "Verify key consistency"},
148 {"", OPT_CIPHER, '-', "Any supported cipher"},
9c3bcfa0
RS
149# ifdef OPENSSL_NO_RC4
150 {"pvk-strong", OPT_PVK_STRONG, '-'},
151 {"pvk-weak", OPT_PVK_WEAK, '-'},
152 {"pvk-none", OPT_PVK_NONE, '-'},
153# endif
7e1b7485
RS
154# ifndef OPENSSL_NO_ENGINE
155 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
156# endif
157 {NULL}
158};
667ac4ec 159
7e1b7485 160int rsa_main(int argc, char **argv)
0f113f3e
MC
161{
162 ENGINE *e = NULL;
7e1b7485 163 BIO *out = NULL;
0f113f3e 164 RSA *rsa = NULL;
0f113f3e 165 const EVP_CIPHER *enc = NULL;
333b070e 166 char *infile = NULL, *outfile = NULL, *prog;
7e1b7485 167 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
3b061a00 168 int i, private = 0;
7e1b7485 169 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
83ae8124
MC
170 int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
171# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
172 int pvk_encr = 2;
173#endif
7e1b7485
RS
174 OPTION_CHOICE o;
175
176 prog = opt_init(argc, argv, rsa_options);
177 while ((o = opt_next()) != OPT_EOF) {
178 switch (o) {
179 case OPT_EOF:
180 case OPT_ERR:
7e1b7485
RS
181 opthelp:
182 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
183 goto end;
184 case OPT_HELP:
185 opt_help(rsa_options);
186 ret = 0;
187 goto end;
188 case OPT_INFORM:
189 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
190 goto opthelp;
191 break;
192 case OPT_IN:
193 infile = opt_arg();
194 break;
195 case OPT_OUTFORM:
196 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
197 goto opthelp;
198 break;
199 case OPT_OUT:
200 outfile = opt_arg();
201 break;
202 case OPT_PASSIN:
203 passinarg = opt_arg();
204 break;
205 case OPT_PASSOUT:
206 passoutarg = opt_arg();
207 break;
208 case OPT_ENGINE:
333b070e 209 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
210 break;
211 case OPT_PUBIN:
0f113f3e 212 pubin = 1;
7e1b7485
RS
213 break;
214 case OPT_PUBOUT:
0f113f3e 215 pubout = 1;
7e1b7485
RS
216 break;
217 case OPT_RSAPUBKEY_IN:
0f113f3e 218 pubin = 2;
7e1b7485
RS
219 break;
220 case OPT_RSAPUBKEY_OUT:
0f113f3e 221 pubout = 2;
7e1b7485 222 break;
83ae8124 223# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
7e1b7485 224 case OPT_PVK_STRONG:
0f113f3e 225 pvk_encr = 2;
7e1b7485
RS
226 break;
227 case OPT_PVK_WEAK:
0f113f3e 228 pvk_encr = 1;
7e1b7485
RS
229 break;
230 case OPT_PVK_NONE:
0f113f3e 231 pvk_encr = 0;
7e1b7485 232 break;
9c3bcfa0
RS
233#else
234 case OPT_PVK_STRONG:
235 case OPT_PVK_WEAK:
236 case OPT_PVK_NONE:
237 break;
35313768 238#endif
7e1b7485 239 case OPT_NOOUT:
0f113f3e 240 noout = 1;
7e1b7485
RS
241 break;
242 case OPT_TEXT:
0f113f3e 243 text = 1;
7e1b7485
RS
244 break;
245 case OPT_MODULUS:
0f113f3e 246 modulus = 1;
7e1b7485
RS
247 break;
248 case OPT_CHECK:
0f113f3e 249 check = 1;
7e1b7485
RS
250 break;
251 case OPT_CIPHER:
252 if (!opt_cipher(opt_unknown(), &enc))
253 goto opthelp;
0f113f3e
MC
254 break;
255 }
0f113f3e 256 }
7e1b7485 257 argc = opt_num_rest();
03358517
KR
258 if (argc != 0)
259 goto opthelp;
260
7eff6aa0 261 private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
0f113f3e 262
7e1b7485 263 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
264 BIO_printf(bio_err, "Error getting passwords\n");
265 goto end;
266 }
0f113f3e
MC
267 if (check && pubin) {
268 BIO_printf(bio_err, "Only private keys can be checked\n");
269 goto end;
270 }
271
0f113f3e
MC
272 {
273 EVP_PKEY *pkey;
274
275 if (pubin) {
276 int tmpformat = -1;
277 if (pubin == 2) {
278 if (informat == FORMAT_PEM)
279 tmpformat = FORMAT_PEMRSA;
280 else if (informat == FORMAT_ASN1)
281 tmpformat = FORMAT_ASN1RSA;
7e1b7485 282 } else
0f113f3e
MC
283 tmpformat = informat;
284
7e1b7485 285 pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
0f113f3e 286 } else
7e1b7485 287 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
0f113f3e
MC
288
289 if (pkey != NULL)
290 rsa = EVP_PKEY_get1_RSA(pkey);
291 EVP_PKEY_free(pkey);
292 }
293
294 if (rsa == NULL) {
295 ERR_print_errors(bio_err);
296 goto end;
297 }
298
bdd58d98 299 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
300 if (out == NULL)
301 goto end;
0f113f3e 302
3b061a00 303 if (text) {
7eff6aa0 304 assert(pubin || private);
0f113f3e
MC
305 if (!RSA_print(out, rsa, 0)) {
306 perror(outfile);
307 ERR_print_errors(bio_err);
308 goto end;
309 }
3b061a00 310 }
0f113f3e
MC
311
312 if (modulus) {
313 BIO_printf(out, "Modulus=");
314 BN_print(out, rsa->n);
315 BIO_printf(out, "\n");
316 }
317
318 if (check) {
319 int r = RSA_check_key(rsa);
320
321 if (r == 1)
322 BIO_printf(out, "RSA key ok\n");
323 else if (r == 0) {
324 unsigned long err;
325
326 while ((err = ERR_peek_error()) != 0 &&
327 ERR_GET_LIB(err) == ERR_LIB_RSA &&
328 ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
329 ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
330 BIO_printf(out, "RSA key error: %s\n",
331 ERR_reason_error_string(err));
332 ERR_get_error(); /* remove e from error stack */
333 }
334 }
335
bc2f5803
RS
336 /* should happen only if r == -1 */
337 if (r == -1 || ERR_peek_error() != 0) {
0f113f3e
MC
338 ERR_print_errors(bio_err);
339 goto end;
340 }
341 }
342
343 if (noout) {
344 ret = 0;
345 goto end;
346 }
347 BIO_printf(bio_err, "writing RSA key\n");
348 if (outformat == FORMAT_ASN1) {
349 if (pubout || pubin) {
350 if (pubout == 2)
351 i = i2d_RSAPublicKey_bio(out, rsa);
352 else
353 i = i2d_RSA_PUBKEY_bio(out, rsa);
3b061a00
RS
354 } else {
355 assert(private);
0f113f3e 356 i = i2d_RSAPrivateKey_bio(out, rsa);
3b061a00 357 }
0f113f3e 358 }
0f113f3e
MC
359 else if (outformat == FORMAT_PEM) {
360 if (pubout || pubin) {
361 if (pubout == 2)
362 i = PEM_write_bio_RSAPublicKey(out, rsa);
363 else
364 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
3b061a00
RS
365 } else {
366 assert(private);
0f113f3e
MC
367 i = PEM_write_bio_RSAPrivateKey(out, rsa,
368 enc, NULL, 0, NULL, passout);
3b061a00 369 }
0f113f3e
MC
370# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
371 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
372 EVP_PKEY *pk;
373 pk = EVP_PKEY_new();
374 EVP_PKEY_set1_RSA(pk, rsa);
7eff6aa0
VD
375 if (outformat == FORMAT_PVK) {
376 if (pubin) {
377 BIO_printf(bio_err, "PVK form impossible with public key input\n");
378 EVP_PKEY_free(pk);
379 goto end;
380 }
381 assert(private);
0f113f3e 382 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
7eff6aa0 383 } else if (pubin || pubout) {
0f113f3e 384 i = i2b_PublicKey_bio(out, pk);
7eff6aa0 385 } else {
3b061a00 386 assert(private);
0f113f3e 387 i = i2b_PrivateKey_bio(out, pk);
3b061a00 388 }
0f113f3e
MC
389 EVP_PKEY_free(pk);
390# endif
391 } else {
392 BIO_printf(bio_err, "bad output format specified for outfile\n");
393 goto end;
394 }
395 if (i <= 0) {
396 BIO_printf(bio_err, "unable to write key\n");
397 ERR_print_errors(bio_err);
398 } else
399 ret = 0;
400 end:
ca3a82c3 401 BIO_free_all(out);
d6407083 402 RSA_free(rsa);
b548a1f1
RS
403 OPENSSL_free(passin);
404 OPENSSL_free(passout);
7e1b7485 405 return (ret);
0f113f3e 406}
f5d7a031 407#endif