1ebfedea0SLionel Sambuc /* apps/rsa.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc #include <openssl/opensslconf.h>
60ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
61ebfedea0SLionel Sambuc # include <stdio.h>
62ebfedea0SLionel Sambuc # include <stdlib.h>
63ebfedea0SLionel Sambuc # include <string.h>
64ebfedea0SLionel Sambuc # include <time.h>
65ebfedea0SLionel Sambuc # include "apps.h"
66ebfedea0SLionel Sambuc # include <openssl/bio.h>
67ebfedea0SLionel Sambuc # include <openssl/err.h>
68ebfedea0SLionel Sambuc # include <openssl/rsa.h>
69ebfedea0SLionel Sambuc # include <openssl/evp.h>
70ebfedea0SLionel Sambuc # include <openssl/x509.h>
71ebfedea0SLionel Sambuc # include <openssl/pem.h>
72ebfedea0SLionel Sambuc # include <openssl/bn.h>
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc # undef PROG
75ebfedea0SLionel Sambuc # define PROG rsa_main
76ebfedea0SLionel Sambuc
77*0a6a1f1dSLionel Sambuc /*-
78*0a6a1f1dSLionel Sambuc * -inform arg - input format - default PEM (one of DER, NET or PEM)
79ebfedea0SLionel Sambuc * -outform arg - output format - default PEM
80ebfedea0SLionel Sambuc * -in arg - input file - default stdin
81ebfedea0SLionel Sambuc * -out arg - output file - default stdout
82ebfedea0SLionel Sambuc * -des - encrypt output if PEM format with DES in cbc mode
83ebfedea0SLionel Sambuc * -des3 - encrypt output if PEM format
84ebfedea0SLionel Sambuc * -idea - encrypt output if PEM format
85ebfedea0SLionel Sambuc * -seed - encrypt output if PEM format
86ebfedea0SLionel Sambuc * -aes128 - encrypt output if PEM format
87ebfedea0SLionel Sambuc * -aes192 - encrypt output if PEM format
88ebfedea0SLionel Sambuc * -aes256 - encrypt output if PEM format
89ebfedea0SLionel Sambuc * -camellia128 - encrypt output if PEM format
90ebfedea0SLionel Sambuc * -camellia192 - encrypt output if PEM format
91ebfedea0SLionel Sambuc * -camellia256 - encrypt output if PEM format
92ebfedea0SLionel Sambuc * -text - print a text version
93ebfedea0SLionel Sambuc * -modulus - print the RSA key modulus
94ebfedea0SLionel Sambuc * -check - verify key consistency
95ebfedea0SLionel Sambuc * -pubin - Expect a public key in input file.
96ebfedea0SLionel Sambuc * -pubout - Output a public key.
97ebfedea0SLionel Sambuc */
98ebfedea0SLionel Sambuc
99ebfedea0SLionel Sambuc int MAIN(int, char **);
100ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)101ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
102ebfedea0SLionel Sambuc {
103ebfedea0SLionel Sambuc ENGINE *e = NULL;
104ebfedea0SLionel Sambuc int ret = 1;
105ebfedea0SLionel Sambuc RSA *rsa = NULL;
106ebfedea0SLionel Sambuc int i, badops = 0, sgckey = 0;
107ebfedea0SLionel Sambuc const EVP_CIPHER *enc = NULL;
108ebfedea0SLionel Sambuc BIO *out = NULL;
109ebfedea0SLionel Sambuc int informat, outformat, text = 0, check = 0, noout = 0;
110ebfedea0SLionel Sambuc int pubin = 0, pubout = 0;
111ebfedea0SLionel Sambuc char *infile, *outfile, *prog;
112ebfedea0SLionel Sambuc char *passargin = NULL, *passargout = NULL;
113ebfedea0SLionel Sambuc char *passin = NULL, *passout = NULL;
114ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
115ebfedea0SLionel Sambuc char *engine = NULL;
116ebfedea0SLionel Sambuc # endif
117ebfedea0SLionel Sambuc int modulus = 0;
118ebfedea0SLionel Sambuc
119ebfedea0SLionel Sambuc int pvk_encr = 2;
120ebfedea0SLionel Sambuc
121ebfedea0SLionel Sambuc apps_startup();
122ebfedea0SLionel Sambuc
123ebfedea0SLionel Sambuc if (bio_err == NULL)
124ebfedea0SLionel Sambuc if ((bio_err = BIO_new(BIO_s_file())) != NULL)
125ebfedea0SLionel Sambuc BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
126ebfedea0SLionel Sambuc
127ebfedea0SLionel Sambuc if (!load_config(bio_err, NULL))
128ebfedea0SLionel Sambuc goto end;
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc infile = NULL;
131ebfedea0SLionel Sambuc outfile = NULL;
132ebfedea0SLionel Sambuc informat = FORMAT_PEM;
133ebfedea0SLionel Sambuc outformat = FORMAT_PEM;
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc prog = argv[0];
136ebfedea0SLionel Sambuc argc--;
137ebfedea0SLionel Sambuc argv++;
138*0a6a1f1dSLionel Sambuc while (argc >= 1) {
139*0a6a1f1dSLionel Sambuc if (strcmp(*argv, "-inform") == 0) {
140*0a6a1f1dSLionel Sambuc if (--argc < 1)
141*0a6a1f1dSLionel Sambuc goto bad;
142ebfedea0SLionel Sambuc informat = str2fmt(*(++argv));
143*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-outform") == 0) {
144*0a6a1f1dSLionel Sambuc if (--argc < 1)
145*0a6a1f1dSLionel Sambuc goto bad;
146ebfedea0SLionel Sambuc outformat = str2fmt(*(++argv));
147*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-in") == 0) {
148*0a6a1f1dSLionel Sambuc if (--argc < 1)
149*0a6a1f1dSLionel Sambuc goto bad;
150ebfedea0SLionel Sambuc infile = *(++argv);
151*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-out") == 0) {
152*0a6a1f1dSLionel Sambuc if (--argc < 1)
153*0a6a1f1dSLionel Sambuc goto bad;
154ebfedea0SLionel Sambuc outfile = *(++argv);
155*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-passin") == 0) {
156*0a6a1f1dSLionel Sambuc if (--argc < 1)
157*0a6a1f1dSLionel Sambuc goto bad;
158ebfedea0SLionel Sambuc passargin = *(++argv);
159*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-passout") == 0) {
160*0a6a1f1dSLionel Sambuc if (--argc < 1)
161*0a6a1f1dSLionel Sambuc goto bad;
162ebfedea0SLionel Sambuc passargout = *(++argv);
163ebfedea0SLionel Sambuc }
164ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
165*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-engine") == 0) {
166*0a6a1f1dSLionel Sambuc if (--argc < 1)
167*0a6a1f1dSLionel Sambuc goto bad;
168ebfedea0SLionel Sambuc engine = *(++argv);
169ebfedea0SLionel Sambuc }
170ebfedea0SLionel Sambuc # endif
171ebfedea0SLionel Sambuc else if (strcmp(*argv, "-sgckey") == 0)
172ebfedea0SLionel Sambuc sgckey = 1;
173ebfedea0SLionel Sambuc else if (strcmp(*argv, "-pubin") == 0)
174ebfedea0SLionel Sambuc pubin = 1;
175ebfedea0SLionel Sambuc else if (strcmp(*argv, "-pubout") == 0)
176ebfedea0SLionel Sambuc pubout = 1;
177ebfedea0SLionel Sambuc else if (strcmp(*argv, "-RSAPublicKey_in") == 0)
178ebfedea0SLionel Sambuc pubin = 2;
179ebfedea0SLionel Sambuc else if (strcmp(*argv, "-RSAPublicKey_out") == 0)
180ebfedea0SLionel Sambuc pubout = 2;
181ebfedea0SLionel Sambuc else if (strcmp(*argv, "-pvk-strong") == 0)
182ebfedea0SLionel Sambuc pvk_encr = 2;
183ebfedea0SLionel Sambuc else if (strcmp(*argv, "-pvk-weak") == 0)
184ebfedea0SLionel Sambuc pvk_encr = 1;
185ebfedea0SLionel Sambuc else if (strcmp(*argv, "-pvk-none") == 0)
186ebfedea0SLionel Sambuc pvk_encr = 0;
187ebfedea0SLionel Sambuc else if (strcmp(*argv, "-noout") == 0)
188ebfedea0SLionel Sambuc noout = 1;
189ebfedea0SLionel Sambuc else if (strcmp(*argv, "-text") == 0)
190ebfedea0SLionel Sambuc text = 1;
191ebfedea0SLionel Sambuc else if (strcmp(*argv, "-modulus") == 0)
192ebfedea0SLionel Sambuc modulus = 1;
193ebfedea0SLionel Sambuc else if (strcmp(*argv, "-check") == 0)
194ebfedea0SLionel Sambuc check = 1;
195*0a6a1f1dSLionel Sambuc else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
196ebfedea0SLionel Sambuc BIO_printf(bio_err, "unknown option %s\n", *argv);
197ebfedea0SLionel Sambuc badops = 1;
198ebfedea0SLionel Sambuc break;
199ebfedea0SLionel Sambuc }
200ebfedea0SLionel Sambuc argc--;
201ebfedea0SLionel Sambuc argv++;
202ebfedea0SLionel Sambuc }
203ebfedea0SLionel Sambuc
204*0a6a1f1dSLionel Sambuc if (badops) {
205ebfedea0SLionel Sambuc bad:
206ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
207ebfedea0SLionel Sambuc BIO_printf(bio_err, "where options are\n");
208*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
209*0a6a1f1dSLionel Sambuc " -inform arg input format - one of DER NET PEM\n");
210*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
211*0a6a1f1dSLionel Sambuc " -outform arg output format - one of DER NET PEM\n");
212ebfedea0SLionel Sambuc BIO_printf(bio_err, " -in arg input file\n");
213ebfedea0SLionel Sambuc BIO_printf(bio_err, " -sgckey Use IIS SGC key format\n");
214*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
215*0a6a1f1dSLionel Sambuc " -passin arg input file pass phrase source\n");
216ebfedea0SLionel Sambuc BIO_printf(bio_err, " -out arg output file\n");
217*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
218*0a6a1f1dSLionel Sambuc " -passout arg output file pass phrase source\n");
219*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
220*0a6a1f1dSLionel Sambuc " -des encrypt PEM output with cbc des\n");
221*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
222*0a6a1f1dSLionel Sambuc " -des3 encrypt PEM output with ede cbc des using 168 bit key\n");
223ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_IDEA
224*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
225*0a6a1f1dSLionel Sambuc " -idea encrypt PEM output with cbc idea\n");
226ebfedea0SLionel Sambuc # endif
227ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SEED
228*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
229*0a6a1f1dSLionel Sambuc " -seed encrypt PEM output with cbc seed\n");
230ebfedea0SLionel Sambuc # endif
231ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_AES
232ebfedea0SLionel Sambuc BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
233*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
234*0a6a1f1dSLionel Sambuc " encrypt PEM output with cbc aes\n");
235ebfedea0SLionel Sambuc # endif
236ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_CAMELLIA
237ebfedea0SLionel Sambuc BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
238*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
239*0a6a1f1dSLionel Sambuc " encrypt PEM output with cbc camellia\n");
240ebfedea0SLionel Sambuc # endif
241ebfedea0SLionel Sambuc BIO_printf(bio_err, " -text print the key in text\n");
242ebfedea0SLionel Sambuc BIO_printf(bio_err, " -noout don't print key out\n");
243ebfedea0SLionel Sambuc BIO_printf(bio_err, " -modulus print the RSA key modulus\n");
244ebfedea0SLionel Sambuc BIO_printf(bio_err, " -check verify key consistency\n");
245*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
246*0a6a1f1dSLionel Sambuc " -pubin expect a public key in input file\n");
247ebfedea0SLionel Sambuc BIO_printf(bio_err, " -pubout output a public key\n");
248ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
249*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
250*0a6a1f1dSLionel Sambuc " -engine e use engine e, possibly a hardware device.\n");
251ebfedea0SLionel Sambuc # endif
252ebfedea0SLionel Sambuc goto end;
253ebfedea0SLionel Sambuc }
254ebfedea0SLionel Sambuc
255ebfedea0SLionel Sambuc ERR_load_crypto_strings();
256ebfedea0SLionel Sambuc
257ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
258ebfedea0SLionel Sambuc e = setup_engine(bio_err, engine, 0);
259ebfedea0SLionel Sambuc # endif
260ebfedea0SLionel Sambuc
261ebfedea0SLionel Sambuc if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
262ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error getting passwords\n");
263ebfedea0SLionel Sambuc goto end;
264ebfedea0SLionel Sambuc }
265ebfedea0SLionel Sambuc
266ebfedea0SLionel Sambuc if (check && pubin) {
267ebfedea0SLionel Sambuc BIO_printf(bio_err, "Only private keys can be checked\n");
268ebfedea0SLionel Sambuc goto end;
269ebfedea0SLionel Sambuc }
270ebfedea0SLionel Sambuc
271ebfedea0SLionel Sambuc out = BIO_new(BIO_s_file());
272ebfedea0SLionel Sambuc
273ebfedea0SLionel Sambuc {
274ebfedea0SLionel Sambuc EVP_PKEY *pkey;
275ebfedea0SLionel Sambuc
276*0a6a1f1dSLionel Sambuc if (pubin) {
277ebfedea0SLionel Sambuc int tmpformat = -1;
278*0a6a1f1dSLionel Sambuc if (pubin == 2) {
279ebfedea0SLionel Sambuc if (informat == FORMAT_PEM)
280ebfedea0SLionel Sambuc tmpformat = FORMAT_PEMRSA;
281ebfedea0SLionel Sambuc else if (informat == FORMAT_ASN1)
282ebfedea0SLionel Sambuc tmpformat = FORMAT_ASN1RSA;
283*0a6a1f1dSLionel Sambuc } else if (informat == FORMAT_NETSCAPE && sgckey)
284ebfedea0SLionel Sambuc tmpformat = FORMAT_IISSGC;
285ebfedea0SLionel Sambuc else
286ebfedea0SLionel Sambuc tmpformat = informat;
287ebfedea0SLionel Sambuc
288ebfedea0SLionel Sambuc pkey = load_pubkey(bio_err, infile, tmpformat, 1,
289ebfedea0SLionel Sambuc passin, e, "Public Key");
290*0a6a1f1dSLionel Sambuc } else
291ebfedea0SLionel Sambuc pkey = load_key(bio_err, infile,
292ebfedea0SLionel Sambuc (informat == FORMAT_NETSCAPE && sgckey ?
293ebfedea0SLionel Sambuc FORMAT_IISSGC : informat), 1,
294ebfedea0SLionel Sambuc passin, e, "Private Key");
295ebfedea0SLionel Sambuc
296ebfedea0SLionel Sambuc if (pkey != NULL)
297ebfedea0SLionel Sambuc rsa = EVP_PKEY_get1_RSA(pkey);
298ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
299ebfedea0SLionel Sambuc }
300ebfedea0SLionel Sambuc
301*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
302ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
303ebfedea0SLionel Sambuc goto end;
304ebfedea0SLionel Sambuc }
305ebfedea0SLionel Sambuc
306*0a6a1f1dSLionel Sambuc if (outfile == NULL) {
307ebfedea0SLionel Sambuc BIO_set_fp(out, stdout, BIO_NOCLOSE);
308ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
309ebfedea0SLionel Sambuc {
310ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
311ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
312ebfedea0SLionel Sambuc }
313ebfedea0SLionel Sambuc # endif
314*0a6a1f1dSLionel Sambuc } else {
315*0a6a1f1dSLionel Sambuc if (BIO_write_filename(out, outfile) <= 0) {
316ebfedea0SLionel Sambuc perror(outfile);
317ebfedea0SLionel Sambuc goto end;
318ebfedea0SLionel Sambuc }
319ebfedea0SLionel Sambuc }
320ebfedea0SLionel Sambuc
321ebfedea0SLionel Sambuc if (text)
322*0a6a1f1dSLionel Sambuc if (!RSA_print(out, rsa, 0)) {
323ebfedea0SLionel Sambuc perror(outfile);
324ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
325ebfedea0SLionel Sambuc goto end;
326ebfedea0SLionel Sambuc }
327ebfedea0SLionel Sambuc
328*0a6a1f1dSLionel Sambuc if (modulus) {
329ebfedea0SLionel Sambuc BIO_printf(out, "Modulus=");
330ebfedea0SLionel Sambuc BN_print(out, rsa->n);
331ebfedea0SLionel Sambuc BIO_printf(out, "\n");
332ebfedea0SLionel Sambuc }
333ebfedea0SLionel Sambuc
334*0a6a1f1dSLionel Sambuc if (check) {
335ebfedea0SLionel Sambuc int r = RSA_check_key(rsa);
336ebfedea0SLionel Sambuc
337ebfedea0SLionel Sambuc if (r == 1)
338ebfedea0SLionel Sambuc BIO_printf(out, "RSA key ok\n");
339*0a6a1f1dSLionel Sambuc else if (r == 0) {
340ebfedea0SLionel Sambuc unsigned long err;
341ebfedea0SLionel Sambuc
342ebfedea0SLionel Sambuc while ((err = ERR_peek_error()) != 0 &&
343ebfedea0SLionel Sambuc ERR_GET_LIB(err) == ERR_LIB_RSA &&
344ebfedea0SLionel Sambuc ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
345*0a6a1f1dSLionel Sambuc ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
346*0a6a1f1dSLionel Sambuc BIO_printf(out, "RSA key error: %s\n",
347*0a6a1f1dSLionel Sambuc ERR_reason_error_string(err));
348ebfedea0SLionel Sambuc ERR_get_error(); /* remove e from error stack */
349ebfedea0SLionel Sambuc }
350ebfedea0SLionel Sambuc }
351ebfedea0SLionel Sambuc
352*0a6a1f1dSLionel Sambuc if (r == -1 || ERR_peek_error() != 0) { /* should happen only if r ==
353*0a6a1f1dSLionel Sambuc * -1 */
354ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
355ebfedea0SLionel Sambuc goto end;
356ebfedea0SLionel Sambuc }
357ebfedea0SLionel Sambuc }
358ebfedea0SLionel Sambuc
359*0a6a1f1dSLionel Sambuc if (noout) {
360ebfedea0SLionel Sambuc ret = 0;
361ebfedea0SLionel Sambuc goto end;
362ebfedea0SLionel Sambuc }
363ebfedea0SLionel Sambuc BIO_printf(bio_err, "writing RSA key\n");
364ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1) {
365*0a6a1f1dSLionel Sambuc if (pubout || pubin) {
366ebfedea0SLionel Sambuc if (pubout == 2)
367ebfedea0SLionel Sambuc i = i2d_RSAPublicKey_bio(out, rsa);
368ebfedea0SLionel Sambuc else
369ebfedea0SLionel Sambuc i = i2d_RSA_PUBKEY_bio(out, rsa);
370*0a6a1f1dSLionel Sambuc } else
371*0a6a1f1dSLionel Sambuc i = i2d_RSAPrivateKey_bio(out, rsa);
372ebfedea0SLionel Sambuc }
373ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RC4
374*0a6a1f1dSLionel Sambuc else if (outformat == FORMAT_NETSCAPE) {
375ebfedea0SLionel Sambuc unsigned char *p, *pp;
376ebfedea0SLionel Sambuc int size;
377ebfedea0SLionel Sambuc
378ebfedea0SLionel Sambuc i = 1;
379ebfedea0SLionel Sambuc size = i2d_RSA_NET(rsa, NULL, NULL, sgckey);
380*0a6a1f1dSLionel Sambuc if ((p = (unsigned char *)OPENSSL_malloc(size)) == NULL) {
381ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
382ebfedea0SLionel Sambuc goto end;
383ebfedea0SLionel Sambuc }
384ebfedea0SLionel Sambuc pp = p;
385ebfedea0SLionel Sambuc i2d_RSA_NET(rsa, &p, NULL, sgckey);
386ebfedea0SLionel Sambuc BIO_write(out, (char *)pp, size);
387ebfedea0SLionel Sambuc OPENSSL_free(pp);
388ebfedea0SLionel Sambuc }
389ebfedea0SLionel Sambuc # endif
390ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM) {
391*0a6a1f1dSLionel Sambuc if (pubout || pubin) {
392ebfedea0SLionel Sambuc if (pubout == 2)
393ebfedea0SLionel Sambuc i = PEM_write_bio_RSAPublicKey(out, rsa);
394ebfedea0SLionel Sambuc else
395ebfedea0SLionel Sambuc i = PEM_write_bio_RSA_PUBKEY(out, rsa);
396*0a6a1f1dSLionel Sambuc } else
397*0a6a1f1dSLionel Sambuc i = PEM_write_bio_RSAPrivateKey(out, rsa,
398ebfedea0SLionel Sambuc enc, NULL, 0, NULL, passout);
399ebfedea0SLionel Sambuc # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
400ebfedea0SLionel Sambuc } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
401ebfedea0SLionel Sambuc EVP_PKEY *pk;
402ebfedea0SLionel Sambuc pk = EVP_PKEY_new();
403ebfedea0SLionel Sambuc EVP_PKEY_set1_RSA(pk, rsa);
404ebfedea0SLionel Sambuc if (outformat == FORMAT_PVK)
405ebfedea0SLionel Sambuc i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
406ebfedea0SLionel Sambuc else if (pubin || pubout)
407ebfedea0SLionel Sambuc i = i2b_PublicKey_bio(out, pk);
408ebfedea0SLionel Sambuc else
409ebfedea0SLionel Sambuc i = i2b_PrivateKey_bio(out, pk);
410ebfedea0SLionel Sambuc EVP_PKEY_free(pk);
411ebfedea0SLionel Sambuc # endif
412ebfedea0SLionel Sambuc } else {
413ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad output format specified for outfile\n");
414ebfedea0SLionel Sambuc goto end;
415ebfedea0SLionel Sambuc }
416*0a6a1f1dSLionel Sambuc if (i <= 0) {
417ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to write key\n");
418ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
419*0a6a1f1dSLionel Sambuc } else
420ebfedea0SLionel Sambuc ret = 0;
421ebfedea0SLionel Sambuc end:
422*0a6a1f1dSLionel Sambuc if (out != NULL)
423*0a6a1f1dSLionel Sambuc BIO_free_all(out);
424*0a6a1f1dSLionel Sambuc if (rsa != NULL)
425*0a6a1f1dSLionel Sambuc RSA_free(rsa);
426*0a6a1f1dSLionel Sambuc if (passin)
427*0a6a1f1dSLionel Sambuc OPENSSL_free(passin);
428*0a6a1f1dSLionel Sambuc if (passout)
429*0a6a1f1dSLionel Sambuc OPENSSL_free(passout);
430ebfedea0SLionel Sambuc apps_shutdown();
431ebfedea0SLionel Sambuc OPENSSL_EXIT(ret);
432ebfedea0SLionel Sambuc }
433ebfedea0SLionel Sambuc #else /* !OPENSSL_NO_RSA */
434ebfedea0SLionel Sambuc
435ebfedea0SLionel Sambuc # if PEDANTIC
436ebfedea0SLionel Sambuc static void *dummy = &dummy;
437ebfedea0SLionel Sambuc # endif
438ebfedea0SLionel Sambuc
439ebfedea0SLionel Sambuc #endif
440