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.
8280297Sjkim *
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).
15280297Sjkim *
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.
22280297Sjkim *
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 :-).
37280297Sjkim * 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)"
40280297Sjkim *
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.
52280297Sjkim *
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
61280297Sjkim# include <stdio.h>
62280297Sjkim# include <stdlib.h>
63280297Sjkim# include <string.h>
64280297Sjkim# include <time.h>
65280297Sjkim# include "apps.h"
66280297Sjkim# include <openssl/bio.h>
67280297Sjkim# include <openssl/err.h>
68280297Sjkim# include <openssl/rsa.h>
69280297Sjkim# include <openssl/evp.h>
70280297Sjkim# include <openssl/x509.h>
71280297Sjkim# include <openssl/pem.h>
72280297Sjkim# include <openssl/bn.h>
7355714Skris
74280297Sjkim# undef PROG
75280297Sjkim# define PROG    rsa_main
7655714Skris
77280297Sjkim/*-
78280297Sjkim * -inform arg  - input format - default PEM (one of DER, NET or PEM)
7955714Skris * -outform arg - output format - default PEM
80280297Sjkim * -in arg      - input file - default stdin
81280297Sjkim * -out arg     - output file - default stdout
82280297Sjkim * -des         - encrypt output if PEM format with DES in cbc mode
83280297Sjkim * -des3        - encrypt output if PEM format
84280297Sjkim * -idea        - encrypt output if PEM format
85280297Sjkim * -seed        - encrypt output if PEM format
86280297Sjkim * -aes128      - encrypt output if PEM format
87280297Sjkim * -aes192      - encrypt output if PEM format
88280297Sjkim * -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
92280297Sjkim * -text        - print a text version
93280297Sjkim * -modulus     - print the RSA key modulus
94280297Sjkim * -check       - verify key consistency
95280297Sjkim * -pubin       - Expect a public key in input file.
96280297Sjkim * -pubout      - Output a public key.
9755714Skris */
9855714Skris
9959191Skrisint MAIN(int, char **);
10059191Skris
10155714Skrisint MAIN(int argc, char **argv)
102280297Sjkim{
103280297Sjkim    ENGINE *e = NULL;
104280297Sjkim    int ret = 1;
105280297Sjkim    RSA *rsa = NULL;
106280297Sjkim    int i, badops = 0, sgckey = 0;
107280297Sjkim    const EVP_CIPHER *enc = NULL;
108280297Sjkim    BIO *out = NULL;
109280297Sjkim    int informat, outformat, text = 0, check = 0, noout = 0;
110280297Sjkim    int pubin = 0, pubout = 0;
111280297Sjkim    char *infile, *outfile, *prog;
112280297Sjkim    char *passargin = NULL, *passargout = NULL;
113280297Sjkim    char *passin = NULL, *passout = NULL;
114280297Sjkim    char *engine = NULL;
115280297Sjkim    int modulus = 0;
11655714Skris
117280297Sjkim    int pvk_encr = 2;
118238405Sjkim
119280297Sjkim    apps_startup();
12055714Skris
121280297Sjkim    if (bio_err == NULL)
122280297Sjkim        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
123280297Sjkim            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
12455714Skris
125280297Sjkim    if (!load_config(bio_err, NULL))
126280297Sjkim        goto end;
127109998Smarkm
128280297Sjkim    infile = NULL;
129280297Sjkim    outfile = NULL;
130280297Sjkim    informat = FORMAT_PEM;
131280297Sjkim    outformat = FORMAT_PEM;
13255714Skris
133280297Sjkim    prog = argv[0];
134280297Sjkim    argc--;
135280297Sjkim    argv++;
136280297Sjkim    while (argc >= 1) {
137280297Sjkim        if (strcmp(*argv, "-inform") == 0) {
138280297Sjkim            if (--argc < 1)
139280297Sjkim                goto bad;
140280297Sjkim            informat = str2fmt(*(++argv));
141280297Sjkim        } else if (strcmp(*argv, "-outform") == 0) {
142280297Sjkim            if (--argc < 1)
143280297Sjkim                goto bad;
144280297Sjkim            outformat = str2fmt(*(++argv));
145280297Sjkim        } else if (strcmp(*argv, "-in") == 0) {
146280297Sjkim            if (--argc < 1)
147280297Sjkim                goto bad;
148280297Sjkim            infile = *(++argv);
149280297Sjkim        } else if (strcmp(*argv, "-out") == 0) {
150280297Sjkim            if (--argc < 1)
151280297Sjkim                goto bad;
152280297Sjkim            outfile = *(++argv);
153280297Sjkim        } else if (strcmp(*argv, "-passin") == 0) {
154280297Sjkim            if (--argc < 1)
155280297Sjkim                goto bad;
156280297Sjkim            passargin = *(++argv);
157280297Sjkim        } else if (strcmp(*argv, "-passout") == 0) {
158280297Sjkim            if (--argc < 1)
159280297Sjkim                goto bad;
160280297Sjkim            passargout = *(++argv);
161280297Sjkim        }
162280297Sjkim# ifndef OPENSSL_NO_ENGINE
163280297Sjkim        else if (strcmp(*argv, "-engine") == 0) {
164280297Sjkim            if (--argc < 1)
165280297Sjkim                goto bad;
166280297Sjkim            engine = *(++argv);
167280297Sjkim        }
168280297Sjkim# endif
169280297Sjkim        else if (strcmp(*argv, "-sgckey") == 0)
170280297Sjkim            sgckey = 1;
171280297Sjkim        else if (strcmp(*argv, "-pubin") == 0)
172280297Sjkim            pubin = 1;
173280297Sjkim        else if (strcmp(*argv, "-pubout") == 0)
174280297Sjkim            pubout = 1;
175280297Sjkim        else if (strcmp(*argv, "-RSAPublicKey_in") == 0)
176280297Sjkim            pubin = 2;
177280297Sjkim        else if (strcmp(*argv, "-RSAPublicKey_out") == 0)
178280297Sjkim            pubout = 2;
179280297Sjkim        else if (strcmp(*argv, "-pvk-strong") == 0)
180280297Sjkim            pvk_encr = 2;
181280297Sjkim        else if (strcmp(*argv, "-pvk-weak") == 0)
182280297Sjkim            pvk_encr = 1;
183280297Sjkim        else if (strcmp(*argv, "-pvk-none") == 0)
184280297Sjkim            pvk_encr = 0;
185280297Sjkim        else if (strcmp(*argv, "-noout") == 0)
186280297Sjkim            noout = 1;
187280297Sjkim        else if (strcmp(*argv, "-text") == 0)
188280297Sjkim            text = 1;
189280297Sjkim        else if (strcmp(*argv, "-modulus") == 0)
190280297Sjkim            modulus = 1;
191280297Sjkim        else if (strcmp(*argv, "-check") == 0)
192280297Sjkim            check = 1;
193280297Sjkim        else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
194280297Sjkim            BIO_printf(bio_err, "unknown option %s\n", *argv);
195280297Sjkim            badops = 1;
196280297Sjkim            break;
197280297Sjkim        }
198280297Sjkim        argc--;
199280297Sjkim        argv++;
200280297Sjkim    }
20155714Skris
202280297Sjkim    if (badops) {
203280297Sjkim bad:
204280297Sjkim        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
205280297Sjkim        BIO_printf(bio_err, "where options are\n");
206280297Sjkim        BIO_printf(bio_err,
207280297Sjkim                   " -inform arg     input format - one of DER NET PEM\n");
208280297Sjkim        BIO_printf(bio_err,
209280297Sjkim                   " -outform arg    output format - one of DER NET PEM\n");
210280297Sjkim        BIO_printf(bio_err, " -in arg         input file\n");
211280297Sjkim        BIO_printf(bio_err, " -sgckey         Use IIS SGC key format\n");
212280297Sjkim        BIO_printf(bio_err,
213280297Sjkim                   " -passin arg     input file pass phrase source\n");
214280297Sjkim        BIO_printf(bio_err, " -out arg        output file\n");
215280297Sjkim        BIO_printf(bio_err,
216280297Sjkim                   " -passout arg    output file pass phrase source\n");
217280297Sjkim        BIO_printf(bio_err,
218280297Sjkim                   " -des            encrypt PEM output with cbc des\n");
219280297Sjkim        BIO_printf(bio_err,
220280297Sjkim                   " -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
221280297Sjkim# ifndef OPENSSL_NO_IDEA
222280297Sjkim        BIO_printf(bio_err,
223280297Sjkim                   " -idea           encrypt PEM output with cbc idea\n");
224280297Sjkim# endif
225280297Sjkim# ifndef OPENSSL_NO_SEED
226280297Sjkim        BIO_printf(bio_err,
227280297Sjkim                   " -seed           encrypt PEM output with cbc seed\n");
228280297Sjkim# endif
229280297Sjkim# ifndef OPENSSL_NO_AES
230280297Sjkim        BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
231280297Sjkim        BIO_printf(bio_err,
232280297Sjkim                   "                 encrypt PEM output with cbc aes\n");
233280297Sjkim# endif
234280297Sjkim# ifndef OPENSSL_NO_CAMELLIA
235280297Sjkim        BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
236280297Sjkim        BIO_printf(bio_err,
237280297Sjkim                   "                 encrypt PEM output with cbc camellia\n");
238280297Sjkim# endif
239280297Sjkim        BIO_printf(bio_err, " -text           print the key in text\n");
240280297Sjkim        BIO_printf(bio_err, " -noout          don't print key out\n");
241280297Sjkim        BIO_printf(bio_err, " -modulus        print the RSA key modulus\n");
242280297Sjkim        BIO_printf(bio_err, " -check          verify key consistency\n");
243280297Sjkim        BIO_printf(bio_err,
244280297Sjkim                   " -pubin          expect a public key in input file\n");
245280297Sjkim        BIO_printf(bio_err, " -pubout         output a public key\n");
246280297Sjkim# ifndef OPENSSL_NO_ENGINE
247280297Sjkim        BIO_printf(bio_err,
248280297Sjkim                   " -engine e       use engine e, possibly a hardware device.\n");
249280297Sjkim# endif
250280297Sjkim        goto end;
251280297Sjkim    }
25255714Skris
253280297Sjkim    ERR_load_crypto_strings();
25455714Skris
255280297Sjkim    e = setup_engine(bio_err, engine, 0);
256109998Smarkm
257280297Sjkim    if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
258280297Sjkim        BIO_printf(bio_err, "Error getting passwords\n");
259280297Sjkim        goto end;
260280297Sjkim    }
26159191Skris
262280297Sjkim    if (check && pubin) {
263280297Sjkim        BIO_printf(bio_err, "Only private keys can be checked\n");
264280297Sjkim        goto end;
265280297Sjkim    }
26659191Skris
267280297Sjkim    out = BIO_new(BIO_s_file());
26855714Skris
269280297Sjkim    {
270280297Sjkim        EVP_PKEY *pkey;
27155714Skris
272280297Sjkim        if (pubin) {
273280297Sjkim            int tmpformat = -1;
274280297Sjkim            if (pubin == 2) {
275280297Sjkim                if (informat == FORMAT_PEM)
276280297Sjkim                    tmpformat = FORMAT_PEMRSA;
277280297Sjkim                else if (informat == FORMAT_ASN1)
278280297Sjkim                    tmpformat = FORMAT_ASN1RSA;
279280297Sjkim            } else if (informat == FORMAT_NETSCAPE && sgckey)
280280297Sjkim                tmpformat = FORMAT_IISSGC;
281280297Sjkim            else
282280297Sjkim                tmpformat = informat;
283109998Smarkm
284280297Sjkim            pkey = load_pubkey(bio_err, infile, tmpformat, 1,
285280297Sjkim                               passin, e, "Public Key");
286280297Sjkim        } else
287280297Sjkim            pkey = load_key(bio_err, infile,
288280297Sjkim                            (informat == FORMAT_NETSCAPE && sgckey ?
289280297Sjkim                             FORMAT_IISSGC : informat), 1,
290280297Sjkim                            passin, e, "Private Key");
29155714Skris
292280297Sjkim        if (pkey != NULL)
293280297Sjkim            rsa = EVP_PKEY_get1_RSA(pkey);
294280297Sjkim        EVP_PKEY_free(pkey);
295280297Sjkim    }
29655714Skris
297280297Sjkim    if (rsa == NULL) {
298280297Sjkim        ERR_print_errors(bio_err);
299280297Sjkim        goto end;
300280297Sjkim    }
30155714Skris
302280297Sjkim    if (outfile == NULL) {
303280297Sjkim        BIO_set_fp(out, stdout, BIO_NOCLOSE);
304280297Sjkim# ifdef OPENSSL_SYS_VMS
305280297Sjkim        {
306280297Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
307280297Sjkim            out = BIO_push(tmpbio, out);
308280297Sjkim        }
309280297Sjkim# endif
310280297Sjkim    } else {
311280297Sjkim        if (BIO_write_filename(out, outfile) <= 0) {
312280297Sjkim            perror(outfile);
313280297Sjkim            goto end;
314280297Sjkim        }
315280297Sjkim    }
31655714Skris
317280297Sjkim    if (text)
318280297Sjkim        if (!RSA_print(out, rsa, 0)) {
319280297Sjkim            perror(outfile);
320280297Sjkim            ERR_print_errors(bio_err);
321280297Sjkim            goto end;
322280297Sjkim        }
32355714Skris
324280297Sjkim    if (modulus) {
325280297Sjkim        BIO_printf(out, "Modulus=");
326280297Sjkim        BN_print(out, rsa->n);
327280297Sjkim        BIO_printf(out, "\n");
328280297Sjkim    }
32955714Skris
330280297Sjkim    if (check) {
331280297Sjkim        int r = RSA_check_key(rsa);
33255714Skris
333280297Sjkim        if (r == 1)
334280297Sjkim            BIO_printf(out, "RSA key ok\n");
335280297Sjkim        else if (r == 0) {
336280297Sjkim            unsigned long err;
33755714Skris
338280297Sjkim            while ((err = ERR_peek_error()) != 0 &&
339280297Sjkim                   ERR_GET_LIB(err) == ERR_LIB_RSA &&
340280297Sjkim                   ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
341280297Sjkim                   ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
342280297Sjkim                BIO_printf(out, "RSA key error: %s\n",
343280297Sjkim                           ERR_reason_error_string(err));
344280297Sjkim                ERR_get_error(); /* remove e from error stack */
345280297Sjkim            }
346280297Sjkim        }
34759191Skris
348280297Sjkim        if (r == -1 || ERR_peek_error() != 0) { /* should happen only if r ==
349280297Sjkim                                                 * -1 */
350280297Sjkim            ERR_print_errors(bio_err);
351280297Sjkim            goto end;
352280297Sjkim        }
353280297Sjkim    }
354280297Sjkim
355280297Sjkim    if (noout) {
356280297Sjkim        ret = 0;
357280297Sjkim        goto end;
358280297Sjkim    }
359280297Sjkim    BIO_printf(bio_err, "writing RSA key\n");
360280297Sjkim    if (outformat == FORMAT_ASN1) {
361280297Sjkim        if (pubout || pubin) {
362280297Sjkim            if (pubout == 2)
363280297Sjkim                i = i2d_RSAPublicKey_bio(out, rsa);
364280297Sjkim            else
365280297Sjkim                i = i2d_RSA_PUBKEY_bio(out, rsa);
366280297Sjkim        } else
367280297Sjkim            i = i2d_RSAPrivateKey_bio(out, rsa);
368280297Sjkim    }
369280297Sjkim# ifndef OPENSSL_NO_RC4
370280297Sjkim    else if (outformat == FORMAT_NETSCAPE) {
371280297Sjkim        unsigned char *p, *pp;
372280297Sjkim        int size;
373280297Sjkim
374280297Sjkim        i = 1;
375280297Sjkim        size = i2d_RSA_NET(rsa, NULL, NULL, sgckey);
376280297Sjkim        if ((p = (unsigned char *)OPENSSL_malloc(size)) == NULL) {
377280297Sjkim            BIO_printf(bio_err, "Memory allocation failure\n");
378280297Sjkim            goto end;
379280297Sjkim        }
380280297Sjkim        pp = p;
381280297Sjkim        i2d_RSA_NET(rsa, &p, NULL, sgckey);
382280297Sjkim        BIO_write(out, (char *)pp, size);
383280297Sjkim        OPENSSL_free(pp);
384280297Sjkim    }
385280297Sjkim# endif
386280297Sjkim    else if (outformat == FORMAT_PEM) {
387280297Sjkim        if (pubout || pubin) {
388280297Sjkim            if (pubout == 2)
389280297Sjkim                i = PEM_write_bio_RSAPublicKey(out, rsa);
390280297Sjkim            else
391280297Sjkim                i = PEM_write_bio_RSA_PUBKEY(out, rsa);
392280297Sjkim        } else
393280297Sjkim            i = PEM_write_bio_RSAPrivateKey(out, rsa,
394280297Sjkim                                            enc, NULL, 0, NULL, passout);
395280297Sjkim# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
396280297Sjkim    } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
397280297Sjkim        EVP_PKEY *pk;
398280297Sjkim        pk = EVP_PKEY_new();
399280297Sjkim        EVP_PKEY_set1_RSA(pk, rsa);
400280297Sjkim        if (outformat == FORMAT_PVK)
401280297Sjkim            i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
402280297Sjkim        else if (pubin || pubout)
403280297Sjkim            i = i2b_PublicKey_bio(out, pk);
404280297Sjkim        else
405280297Sjkim            i = i2b_PrivateKey_bio(out, pk);
406280297Sjkim        EVP_PKEY_free(pk);
407280297Sjkim# endif
408280297Sjkim    } else {
409280297Sjkim        BIO_printf(bio_err, "bad output format specified for outfile\n");
410280297Sjkim        goto end;
411280297Sjkim    }
412280297Sjkim    if (i <= 0) {
413280297Sjkim        BIO_printf(bio_err, "unable to write key\n");
414280297Sjkim        ERR_print_errors(bio_err);
415280297Sjkim    } else
416280297Sjkim        ret = 0;
417280297Sjkim end:
418312826Sjkim    release_engine(e);
419280297Sjkim    if (out != NULL)
420280297Sjkim        BIO_free_all(out);
421280297Sjkim    if (rsa != NULL)
422280297Sjkim        RSA_free(rsa);
423280297Sjkim    if (passin)
424280297Sjkim        OPENSSL_free(passin);
425280297Sjkim    if (passout)
426280297Sjkim        OPENSSL_free(passout);
427280297Sjkim    apps_shutdown();
428280297Sjkim    OPENSSL_EXIT(ret);
429280297Sjkim}
430280297Sjkim#else                           /* !OPENSSL_NO_RSA */
431280297Sjkim
43259191Skris# if PEDANTIC
433280297Sjkimstatic void *dummy = &dummy;
43459191Skris# endif
43559191Skris
43655714Skris#endif
437