155714Skris/* apps/rsa.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296465Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59160814Ssimon#include <openssl/opensslconf.h>
60109998Smarkm#ifndef OPENSSL_NO_RSA
61296465Sdelphij# include <stdio.h>
62296465Sdelphij# include <stdlib.h>
63296465Sdelphij# include <string.h>
64296465Sdelphij# include <time.h>
65296465Sdelphij# include "apps.h"
66296465Sdelphij# include <openssl/bio.h>
67296465Sdelphij# include <openssl/err.h>
68296465Sdelphij# include <openssl/rsa.h>
69296465Sdelphij# include <openssl/evp.h>
70296465Sdelphij# include <openssl/x509.h>
71296465Sdelphij# include <openssl/pem.h>
72296465Sdelphij# include <openssl/bn.h>
7355714Skris
74296465Sdelphij# undef PROG
75296465Sdelphij# define PROG    rsa_main
7655714Skris
77296465Sdelphij/*-
78296465Sdelphij * -inform arg  - input format - default PEM (one of DER, NET or PEM)
7955714Skris * -outform arg - output format - default PEM
80296465Sdelphij * -in arg      - input file - default stdin
81296465Sdelphij * -out arg     - output file - default stdout
82296465Sdelphij * -des         - encrypt output if PEM format with DES in cbc mode
83296465Sdelphij * -des3        - encrypt output if PEM format
84296465Sdelphij * -idea        - encrypt output if PEM format
85296465Sdelphij * -seed        - encrypt output if PEM format
86296465Sdelphij * -aes128      - encrypt output if PEM format
87296465Sdelphij * -aes192      - encrypt output if PEM format
88296465Sdelphij * -aes256      - encrypt output if PEM format
89162911Ssimon * -camellia128 - encrypt output if PEM format
90162911Ssimon * -camellia192 - encrypt output if PEM format
91162911Ssimon * -camellia256 - encrypt output if PEM format
92296465Sdelphij * -text        - print a text version
93296465Sdelphij * -modulus     - print the RSA key modulus
94296465Sdelphij * -check       - verify key consistency
95296465Sdelphij * -pubin       - Expect a public key in input file.
96296465Sdelphij * -pubout      - Output a public key.
9755714Skris */
9855714Skris
9959191Skrisint MAIN(int, char **);
10059191Skris
10155714Skrisint MAIN(int argc, char **argv)
102296465Sdelphij{
103296465Sdelphij    ENGINE *e = NULL;
104296465Sdelphij    int ret = 1;
105296465Sdelphij    RSA *rsa = NULL;
106296465Sdelphij    int i, badops = 0, sgckey = 0;
107296465Sdelphij    const EVP_CIPHER *enc = NULL;
108296465Sdelphij    BIO *out = NULL;
109296465Sdelphij    int informat, outformat, text = 0, check = 0, noout = 0;
110296465Sdelphij    int pubin = 0, pubout = 0;
111296465Sdelphij    char *infile, *outfile, *prog;
112296465Sdelphij    char *passargin = NULL, *passargout = NULL;
113296465Sdelphij    char *passin = NULL, *passout = NULL;
114296465Sdelphij# ifndef OPENSSL_NO_ENGINE
115296465Sdelphij    char *engine = NULL;
116296465Sdelphij# endif
117296465Sdelphij    int modulus = 0;
11855714Skris
119296465Sdelphij    apps_startup();
12055714Skris
121296465Sdelphij    if (bio_err == NULL)
122296465Sdelphij        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
123296465Sdelphij            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
12455714Skris
125296465Sdelphij    if (!load_config(bio_err, NULL))
126296465Sdelphij        goto end;
127109998Smarkm
128296465Sdelphij    infile = NULL;
129296465Sdelphij    outfile = NULL;
130296465Sdelphij    informat = FORMAT_PEM;
131296465Sdelphij    outformat = FORMAT_PEM;
13255714Skris
133296465Sdelphij    prog = argv[0];
134296465Sdelphij    argc--;
135296465Sdelphij    argv++;
136296465Sdelphij    while (argc >= 1) {
137296465Sdelphij        if (strcmp(*argv, "-inform") == 0) {
138296465Sdelphij            if (--argc < 1)
139296465Sdelphij                goto bad;
140296465Sdelphij            informat = str2fmt(*(++argv));
141296465Sdelphij        } else if (strcmp(*argv, "-outform") == 0) {
142296465Sdelphij            if (--argc < 1)
143296465Sdelphij                goto bad;
144296465Sdelphij            outformat = str2fmt(*(++argv));
145296465Sdelphij        } else if (strcmp(*argv, "-in") == 0) {
146296465Sdelphij            if (--argc < 1)
147296465Sdelphij                goto bad;
148296465Sdelphij            infile = *(++argv);
149296465Sdelphij        } else if (strcmp(*argv, "-out") == 0) {
150296465Sdelphij            if (--argc < 1)
151296465Sdelphij                goto bad;
152296465Sdelphij            outfile = *(++argv);
153296465Sdelphij        } else if (strcmp(*argv, "-passin") == 0) {
154296465Sdelphij            if (--argc < 1)
155296465Sdelphij                goto bad;
156296465Sdelphij            passargin = *(++argv);
157296465Sdelphij        } else if (strcmp(*argv, "-passout") == 0) {
158296465Sdelphij            if (--argc < 1)
159296465Sdelphij                goto bad;
160296465Sdelphij            passargout = *(++argv);
161296465Sdelphij        }
162296465Sdelphij# ifndef OPENSSL_NO_ENGINE
163296465Sdelphij        else if (strcmp(*argv, "-engine") == 0) {
164296465Sdelphij            if (--argc < 1)
165296465Sdelphij                goto bad;
166296465Sdelphij            engine = *(++argv);
167296465Sdelphij        }
168296465Sdelphij# endif
169296465Sdelphij        else if (strcmp(*argv, "-sgckey") == 0)
170296465Sdelphij            sgckey = 1;
171296465Sdelphij        else if (strcmp(*argv, "-pubin") == 0)
172296465Sdelphij            pubin = 1;
173296465Sdelphij        else if (strcmp(*argv, "-pubout") == 0)
174296465Sdelphij            pubout = 1;
175296465Sdelphij        else if (strcmp(*argv, "-noout") == 0)
176296465Sdelphij            noout = 1;
177296465Sdelphij        else if (strcmp(*argv, "-text") == 0)
178296465Sdelphij            text = 1;
179296465Sdelphij        else if (strcmp(*argv, "-modulus") == 0)
180296465Sdelphij            modulus = 1;
181296465Sdelphij        else if (strcmp(*argv, "-check") == 0)
182296465Sdelphij            check = 1;
183296465Sdelphij        else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
184296465Sdelphij            BIO_printf(bio_err, "unknown option %s\n", *argv);
185296465Sdelphij            badops = 1;
186296465Sdelphij            break;
187296465Sdelphij        }
188296465Sdelphij        argc--;
189296465Sdelphij        argv++;
190296465Sdelphij    }
19155714Skris
192296465Sdelphij    if (badops) {
193296465Sdelphij bad:
194296465Sdelphij        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
195296465Sdelphij        BIO_printf(bio_err, "where options are\n");
196296465Sdelphij        BIO_printf(bio_err,
197296465Sdelphij                   " -inform arg     input format - one of DER NET PEM\n");
198296465Sdelphij        BIO_printf(bio_err,
199296465Sdelphij                   " -outform arg    output format - one of DER NET PEM\n");
200296465Sdelphij        BIO_printf(bio_err, " -in arg         input file\n");
201296465Sdelphij        BIO_printf(bio_err, " -sgckey         Use IIS SGC key format\n");
202296465Sdelphij        BIO_printf(bio_err,
203296465Sdelphij                   " -passin arg     input file pass phrase source\n");
204296465Sdelphij        BIO_printf(bio_err, " -out arg        output file\n");
205296465Sdelphij        BIO_printf(bio_err,
206296465Sdelphij                   " -passout arg    output file pass phrase source\n");
207296465Sdelphij        BIO_printf(bio_err,
208296465Sdelphij                   " -des            encrypt PEM output with cbc des\n");
209296465Sdelphij        BIO_printf(bio_err,
210296465Sdelphij                   " -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
211296465Sdelphij# ifndef OPENSSL_NO_IDEA
212296465Sdelphij        BIO_printf(bio_err,
213296465Sdelphij                   " -idea           encrypt PEM output with cbc idea\n");
214296465Sdelphij# endif
215296465Sdelphij# ifndef OPENSSL_NO_SEED
216296465Sdelphij        BIO_printf(bio_err,
217296465Sdelphij                   " -seed           encrypt PEM output with cbc seed\n");
218296465Sdelphij# endif
219296465Sdelphij# ifndef OPENSSL_NO_AES
220296465Sdelphij        BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
221296465Sdelphij        BIO_printf(bio_err,
222296465Sdelphij                   "                 encrypt PEM output with cbc aes\n");
223296465Sdelphij# endif
224296465Sdelphij# ifndef OPENSSL_NO_CAMELLIA
225296465Sdelphij        BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
226296465Sdelphij        BIO_printf(bio_err,
227296465Sdelphij                   "                 encrypt PEM output with cbc camellia\n");
228296465Sdelphij# endif
229296465Sdelphij        BIO_printf(bio_err, " -text           print the key in text\n");
230296465Sdelphij        BIO_printf(bio_err, " -noout          don't print key out\n");
231296465Sdelphij        BIO_printf(bio_err, " -modulus        print the RSA key modulus\n");
232296465Sdelphij        BIO_printf(bio_err, " -check          verify key consistency\n");
233296465Sdelphij        BIO_printf(bio_err,
234296465Sdelphij                   " -pubin          expect a public key in input file\n");
235296465Sdelphij        BIO_printf(bio_err, " -pubout         output a public key\n");
236296465Sdelphij# ifndef OPENSSL_NO_ENGINE
237296465Sdelphij        BIO_printf(bio_err,
238296465Sdelphij                   " -engine e       use engine e, possibly a hardware device.\n");
239296465Sdelphij# endif
240296465Sdelphij        goto end;
241296465Sdelphij    }
24255714Skris
243296465Sdelphij    ERR_load_crypto_strings();
24455714Skris
245296465Sdelphij# ifndef OPENSSL_NO_ENGINE
246296465Sdelphij    e = setup_engine(bio_err, engine, 0);
247296465Sdelphij# endif
248109998Smarkm
249296465Sdelphij    if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
250296465Sdelphij        BIO_printf(bio_err, "Error getting passwords\n");
251296465Sdelphij        goto end;
252296465Sdelphij    }
25359191Skris
254296465Sdelphij    if (check && pubin) {
255296465Sdelphij        BIO_printf(bio_err, "Only private keys can be checked\n");
256296465Sdelphij        goto end;
257296465Sdelphij    }
25859191Skris
259296465Sdelphij    out = BIO_new(BIO_s_file());
26055714Skris
261296465Sdelphij    {
262296465Sdelphij        EVP_PKEY *pkey;
26355714Skris
264296465Sdelphij        if (pubin)
265296465Sdelphij            pkey = load_pubkey(bio_err, infile,
266296465Sdelphij                               (informat == FORMAT_NETSCAPE && sgckey ?
267296465Sdelphij                                FORMAT_IISSGC : informat), 1,
268296465Sdelphij                               passin, e, "Public Key");
269296465Sdelphij        else
270296465Sdelphij            pkey = load_key(bio_err, infile,
271296465Sdelphij                            (informat == FORMAT_NETSCAPE && sgckey ?
272296465Sdelphij                             FORMAT_IISSGC : informat), 1,
273296465Sdelphij                            passin, e, "Private Key");
274109998Smarkm
275296465Sdelphij        if (pkey != NULL)
276296465Sdelphij            rsa = pkey == NULL ? NULL : EVP_PKEY_get1_RSA(pkey);
277296465Sdelphij        EVP_PKEY_free(pkey);
278296465Sdelphij    }
27955714Skris
280296465Sdelphij    if (rsa == NULL) {
281296465Sdelphij        ERR_print_errors(bio_err);
282296465Sdelphij        goto end;
283296465Sdelphij    }
28455714Skris
285296465Sdelphij    if (outfile == NULL) {
286296465Sdelphij        BIO_set_fp(out, stdout, BIO_NOCLOSE);
287296465Sdelphij# ifdef OPENSSL_SYS_VMS
288296465Sdelphij        {
289296465Sdelphij            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
290296465Sdelphij            out = BIO_push(tmpbio, out);
291296465Sdelphij        }
292296465Sdelphij# endif
293296465Sdelphij    } else {
294296465Sdelphij        if (BIO_write_filename(out, outfile) <= 0) {
295296465Sdelphij            perror(outfile);
296296465Sdelphij            goto end;
297296465Sdelphij        }
298296465Sdelphij    }
29955714Skris
300296465Sdelphij    if (text)
301296465Sdelphij        if (!RSA_print(out, rsa, 0)) {
302296465Sdelphij            perror(outfile);
303296465Sdelphij            ERR_print_errors(bio_err);
304296465Sdelphij            goto end;
305296465Sdelphij        }
30655714Skris
307296465Sdelphij    if (modulus) {
308296465Sdelphij        BIO_printf(out, "Modulus=");
309296465Sdelphij        BN_print(out, rsa->n);
310296465Sdelphij        BIO_printf(out, "\n");
311296465Sdelphij    }
31255714Skris
313296465Sdelphij    if (check) {
314296465Sdelphij        int r = RSA_check_key(rsa);
31555714Skris
316296465Sdelphij        if (r == 1)
317296465Sdelphij            BIO_printf(out, "RSA key ok\n");
318296465Sdelphij        else if (r == 0) {
319296465Sdelphij            unsigned long err;
32055714Skris
321296465Sdelphij            while ((err = ERR_peek_error()) != 0 &&
322296465Sdelphij                   ERR_GET_LIB(err) == ERR_LIB_RSA &&
323296465Sdelphij                   ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
324296465Sdelphij                   ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
325296465Sdelphij                BIO_printf(out, "RSA key error: %s\n",
326296465Sdelphij                           ERR_reason_error_string(err));
327296465Sdelphij                ERR_get_error(); /* remove e from error stack */
328296465Sdelphij            }
329296465Sdelphij        }
33055714Skris
331296465Sdelphij        if (r == -1 || ERR_peek_error() != 0) { /* should happen only if r ==
332296465Sdelphij                                                 * -1 */
333296465Sdelphij            ERR_print_errors(bio_err);
334296465Sdelphij            goto end;
335296465Sdelphij        }
336296465Sdelphij    }
33759191Skris
338296465Sdelphij    if (noout) {
339296465Sdelphij        ret = 0;
340296465Sdelphij        goto end;
341296465Sdelphij    }
342296465Sdelphij    BIO_printf(bio_err, "writing RSA key\n");
343296465Sdelphij    if (outformat == FORMAT_ASN1) {
344296465Sdelphij        if (pubout || pubin)
345296465Sdelphij            i = i2d_RSA_PUBKEY_bio(out, rsa);
346296465Sdelphij        else
347296465Sdelphij            i = i2d_RSAPrivateKey_bio(out, rsa);
348296465Sdelphij    }
349296465Sdelphij# ifndef OPENSSL_NO_RC4
350296465Sdelphij    else if (outformat == FORMAT_NETSCAPE) {
351296465Sdelphij        unsigned char *p, *pp;
352296465Sdelphij        int size;
353296465Sdelphij
354296465Sdelphij        i = 1;
355296465Sdelphij        size = i2d_RSA_NET(rsa, NULL, NULL, sgckey);
356296465Sdelphij        if ((p = (unsigned char *)OPENSSL_malloc(size)) == NULL) {
357296465Sdelphij            BIO_printf(bio_err, "Memory allocation failure\n");
358296465Sdelphij            goto end;
359296465Sdelphij        }
360296465Sdelphij        pp = p;
361296465Sdelphij        i2d_RSA_NET(rsa, &p, NULL, sgckey);
362296465Sdelphij        BIO_write(out, (char *)pp, size);
363296465Sdelphij        OPENSSL_free(pp);
364296465Sdelphij    }
365296465Sdelphij# endif
366296465Sdelphij    else if (outformat == FORMAT_PEM) {
367296465Sdelphij        if (pubout || pubin)
368296465Sdelphij            i = PEM_write_bio_RSA_PUBKEY(out, rsa);
369296465Sdelphij        else
370296465Sdelphij            i = PEM_write_bio_RSAPrivateKey(out, rsa,
371296465Sdelphij                                            enc, NULL, 0, NULL, passout);
372296465Sdelphij    } else {
373296465Sdelphij        BIO_printf(bio_err, "bad output format specified for outfile\n");
374296465Sdelphij        goto end;
375296465Sdelphij    }
376296465Sdelphij    if (!i) {
377296465Sdelphij        BIO_printf(bio_err, "unable to write key\n");
378296465Sdelphij        ERR_print_errors(bio_err);
379296465Sdelphij    } else
380296465Sdelphij        ret = 0;
381296465Sdelphij end:
382296465Sdelphij    if (out != NULL)
383296465Sdelphij        BIO_free_all(out);
384296465Sdelphij    if (rsa != NULL)
385296465Sdelphij        RSA_free(rsa);
386296465Sdelphij    if (passin)
387296465Sdelphij        OPENSSL_free(passin);
388296465Sdelphij    if (passout)
389296465Sdelphij        OPENSSL_free(passout);
390296465Sdelphij    apps_shutdown();
391296465Sdelphij    OPENSSL_EXIT(ret);
392296465Sdelphij}
393296465Sdelphij#else                           /* !OPENSSL_NO_RSA */
394296465Sdelphij
39559191Skris# if PEDANTIC
396296465Sdelphijstatic void *dummy = &dummy;
39759191Skris# endif
39859191Skris
39955714Skris#endif
400